Parse PBF and Extract Amenity Data in Code

I need to extract Restaurants from a planet.osm.pbf file to store the data in an uncommon database format that I am using. I have no issue writing the data once I have it, but I can’t figure out how to extract it. I tried with Osmosis, but I found the documentation to be very difficult. Java is my language of choice but at this point, any language and library will do. I need to do this:

Pseudocode:

PBF-Parser parser;
parser = new parser(“planet.osm.pbf”);
while(parser.hasNext(“amenity=restaurant”))
{
Amenity restaurant = this.amenity;
String street = restaurant.street;
String city = restaurant.city;
String region = restaurant.region;
String name = restaurant.name;
String cuisine = restaurant.cuisine;
// I do my own thing with this information here…
}

You can try to find some hints in the OSM wiki about

http://wiki.openstreetmap.org/wiki/Category:Java

or

http://wiki.openstreetmap.org/wiki/Frameworks

or

http://wiki.openstreetmap.org/wiki/Category:OSM_processing

While you’re absolutely correct about the documentation issue, Osmosis can nevertheless do what you want. (Even though the interface is pretty ugly for this particular task.) Did you find my previous answer to a similar topic?

There have been some changes to the various classes and interfaces, e.g. the Sink interface has gained another method since then that you need to implement, but that’s the basic idea. Once you have a Node/Way/Relation object (or even just a Entity object if you don’t care about the distinction), you can access its tags.

Just ask if something doesn’t work By the way, if the terms like “way” and “tag” confuse you, make sure to read the wiki page about the OSM data model: http://wiki.openstreetmap.org/wiki/Elements