Search in sources :

Example 1 with OsmNode

use of com.revolsys.record.io.format.openstreetmap.model.OsmNode in project com.revolsys.open by revolsys.

the class OsmPbfRecordIterator method parseNode.

private void parseNode(final ProtocolBufferInputStream input) throws IOException {
    final OsmNode node = new OsmNode();
    final List<String> keys = new ArrayList<>();
    final List<String> values = new ArrayList<>();
    double lat = 0;
    double lon = 0;
    final int inLength = input.startLengthDelimited();
    boolean running = true;
    while (running) {
        final int tag = input.readTag();
        switch(tag) {
            case 0:
                running = false;
                break;
            case 8:
                final long id = input.readInt64();
                node.setId(id);
                break;
            case 16:
                readStringById(input, keys);
                break;
            case 18:
                readStringsByIds(input, keys);
                break;
            case 24:
                readStringById(input, values);
                break;
            case 26:
                readStringsByIds(input, values);
                break;
            case 34:
                parseInfo(input, node);
                break;
            case 64:
                lat = toDegrees(input.readSInt64());
                break;
            case 72:
                lon = toDegrees(input.readSInt64());
                break;
            default:
                input.skipField(tag);
                break;
        }
    }
    input.endLengthDelimited(inLength);
    final Point point = OsmConstants.WGS84_2D.point(lat, lon);
    node.setGeometryValue(point);
    addTags(node, keys, values);
    this.currentRecords.add(node);
}
Also used : OsmNode(com.revolsys.record.io.format.openstreetmap.model.OsmNode) ArrayList(java.util.ArrayList) LineString(com.revolsys.geometry.model.LineString) Point(com.revolsys.geometry.model.Point) Point(com.revolsys.geometry.model.Point)

Example 2 with OsmNode

use of com.revolsys.record.io.format.openstreetmap.model.OsmNode in project com.revolsys.open by revolsys.

the class OsmPbfRecordIterator method parseDenseNodes.

private void parseDenseNodes(final ProtocolBufferInputStream in) throws IOException {
    final List<Long> ids = new ArrayList<>();
    final List<Double> latitudes = new ArrayList<>();
    final List<Double> longitudes = new ArrayList<>();
    final List<String> keysAndValues = new ArrayList<>();
    DenseInfo denseInfo = null;
    final int inLength = in.startLengthDelimited();
    boolean running = true;
    while (running) {
        final int tag = in.readTag();
        switch(tag) {
            case 0:
                running = false;
                break;
            case 8:
                in.readLong(ids);
                break;
            case 10:
                in.readLongs(ids);
                break;
            case 42:
                denseInfo = parseDenseInfo(in);
                break;
            case 64:
                readDegreesById(in, latitudes);
                break;
            case 66:
                readDegreesByIds(in, latitudes);
                break;
            case 72:
                readDegreesById(in, longitudes);
                break;
            case 74:
                readDegreesByIds(in, longitudes);
                break;
            case 80:
                readStringById(in, keysAndValues);
                break;
            case 82:
                readStringsByIds(in, keysAndValues);
                break;
            default:
                in.skipField(tag);
                break;
        }
    }
    in.endLengthDelimited(inLength);
    if (ids.size() != latitudes.size() || ids.size() != longitudes.size()) {
        throw new RuntimeException("Number of ids (" + ids.size() + "), latitudes (" + latitudes.size() + "), and longitudes (" + longitudes.size() + ") don't match");
    }
    if (denseInfo == null && keysAndValues.isEmpty()) {
        for (int i = 0; i < ids.size(); i++) {
            final long id = ids.get(i);
            final double latitude = latitudes.get(i);
            final double longitude = longitudes.get(i);
            final Point point = new PointDoubleXY(longitude, latitude);
            this.nodePoints.put(id, point);
        }
    } else {
        final Iterator<String> keysAndValuesIterator = keysAndValues.iterator();
        long id = 0;
        for (int i = 0; i < ids.size(); i++) {
            final long idOffset = ids.get(i);
            id += idOffset;
            final double latitude = latitudes.get(i);
            final double longitude = longitudes.get(i);
            final Point point = OsmConstants.WGS84_2D.point(longitude, latitude);
            this.nodePoints.put(id, point);
            OsmNode node = null;
            while (keysAndValuesIterator.hasNext()) {
                final String key = keysAndValuesIterator.next();
                if (key.length() == 0) {
                    break;
                }
                if (!keysAndValuesIterator.hasNext()) {
                    throw new RuntimeException("The PBF DenseInfo keys/values list contains a key with no corresponding value.");
                }
                if (node == null) {
                    node = new OsmNode();
                    node.setId(id);
                    node.setGeometryValue(point);
                    this.currentRecords.add(node);
                }
                final String value = keysAndValuesIterator.next();
                node.addTag(key, value);
            }
            if (denseInfo != null && node != null) {
                node.setVersion(denseInfo.versions.get(i));
                node.setChangeset(denseInfo.changesets.get(i));
                node.setTimestamp(denseInfo.timestamps.get(i));
                node.setUid(denseInfo.uids.get(i));
                node.setUser(denseInfo.userNames.get(i));
                node.setVisible(denseInfo.visibles.get(i));
            }
        }
    }
}
Also used : OsmNode(com.revolsys.record.io.format.openstreetmap.model.OsmNode) ArrayList(java.util.ArrayList) LineString(com.revolsys.geometry.model.LineString) Point(com.revolsys.geometry.model.Point) Point(com.revolsys.geometry.model.Point) PointDoubleXY(com.revolsys.geometry.model.impl.PointDoubleXY)

Aggregations

LineString (com.revolsys.geometry.model.LineString)2 Point (com.revolsys.geometry.model.Point)2 OsmNode (com.revolsys.record.io.format.openstreetmap.model.OsmNode)2 ArrayList (java.util.ArrayList)2 PointDoubleXY (com.revolsys.geometry.model.impl.PointDoubleXY)1