Skip to content

Commit

Permalink
Merge pull request #101 from spatial-go/fix-100-geojson-decode
Browse files Browse the repository at this point in the history
fix: FeatureCollection parsing bug
  • Loading branch information
coolwxb committed Sep 21, 2023
2 parents cba139c + 249c0f4 commit 35705f2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 10 additions & 0 deletions geoencoding/geojson/feature_collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ import (
"github.com/spatial-go/geoos/space"
)

func TestFeaturecollectionDecode(t *testing.T) {
fc := `{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[122.993197,41.117725],[122.999399,41.115696],[122.99573,41.109516],[122.987146,41.106994],[122.984775,41.107699],[122.990687,41.117878],[122.993197,41.117725]]]},"properties":{}}]}`
encoder := Encoder{}
geom, err := encoder.Decode([]byte(fc))
if err != nil {
t.Fatalf("should unmarshal feature collection without issue, err %v", err)
}
println(geom)
}

func TestNewFeatureCollection(t *testing.T) {
fc := NewFeatureCollection()

Expand Down
4 changes: 2 additions & 2 deletions geoencoding/geojson/geojson_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (e *Encoder) Encode(g space.Geometry) []byte {

// Decode Returns geometry of that decode string by codeType.
func (e *Encoder) Decode(s []byte) (space.Geometry, error) {
if strings.Contains(string(s[9:20]), "FeatureCollection") {
if strings.Contains(string(s), "\"type\":\"FeatureCollection\"") {
colls, err := UnmarshalFeatureCollection(s)
if err != nil {
log.Println(err)
Expand All @@ -33,7 +33,7 @@ func (e *Encoder) Decode(s []byte) (space.Geometry, error) {
geom = append(geom, v.Geometry.Geometry())
}
return geom, nil
} else if strings.Contains(string(s[9:20]), "Feature") {
} else if strings.Contains(string(s), "\"type\":\"Feature\"") {
feat, err := UnmarshalFeature(s)
if err != nil {
log.Println(err)
Expand Down

0 comments on commit 35705f2

Please sign in to comment.