Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions geo/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,19 @@ func extract2DCoordinates(thing interface{}) [][]float64 {

func extract3DCoordinates(thing interface{}) (c [][][]float64) {
coords := reflect.ValueOf(thing)
for i := 0; i < coords.Len(); i++ {
vals := coords.Index(i)

edges := vals.Interface()
if es, ok := edges.([]interface{}); ok {
loop := extract2DCoordinates(es)
if len(loop) > 0 {
c = append(c, loop)
if !coords.IsValid() {
return nil
}

if coords.Kind() == reflect.Slice {
for i := 0; i < coords.Len(); i++ {
vals := coords.Index(i)
edges := vals.Interface()
if es, ok := edges.([]interface{}); ok {
loop := extract2DCoordinates(es)
if len(loop) > 0 {
c = append(c, loop)
}
}
}
}
Expand Down
102 changes: 101 additions & 1 deletion geo/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
package geo

import (
"encoding/json"
"reflect"
"testing"
)

func TestExtractGeoPoint(t *testing.T) {

tests := []struct {
in interface{}
lon float64
Expand Down Expand Up @@ -353,3 +353,103 @@ func TestExtractGeoShape(t *testing.T) {
}
}
}

func TestExtractGeoShapeCoordinates(t *testing.T) {
tests := []struct {
x []byte
typ string
expectOK bool
}{
{
x: []byte(`[
[
[77.58894681930542,12.976498523818783],
[77.58677959442139,12.974533005048169],
[77.58894681930542,12.976498523818783]
]
]`),
typ: PolygonType,
expectOK: true,
},
{ // Invalid construct, but handled
x: []byte(`[
[
{"lon":77.58894681930542,"lat":12.976498523818783},
{"lon":77.58677959442139,"lat":12.974533005048169},
{"lon":77.58894681930542,"lat":12.976498523818783}
]
]`),
typ: PolygonType,
expectOK: false,
},
{ // Invalid construct causes panic (within extract3DCoordinates), fix MB-65807
x: []byte(`{
"coordinates": [
[77.58894681930542,12.976498523818783],
[77.58677959442139,12.974533005048169],
[77.58894681930542,12.976498523818783]
]
}`),
typ: PolygonType,
expectOK: false,
},
{
x: []byte(`[
[
[
[-0.163421630859375,51.531600743186644],
[-0.15277862548828125,51.52455221546295],
[-0.15895843505859375,51.53693981046689],
[-0.163421630859375,51.531600743186644]
]
],
[
[
[-0.1902008056640625,51.5091698216777],
[-0.1599884033203125,51.51322956905176],
[-0.1902008056640625,51.5091698216777]
]
]
]`),
typ: MultiPolygonType,
expectOK: true,
},
{ // Invalid construct causes panic (within extract3DCoordinates), fix MB-65807
x: []byte(`[
{
"coordinates": [
[-0.163421630859375,51.531600743186644],
[-0.15277862548828125,51.52455221546295],
[-0.15895843505859375,51.53693981046689],
[-0.163421630859375,51.531600743186644]
]
},
{
"coordinates": [
[-0.1902008056640625,51.5091698216777],
[-0.1599884033203125,51.51322956905176],
[-0.1902008056640625,51.5091698216777]
]
}
]`),
typ: MultiPolygonType,
expectOK: false,
},
}

for i := range tests {
var x interface{}
if err := json.Unmarshal(tests[i].x, &x); err != nil {
t.Fatalf("[%d] JSON err: %v", i+1, err)
}

_, typ, ok := ExtractGeoShapeCoordinates(x, tests[i].typ)
if ok != tests[i].expectOK {
t.Errorf("[%d] expected ok %t, got %t", i+1, tests[i].expectOK, ok)
}

if ok && typ != tests[i].typ {
t.Errorf("[%d] expected type %s, got %s", i+1, tests[i].typ, typ)
}
}
}