Search in sources :

Example 96 with CoordinateReferenceSystem

use of org.opengis.referencing.crs.CoordinateReferenceSystem in project ddf by codice.

the class GeospatialUtil method createBufferedCircleFromPoint.

private static Geometry createBufferedCircleFromPoint(Measure distance, CoordinateReferenceSystem origCRS, Geometry point) {
    Geometry pointGeo = point;
    Unit<?> unit = distance.getUnit();
    UnitConverter unitConverter = null;
    if (!(origCRS instanceof ProjectedCRS)) {
        double x = point.getCoordinate().x;
        double y = point.getCoordinate().y;
        // CRS code for UTM
        String crsCode = "AUTO:42001," + x + "," + y;
        try {
            CoordinateReferenceSystem utmCrs = CRS.decode(crsCode);
            MathTransform toTransform = CRS.findMathTransform(DefaultGeographicCRS.WGS84, utmCrs);
            MathTransform fromTransform = CRS.findMathTransform(utmCrs, DefaultGeographicCRS.WGS84);
            pointGeo = JTS.transform(point, toTransform);
            return JTS.transform(pointGeo.buffer(distance.doubleValue()), fromTransform);
        } catch (MismatchedDimensionException | TransformException | FactoryException e) {
            LOGGER.debug("Unable to create buffered circle from point.", e);
        }
    } else {
        try {
            unitConverter = unit.getConverterToAny(origCRS.getCoordinateSystem().getAxis(0).getUnit());
        } catch (IncommensurableException e) {
            LOGGER.debug("Unable to create unit converter.", e);
        }
    }
    if (unitConverter != null) {
        return pointGeo.buffer(unitConverter.convert(distance.doubleValue()));
    }
    return pointGeo.buffer(distance.doubleValue());
}
Also used : MathTransform(org.opengis.referencing.operation.MathTransform) FactoryException(org.opengis.referencing.FactoryException) IncommensurableException(javax.measure.IncommensurableException) TransformException(org.opengis.referencing.operation.TransformException) MismatchedDimensionException(org.opengis.geometry.MismatchedDimensionException) Geometry(org.locationtech.jts.geom.Geometry) ProjectedCRS(org.opengis.referencing.crs.ProjectedCRS) UnitConverter(javax.measure.UnitConverter) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Example 97 with CoordinateReferenceSystem

use of org.opengis.referencing.crs.CoordinateReferenceSystem 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(GeospatialUtil.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(org.locationtech.jts.geom.Geometry) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Coordinate(org.locationtech.jts.geom.Coordinate) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Point(org.locationtech.jts.geom.Point) Envelope(org.locationtech.jts.geom.Envelope) Test(org.junit.Test)

Example 98 with CoordinateReferenceSystem

use of org.opengis.referencing.crs.CoordinateReferenceSystem in project ddf by codice.

the class OpenSearchQueryTest method testWktParser.

@Test
public void testWktParser() throws Exception {
    String geometryWkt = "POINT( 48.44 -123.37)";
    GeometryBuilder builder = new GeometryBuilder(DefaultGeographicCRS.WGS84);
    WKTParser parser = new WKTParser(builder);
    // This fixed the NPE in parser.parse() - seems GeoTools has bug with
    // keeping the CRS hint set ...
    parser.setFactory(new PrimitiveFactoryImpl(DefaultGeographicCRS.WGS84));
    Geometry geometry = parser.parse(geometryWkt);
    CoordinateReferenceSystem crs = geometry.getCoordinateReferenceSystem();
    assertNotNull(crs);
    String geometryWkt2 = "POINT( 48.44 -123.37)";
    builder = new GeometryBuilder(DefaultGeographicCRS.WGS84);
    WKTParser parser2 = new WKTParser(builder);
    Geometry geometry2 = parser2.parse(geometryWkt2);
    assertTrue(geometry2.intersects(geometry));
    double[] coords = geometry.getCentroid().getCoordinate();
    LOGGER.debug("coords[0] = {},   coords[1] = {}", coords[0], coords[1]);
}
Also used : Geometry(org.opengis.geometry.Geometry) PrimitiveFactoryImpl(org.geotools.geometry.jts.spatialschema.geometry.primitive.PrimitiveFactoryImpl) WKTParser(org.geotools.geometry.text.WKTParser) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) GeometryBuilder(org.geotools.geometry.GeometryBuilder) Test(org.junit.Test)

