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