OSMfilter: How to correctly filter for all roads properly?

I want to filter the OSM US file for all the roads, basically anything that has a highway tag. I understand roads can be either ways or relations, so I tried running the following query:
–read-xml input.osm
–tf accept-ways highway=*
–tf accept-relations highway=*
–write-xml output.osm

I then searched for the nearest_edges using osmnx.nearest_edges function from the osmnx library.
I used 1000 latitude,longitude points. However, when I compared the results of using the full OSM file vs this filtered OSM file, 32 road types were missing on the filtered graph.
Any ideas on how to modify my query? I want to get the road type as small as possible?
Maybe instead of filtering highway and nodes, I can drop all buildings or cafes. Anything to make sure I don’t lose all the roads, but simplify the graph as much as possible. Thank you.

I can’t really help with the osmosis program as I haven’t used it for a very long time.

But you might try using the osmium tags-filter:

For a minimal example:

$ osmium tags-filter --overwrite -o hungary-highway.osm.pbf hungary-latest.osm.pbf highway
[======================================================================] 100% 

And to verify, the number of highway tags in the two files (input vs. filtered) should match.

$ osmium tags-count hungary-latest.osm.pbf  highway
[======================================================================] 100% 
1067823	"highway"

$ osmium tags-count hungary-highway.osm.pbf highway
[======================================================================] 100% 
1067823	"highway"

The latest version of the osmium tool is v1.16; and it’s recommended to use this version.
If you are struggling with compiling the software packages, it’s advisable
to use conda-forge (available for linux-64, osx-64, win-64).


If you are focusing on the roads, in my opinion, it might also be worth filtering out the ferry routes ( route=ferry ). Tag:route=ferry - OpenStreetMap Wiki
But it’s also advisable to check visually because sometimes even that is not enough.

Maybe GeoDesk can also be suitable for filtering raw OSM data on a desktop PC?

I’m slightly surprised that neither of the previous posts pointed this out, but there is no relation type that utilises the highway tag and is used for modelling roads. With other words the option is unnecessary.

Unfortunately the initial post suggested perhaps a bit of a deeper misunderstanding:

The highway tag is used for all sorts of things - areas, crossings, and lots more. Depending on what you want, you might also want to exclude a highway=footway like here.

I suspect that the relations that you might be interested are route relations like this one. In the US, a way such as this might be part of multiple route relations.

However, what you actually want in your resulting file rather depends on what you want to do with the data.

Yes, that approach can also work. You might also find osm-tags-transform useful for that.