use of com.vividsolutions.jts.geom.Polygon in project pigeon by aseldawy.
the class Break method breakGeom.
private void breakGeom(Geometry geom, Vector<Coordinate[]> segments) {
if (geom == null)
return;
if (geom instanceof LineString) {
LineString linestring = (LineString) geom;
Coordinate[] coordinates = linestring.getCoordinates();
for (int i = 1; i < coordinates.length; i++) {
Coordinate[] segment = new Coordinate[2];
segment[0] = new Coordinate(coordinates[i - 1]);
segment[1] = new Coordinate(coordinates[i]);
segments.add(segment);
}
} else if (geom instanceof Polygon) {
Polygon polygon = (Polygon) geom;
breakGeom(polygon.getExteriorRing(), segments);
for (int n = 0; n < polygon.getNumInteriorRing(); n++) {
breakGeom(polygon.getInteriorRingN(n), segments);
}
} else if (geom instanceof GeometryCollection) {
GeometryCollection geomCollection = (GeometryCollection) geom;
for (int n = 0; n < geomCollection.getNumGeometries(); n++) {
breakGeom(geomCollection.getGeometryN(n), segments);
}
} else if (geom instanceof Point) {
// Skip
} else {
throw new RuntimeException("Cannot break geometry of type " + geom.getClass());
}
}
use of com.vividsolutions.jts.geom.Polygon in project Osmand by osmandapp.
the class JtsAdapter method flatFeatureList.
/**
* <p>Recursively convert a {@link Geometry}, which may be an instance of {@link GeometryCollection} with mixed
* element types, into a flat list containing only the following {@link Geometry} types:</p>
* <ul>
* <li>{@link Point}</li>
* <li>{@link LineString}</li>
* <li>{@link Polygon}</li>
* <li>{@link MultiPoint}</li>
* <li>{@link MultiLineString}</li>
* <li>{@link MultiPolygon}</li>
* </ul>
* <p>WARNING: Any other Geometry types that were not mentioned in the list above will be discarded!</p>
* <p>Useful for converting a generic geometry into a list of simple MVT-feature-ready geometries.</p>
*
* @param geom geometry to flatten
* @return list of MVT-feature-ready geometries
*/
public static List<Geometry> flatFeatureList(Geometry geom) {
final List<Geometry> singleGeoms = new ArrayList<>();
final Stack<Geometry> geomStack = new Stack<>();
Geometry nextGeom;
int nextGeomCount;
geomStack.push(geom);
while (!geomStack.isEmpty()) {
nextGeom = geomStack.pop();
if (nextGeom instanceof Point || nextGeom instanceof MultiPoint || nextGeom instanceof LineString || nextGeom instanceof MultiLineString || nextGeom instanceof Polygon || nextGeom instanceof MultiPolygon) {
singleGeoms.add(nextGeom);
} else if (nextGeom instanceof GeometryCollection) {
// Push all child geometries
nextGeomCount = nextGeom.getNumGeometries();
for (int i = 0; i < nextGeomCount; ++i) {
geomStack.push(nextGeom.getGeometryN(i));
}
}
}
return singleGeoms;
}
use of com.vividsolutions.jts.geom.Polygon in project Osmand by osmandapp.
the class JtsAdapter method toFeature.
/**
* Create and return a feature from a geometry. Returns null on failure.
*
* @param geom flat geometry via {@link #flatFeatureList(Geometry)} that can be translated to a feature
* @param cursor vector tile cursor position
* @param layerProps layer properties for tagging features
* @return new tile feature instance, or null on failure
*/
private static VectorTile.Tile.Feature toFeature(Geometry geom, Vec2d cursor, MvtLayerProps layerProps, IUserDataConverter userDataConverter) {
// Guard: UNKNOWN Geometry
final VectorTile.Tile.GeomType mvtGeomType = JtsAdapter.toGeomType(geom);
if (mvtGeomType == VectorTile.Tile.GeomType.UNKNOWN) {
return null;
}
final VectorTile.Tile.Feature.Builder featureBuilder = VectorTile.Tile.Feature.newBuilder();
final boolean mvtClosePath = MvtUtil.shouldClosePath(mvtGeomType);
final List<Integer> mvtGeom = new ArrayList<>();
featureBuilder.setType(mvtGeomType);
if (geom instanceof Point || geom instanceof MultiPoint) {
// Encode as MVT point or multipoint
mvtGeom.addAll(ptsToGeomCmds(geom, cursor));
} else if (geom instanceof LineString || geom instanceof MultiLineString) {
// Encode as MVT linestring or multi-linestring
for (int i = 0; i < geom.getNumGeometries(); ++i) {
mvtGeom.addAll(linesToGeomCmds(geom.getGeometryN(i), mvtClosePath, cursor, 1));
}
} else if (geom instanceof MultiPolygon || geom instanceof Polygon) {
// Encode as MVT polygon or multi-polygon
for (int i = 0; i < geom.getNumGeometries(); ++i) {
final Polygon nextPoly = (Polygon) geom.getGeometryN(i);
final List<Integer> nextPolyGeom = new ArrayList<>();
boolean valid = true;
// Add exterior ring
final LineString exteriorRing = nextPoly.getExteriorRing();
// Area must be non-zero
final double exteriorArea = CGAlgorithms.signedArea(exteriorRing.getCoordinates());
if (((int) Math.round(exteriorArea)) == 0) {
continue;
}
// Check CCW Winding (must be positive area)
if (exteriorArea < 0d) {
CoordinateArrays.reverse(exteriorRing.getCoordinates());
}
nextPolyGeom.addAll(linesToGeomCmds(exteriorRing, mvtClosePath, cursor, 2));
// Add interior rings
for (int ringIndex = 0; ringIndex < nextPoly.getNumInteriorRing(); ++ringIndex) {
final LineString nextInteriorRing = nextPoly.getInteriorRingN(ringIndex);
// Area must be non-zero
final double interiorArea = CGAlgorithms.signedArea(nextInteriorRing.getCoordinates());
if (((int) Math.round(interiorArea)) == 0) {
continue;
}
// Check CW Winding (must be negative area)
if (interiorArea > 0d) {
CoordinateArrays.reverse(nextInteriorRing.getCoordinates());
}
// Interior ring area must be < exterior ring area, or entire geometry is invalid
if (Math.abs(exteriorArea) <= Math.abs(interiorArea)) {
valid = false;
break;
}
nextPolyGeom.addAll(linesToGeomCmds(nextInteriorRing, mvtClosePath, cursor, 2));
}
if (valid) {
mvtGeom.addAll(nextPolyGeom);
}
}
}
if (mvtGeom.size() < 1) {
return null;
}
featureBuilder.addAllGeometry(mvtGeom);
// Feature Properties
userDataConverter.addTags(geom.getUserData(), layerProps, featureBuilder);
return featureBuilder.build();
}
use of com.vividsolutions.jts.geom.Polygon 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.Polygon in project elasticsearch by elastic.
the class ShapeBuilderTests method testNewPolygon.
public void testNewPolygon() {
Polygon polygon = ShapeBuilders.newPolygon(new CoordinatesBuilder().coordinate(-45, 30).coordinate(45, 30).coordinate(45, -30).coordinate(-45, -30).coordinate(-45, 30)).toPolygon();
LineString exterior = polygon.getExteriorRing();
assertEquals(exterior.getCoordinateN(0), new Coordinate(-45, 30));
assertEquals(exterior.getCoordinateN(1), new Coordinate(45, 30));
assertEquals(exterior.getCoordinateN(2), new Coordinate(45, -30));
assertEquals(exterior.getCoordinateN(3), new Coordinate(-45, -30));
}
Aggregations