Search in sources :

Example 1 with OsmWay

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

the class PbfWriter method serializeWays.

private Osmformat.PrimitiveGroup serializeWays(Collection<OsmWay> ways) {
    Osmformat.PrimitiveGroup.Builder builder = Osmformat.PrimitiveGroup.newBuilder();
    for (OsmWay way : ways) {
        Osmformat.Way.Builder bi = Osmformat.Way.newBuilder();
        bi.setId(way.getId());
        long lastid = 0;
        for (int k = 0; k < way.getNumberOfNodes(); k++) {
            long id = way.getNodeId(k);
            bi.addRefs(id - lastid);
            lastid = id;
        }
        for (int k = 0; k < way.getNumberOfTags(); k++) {
            OsmTag t = way.getTag(k);
            bi.addKeys(stringTable.getIndex(t.getKey()));
            bi.addVals(stringTable.getIndex(t.getValue()));
        }
        if (writeMetadata && way.getMetadata() != null) {
            bi.setInfo(serializeMetadata(way));
        }
        builder.addWays(bi);
    }
    return builder.build();
}
Also used : PrimitiveGroup(de.topobyte.osm4j.pbf.protobuf.Osmformat.PrimitiveGroup) OsmWay(de.topobyte.osm4j.core.model.iface.OsmWay) OsmTag(de.topobyte.osm4j.core.model.iface.OsmTag) OsmWay(de.topobyte.osm4j.core.model.iface.OsmWay)

Example 2 with OsmWay

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

the class PrimParser method convert.

public OsmWay convert(Osmformat.Way w) {
    long id = w.getId();
    TLongArrayList nodes = new TLongArrayList();
    long lastId = 0;
    for (long j : w.getRefsList()) {
        nodes.add(j + lastId);
        lastId = j + lastId;
    }
    List<OsmTag> tags = new ArrayList<>();
    for (int j = 0; j < w.getKeysCount(); j++) {
        tags.add(new Tag(strings[w.getKeys(j)], strings[w.getVals(j)]));
    }
    OsmMetadata metadata = null;
    if (fetchMetadata && w.hasInfo()) {
        Osmformat.Info info = w.getInfo();
        metadata = convertMetadata(info);
    }
    return new Way(id, nodes, tags, metadata);
}
Also used : TLongArrayList(com.slimjars.dist.gnu.trove.list.array.TLongArrayList) OsmMetadata(de.topobyte.osm4j.core.model.iface.OsmMetadata) OsmTag(de.topobyte.osm4j.core.model.iface.OsmTag) ArrayList(java.util.ArrayList) TLongArrayList(com.slimjars.dist.gnu.trove.list.array.TLongArrayList) Tag(de.topobyte.osm4j.core.model.impl.Tag) OsmTag(de.topobyte.osm4j.core.model.iface.OsmTag) Osmformat(de.topobyte.osm4j.pbf.protobuf.Osmformat) OsmWay(de.topobyte.osm4j.core.model.iface.OsmWay) Way(de.topobyte.osm4j.core.model.impl.Way)

Example 3 with OsmWay

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

the class CopyElementwise method main.

public static void main(String[] args) throws IOException, OsmInputException {
    if (args.length != 2) {
        System.out.println("usage: " + CopyElementwise.class.getSimpleName() + " <input> <output>");
        System.exit(1);
    }
    InputStream input = new FileInputStream(args[0]);
    OutputStream output = new FileOutputStream(args[1]);
    final PbfWriter writer = new PbfWriter(output, true);
    PbfParser parser = new PbfParser(new OsmHandler() {

        @Override
        public void handle(OsmBounds bounds) throws IOException {
            writer.write(bounds);
        }

        @Override
        public void handle(OsmNode node) throws IOException {
            writer.write(node);
        }

        @Override
        public void handle(OsmWay way) throws IOException {
            writer.write(way);
        }

        @Override
        public void handle(OsmRelation relation) throws IOException {
            writer.write(relation);
        }

        @Override
        public void complete() throws IOException {
            writer.complete();
        }
    }, true);
    parser.parse(input);
    output.close();
}
Also used : OsmBounds(de.topobyte.osm4j.core.model.iface.OsmBounds) OsmNode(de.topobyte.osm4j.core.model.iface.OsmNode) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) OsmRelation(de.topobyte.osm4j.core.model.iface.OsmRelation) OsmHandler(de.topobyte.osm4j.core.access.OsmHandler) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) OsmWay(de.topobyte.osm4j.core.model.iface.OsmWay) PbfParser(de.topobyte.osm4j.pbf.seq.PbfParser) FileOutputStream(java.io.FileOutputStream) PbfWriter(de.topobyte.osm4j.pbf.seq.PbfWriter)

Example 4 with OsmWay

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

the class IdDataSetReader method read.

