无法在tmap和leaflet中绘制OSM多边形。

huangapple go评论69阅读模式
英文:

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

无法在tmap和leaflet中绘制OSM多边形。

英文:

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 &lt;- 
  opq(bbox = &quot;Norrk&#246;ping&quot;) %&gt;% 
  add_osm_feature(key = &quot;amenity&quot;, value = &quot;school&quot;) %&gt;%
  osmdata_sf()

st_is_valid(nkpg_schools$osm_multipolygons)
#[1] TRUE TRUE TRUE TRUE 

intmd &lt;- nkpg_schools$osm_multipolygons %&gt;% 
  st_make_valid()

tmap_mode(&quot;view&quot;)
tm_shape(intmd) +
  tm_polygons(col = &quot;red&quot;) +
  tm_basemap(&quot;Esri.WorldGrayCanvas&quot;)

无法在tmap和leaflet中绘制OSM多边形。

huangapple
  • 本文由 发表于 2023年6月12日 17:54:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76455486.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定