【问题标题】:identify zip codes that fall within latitude and longitudinal coordinates识别属于纬度和经度坐标的邮政编码
【发布时间】:2019-04-16 23:02:45
【问题描述】:

我在 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 

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

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

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

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

over(cen, dat)

但我收到所有NAs。

【问题讨论】:

    标签: r coordinates polygon geocoding latitude-longitude


    【解决方案1】:

    我使用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
    

    【讨论】:

    • 我实际上没有得到我期望的结果......例如我有7个数据点-78.93477 34.60786 -78.09446 34.23414 -77.97638 35.791 -77.88063 34.2169 -772389 35.53175 -76.75031 34.73785 我用于我的多边形,由于某种原因下一个数据点没有落在多边形 -77.58959 34.87893
    猜你喜欢
    • 2019-07-14
    • 1970-01-01
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多