如何使用 R 包 osmdata 检索邮政编码

How to retrieve postal codes using R package osmdata

我正在尝试使用以下代码检索给定 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 对象,但 returns 只有这个空集。我做错了什么?

查看此 post and this link(来自 post)了解更多上下文。基本上,您的代码可以工作,但德国以外的地图绘制者很少映射 boundary=postal_code.

在柏林进行的测试显示了很多结果。根据上面 link 中的小地图,巴西利亚似乎是巴西唯一标有 postal 代码边界的地方。我们在那里也得到了一些结果,但要少得多。

柏林

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

如果将查询保存到对象,postal 代码数据可以按如下方式访问:

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