获得经度的快捷方式来自City&的纬度国家输入

时间:2015-01-09 19:32:22

标签: r

这可能是一个相当随机/模糊的问题,但是有一个包含其中包含函数的包,它将美国CityState(例如“CA”)输入转换为字符串并返回经度与经济纬度坐标?

4 个答案:

答案 0 :(得分:5)

更新

或者您可以执行devtools::install_github("hrbrmstr/localgeo")并从中运行geocode(刚刚构建它)。它没有rgdalrgeoshttr个依赖关系,只有dplyr

另外,this place有一个ZIP / City / State / lon / lat的免费CSV文件,您可以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(city=shp@data$NAME, state=shp@data$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

可能有更全面的shapefile具有这些属性。这个有28,706个城市&amp;城镇,所以看起来非常全面。

这些可以很容易地包装到函数中以便于使用:

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(city=shp@data$NAME, state=shp@data$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

答案 1 :(得分:3)

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

...当然将{city}和{state}替换为您正在搜索的城市/州。它返回一个JSON字符串,因此您可以通过ajax进行此调用并执行JS处理。

答案 2 :(得分:2)

扩展@ZacWolf的答案,你可以使用我的googleway软件包和Google Maps 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

答案 3 :(得分:1)

ggmap::geocode整齐地包装了Google和Data Science Toolkit地理编码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