I am using Eclipse 4.2 basic java developer edition with the following plugins:

  • Swing designer
  • Window builder
  • m2e – maven plugin

I am using JDK 1.6 for no other reason than this is the jdk I have currently installed.

Step 1: Create a new project:01-00 New Project

Create a new project in Eclipse:

Give it a name and location and click on finish.

 

 

 

 

 

01-02 ConvertToMaven

Step 2: Convert Project to Maven & configure dependencies

Right click the project and select “Configure -> Convert to to Maven Project”.

 

Accept the default values and click finish.

 

Add the following dependencies to the pom:

        <dependency>
            <groupId>org.openstreetmap.osmosis</groupId>
            <artifactId>osmosis-core</artifactId>
            <version>0.43.1</version>
        </dependency>
        <dependency>
            <groupId>org.openstreetmap.osmosis</groupId>
            <artifactId>osmosis-xml</artifactId>
            <version>0.43.1</version>
        </dependency>
        <dependency>
            <groupId>org.openstreetmap.osmosis</groupId>
            <artifactId>osmosis-pbf</artifactId>
            <version>0.43.1</version>
        </dependency>
        <dependency>
            <groupId>org.openstreetmap.osmosis</groupId>
            <artifactId>osmosis-osm-binary</artifactId>
            <version>0.43.1</version>
        </dependency>
        <dependency>
            <groupId>org.openstreetmap.osmosis</groupId>
            <artifactId>osmosis-osm-binary</artifactId>
            <version>0.43.1</version>
        </dependency>

A word about the libraries I am using: Osmosis is an open street maps api for parsing osm data. Osm data is the basic map data that we will be using in the tutorial. For further information see http://wiki.openstreetmap.org/wiki/Osmosis.

Step 3: Locating map data

The map data that will be used in this tutorial is freely available from openstreetmaps. The data can be easily downloaded using the openstretmaps website, although data file size limitations exist and I find it simpler to download from this site: http://download.geofabrik.de/.

For the early tutorials do not be tempted to use a large data file. It will not be until tutorial 5 that I start to look at persisting the data in a spatial database. Until then all processing will be done in memory and even a small file of 30mb can slow down the machine.

For this tutorial I will use Luxembourg. It can be downloaded here http://download.geofabrik.de/europe/luxembourg-latest.osm.pbf.

Put the data into a data folder in the root of the project:

Just to check that everything works let’s just check that we can parse the data.

/*
 * Copyright (c) 2013, Robert Kovacs, http://www.nofrillsjava.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided
 * that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this list of conditions
 * and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
 * and the following disclaimer in the documentation and/or other materials provided with the
 * distribution.
 *
 * None of the name of the University of Maryland, the name of the Piccolo2D project, or the names of its
 * contributors may be used to endorse or promote products derived from this software without specific
 * prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
package com.nofrills.mapping.tutorial.parser;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Calendar;
import java.util.Map;
 
import org.openstreetmap.osmosis.core.container.v0_6.EntityContainer;
import org.openstreetmap.osmosis.core.domain.v0_6.Entity;
import org.openstreetmap.osmosis.core.domain.v0_6.Node;
import org.openstreetmap.osmosis.core.domain.v0_6.Relation;
import org.openstreetmap.osmosis.core.domain.v0_6.Way;
import org.openstreetmap.osmosis.core.task.v0_6.RunnableSource;
import org.openstreetmap.osmosis.core.task.v0_6.Sink;
import org.openstreetmap.osmosis.xml.common.CompressionMethod;
import org.openstreetmap.osmosis.xml.v0_6.XmlReader;
 
import crosby.binary.osmosis.OsmosisReader;
 
public class OsmosisParser {
 
    long numNodes =0;
    long numWays =0;
    long numRelations =0;
    int eleCount =0;
 
    public static void main(String[] args) throws FileNotFoundException {
        new OsmosisParser(new File("data/luxembourg-latest.osm.pbf"));
    }
 
    public OsmosisParser(File file) throws FileNotFoundException {
 
        Sink sinkImplementation = new Sink() {
            public void process(EntityContainer entityContainer) {
                Entity entity = entityContainer.getEntity();
                if (entity instanceof Node) {
                    numNodes++;
                } else if (entity instanceof Way) {
                    numWays++;
                } else if (entity instanceof Relation) {
                    numRelations++;
                }
            }
 
            public void release() {
            }
 
            public void complete() {
            }
 
            public void initialize(Map<String, Object> arg0) {
 
            }
        };
 
        RunnableSource reader;
        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;
        }
 
        if (pbf) {
            reader = new OsmosisReader(new FileInputStream(file));
        } else {
            reader = new XmlReader(file, false, compression);
        }
 
        reader.setSink(sinkImplementation);
 
        Thread readerThread = new Thread(reader);
        long start = Calendar.getInstance().getTimeInMillis();
        readerThread.start();
 
        // Wait for the reader thread to finish
        while (readerThread.isAlive()) {
            try {
                readerThread.join();
            } catch (InterruptedException e) {
                /* do nothing */
            }
        }
        // Before printing the results
        long end = Calendar.getInstance().getTimeInMillis();
        System.out.format("Read time:      %,12d ms.%n",(end-start));
        System.out.format("Nodes read:     %,12d%n",numNodes);
        System.out.format("Ways read:      %,12d%n",numWays);
        System.out.format("Relations read: %,12d%n",numRelations);
 
    }
 
}

Running this should give the following sort of output:

Read time:             2,173 ms.
Nodes read:        1,107,800
Ways read:           119,760
Relations read:        1,386

Conclusion

Well that is the end of the 1st tutorial. What have I achieved? Not a lot other than the basic building blocks:

  • The development environment
  • Created a project
  • Configured it to use maven
  • Added the osmosis dependencies
  • Found some data
  • Created a basic parser

 

 

Project setup and data gathering.