【问题标题】:How to retrieve postal codes using R package osmdata如何使用 R 包 osmdata 检索邮政编码
【发布时间】:2020-06-30 21:22:39
【问题描述】:

我正在尝试使用以下代码来检索给定 sf 对象的邮政编码:

library(osmdata)
library(tidyverse)

x <- getbb("Rio de Janeiro") %>% 
    opq() %>% 
    add_osm_feature(key = 'boundary', value = 'postal_code') %>% 
    osmdata_sf()

但是,当我运行它时,我得到以下结果:

Object of class 'osmdata' with:
             $bbox : -23.0827051,-43.796252,-22.7460878,-43.0990811
    $overpass_call : The call submitted to the overpass API
             $meta : metadata including timestamp and version numbers
       $osm_points : 'sf' Simple Features Collection with 0 points
        $osm_lines : NULL
     $osm_polygons : 'sf' Simple Features Collection with 0 polygons
   $osm_multilines : NULL
$osm_multipolygons : NULL

我期待收到一个带有多边形和相应邮政编码的 sf 对象,但它只返回这个空集。我做错了什么?

【问题讨论】:

    标签: r sf


    【解决方案1】:

    请参阅此post 和此link(来自该帖子)以获取更多上下文。基本上,您的代码可以工作,但德国以外的映射器很少映射 boundary=postal_code

    在柏林对此进行测试显示了许多结果。根据上面链接中的小地图,看起来巴西利亚是巴西唯一一个绘制了邮政编码边界的地方。我们在那里也得到了一些结果,但要少得多。

    柏林

    x <- getbb("Berlin") %>% 
      opq() %>% 
      add_osm_feature(key = 'boundary', value = 'postal_code',
                      value_exact = F, key_exact = F) %>% 
      osmdata_sf()
    
    x
    
    Object of class 'osmdata' with:
                     $bbox : 52.3382448,13.088345,52.6755087,13.7611609
            $overpass_call : The call submitted to the overpass API
                     $meta : metadata including timestamp and version numbers
               $osm_points : 'sf' Simple Features Collection with 36128 points
                $osm_lines : 'sf' Simple Features Collection with 1677 linestrings
             $osm_polygons : 'sf' Simple Features Collection with 1 polygons
           $osm_multilines : NULL
        $osm_multipolygons : 'sf' Simple Features Collection with 231 multipolygons
    

    巴西利亚

    y <- getbb("Brasilia") %>% 
      opq() %>% 
      add_osm_feature(key = 'boundary', value = 'postal_code',
                      value_exact = F, key_exact = F) %>% 
      osmdata_sf()
    
    y
    
    Object of class 'osmdata' with:
                     $bbox : -15.8589663,-48.0895565,-15.5781078,-47.7828767
            $overpass_call : The call submitted to the overpass API
                     $meta : metadata including timestamp and version numbers
               $osm_points : 'sf' Simple Features Collection with 15 points
                $osm_lines : NULL
             $osm_polygons : 'sf' Simple Features Collection with 4 polygons
           $osm_multilines : NULL
        $osm_multipolygons : NULL
    

    如果将查询保存到对象,则可以通过以下方式访问邮政编码数据:

    y <- getbb("Brasilia") %>% 
         opq() %>% 
         add_osm_feature(key = 'boundary', value = 'postal_code',
                         value_exact = F, key_exact = F) %>% 
         osmdata_sf() -> brasiliaObject
    brasiliaObject[["osm_polygons"]]["addr.postcode"]
    

    ...和输出:

    > brasiliaObject[["osm_polygons"]]["addr.postcode"]
              addr.postcode
    681474840     71505-765
    693119174     71515-030
    696548754     71515-020
    721414275          <NA>
    >
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-26
      相关资源
      最近更新 更多