Example 99 with CoordinateReferenceSystem

use of org.opengis.referencing.crs.CoordinateReferenceSystem in project ddf by codice.

the class CswRecordMapperFilterVisitor method convertGeometryExpressionToEpsg4326.

private static void convertGeometryExpressionToEpsg4326(Expression expression) {
    if (expression instanceof LiteralExpressionImpl) {
        LiteralExpressionImpl literalExpression = (LiteralExpressionImpl) expression;
        Object valueObj = literalExpression.getValue();
        if (valueObj instanceof Geometry) {
            Geometry geometry = (Geometry) valueObj;
            Object userDataObj = geometry.getUserData();
            if (userDataObj instanceof CoordinateReferenceSystem) {
                CoordinateReferenceSystem sourceCRS = (CoordinateReferenceSystem) userDataObj;
                Geometry convertedGeometry = null;
                try {
                    convertedGeometry = GeospatialUtil.transformToEPSG4326LonLatFormat(geometry, sourceCRS);
                    literalExpression.setValue(convertedGeometry);
                } catch (GeoFormatException e) {
                    LOGGER.trace("Unable to convert geometry to EPSG:4326 format", e);
                }
            }
        }
    }
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) LiteralExpressionImpl(org.geotools.filter.LiteralExpressionImpl) GeoFormatException(org.codice.ddf.libs.geo.GeoFormatException) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Example 100 with CoordinateReferenceSystem

use of org.opengis.referencing.crs.CoordinateReferenceSystem in project ddf by codice.

the class OpenSearchQueryTest method testWktParser.

@Test
public void testWktParser() throws Exception {
    String geometryWkt = "POINT( 48.44 -123.37)";
    GeometryBuilder builder = new GeometryBuilder(DefaultGeographicCRS.WGS84);
    WKTParser parser = new WKTParser(builder);
    // This fixed the NPE in parser.parse() - seems GeoTools has bug with
    // keeping the CRS hint set ...
    parser.setFactory(new PrimitiveFactoryImpl(DefaultGeographicCRS.WGS84));
    Geometry geometry = parser.parse(geometryWkt);
    CoordinateReferenceSystem crs = geometry.getCoordinateReferenceSystem();
    assertNotNull(crs);
    String geometryWkt2 = "POINT( 48.44 -123.37)";
    builder = new GeometryBuilder(DefaultGeographicCRS.WGS84);
    WKTParser parser2 = new WKTParser(builder);
    Geometry geometry2 = parser2.parse(geometryWkt2);
    assertTrue(geometry2.intersects(geometry));
    double[] coords = geometry.getCentroid().getCoordinate();
    LOGGER.debug("coords[0] = {},   coords[1] = {}", coords[0], coords[1]);
}
Also used : Geometry(org.opengis.geometry.Geometry) PrimitiveFactoryImpl(org.geotools.geometry.jts.spatialschema.geometry.primitive.PrimitiveFactoryImpl) WKTParser(org.geotools.geometry.text.WKTParser) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) GeometryBuilder(org.geotools.geometry.GeometryBuilder) Test(org.junit.Test)

Aggregations

CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)210 Test (org.junit.Test)80 MathTransform (org.opengis.referencing.operation.MathTransform)32 FactoryException (org.opengis.referencing.FactoryException)25 CoordinateOperation (org.opengis.referencing.operation.CoordinateOperation)24 ReferencedEnvelope (org.geotools.geometry.jts.ReferencedEnvelope)23 Geometry (com.vividsolutions.jts.geom.Geometry)21 TransformException (org.opengis.referencing.operation.TransformException)21 DependsOnMethod (org.apache.sis.test.DependsOnMethod)19 CoordinateSystem (org.opengis.referencing.cs.CoordinateSystem)13 Geometry (org.locationtech.jts.geom.Geometry)11 FactoryException (org.opengis.util.FactoryException)11 SimpleFeature (org.opengis.feature.simple.SimpleFeature)9 DirectPosition (org.opengis.geometry.DirectPosition)9 GeographicCRS (org.opengis.referencing.crs.GeographicCRS)9 VerticalCRS (org.opengis.referencing.crs.VerticalCRS)9 CoordinateSystemAxis (org.opengis.referencing.cs.CoordinateSystemAxis)9 ArrayList (java.util.ArrayList)8 GeometryType (org.opengis.feature.type.GeometryType)8 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)7