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);
}
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);
}
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);
}
}
}
}
}
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);
}
}
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;
}
Aggregations