Search in sources :

Example 1 with OsmNode

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

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

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

Example 4 with OsmNode

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

the class PrimParser method convert.

public List<OsmNode> convert(Osmformat.DenseNodes nodes) {
    List<OsmNode> results = new ArrayList<>(nodes.getIdCount());
    Osmformat.DenseInfo denseInfo = null;
    boolean hasVisible = false;
    if (fetchMetadata && nodes.hasDenseinfo()) {
        denseInfo = nodes.getDenseinfo();
        hasVisible = denseInfo.getVisibleCount() != 0;
    }
    long id = 0, lat = 0, lon = 0;
    int version = 0, uid = 0, userSid = 0;
    long timestamp = 0, changeset = 0;
    // Index into the keysvals array.
    int j = 0;
    for (int i = 0; i < nodes.getIdCount(); i++) {
        id += nodes.getId(i);
        lat += nodes.getLat(i);
        lon += nodes.getLon(i);
        double latf = parseLat(lat), lonf = parseLon(lon);
        List<OsmTag> tags = new ArrayList<>();
        OsmMetadata metadata = null;
        if (fetchMetadata && nodes.hasDenseinfo()) {
            version = denseInfo.getVersion(i);
            timestamp += denseInfo.getTimestamp(i);
            uid += denseInfo.getUid(i);
            userSid += denseInfo.getUserSid(i);
            changeset += denseInfo.getChangeset(i);
            boolean visible = true;
            if (hasVisible) {
                visible = denseInfo.getVisible(i);
            }
            metadata = new Metadata(version, timestamp * dateGranularity, uid, strings[userSid], changeset, visible);
        }
        // If empty, assume that nothing here has keys or vals.
        if (nodes.getKeysValsCount() > 0) {
            while (nodes.getKeysVals(j) != 0) {
                int keyid = nodes.getKeysVals(j++);
                int valid = nodes.getKeysVals(j++);
                tags.add(new Tag(strings[keyid], strings[valid]));
            }
            // Skip over the '0' delimiter.
            j++;
        }
        results.add(new Node(id, lonf, latf, tags, metadata));
    }
    return results;
}
Also used : OsmMetadata(de.topobyte.osm4j.core.model.iface.OsmMetadata) OsmNode(de.topobyte.osm4j.core.model.iface.OsmNode) 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) Metadata(de.topobyte.osm4j.core.model.impl.Metadata) OsmMetadata(de.topobyte.osm4j.core.model.iface.OsmMetadata) Osmformat(de.topobyte.osm4j.pbf.protobuf.Osmformat) Tag(de.topobyte.osm4j.core.model.impl.Tag) OsmTag(de.topobyte.osm4j.core.model.iface.OsmTag)

Example 5 with OsmNode

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

Aggregations

OsmNode (de.topobyte.osm4j.core.model.iface.OsmNode)25 OsmWay (de.topobyte.osm4j.core.model.iface.OsmWay)10 ArrayList (java.util.ArrayList)10 OsmRelation (de.topobyte.osm4j.core.model.iface.OsmRelation)7 Node (de.topobyte.osm4j.core.model.impl.Node)7 TLongArrayList (com.slimjars.dist.gnu.trove.list.array.TLongArrayList)6 EntityNotFoundException (de.topobyte.osm4j.core.resolve.EntityNotFoundException)6 CoordinateSequence (com.vividsolutions.jts.geom.CoordinateSequence)4 OsmHandler (de.topobyte.osm4j.core.access.OsmHandler)4 OsmBounds (de.topobyte.osm4j.core.model.iface.OsmBounds)4 OsmTag (de.topobyte.osm4j.core.model.iface.OsmTag)4 Relation (de.topobyte.osm4j.core.model.impl.Relation)4 Way (de.topobyte.osm4j.core.model.impl.Way)4 IOException (java.io.IOException)4 TLongIterator (com.slimjars.dist.gnu.trove.iterator.TLongIterator)3 OsmMetadata (de.topobyte.osm4j.core.model.iface.OsmMetadata)3 EntityFinder (de.topobyte.osm4j.core.resolve.EntityFinder)3 EntityNotFoundStrategy (de.topobyte.osm4j.core.resolve.EntityNotFoundStrategy)3 HashSet (java.util.HashSet)3 Coordinate (com.vividsolutions.jts.geom.Coordinate)2