2015-01-09 82 views
0

這可能是一個相當隨機/隱晦的問題,但它是否包含一個函數,其中包含一個將美國CityState(例如「CA」)輸入轉換爲字符串並返回經度&緯度座標?從城市和州輸入獲取經度和緯度的快速方法

+0

Google maps api? – 2015-01-09 19:35:43

+0

也許您可以使用此網站的網絡服務http://www.gpsvisualizer.com/geocoder/ – ckluss 2015-01-09 19:35:47

+1

請參閱https://stackoverflow.com/questions/3257441/geocoding-in-r-with-google-maps或http://blog.corynissen.com/2014/10/making-r-package-to-use-here-geocode-api.html或https://stackoverflow.com/questions/22887833/r-how-to- geocode-a-simple-address-using-data-science-toolbox – ckluss 2015-01-09 19:36:46

回答

3
https://maps.googleapis.com/maps/api/geocode/json?address={city},{state} 

......當然可以用您正在搜索的城市/州替換{city}和{state}。它返回一個JSON字符串,所以你可以通過ajax進行這個調用並執行JS處理。

5

UPDATE

或者你可以做一個devtools::install_github("hrbrmstr/localgeo")並運行它geocode(剛建的話)。它沒有rgdal,rgeoshttr依賴關係,只有dplyr

而且this place有ZIP /市/州/經度的自由CSV文件/ LAT你可以只matchdplyr::left_join


無需使用API​​:

library(rgeos) 
library(rgdal) 
library(httr) 
library(dplyr) 

# httr's write_disk can act like a cache as it won't download if 
# the file exists 

GET("http://www.mapcruzin.com/fcc-wireless-shapefiles/cities-towns.zip", 
    write_disk("cities.zip")) 
unzip("cities.zip", exdir="cities") 

# read in the shapefile 
shp <- readOGR("cities/citiesx020.shp", "citiesx020") 

# extract the city centroids with name and state 

geo <- 
    gCentroid(shp, byid=TRUE) %>% 
    data.frame() %>% 
    rename(lon=x, lat=y) %>% 
    mutate([email protected]$NAME, [email protected]$STATE) 

# lookup! 

geo %>% filter(city=="Portland", state=="ME") 

##   lon  lat  city state 
## 1 -70.25404 43.66186 Portland ME 

geo %>% filter(city=="Berwick", state=="ME") 

##   lon  lat city state 
## 1 -70.86323 43.26593 Berwick ME 

有可能用這些屬性來更加全面的形狀文件。這個城鎮有28,706個城市,所以看起來相當全面。

那些可以很容易地在函數中,使用更簡便:

geo_init <- function() { 

    try({ 
    GET("http://www.mapcruzin.com/fcc-wireless-shapefiles/cities-towns.zip", 
     write_disk("cities.zip")) 
    unzip("cities.zip", exdir="cities") }) 

    shp <- readOGR("cities/citiesx020.shp", "citiesx020") 

    geo <- 
    gCentroid(shp, byid=TRUE) %>% 
    data.frame() %>% 
    rename(lon=x, lat=y) %>% 
    mutate([email protected]$NAME, [email protected]$STATE) 

} 

geocode <- function(geo_db, city, state) { 
    do.call(rbind.data.frame, mapply(function(x, y) { 
    geo_db %>% filter(city==x, state==y) 
    }, city, state, SIMPLIFY=FALSE)) 
} 


geo_db <- geo_init() 

geo_db %>% geocode("Portland", "ME") 

##    lon  lat  city state 
## Portland -70.25404 43.66186 Portland ME 

geo_db %>% 
    geocode(c("Portland", "Berwick", "Alfred"), "ME") 

##    lon  lat  city state 
## Portland -70.25404 43.66186 Portland ME 
## Berwick -70.86323 43.26593 Berwick ME 
## Alfred -70.71754 43.47681 Alfred ME 

geo_db %>% 
    geocode(city=c("Baltimore", "Pittsburgh", "Houston"), 
      state=c("MD", "PA", "TX")) 

##     lon  lat  city state 
## Baltimore -76.61158 39.29076 Baltimore MD 
## Pittsburgh -79.99538 40.44091 Pittsburgh PA 
## Houston -95.36400 29.76376 Houston TX 
2

擴大@ ZacWolf的答案,你可以做到這一點使用我的googleway包和谷歌地圖API(您需要一個API密鑰)

library(googleway) 

## your api key 
key <- read.dcf("~/Documents/.googleAPI", fields = "GOOGLE_API_KEY") 

google_geocode(address = "Los Angeles, California", key = key) 

# $results 

# address_components 
# 1 Los Angeles, Los Angeles County, California, United States, Los Angeles, Los Angeles County, CA, US, locality, political, administrative_area_level_2, political, administrative_area_level_1, political, country, political 
# formatted_address geometry.bounds.northeast.lat geometry.bounds.northeast.lng geometry.bounds.southwest.lat geometry.bounds.southwest.lng 
# 1 Los Angeles, CA, USA      34.33731      -118.1553      33.70369      -118.6682 
# geometry.location.lat geometry.location.lng geometry.location_type geometry.viewport.northeast.lat geometry.viewport.northeast.lng 
# 1    34.05223    -118.2437   APPROXIMATE      34.33731      -118.1553 
# geometry.viewport.southwest.lat geometry.viewport.southwest.lng     place_id    types 
# 1      33.70369      -118.6682 ChIJE9on3F3HwoAR9AhGJW_fL-I locality, political 

如果你有多個城市/州

df <- data.frame(city = c("Portland", "Houston", "Pittsburg"), 
           state = c("ME", "TX", "PA")) 


res <- apply(df, 1, function(x) { 
    google_geocode(address = paste0(x["city"], ", ", x["state"]), key = key) 
}) 


lapply(res, function(x) x[['results']][['geometry']][['location']]) 

# [[1]] 
# lat  lng 
# 1 43.66147 -70.25533 
# 
# [[2]] 
# lat  lng 
# 1 29.76043 -95.3698 
# 
# [[3]] 
# lat  lng 
# 1 40.44062 -79.99589 
+0

...而反對票是因爲? – SymbolixAU 2016-11-12 11:24:05

1

ggmap::geocode包裝了谷歌和數據科學工具包的地理編碼的API整齊:

df <- data.frame(city = c('Washington', 'Los Angeles'), 
       state = c('DC', 'CA')) 

df <- cbind(df, geocode(paste(df$city, df$state))) 

df 
##   city state  lon  lat 
## 1 Washington DC -77.03687 38.90719 
## 2 Los Angeles CA -118.24368 34.05223 

它還包括一個mutate_geocode版本,如果你喜歡dplyr語法,雖然詳細地址已被完全組裝爲列:

library(dplyr) 

df <- df %>% mutate(address = paste(city, state)) %>% mutate_geocode(address) 

df 
##   city state  address  lon  lat 
## 1 Washington DC Washington DC -77.03687 38.90719 
## 2 Los Angeles CA Los Angeles CA -118.24368 34.05223 
相關問題