Search in sources :

Example 1 with OsmXmlIterator

use of de.topobyte.osm4j.xml.dynsax.OsmXmlIterator in project osm4j-core by topobyte.

the class BaseTestDataSets method prepare.

@Before
public void prepare() throws IOException {
    InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("data1.osm");
    OsmXmlIterator iterator = new OsmXmlIterator(input, false);
    data = loader.load(iterator);
    input.close();
}
Also used : OsmXmlIterator(de.topobyte.osm4j.xml.dynsax.OsmXmlIterator) InputStream(java.io.InputStream) Before(org.junit.Before)

Example 2 with OsmXmlIterator

use of de.topobyte.osm4j.xml.dynsax.OsmXmlIterator in project osm4j-geometry by topobyte.

the class TestBuildPolygon method main.

public static void main(String[] args) throws MalformedURLException, IOException, ParserConfigurationException, SAXException, EntityNotFoundException {
    String query = "http://overpass-api.de/api/interpreter?data=(rel(8638);>;);out;";
    // Open a stream
    InputStream input = new URL(query).openStream();
    OsmIterator iterator = new OsmXmlIterator(input, false);
    InMemoryMapDataSet data = MapDataSetLoader.read(iterator, false, false, true);
    TLongObjectMap<OsmRelation> relations = data.getRelations();
    if (relations.isEmpty()) {
        System.out.println("No relation found");
        return;
    }
    TLongObjectIterator<OsmRelation> ri = relations.iterator();
    ri.advance();
    OsmRelation relation = ri.value();
    File outputDir = new File("/tmp");
    String filename = String.format("relation-%d.wkt", relation.getId());
    File file = new File(outputDir, filename);
    GeometryBuilder geometryBuilder = new GeometryBuilder();
    geometryBuilder.setMissingEntitiesStrategy(MissingEntitiesStrategy.BUILD_PARTIAL);
    geometryBuilder.setMissingWayNodeStrategy(MissingWayNodeStrategy.OMIT_VERTEX_FROM_POLYLINE);
    Geometry polygon = geometryBuilder.build(relation, data);
    WKTWriter writer = new WKTWriter();
    FileWriter fileWriter = new FileWriter(file);
    writer.write(polygon, fileWriter);
    fileWriter.close();
    System.out.println("Successfully created WKT representation");
}
Also used : WKTWriter(com.vividsolutions.jts.io.WKTWriter) InMemoryMapDataSet(de.topobyte.osm4j.core.dataset.InMemoryMapDataSet) InputStream(java.io.InputStream) OsmRelation(de.topobyte.osm4j.core.model.iface.OsmRelation) FileWriter(java.io.FileWriter) URL(java.net.URL) Geometry(com.vividsolutions.jts.geom.Geometry) OsmXmlIterator(de.topobyte.osm4j.xml.dynsax.OsmXmlIterator) OsmIterator(de.topobyte.osm4j.core.access.OsmIterator) File(java.io.File) GeometryBuilder(de.topobyte.osm4j.geometry.GeometryBuilder)

Aggregations

OsmXmlIterator (de.topobyte.osm4j.xml.dynsax.OsmXmlIterator)2 InputStream (java.io.InputStream)2 Geometry (com.vividsolutions.jts.geom.Geometry)1 WKTWriter (com.vividsolutions.jts.io.WKTWriter)1 OsmIterator (de.topobyte.osm4j.core.access.OsmIterator)1 InMemoryMapDataSet (de.topobyte.osm4j.core.dataset.InMemoryMapDataSet)1 OsmRelation (de.topobyte.osm4j.core.model.iface.OsmRelation)1 GeometryBuilder (de.topobyte.osm4j.geometry.GeometryBuilder)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 URL (java.net.URL)1 Before (org.junit.Before)1