use of org.opengis.referencing.crs.CoordinateReferenceSystem in project hale by halestudio.
the class CRSDefinitionUtil method lookupCrs.
/**
* Try to lookup the given CRS via Geotools. If the CRS can be resolved, the
* returned {@link CRSDefinition} will contain a
* {@link CoordinateReferenceSystem} with additional information like
* Bursa-Wolf parameters, otherwise the WKT definition of the input CRS will
* be used as is.
*
* @param crs The CRS to look up
* @return A {@link CodeDefinition} with the resolved CRS or a
* {@link WKTDefinition} if the CRS could not be resolved.
*/
public static CRSDefinition lookupCrs(CoordinateReferenceSystem crs) {
try {
Integer epsgCode = CRS.lookupEpsgCode(crs, true);
if (epsgCode != null) {
// We must use the "EPSG:" prefix here, otherwise Geotools will
// not honour the longitudeFirst parameter and will always
// return the lat/lon variant...
String code = SrsSyntax.EPSG_CODE.getPrefix() + String.valueOf(epsgCode);
// Check if the input CRS is lon/lat
boolean lonFirst = (CRS.getAxisOrder(crs) == AxisOrder.EAST_NORTH);
// Look up the code
CoordinateReferenceSystem resolved = CRS.decode(code, lonFirst);
// is still the same (not guaranteed)
if (CRS.getAxisOrder(crs).equals(CRS.getAxisOrder(resolved))) {
return new CodeDefinition(code, resolved);
}
}
} catch (FactoryException e) {
// Ignore
}
return new WKTDefinition(crs.toWKT(), crs);
}
use of org.opengis.referencing.crs.CoordinateReferenceSystem in project ddf by codice.
the class OpenSearchQueryTest method testWktParserPolygon.
@Test
@Ignore
public void testWktParserPolygon() throws Exception {
String geometryWkt = "POLYGON(( 0 10, 0 30, 20 30, 20 10, 0 10 ))";
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);
double[] coords = geometry.getCentroid().getCoordinate();
LOGGER.debug("coords[0] = {}, coords[1] = {}", coords[0], coords[1]);
// String geometryWkt2 = "POINT( 10 20 )";
String geometryWkt2 = "POLYGON(( 10 15, 10 25, 15 25, 15 15, 10 15 ))";
builder = new GeometryBuilder(DefaultGeographicCRS.WGS84);
WKTParser parser2 = new WKTParser(builder);
// This fixed the NPE in parser.parse() - seems GeoTools has bug with
// keeping the CRS hint set ...
parser2.setFactory(new PrimitiveFactoryImpl(DefaultGeographicCRS.WGS84));
Geometry geometry2 = parser2.parse(geometryWkt2);
double[] coords2 = geometry2.getCentroid().getCoordinate();
LOGGER.debug("coords[0] = {}, coords[1] = {}", coords2[0], coords2[1]);
// This fails - why?
assertTrue(geometry.contains(geometry2));
}
use of org.opengis.referencing.crs.CoordinateReferenceSystem in project ddf by codice.
the class TestCswRecordMapperFilterVisitor method testIntersectsUtm.
@Test
public void testIntersectsUtm() throws FactoryException, TransformException {
double lon = 33.45;
double lat = 25.22;
double easting = 545328.48;
double northing = 2789384.24;
CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:32636");
GeometryFactory geoFactory = new GeometryFactory();
Geometry utmPoint = geoFactory.createPoint(new Coordinate(easting, northing));
utmPoint.setUserData(sourceCRS);
Expression pt1 = factory.literal(geoFactory.createPoint(new Coordinate(1, 2)));
Expression pt2 = factory.literal(utmPoint);
Intersects filter = factory.intersects(pt1, pt2);
visitor.visit(filter, null);
assertThat(pt2, instanceOf(Literal.class));
Literal literalExpression = (Literal) pt2;
assertThat(literalExpression.getValue(), instanceOf(Geometry.class));
Geometry geometry = (Geometry) literalExpression.getValue();
assertThat(geometry.getCoordinates()[0].x, closeTo(lon, .00001));
assertThat(geometry.getCoordinates()[0].y, closeTo(lat, .00001));
}
use of org.opengis.referencing.crs.CoordinateReferenceSystem in project GeoGig by boundlessgeo.
the class FormatCommonV2 method writePropertyType.
private static void writePropertyType(PropertyType type, DataOutput data) throws IOException {
writeName(type.getName(), data);
data.writeByte(FieldType.forBinding(type.getBinding()).getTag());
if (type instanceof GeometryType) {
GeometryType gType = (GeometryType) type;
CoordinateReferenceSystem crs = gType.getCoordinateReferenceSystem();
String srsName;
if (crs == null) {
srsName = "urn:ogc:def:crs:EPSG::0";
} else {
final boolean longitudeFirst = CRS.getAxisOrder(crs, false) == AxisOrder.EAST_NORTH;
final boolean codeOnly = true;
String crsCode = CRS.toSRS(crs, codeOnly);
if (crsCode != null) {
srsName = (longitudeFirst ? "EPSG:" : "urn:ogc:def:crs:EPSG::") + crsCode;
// able to decode it later. If not, we will use WKT instead
try {
CRS.decode(srsName, longitudeFirst);
} catch (NoSuchAuthorityCodeException e) {
srsName = null;
} catch (FactoryException e) {
srsName = null;
}
} else {
srsName = null;
}
}
if (srsName != null) {
data.writeBoolean(true);
data.writeUTF(srsName);
} else {
final String wkt;
if (crs instanceof Formattable) {
wkt = ((Formattable) crs).toWKT(Formattable.SINGLE_LINE);
} else {
wkt = crs.toWKT();
}
data.writeBoolean(false);
data.writeUTF(wkt);
}
}
}
use of org.opengis.referencing.crs.CoordinateReferenceSystem in project GeoGig by boundlessgeo.
the class CrsTextSerializer method deserialize.
public static CoordinateReferenceSystem deserialize(String crsText) {
CoordinateReferenceSystem crs;
boolean crsCode = crsText.startsWith("EPSG") || crsText.startsWith("urn:ogc:def:crs:EPSG");
try {
if (crsCode) {
if ("urn:ogc:def:crs:EPSG::0".equals(crsText)) {
crs = null;
} else {
boolean forceLongitudeFirst = crsText.startsWith("EPSG:");
crs = CRS.decode(crsText, forceLongitudeFirst);
}
} else {
crs = CRS.parseWKT(crsText);
}
} catch (FactoryException e) {
throw new IllegalArgumentException("Cannot parse CRS definition: " + crsText);
}
return crs;
}
Aggregations