R将zipcode或lat / long转换为县 [英] R convert zipcode or lat/long to county

查看:146
本文介绍了R将zipcode或lat / long转换为县的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我分别列出了县级。我玩过 zipcode 包, ggmap 包以及其他几个免费地理编码网站,包括美国Gazeteer文件,但似乎无法找到匹配这两个部分的方法。



目前是否有任何软件包或其他来源能够做到这一点? 我最终使用了上面提到的来自 JoshO'Brien 的建议,并发现这里



我拿了他的代码,并改变了如下所示:

  library(sp)
library(maps)
library(maptools)

#这个函数的唯一参数pointsDF是一个data.frame,其中:
# - 第1列包含以度为单位的经度(在美国为负值)
# - 第2列包含以度为单位的纬度

latlong2county< - function(pointsDF){
#使用一个SpatialPolygon准备SpatialPolygons对象
#每个县
个县< - map('county',fill = TRUE,col =transparent,plot = FALSE)
IDs < SA pply(strsplit(counties $ names,:),function(x)x [1])$ ​​b $ b counties_sp < - map2SpatialPolygons(counties,IDs = IDs,
proj4string = CRS(+ proj = long points + datum = wgs84))

#将pointsDF转换为SpatialPoints对象
pointsSP< - SpatialPoints(pointsDF,
proj4string = CRS(+ proj = longlat + datum = wgs84))

#使用'over'来获得包含每个点的Polygons对象的_indices_
indices < - over(pointsSP,counties_sp)

#返回包含每个点的Polygons对象的县名
countyNames< - sapply(counties_sp @ polygons,function(x)x @ ID)
countyNames [indices]
}

#在威斯康星州和俄勒冈州测试使用点的功能。
testPoints< - data.frame(x = c(-90,-120),y = c(44,44))

latlong2county(testPoints)
[1 ]威斯康辛,朱诺俄勒冈,骗子#它工作


I have a list of locations that contains a city, state, zip, latitude and longitude for each location.

I separately have a list of economic indicators at the county level. I've played with the zipcode package, the ggmap package, and several other free geocoding websites including the US Gazeteer files, but can't seem to find a way to match the two pieces.

Are there currently any packages or other sources that do this?

解决方案

I ended up using the suggestion from JoshO'Brien mentioned above and found here.

I took his code and changed state to county as shown here:

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将zipcode或lat / long转换为县的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