Search in sources :

Example 1 with GeomCmd

use of com.wdtinc.mapbox_vector_tile.encoding.GeomCmd in project Osmand by osmandapp.

the class MvtReader method readPoints.

/**
 * Create {@link Point} or {@link MultiPoint} from MVT geometry drawing commands.
 *
 * @param geomFactory creates JTS geometry
 * @param geomCmds    contains MVT geometry commands
 * @param cursor      contains current MVT extent position
 * @return JTS geometry or null on failure
 */
private static Geometry readPoints(GeometryFactory geomFactory, List<Integer> geomCmds, Vec2d cursor) {
    // Guard: must have header
    if (geomCmds.isEmpty()) {
        return null;
    }
    /**
     * Geometry command index
     */
    int i = 0;
    // Read command header
    final int cmdHdr = geomCmds.get(i++);
    final int cmdLength = GeomCmdHdr.getCmdLength(cmdHdr);
    final GeomCmd cmd = GeomCmdHdr.getCmd(cmdHdr);
    // Guard: command type
    if (cmd != GeomCmd.MoveTo) {
        return null;
    }
    // Guard: minimum command length
    if (cmdLength < 1) {
        return null;
    }
    // (require header and at least 1 value * 2 params)
    if (cmdLength * GeomCmd.MoveTo.getParamCount() + 1 > geomCmds.size()) {
        return null;
    }
    final CoordinateSequence coordSeq = geomFactory.getCoordinateSequenceFactory().create(cmdLength, 2);
    int coordIndex = 0;
    Coordinate nextCoord;
    while (i < geomCmds.size() - 1) {
        cursor.add(ZigZag.decode(geomCmds.get(i++)), ZigZag.decode(geomCmds.get(i++)));
        nextCoord = coordSeq.getCoordinate(coordIndex++);
        nextCoord.setOrdinate(0, cursor.x);
        nextCoord.setOrdinate(1, cursor.y);
    }
    return coordSeq.size() == 1 ? geomFactory.createPoint(coordSeq) : geomFactory.createMultiPoint(coordSeq);
}
Also used : CoordinateSequence(com.vividsolutions.jts.geom.CoordinateSequence) Coordinate(com.vividsolutions.jts.geom.Coordinate) GeomCmd(com.wdtinc.mapbox_vector_tile.encoding.GeomCmd) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint)

Example 2 with GeomCmd

use of com.wdtinc.mapbox_vector_tile.encoding.GeomCmd in project Osmand by osmandapp.

the class MvtReader method readLines.

/**
 * Create {@link LineString} or {@link MultiLineString} from MVT geometry drawing commands.
 *
 * @param geomFactory creates JTS geometry
 * @param geomCmds    contains MVT geometry commands
 * @param cursor      contains current MVT extent position
 * @return JTS geometry or null on failure
 */
