如何将Lat-Long转换为FIPS县代码而没有R问题?

时间:2014-04-15 18:43:19

标签: python r geospatial spatial

我有数千个Lat-long点,我使用此代码将其转换为FIPS县代码。

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]
}

data(county.fips)
latlon <- data.frame(all$lon,all$lat)
county<-latlong2county(latlon)
fips<-with(county.fips, fips[match(county, polyname)])

但是,问题在于它没有将所有lat-long转换为FIPS代码。我得到合法县的NA值。任何人都可以帮忙吗?

或者只是建议一个不同的解决方案?

1 个答案:

答案 0 :(得分:1)

虽然此解决方案不是基于python的,但您可以使用ruby和geokit gem来预处理数据,然后通过csv文件或其他方式将其传递给R.

有关详细说明,请参阅此write-up