diff --git a/src/com/esri/core/geometry/GeometryEngine.java b/src/com/esri/core/geometry/GeometryEngine.java index 8323726b..faeb34a4 100644 --- a/src/com/esri/core/geometry/GeometryEngine.java +++ b/src/com/esri/core/geometry/GeometryEngine.java @@ -37,7 +37,9 @@ public class GeometryEngine { private static OperatorFactoryLocal factory = OperatorFactoryLocal .getInstance(); - + + + /** * Imports the MapGeometry from its JSON representation. M and Z values are * not imported from JSON representation. @@ -88,6 +90,47 @@ public static String geometryToJson(SpatialReference spatialReference, return exporter.execute(spatialReference, geometry); } + public static String geometryToGeoJson(Geometry geometry) { + OperatorExportToGeoJson exporter = (OperatorExportToGeoJson) factory + .getOperator(Operator.Type.ExportToGeoJson); + + return exporter.execute(geometry); + } + + /** + * Exports the specified geometry instance to its GeoJSON representation. + * + * @see GeometryEngine#geometryToGeoJson(SpatialReference spatialReference, + * Geometry geometry) + * + * @param wkid + * The spatial reference Well Known ID to be used for the GeoJSON representation. + * @param geometry + * The geometry to be exported to GeoJSON. + * @return The GeoJSON representation of the specified geometry. + */ + public static String geometryToGeoJson(int wkid, Geometry geometry) { + return GeometryEngine.geometryToGeoJson( + wkid > 0 ? SpatialReference.create(wkid) : null, geometry); + } + + /** + * Exports the specified geometry instance to it's JSON representation. + * + * @param spatialReference + * The spatial reference of associated object. + * @param geometry + * The geometry. + * @return The GeoJSON representation of the specified geometry. + */ + public static String geometryToGeoJson(SpatialReference spatialReference, + Geometry geometry) { + OperatorExportToGeoJson exporter = (OperatorExportToGeoJson) factory + .getOperator(Operator.Type.ExportToGeoJson); + + return exporter.execute(spatialReference, geometry); + } + /** * Imports geometry from the ESRI shape file format. * @@ -557,7 +600,8 @@ public static Geometry[] cut(Geometry cuttee, Polyline cutter, return cutsList.toArray(new Geometry[0]); } - + + /** * Calculates a buffer polygon for each geometry at each of the * corresponding specified distances. It is assumed that all geometries have @@ -600,7 +644,8 @@ public static Polygon[] buffer(Geometry[] geometries, return buffers; } } - + + /** * Calculates a buffer polygon of the geometry as specified by the * distance input. The buffer is implemented in the xy-plane. @@ -773,7 +818,8 @@ static boolean isSimple(Geometry geometry, SpatialReference spatialReference) { boolean result = op.isSimpleAsFeature(geometry, spatialReference, null); return result; } - + + /** * A geodesic distance is the shortest distance between any two points on the earth's surface when the earth's * surface is approximated by a spheroid. The function returns the shortest distance between two points on the diff --git a/src/com/esri/core/geometry/ogc/OGCGeometry.java b/src/com/esri/core/geometry/ogc/OGCGeometry.java index 49328ab1..47c5c304 100644 --- a/src/com/esri/core/geometry/ogc/OGCGeometry.java +++ b/src/com/esri/core/geometry/ogc/OGCGeometry.java @@ -24,6 +24,7 @@ import com.esri.core.geometry.OperatorBuffer; import com.esri.core.geometry.OperatorConvexHull; import com.esri.core.geometry.OperatorExportToWkb; +import com.esri.core.geometry.OperatorExportToGeoJson; import com.esri.core.geometry.OperatorFactoryLocal; import com.esri.core.geometry.OperatorImportFromESRIShape; import com.esri.core.geometry.OperatorImportFromGeoJson; @@ -89,6 +90,12 @@ public ByteBuffer asBinary() { return op.execute(0, getEsriGeometry(), null); } + public String asGeoJson() { + OperatorExportToGeoJson op = (OperatorExportToGeoJson) OperatorFactoryLocal + .getInstance().getOperator(Operator.Type.ExportToGeoJson); + return op.execute(getEsriGeometry()); + } + public boolean isEmpty() { return getEsriGeometry().isEmpty(); } diff --git a/unittest/com/esri/core/geometry/TestGeomToGeoJson.java b/unittest/com/esri/core/geometry/TestGeomToGeoJson.java index 85801924..f47b83ea 100644 --- a/unittest/com/esri/core/geometry/TestGeomToGeoJson.java +++ b/unittest/com/esri/core/geometry/TestGeomToGeoJson.java @@ -1,5 +1,10 @@ package com.esri.core.geometry; +import com.esri.core.geometry.ogc.OGCGeometry; +import com.esri.core.geometry.ogc.OGCPoint; +import com.esri.core.geometry.ogc.OGCMultiPoint; +import com.esri.core.geometry.ogc.OGCLineString; +import com.esri.core.geometry.ogc.OGCPolygon; import junit.framework.TestCase; import org.junit.Test; @@ -32,6 +37,21 @@ public void testEmptyPoint() { assertEquals("{\"type\":\"Point\",\"coordinates\":null}", result); } + @Test + public void testPointGeometryEngine() { + Point p = new Point(10.0, 20.0); + String result = GeometryEngine.geometryToGeoJson(p); + assertEquals("{\"type\":\"Point\",\"coordinates\":[10.0,20.0]}", result); + } + + @Test + public void testOGCPoint() { + Point p = new Point(10.0, 20.0); + OGCGeometry ogcPoint = new OGCPoint(p, null); + String result = ogcPoint.asGeoJson(); + assertEquals("{\"type\":\"Point\",\"coordinates\":[10.0,20.0]}", result); + } + @Test public void testMultiPoint() { MultiPoint mp = new MultiPoint(); @@ -50,6 +70,25 @@ public void testEmptyMultiPoint() { assertEquals("{\"type\":\"MultiPoint\",\"coordinates\":null}", result); } + @Test + public void testMultiPointGeometryEngine() { + MultiPoint mp = new MultiPoint(); + mp.add(10.0, 20.0); + mp.add(20.0, 30.0); + String result = GeometryEngine.geometryToGeoJson(mp); + assertEquals("{\"type\":\"MultiPoint\",\"coordinates\":[[10.0,20.0],[20.0,30.0]]}", result); + } + + @Test + public void testOGCMultiPoint() { + MultiPoint mp = new MultiPoint(); + mp.add(10.0, 20.0); + mp.add(20.0, 30.0); + OGCMultiPoint ogcMultiPoint = new OGCMultiPoint(mp, null); + String result = ogcMultiPoint.asGeoJson(); + assertEquals("{\"type\":\"MultiPoint\",\"coordinates\":[[10.0,20.0],[20.0,30.0]]}", result); + } + @Test public void testPolyline() { Polyline p = new Polyline(); @@ -70,6 +109,29 @@ public void testEmptyPolyline() { assertEquals("{\"type\":\"LineString\",\"coordinates\":null}", result); } + @Test + public void testPolylineGeometryEngine() { + Polyline p = new Polyline(); + p.startPath(100.0, 0.0); + p.lineTo(101.0, 0.0); + p.lineTo(101.0, 1.0); + p.lineTo(100.0, 1.0); + String result = GeometryEngine.geometryToGeoJson(p); + assertEquals("{\"type\":\"LineString\",\"coordinates\":[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0]]}", result); + } + + @Test + public void testOGCLineString() { + Polyline p = new Polyline(); + p.startPath(100.0, 0.0); + p.lineTo(101.0, 0.0); + p.lineTo(101.0, 1.0); + p.lineTo(100.0, 1.0); + OGCLineString ogcLineString = new OGCLineString(p, 0, null); + String result = ogcLineString.asGeoJson(); + assertEquals("{\"type\":\"LineString\",\"coordinates\":[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0]]}", result); + } + @Test public void testPolygon() { Polygon p = new Polygon(); @@ -112,6 +174,72 @@ public void testEmptyPolygon() { assertEquals("{\"type\":\"Polygon\",\"coordinates\":null}", result); } + @Test + public void testPolygonGeometryEngine() { + Polygon p = new Polygon(); + p.startPath(100.0, 0.0); + p.lineTo(101.0, 0.0); + p.lineTo(101.0, 1.0); + p.lineTo(100.0, 1.0); + p.closePathWithLine(); + String result = GeometryEngine.geometryToGeoJson(p); + assertEquals("{\"type\":\"Polygon\",\"coordinates\":[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]]]}", result); + } + + @Test + public void testOGCPolygon() { + Polygon p = new Polygon(); + p.startPath(100.0, 0.0); + p.lineTo(101.0, 0.0); + p.lineTo(101.0, 1.0); + p.lineTo(100.0, 1.0); + p.closePathWithLine(); + OGCPolygon ogcPolygon = new OGCPolygon(p, null); + String result = ogcPolygon.asGeoJson(); + assertEquals("{\"type\":\"Polygon\",\"coordinates\":[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]]]}", result); + } + + @Test + public void testPolygonWithHoleGeometryEngine() { + Polygon p = new Polygon(); + + p.startPath(100.0, 0.0); + p.lineTo(101.0, 0.0); + p.lineTo(101.0, 1.0); + p.lineTo(100.0, 1.0); + p.closePathWithLine(); + + p.startPath(100.2, 0.2); + p.lineTo(100.8, 0.2); + p.lineTo(100.8, 0.8); + p.lineTo(100.2, 0.8); + p.closePathWithLine(); + + String result = GeometryEngine.geometryToGeoJson(p); + assertEquals("{\"type\":\"Polygon\",\"coordinates\":[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]],[[100.2,0.2],[100.8,0.2],[100.8,0.8],[100.2,0.8],[100.2,0.2]]]}", result); + } + + @Test + public void testOGCPolygonWithHole() { + Polygon p = new Polygon(); + + p.startPath(100.0, 0.0); + p.lineTo(101.0, 0.0); + p.lineTo(101.0, 1.0); + p.lineTo(100.0, 1.0); + p.closePathWithLine(); + + p.startPath(100.2, 0.2); + p.lineTo(100.8, 0.2); + p.lineTo(100.8, 0.8); + p.lineTo(100.2, 0.8); + p.closePathWithLine(); + + OGCPolygon ogcPolygon = new OGCPolygon(p, null); + String result = ogcPolygon.asGeoJson(); + assertEquals("{\"type\":\"Polygon\",\"coordinates\":[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]],[[100.2,0.2],[100.8,0.2],[100.8,0.8],[100.2,0.8],[100.2,0.2]]]}", result); + } + @Test public void testEnvelope() { Envelope e = new Envelope(); @@ -128,4 +256,12 @@ public void testEmptyEnvelope() { String result = exporter.execute(e); assertEquals("{\"bbox\":null}", result); } + + @Test + public void testEnvelopeGeometryEngine() { + Envelope e = new Envelope(); + e.setCoords(-180.0, -90.0, 180.0, 90.0); + String result = GeometryEngine.geometryToGeoJson(e); + assertEquals("{\"bbox\":[-180.0,-90.0,180.0,90.0]}", result); + } }