Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
import com.esri.core.geometry.GeometryCursor;
import com.esri.core.geometry.Polygon;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.geometry.Operator;
import com.esri.core.geometry.JsonCursor;
import com.esri.core.geometry.OperatorFactoryLocal;
import com.esri.core.geometry.OperatorExportToGeoJson;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
Expand Down Expand Up @@ -323,4 +328,34 @@ public OGCGeometry convertToMulti()
public String asJson() {
throw new UnsupportedOperationException();
}

@Override
public String asGeoJson() {
StringBuilder sb = new StringBuilder();

OperatorExportToGeoJson op = (OperatorExportToGeoJson) OperatorFactoryLocal
.getInstance().getOperator(Operator.Type.ExportToGeoJson);
JsonCursor cursor = op.execute(this.esriSR, getEsriGeometryCursor());

sb.append("{\"type\" : \"GeometryCollection\", \"geometries\" : ");
String shape = cursor.next();
if (shape == null){
// geometry collection with empty list of geometries
sb.append("[]}");
return sb.toString();
}

sb.append("[");
sb.append(shape);

while(true){
shape = cursor.next();
if(shape == null)
break;
sb.append(", ").append(shape);
}

sb.append("]}");
return sb.toString();
}
}
45 changes: 45 additions & 0 deletions src/test/java/com/esri/core/geometry/TestGeomToGeoJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@
import com.esri.core.geometry.ogc.OGCMultiPoint;
import com.esri.core.geometry.ogc.OGCLineString;
import com.esri.core.geometry.ogc.OGCPolygon;
import com.esri.core.geometry.ogc.OGCConcreteGeometryCollection;
import junit.framework.TestCase;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonParser;
import org.json.JSONException;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class TestGeomToGeoJson extends TestCase {
OperatorFactoryLocal factory = OperatorFactoryLocal.getInstance();
Expand Down Expand Up @@ -358,4 +361,46 @@ public void testEnvelopeGeometryEngine() {
assertEquals("{\"bbox\":[-180.0,-90.0,180.0,90.0]}", result);
}

@Test
public void testGeometryCollection(){
SpatialReference sr = SpatialReference.create(4326);

StringBuilder geometrySb = new StringBuilder();
geometrySb.append("{\"type\" : \"GeometryCollection\", \"geometries\" : [");

OGCPoint point = new OGCPoint(new Point(1.0, 1.0), sr);
assertEquals("{\"x\":1,\"y\":1,\"spatialReference\":{\"wkid\":4326}}", point.asJson());
assertEquals("{\"type\":\"Point\",\"coordinates\":[1.0,1.0]}", point.asGeoJson());
geometrySb.append(point.asGeoJson()).append(", ");

OGCLineString line = new OGCLineString(new Polyline(new Point(1.0, 1.0), new Point(2.0, 2.0)), 0, sr);
assertEquals("{\"paths\":[[[1,1],[2,2]]],\"spatialReference\":{\"wkid\":4326}}", line.asJson());
assertEquals("{\"type\":\"LineString\",\"coordinates\":[[1.0,1.0],[2.0,2.0]]}", line.asGeoJson());
geometrySb.append(line.asGeoJson()).append(", ");

Polygon p = new Polygon();
p.startPath(1.0, 1.0);
p.lineTo(2.0, 2.0);
p.lineTo(3.0, 1.0);
p.lineTo(2.0, 0.0);

OGCPolygon polygon = new OGCPolygon(p, sr);
assertEquals("{\"rings\":[[[1,1],[2,2],[3,1],[2,0],[1,1]]],\"spatialReference\":{\"wkid\":4326}}",
polygon.asJson());
assertEquals("{\"type\":\"Polygon\",\"coordinates\":[[[1.0,1.0],[2.0,2.0],[3.0,1.0],[2.0,0.0],[1.0,1.0]]]}",
polygon.asGeoJson());
geometrySb.append(polygon.asGeoJson()).append("]}");

List<OGCGeometry> geoms = new ArrayList<OGCGeometry>(3);
geoms.add(point);geoms.add(line);geoms.add(polygon);
OGCConcreteGeometryCollection collection = new OGCConcreteGeometryCollection(geoms, sr);
assertEquals(geometrySb.toString(), collection.asGeoJson());
}

@Test
public void testEmptyGeometryCollection(){
SpatialReference sr = SpatialReference.create(4326);
OGCConcreteGeometryCollection collection = new OGCConcreteGeometryCollection(new ArrayList<OGCGeometry>(), sr);
assertEquals("{\"type\" : \"GeometryCollection\", \"geometries\" : []}", collection.asGeoJson());
}
}