Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
纬度经度坐标到状态代码(R)_R_Latitude Longitude_Google Geocoder - Fatal编程技术网

纬度经度坐标到状态代码(R)

纬度经度坐标到状态代码(R),r,latitude-longitude,google-geocoder,R,Latitude Longitude,Google Geocoder,有没有一种快速的方法将纬度和经度坐标转换为R中的州代码?我一直在使用zipcode包作为查找表,但当我查询大量lat/long值时,它太慢了 如果不是在R中,是否有任何方法可以使用google geocoder或任何其他类型的快速查询服务来做到这一点 谢谢 请参见sp软件包中的 您需要将状态边界作为空间多边形数据帧。这里有两个选项,一个使用sf,另一个使用sp包函数sf是用于分析空间数据的更现代(并且在2020年推荐)的软件包,但如果它仍然有用,我将留下我2012年的原始答案,说明如何使用sp相

有没有一种快速的方法将纬度和经度坐标转换为R中的州代码?我一直在使用zipcode包作为查找表,但当我查询大量lat/long值时,它太慢了

如果不是在R中,是否有任何方法可以使用google geocoder或任何其他类型的快速查询服务来做到这一点


谢谢

请参见sp软件包中的


您需要将状态边界作为空间多边形数据帧。

这里有两个选项,一个使用sf,另一个使用sp包函数sf是用于分析空间数据的更现代(并且在2020年推荐)的软件包,但如果它仍然有用,我将留下我2012年的原始答案,说明如何使用sp相关功能实现这一点


方法1(使用sf):
库(sf)
图书馆(spData)
##pointsDF:第一列包含经度和宽度的data.frame
##其第二列包含纬度。
##
##状态:具有50个状态和DC的sf多多边形对象。
##
##name_col:“states”中提供州名称的列的名称
##名字。

lonlat_to_state你可以用几行R来完成

library(sp)
library(rgdal)
#lat and long
Lat <- 57.25
Lon <- -9.41
#make a data frame
coords <- as.data.frame(cbind(Lon,Lat))
#and into Spatial
points <- SpatialPoints(coords)
#SpatialPolygonDataFrame - I'm using a shapefile of UK counties
counties <- readOGR(".", "uk_counties")
#assume same proj as shapefile!
proj4string(points) <- proj4string(counties)
#get county polygon point is in
result <- as.character(over(points, counties)$County_Name)
库(sp)
图书馆(rgdal)
#纵横
Lat示例数据(多边形和点)


使用以下方法非常简单:

库(地图)
图书馆(sf)
##获取状态图,转化为sf对象

我们必须将wgs84更改为wgs84才能使此示例正常工作。@感谢您指出这一点。不知道什么时候(在哪里)发生了变化。无论如何,我现在已经进行了编辑以修复它。@AgustínIndaco不快,因为在我的代码中,州的多边形层是由maps包提供的,它没有相应的邮政编码边界层。如果您找到这样一个层,您当然可以修改我的代码来使用它。或者,您可能希望研究“反向地理编码”,例如,我发现这个答案产生的结果对于某些应用程序来说不够精确。例如,
38.83226,-76.98946
编码为马里兰,而不是哥伦比亚特区。而
34.97982,-85.42203
编码为田纳西州,而不是乔治亚州。如果你像我一样处理15000个点,这个方法会产生很多不正确的结果(我估计我处理的数据集中大约有900个)。不过,我不确定哪种解决方案更好。通过将“州”改为“县”,这也适用于县。请参见我的回答,使用
ggmap::revgeocode
library(raster)
pols <- shapefile(system.file("external/lux.shp", package="raster"))
xy <- coordinates(p)
extract(p, xy)

#   point.ID poly.ID ID_1       NAME_1 ID_2           NAME_2 AREA
#1         1       1    1     Diekirch    1         Clervaux  312
#2         2       2    1     Diekirch    2         Diekirch  218
#3         3       3    1     Diekirch    3          Redange  259
#4         4       4    1     Diekirch    4          Vianden   76
#5         5       5    1     Diekirch    5            Wiltz  263
#6         6       6    2 Grevenmacher    6       Echternach  188
#7         7       7    2 Grevenmacher    7           Remich  129
#8         8       8    2 Grevenmacher   12     Grevenmacher  210
#9         9       9    3   Luxembourg    8         Capellen  185
#10       10      10    3   Luxembourg    9 Esch-sur-Alzette  251
#11       11      11    3   Luxembourg   10       Luxembourg  237
#12       12      12    3   Luxembourg   11           Mersch  233
library(maps)
library(sf)

## Get the states map, turn into sf object
US <- st_as_sf(map("state", plot = FALSE, fill = TRUE))

## Test the function using points in Wisconsin and Oregon
testPoints <- data.frame(x = c(-90, -120), y = c(44, 44))

# Make it a spatial dataframe, using the same coordinate system as the US spatial dataframe
testPoints <- st_as_sf(testPoints, coords = c("x", "y"), crs = st_crs(US))

#.. and perform a spatial join!
st_join(testPoints, US)


         ID        geometry
1 wisconsin  POINT (-90 44)
2    oregon POINT (-120 44)