将邮编或经纬度转换为县/区

31

我有一个地点列表,其中包含每个地点的城市、州、邮政编码、纬度和经度。

我另外有一个县级经济指标列表。我已经尝试使用zipcode包、ggmap包和几个免费的地理编码网站,包括美国地名词典文件,但似乎找不到将这两个数据匹配的方法。

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


这有帮助吗?http://gis.stackexchange.com/questions/28035/how-to-convert-zip-codes-to-latitude-and-longitude - thelatemail
2
对于经纬度数据,您可能可以使用我提供的代码进行调整[这里](https://dev59.com/CWoy5IYBdhLWcg3wJKoq#8751965),以回答有关如何将经纬度坐标转换为州代码的问题。 - Josh O'Brien
@JoshO'Brien:看起来可能会起作用。他们似乎有美国所有县的地图。我得编写代码来验证一下。谢谢你的建议。 - screechOwl
1
@JoshO'Brien:好主意。我刚把“state”改成了“county”,然后它完美地工作了。非常感谢你。 - screechOwl
@JoshO'Brien:你想把那个放在答案里,然后我选择它? - screechOwl
1
哦,你真是太好了,但我不需要了。我很高兴得知这种方法在使用maps县级数据库和州级数据库时都能奏效。不过,你可以在几天后发布你所用的代码,并接受它,这将非常合适。我肯定会点赞一个真正的答案,而且对于未来的搜索者来说也可能会有帮助。干杯! - Josh O'Brien
4个回答

24

我最终使用了上面提到的JoshO'Brien的建议,并在这里找到了答案。

我采用了他的代码,并将state更改为county,如下所示:

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

2
你好,我该如何修改这段代码以将经纬度转换为FIPS代码?谢谢。 - Geekuna Matata
1
这段代码给我一个“未知椭圆参数”错误,但是当我在SpatialPoints和map2SpatialPolygons中删除proj4string时,它对我起作用了。也许是包的更新?感谢您编写这个! - bpheazye
你有任何想法为什么有些地方没有被覆盖吗?例如:Calexico CA: testPoints <- data.frame(y =32.67138 , x = -115.5031) latlong2county(testPoints) - MatthewR

10

将邮政编码与县匹配是困难的。(某些邮政编码跨越多个县,有时甚至跨越多个州。例如30165)

我不知道是否有任何特定的R软件包可以为您匹配它们。

但是,您可以从密苏里人口普查数据中心获得一个好的表格。
您可以使用此页面进行数据提取。

示例输出可能如下所示:

state,zcta5,ZIPName,County,County2
01,30165,"Rome, GA",Cherokee AL,
01,31905,"Fort Benning, GA",Russell AL,
01,35004,"Moody, AL",St. Clair AL,
01,35005,"Adamsville, AL",Jefferson AL,
01,35006,"Adger, AL",Jefferson AL,Walker AL
...

请注意County2。可以在此处找到元数据说明。

county 
The county in which the ZCTA is all or mostly contained. Over 90% of ZCTAs fall entirely within a single county.

county2 
The "secondary" county for the ZCTA, i.e. the county which has the 2nd largest intersection with it. Over 90% of the time this value will be blank.

请参阅 ANSI 县级代码 http://www.census.gov/geo/www/ansi/ansi.html


7

我认为 "noncensus" 这个包很有帮助。

"corresponding" 是我用来将邮政编码与县份匹配的。

### code for get county based on zipcode

library(noncensus)
data(zip_codes)
data(counties)

state_fips  = as.numeric(as.character(counties$state_fips))
county_fips = as.numeric(as.character(counties$county_fips))    
counties$fips = state_fips*1000+county_fips    
zip_codes$fips =  as.numeric(as.character(zip_codes$fips))

# test
temp = subset(zip_codes, zip == "30329")    
subset(counties, fips == temp$fips)

2
你的前三行代码,建立counties$fips变量,可以使用以下单行代码完成:counties$fips <- interaction(counties$state_fips, counties$county_fips) - lmo
3
似乎非普查包已于2020年1月16日从CRAN中删除。 - ChristinaP

4

一个简单的选项是使用ggmap中的geocode()函数,选项为output="more"output="all"

它可以接受灵活的输入,如地址或lat/lon,并将地址、城市、县、州、国家、邮政编码等作为列表返回。

require("ggmap")
address <- geocode("Yankee Stadium", output="more")

str(address)
$ lon                        : num -73.9
$ lat                        : num 40.8
$ type                       : Factor w/ 1 level "stadium": 1
$ loctype                    : Factor w/ 1 level "approximate": 1
$ address                    : Factor w/ 1 level "yankee stadium, 1 east 161st street, bronx, ny 10451, usa": 1
$ north                      : num 40.8
$ south                      : num 40.8
$ east                       : num -73.9
$ west                       : num -73.9
$ postal_code                : chr "10451"
$ country                    : chr "united states"
$ administrative_area_level_2: chr "bronx"
$ administrative_area_level_1: chr "ny"
$ locality                   : chr "new york"
$ street                     : chr "east 161st street"
$ streetNo                   : num 1
$ point_of_interest          : chr "yankee stadium"
$ query                      : chr "Yankee Stadium"

另一种解决方案是使用人口普查的shapefile和与问题中相同的over()命令。我在使用maptools基础地图时遇到了一个问题:由于它使用WGS84 datum,在北美,距离海岸线几英里的点被错误地映射,我的数据集中约5%的数据不匹配。请尝试使用sp包和Census TIGERLine shape文件。
counties <- readShapeSpatial("maps/tl_2013_us_county.shp", proj4string=CRS("+proj=longlat +datum=NAD83"))

# Convert pointsDF to a SpatialPoints object 
pointsSP <- SpatialPoints(pointsDF, proj4string=CRS("+proj=longlat +datum=NAD83"))

countynames <- over(pointsSP, counties)
countynames <- countynames$NAMELSAD

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接