英文:
Impossible to plot OSM multipolygons in tmap and leaflet
问题
我正在使用osmdata
从OpenStreetMap提取数据,并从查询中获取一组点、线、多边形和多多边形。不知何故,在尝试绘制多多边形时,Leaflet和Tmap找不到它们,而使用plot(st_geometry(...))
可以正常工作。
对于发生的情况有任何想法吗?
英文:
I'm extracting data from OpenStreetMap using osmdata
, and from the query I get a set of points, lines, polygons, and multipolygons. Somehow, when trying to plot multipolygons, leaflet and tmap do not find them, while using plot(st_geometry(...))
works well.
Any ideas as to what is happening?
library(osmdata)
library(sf)
library(tmap)
nkpg_schools <-
opq(bbox = "Norrköping") %>%
add_osm_feature(key = "amenity", value = "school") %>%
osmdata_sf()
plot(st_geometry(nkpg_schools$osm_multipolygons)) #Works well
tmap_mode("view")
tm_basemap("Esri.WorldGrayCanvas") +
tm_shape(nkpg_schools$osm_multipolygons) +
tm_polygons()
#Does not work
答案1
得分: 1
这是一个有趣的问题!我的第一直觉是Norrköping学校多边形对象在空间上无效(不幸地,这种情况偶尔会发生在OSM对象上)。
看起来似乎不是这种情况,可以参考sf::st_is_valid()
测试结果。
但是!更加奇怪的是:通过sf::st_make_valid()
创建一个经过验证的学校中间对象会使学校行为正常。
我承认我不能完全解释发生了什么,但如果它解决了你的问题 - 我们又有什么资格评判呢?
library(osmdata)
library(sf)
library(tmap)
nkpg_schools <-
opq(bbox = "Norrköping") %>%
add_osm_feature(key = "amenity", value = "school") %>%
osmdata_sf()
st_is_valid(nkpg_schools$osm_multipolygons)
#[1] TRUE TRUE TRUE TRUE
intmd <- nkpg_schools$osm_multipolygons %>%
st_make_valid()
tmap_mode("view")
tm_shape(intmd) +
tm_polygons(col = "red") +
tm_basemap("Esri.WorldGrayCanvas")
英文:
This is an interesting problem! My first hunch was that the Norrköping schools multipolygon object was spatially invalid (which does happen with the OSM objects from time to time, unfortunately).
This seems not to be the case, see sf::st_is_valid()
test results.
But! curiouser and curiouser: creating an intermediary object of schools made valid via sf::st_make_valid()
causes the schools to behave themselves.
I confess I can not fully explain what's going there, but if it solves your problem - who are we to judge?
library(osmdata)
library(sf)
library(tmap)
nkpg_schools <-
opq(bbox = "Norrköping") %>%
add_osm_feature(key = "amenity", value = "school") %>%
osmdata_sf()
st_is_valid(nkpg_schools$osm_multipolygons)
#[1] TRUE TRUE TRUE TRUE
intmd <- nkpg_schools$osm_multipolygons %>%
st_make_valid()
tmap_mode("view")
tm_shape(intmd) +
tm_polygons(col = "red") +
tm_basemap("Esri.WorldGrayCanvas")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论