osmcode / libosmium

Fast and flexible C++ library for working with OpenStreetMap data.
https://osmcode.org/libosmium/
Boost Software License 1.0
467 stars 113 forks source link

How do I cache only nodes that are members of ways with a certain tag? #325

Closed yaseenelzawahry closed 3 years ago

yaseenelzawahry commented 3 years ago

I am aware of using the NodeLocationsForWays handler to cache node locations so that I can later access the location information from the NodeRef objects. If I understand correctly, it seems to me that this will cache ALL the node locations, which of course results in rather (unnecessarily) large files. My question is, is there a way to only cache node locations, for example, that are part of a way that has Tag:natural=coastline instead of all the nodes in the .pbf file?

joto commented 3 years ago

Sure you can do that. You have to read the file at least twice then, check all ways for the tag you are interested in, remember the node ids you need, then re-read the file, this time only calling the NodeLocationsForWays handler for the nodes you have remembered. It depends very much on your exact use case whether that makes sense or whether it is faster to just remember all nodes. (But that, as you mention, will need more memory.)

It might be easier in your case to just use osmium-tool to filter out all coastlines (osmium tags-filter input.osm.pbf w/natural=coastline -o out.osm.pbf) and then work with that filtered file, no need to write something special for that.