OpenStreetMap Forum

The Free Wiki World Map

You are not logged in.

#1 2012-01-09 21:45:23

jatin
Member
Registered: 2012-01-09
Posts: 5
Website

Parsing .osm files

Hello,

I am new to Open Street Map, and have enjoyed whatever time spent on it. It might be a repetitive question but could not find any answer to it on the forum.

I need to parse the data of a city say New Delhi, once after parsing I will upload the data in Neo4J which is a graph database, on which later I can perform some computations. I was wondering if already there is an API/code available in Java which will effectively parse the input .osm files.

This is so that I dont re-invent the wheel.

Thanks in advance

Last edited by jatin (2012-01-09 22:02:29)

Offline

#2 2012-01-10 07:12:39

Marqqs
Member
Registered: 2011-01-01
Posts: 724

Re: Parsing .osm files

jatin wrote:

I was wondering if already there is an API/code available in Java which will effectively parse the input .osm files.

Hello, welcome to the forum!

Maybe the toolkit Osmium could help you parsing .osm files: https://wiki.openstreetmap.org/wiki/Osmium
You even could download and parse the file in .pbf format which allows smaller file sizes and can be read faster than .osm format.

Markus

Offline

#3 2012-01-10 10:03:02

jatin
Member
Registered: 2012-01-09
Posts: 5
Website

Re: Parsing .osm files

Hello Thanks a lot for the support.

But Osmium is for C++, I needed something in Java as Neo4J currently does not have a library for C++.
Or can you please suggest an alternate way i can do it.

Thanks again

Offline

#4 2012-01-10 10:21:29

JRA
Member
Registered: 2007-12-17
Posts: 663

Re: Parsing .osm files

Offline

#5 2012-01-10 13:22:41

Tordanik
Moderator
From: Germany
Registered: 2008-06-17
Posts: 2,383
Website

Re: Parsing .osm files

I also recommend Osmosis, as I've used it in my own Java programs. It's a mature tool and has support for reading and writing all popular OSM file formats. Don't let the command line interface irritate you, the same .jar archives can also be used as libraries.

Offline

#6 2012-01-10 15:35:48

jatin
Member
Registered: 2012-01-09
Posts: 5
Website

Re: Parsing .osm files

Hello,

Thanks for the suggestion. One more advice needed please...
I sat overnight and wrote a parser myself. For city new Delhi which has:
Number of Ways 26165
Number of Relations  44
Number of Nodes 130003

And on my machine it took 58 minutes to parse.

My Question:
1) Can this time come down using jar archives in Osmosis , because insertion in the graph database will probably take many more hours to insert. If yes, any suggestions on how should I proceed?

Best Regards,
Jatin

Last edited by jatin (2012-01-10 20:37:16)

Offline

#7 2012-01-10 22:52:08

Tordanik
Moderator
From: Germany
Registered: 2008-06-17
Posts: 2,383
Website

Re: Parsing .osm files

Parsing this file with Osmosis should take less than a minute. I don't know how long inserting into your database takes, though.

If you want to try Osmosis: Download the latest version, add the jars to your classpath (you don't need all of them, but I suggest that you first add them all, get it to work, and then remove those that are not actually required). The actual code will then look somewhat like this:

import org.openstreetmap.osmosis.core.container.v0_6.EntityContainer;
import org.openstreetmap.osmosis.core.domain.v0_6.*;
import org.openstreetmap.osmosis.core.task.v0_6.*;
import org.openstreetmap.osmosis.xml.common.CompressionMethod;
import org.openstreetmap.osmosis.xml.v0_6.XmlReader;

...

File file = ...; // the input file

Sink sinkImplementation = new Sink() {
    public void process(EntityContainer entityContainer) {
        Entity entity = entityContainer.getEntity();
        if (entity instanceof Node) {
            //do something with the node
        } else if (entity instanceof Way) {
            //do something with the way
        } else if (entity instanceof Relation) {
            //do something with the relation
        }
    }
    public void release() { }
    public void complete() { }
};

boolean pbf = false;
CompressionMethod compression = CompressionMethod.None;

if (file.getName().endsWith(".pbf")) {
    pbf = true;
} else if (file.getName().endsWith(".gz")) {
    compression = CompressionMethod.GZip;
} else if (file.getName().endsWith(".bz2")) {
    compression = CompressionMethod.BZip2;
}

RunnableSource reader;

if (pbf) {
    reader = new crosby.binary.osmosis.OsmosisReader(
            new FileInputStream(file));
} else {
    reader = new XmlReader(file, false, compression);
}

reader.setSink(sinkImplementation);

Thread readerThread = new Thread(reader);
readerThread.start();

while (readerThread.isAlive()) {
    try {
        readerThread.join();
    } catch (InterruptedException e) {
        /* do nothing */
    }
}

...

This is copied from one of my own tools that use Osmosis, and can also parse .osm.pbf, .osm.gz, and .osm.bz2 files. If you only need .osm (xml) parsing, the code gets a lot shorter.

Offline

#8 2012-01-11 16:49:04

jatin
Member
Registered: 2012-01-09
Posts: 5
Website

Re: Parsing .osm files

Thanks,

Well it indeed is very fast. A much better tool

Regards,
Jatin

Offline

Board footer

Powered by FluxBB