纬度经度坐标到R的国家代码

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

如果不是在R是有什么办法做到这一点使用谷歌地理编码器或任何其他types的快速查询服务?

谢谢!

这里是一个函数,它在下48个状态中获取lat-long的data.frame,并为每个点返回它所在的状态。

大部分函数只是准备sp包中的over()函数所需的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(states_sp@polygons, function(x) x@ID) 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 

你可以在几行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包中查看。 您需要将状态边界设置为SpatialPolygonDataFrame。

Interesting Posts