Search in sources :

Example 1 with OsmTag

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

the class PbfWriter method serializeDense.

private Osmformat.PrimitiveGroup serializeDense(Collection<OsmNode> nodes) {
    Osmformat.PrimitiveGroup.Builder builder = Osmformat.PrimitiveGroup.newBuilder();
    long lastlat = 0, lastlon = 0, lastid = 0;
    Osmformat.DenseNodes.Builder bi = Osmformat.DenseNodes.newBuilder();
    // Does anything in this block have tags?
    boolean doesBlockHaveTags = false;
    for (OsmNode node : nodes) {
        if (node.getNumberOfTags() != 0) {
            doesBlockHaveTags = true;
            break;
        }
    }
    // Find out if any of the nodes has metadata. If none does, we can omit
    // the metadata completely.
    boolean hasMetadata = false;
    for (OsmNode node : nodes) {
        if (node.getMetadata() != null) {
            hasMetadata = true;
        }
    }
    if (writeMetadata && hasMetadata) {
        Osmformat.DenseInfo.Builder bdi = Osmformat.DenseInfo.newBuilder();
        serializeMetadataDense(bdi, nodes);
        bi.setDenseinfo(bdi);
    }
    for (OsmNode node : nodes) {
        long id = node.getId();
        int lat = mapDegrees(node.getLatitude());
        int lon = mapDegrees(node.getLongitude());
        bi.addId(id - lastid);
        lastid = id;
        bi.addLon(lon - lastlon);
        lastlon = lon;
        bi.addLat(lat - lastlat);
        lastlat = lat;
        // Then we must include tag information.
        if (doesBlockHaveTags) {
            for (int k = 0; k < node.getNumberOfTags(); k++) {
                OsmTag t = node.getTag(k);
                bi.addKeysVals(stringTable.getIndex(t.getKey()));
                bi.addKeysVals(stringTable.getIndex(t.getValue()));
            }
            // Add delimiter.
            bi.addKeysVals(0);
        }
    }
    builder.setDense(bi);
    return builder.build();
}
Also used : PrimitiveGroup(de.topobyte.osm4j.pbf.protobuf.Osmformat.PrimitiveGroup) OsmNode(de.topobyte.osm4j.core.model.iface.OsmNode) OsmTag(de.topobyte.osm4j.core.model.iface.OsmTag)

Example 2 with OsmTag

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

the class PbfWriter method serializeNonDense.

private Osmformat.PrimitiveGroup serializeNonDense(Collection<OsmNode> nodes) {
    Osmformat.PrimitiveGroup.Builder builder = Osmformat.PrimitiveGroup.newBuilder();
    for (OsmNode node : nodes) {
        Osmformat.Node.Builder bi = Osmformat.Node.newBuilder();
        bi.setId(node.getId());
        bi.setLon(mapDegrees(node.getLongitude()));
        bi.setLat(mapDegrees(node.getLatitude()));
        for (int k = 0; k < node.getNumberOfTags(); k++) {
            OsmTag t = node.getTag(k);
            bi.addKeys(stringTable.getIndex(t.getKey()));
            bi.addVals(stringTable.getIndex(t.getValue()));
        }
        if (writeMetadata && node.getMetadata() != null) {
            bi.setInfo(serializeMetadata(node));
        }
        builder.addNodes(bi);
    }
    return builder.build();
}
Also used : PrimitiveGroup(de.topobyte.osm4j.pbf.protobuf.Osmformat.PrimitiveGroup) OsmNode(de.topobyte.osm4j.core.model.iface.OsmNode) OsmNode(de.topobyte.osm4j.core.model.iface.OsmNode) OsmTag(de.topobyte.osm4j.core.model.iface.OsmTag)

Example 3 with OsmTag

use of de.topobyte.osm4j.core.model.iface.OsmTag 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 4 with OsmTag

use of de.topobyte.osm4j.core.model.iface.OsmTag 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 5 with OsmTag

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

the class PrimParser method convert.

public OsmNode convert(Osmformat.Node n) {
    long id = n.getId();
    double lat = parseLat(n.getLat());
    double lon = parseLon(n.getLon());
    List<OsmTag> tags = new ArrayList<>();
    for (int j = 0; j < n.getKeysCount(); j++) {
        tags.add(new Tag(strings[n.getKeys(j)], strings[n.getVals(j)]));
    }
    OsmMetadata metadata = null;
    if (fetchMetadata && n.hasInfo()) {
        Osmformat.Info info = n.getInfo();
        metadata = convertMetadata(info);
    }
    return new Node(id, lon, lat, tags, metadata);
}
Also used : OsmMetadata(de.topobyte.osm4j.core.model.iface.OsmMetadata) OsmTag(de.topobyte.osm4j.core.model.iface.OsmTag) Node(de.topobyte.osm4j.core.model.impl.Node) OsmNode(de.topobyte.osm4j.core.model.iface.OsmNode) 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)

Aggregations

OsmTag (de.topobyte.osm4j.core.model.iface.OsmTag)13 TLongArrayList (com.slimjars.dist.gnu.trove.list.array.TLongArrayList)7 ArrayList (java.util.ArrayList)7 Tag (de.topobyte.osm4j.core.model.impl.Tag)6 OsmMetadata (de.topobyte.osm4j.core.model.iface.OsmMetadata)5 OsmNode (de.topobyte.osm4j.core.model.iface.OsmNode)5 Osmformat (de.topobyte.osm4j.pbf.protobuf.Osmformat)5 PrimitiveGroup (de.topobyte.osm4j.pbf.protobuf.Osmformat.PrimitiveGroup)4 Node (de.topobyte.osm4j.core.model.impl.Node)3 EntityType (de.topobyte.osm4j.core.model.iface.EntityType)2 OsmRelation (de.topobyte.osm4j.core.model.iface.OsmRelation)2 OsmWay (de.topobyte.osm4j.core.model.iface.OsmWay)2 Metadata (de.topobyte.osm4j.core.model.impl.Metadata)2 OsmEntity (de.topobyte.osm4j.core.model.iface.OsmEntity)1 OsmRelationMember (de.topobyte.osm4j.core.model.iface.OsmRelationMember)1 Relation (de.topobyte.osm4j.core.model.impl.Relation)1 RelationMember (de.topobyte.osm4j.core.model.impl.RelationMember)1 Way (de.topobyte.osm4j.core.model.impl.Way)1 HashMap (java.util.HashMap)1