Search in sources :

Example 6 with CoordinateReferenceSystem

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);
}
Also used : CodeDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition) FactoryException(org.opengis.referencing.FactoryException) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) WKTDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.WKTDefinition)

Example 7 with CoordinateReferenceSystem

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));
}
Also used : Geometry(org.opengis.geometry.Geometry) PrimitiveFactoryImpl(org.geotools.geometry.jts.spatialschema.geometry.primitive.PrimitiveFactoryImpl) WKTParser(org.geotools.geometry.text.WKTParser) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) GeometryBuilder(org.geotools.geometry.GeometryBuilder) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 8 with CoordinateReferenceSystem

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));
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) Coordinate(com.vividsolutions.jts.geom.Coordinate) Expression(org.opengis.filter.expression.Expression) Intersects(org.opengis.filter.spatial.Intersects) Literal(org.opengis.filter.expression.Literal) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) CswQueryFactoryTest(org.codice.ddf.spatial.ogc.csw.catalog.endpoint.CswQueryFactoryTest) Test(org.junit.Test)

Example 9 with CoordinateReferenceSystem

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);
        }
    }
}
Also used : GeometryType(org.opengis.feature.type.GeometryType) NoSuchAuthorityCodeException(org.opengis.referencing.NoSuchAuthorityCodeException) FactoryException(org.opengis.referencing.FactoryException) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Integer.toBinaryString(java.lang.Integer.toBinaryString) Formattable(org.geotools.referencing.wkt.Formattable)

Example 10 with CoordinateReferenceSystem

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;
}
Also used : FactoryException(org.opengis.referencing.FactoryException) 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