Search in sources :

Example 91 with Polygon

use of org.locationtech.jts.geom.Polygon in project sensorweb-server-helgoland by 52North.

the class GeoJSONEncoder method encode.

protected ObjectNode encode(MultiPolygon geometry, int parentSrid) {
    ObjectNode json = jsonFactory.objectNode();
    ArrayNode list = json.put(JSONConstants.TYPE, JSONConstants.MULTI_POLYGON).putArray(JSONConstants.COORDINATES);
    for (int i = 0; i < geometry.getNumGeometries(); ++i) {
        list.add(encodeCoordinates((Polygon) geometry.getGeometryN(i)));
    }
    encodeCRS(json, geometry, parentSrid);
    return json;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) Point(org.locationtech.jts.geom.Point) MultiPoint(org.locationtech.jts.geom.MultiPoint)

Example 92 with Polygon

use of org.locationtech.jts.geom.Polygon in project cdmlib by cybertaxonomy.

the class GeometryBuilder method simpleCircle.

/**
 * Creates perfect circles which are looking good but might be projected
 * incorrectly for the resulting map
 *
 * @param distance
 * @param latitude
 * @param longitude
 * @return
 */
public Geometry simpleCircle(Quantity<Length> distance, Double latitude, Double longitude) {
    GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
    CoordinateSequence coordinateSequence = geometryFactory.getCoordinateSequenceFactory().create(new Coordinate[] { new Coordinate(longitude, latitude) });
    Point point = new Point(coordinateSequence, geometryFactory);
    GeodeticCalculator calc = new GeodeticCalculator(DefaultGeographicCRS.WGS84);
    calc.setStartingGeographicPoint(longitude, latitude);
    UnitConverter converter = distance.getUnit().getConverterTo(SI.METRE);
    double d = converter.convert(distance.getValue()).doubleValue();
    calc.setDirection(0.0, d);
    Point2D p2 = calc.getDestinationGeographicPoint();
    calc.setDirection(90.0, d);
    Point2D p3 = calc.getDestinationGeographicPoint();
    double dy = p2.getY() - latitude;
    double dx = p3.getX() - longitude;
    double dist = (dy + dx) / 2.0;
    Polygon p1 = (Polygon) point.buffer(dist);
    return p1;
}
Also used : CoordinateSequence(org.locationtech.jts.geom.CoordinateSequence) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Coordinate(org.locationtech.jts.geom.Coordinate) Point2D(java.awt.geom.Point2D) UnitConverter(javax.measure.UnitConverter) Point(org.locationtech.jts.geom.Point) Polygon(org.locationtech.jts.geom.Polygon) GeodeticCalculator(org.geotools.referencing.GeodeticCalculator)

Example 93 with Polygon

use of org.locationtech.jts.geom.Polygon in project geowave by locationtech.

the class GeometryUtils method fixRangeOfCoordinates.

/**
 * Adjust geometry so that coordinates fit into long/lat bounds.
 *
 * <p> Split date-line crossing polygons.
 *
 * <p> For now, clip hemisphere crossing portions of the polygon.
 *
 * @param geometry
 * @return list valid polygons
 */
public static List<Polygon> fixRangeOfCoordinates(final CoordinateReferenceSystem crs, final Geometry geometry) {
    final List<Polygon> replacements = new ArrayList<>();
    if (geometry instanceof MultiPolygon) {
        final MultiPolygon multi = (MultiPolygon) geometry;
        for (int i = 0; i < multi.getNumGeometries(); i++) {
            final Geometry geo = multi.getGeometryN(i);
            replacements.addAll(fixRangeOfCoordinates(crs, geo));
        }
        return replacements;
    } else // collection is more general than multi-polygon
    if (geometry instanceof GeometryCollection) {
        final GeometryCollection multi = (GeometryCollection) geometry;
        for (int i = 0; i < multi.getNumGeometries(); i++) {
            final Geometry geo = multi.getGeometryN(i);
            replacements.addAll(fixRangeOfCoordinates(crs, geo));
        }
        return replacements;
    }
    final Coordinate[] geoCoords = geometry.getCoordinates();
    final Coordinate modifier = findModifier(crs, geoCoords);
    replacements.addAll(constructGeometriesOverMapRegions(modifier, geometry));
    return replacements;
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) GeometryCollection(org.locationtech.jts.geom.GeometryCollection) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) Coordinate(org.locationtech.jts.geom.Coordinate) ArrayList(java.util.ArrayList) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) Point(org.locationtech.jts.geom.Point)

Example 94 with Polygon

use of org.locationtech.jts.geom.Polygon in project geowave by locationtech.

the class GeometryUtils method visitGeometry.

public static void visitGeometry(final Geometry geom, final GeometryHandler geometryHandler) {
    if (geom == null) {
        return;
    }
    if (geom instanceof GeometryCollection) {
        final int numGeom = ((GeometryCollection) geom).getNumGeometries();
        for (int i = 0; i < numGeom; i++) {
            visitGeometry(((GeometryCollection) geom).getGeometryN(i), geometryHandler);
        }
    } else if (geom instanceof LineString) {
        geometryHandler.handleLineString((LineString) geom);
    } else if (geom instanceof Polygon) {
        geometryHandler.handlePolygon((Polygon) geom);
    } else {
        final Point centroid = geom.getCentroid();
        geometryHandler.handlePoint(centroid);
    }
}
Also used : GeometryCollection(org.locationtech.jts.geom.GeometryCollection) LineString(org.locationtech.jts.geom.LineString) Point(org.locationtech.jts.geom.Point) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) Point(org.locationtech.jts.geom.Point)

Example 95 with Polygon

use of org.locationtech.jts.geom.Polygon in project geowave by locationtech.

the class GeometryUtils method constructGeometriesOverMapRegions.

/**
 * Produce a set of polygons for each region of the map corrected for date line and hemisphere
 * crossings. Due to the complexity of going around the hemisphere, clip the range.
 *
 * <p> Consider a polygon that cross both the hemisphere in the north and the date line in the
 * west (-182 92, -182 88, -178 88, -178 92, -182 92). The result is two polygons: (-180 90, -180
 * 88, -178 88, -178 90, -180 90) (180 90, 180 88, 178 88, 178 90, 180 90)
 *
 * @param modifier
 * @param geometry - a geometry that may cross date line and/or hemispheres.
 * @return the set of polygons
 */
public static List<Polygon> constructGeometriesOverMapRegions(final Coordinate modifier, final Geometry geometry) {
    final Coordinate[] geoCoords = geometry.getCoordinates();
    final List<Polygon> polygons = new LinkedList<>();
    final Geometry world = world(geometry.getFactory(), GeometryUtils.getDefaultCRS());
    // First do the polygon unchanged world
    final Geometry worldIntersections = world.intersection(geometry);
    for (int i = 0; i < worldIntersections.getNumGeometries(); i++) {
        final Polygon polyToAdd = (Polygon) worldIntersections.getGeometryN(i);
        if (!polygons.contains(polyToAdd)) {
            polygons.add(polyToAdd);
        }
    }
    // optimization...do not modify if 0
    if (Math.abs(modifier.x) > 0.0000000001) {
        final Coordinate[] newCoords = new Coordinate[geoCoords.length];
        int c = 0;
        for (final Coordinate geoCoord : geoCoords) {
            newCoords[c++] = new Coordinate(geoCoord.x + modifier.x, geoCoord.y, geoCoord.z);
        }
        final Polygon transposedPoly = geometry.getFactory().createPolygon(newCoords);
        final Geometry adjustedPolyWorldIntersections = world.intersection(transposedPoly);
        for (int i = 0; i < adjustedPolyWorldIntersections.getNumGeometries(); i++) {
            final Polygon polyToAdd = (Polygon) adjustedPolyWorldIntersections.getGeometryN(i);
            if (!polygons.contains(polyToAdd)) {
                polygons.add(polyToAdd);
            }
        }
    }
    return polygons;
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) Coordinate(org.locationtech.jts.geom.Coordinate) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) LinkedList(java.util.LinkedList) Point(org.locationtech.jts.geom.Point)

Aggregations

Polygon (org.locationtech.jts.geom.Polygon)234 MultiPolygon (org.locationtech.jts.geom.MultiPolygon)112 Coordinate (org.locationtech.jts.geom.Coordinate)99 Point (org.locationtech.jts.geom.Point)73 Geometry (org.locationtech.jts.geom.Geometry)72 LinearRing (org.locationtech.jts.geom.LinearRing)67 LineString (org.locationtech.jts.geom.LineString)60 Test (org.junit.Test)56 ArrayList (java.util.ArrayList)51 GeometryFactory (org.locationtech.jts.geom.GeometryFactory)42 MultiPoint (org.locationtech.jts.geom.MultiPoint)40 MultiLineString (org.locationtech.jts.geom.MultiLineString)23 Test (org.junit.jupiter.api.Test)22 List (java.util.List)21 GeometryCollection (org.locationtech.jts.geom.GeometryCollection)19 WKTReader (org.locationtech.jts.io.WKTReader)14 HashMap (java.util.HashMap)11 IOException (java.io.IOException)10 Map (java.util.Map)9 CustomCoordinateSequence (org.apache.jena.geosparql.implementation.jts.CustomCoordinateSequence)8