我如何将lat-long转换为县?

时间:2015-07-21 16:10:14

标签: r

作为较长代码的一部分,我将此行从lat-long转换为county。

county<-latlong2county(latlon)

其中

dput(droplevels(head(latlon, 4)))

structure(list(alldata.lon = c(-71.566389, -71.566389, -71.566389, 
-71.566389), alldata.lat = c(41.69, 41.69, 41.69, 41.69)), .Names = c("alldata.lon", 
"alldata.lat"), row.names = c(NA, 4L), class = "data.frame")

一个月前工作正常。但现在我得到一个错误:

Error in CRS("+proj=longlat +datum=wgs84") : 
  unknown elliptical parameter name

不确定发生了什么变化。也许是原始地图包里的东西。你能帮忙吗?

功能

latlong2county <- function(pointsDF) {
  # Prepare SpatialPolygons object with one SpatialPolygon
  # per state (plus DC, minus HI & AK)
  states <- map('county', 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]
}

0 个答案:

没有答案