Search in sources :

Example 1 with OsmWay

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

the class OsmPbfRecordIterator method parseWay.

private void parseWay(final ProtocolBufferInputStream input) throws IOException {
    final OsmWay way = new OsmWay();
    final List<String> keys = new ArrayList<>();
    final List<String> values = new ArrayList<>();
    final List<Long> nodeIds = new ArrayList<>();
    final int inLength = input.startLengthDelimited();
    boolean running = true;
    long wayId = 0;
    while (running) {
        final int tag = input.readTag();
        switch(tag) {
            case 0:
                running = false;
                break;
            case 8:
                wayId = input.readInt64();
                way.setId(wayId);
                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, way);
                break;
            case 64:
                input.readLong(nodeIds);
                break;
            case 66:
                input.readLongs(nodeIds);
                break;
            default:
                input.skipField(tag);
                break;
        }
    }
    input.endLengthDelimited(inLength);
    addTags(way, keys, values);
    final List<Point> points = new ArrayList<>();
    long nodeId = 0;
    for (final long nodeIdOffset : nodeIds) {
        nodeId += nodeIdOffset;
        final Point point = this.nodePoints.get(nodeId);
        if (point != null) {
            points.add(point);
        }
    }
    if (nodeIds.size() == points.size()) {
        Geometry geometry;
        if (points.size() == 1) {
            geometry = points.get(0);
        } else {
            geometry = OsmConstants.WGS84_2D.lineString(points);
        }
        way.setGeometryValue(geometry);
        isPolygon(way, geometry);
        if (way.hasTags()) {
            this.currentRecords.add(way);
        }
        geometry = way.getGeometry();
        this.wayGeometries.put(wayId, geometry);
    } else {
        this.ways.add(way);
        this.wayNodeIds.add(nodeIds);
    }
}
Also used : OsmWay(com.revolsys.record.io.format.openstreetmap.model.OsmWay) Geometry(com.revolsys.geometry.model.Geometry) 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 OsmWay

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

the class OsmPbfRecordIterator method processWaysWithMissingNodes.

public Record processWaysWithMissingNodes() {
    while (!this.ways.isEmpty()) {
        final OsmWay way = this.ways.removeFirst();
        final List<Long> nodeIds = this.wayNodeIds.removeFirst();
        final List<LineString> lines = new ArrayList<>();
        final List<Point> points = new ArrayList<>();
        long nodeId = 0;
        for (final Long nodeIdRef : nodeIds) {
            nodeId += nodeIdRef;
            final Point point = this.nodePoints.get(nodeId);
            if (point == null) {
                if (points.size() > 1) {
                    final LineString line = OsmConstants.WGS84_2D.lineString(points);
                    lines.add(line);
                }
                points.clear();
            } else {
                points.add(point);
            }
        }
        if (points.size() > 1) {
            final LineString line = OsmConstants.WGS84_2D.lineString(points);
            lines.add(line);
        }
        if (!lines.isEmpty()) {
            final Geometry geometry = OsmConstants.WGS84_2D.geometry(lines);
            way.setGeometryValue(geometry);
            final long wayId = way.getId();
            this.wayGeometries.put(wayId, geometry);
            return way;
        }
    }
    throw new NoSuchElementException();
}
Also used : OsmWay(com.revolsys.record.io.format.openstreetmap.model.OsmWay) Geometry(com.revolsys.geometry.model.Geometry) LineString(com.revolsys.geometry.model.LineString) ArrayList(java.util.ArrayList) Point(com.revolsys.geometry.model.Point) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

Geometry (com.revolsys.geometry.model.Geometry)2 LineString (com.revolsys.geometry.model.LineString)2 Point (com.revolsys.geometry.model.Point)2 OsmWay (com.revolsys.record.io.format.openstreetmap.model.OsmWay)2 ArrayList (java.util.ArrayList)2 NoSuchElementException (java.util.NoSuchElementException)1