Hi Sadeer,

On 4 April 2013 18:53, Sadeer Nasser <sad...@hotmail.co.uk> wrote:

> Hi guys,
>
> I'm trying to use the Osmosis API (the Jar file not the command-line tool)
> in my Java application but not sure how to get started as there doesn't
> seem to be enough stuff on the web to help people with this. Specifically,
> I'm trying to extract all the highways for a particular area (which will be
> specified using a bbox).
>

There is minimal documentation (other than javadocs) describing how to
directly code to the Osmosis API.  The code has been designed to be fairly
modular and used outside of the command line application, however there is
no formal API as such so it may not be immediately obvious which code is
specific to the command line app and what can be re-used.

The best way to start is to build a pipeline using the command line
application that does what you need.  I don't want to get into the
specifics of how to best extract highways because I suspect that the answer
isn't simple, but let's say you have the following command line.

*osmosis --read-xml planet.xml --bounding-box left=0 right=1 bottom=0 top=1
--tag-filter accept-ways highway=* --used-node --write-xml myresults.xml*

To do the same thing internally in your app you need to build the same
pipeline in code.  If you wanted to read from a file and write to a file
(ie. don't need to access objects in your app), the simplest thing to do is
simply call the org.openstreetmap.osmosis.core.Osmosis.main method directly
and pass the above arguments.  However in most cases you'll probably want
to hook into the pipeline directly in which case you'll need to dig a bit
deeper.

Every task in the pipeline is represented by a single class in one of the
Osmosis projects.  For example, the --read-xml task is implemented by the
org.openstreetmap.osmosis.xml.v0_6.XmlReader class in the osmosis-xml
project.   The steps for figuring this out are:
* Identify which plugin class registers the command line task.  To do this,
look for all classes implementing the
org.openstreetmap.osmosis.core.plugin.PluginLoader interface, there are 17
of them, one in each osmosis project.  For --read-xml, it is registered in
the class called org.openstreetmap.osmosis.xml.XmlPluginLoader in the
osmosis-xml project.
* In the plugin class, identify the factory class registered for the task
name.  For --read-xml, the plugin loader registers the factory class
org.openstreetmap.osmosis.xml.v0_6.XmlReaderFactory.
* Open the factory class and examine the createTaskManagerImpl method to
see what task class is being instantiated.  In the --read-xml case, this is
org.openstreetmap.osmosis.xml.v0_6.XmlReader.

Using the above steps, you can derive the following class names for each
task:
* --read-xml = org.openstreetmap.osmosis.xml.v0_6.XmlReader
* --bounding-box =
org.openstreetmap.osmosis.areafilter.v0_6.BoundingBoxFilter
* --tag-filter = org.openstreetmap.osmosis.tagfilter.v0_6.TagFilter
* --used-node = org.openstreetmap.osmosis.tagfilter.v0_6.UsedNodeFilter
* --write-xml = org.openstreetmap.osmosis.xml.v0_6.XmlWriter

Each of these classes can be instantiated directly inside your application
code.  To construct a pipeline, you need to call the setSink method on each
task and pass the following task as an argument to it.  For example, call
setSink on the XmlReader instance and pass in the BoundingBoxFilter
instance.  The final XmlWriter won't have a setSink method because it
doesn't implement the org.openstreetmap.osmosis.core.task.v0_6.Source
interface.

Once you have constructed a pipeline, you can call the run method on the
XmlReader instance.  Task types designed to initiate a pipeline implement
the java.lang.Runnable interface.  Osmosis launches these in separate
threads, and you can also do this if you wish.

I won't create a complete code example for the pipeline described above
(for brevity reasons ... and laziness), but if If you were running a
pipeline like "*osmosis --read-xml planet.osm --write-xml output.osm*", you
would write code like this:

*XmlReader xmlReader = new XmlReader(new File("planet.osm"), true,
CompressionMethod.None);
XmlWriter xmlWriter = new XmlWriter(new File("output.osm"),
CompressionMethod.None);
xmlReader.setSink(xmlWriter);
xmlReader.run();*

At this point you may wish to have the output data fed directly into your
own code.  To do this you can create a class that implements the
org.openstreetmap.osmosis.core.task.v0_6.Sink interface, and pass your
object into the setSink method of an osmosis task instance making your
class part of the pipeline.

I hope that makes sense.
Brett
_______________________________________________
dev mailing list
dev@openstreetmap.org
http://lists.openstreetmap.org/listinfo/dev

Reply via email to