2

I want to do a navigation application using the OSM data files ( pbf files ).

I want to use the pbf format in my java application.

I was looking for a way to access and read the pbf files, I found that it can be accessed via the osmosis library.

Unfortunately, I have no idea how to use that library and there's no documentation as well.

Hani
  • 21
  • 1
  • 2
  • crosspost: https://gis.stackexchange.com/questions/118892/osmosis-library-in-java-application – scai Oct 20 '14 at 10:49

3 Answers3

1

I don't know osmosis in detail enough, if it also does routing. But if you only want to read the OSM-PBF files to somehow extract a graph yourself to do the routing, have a look at https://github.com/scrosby/OSM-binary. This is (iirc) the actual library that osmosis uses as well to load pbf files.

An alternative would be to use Graphhopper (https://graphhopper.com). It is an open source routing library that supports loading OSM data, which is very fast, and uses a reasonable amount of memory only for what it does and what data it has to handle.

cello
  • 5,356
  • 3
  • 23
  • 28
  • 1
    No, osmosis can't route. It can only process OSM files in order to convert it to a different format, to extract specific things or for dumping it into a database. – scai Oct 20 '14 at 07:48
  • I don't want it route I just want to extract the information from the obf files and I have no idea how to it. – Hani Oct 20 '14 at 13:11
  • 1
    @Hani: well, what information do you want to extract? OSM-data has tons of information, e.g. about roads, buildings, but also about trees, post boxes, ship routes, etc. Also, navigation app without routing? Maybe you should provide more context what your app should be able to do, otherwise it's difficult to give more specific answers. – cello Oct 20 '14 at 18:50
  • @Cello I want to do routing application. But first I want to extract the needed information Like if I'm at a given location I have the long and lat using the GPS from my phone I want to extract the address of that location. and then If the user asks for the a restaurant or place I can extract the location ( long and lat ). Then being able to extract the roads connected from the given location to be able to build the route finding algorithm. ( I wish to build my own algorithm not to use any already in existence ones). – Hani Oct 21 '14 at 00:34
  • You can't use osmosis that way. For geocoding, routing and similar tasks you have to use a proper database and do some pre-procession on the data. Osmosis can't magically extract a certain address simply due to the complexity of addresses. Likewise searching for a restaurant of a given name would be horrible slow with osmosis. Take a look at already existing applications to get an idea where to start, or ask a new question about this topic. And be aware that developing a routing and geocoding application from scratch won't be an easy task. – scai Oct 21 '14 at 07:02
  • @Hani You mean reverse geocoding? http://wiki.openstreetmap.org/wiki/Nominatim#Reverse_Geocoding_.2F_Address_lookup – sabas Oct 22 '14 at 07:54
0

Judging by your comments in the cross post, I believe this link is what you're after.

https://lists.openstreetmap.org/pipermail/dev/2011-February/021804.html

All tasks can be instantiated and invoked independently if you wish. Every task available on the command line such as --read-xml, --read-pbf, etc is implemented by a class that can be used within your own code.

So there are a lot of jar files associated with osmosis which can be imported, and then the classes within can be used. I would suggest using the documentation for the command line version as a reference, then trying to build your application by looking at these docs and the code.

Good luck!

user3390629
  • 854
  • 10
  • 17
0

Here is an example of how you would get all the shops in a specific bouding box and store them in lists for the nodes, ways, and relations in lists.

I use kotllin with gradle.

In the gradle.kts:

dependencies {
    implementation("org.openstreetmap.osmosis:osmosis-pbf:0.48.3")
    implementation("org.openstreetmap.osmosis:osmosis-areafilter:0.48.3")
}

The code:

import crosby.binary.osmosis.OsmosisReader
import org.openstreetmap.osmosis.areafilter.v0_6.BoundingBoxFilter
import org.openstreetmap.osmosis.core.container.v0_6.*
import org.openstreetmap.osmosis.core.domain.v0_6.*
import org.openstreetmap.osmosis.core.filter.common.IdTrackerType
import org.openstreetmap.osmosis.core.task.v0_6.Sink
import java.io.FileInputStream

class CustomProcessor : Sink {
    // Here the results will be stored
    val nodes = mutableListOf<Node>()
    val ways = mutableListOf<Way>()
    val relations = mutableListOf<Relation>()

    override fun close() {
        // Close all your readers and such
    }

    override fun initialize(metaData: MutableMap<String, Any>?) {
        // Don't know when to use this
    }

    override fun complete() {
        // If you first put data into storages you can process them here after you have all you need
    }

    override fun process(entityContainer: EntityContainer?) {
        val entity = entityContainer?.entity

        // Filter for shops
        var foundIt = false
        if (entity != null) {
            for (tag in entity.tags) {
                if (tag.key == "shop") {
                    foundIt = true
                    break
                }
            }
        }

        // If the object is a shop
        if (foundIt) {
            if (entity is Node) {
                nodes.add(entity)
            } else if (entity is Way) {
                ways.add(entity)
            } else if (entity is Relation) {
                relations.add(entity)
            } else if (entity is Bound) {
                // We don't store bounds
            } else {
                // Shouldn't be possible
            }
        }
    }
}

fun main(args: Array<String>) {
    // Set input stream
    val inputStream = FileInputStream("path/to/my/file.osm.pbf")

    // Create the bounding box filter
    val bbFilter = BoundingBoxFilter(
        IdTrackerType.Dynamic, // ID tracker used internally. This is the default.
        11.5256, // Left lon
        11.5004, // Right lon
        48.84543, // Top lat
        48.8015, // Bottom lat
        true, // Remove ways and relation only partly in bounding box?
        false, // Include all nodes of ways only partly in bounding box.
        false, // Include all nodes and ways of relations only partly in bounding box.
        false // Include all relations that reference relation inside bounding box.
    )

    // Create your processor
    val processor = CustomProcessor()

    // Define your pipeline
    val reader = OsmosisReader(inputStream)
    bbFilter.setSink(processor)
    reader.setSink(processor)

    // Run
    reader.run()

    // Get your data
    val nodes = processor.nodes
    val ways = processor.ways
    val relations = processor.relations
}

If you want the geometries of ways and relations you will also need to store the nodes they consist of. These are not tagged with "shop". Storing them in lists is quite RAM intensive so it is best to use the org.openstreetmap.osmosis.core.store.SimpleObjectStore class.

Jacopo
  • 11
  • 2