ropensci / osmdata

R package for downloading OpenStreetMap data
https://docs.ropensci.org/osmdata
317 stars 45 forks source link

How to download only $osm_lines and specific attributes? #144

Closed ibarraespinosa closed 6 years ago

ibarraespinosa commented 6 years ago

I'm starting to use your excellent package but I found that it download the whole information, consuming too much RAM. I just need osm_lines with the attributes "highway" and "maxspeed".

Is it possible to do that?

Thanks!

# gCO is just a spatial feature object
q0 <- opq(bbox = st_bbox(gCO))
q1 <- add_osm_feature(q0, key = 'name')
x <- osmdata_sf(q1)
osm <- x$osm_lines[, c("highway", "maxspeed")]
st <- c("motorway", "motorway_link", "trunk", "trunk_link", 
        "primary", "primary_link", "secondary", "secondary_link", 
        "tertiary", "tertiary_link")
osm <- osm[osm$highway %in% st, ]

My sessionInfo:

R version 3.4.4 (2018-03-15)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.1 LTS

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/openblas/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/libopenblasp-r0.2.20.so

locale:
 [1] LC_CTYPE=pt_BR.UTF-8       LC_NUMERIC=C               LC_TIME=pt_BR.UTF-8       
 [4] LC_COLLATE=pt_BR.UTF-8     LC_MONETARY=pt_BR.UTF-8    LC_MESSAGES=pt_BR.UTF-8   
 [7] LC_PAPER=pt_BR.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=pt_BR.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] sf_0.7-0      osmdata_0.0.7 vein_0.5.2   

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.19      xml2_1.2.0        raster_2.6-7      magrittr_1.5      units_0.6-1      
 [6] rvest_0.3.2       eixport_0.3.5     lattice_0.20-35   R6_2.2.2          cptcity_1.0.3    
[11] stringr_1.3.1     httr_1.3.1        tools_3.4.4       grid_3.4.4        data.table_1.11.8
[16] e1071_1.6-8       DBI_1.0.0.9000    class_7.3-14      yaml_2.1.18       lwgeom_0.1-4     
[21] spData_0.2.8.3    curl_3.2          ncdf4_1.16        sp_1.3-1          stringi_1.2.4    
[26] compiler_3.4.4    classInt_0.2-3    jsonlite_1.5      lubridate_1.7.4  

And my screenfetch


                          ./+o+-       sergio@sergio-pos
                  yyyyy- -yyyyyy+      OS: Ubuntu 18.04 bionic
               ://+//////-yyyyyyo      Kernel: x86_64 Linux 4.15.0-34-generic
           .++ .:/++++++/-.+sss/`      Uptime: 10m
         .:++o:  /++++++++/:--:/-      Packages: 3111
        o:+o+:++.`..```.-/oo+++++/     Shell: bash
       .:+o:+o/.          `+sssoo+/    Resolution: 3040x900
  .++/+:+oo+o:`             /sssooo.   DE: Budgie
 /+++//+:`oo+o               /::--:.   WM: BudgieWM
 \+/+o+++`o++o               ++////.   WM Theme: Pocillo
  .++.o+++oo+:`             /dddhhh.   GTK Theme: Default [GTK2/3]
       .+.o+oo:.          `oddhhhh+    Icon Theme: LoginIcons
        \+.++o+o``-````.:ohdhhhhh+     Font: Ubuntu 11
         `:o+++ `ohhhhhhhhyo++os:      CPU: Intel Core i5-4440 @ 4x 3.3GHz [27.8°C]
           .o:`.syhhhhhhh/.oo++o`      GPU: Mesa DRI Intel(R) Haswell Desktop
               /osyyyyyyo++ooo+++/     RAM: 4287MiB / 7849MiB
                   ````` +oo+++o\:
                          `oo++.

Thanks!

ibarraespinosa commented 6 years ago

I solved in a non-elegant way

osm <- osmdata_sf(
  add_osm_feature(
    opq(bbox = st_bbox(gCO)),
    key = 'highway'))$osm_lines[, c("highway")]
format(object.size(osm), units = "Mb")
[1] "3.6 Mb"
mpadge commented 6 years ago

Thanks for using the package. As you discovered, using add_osm_feature(key = "highway") is the best way to restrict to highways alone. The package is intentionally designed not to allow restricting to osm_lines alone, rather to return all OSM data associated with a given query. In your example, highways are made of points, so those are returned as osm_points, often with additional data, and highways may also form defined polygons, or higher-level multi-way entities ($osm_multiline).

You can also easily pipe queries to make them possibly more elegant, so the above could be written

opq(bbox = st_bbox(gCO)) %>%
  add_osm_feature(key = "highway") %>%
  osmdata_sf() %>%
  magrittr::extract2("osm_lines") %>%
  magrittr::extract("highway")

That should do the job