python - How can I convert Lat-Long to FIPS county code without problems in R? -
i have thousands of lat-long points , used code convert fips county codes.
latlong2county <- function(pointsdf) { # prepare spatialpolygons object 1 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 spatialpoints object pointssp <- spatialpoints(pointsdf, proj4string=crs("+proj=longlat +datum=wgs84")) # use 'over' _indices_ of polygons object containing each point indices <- over(pointssp, states_sp) # return state names of 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)]) but, problem is not converting lat-longs fips codes. na values legitimate counties. can it?
or suggest different solution?
although solution isn't python-based, use ruby , geokit gem pre-process data, pass r via csv file or other means.
see write-up further instructions.
Comments
Post a Comment