gpt4 book ai didi

r - 识别位于纬度和经度坐标内的邮政编码

转载 作者:行者123 更新时间:2023-12-04 12:02:34 26 4
gpt4 key购买 nike

我在 R 中有几个数据框。第一个数据框包含按市场计算的一组纬度和经度坐标的凸包(由 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)

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

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)

现在我相信第一个数据表为我提供了一个多边形或边界,我应该能够使用它来识别位于该边界内的邮政编码。理想情况下,我想创建第三个数据表,如下所示:

 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

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

visual representation of data

但是...我在执行此操作时遇到了麻烦...我尝试使用以下软件包和脚本:

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

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

over(cen, dat)

但我收到了所有NA

最佳答案

我使用library(sf)来解决这种类型的多边形点问题(sfsp的后继者)。

函数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

结果是多边形内的所有点


您还可以使用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

您可以将其用作原始 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

关于r - 识别位于纬度和经度坐标内的邮政编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53292423/

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