Skip to content

Commit

Permalink
add GeometryEngine#geometryToGeoJson and OGCGeometry#asGeoJson (with …
Browse files Browse the repository at this point in the history
…tests)
  • Loading branch information
geographist committed Jun 13, 2013
1 parent b021366 commit 67b08c4
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 4 deletions.
54 changes: 50 additions & 4 deletions src/com/esri/core/geometry/GeometryEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/com/esri/core/geometry/ogc/OGCGeometry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
136 changes: 136 additions & 0 deletions unittest/com/esri/core/geometry/TestGeomToGeoJson.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
}

0 comments on commit 67b08c4

Please sign in to comment.