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);
}
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()]));
}
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()]));
}
}
Aggregations