Search in sources :

Example 1 with TransformException

use of org.opengis.referencing.operation.TransformException in project GeoGig by boundlessgeo.

the class BoundsFilteringDiffConsumer method createProjectedFilter.

private ReferencedEnvelope createProjectedFilter(ObjectId metadataId) {
    final ReferencedEnvelope boundsFilter = this.boundsFilter;
    RevFeatureType featureType = ftypeSource.getFeatureType(metadataId);
    CoordinateReferenceSystem nativeCrs = featureType.type().getCoordinateReferenceSystem();
    if (null == nativeCrs || nativeCrs instanceof DefaultEngineeringCRS) {
        return boundsFilter;
    }
    ReferencedEnvelope transformedFilter;
    try {
        transformedFilter = boundsFilter.transform(nativeCrs, true);
    } catch (TransformException | FactoryException e) {
        throw Throwables.propagate(e);
    }
    return transformedFilter;
}
Also used : ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) FactoryException(org.opengis.referencing.FactoryException) TransformException(org.opengis.referencing.operation.TransformException) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) DefaultEngineeringCRS(org.geotools.referencing.crs.DefaultEngineeringCRS)

Example 2 with TransformException

use of org.opengis.referencing.operation.TransformException 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");
    }
    //If we don't have source CRS just return geometry as we can't transform without that 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(com.vividsolutions.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)

Example 3 with TransformException

use of org.opengis.referencing.operation.TransformException 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");
    }
    //If we don't have source CRS just return geometry as we can't transform without that 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 4 with TransformException

use of org.opengis.referencing.operation.TransformException in project series-rest-api by 52North.

the class IoParameters method transformToInnerCrs.

/**
     * @param point
     *        a GeoJSON point to be transformed to internally used CRS:84.
     * @param crsUtils
     *        a reference helper.
     * @return a transformed GeoJSON instance.
     * @throws IoParseException
     *         if point could not be transformed, or if requested CRS object could not be created.
     */
private GeojsonPoint transformToInnerCrs(GeojsonPoint point, CRSUtils crsUtils) {
    try {
        Point toTransformed = crsUtils.convertToPointFrom(point, getCrs());
        Point crs84Point = (Point) crsUtils.transformOuterToInner(toTransformed, getCrs());
        return crsUtils.convertToGeojsonFrom(crs84Point);
    } catch (TransformException e) {
        throw new IoParseException("Could not transform to internally used CRS:84.", e);
    } catch (FactoryException e) {
        throw new IoParseException("Check if 'crs' parameter is a valid EPSG CRS. Was: '" + getCrs() + "'.", e);
    }
}
Also used : IoParseException(org.n52.io.IoParseException) FactoryException(org.opengis.referencing.FactoryException) TransformException(org.opengis.referencing.operation.TransformException) Point(com.vividsolutions.jts.geom.Point) GeojsonPoint(org.n52.io.geojson.old.GeojsonPoint)

Aggregations

FactoryException (org.opengis.referencing.FactoryException)4 TransformException (org.opengis.referencing.operation.TransformException)4 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)3 GeoFormatException (org.codice.ddf.libs.geo.GeoFormatException)2 Hints (org.geotools.factory.Hints)2 CRSAuthorityFactory (org.opengis.referencing.crs.CRSAuthorityFactory)2 MathTransform (org.opengis.referencing.operation.MathTransform)2 Geometry (com.vividsolutions.jts.geom.Geometry)1 Point (com.vividsolutions.jts.geom.Point)1 ReferencedEnvelope (org.geotools.geometry.jts.ReferencedEnvelope)1 DefaultEngineeringCRS (org.geotools.referencing.crs.DefaultEngineeringCRS)1 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)1 IoParseException (org.n52.io.IoParseException)1 GeojsonPoint (org.n52.io.geojson.old.GeojsonPoint)1 ReferenceIdentifier (org.opengis.referencing.ReferenceIdentifier)1