簡體   English   中英

如何在R中沒有問題的情況下將Long-Long轉換為FIPS縣代碼?

[英]How can I convert Lat-Long to FIPS county code without problems in R?

我有成千上萬的緯度點,我使用此代碼將其轉換為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)])

但是,這樣做的問題是它沒有將所有經緯度轉換為FIPS代碼。 我得到合法縣的NA值。 有人可以幫忙嗎?

還是只是提出其他解決方案?

盡管此解決方案不是基於python的,但您可以使用ruby和geokit gem預處理數據,然后通過csv文件或其他方式將其傳遞給R。

看到這寫了進一步的說明。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM