Search in sources :

Example 16 with Point

use of com.vividsolutions.jts.geom.Point in project ddf by codice.

the class GeoUtilTest method testTransformEpsg4326EpsgMatch.

@Test
public void testTransformEpsg4326EpsgMatch() throws FactoryException, TransformException, GeoFormatException {
    double lon = 33.45;
    double lat = 25.22;
    CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:4326");
    GeometryFactory geometryFactory = new GeometryFactory();
    Coordinate coordinate = new Coordinate(lon, lat);
    Point utmPoint = geometryFactory.createPoint(coordinate);
    Envelope envelope = JTS.toEnvelope(utmPoint);
    Geometry utmGeometry = JTS.toGeometry(envelope);
    Geometry lonLatGeom = GeospatialUtil.transformToEPSG4326LonLatFormat(utmGeometry, sourceCRS);
    assertThat(lonLatGeom.getCoordinates()[0].x, closeTo(lon, .00001));
    assertThat(lonLatGeom.getCoordinates()[0].y, closeTo(lat, .00001));
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) Coordinate(com.vividsolutions.jts.geom.Coordinate) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Point(com.vividsolutions.jts.geom.Point) Envelope(com.vividsolutions.jts.geom.Envelope) Test(org.junit.Test)

Example 17 with Point

use of com.vividsolutions.jts.geom.Point in project ddf by codice.

the class GeoUtilTest method testTransformEpsg4326LonLat.

@Test
public void testTransformEpsg4326LonLat() throws GeoFormatException {
    GeometryFactory gf = new GeometryFactory();
    Coordinate coord = new Coordinate(25.22, 33.45);
    Point point = gf.createPoint(coord);
    Geometry convertedGeometry = GeospatialUtil.transformToEPSG4326LonLatFormat(point, GeospatialUtil.EPSG_4326);
    assertThat(convertedGeometry.getCoordinates()[0].x, is(33.45));
    assertThat(convertedGeometry.getCoordinates()[0].y, is(25.22));
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) Coordinate(com.vividsolutions.jts.geom.Coordinate) Point(com.vividsolutions.jts.geom.Point) Test(org.junit.Test)

Example 18 with Point

use of com.vividsolutions.jts.geom.Point in project ddf by codice.

the class GeospatialEvaluator method buildGeometry.

public static Geometry buildGeometry(String gmlText) throws IOException, SAXException, ParserConfigurationException {
    String methodName = "buildGeometry";
    LOGGER.debug("ENTERING: {}", methodName);
    Geometry geometry = null;
    gmlText = supportSRSName(gmlText);
    try {
        LOGGER.debug("Creating geoTools Configuration ...");
        Configuration config = new org.geotools.gml3.GMLConfiguration();
        LOGGER.debug("Parsing geoTools configuration");
        Parser parser = new Parser(config);
        LOGGER.debug("Parsing gmlText");
        geometry = (Geometry) (parser.parse(new StringReader(gmlText)));
        LOGGER.debug("geometry (before conversion): {}", geometry.toText());
        // The metadata schema states that <gml:pos> elements specify points in
        // LAT,LON order. But WKT specifies points in LON,LAT order. When the geoTools
        // libraries return the geometry data, it's WKT is in LAT,LON order (which is
        // incorrect).
        // As a workaround here, for Polygons and Points (which are currently the only spatial
        // criteria supported) we must swap the x,y of each coordinate so that they are
        // specified in LON,LAT order and then use the swapped coordinates to create a new
        // Polygon or Point to be returned to the caller.
        GeometryFactory geometryFactory = new GeometryFactory();
        if (geometry instanceof Polygon) {
            // Build new array of coordinates using the swapped coordinates
            ArrayList<Coordinate> newCoords = new ArrayList<Coordinate>();
            // Swap each coordinate's x,y so that they specify LON,LAT order
            for (Coordinate coord : geometry.getCoordinates()) {
                newCoords.add(new Coordinate(coord.y, coord.x));
            }
            // Create a new polygon using the swapped coordinates
            Polygon polygon = new Polygon(geometryFactory.createLinearRing(newCoords.toArray(new Coordinate[newCoords.size()])), null, geometryFactory);
            // this logs the transformed WKT
            LOGGER.debug("Translates to {}", polygon.toText());
            // with LON,LAT ordered points
            LOGGER.debug("EXITING: {}", methodName);
            return polygon;
        }
        if (geometry instanceof Point) {
            // Create a new point using the swapped coordinates that specify LON,LAT order
            Point point = geometryFactory.createPoint(new Coordinate(geometry.getCoordinate().y, geometry.getCoordinate().x));
            // this logs the transformed WKT
            LOGGER.debug("Translates to {}", point.toText());
            // with a LON,LAT ordered point
            LOGGER.debug("EXITING: {}", methodName);
            return point;
        }
    } catch (Exception e) {
        LOGGER.debug("Exception using geotools", e);
    }
    LOGGER.debug("No translation done for geometry - probably not good ...");
    LOGGER.debug("EXITING: {}", methodName);
    return geometry;
}
Also used : GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) Configuration(org.geotools.xml.Configuration) ArrayList(java.util.ArrayList) Point(com.vividsolutions.jts.geom.Point) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) Parser(org.geotools.xml.Parser) Geometry(com.vividsolutions.jts.geom.Geometry) Coordinate(com.vividsolutions.jts.geom.Coordinate) StringReader(java.io.StringReader) Polygon(com.vividsolutions.jts.geom.Polygon)

Example 19 with Point

use of com.vividsolutions.jts.geom.Point in project ddf by codice.

the class WfsFilterDelegate method createGeometryOperand.

private JAXBElement<? extends AbstractGeometryType> createGeometryOperand(String wkt) {
    String convertedWkt = wkt;
    Geometry wktGeometry = null;
    try {
        wktGeometry = getGeometryFromWkt(convertedWkt);
    } catch (ParseException e) {
        throw new IllegalArgumentException("Unable to parse WKT Geometry [" + convertedWkt + "]", e);
    }
    if (wktGeometry instanceof Polygon) {
        if (isGeometryOperandSupported(Wfs10Constants.POLYGON)) {
            return createPolygon(convertedWkt);
        } else {
            throw new IllegalArgumentException("The Polygon operand is not supported.");
        }
    } else if (wktGeometry instanceof Point) {
        if (isGeometryOperandSupported(Wfs10Constants.POINT)) {
            return createPoint(convertedWkt);
        } else {
            throw new IllegalArgumentException("The Point operand is not supported.");
        }
    } else if (wktGeometry instanceof LineString) {
        if (isGeometryOperandSupported(Wfs10Constants.LINESTRING)) {
            return createLineString(wktGeometry);
        } else {
            throw new IllegalArgumentException("The LineString operand is not supported.");
        }
    } else if (wktGeometry instanceof MultiPoint) {
        if (isGeometryOperandSupported(Wfs10Constants.MULTI_POINT)) {
            return createMultiPoint(wktGeometry);
        } else {
            throw new IllegalArgumentException("The MultiPoint operand is not supported.");
        }
    } else if (wktGeometry instanceof MultiLineString) {
        if (isGeometryOperandSupported(Wfs10Constants.MULTI_LINESTRING)) {
            return createMultiLineString(wktGeometry);
        } else {
            throw new IllegalArgumentException("The MultiLineString operand is not supported.");
        }
    } else if (wktGeometry instanceof MultiPolygon) {
        if (isGeometryOperandSupported(Wfs10Constants.MULTI_POLYGON)) {
            return createMultiPolygon(wktGeometry);
        } else {
            throw new IllegalArgumentException("The MultiPolygon operand is not supported.");
        }
    } else if (wktGeometry instanceof GeometryCollection) {
        if (isGeometryOperandSupported(Wfs10Constants.GEOMETRY_COLLECTION)) {
            return createGeometryCollection(wktGeometry);
        } else {
            throw new IllegalArgumentException("The GeometryCollection operand is not supported.");
        }
    }
    throw new IllegalArgumentException("Unable to create Geometry from WKT String");
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) MultiPoint(com.vividsolutions.jts.geom.MultiPoint) GeometryCollection(com.vividsolutions.jts.geom.GeometryCollection) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) LineString(com.vividsolutions.jts.geom.LineString) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) LineString(com.vividsolutions.jts.geom.LineString) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) ParseException(com.vividsolutions.jts.io.ParseException) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) Polygon(com.vividsolutions.jts.geom.Polygon)

Example 20 with Point

use of com.vividsolutions.jts.geom.Point in project dhis2-core by dhis2.

the class GeoUtils method checkPointWithMultiPolygon.

/**
     * Check if the point coordinate falls within the polygon/MultiPolygon Shape
     *
     * @param longitude the longitude.
     * @param latitude the latitude.
     * @param multiPolygonJson the GeoJSON coordinates of the MultiPolygon
     * @param featureType the featureType of the MultiPolygon.
     */
public static boolean checkPointWithMultiPolygon(double longitude, double latitude, String multiPolygonJson, FeatureType featureType) {
    try {
        boolean contains = false;
        GeometryJSON gtjson = new GeometryJSON();
        Point point = getGeoJsonPoint(longitude, latitude);
        if (point != null && point.isValid()) {
            if (featureType == FeatureType.POLYGON) {
                Polygon polygon = gtjson.readPolygon(new StringReader("{\"type\":\"Polygon\", \"coordinates\":" + multiPolygonJson + "}"));
                contains = polygon.contains(point);
            } else if (featureType == FeatureType.MULTI_POLYGON) {
                MultiPolygon multiPolygon = gtjson.readMultiPolygon(new StringReader("{\"type\":\"MultiPolygon\", \"coordinates\":" + multiPolygonJson + "}"));
                contains = multiPolygon.contains(point);
            }
        }
        return contains;
    } catch (Exception ex) {
        return false;
    }
}
Also used : GeometryJSON(org.geotools.geojson.geom.GeometryJSON) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) StringReader(java.io.StringReader) Point(com.vividsolutions.jts.geom.Point) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) Polygon(com.vividsolutions.jts.geom.Polygon) IOException(java.io.IOException)

Aggregations

Point (com.vividsolutions.jts.geom.Point)48 Coordinate (com.vividsolutions.jts.geom.Coordinate)21 Geometry (com.vividsolutions.jts.geom.Geometry)15 Test (org.junit.Test)12 MultiPoint (com.vividsolutions.jts.geom.MultiPoint)11 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)10 Polygon (com.vividsolutions.jts.geom.Polygon)9 LineString (com.vividsolutions.jts.geom.LineString)7 MultiLineString (com.vividsolutions.jts.geom.MultiLineString)5 Envelope (com.vividsolutions.jts.geom.Envelope)4 GeometryCollection (com.vividsolutions.jts.geom.GeometryCollection)4 ParseException (com.vividsolutions.jts.io.ParseException)4 ArrayList (java.util.ArrayList)4 GeojsonPoint (org.n52.io.geojson.old.GeojsonPoint)4 MultiPolygon (com.vividsolutions.jts.geom.MultiPolygon)3 StringReader (java.io.StringReader)3 SolrQuery (org.apache.solr.client.solrj.SolrQuery)3 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)3 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)3 JtsPoint (org.locationtech.spatial4j.shape.jts.JtsPoint)3