Skip to content

Commit

Permalink
Merge pull request #117 from paulmach/geojson-unmarshal
Browse files Browse the repository at this point in the history
geojson: ensure geometry unmarshal errors get returned
  • Loading branch information
paulmach authored Jan 5, 2023
2 parents 7f89869 + 6d4a381 commit ace85f4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
4 changes: 2 additions & 2 deletions geojson/example_pointer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ type CentroidPoint struct {
func (cp CentroidPoint) Point() orb.Point {
// this is where you would decide how to define
// the representative point of the feature.
c, _ := planar.CentroidArea(cp.Feature.Geometry)
c, _ := planar.CentroidArea(cp.Geometry)
return c
}

func main() {
func Example_centroid() {
qt := quadtree.New(orb.Bound{Min: orb.Point{0, 0}, Max: orb.Point{1, 1}})

// feature with center {0.5, 0.5} but centroid {0.25, 0.25}
Expand Down
18 changes: 18 additions & 0 deletions geojson/geometry.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,26 +113,44 @@ func (g *Geometry) UnmarshalJSON(data []byte) error {
case "Point":
p := orb.Point{}
err = unmarshalJSON(jg.Coordinates, &p)
if err != nil {
return err
}
g.Coordinates = p
case "MultiPoint":
mp := orb.MultiPoint{}
err = unmarshalJSON(jg.Coordinates, &mp)
if err != nil {
return err
}
g.Coordinates = mp
case "LineString":
ls := orb.LineString{}
err = unmarshalJSON(jg.Coordinates, &ls)
if err != nil {
return err
}
g.Coordinates = ls
case "MultiLineString":
mls := orb.MultiLineString{}
err = unmarshalJSON(jg.Coordinates, &mls)
if err != nil {
return err
}
g.Coordinates = mls
case "Polygon":
p := orb.Polygon{}
err = unmarshalJSON(jg.Coordinates, &p)
if err != nil {
return err
}
g.Coordinates = p
case "MultiPolygon":
mp := orb.MultiPolygon{}
err = unmarshalJSON(jg.Coordinates, &mp)
if err != nil {
return err
}
g.Coordinates = mp
case "GeometryCollection":
g.Geometries = jg.Geometries
Expand Down
49 changes: 45 additions & 4 deletions geojson/geometry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ func TestGeometryUnmarshal(t *testing.T) {
}{
{
name: "point",
geom: orb.Point{},
geom: orb.Point{1, 2},
},
{
name: "multi point",
geom: orb.MultiPoint{},
geom: orb.MultiPoint{{1, 2}, {3, 4}},
},
{
name: "linestring",
geom: orb.LineString{},
geom: orb.LineString{{1, 2}, {3, 4}, {5, 6}},
},
{
name: "multi linestring",
Expand All @@ -130,7 +130,7 @@ func TestGeometryUnmarshal(t *testing.T) {
},
{
name: "collection",
geom: orb.Collection{orb.LineString{}},
geom: orb.Collection{orb.LineString{{1, 2}, {3, 4}}, orb.Point{5, 6}},
},
}

Expand Down Expand Up @@ -195,6 +195,47 @@ func TestGeometryUnmarshal(t *testing.T) {
}
}

func TestGeometryUnmarshal_errors(t *testing.T) {
cases := []struct {
name string
data string
}{
{
name: "point",
data: `{"type":"Point","coordinates":1}`,
},
{
name: "multi point",
data: `{"type":"MultiPoint","coordinates":2}`,
},
{
name: "linestring",
data: `{"type":"LineString","coordinates":3}`,
},
{
name: "multi linestring",
data: `{"type":"MultiLineString","coordinates":4}`,
},
{
name: "polygon",
data: `{"type":"Polygon","coordinates":10.2}`,
},
{
name: "multi polygon",
data: `{"type":"MultiPolygon","coordinates":{}}`,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
_, err := UnmarshalGeometry([]byte(tc.data))
if err == nil {
t.Errorf("expected error, got nothing")
}
})
}
}

func TestHelperTypes(t *testing.T) {
// This test makes sure the marshal-unmarshal loop does the same thing.
// The code and types here are complicated to avoid duplicate code.
Expand Down

0 comments on commit ace85f4

Please sign in to comment.