gpt4 book ai didi

R将邮政编码或纬度/经度转换为县

转载 作者:行者123 更新时间:2023-12-03 11:17:18 25 4
gpt4 key购买 nike

我有一个位置列表,其中包含每个位置的城市、州、 zip 、纬度和经度。

我另外有一份县级经济指标 list 。我玩过 zipcode包,ggmap package 和其他几个免费地理编码网站,包括美国地名文件,但似乎无法找到匹配这两部分的方法。

目前是否有任何软件包或其他来源可以做到这一点?

最佳答案

我最终使用了来自 JoshO'Brien 的建议上面提到并找到here .

我拿了他的代码改了statecounty如图所示:

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

latlong2county <- function(pointsDF) {
# Prepare SpatialPolygons object with one SpatialPolygon
# per county
counties <- map('county', fill=TRUE, col="transparent", plot=FALSE)
IDs <- sapply(strsplit(counties$names, ":"), function(x) x[1])
counties_sp <- map2SpatialPolygons(counties, 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, counties_sp)

# Return the county names of the Polygons object containing each point
countyNames <- sapply(counties_sp@polygons, function(x) x@ID)
countyNames[indices]
}

# Test the function using points in Wisconsin and Oregon.
testPoints <- data.frame(x = c(-90, -120), y = c(44, 44))

latlong2county(testPoints)
[1] "wisconsin,juneau" "oregon,crook" # IT WORKS

关于R将邮政编码或纬度/经度转换为县,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13316185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com