Skip to content

Commit

Permalink
Merge pull request bjornharrtell#13 from jeffreyeriksondg/handle-geom…
Browse files Browse the repository at this point in the history
…etry-collection

Add annotations for handling Geometry subclasses in GeometryCollection
  • Loading branch information
bjornharrtell committed Aug 27, 2016
2 parents cdae154 + 5dd9c69 commit 3cd0a4d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/main/java/org/wololo/geojson/Geometry.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXISTING_PROPERTY,
property = "type"
)
@JsonSubTypes( {
@JsonSubTypes.Type(value=Point.class, name="Point" ),
@JsonSubTypes.Type(value=LineString.class, name="LineString" ),
@JsonSubTypes.Type(value=Polygon.class, name="Polygon" ),
@JsonSubTypes.Type(value=MultiPoint.class, name="MultiPoint" ),
@JsonSubTypes.Type(value=MultiLineString.class, name="MultiLineString" ),
@JsonSubTypes.Type(value=MultiPolygon.class, name="MultiPolygon" ),
@JsonSubTypes.Type(value=Feature.class, name="Feature" ),
@JsonSubTypes.Type(value=FeatureCollection.class, name="FeatureCollection" ),
@JsonSubTypes.Type(value=GeometryCollection.class, name="GeometryCollection" )
} )

@JsonPropertyOrder({"type", "coordinates", "bbox"})
public abstract class Geometry extends GeoJSON {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.wololo.jts2geojson

import java.util.HashMap

import org.scalatest.WordSpec
import org.wololo.geojson._

class DeserializeGeometryCollectionSpec extends WordSpec {
"GeoJSONFactory" when {
"parsing GeoJSON to object" should {
val geometry = new Point(Array(1, 1))
val properties = new HashMap[String, Object]()
properties.put("test", 1.asInstanceOf[Object])
val feature = new Feature(geometry, properties)


"deserialize GeometryCollection successfully" in {
val geoJSON = """{"type": "GeometryCollection", "geometries": [{"type": "Point", "coordinates": [1.1, 2.2] }]}"""
val json = GeoJSONFactory.create(geoJSON)
assert(json.isInstanceOf[GeometryCollection])
val gc = json.asInstanceOf[GeometryCollection]
assert(gc.getGeometries.nonEmpty)
val g = gc.getGeometries.head
assert(g != null)
assert(g.isInstanceOf[Point])
val point = g.asInstanceOf[Point]
assert(point.getCoordinates.toSeq == Seq(1.1, 2.2))
}

}

}
}

0 comments on commit 3cd0a4d

Please sign in to comment.