private static Geometry readLines(GeometryFactory geomFactory, List<Integer> geomCmds, Vec2d cursor) {
    // Guard: must have header
    if (geomCmds.isEmpty()) {
        return null;
    }
    /**
     * Geometry command index
     */
    int i = 0;
    int cmdHdr;
    int cmdLength;
    GeomCmd cmd;
    List<LineString> geoms = new ArrayList<>(1);
    CoordinateSequence nextCoordSeq;
    Coordinate nextCoord;
    while (i <= geomCmds.size() - MIN_LINE_STRING_LEN) {
        // --------------------------------------------
        // Expected: MoveTo command of length 1
        // --------------------------------------------
        // Read command header
        cmdHdr = geomCmds.get(i++);
        cmdLength = GeomCmdHdr.getCmdLength(cmdHdr);
        cmd = GeomCmdHdr.getCmd(cmdHdr);
        // Guard: command type and length
        if (cmd != GeomCmd.MoveTo || cmdLength != 1) {
            break;
        }
        // Update cursor position with relative move
        cursor.add(ZigZag.decode(geomCmds.get(i++)), ZigZag.decode(geomCmds.get(i++)));
        // --------------------------------------------
        // Expected: LineTo command of length > 0
        // --------------------------------------------
        // Read command header
        cmdHdr = geomCmds.get(i++);
        cmdLength = GeomCmdHdr.getCmdLength(cmdHdr);
        cmd = GeomCmdHdr.getCmd(cmdHdr);
        // Guard: command type and length
        if (cmd != GeomCmd.LineTo || cmdLength < 1) {
            break;
        }
        // (require at least (1 value * 2 params) + current_index)
        if ((cmdLength * GeomCmd.LineTo.getParamCount()) + i > geomCmds.size()) {
            break;
        }
        nextCoordSeq = geomFactory.getCoordinateSequenceFactory().create(1 + cmdLength, 2);
        // Set first point from MoveTo command
        nextCoord = nextCoordSeq.getCoordinate(0);
        nextCoord.setOrdinate(0, cursor.x);
        nextCoord.setOrdinate(1, cursor.y);
        // Set remaining points from LineTo command
        for (int lineToIndex = 0; lineToIndex < cmdLength; ++lineToIndex) {
            // Update cursor position with relative line delta
            cursor.add(ZigZag.decode(geomCmds.get(i++)), ZigZag.decode(geomCmds.get(i++)));
            nextCoord = nextCoordSeq.getCoordinate(lineToIndex + 1);
            nextCoord.setOrdinate(0, cursor.x);
            nextCoord.setOrdinate(1, cursor.y);
        }
        geoms.add(geomFactory.createLineString(nextCoordSeq));
    }
    return geoms.size() == 1 ? geoms.get(0) : geomFactory.createMultiLineString(geoms.toArray(new LineString[geoms.size()]));
}
Also used : CoordinateSequence(com.vividsolutions.jts.geom.CoordinateSequence) LineString(com.vividsolutions.jts.geom.LineString) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) Coordinate(com.vividsolutions.jts.geom.Coordinate) ArrayList(java.util.ArrayList) GeomCmd(com.wdtinc.mapbox_vector_tile.encoding.GeomCmd) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint)

Example 3 with GeomCmd

use of com.wdtinc.mapbox_vector_tile.encoding.GeomCmd in project Osmand by osmandapp.

the class MvtReader method readPolys.

/**
 * Create {@link Polygon} or {@link MultiPolygon} from MVT geometry drawing commands.
 *
 * @param geomFactory    creates JTS geometry
 * @param geomCmds       contains MVT geometry commands
 * @param cursor         contains current MVT extent position
 * @param ringClassifier
 * @return JTS geometry or null on failure
 */
