Search in sources :

Example 91 with CoordinateReferenceSystem

use of org.opengis.referencing.crs.CoordinateReferenceSystem in project jena by apache.

the class UnitsOfMeasureTest method testConversionRadianToDegree.

/**
 * Test of conversion method, of class UnitsOfMeasure.
 *
 * @throws org.opengis.util.FactoryException
 */
@Test
public void testConversionRadianToDegree() throws FactoryException {
    double distance = 0.5;
    String sourceDistanceUnitURI = "http://www.opengis.net/def/uom/OGC/1.0/radian";
    // WGS84 degrees non-projected
    CoordinateReferenceSystem crs = CRS.forCode("http://www.opengis.net/def/crs/EPSG/0/4326");
    UnitsOfMeasure targetUnitsOfMeasure = new UnitsOfMeasure(crs);
    double radsToDegrees = 180 / Math.PI;
    double value = distance * radsToDegrees;
    double expResult = cleanUpPrecision(value);
    double result = UnitsOfMeasure.conversion(distance, sourceDistanceUnitURI, targetUnitsOfMeasure.getUnitURI());
    assertEquals(expResult, result, 0.0);
}
Also used : CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Test(org.junit.Test)

Example 92 with CoordinateReferenceSystem

use of org.opengis.referencing.crs.CoordinateReferenceSystem in project jena by apache.

the class UnitsOfMeasureTest method testConversionMetreToMetre.

/**
 * Test of conversion method, of class UnitsOfMeasure.
 *
 * @throws org.opengis.util.FactoryException
 */
@Test
public void testConversionMetreToMetre() throws FactoryException {
    double distance = 100.0;
    String sourceDistanceUnitURI = "http://www.opengis.net/def/uom/OGC/1.0/metre";
    // OSGB - metres projected
    CoordinateReferenceSystem crs = CRS.forCode("http://www.opengis.net/def/crs/EPSG/0/27700");
    UnitsOfMeasure targetUnitsOfMeasure = new UnitsOfMeasure(crs);
    double expResult = 100.0;
    double result = UnitsOfMeasure.conversion(distance, sourceDistanceUnitURI, targetUnitsOfMeasure.getUnitURI());
    assertEquals(expResult, result, 0.0);
}
Also used : CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Test(org.junit.Test)

Example 93 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.debug("Unable to convert geometry {} to EPSG:4326 format", valueObj, e);
                }
            }
        }
    }
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) LiteralExpressionImpl(org.geotools.filter.LiteralExpressionImpl) GeoFormatException(org.codice.ddf.libs.geo.GeoFormatException) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Example 94 with CoordinateReferenceSystem

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

the class GeospatialUtil method transformToEPSG4326LonLatFormat.

/**
 * Transform a geometry to EPSG:4326 format with lon/lat coordinate ordering. NOTE: This method
 * will perform the transform swapping coordinates even if the sourceCrsName is EPSG:4326
 *
 * @param geometry - Geometry to transform
 * @param sourceCrsName - Source geometry's coordinate reference system
 * @return Geometry - Transformed geometry into EPSG:4326 lon/lat coordinate system
 */
public static Geometry transformToEPSG4326LonLatFormat(Geometry geometry, String sourceCrsName) throws GeoFormatException {
    if (geometry == null) {
        throw new GeoFormatException("Unable to convert null geometry");
    }
    // information
    if (sourceCrsName == null) {
        return geometry;
    }
    try {
        CoordinateReferenceSystem sourceCrs = CRS.decode(sourceCrsName);
        Hints hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE);
        CRSAuthorityFactory factory = ReferencingFactoryFinder.getCRSAuthorityFactory("EPSG", hints);
        CoordinateReferenceSystem targetCRS = factory.createCoordinateReferenceSystem(EPSG_4326);
        MathTransform transform = CRS.findMathTransform(sourceCrs, targetCRS);
        return JTS.transform(geometry, transform);
    } catch (FactoryException | TransformException e) {
        throw new GeoFormatException("Unable to convert coordinate to " + EPSG_4326, e);
    }
}
Also used : Hints(org.geotools.factory.Hints) MathTransform(org.opengis.referencing.operation.MathTransform) FactoryException(org.opengis.referencing.FactoryException) GeoFormatException(org.codice.ddf.libs.geo.GeoFormatException) TransformException(org.opengis.referencing.operation.TransformException) CRSAuthorityFactory(org.opengis.referencing.crs.CRSAuthorityFactory) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Example 95 with CoordinateReferenceSystem

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

the class GeospatialUtil method transformToEPSG4326LonLatFormat.

/**
 * Transform a geometry to EPSG:4326 format with lon/lat coordinate ordering. NOTE: This method
 * will NOT perform the transform swapping coordinates even if the sourceCrsName is EPSG:4326.
 *
 * @param geometry - Geometry to transform
 * @param sourceCrs - Source geometry's coordinate reference system
 * @return Geometry - Transformed geometry into EPSG:4326 lon/lat coordinate system
 */
public static Geometry transformToEPSG4326LonLatFormat(Geometry geometry, CoordinateReferenceSystem sourceCrs) throws GeoFormatException {
    if (geometry == null) {
        throw new GeoFormatException("Unable to convert null geometry");
    }
    // information
    if (sourceCrs == null || CollectionUtils.isEmpty(sourceCrs.getIdentifiers())) {
        return geometry;
    }
    Geometry transformedGeometry = geometry;
    try {
        boolean sourceCrsMatchesTarget = false;
        for (ReferenceIdentifier referenceIdentifier : sourceCrs.getIdentifiers()) {
            if (referenceIdentifier.toString().equalsIgnoreCase(EPSG_4326)) {
                sourceCrsMatchesTarget = true;
                break;
            }
        }
        if (!sourceCrsMatchesTarget) {
            Hints hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE);
            CRSAuthorityFactory factory = ReferencingFactoryFinder.getCRSAuthorityFactory("EPSG", hints);
            CoordinateReferenceSystem targetCRS = factory.createCoordinateReferenceSystem(EPSG_4326);
            MathTransform transform = CRS.findMathTransform(sourceCrs, targetCRS);
            transformedGeometry = JTS.transform(geometry, transform);
            LOGGER.debug("Converted CRS {} into {} : {}", sourceCrs, EPSG_4326, geometry);
        }
    } catch (FactoryException | TransformException e) {
        throw new GeoFormatException("Unable to convert coordinate to " + EPSG_4326, e);
    }
    return transformedGeometry;
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) ReferenceIdentifier(org.opengis.referencing.ReferenceIdentifier) Hints(org.geotools.factory.Hints) MathTransform(org.opengis.referencing.operation.MathTransform) FactoryException(org.opengis.referencing.FactoryException) GeoFormatException(org.codice.ddf.libs.geo.GeoFormatException) TransformException(org.opengis.referencing.operation.TransformException) CRSAuthorityFactory(org.opengis.referencing.crs.CRSAuthorityFactory) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

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