public static InMemorySetIdDataSet read(OsmReader reader) throws OsmInputException {
    final InMemorySetIdDataSet dataSet = new InMemorySetIdDataSet();
    final TLongSet nodeIds = dataSet.getNodeIds();
    final TLongSet wayIds = dataSet.getWayIds();
    final TLongSet relationIds = dataSet.getRelationIds();
    reader.setHandler(new OsmHandler() {

        @Override
        public void handle(OsmBounds bounds) throws IOException {
            dataSet.setBounds(bounds);
        }

        @Override
        public void handle(OsmNode node) throws IOException {
            nodeIds.add(node.getId());
        }

        @Override
        public void handle(OsmWay way) throws IOException {
            wayIds.add(way.getId());
        }

        @Override
        public void handle(OsmRelation relation) throws IOException {
            relationIds.add(relation.getId());
        }

        @Override
        public void complete() throws IOException {
        // nothing to do here
        }
    });
    reader.read();
    return dataSet;
}
Also used : OsmWay(de.topobyte.osm4j.core.model.iface.OsmWay) OsmBounds(de.topobyte.osm4j.core.model.iface.OsmBounds) OsmNode(de.topobyte.osm4j.core.model.iface.OsmNode) TLongSet(com.slimjars.dist.gnu.trove.set.TLongSet) OsmRelation(de.topobyte.osm4j.core.model.iface.OsmRelation) OsmHandler(de.topobyte.osm4j.core.access.OsmHandler) IOException(java.io.IOException)

Example 5 with OsmWay

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

the class ListDataSetLoader method read.

public static InMemoryListDataSet read(OsmReader reader, final boolean keepNodeTags, final boolean keepWayTags, final boolean keepRelationTags) throws OsmInputException {
    final InMemoryListDataSet dataSet = new InMemoryListDataSet();
    final List<OsmNode> nodes = dataSet.getNodes();
    final List<OsmWay> ways = dataSet.getWays();
    final List<OsmRelation> relations = dataSet.getRelations();
    reader.setHandler(new OsmHandler() {

        @Override
        public void handle(OsmBounds bounds) throws IOException {
            dataSet.setBounds(bounds);
        }

        @Override
        public void handle(OsmNode node) throws IOException {
            if (!keepNodeTags) {
                node = new Node(node.getId(), node.getLongitude(), node.getLatitude());
            }
            nodes.add(node);
        }

        @Override
        public void handle(OsmWay way) throws IOException {
            if (!keepWayTags) {
                TLongArrayList ids = new TLongArrayList();
                for (int i = 0; i < way.getNumberOfNodes(); i++) {
                    ids.add(way.getNodeId(i));
                }
                way = new Way(way.getId(), ids);
            }
            ways.add(way);
        }

        @Override
        public void handle(OsmRelation relation) throws IOException {
            if (!keepRelationTags) {
                List<OsmRelationMember> members = new ArrayList<>();
                for (int i = 0; i < relation.getNumberOfMembers(); i++) {
                    members.add(relation.getMember(i));
                }
                relation = new Relation(relation.getId(), members);
            }
            relations.add(relation);
        }

        @Override
        public void complete() throws IOException {
        // nothing to do here
        }
    });
    reader.read();
    return dataSet;
}
Also used : OsmBounds(de.topobyte.osm4j.core.model.iface.OsmBounds) TLongArrayList(com.slimjars.dist.gnu.trove.list.array.TLongArrayList) OsmNode(de.topobyte.osm4j.core.model.iface.OsmNode) OsmRelation(de.topobyte.osm4j.core.model.iface.OsmRelation) Node(de.topobyte.osm4j.core.model.impl.Node) OsmNode(de.topobyte.osm4j.core.model.iface.OsmNode) OsmHandler(de.topobyte.osm4j.core.access.OsmHandler) IOException(java.io.IOException) OsmWay(de.topobyte.osm4j.core.model.iface.OsmWay) Way(de.topobyte.osm4j.core.model.impl.Way) OsmWay(de.topobyte.osm4j.core.model.iface.OsmWay) Relation(de.topobyte.osm4j.core.model.impl.Relation) OsmRelation(de.topobyte.osm4j.core.model.iface.OsmRelation) ArrayList(java.util.ArrayList) TLongArrayList(com.slimjars.dist.gnu.trove.list.array.TLongArrayList) List(java.util.List)

Aggregations

OsmWay (de.topobyte.osm4j.core.model.iface.OsmWay)24 OsmNode (de.topobyte.osm4j.core.model.iface.OsmNode)10 ArrayList (java.util.ArrayList)10 TLongArrayList (com.slimjars.dist.gnu.trove.list.array.TLongArrayList)9 OsmRelation (de.topobyte.osm4j.core.model.iface.OsmRelation)7 Way (de.topobyte.osm4j.core.model.impl.Way)7 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 Relation (de.topobyte.osm4j.core.model.impl.Relation)4 IOException (java.io.IOException)4 HashSet (java.util.HashSet)4 TLongIterator (com.slimjars.dist.gnu.trove.iterator.TLongIterator)3 EntityFinder (de.topobyte.osm4j.core.resolve.EntityFinder)3 EntityNotFoundException (de.topobyte.osm4j.core.resolve.EntityNotFoundException)3 EntityNotFoundStrategy (de.topobyte.osm4j.core.resolve.EntityNotFoundStrategy)3 TLongList (com.slimjars.dist.gnu.trove.list.TLongList)2 EntityContainer (de.topobyte.osm4j.core.model.iface.EntityContainer)2 OsmMetadata (de.topobyte.osm4j.core.model.iface.OsmMetadata)2 OsmRelationMember (de.topobyte.osm4j.core.model.iface.OsmRelationMember)2