Require properties member for geojson features

This commit is contained in:
tidwall 2019-02-12 06:49:13 -07:00
parent fb7259b10b
commit 30f903bd51
21 changed files with 151 additions and 35 deletions

6
Gopkg.lock generated
View File

@ -242,8 +242,7 @@
version = "v1.0.2"
[[projects]]
branch = "master"
digest = "1:0a6da4a08925415ab3326e140985ec628417903637461e9b68c07a03dfcc918d"
digest = "1:fc81262a6ad5aeec27e1bd15356f790e6b2d8fd14acb6bd5ff3f0f51bf67417f"
name = "github.com/tidwall/geojson"
packages = [
".",
@ -251,7 +250,8 @@
"geometry",
]
pruneopts = ""
revision = "d0a98d02b48e887b4de454d80c61ef5fb9312482"
revision = "6baab67ab6a9bac4abf153ab779c736254a37fd1"
version = "v1.1.0"
[[projects]]
digest = "1:3ddca2bd5496c6922a2a9e636530e178a43c2a534ea6634211acdc7d10222794"

View File

@ -23,7 +23,6 @@
required = [
"github.com/tidwall/lotsa",
"github.com/mmcloughlin/geohash",
"github.com/tidwall/geojson",
"github.com/tidwall/evio"
]
@ -35,6 +34,10 @@ required = [
branch = "master"
name = "github.com/tidwall/boxtree"
[[constraint]]
name = "github.com/tidwall/geojson"
version = "1.1.0"
[[constraint]]
name = "github.com/Shopify/sarama"
version = "1.13.0"

View File

@ -58,6 +58,11 @@ func (s String) JSON() string {
return string(s.AppendJSON(nil))
}
// MarshalJSON ...
func (s String) MarshalJSON() ([]byte, error) {
return s.AppendJSON(nil), nil
}
// Within ...
func (s String) Within(obj geojson.Object) bool {
return false

View File

@ -30,7 +30,7 @@ func json_JSET_geojson_test(mc *mockServer) error {
{"JSET", "mykey", "myid1", "coordinates.1", 44}, {"OK"},
{"JGET", "mykey", "myid1"}, {`{"type":"Point","coordinates":[-115,44]}`},
{"SET", "mykey", "myid1", "OBJECT", `{"type":"Feature","geometry":{"type":"Point","coordinates":[-115,44]}}`}, {"OK"},
{"JGET", "mykey", "myid1"}, {`{"type":"Feature","geometry":{"type":"Point","coordinates":[-115,44]}}`},
{"JGET", "mykey", "myid1"}, {`{"type":"Feature","geometry":{"type":"Point","coordinates":[-115,44]},"properties":{}}`},
{"JGET", "mykey", "myid1", "geometry.type"}, {"Point"},
{"JSET", "mykey", "myid1", "properties.tags.-1", "southwest"}, {"OK"},
{"JSET", "mykey", "myid1", "properties.tags.-1", "united states"}, {"OK"},

View File

@ -111,7 +111,7 @@ func testcmd_INTERSECTS_CLIP_test(mc *mockServer) error {
{"TEST", "OBJECT", poly9, "INTERSECTS", "CLIP", "BOUNDS", 37.732906137107, -122.44126439094543, 37.73421283683962, -122.43980526924135}, {"[1 " + poly9 + "]"},
{"TEST", "OBJECT", poly8, "INTERSECTS", "CLIP", "BOUNDS", 37.733, -122.4408378, 37.7341129, -122.44}, {"[1 " + poly8 + "]"},
{"TEST", "OBJECT", multipoly5, "INTERSECTS", "CLIP", "BOUNDS", 37.73227823422744, -122.44120001792908, 37.73319038868677, -122.43955314159392}, {"[1 " + `{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[-122.4408378,37.73319038868677],[-122.4408378,37.733],[-122.44,37.733],[-122.44,37.73319038868677],[-122.4408378,37.73319038868677]]]}},{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[-122.44091033935547,37.73227823422744],[-122.43994474411011,37.73227823422744],[-122.43994474411011,37.73254976045042],[-122.44091033935547,37.73254976045042],[-122.44091033935547,37.73227823422744]]]}}]}` + "]"},
{"TEST", "OBJECT", multipoly5, "INTERSECTS", "CLIP", "BOUNDS", 37.73227823422744, -122.44120001792908, 37.73319038868677, -122.43955314159392}, {"[1 " + `{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[-122.4408378,37.73319038868677],[-122.4408378,37.733],[-122.44,37.733],[-122.44,37.73319038868677],[-122.4408378,37.73319038868677]]]},"properties":{}},{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[-122.44091033935547,37.73227823422744],[-122.43994474411011,37.73227823422744],[-122.43994474411011,37.73254976045042],[-122.44091033935547,37.73254976045042],[-122.44091033935547,37.73227823422744]]]},"properties":{}}]}` + "]"},
{"TEST", "OBJECT", poly101, "INTERSECTS", "CLIP", "BOUNDS", 37.73315644825698, -122.44054287672043, 37.73349585185455, -122.44008690118788}, {"0"},
})
}

View File

@ -57,7 +57,9 @@ func runSubTest(t *testing.T, name string, mc *mockServer, test func(t *testing.
}
func runStep(t *testing.T, mc *mockServer, name string, step func(mc *mockServer) error) {
t.Helper()
t.Run(name, func(t *testing.T) {
t.Helper()
if err := func() error {
// reset the current server
mc.ResetConn()

View File

@ -53,6 +53,11 @@ func (g *Circle) JSON() string {
return string(g.AppendJSON(nil))
}
// MarshalJSON ...
func (g *Circle) MarshalJSON() ([]byte, error) {
return g.AppendJSON(nil), nil
}
// String ...
func (g *Circle) String() string {
return string(g.AppendJSON(nil))

View File

@ -90,6 +90,11 @@ func (g *collection) JSON() string {
return string(g.AppendJSON(nil))
}
// MarshalJSON ...
func (g *collection) MarshalJSON() ([]byte, error) {
return g.AppendJSON(nil), nil
}
// String ...
func (g *collection) String() string {
return string(g.AppendJSON(nil))

View File

@ -77,7 +77,7 @@ func (g *Feature) Members() string {
func (g *Feature) AppendJSON(dst []byte) []byte {
dst = append(dst, `{"type":"Feature","geometry":`...)
dst = g.base.AppendJSON(dst)
dst = g.extra.appendJSONExtra(dst)
dst = g.extra.appendJSONExtra(dst, true)
dst = append(dst, '}')
return dst
@ -93,6 +93,11 @@ func (g *Feature) JSON() string {
return string(g.AppendJSON(nil))
}
// MarshalJSON ...
func (g *Feature) MarshalJSON() ([]byte, error) {
return g.AppendJSON(nil), nil
}
// Spatial ...
func (g *Feature) Spatial() Spatial {
return g

View File

@ -1,28 +1,32 @@
package geojson
import "testing"
import (
"testing"
"github.com/tidwall/gjson"
)
func TestFeatureParse(t *testing.T) {
p := expectJSON(t, `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2,3]}}`, nil)
p := expectJSON(t, `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2,3]},"properties":{}}`, nil)
expect(t, p.Center() == P(1, 2))
expectJSON(t, `{"type":"Feature"}`, errGeometryMissing)
expectJSON(t, `{"type":"Feature","geometry":null}`, errDataInvalid)
expectJSON(t, `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2,3]},"bbox":null}`, nil)
expectJSON(t, `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2]},"id":[4,true]}`, nil)
expectJSON(t, `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2,3]},"bbox":null,"properties":{}}`, nil)
expectJSON(t, `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2]},"id":[4,true],"properties":{}}`, nil)
expectJSON(t, `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2]},"id":"15","properties":{"a":"b"}}`, nil)
expectJSON(t, `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2,3]},"bbox":[1,2,3,4]}`, nil)
expectJSON(t, `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2],"bbox":[1,2,3,4]},"id":[4,true]}`, nil)
expectJSON(t, `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2,3]},"bbox":[1,2,3,4],"properties":{}}`, nil)
expectJSON(t, `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2],"bbox":[1,2,3,4]},"id":[4,true],"properties":{}}`, nil)
}
func TestFeatureVarious(t *testing.T) {
var g = expectJSON(t, `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2,3]}}`, nil)
expect(t, string(g.AppendJSON(nil)) == `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2,3]}}`)
var g = expectJSON(t, `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2,3]},"properties":{}}`, nil)
expect(t, string(g.AppendJSON(nil)) == `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2,3]},"properties":{}}`)
expect(t, g.Rect() == R(1, 2, 1, 2))
expect(t, g.Center() == P(1, 2))
expect(t, !g.Empty())
g = expectJSONOpts(t,
`{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2,3]},"bbox":[1,2,3,4]}`,
`{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2,3]},"bbox":[1,2,3,4],"properties":{}}`,
nil, nil)
expect(t, !g.Empty())
expect(t, g.Rect() == R(1, 2, 1, 2))
@ -30,10 +34,44 @@ func TestFeatureVarious(t *testing.T) {
}
// func TestFeaturePoly(t *testing.T) {
// p := expectJSON(t, `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2]}}`, nil)
// expect(t, p.Intersects(PO(1, 2)))
// expect(t, p.Contains(PO(1, 2)))
// expect(t, p.Within(PO(1, 2)))
func TestFeatureProperties(t *testing.T) {
obj, err := Parse(`{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2]}}`, nil)
if err != nil {
t.Fatal(err)
}
json := obj.JSON()
if !gjson.Valid(json) {
t.Fatal("invalid json")
}
if !gjson.Get(json, "properties").Exists() {
t.Fatal("expected 'properties' member")
}
// }
obj, err = Parse(`{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2]},"properties":true}`, nil)
if err != nil {
t.Fatal(err)
}
json = obj.JSON()
if !gjson.Valid(json) {
t.Fatal("invalid json")
}
if gjson.Get(json, "properties").Type != gjson.True {
t.Fatal("expected 'properties' member to be 'true'")
}
obj, err = Parse(`{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2]},"id":{}}`, nil)
if err != nil {
t.Fatal(err)
}
json = obj.JSON()
if !gjson.Valid(json) {
t.Fatal("invalid json")
}
if !gjson.Get(json, "properties").Exists() {
t.Fatal("expected 'properties' member")
}
if gjson.Get(json, "id").String() != "{}" {
t.Fatal("expected 'id' member")
}
}

View File

@ -28,7 +28,7 @@ func (g *FeatureCollection) AppendJSON(dst []byte) []byte {
}
dst = append(dst, ']')
if g.extra != nil {
dst = g.extra.appendJSONExtra(dst)
dst = g.extra.appendJSONExtra(dst, false)
}
dst = append(dst, '}')
strings.Index("", " ")
@ -45,6 +45,11 @@ func (g *FeatureCollection) JSON() string {
return string(g.AppendJSON(nil))
}
// MarshalJSON ...
func (g *FeatureCollection) MarshalJSON() ([]byte, error) {
return g.AppendJSON(nil), nil
}
func parseJSONFeatureCollection(
keys *parseKeys, opts *ParseOptions,
) (Object, error) {

View File

@ -28,7 +28,7 @@ func (g *GeometryCollection) AppendJSON(dst []byte) []byte {
}
dst = append(dst, ']')
if g.extra != nil {
dst = g.extra.appendJSONExtra(dst)
dst = g.extra.appendJSONExtra(dst, false)
}
dst = append(dst, '}')
strings.Index("", " ")
@ -45,6 +45,11 @@ func (g *GeometryCollection) JSON() string {
return string(g.AppendJSON(nil))
}
// MarshalJSON ...
func (g *GeometryCollection) MarshalJSON() ([]byte, error) {
return g.AppendJSON(nil), nil
}
func parseJSONGeometryCollection(
keys *parseKeys, opts *ParseOptions,
) (Object, error) {

View File

@ -46,7 +46,7 @@ func (g *LineString) AppendJSON(dst []byte) []byte {
dst = append(dst, `{"type":"LineString","coordinates":`...)
dst, _ = appendJSONSeries(dst, &g.base, g.extra, 0)
if g.extra != nil {
dst = g.extra.appendJSONExtra(dst)
dst = g.extra.appendJSONExtra(dst, false)
}
dst = append(dst, '}')
return dst
@ -62,6 +62,11 @@ func (g *LineString) JSON() string {
return string(g.AppendJSON(nil))
}
// MarshalJSON ...
func (g *LineString) MarshalJSON() ([]byte, error) {
return g.AppendJSON(nil), nil
}
// Spatial ...
func (g *LineString) Spatial() Spatial {
return g

View File

@ -30,7 +30,7 @@ func (g *MultiLineString) AppendJSON(dst []byte) []byte {
}
dst = append(dst, ']')
if g.extra != nil {
dst = g.extra.appendJSONExtra(dst)
dst = g.extra.appendJSONExtra(dst, false)
}
dst = append(dst, '}')
return dst
@ -58,6 +58,11 @@ func (g *MultiLineString) JSON() string {
return string(g.AppendJSON(nil))
}
// MarshalJSON ...
func (g *MultiLineString) MarshalJSON() ([]byte, error) {
return g.AppendJSON(nil), nil
}
func parseJSONMultiLineString(
keys *parseKeys, opts *ParseOptions,
) (Object, error) {

View File

@ -30,7 +30,7 @@ func (g *MultiPoint) AppendJSON(dst []byte) []byte {
}
dst = append(dst, ']')
if g.extra != nil {
dst = g.extra.appendJSONExtra(dst)
dst = g.extra.appendJSONExtra(dst, false)
}
dst = append(dst, '}')
return dst
@ -46,6 +46,11 @@ func (g *MultiPoint) JSON() string {
return string(g.AppendJSON(nil))
}
// MarshalJSON ...
func (g *MultiPoint) MarshalJSON() ([]byte, error) {
return g.AppendJSON(nil), nil
}
func parseJSONMultiPoint(keys *parseKeys, opts *ParseOptions) (Object, error) {
var g MultiPoint
var err error

View File

@ -30,7 +30,7 @@ func (g *MultiPolygon) AppendJSON(dst []byte) []byte {
}
dst = append(dst, ']')
if g.extra != nil {
dst = g.extra.appendJSONExtra(dst)
dst = g.extra.appendJSONExtra(dst, false)
}
dst = append(dst, '}')
return dst
@ -57,6 +57,11 @@ func (g *MultiPolygon) JSON() string {
return string(g.AppendJSON(nil))
}
// MarshalJSON ...
func (g *MultiPolygon) MarshalJSON() ([]byte, error) {
return g.AppendJSON(nil), nil
}
func parseJSONMultiPolygon(
keys *parseKeys, opts *ParseOptions,
) (Object, error) {

View File

@ -43,6 +43,7 @@ type Object interface {
NumPoints() int
ForEach(iter func(geom Object) bool) bool
Spatial() Spatial
MarshalJSON() ([]byte, error)
}
var _ = []Object{
@ -244,10 +245,17 @@ func appendJSONPoint(dst []byte, point geometry.Point, ex *extra, idx int) []byt
return dst
}
func (ex *extra) appendJSONExtra(dst []byte) []byte {
func (ex *extra) appendJSONExtra(dst []byte, propertiesRequired bool) []byte {
if ex != nil && ex.members != "" {
dst = append(dst, ',')
dst = append(dst, ex.members[1:len(ex.members)-1]...)
if propertiesRequired {
if !gjson.Get(ex.members, "properties").Exists() {
dst = append(dst, `,"properties":{}`...)
}
}
} else if propertiesRequired {
dst = append(dst, `,"properties":{}`...)
}
return dst

View File

@ -101,9 +101,9 @@ func expect(t testing.TB, what bool) {
}
if !what {
if t == nil {
panic("exception failure")
panic("expectation failure")
} else {
t.Fatal("expection failure")
t.Fatal("expectation failure")
}
}
}

View File

@ -63,7 +63,7 @@ func (g *Point) Base() geometry.Point {
func (g *Point) AppendJSON(dst []byte) []byte {
dst = append(dst, `{"type":"Point","coordinates":`...)
dst = appendJSONPoint(dst, g.base, g.extra, 0)
dst = g.extra.appendJSONExtra(dst)
dst = g.extra.appendJSONExtra(dst, false)
dst = append(dst, '}')
return dst
}
@ -73,6 +73,11 @@ func (g *Point) JSON() string {
return string(g.AppendJSON(nil))
}
// MarshalJSON ...
func (g *Point) MarshalJSON() ([]byte, error) {
return g.AppendJSON(nil), nil
}
// String ...
func (g *Point) String() string {
return string(g.AppendJSON(nil))

View File

@ -52,7 +52,7 @@ func (g *Polygon) AppendJSON(dst []byte) []byte {
}
dst = append(dst, ']')
if g.extra != nil {
dst = g.extra.appendJSONExtra(dst)
dst = g.extra.appendJSONExtra(dst, false)
}
dst = append(dst, '}')
return dst
@ -63,6 +63,11 @@ func (g *Polygon) JSON() string {
return string(g.AppendJSON(nil))
}
// MarshalJSON ...
func (g *Polygon) MarshalJSON() ([]byte, error) {
return g.AppendJSON(nil), nil
}
// String ...
func (g *Polygon) String() string {
return string(g.AppendJSON(nil))

View File

@ -56,6 +56,11 @@ func (g *Rect) JSON() string {
return string(g.AppendJSON(nil))
}
// MarshalJSON ...
func (g *Rect) MarshalJSON() ([]byte, error) {
return g.AppendJSON(nil), nil
}
// String ...
func (g *Rect) String() string {
return string(g.AppendJSON(nil))