Guilherme Jardim
Guilherme Jardim

Reputation: 163

How to retrieve postal codes using R package osmdata

I'm trying to use the following code to retrieve postal codes for a given sf object:

library(osmdata)
library(tidyverse)

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

However, when I run it, I get the following result:

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

I was expecting to receive a sf object with the polygons and corresponding postal codes, but it returns only this empty set. What am I doing wrong?

Upvotes: 1

Views: 493

Answers (1)

Eugene Chong
Eugene Chong

Reputation: 1741

See this post and this link (from that post) for more context. Basically, your code works, but few mappers outside of Germany have mapped boundary=postal_code.

Testing this on Berlin shows many results there. Based on the small map in the link above, it looks like Brasília is the only place in Brazil where postal code boundaries are mapped. We get some results there too, but far fewer.

Berlin

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

Brasília

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

If one saves the query to an object, the postal code data can be accessed as follows:

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

...and the output:

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

Upvotes: 3

Related Questions