ropensci / osmdata

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

How can I add `timestamp` in `osmdata_sf()` results? #269

Open vanhry opened 2 years ago

vanhry commented 2 years ago

When I query osm data, I want to receive the timestamp, when some object was added or changed, is it possible to include this data? There is my query:

q <- opq("London",timeout = 20000) %>%
  add_osm_feature(key = "landuse", value="grass",value_exact = F) %>%
  osmdata_sf()

Then I use q$osm_polygons data.

in general, I want to convert this script to R:

[out:json][timeout:200];
{{geocodeArea:London}}->.searchArea;
way["landuse"~"grass"](area.searchArea);
(._;>;);
out meta;
mpadge commented 2 years ago

Thanks @vanhry, that's an interesting question. timestamps of individual objects are not by default delivered by the overpass server, but can be extracted as "metadata" as in your query above. Getting a local copy of the raw + metadata can be done by modifying your example like this:

q <- opq("London",timeout = 20000) %>%
  add_osm_feature(key = "landuse", value="grass",value_exact = F) %>%
  opq_string()
q <- gsub("out body", "out body meta", q)
filename <- "mydata.osm"
x <- osmdata_xml(q, filename = filename)

You'll then see the timestamps (and other metadata) appear alongside each object. The file can still be read directly with osmdata_sf(), but those metadata can not be converted into equivalent data columns in the sf objects, because there is no direct translation between OSM and sf objects. It might be tempting to think that the meta statement shown above might translate nicely to some kind of meta = FALSE parameter which could be switched on to get these data, but:

  1. I would be opposed to doing that becuase the amount of additional data downloaded from the overpass server is considerable - queries generally deliver 2-3 times as much data with metadata included; and
  2. There is no direct way to append these metadata to sf objects.

A more workable alternative might be to utilise the ::timestamp data accessible via the csv output mode, as is currently under discussion in #252. I suggest leaving this issue open for the moment, and I'll return to it as part of addressing that broader issue. Once some kind of osmdata_csv() function has been developed, that will then allow simple queries taking OSM ID values as inputs and returning timestamps as outputs. In the context of your example, you'll get the standard output from your searchArea query above as delivered with the last line of out body, and then be able to use the resultant ID values to get any desired bits of out meta. Happy to discuss any other ideas you might have until then.

vanhry commented 2 years ago

Thanks @mpadge, You helped me a lot!

jmaspons commented 1 year ago

If #285 is accepted, I can work on implementing different out modes such as meta, tags and others. Can be useful to select objects based on tags and metadata and then download the spatial data based on OSM type and ID now that #283 is working.

jmaspons commented 1 year ago

Almost ready for a PR at https://github.com/jmaspons/osmdata/commit/83148a929586459760dd0483f28f640f96e7b858 . Waiting for #285