Drawing Lines on Google Map with R

From Address to Geographical Coordinates

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import requests
import json
def getGeoForAddress(address):
    proxy = {
        'https': 'https://127.0.0.1:1087',
        'http': 'http://127.0.0.1:1087'
    }
    addressUrl = "https://maps.googleapis.com/maps/api/geocode/json?address=%s&key=..."%address
    BROWSER_HEADERS = {
                        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 ' +
                                      '(KHTML, like Gecko) Chrome/50.0.2661.86 Safari/537.36'
                    }
    r = requests.get(addressUrl,proxies = proxy,headers=BROWSER_HEADERS, timeout=5)
    t1=json.loads(r.text)
    lat = t1['results'][0]['geometry']['location']['lat']  
    lon = t1['results'][0]['geometry']['location']['lng']
    return [lat,lon]

From Geographical Coordinates to Lines on Map

library(ggmap)

map = get_map(location='China', zoom=5, maptype="roadmap", api_key="...")

ggmap(map) + geom_point(data=city, aes(x=lon, y=lat), colour='red4', alpha=0.8) + geom_path(data=cityline, aes(x=lon, y=lat, group=group),arrow=arrow(angle=2, ends="first", type="closed"), size=0.1, alpha=0.05, color='red4')

The Result

map_propagation

More

install map package first if not already,

install.packages("ggmap")

configure r proxy because of GFW of China,

file.edit('~/.Renviron')

with content as following,

http_proxy=http://proxy.dom.com/
http_proxy_user=user:passwd

https_proxy=https://proxy.dom.com/
https_proxy_user=user:passwd

* cached version, generated at 2019-06-22 03:45:51 UTC.

Subscribe by RSS