private static Geometry readPolys(GeometryFactory geomFactory, List<Integer> geomCmds, Vec2d cursor, RingClassifier ringClassifier) {
    // Guard: must have header
    if (geomCmds.isEmpty()) {
        return null;
    }
    /**
     * Geometry command index
     */
    int i = 0;
    int cmdHdr;
    int cmdLength;
    GeomCmd cmd;
    List<LinearRing> rings = new ArrayList<>(1);
    CoordinateSequence nextCoordSeq;
    Coordinate nextCoord;
    while (i <= geomCmds.size() - MIN_POLYGON_LEN) {
        // --------------------------------------------
        // Expected: MoveTo command of length 1
        // --------------------------------------------
        // Read command header
        cmdHdr = geomCmds.get(i++);
        cmdLength = GeomCmdHdr.getCmdLength(cmdHdr);
        cmd = GeomCmdHdr.getCmd(cmdHdr);
        // Guard: command type and length
        if (cmd != GeomCmd.MoveTo || cmdLength != 1) {
            break;
        }
        // Update cursor position with relative move
        cursor.add(ZigZag.decode(geomCmds.get(i++)), ZigZag.decode(geomCmds.get(i++)));
        // --------------------------------------------
        // Expected: LineTo command of length > 1
        // --------------------------------------------
        // Read command header
        cmdHdr = geomCmds.get(i++);
        cmdLength = GeomCmdHdr.getCmdLength(cmdHdr);
        cmd = GeomCmdHdr.getCmd(cmdHdr);
        // Guard: command type and length
        if (cmd != GeomCmd.LineTo || cmdLength < 2) {
            break;
        }
        // (require at least (2 values * 2 params) + (current index 'i') + (1 for ClosePath))
        if ((cmdLength * GeomCmd.LineTo.getParamCount()) + i + 1 > geomCmds.size()) {
            break;
        }
        nextCoordSeq = geomFactory.getCoordinateSequenceFactory().create(2 + cmdLength, 2);
        // Set first point from MoveTo command
        nextCoord = nextCoordSeq.getCoordinate(0);
        nextCoord.setOrdinate(0, cursor.x);
        nextCoord.setOrdinate(1, cursor.y);
        // Set remaining points from LineTo command
        for (int lineToIndex = 0; lineToIndex < cmdLength; ++lineToIndex) {
            // Update cursor position with relative line delta
            cursor.add(ZigZag.decode(geomCmds.get(i++)), ZigZag.decode(geomCmds.get(i++)));
            nextCoord = nextCoordSeq.getCoordinate(lineToIndex + 1);
            nextCoord.setOrdinate(0, cursor.x);
            nextCoord.setOrdinate(1, cursor.y);
        }
        // --------------------------------------------
        // Expected: ClosePath command of length 1
        // --------------------------------------------
        // Read command header
        cmdHdr = geomCmds.get(i++);
        cmdLength = GeomCmdHdr.getCmdLength(cmdHdr);
        cmd = GeomCmdHdr.getCmd(cmdHdr);
        if (cmd != GeomCmd.ClosePath || cmdLength != 1) {
            break;
        }
        // Set last point from ClosePath command
        nextCoord = nextCoordSeq.getCoordinate(nextCoordSeq.size() - 1);
        nextCoord.setOrdinate(0, nextCoordSeq.getOrdinate(0, 0));
        nextCoord.setOrdinate(1, nextCoordSeq.getOrdinate(0, 1));
        rings.add(geomFactory.createLinearRing(nextCoordSeq));
    }
    // Classify rings
    final List<Polygon> polygons = ringClassifier.classifyRings(rings, geomFactory);
    if (polygons.size() < 1) {
        return null;
    } else if (polygons.size() == 1) {
        return polygons.get(0);
    } else {
        return geomFactory.createMultiPolygon(polygons.toArray(new Polygon[polygons.size()]));
    }
}
Also used : CoordinateSequence(com.vividsolutions.jts.geom.CoordinateSequence) Coordinate(com.vividsolutions.jts.geom.Coordinate) ArrayList(java.util.ArrayList) GeomCmd(com.wdtinc.mapbox_vector_tile.encoding.GeomCmd) LinearRing(com.vividsolutions.jts.geom.LinearRing) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) Polygon(com.vividsolutions.jts.geom.Polygon) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint)

Aggregations

Coordinate (com.vividsolutions.jts.geom.Coordinate)3 CoordinateSequence (com.vividsolutions.jts.geom.CoordinateSequence)3 MultiPoint (com.vividsolutions.jts.geom.MultiPoint)3 Point (com.vividsolutions.jts.geom.Point)3 GeomCmd (com.wdtinc.mapbox_vector_tile.encoding.GeomCmd)3 ArrayList (java.util.ArrayList)2 LineString (com.vividsolutions.jts.geom.LineString)1 LinearRing (com.vividsolutions.jts.geom.LinearRing)1 MultiLineString (com.vividsolutions.jts.geom.MultiLineString)1 MultiPolygon (com.vividsolutions.jts.geom.MultiPolygon)1 Polygon (com.vividsolutions.jts.geom.Polygon)1