Permalink
Browse files

GEOT-4208, GEOT-4218, handling GeometryCollection within a feature ob…

…ject
  • Loading branch information...
1 parent 8b69e52 commit c984ea7d42f3240e72792356ab86915a8108177e jdeolive committed Aug 3, 2012
@@ -60,7 +60,11 @@
protected static List NULL_LIST = Collections.unmodifiableList(new ArrayList(0));
protected ContentHandler delegate = NULL;
-
+
+ public ContentHandler getDelegate() {
+ return delegate;
+ }
+
public void startJSON() throws ParseException, IOException {
delegate.startJSON();
}
@@ -24,6 +24,7 @@
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geojson.DelegatingHandler;
import org.geotools.geojson.IContentHandler;
+import org.geotools.geojson.geom.GeometryCollectionHandler;
import org.geotools.geojson.geom.GeometryHandler;
import org.json.simple.parser.ParseException;
import org.opengis.feature.simple.SimpleFeature;
@@ -124,15 +125,23 @@ public boolean endObject() throws ParseException, IOException {
((IContentHandler) delegate).endObject();
if (delegate instanceof GeometryHandler) {
- if (properties != null) {
- //this is a regular property
- values.add(((IContentHandler<Geometry>)delegate).getValue());
+ Geometry g = ((IContentHandler<Geometry>)delegate).getValue();
+ if (g == null &&
+ ((GeometryHandler)delegate).getDelegate() instanceof GeometryCollectionHandler) {
+ //this means that the collecetion handler is still parsing objects, continue
+ // to delegate to it
}
else {
- //its the default geometry
- geometry = ((IContentHandler<Geometry>)delegate).getValue();
+ if (properties != null) {
+ //this is a regular property
+ values.add(g);
+ }
+ else {
+ //its the default geometry
+ geometry = g;
+ }
+ delegate = NULL;
}
- delegate = NULL;
}
else if (delegate instanceof CRSHandler) {
crs = ((CRSHandler)delegate).getValue();
@@ -36,6 +36,7 @@
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.Geometry;
+import com.vividsolutions.jts.geom.GeometryCollection;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.Point;
@@ -104,7 +105,84 @@ public void testFeatureRead() throws Exception {
SimpleFeature f2 = fjson.readFeature(reader(strip(featureText(1))));
assertEqualsLax(f1, f2);
}
-
+
+ public void testFeatureWithGeometryCollectionRead() throws Exception {
+ String json = strip("{" +
+ " 'type':'Feature'," +
+ " 'geometry': {" +
+ " 'type':'GeometryCollection'," +
+ " 'geometries':[{" +
+ " 'type':'Point','coordinates':[4,6]" +
+ " },{" +
+ " 'type':'LineString','coordinates':[[4,6],[7,10]]" +
+ " }" +
+ " ]" +
+ " }," +
+ " 'properties':{" +
+ " 'name':'Name123'," +
+ " 'label':'Label321'," +
+ " 'roles':'[1,2,3]'" +
+ " }," +
+ " 'id':'fid-7205cfc1_138e7ce8900_-7ffe'" +
+ "}");
+
+ SimpleFeature f1 = fjson.readFeature(json);
+ assertNotNull(f1.getDefaultGeometry());
+
+ GeometryCollection gc = (GeometryCollection) f1.getDefaultGeometry();
+ assertEquals(2, gc.getNumGeometries());
+
+ WKTReader wkt = new WKTReader();
+ assertTrue(wkt.read("POINT (4 6)").equals(gc.getGeometryN(0)));
+ assertTrue(wkt.read("LINESTRING (4 6, 7 10)").equals(gc.getGeometryN(1)));
+
+ assertEquals("fid-7205cfc1_138e7ce8900_-7ffe", f1.getID());
+ assertEquals("Name123", f1.getAttribute("name"));
+ assertEquals("Label321", f1.getAttribute("label"));
+ assertEquals("[1,2,3]", f1.getAttribute("roles"));
+ }
+
+ public void testFeatureWithGeometryCollectionRead2() throws Exception {
+ String json = strip("{"+
+ " 'type':'Feature',"+
+ " 'geometry':{"+
+ " 'type':'GeometryCollection',"+
+ " 'geometries':["+
+ " {"+
+ " 'type':'Polygon',"+
+ " 'coordinates':[[[-28.1107, 142.998], [-28.1107, 148.623], [-30.2591, 148.623], [-30.2591, 142.998], [-28.1107, 142.998]]]"+
+ " },"+
+ " {"+
+ " 'type':'Polygon',"+
+ " 'coordinates':[[[-27.1765, 142.998], [-25.6811, 146.4258], [-27.1765, 148.5352], [-27.1765, 142.998]]]"+
+ " }"+
+ " ]"+
+ " },"+
+ " 'properties':{"+
+ " 'name':'',"+
+ " 'caseSN':'x_2000a',"+
+ " 'siteNum':2"+
+ " },"+
+ " 'id':'fid-397164b3_13880d348b9_-7a5c'"+
+ "}");
+
+ SimpleFeature f1 = fjson.readFeature(json);
+ assertNotNull(f1.getDefaultGeometry());
+
+ GeometryCollection gc = (GeometryCollection) f1.getDefaultGeometry();
+ assertEquals(2, gc.getNumGeometries());
+
+ WKTReader wkt = new WKTReader();
+ assertTrue(wkt.read("POLYGON ((-28.1107 142.998, -28.1107 148.623, -30.2591 148.623, -30.2591 142.998, -28.1107 142.998))").equals(gc.getGeometryN(0)));
+ assertTrue(wkt.read("POLYGON((-27.1765 142.998, -25.6811 146.4258, -27.1765 148.5352, -27.1765 142.998))").equals(gc.getGeometryN(1)));
+
+ assertEquals("fid-397164b3_13880d348b9_-7a5c", f1.getID());
+ assertEquals("", f1.getAttribute("name"));
+ assertEquals("x_2000a", f1.getAttribute("caseSN"));
+ assertEquals(2l, f1.getAttribute("siteNum"));
+
+
+ }
public void testFeatureWithRegularGeometryAttributeRead() throws Exception {
SimpleFeature f = fjson.readFeature(reader(strip("{" +
" 'type': 'Feature'," +

0 comments on commit c984ea7

Please sign in to comment.