2013-10-21 17:33:47 +04:00
|
|
|
package geojson
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestParsePolygon(t *testing.T) {
|
2015-03-02 11:22:08 +03:00
|
|
|
r := bytes.NewBufferString(`{"type": "Polygon", "coordinates": [[[8, 50], [11, 50], [11, 53], [8, 53], [8, 50]]]}`)
|
|
|
|
features, err := ParseGeoJSON(r)
|
2013-10-21 17:33:47 +04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-05-21 11:15:32 +04:00
|
|
|
if len(features) != 1 {
|
|
|
|
t.Fatal(features)
|
2013-10-21 17:33:47 +04:00
|
|
|
}
|
|
|
|
|
2015-03-02 11:22:08 +03:00
|
|
|
if len(features[0].Polygon[0]) != 5 {
|
|
|
|
t.Fatal(features)
|
2013-10-21 17:33:47 +04:00
|
|
|
}
|
|
|
|
|
2013-11-26 12:47:00 +04:00
|
|
|
// ignore z values
|
2015-03-02 11:22:08 +03:00
|
|
|
r = bytes.NewBufferString(`{"type": "Polygon", "coordinates": [[[8, 50, 0], [11, 50, 0], [11, 53, 0], [8, 53, 0], [8, 50, 0]]]}`)
|
|
|
|
features, err = ParseGeoJSON(r)
|
2013-11-26 12:47:00 +04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-05-21 11:15:32 +04:00
|
|
|
if len(features) != 1 {
|
|
|
|
t.Fatal(features)
|
2013-11-26 12:47:00 +04:00
|
|
|
}
|
|
|
|
|
2015-03-02 11:22:08 +03:00
|
|
|
if len(features[0].Polygon[0]) != 5 {
|
|
|
|
t.Fatal(features)
|
2013-11-26 12:47:00 +04:00
|
|
|
}
|
|
|
|
|
2015-03-02 11:22:08 +03:00
|
|
|
if p := features[0].Polygon[0][0]; p.Long != 8.0 || p.Lat != 50 {
|
|
|
|
t.Fatal(features)
|
|
|
|
}
|
|
|
|
|
|
|
|
// with hole
|
|
|
|
r = bytes.NewBufferString(`{"type": "Polygon", "coordinates": [[[8, 50], [11, 50], [11, 53], [8, 53], [8, 50]], [[9, 51], [10, 51], [10, 52], [9, 52], [9, 51]]]}`)
|
|
|
|
features, err = ParseGeoJSON(r)
|
2013-10-21 17:33:47 +04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-05-21 11:15:32 +04:00
|
|
|
if len(features) != 1 {
|
|
|
|
t.Fatal(features)
|
2013-10-21 17:33:47 +04:00
|
|
|
}
|
|
|
|
|
2015-03-02 11:22:08 +03:00
|
|
|
if len(features[0].Polygon) != 2 {
|
|
|
|
t.Fatal(features)
|
2013-10-21 17:33:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseMultiPolygon(t *testing.T) {
|
|
|
|
r := bytes.NewBufferString(`{"type": "MultiPolygon", "coordinates":
|
2015-03-02 11:22:08 +03:00
|
|
|
[[[[8, 50], [11, 50], [11, 53], [8, 50]]],
|
|
|
|
[[[8, 50], [11, 50], [11, 53], [8, 50]]]]
|
2013-10-21 17:33:47 +04:00
|
|
|
}`)
|
2015-03-02 11:22:08 +03:00
|
|
|
features, err := ParseGeoJSON(r)
|
2013-10-21 17:33:47 +04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-05-21 11:15:32 +04:00
|
|
|
if len(features) != 2 {
|
|
|
|
t.Fatal(features)
|
2013-10-21 17:33:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseFeature(t *testing.T) {
|
|
|
|
r := bytes.NewBufferString(`{"type": "Feature", "geometry": {
|
2015-03-02 11:22:08 +03:00
|
|
|
"type": "Polygon", "coordinates": [[[8, 50], [11, 50], [11, 53], [8, 53], [8, 50]]]
|
2013-10-21 17:33:47 +04:00
|
|
|
}}`)
|
2015-03-02 11:22:08 +03:00
|
|
|
features, err := ParseGeoJSON(r)
|
2013-10-21 17:33:47 +04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-05-21 11:15:32 +04:00
|
|
|
if len(features) != 1 {
|
|
|
|
t.Fatal(features)
|
2013-10-21 17:33:47 +04:00
|
|
|
}
|
2015-03-02 11:22:08 +03:00
|
|
|
if len(features[0].Polygon[0]) != 5 {
|
|
|
|
t.Fatal(features)
|
2013-10-21 17:33:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseFeatureCollection(t *testing.T) {
|
|
|
|
r := bytes.NewBufferString(`{"type": "FeatureCollection", "features": [
|
|
|
|
{"type": "Feature", "geometry":
|
2015-03-02 11:22:08 +03:00
|
|
|
{"type": "Polygon", "coordinates": [[[8, 50], [11, 50], [11, 53], [8, 53], [8, 50]]]}
|
2013-10-21 17:33:47 +04:00
|
|
|
},
|
|
|
|
{"type": "Feature", "geometry":
|
2015-03-02 11:22:08 +03:00
|
|
|
{"type": "Polygon", "coordinates": [[[8, 50], [11, 50], [11, 53], [8, 53], [8, 50]]]}
|
2013-10-21 17:33:47 +04:00
|
|
|
}
|
|
|
|
]}`)
|
2015-03-02 11:22:08 +03:00
|
|
|
features, err := ParseGeoJSON(r)
|
2013-10-21 17:33:47 +04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-05-21 11:15:32 +04:00
|
|
|
if len(features) != 2 {
|
|
|
|
t.Fatal(features)
|
2013-10-21 17:33:47 +04:00
|
|
|
}
|
2015-03-02 11:22:08 +03:00
|
|
|
if len(features[0].Polygon[0]) != 5 {
|
|
|
|
t.Fatal(features)
|
|
|
|
}
|
|
|
|
if len(features[1].Polygon[0]) != 5 {
|
|
|
|
t.Fatal(features)
|
2013-12-02 17:13:31 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-02 11:22:08 +03:00
|
|
|
func TestParseProperties(t *testing.T) {
|
2013-12-02 17:13:31 +04:00
|
|
|
r := bytes.NewBufferString(`{"type": "FeatureCollection", "features": [
|
2014-05-21 11:15:32 +04:00
|
|
|
{"type": "Feature", "properties": {"foo": "bar", "baz": 42}, "geometry":
|
2015-03-02 11:22:08 +03:00
|
|
|
{"type": "Polygon", "coordinates": [[[8, 50], [11, 50], [11, 53], [8, 53], [8, 50]]]}
|
2013-12-02 17:13:31 +04:00
|
|
|
},
|
|
|
|
{"type": "Feature", "geometry":
|
2015-03-02 11:22:08 +03:00
|
|
|
{"type": "Polygon", "coordinates": [[[8, 50], [11, 50], [11, 53], [8, 53], [8, 50]]]}
|
2013-12-02 17:13:31 +04:00
|
|
|
}
|
|
|
|
]}`)
|
2015-03-02 11:22:08 +03:00
|
|
|
features, err := ParseGeoJSON(r)
|
2013-12-02 17:13:31 +04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-05-21 11:15:32 +04:00
|
|
|
if len(features) != 2 {
|
|
|
|
t.Fatal(features)
|
|
|
|
}
|
|
|
|
if v, ok := features[0].Properties["foo"]; !ok || v != "bar" {
|
|
|
|
t.Errorf("foo != bar, but '%v'", v)
|
2013-12-02 17:13:31 +04:00
|
|
|
}
|
2014-05-21 11:15:32 +04:00
|
|
|
if v, ok := features[0].Properties["baz"]; !ok || v != "42" {
|
|
|
|
t.Errorf("baz != 42, but '%v'", v)
|
|
|
|
}
|
|
|
|
|
2015-03-02 11:22:08 +03:00
|
|
|
if len(features[0].Polygon[0]) != 5 {
|
2014-05-21 11:15:32 +04:00
|
|
|
t.Fatal(features)
|
2013-12-02 17:13:31 +04:00
|
|
|
}
|
2015-03-02 11:22:08 +03:00
|
|
|
if len(features[1].Polygon[0]) != 5 {
|
|
|
|
t.Fatal(features)
|
2013-10-21 17:33:47 +04:00
|
|
|
}
|
|
|
|
}
|