3

I am using the R package osm_data to retrieve some polygons. If I want to plot them using leaflet, it does only work if I export the polygons to a shapefile and re-import it, but plotting it directly does not work.

Plotting using the provided plot variant does work, however.

I have played around with the format that osm_data returns, but to no success. Does anyone have an idea for the reason?

library(osmdata)
library(sf)
library(leaflet)
library(tidyverse)

result <- opq(bbox = 'Lisbon, Portugal') %>% 
  add_osm_feature(
    key = 'amenity', 
    value = 'university'
  ) %>% 
  osmdata_sf()

universities <- result$osm_polygons %>% 
  select(osm_id, name) %>% 
  drop_na(name)

head(universities)

universities %>% select(name) %>% plot

leaflet() %>% 
  addTiles %>% 
  addPolygons(data = universities)

universities %>% 
  write_sf('universities.shp', driver = 'ESRI Shapefile')

universities_from_shapefile <- read_sf('universities.shp')

head(universities_from_shapefile)

leaflet() %>% 
  addTiles %>% 
  addPolygons(data = universities_from_shapefile, label = universities_from_shapefile$name)

Output of head(universities)

> head(universities)
Simple feature collection with 6 features and 2 fields
geometry type:  POLYGON
dimension:      XY
bbox:           xmin: -9.209042 ymin: 38.65691 xmax: -9.136679 ymax: 38.74995
epsg (SRID):    4326
proj4string:    +proj=longlat +datum=WGS84 +no_defs
           osm_id                                                              name                       geometry
28681498 28681498                                 Faculdade de Medicina Veterinária POLYGON ((-9.195866 38.7140...
52430141 52430141 Faculdade de Ciencias e Tecnologia da Universidade Nova de Lisboa POLYGON ((-9.203055 38.6594...
71571983 71571983                           Universidade Católica Portuguesa Lisboa POLYGON ((-9.166331 38.7480...
93763775 93763775                                        Instituto Superior Técnico POLYGON ((-9.140742 38.7377...
95417328 95417328                   Instituto Superior de Ciências da Administração POLYGON ((-9.14661 38.70841...
97822065 97822065                  Universidade Nova, Faculdade de Ciências Médicas POLYGON ((-9.139408 38.7189...

output of head(universities_from_shapefile)

> head(universities_from_shapefile)
Simple feature collection with 6 features and 2 fields
geometry type:  POLYGON
dimension:      XY
bbox:           xmin: -9.209042 ymin: 38.65691 xmax: -9.136679 ymax: 38.74995
epsg (SRID):    4326
proj4string:    +proj=longlat +datum=WGS84 +no_defs

# A tibble: 6 x 3
  osm_id   name                                                                                                   geometry
  <chr>    <chr>                                                                                             <POLYGON [°]>
1 28681498 Faculdade de Medicina Veterinária           ((-9.195866 38.71404, -9.195883 38.71407, -9.195884 38.71412, -9.1…
2 52430141 Faculdade de Ciencias e Tecnologia da Univ… ((-9.203055 38.65946, -9.203157 38.659, -9.203167 38.65862, -9.203…
3 71571983 Universidade Católica Portuguesa Lisboa     ((-9.166331 38.74801, -9.167548 38.74971, -9.167599 38.74991, -9.1…
4 93763775 Instituto Superior Técnico                  ((-9.140742 38.73776, -9.140703 38.73779, -9.140682 38.73782, -9.1…
5 95417328 Instituto Superior de Ciências da Administ… ((-9.14661 38.70841, -9.146233 38.7083, -9.146319 38.70812, -9.146…
6 97822065 Universidade Nova, Faculdade de Ciências M… ((-9.139408 38.71897, -9.139442 38.71903, -9.139504 38.71916, -9.1…

1 Answer 1

1

On the osmdata GitHub issues I found that the problem relates to the simple features having an assigned "name", which is the row name.

Here is the issue, and what it shows is that if you add this line to your code:

names(universities$geometry) <- NULL

and then plot with leaflet it will work. Basically when you export to shapefile and reimport, the row names are gone and that is why you can then plot them.

1
  • 1
    Perfect, thanks alot! This does the trick!
    – telegott
    Nov 13, 2019 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.