如何在R中批处理反向地址解析? [英] How to batch reverse geocode in R?

查看:72
本文介绍了如何在R中批处理反向地址解析?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取纽约地区纬度坐标的邮政编码

I am trying to get zipcode for lat-long coordinates in new york region

我尝试使用Google的反向地址解析器API,但每天限制为2500次这样可以批量处理我的数据框。

I tried to use reverse geocoder API from google but its limited to 2500 hits per day so can process my data frame in batch.

接下来,我尝试将库(邮政编码)与数据集的邮政编码一起使用,但无法将纬度经度与火车数据的坐标相匹配

Next, I tried using the library(zipcode) with dataset zip code but could not match latitude longitude with the coordinates of train data set as all lat-long coordinates are not in the dataset.

进一步使用KNN预测数据集的邮政编码,但无法获得正确的结果。

Further though of using KNN to predict zipcode for the dataset but can't get correct results.

zipcode_latlon = zipcode[zipcode$state=="NY",c(1,4,5)]
train_latlon = train_data[,c("latitude","longitude")]
zip1 = rep(10007, nrow(train_latlon))
zip1 = as.character(zip1)
train_latlon = cbind(zip1, train_latlon)
colnames(train_latlon) = c("zip","latitude","longitude")
knn_fit = knn(zipcode_latlon, train_latlon,zipcode_latlon$zip, k=1)

需要知道如何获取zipc

Need to know how I can get zipcodes from lat long in batch, any method would be good in R.

推荐答案

我认为您正在以错误的方式进行操作。您无需地理编码器即可找到纬度/经度坐标的邮政编码-您所需要做的就是下载美国邮政编码形状文件此处,然后进行空间连接:

I think you are going about this the wrong way. You can find the zip codes of lat/lon coordinates without a geocoder - all you need is to download the US zipcodes shapefile here and then do a spatial join:

library(sp)
library(rgdal)

#import zips shapefile and transform CRS 
zips <- readOGR("cb_2015_us_zcta510_500k.shp")
zips <- spTransform(zips, CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))

#here is a sample with three cities in New York State and their coordinates      
df <- as.data.frame(matrix(nrow = 3, ncol =3))
colnames(df) <- c("lat", "lon", "city")

df$lon <- c(43.0481, 43.1610, 42.8864)
df$lat <- c(-76.1474, -77.6109,-78.8784)
df$city <- c("Syracuse", "Rochester", "Buffalo")

df
       lat     lon      city
1 -76.1474 43.0481  Syracuse
2 -77.6109 43.1610 Rochester
3 -78.8784 42.8864   Buffalo

#extract only the lon/lat                   
xy <- df[,c(1,2)]

#transform coordinates into a SpatialPointsDataFrame
spdf <- SpatialPointsDataFrame(coords = xy, data = df, proj4string = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))

#subset only the zipcodes in which points are found
zips_subset <- zips[spdf, ]

#NOTE: the column in zips_subset containing zipcodes is ZCTA5CE10
#use over() to overlay points in polygons and then add that to the original dataframe

df$zip <- over(spdf, zips_subset[,"ZCTA5CE10"])

瞧!您具有每个点的邮政编码

And voila! You have the zipcode of each point

df
       lat     lon      city ZCTA5CE10
1 -76.1474 43.0481  Syracuse     13202
2 -77.6109 43.1610 Rochester     14604
3 -78.8784 42.8864   Buffalo     14202

这篇关于如何在R中批处理反向地址解析?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