识别纬度和纵坐标内的邮政编码

Jes*_*sie 2 geocoding r polygon latitude-longitude coordinates

我在R中有几个数据帧.第一个数据帧包含市场上一组lat和long坐标的计算凸包(由R中的chull提供).它看起来像这样:

MyGeo<- "Part of Chicago & Wisconsin"
Longitude <- c(-90.31914,  -90.61911,  -89.37842,  -88.0988,  -87.44875)
Latitude <- c(38.45781, 38.80097, 43.07961, 43.0624,41.49182)

dat <- data.frame(Longitude, Latitude, MyGeo)
Run Code Online (Sandbox Code Playgroud)

第二个是纬度和纵坐标的邮政编码(美国人口普查网站提供).它看起来像这样:

CensuseZip <- c("SomeZipCode1","SomeZipCode2","SomeZipCode3","SomeZipCode4","SomeZipCode5","SomeZipCode6","SomeZipCode7") 
Longitude2 <- c(-131.470425,-133.457924,-131.693453,-87.64957,-87.99734,-87.895,-88.0228)
Latitude2 <- c(55.138352,56.239062,56.370538,41.87485,42.0086,42.04957,41.81055)

cen <- data.frame(Longitude2, Latitude2,   CensuseZip)
Run Code Online (Sandbox Code Playgroud)

现在我相信第一个数据表为我提供了一个多边形或边框,我应该可以使用它来识别属于该边界的邮政编码.理想情况下,我想创建一个类似于下面的第三个数据表:

 Longitude2 Latitude2    CensusZip                        MyGeo
-131.470425 55.138352 SomeZipCode1  
-133.457924 56.239062 SomeZipCode2  
-131.693453 56.370538 SomeZipCode3
-87.64957    41.87485 SomeZipCode4  Part of Chicago & Wisconsin 
-87.99734     42.0086 SomeZipCode5  Part of Chicago & Wisconsin 
-87.895      42.04957 SomeZipCode6  Part of Chicago & Wisconsin 
-88.0228     41.81055 SomeZipCode7  Part of Chicago & Wisconsin 
Run Code Online (Sandbox Code Playgroud)

本质上,我希望识别所有位于蓝色(见下面的可点击图片)长点和纬点之间的邮政编码.虽然它在下面可视化,但我实际上正在寻找上述表格.

数据的可视化表示

但是......我在这方面遇到了麻烦...我尝试使用以下软件包和脚本:

library(rgeos)
library(sp)
library(rgdal)

coordinates(dat) <- ~ Longitude + Latitude
coordinates(cen) <- ~ Longitude2 + Latitude2

over(cen, dat)
Run Code Online (Sandbox Code Playgroud)

但我收到了所有NA的.

Sym*_*xAU 5

library(sf)用来解决这种类型的多边形点问题(sf是继承者sp).

该函数sf::st_intersection()为您提供两个sf对象的交集.在您的情况下,您可以构造单独的POLYGON和POINT sf对象.

library(sf)

Longitude <- c(-90.31914,  -90.61911,  -89.37842,  -88.0988,  -87.44875)
Latitude <- c(38.45781, 38.80097, 43.07961, 43.0624,41.49182)

## closing the polygon
Longitude[length(Longitude) + 1] <- Longitude[1]
Latitude[length(Latitude) + 1] <- Latitude[1]

## construct sf POLYGON
sf_poly <- sf::st_sf( geometry = sf::st_sfc( sf::st_polygon( x = list(matrix(c(Longitude, Latitude), ncol = 2)))) )

## construct sf POINT
sf_points <- sf::st_as_sf( cen, coords = c("Longitude2", "Latitude2"))

sf::st_intersection(sf_points, sf_poly)

# Simple feature collection with 4 features and 1 field
# geometry type:  POINT
# dimension:      XY
# bbox:           xmin: -88.0228 ymin: 41.81055 xmax: -87.64957 ymax: 42.04957
# epsg (SRID):    NA
# proj4string:    NA
# CensuseZip                   geometry
# 4 SomeZipCode4 POINT (-87.64957 41.87485)
# 5 SomeZipCode5  POINT (-87.99734 42.0086)
# 6 SomeZipCode6   POINT (-87.895 42.04957)
# 7 SomeZipCode7  POINT (-88.0228 41.81055)
# Warning message:
#   attribute variables are assumed to be spatially constant throughout all geometries 
Run Code Online (Sandbox Code Playgroud)

结果是多边形内的所有点


您也可以使用sf::st_join(sf_poly, sf_points)相同的结果


并且,该函数sf::st_intersects(sf_points, sf_poly)将返回一个列表,说明给定的POINT是否在多边形内

sf::st_intersects(sf_points, sf_poly)

# Sparse geometry binary predicate list of length 7, where the predicate was `intersects'
#  1: (empty)
# 2: (empty)
# 3: (empty)
# 4: 1
# 5: 1
# 6: 1
# 7: 1
Run Code Online (Sandbox Code Playgroud)

您可以将其用作原始sf_points对象的索引/标识符以添加新列

is_in <- sf::st_intersects(sf_points, sf_poly)

sf_points$inside_polygon <- as.logical(is_in)

sf_points
# Simple feature collection with 7 features and 2 fields
# geometry type:  POINT
# dimension:      XY
# bbox:           xmin: -133.4579 ymin: 41.81055 xmax: -87.64957 ymax: 56.37054
# epsg (SRID):    NA
# proj4string:    NA
# CensuseZip                   geometry inside_polygon
# 1 SomeZipCode1 POINT (-131.4704 55.13835)             NA
# 2 SomeZipCode2 POINT (-133.4579 56.23906)             NA
# 3 SomeZipCode3 POINT (-131.6935 56.37054)             NA
# 4 SomeZipCode4 POINT (-87.64957 41.87485)           TRUE
# 5 SomeZipCode5  POINT (-87.99734 42.0086)           TRUE
# 6 SomeZipCode6   POINT (-87.895 42.04957)           TRUE
# 7 SomeZipCode7  POINT (-88.0228 41.81055)           TRUE
Run Code Online (Sandbox Code Playgroud)