You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by js...@apache.org on 2019/01/08 11:11:38 UTC
[sis] branch geoapi-4.0 updated: Geometry : add method to retrieve
crs from JTS geometry
This is an automated email from the ASF dual-hosted git repository.
jsorel pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git
The following commit(s) were added to refs/heads/geoapi-4.0 by this push:
new 2b6dd8f Geometry : add method to retrieve crs from JTS geometry
2b6dd8f is described below
commit 2b6dd8fb84fb056f75afbea2c12a6c44bfa160f6
Author: jsorel <jo...@geomatys.com>
AuthorDate: Tue Jan 8 13:10:56 2019 +0100
Geometry : add method to retrieve crs from JTS geometry
---
.../java/org/apache/sis/internal/feature/JTS.java | 52 +++++++++++++++++++++-
.../org/apache/sis/internal/feature/JTSTest.java | 32 +++++++++++++
2 files changed, 83 insertions(+), 1 deletion(-)
diff --git a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/JTS.java b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/JTS.java
index f7863c3..af3a6bb 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/JTS.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/JTS.java
@@ -20,6 +20,7 @@ import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Iterator;
+import java.util.Map;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;
@@ -33,7 +34,10 @@ import org.locationtech.jts.io.ParseException;
import org.apache.sis.geometry.GeneralEnvelope;
import org.apache.sis.setup.GeometryLibrary;
import org.apache.sis.math.Vector;
+import org.apache.sis.referencing.CRS;
import org.apache.sis.util.Classes;
+import org.opengis.referencing.crs.CoordinateReferenceSystem;
+import org.opengis.util.FactoryException;
/**
@@ -47,7 +51,13 @@ import org.apache.sis.util.Classes;
* @since 0.7
* @module
*/
-final class JTS extends Geometries<Geometry> {
+public final class JTS extends Geometries<Geometry> {
+
+ /**
+ * Key use in Geometry userData Map to store an instanceof CoordinateReferenceSystem.
+ */
+ public static final String KEY_CRS = "crs";
+
/**
* The factory to use for creating JTS geometries. Currently set to a factory using
* double-precision floating point numbers and a spatial-reference ID of 0.
@@ -249,4 +259,44 @@ final class JTS extends Geometries<Geometry> {
public Object parseWKT(final String wkt) throws ParseException {
return new WKTReader(factory).read(wkt);
}
+
+ /**
+ * Extract CoordinateReferenceSystem from given geometry.
+ * <p>
+ * This method expect the CoordinateReferenceSystem to be store in one
+ * the following ways :
+ * </p>
+ * <ul>
+ * <li>Geometry UserData value is a CoordinateReferenceSystem</li>
+ * <li>Geometry UserData value is a Map with a value for key 'crs'</li>
+ * <li>Geometry SRID if positive, interpreted as an EPSG code</li>
+ * </ul>
+ * <p>
+ * If none of the above is valid, null is returned.
+ * </p>
+ *
+ * @param geometry source geometry
+ * @return CoordinateReferenceSystem or null
+ * @throws org.opengis.util.FactoryException
+ */
+ public static CoordinateReferenceSystem findCoordinateReferenceSystem(Geometry geometry) throws FactoryException {
+ Object userData = geometry.getUserData();
+ if (userData instanceof CoordinateReferenceSystem) {
+ return (CoordinateReferenceSystem) userData;
+ } else if (userData instanceof Map) {
+ final Map map = (Map) userData;
+ final Object value = map.get(KEY_CRS);
+ if (value instanceof CoordinateReferenceSystem) {
+ return (CoordinateReferenceSystem) value;
+ }
+ }
+
+ //fallback on SRID
+ int srid = geometry.getSRID();
+ if (srid > 0) {
+ return CRS.forCode("EPSG:"+srid);
+ }
+ return null;
+ }
+
}
diff --git a/core/sis-feature/src/test/java/org/apache/sis/internal/feature/JTSTest.java b/core/sis-feature/src/test/java/org/apache/sis/internal/feature/JTSTest.java
index 572bd28..dbfb7fd 100644
--- a/core/sis-feature/src/test/java/org/apache/sis/internal/feature/JTSTest.java
+++ b/core/sis-feature/src/test/java/org/apache/sis/internal/feature/JTSTest.java
@@ -16,9 +16,15 @@
*/
package org.apache.sis.internal.feature;
+import java.util.Collections;
+import org.apache.sis.referencing.CommonCRS;
import org.locationtech.jts.geom.Coordinate;
+import org.locationtech.jts.geom.Geometry;
+import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.MultiLineString;
import org.junit.Test;
+import org.opengis.referencing.crs.CoordinateReferenceSystem;
+import org.opengis.util.FactoryException;
import static org.junit.Assert.*;
@@ -84,4 +90,30 @@ public final strictfp class JTSTest extends GeometriesTestCase {
new Coordinate(15, 11),
new Coordinate(13, 10)}, mp.getGeometryN(2).getCoordinates());
}
+
+ /**
+ * Tests {@link JTS#findCoordinateReferenceSystem(org.locationtech.jts.geom.Geometry) }.
+ */
+ @Test
+ public void testFindCoordinateReferenceSystem() throws FactoryException {
+ final GeometryFactory gf = new GeometryFactory();
+ final Geometry geometry = gf.createPoint(new Coordinate(5, 6));
+
+ CoordinateReferenceSystem crs = JTS.findCoordinateReferenceSystem(geometry);
+ assertNull(crs);
+
+ // test crs as user data
+ geometry.setUserData(CommonCRS.ED50.geographic());
+ assertEquals(CommonCRS.ED50.geographic(), JTS.findCoordinateReferenceSystem(geometry));
+
+ // test crs as map value
+ geometry.setUserData(Collections.singletonMap("crs", CommonCRS.NAD83.geographic()));
+ assertEquals(CommonCRS.NAD83.geographic(), JTS.findCoordinateReferenceSystem(geometry));
+
+ // test crs as srid
+ geometry.setUserData(null);
+ geometry.setSRID(4326);
+ assertEquals(CommonCRS.WGS84.geographic(), JTS.findCoordinateReferenceSystem(geometry));
+ }
+
}