use of com.vividsolutions.jts.geom.Coordinate in project Osmand by osmandapp.
the class JtsAdapter method ptsToGeomCmds.
/**
* <p>Convert a {@link Point} or {@link MultiPoint} geometry to a list of MVT geometry drawing commands. See
* <a href="https://github.com/mapbox/vector-tile-spec">vector-tile-spec</a>
* for details.</p>
*
* <p>WARNING: The value of the {@code cursor} parameter is modified as a result of calling this method.</p>
*
* @param geom input of type {@link Point} or {@link MultiPoint}. Type is NOT checked and expected to be correct.
* @param cursor modified during processing to contain next MVT cursor position
* @return list of commands
*/
private static List<Integer> ptsToGeomCmds(final Geometry geom, final Vec2d cursor) {
// Guard: empty geometry coordinates
final Coordinate[] geomCoords = geom.getCoordinates();
if (geomCoords.length <= 0) {
Collections.emptyList();
}
/**
* Tile commands and parameters
*/
final List<Integer> geomCmds = new ArrayList<>(geomCmdBuffLenPts(geomCoords.length));
/**
* Holds next MVT coordinate
*/
final Vec2d mvtPos = new Vec2d();
/**
* Length of 'MoveTo' draw command
*/
int moveCmdLen = 0;
// Insert placeholder for 'MoveTo' command header
geomCmds.add(0);
Coordinate nextCoord;
for (int i = 0; i < geomCoords.length; ++i) {
nextCoord = geomCoords[i];
mvtPos.set(nextCoord.x, nextCoord.y);
// Ignore duplicate MVT points
if (i == 0 || !equalAsInts(cursor, mvtPos)) {
++moveCmdLen;
moveCursor(cursor, geomCmds, mvtPos);
}
}
if (moveCmdLen <= GeomCmdHdr.CMD_HDR_LEN_MAX) {
// Write 'MoveTo' command header to first index
geomCmds.set(0, GeomCmdHdr.cmdHdr(GeomCmd.MoveTo, moveCmdLen));
return geomCmds;
} else {
// Invalid geometry, need at least 1 'MoveTo' value to make points
return Collections.emptyList();
}
}
use of com.vividsolutions.jts.geom.Coordinate in project Osmand by osmandapp.
the class JtsAdapter method linesToGeomCmds.
/**
* <p>Convert a {@link LineString} or {@link Polygon} to a list of MVT geometry drawing commands.
* A {@link MultiLineString} or {@link MultiPolygon} can be encoded by calling this method multiple times.</p>
*
* <p>See <a href="https://github.com/mapbox/vector-tile-spec">vector-tile-spec</a> for details.</p>
*
* <p>WARNING: The value of the {@code cursor} parameter is modified as a result of calling this method.</p>
*
* @param geom input of type {@link LineString} or {@link Polygon}. Type is NOT checked and expected to be correct.
* @param closeEnabled whether a 'ClosePath' command should terminate the command list
* @param cursor modified during processing to contain next MVT cursor position
* @param minLineToLen minimum allowed length for LineTo command.
* @return list of commands
*/
private static List<Integer> linesToGeomCmds(final Geometry geom, final boolean closeEnabled, final Vec2d cursor, final int minLineToLen) {
final Coordinate[] geomCoords = geom.getCoordinates();
// Check geometry for repeated end points
final int repeatEndCoordCount = countCoordRepeatReverse(geomCoords);
final int minExpGeomCoords = geomCoords.length - repeatEndCoordCount;
// Guard/Optimization: Not enough geometry coordinates for a line
if (minExpGeomCoords < 2) {
Collections.emptyList();
}
/**
* Tile commands and parameters
*/
final List<Integer> geomCmds = new ArrayList<>(geomCmdBuffLenLines(minExpGeomCoords, closeEnabled));
/**
* Holds next MVT coordinate
*/
final Vec2d mvtPos = new Vec2d();
// Initial coordinate
Coordinate nextCoord = geomCoords[0];
mvtPos.set(nextCoord.x, nextCoord.y);
// Encode initial 'MoveTo' command
geomCmds.add(GeomCmdHdr.cmdHdr(GeomCmd.MoveTo, 1));
moveCursor(cursor, geomCmds, mvtPos);
/**
* Index of 'LineTo' 'command header'
*/
final int lineToCmdHdrIndex = geomCmds.size();
// Insert placeholder for 'LineTo' command header
geomCmds.add(0);
/**
* Length of 'LineTo' draw command
*/
int lineToLength = 0;
for (int i = 1; i < minExpGeomCoords; ++i) {
nextCoord = geomCoords[i];
mvtPos.set(nextCoord.x, nextCoord.y);
// Ignore duplicate MVT points in sequence
if (!equalAsInts(cursor, mvtPos)) {
++lineToLength;
moveCursor(cursor, geomCmds, mvtPos);
}
}
if (lineToLength >= minLineToLen && lineToLength <= GeomCmdHdr.CMD_HDR_LEN_MAX) {
// Write 'LineTo' 'command header'
geomCmds.set(lineToCmdHdrIndex, GeomCmdHdr.cmdHdr(GeomCmd.LineTo, lineToLength));
if (closeEnabled) {
geomCmds.add(GeomCmdHdr.closePathCmdHdr());
}
return geomCmds;
} else {
// Invalid geometry, need at least 1 'LineTo' value to make a Multiline or Polygon
return Collections.emptyList();
}
}
use of com.vividsolutions.jts.geom.Coordinate 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.vividsolutions.jts.geom.Coordinate in project eol-globi-data by jhpoelen.
the class StudyImporterForRaymond method calculateCentroidOfBBox.
protected static LatLng calculateCentroidOfBBox(double left, double top, double right, double bottom) {
LatLng latLng;
if (left == right && top == bottom) {
latLng = new LatLng(top, left);
} else {
Coordinate[] points = { GeoUtil.getCoordinate(top, left), GeoUtil.getCoordinate(top, right), GeoUtil.getCoordinate(bottom, right), GeoUtil.getCoordinate(bottom, left), GeoUtil.getCoordinate(top, left) };
GeometryFactory geometryFactory = new GeometryFactory();
Polygon polygon = geometryFactory.createPolygon(points);
Point centroid = polygon.getCentroid();
latLng = new LatLng(centroid.getCoordinate().y, centroid.getCoordinate().x);
}
return latLng;
}
use of com.vividsolutions.jts.geom.Coordinate in project eol-globi-data by jhpoelen.
the class StudyImporterForSzoboszlai method importShapes.
protected Map<Integer, LatLng> importShapes() throws StudyImporterException {
Map<Integer, LatLng> localityMap = new TreeMap<>();
FileDataStore dataStore = null;
try {
InputStream shapeZipArchive = getDataset().getResource("shapes");
File tmpFolder = new File(FileUtils.getTempDirectory(), UUID.randomUUID().toString());
tmpFolder.deleteOnExit();
unpackZip(shapeZipArchive, tmpFolder);
dataStore = FileDataStoreFinder.getDataStore(new File(tmpFolder, "LocatPolygonsPoints.shp"));
if (dataStore == null) {
throw new StudyImporterException("failed to parse shapefiles");
}
FeatureReader<SimpleFeatureType, SimpleFeature> featureReader = dataStore.getFeatureReader();
while (featureReader.hasNext()) {
SimpleFeature feature = featureReader.next();
Object geom = feature.getAttribute("the_geom");
if (geom instanceof Point) {
Coordinate coordinate = ((Point) geom).getCoordinate();
Object localNum = feature.getAttribute("LocatNum");
if (localNum instanceof Integer) {
localityMap.put((Integer) localNum, new LatLng(coordinate.y, coordinate.x));
}
}
}
featureReader.close();
} catch (IOException e) {
throw new StudyImporterException(e);
} finally {
if (dataStore != null) {
dataStore.dispose();
}
}
return localityMap;
}
Aggregations