Search in sources :

Example 16 with OsmRelation

use of de.topobyte.osm4j.core.model.iface.OsmRelation in project osm4j-core by topobyte.

the class ImplUtil method clone.

public static Relation clone(OsmRelation relation) {
    List<? extends OsmTag> tags = cloneTags(relation);
    OsmMetadata metadata = cloneMetadata(relation);
    List<RelationMember> members = new ArrayList<>(relation.getNumberOfMembers());
    for (int i = 0; i < relation.getNumberOfMembers(); i++) {
        OsmRelationMember member = relation.getMember(i);
        members.add(new RelationMember(member.getId(), member.getType(), member.getRole()));
    }
    return new Relation(relation.getId(), members, tags, metadata);
}
Also used : Relation(de.topobyte.osm4j.core.model.impl.Relation) OsmRelation(de.topobyte.osm4j.core.model.iface.OsmRelation) RelationMember(de.topobyte.osm4j.core.model.impl.RelationMember) OsmRelationMember(de.topobyte.osm4j.core.model.iface.OsmRelationMember) OsmMetadata(de.topobyte.osm4j.core.model.iface.OsmMetadata) ArrayList(java.util.ArrayList) TLongArrayList(com.slimjars.dist.gnu.trove.list.array.TLongArrayList) OsmRelationMember(de.topobyte.osm4j.core.model.iface.OsmRelationMember)

Example 17 with OsmRelation

use of de.topobyte.osm4j.core.model.iface.OsmRelation in project osm4j-geometry by topobyte.

the class TestBuildLinework method main.

public static void main(String[] args) throws MalformedURLException, IOException, ParserConfigurationException, SAXException, EntityNotFoundException {
    if (args.length == 0) {
        System.out.println("Please specify an input file");
        usage();
    }
    if (args.length != 1) {
        System.out.println("Please specify only one input file");
        usage();
    }
    InputStream input = new FileInputStream(args[0]);
    OsmIterator iterator = new TboIterator(input, true, true);
    InMemoryMapDataSet data = MapDataSetLoader.read(iterator, false, false, true);
    TLongObjectMap<OsmRelation> relations = data.getRelations();
    if (relations.isEmpty()) {
        logger.warn("No relation found");
        return;
    }
    LineworkBuilder lineworkBuilder = new LineworkBuilder();
    lineworkBuilder.setMissingEntitiesStrategy(MissingEntitiesStrategy.BUILD_PARTIAL);
    lineworkBuilder.setMissingWayNodeStrategy(MissingWayNodeStrategy.SPLIT_POLYLINE);
    RegionBuilder regionBuilder = new RegionBuilder();
    regionBuilder.setMissingEntitiesStrategy(MissingEntitiesStrategy.BUILD_PARTIAL);
    long[] ids = relations.keys();
    Arrays.sort(ids);
    for (long id : ids) {
        OsmRelation relation = relations.get(id);
        logger.info("Relation " + relation.getId());
        lineworkBuilder.build(relation, data);
        regionBuilder.build(relation, data);
    }
}
Also used : InMemoryMapDataSet(de.topobyte.osm4j.core.dataset.InMemoryMapDataSet) TboIterator(de.topobyte.osm4j.tbo.access.TboIterator) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OsmRelation(de.topobyte.osm4j.core.model.iface.OsmRelation) RegionBuilder(de.topobyte.osm4j.geometry.RegionBuilder) OsmIterator(de.topobyte.osm4j.core.access.OsmIterator) FileInputStream(java.io.FileInputStream) LineworkBuilder(de.topobyte.osm4j.geometry.LineworkBuilder)

Example 18 with OsmRelation

use of de.topobyte.osm4j.core.model.iface.OsmRelation 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)

Example 19 with OsmRelation

use of de.topobyte.osm4j.core.model.iface.OsmRelation in project osm4j-geometry by topobyte.

the class RegionBuilder method build.

/**
 * Build a MultiPolygon from a Relation.
 *
 * @param relation
 *            a relation to construct the region for.
 * @return the constructed MultiPolygon.
 * @throws EntityNotFoundException
 *             when a required entity cannot be obtained.
 */
