Skip to content

Commit

Permalink
fix dimensions issue and bound geojson
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmach committed Nov 27, 2017
1 parent edb0fa6 commit 507e2c4
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 6 deletions.
7 changes: 7 additions & 0 deletions clip/smartclip/smart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ import (
"github.com/paulmach/orb"
)

func TestSmartClip(t *testing.T) {
bound := orb.Bound{Min: orb.Point{-1, -1}, Max: orb.Point{1, 1}}
for _, g := range orb.AllGeometries {
SmartClip(bound, g, orb.CCW)
}
}

func TestRing(t *testing.T) {
oneSix := orb.Bound{Min: orb.Point{1, 1}, Max: orb.Point{6, 6}}

Expand Down
9 changes: 6 additions & 3 deletions geojson/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ func (f Feature) MarshalJSON() ([]byte, error) {
err error
)

if ring, ok := f.Geometry.(orb.Ring); ok {
coords, err = json.Marshal(orb.Polygon{ring})
} else {
switch g := f.Geometry.(type) {
case orb.Ring:
coords, err = json.Marshal(orb.Polygon{g})
case orb.Bound:
coords, err = json.Marshal(g.ToPolygon())
default:
coords, err = json.Marshal(f.Geometry)
}
if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions geojson/feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ func TestFeatureMarshalJSON(t *testing.T) {
}
}

func TestFeatureMarshalJSON_Bound(t *testing.T) {
f := NewFeature(orb.Bound{Min: orb.Point{1, 1}, Max: orb.Point{2, 2}})
blob, err := f.MarshalJSON()

if err != nil {
t.Fatalf("error marshalling to json: %v", err)
}

if !bytes.Contains(blob, []byte(`"type":"Polygon"`)) {
t.Errorf("should set type to polygon")
}

if !bytes.Contains(blob, []byte(`"coordinates":[[[1,1],[2,1],[2,2],[1,2],[1,1]]]`)) {
t.Errorf("should set type to polygon coords: %v", string(blob))
}
}

func TestFeatureMarshal(t *testing.T) {
f := NewFeature(orb.Point{1, 2})
blob, err := json.Marshal(f)
Expand Down
2 changes: 1 addition & 1 deletion multi_line_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func (mls MultiLineString) GeoJSONType() string {

// Dimensions returns 1 because a MultiLineString is a 2d object.
func (mls MultiLineString) Dimensions() int {
return 2
return 1
}

// Bound returns a bound around all the line strings.
Expand Down
2 changes: 1 addition & 1 deletion multi_point.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func (mp MultiPoint) GeoJSONType() string {

// Dimensions returns 0 because a MultiPoint is a 0d object.
func (mp MultiPoint) Dimensions() int {
return 2
return 0
}

// Clone returns a new copy of the points.
Expand Down
2 changes: 1 addition & 1 deletion planar/area.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func Area(g orb.Geometry) float64 {
return a
}

// CentroidArea returns botht the centroid and the area in the 2d plane.
// CentroidArea returns both the centroid and the area in the 2d plane.
// Since the area is need for the centroid, return both.
func CentroidArea(g orb.Geometry) (orb.Point, float64) {
if g == nil {
Expand Down

0 comments on commit 507e2c4

Please sign in to comment.