首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用R包osmdata检索邮政编码

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

Stack Overflow用户
提问于 2020-07-01 05:22:39
回答 1查看 103关注 0票数 1

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

代码语言:javascript
运行
复制
library(osmdata)
library(tidyverse)

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

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

代码语言:javascript
运行
复制
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对象,但它只返回这个空集。我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-01 06:33:07

有关更多上下文,请参阅此post和此link (来自该帖子)。基本上,您的代码可以正常工作,但是德国以外的几个映射器已经映射了boundary=postal_code

在柏林进行测试表明,那里有很多结果。根据上面链接中的小地图,巴西利亚似乎是巴西唯一一个绘制了邮政编码边界的地方。我们在那里也得到了一些结果,但要少得多。

柏林

代码语言:javascript
运行
复制
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

巴西利亚

代码语言:javascript
运行
复制
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

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

代码语言:javascript
运行
复制
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操作:

代码语言:javascript
运行
复制
> brasiliaObject[["osm_polygons"]]["addr.postcode"]
          addr.postcode
681474840     71505-765
693119174     71515-030
696548754     71515-020
721414275          <NA>
>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62666345

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档