public RegionBuilderResult build(OsmRelation relation, OsmEntityProvider resolver) throws EntityNotFoundException {
    EntityNotFoundStrategy enfs = Util.strategy(missingEntitiesStrategy, log, logLevel);
    logger.debug("building region id:" + relation.getId());
    Set<OsmRelation> relations = new HashSet<>();
    MultiSet<OsmWay> ways = new HashMultiSet<>();
    EntityFinder finder = EntityFinders.create(resolver, enfs);
    relations.add(relation);
    finder.findMemberRelationsRecursively(relation, relations);
    finder.findMemberWays(relations, ways);
    Set<OsmNode> nodes = new HashSet<>();
    if (includePuntal) {
        finder.findMemberNodes(relations, nodes);
    }
    return build(ways, resolver, nodes);
}
Also used : OsmWay(de.topobyte.osm4j.core.model.iface.OsmWay) OsmNode(de.topobyte.osm4j.core.model.iface.OsmNode) OsmRelation(de.topobyte.osm4j.core.model.iface.OsmRelation) HashMultiSet(de.topobyte.adt.multicollections.HashMultiSet) EntityNotFoundStrategy(de.topobyte.osm4j.core.resolve.EntityNotFoundStrategy) EntityFinder(de.topobyte.osm4j.core.resolve.EntityFinder) HashSet(java.util.HashSet)

Example 20 with OsmRelation

use of de.topobyte.osm4j.core.model.iface.OsmRelation in project osm4j-pbf by topobyte.

the class PbfWriter method serializeRelations.

private Osmformat.PrimitiveGroup serializeRelations(Collection<OsmRelation> relations) {
    Osmformat.PrimitiveGroup.Builder builder = Osmformat.PrimitiveGroup.newBuilder();
    for (OsmRelation relation : relations) {
        Osmformat.Relation.Builder bi = Osmformat.Relation.newBuilder();
        bi.setId(relation.getId());
        long lastid = 0;
        for (int k = 0; k < relation.getNumberOfMembers(); k++) {
            OsmRelationMember j = relation.getMember(k);
            long id = j.getId();
            bi.addMemids(id - lastid);
            lastid = id;
            EntityType t = j.getType();
            Osmformat.Relation.MemberType type = getType(t);
            bi.addTypes(type);
            bi.addRolesSid(stringTable.getIndex(j.getRole()));
        }
        for (int k = 0; k < relation.getNumberOfTags(); k++) {
            OsmTag t = relation.getTag(k);
            bi.addKeys(stringTable.getIndex(t.getKey()));
            bi.addVals(stringTable.getIndex(t.getValue()));
        }
        if (writeMetadata && relation.getMetadata() != null) {
            bi.setInfo(serializeMetadata(relation));
        }
        builder.addRelations(bi);
    }
    return builder.build();
}
Also used : PrimitiveGroup(de.topobyte.osm4j.pbf.protobuf.Osmformat.PrimitiveGroup) EntityType(de.topobyte.osm4j.core.model.iface.EntityType) OsmRelation(de.topobyte.osm4j.core.model.iface.OsmRelation) OsmRelation(de.topobyte.osm4j.core.model.iface.OsmRelation) OsmTag(de.topobyte.osm4j.core.model.iface.OsmTag) OsmRelationMember(de.topobyte.osm4j.core.model.iface.OsmRelationMember)

Aggregations

OsmRelation (de.topobyte.osm4j.core.model.iface.OsmRelation)21 ArrayList (java.util.ArrayList)9 OsmRelationMember (de.topobyte.osm4j.core.model.iface.OsmRelationMember)8 OsmNode (de.topobyte.osm4j.core.model.iface.OsmNode)7 OsmWay (de.topobyte.osm4j.core.model.iface.OsmWay)7 TLongArrayList (com.slimjars.dist.gnu.trove.list.array.TLongArrayList)6 Relation (de.topobyte.osm4j.core.model.impl.Relation)6 TLongSet (com.slimjars.dist.gnu.trove.set.TLongSet)4 OsmHandler (de.topobyte.osm4j.core.access.OsmHandler)4 OsmBounds (de.topobyte.osm4j.core.model.iface.OsmBounds)4 Node (de.topobyte.osm4j.core.model.impl.Node)4 Way (de.topobyte.osm4j.core.model.impl.Way)4 IOException (java.io.IOException)4 TLongIterator (com.slimjars.dist.gnu.trove.iterator.TLongIterator)3 TLongHashSet (com.slimjars.dist.gnu.trove.set.hash.TLongHashSet)3 InputStream (java.io.InputStream)3 OsmIterator (de.topobyte.osm4j.core.access.OsmIterator)2 InMemoryMapDataSet (de.topobyte.osm4j.core.dataset.InMemoryMapDataSet)2 EntityContainer (de.topobyte.osm4j.core.model.iface.EntityContainer)2 EntityType (de.topobyte.osm4j.core.model.iface.EntityType)2