imposm3/geom/geojson/geojson_test.go

154 lines
3.4 KiB
Go
Raw Permalink Normal View History

2013-10-21 17:33:47 +04:00
package geojson
import (
"bytes"
"testing"
)
func TestParsePolygon(t *testing.T) {
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
}
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
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
}
if len(features[0].Polygon[0]) != 5 {
t.Fatal(features)
2013-11-26 12:47:00 +04: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
}
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":
[[[[8, 50], [11, 50], [11, 53], [8, 50]]],
[[[8, 50], [11, 50], [11, 53], [8, 50]]]]
2013-10-21 17:33:47 +04: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": {
"type": "Polygon", "coordinates": [[[8, 50], [11, 50], [11, 53], [8, 53], [8, 50]]]
2013-10-21 17:33:47 +04: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
}
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":
{"type": "Polygon", "coordinates": [[[8, 50], [11, 50], [11, 53], [8, 53], [8, 50]]]}
2013-10-21 17:33:47 +04:00
},
{"type": "Feature", "geometry":
{"type": "Polygon", "coordinates": [[[8, 50], [11, 50], [11, 53], [8, 53], [8, 50]]]}
2013-10-21 17:33:47 +04: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
}
if len(features[0].Polygon[0]) != 5 {
t.Fatal(features)
}
if len(features[1].Polygon[0]) != 5 {
t.Fatal(features)
}
}
func TestParseProperties(t *testing.T) {
r := bytes.NewBufferString(`{"type": "FeatureCollection", "features": [
2014-05-21 11:15:32 +04:00
{"type": "Feature", "properties": {"foo": "bar", "baz": 42}, "geometry":
{"type": "Polygon", "coordinates": [[[8, 50], [11, 50], [11, 53], [8, 53], [8, 50]]]}
},
{"type": "Feature", "geometry":
{"type": "Polygon", "coordinates": [[[8, 50], [11, 50], [11, 53], [8, 53], [8, 50]]]}
}
]}`)
features, err := ParseGeoJSON(r)
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)
}
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)
}
if len(features[0].Polygon[0]) != 5 {
2014-05-21 11:15:32 +04:00
t.Fatal(features)
}
if len(features[1].Polygon[0]) != 5 {
t.Fatal(features)
2013-10-21 17:33:47 +04:00
}
}