There's not only an API for reading data, but also for writing. The core interface for writing data is the OsmOutputStream. You can feed OsmEntity instances to it and they will be written to a regular OutputStream or File in the chosen file format.
There are currently three major file format implementations available and each provides an OsmOutputStream implementation:
The following example will download some XML data contained in a bounding box from the Overpass API and store it in a PBF file on disk.
| public static void main(String[] args) throws IOException |
| { |
| // Define a query to retrieve some data |
| String query = "http://www.overpass-api.de/api/xapi?*[bbox=" |
| + "13.465661,52.504055,13.469817,52.506204]"; |
| // Open a stream |
| InputStream input = new URL(query).openStream(); |
| // Create an iterator for XML data |
| OsmIterator iterator = new OsmXmlIterator(input, true); |
| // Create an output stream |
| OutputStream output = new FileOutputStream("/tmp/bbox.pbf"); |
| OsmOutputStream osmOutput = new PbfWriter(output, true); |
| // Iterate objects and copy them to the output |
| for (EntityContainer container : iterator) { |
| switch (container.getType()) { |
| default: |
| case Node: |
| osmOutput.write((OsmNode) container.getEntity()); |
| break; |
| case Way: |
| osmOutput.write((OsmWay) container.getEntity()); |
| break; |
| case Relation: |
| osmOutput.write((OsmRelation) container.getEntity()); |
| break; |
| } |
| } |
| // Close output |
| osmOutput.complete(); |
| output.close(); |
| } |
You just create a PbfWriter instance and then call the write() method for all objects you would like to write to the file.
It is important, that you call complete() on the OsmOutputStream when you're done, so that the underlying implementation may finish the output operation. This makes sure, all data is written to the output completely.
Depending on the output you have passed to the PbfWriter at construction, it may also be necessary to call another method to ensure all data is written. In the example, we're using an output stream and we call close() to make sure everything is written to disk.
Of course, the reverse is also possible: reading from PBF and writing XML. Assuming that you have run the previous example, the following code will open the PBF file generated previously and write it as XML to the console.
| public static void main(String[] args) throws IOException |
| { |
| // Open a file as input |
| InputStream input = new FileInputStream("/tmp/bbox.pbf"); |
| // Create an iterator for PBF data |
| OsmIterator iterator = new PbfIterator(input, true); |
| // Create an output stream |
| OsmOutputStream osmOutput = new OsmXmlOutputStream(System.out, true); |
| // Iterate objects and copy them to the output |
| for (EntityContainer container : iterator) { |
| switch (container.getType()) { |
| default: |
| case Node: |
| osmOutput.write((OsmNode) container.getEntity()); |
| break; |
| case Way: |
| osmOutput.write((OsmWay) container.getEntity()); |
| break; |
| case Relation: |
| osmOutput.write((OsmRelation) container.getEntity()); |
| break; |
| } |
| } |
| // Close output |
| osmOutput.complete(); |
| } |