纬度经度坐标到R中的州代码

问题描述:

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

如果不是在R有任何方式使用谷歌地理编码器或任何其他类型快速查询服务?

谢谢!

+0

这里也看到我的答案,使用'ggmap :: revgeocode':https://*.com/questions/46150851/how-to-get-california-县位置从-LAT itude-and-long-information/46151310#46151310 – 2017-09-11 08:51:12

这是一个函数,它在低48个状态内采用lat-long的data.frame,并为每个点返回它所在的状态。

大部分功能的简单准备由spover()函数,该函数计算的点和面的“交集”的真正繁重所需的SpatialPointsSpatialPolygons对象:

library(sp) 
library(maps) 
library(maptools) 

# The single argument to this function, pointsDF, is a data.frame in which: 
# - column 1 contains the longitude in degrees (negative in the US) 
# - column 2 contains the latitude in degrees 

latlong2state <- function(pointsDF) { 
    # Prepare SpatialPolygons object with one SpatialPolygon 
    # per state (plus DC, minus HI & AK) 
    states <- map('state', fill=TRUE, col="transparent", plot=FALSE) 
    IDs <- sapply(strsplit(states$names, ":"), function(x) x[1]) 
    states_sp <- map2SpatialPolygons(states, IDs=IDs, 
        proj4string=CRS("+proj=longlat +datum=WGS84")) 

    # Convert pointsDF to a SpatialPoints object 
    pointsSP <- SpatialPoints(pointsDF, 
        proj4string=CRS("+proj=longlat +datum=WGS84")) 

    # Use 'over' to get _indices_ of the Polygons object containing each point 
    indices <- over(pointsSP, states_sp) 

    # Return the state names of the Polygons object containing each point 
    stateNames <- sapply([email protected], function(x) [email protected]) 
    stateNames[indices] 
} 

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

latlong2state(testPoints) 
[1] "wisconsin" "oregon" # IT WORKS 
+2

我必须将wgs84更改为WGS84才能使此示例正常工作。 – lever 2016-03-08 23:11:19

+0

@lever感谢您指出。不知道什么时候(以及哪里)发生了变化。无论如何,我现在编辑来解决它。 – 2016-03-08 23:17:27

请参阅sp包中的内容。您需要将状态边界作为SpatialPolygonDataFrame。

你能做到这一点的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) 
+0

谢谢!这更简单:) – 2016-08-04 13:18:02