Skip to content
This repository has been archived by the owner on Aug 23, 2024. It is now read-only.

Fix GeometryCollection writing issue. Add a test for GeometryCollection. #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/main/java/org/mapfish/geo/MfGeoJSONWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ public void encodeGeometry(Geometry g) throws JSONException {
}

private void encodeGeomCollection(GeometryCollection collection) throws JSONException {
builder.array();
builder.key("geometries");
builder.array();

for (int i = 0, n = collection.getNumGeometries(); i < n; i++) {
encodeGeometry(collection.getGeometryN(i));
Expand Down
28 changes: 20 additions & 8 deletions src/test/java/org/mapfish/geo/MfGeoJSONWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryCollection;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LinearRing;

import junit.framework.TestCase;

import org.json.JSONStringer;
import org.json.JSONWriter;
import org.json.JSONException;
Expand Down Expand Up @@ -51,8 +54,6 @@ public void testMfGeoJSON() throws JSONException {
JSONStringer stringer;
MfGeoJSONWriter builder;

Geometry g;

MfFeature f1;
MfFeature f2;
MfFeatureCollection fc;
Expand All @@ -67,21 +68,21 @@ public void testMfGeoJSON() throws JSONException {
// encodeGeometry POINT test
stringer = new JSONStringer();
builder = new MfGeoJSONWriter(stringer);
g = new GeometryFactory().createPoint(coord1);
Geometry point = new GeometryFactory().createPoint(coord1);
geojsonExpected =
"{\"type\":\"Point\",\"coordinates\":[1.1,2.2]}";
builder.encodeGeometry(g);
builder.encodeGeometry(point);
geojsonResulted = stringer.toString();
assertTrue(geojsonExpected.equals(geojsonResulted));

// encodeGeometry LINESTRING test
stringer = new JSONStringer();
builder = new MfGeoJSONWriter(stringer);
Coordinate[] coordArrayLS = {coord1, coord2};
g = new GeometryFactory().createLineString(coordArrayLS);
Geometry ls = new GeometryFactory().createLineString(coordArrayLS);
geojsonExpected =
"{\"type\":\"LineString\",\"coordinates\":[[1.1,2.2],[3.3,4.4]]}";
builder.encodeGeometry(g);
builder.encodeGeometry(ls);
geojsonResulted = stringer.toString();
assertTrue(geojsonExpected.equals(geojsonResulted));

Expand All @@ -90,13 +91,24 @@ public void testMfGeoJSON() throws JSONException {
builder = new MfGeoJSONWriter(stringer);
Coordinate[] coordArrayLR = {coord1, coord2, coord3, coord1};
LinearRing lr = new GeometryFactory().createLinearRing(coordArrayLR);
g = new GeometryFactory().createPolygon(lr, null);
Geometry poly = new GeometryFactory().createPolygon(lr, null);
geojsonExpected =
"{\"type\":\"Polygon\",\"coordinates\":[[[1.1,2.2],[3.3,4.4],[5.5,6.6],[1.1,2.2]]]}";
builder.encodeGeometry(g);
builder.encodeGeometry(poly);
geojsonResulted = stringer.toString();
assertTrue(geojsonExpected.equals(geojsonResulted));

// encodeGeometry GeometryCollection test
stringer = new JSONStringer();
builder = new MfGeoJSONWriter(stringer);
Geometry[] geo = {point, ls, poly};
GeometryCollection gc = new GeometryFactory().createGeometryCollection(geo);
geojsonExpected =
"{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"Point\",\"coordinates\":[1.1,2.2]},{\"type\":\"LineString\",\"coordinates\":[[1.1,2.2],[3.3,4.4]]},{\"type\":\"Polygon\",\"coordinates\":[[[1.1,2.2],[3.3,4.4],[5.5,6.6],[1.1,2.2]]]}]}";
builder.encodeGeometry(gc);
geojsonResulted = stringer.toString();
assertTrue(geojsonExpected.equals(geojsonResulted));

// encodeFeature test
stringer = new JSONStringer();
builder = new MfGeoJSONWriter(stringer);
Expand Down