Skip to content

Commit 4f929f6

Browse files
Sreekanth SivasankaranPhiliphil
authored andcommitted
Adding Value() ([]byte, error) method (#6)
implementation to all the shapes. Includes changes to the NewCircle API parameters.
1 parent a7a4295 commit 4f929f6

File tree

4 files changed

+63
-13
lines changed

4 files changed

+63
-13
lines changed

geojson/geojson_shapes_impl.go

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ func (p *Point) Type() string {
9090
return strings.ToLower(p.Typ)
9191
}
9292

93+
func (p *Point) Value() ([]byte, error) {
94+
return jsoniter.Marshal(p)
95+
}
96+
9397
func NewGeoJsonPoint(v []float64) index.GeoJSON {
9498
rv := &Point{Typ: PointType, Vertices: v}
9599
rv.init()
@@ -193,6 +197,10 @@ func (p *MultiPoint) Type() string {
193197
return strings.ToLower(p.Typ)
194198
}
195199

200+
func (mp *MultiPoint) Value() ([]byte, error) {
201+
return jsoniter.Marshal(mp)
202+
}
203+
196204
func (p *MultiPoint) Intersects(other index.GeoJSON) (bool, error) {
197205
p.init()
198206

@@ -268,6 +276,10 @@ func (ls *LineString) Type() string {
268276
return strings.ToLower(ls.Typ)
269277
}
270278

279+
func (ls *LineString) Value() ([]byte, error) {
280+
return jsoniter.Marshal(ls)
281+
}
282+
271283
func (ls *LineString) Marshal() ([]byte, error) {
272284
ls.init()
273285

@@ -323,6 +335,10 @@ func (mls *MultiLineString) Type() string {
323335
return strings.ToLower(mls.Typ)
324336
}
325337

338+
func (mls *MultiLineString) Value() ([]byte, error) {
339+
return jsoniter.Marshal(mls)
340+
}
341+
326342
func (mls *MultiLineString) Marshal() ([]byte, error) {
327343
mls.init()
328344

@@ -402,6 +418,10 @@ func (p *Polygon) Type() string {
402418
return strings.ToLower(p.Typ)
403419
}
404420

421+
func (p *Polygon) Value() ([]byte, error) {
422+
return jsoniter.Marshal(p)
423+
}
424+
405425
func (p *Polygon) Marshal() ([]byte, error) {
406426
p.init()
407427

@@ -465,6 +485,10 @@ func (p *MultiPolygon) Type() string {
465485
return strings.ToLower(p.Typ)
466486
}
467487

488+
func (p *MultiPolygon) Value() ([]byte, error) {
489+
return jsoniter.Marshal(p)
490+
}
491+
468492
func (p *MultiPolygon) Marshal() ([]byte, error) {
469493
p.init()
470494

@@ -542,6 +566,10 @@ func (gc *GeometryCollection) Type() string {
542566
return strings.ToLower(gc.Typ)
543567
}
544568

569+
func (gc *GeometryCollection) Value() ([]byte, error) {
570+
return jsoniter.Marshal(gc)
571+
}
572+
545573
func (gc *GeometryCollection) Members() []index.GeoJSON {
546574
shapes := make([]index.GeoJSON, 0, len(gc.Shapes))
547575
for _, shape := range gc.Shapes {
@@ -721,24 +749,35 @@ func (gc *GeometryCollection) UnmarshalJSON(data []byte) error {
721749
type Circle struct {
722750
Typ string `json:"type"`
723751
Vertices []float64 `json:"coordinates"`
724-
RadiusInMeters float64 `json:"radiusInMeters"`
752+
Radius string `json:"radius"`
753+
radiusInMeters float64
725754
s2cap *s2.Cap
726755
}
727756

728757
func NewGeoCircle(points []float64,
729-
radiusInMeter float64) index.GeoJSON {
758+
radius string) index.GeoJSON {
759+
r, err := ParseDistance(radius)
760+
if err != nil {
761+
return nil
762+
}
763+
730764
return &Circle{Typ: CircleType,
731765
Vertices: points,
732-
RadiusInMeters: radiusInMeter}
766+
Radius: radius,
767+
radiusInMeters: r}
733768
}
734769

735770
func (c *Circle) Type() string {
736771
return strings.ToLower(c.Typ)
737772
}
738773

774+
func (c *Circle) Value() ([]byte, error) {
775+
return jsoniter.Marshal(c)
776+
}
777+
739778
func (c *Circle) init() {
740779
if c.s2cap == nil {
741-
c.s2cap = s2Cap(c.Vertices, c.RadiusInMeters)
780+
c.s2cap = s2Cap(c.Vertices, c.radiusInMeters)
742781
}
743782
}
744783

@@ -770,10 +809,9 @@ func (c *Circle) Contains(other index.GeoJSON) (bool, error) {
770809

771810
func (c *Circle) UnmarshalJSON(data []byte) error {
772811
tmp := struct {
773-
Typ string `json:"type"`
774-
Vertices []float64 `json:"coordinates"`
775-
Radius string `json:"radius"`
776-
RadiusInMeters float64 `json:"radiusInMeters"`
812+
Typ string `json:"type"`
813+
Vertices []float64 `json:"coordinates"`
814+
Radius string `json:"radius"`
777815
}{}
778816

779817
err := jsoniter.Unmarshal(data, &tmp)
@@ -782,9 +820,9 @@ func (c *Circle) UnmarshalJSON(data []byte) error {
782820
}
783821
c.Typ = tmp.Typ
784822
c.Vertices = tmp.Vertices
785-
c.RadiusInMeters = tmp.RadiusInMeters
823+
c.Radius = tmp.Radius
786824
if tmp.Radius != "" {
787-
c.RadiusInMeters, err = ParseDistance(tmp.Radius)
825+
c.radiusInMeters, err = ParseDistance(tmp.Radius)
788826
}
789827

790828
return err
@@ -807,6 +845,10 @@ func (e *Envelope) Type() string {
807845
return strings.ToLower(e.Typ)
808846
}
809847

848+
func (e *Envelope) Value() ([]byte, error) {
849+
return jsoniter.Marshal(e)
850+
}
851+
810852
func (e *Envelope) init() {
811853
if e.r == nil {
812854
e.r = s2RectFromBounds(e.Vertices[0], e.Vertices[1])

geojson/geojson_shapes_util.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,15 @@ func NewGeometryCollection(coordinates [][][][][]float64,
415415
// prefix the byte contents with certain glue bytes that
416416
// can be used later while filering the doc values.
417417
func NewGeoCircleShape(cp []float64,
418-
radiusInMeter float64) (*Circle, []byte, error) {
418+
radius string) (*Circle, []byte, error) {
419+
r, err := ParseDistance(radius)
420+
if err != nil {
421+
return nil, nil, err
422+
}
419423
rv := &Circle{Typ: CircleType, Vertices: cp,
420-
RadiusInMeters: radiusInMeter}
424+
Radius: radius,
425+
radiusInMeters: r}
426+
421427
vbytes, err := rv.Marshal()
422428
if err != nil {
423429
return nil, nil, err

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/blevesearch/geo
33
go 1.12
44

55
require (
6-
github.com/blevesearch/bleve_index_api v1.0.1
6+
github.com/blevesearch/bleve_index_api v1.0.3-0.20220722100506-ab4b3dba2301
77
github.com/golang/geo v0.0.0-20210211234256-740aa86cb551
88
github.com/google/gofuzz v1.2.0 // indirect
99
github.com/json-iterator/go v0.0.0-20171115153421-f7279a603ede

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/blevesearch/bleve_index_api v1.0.1 h1:nx9++0hnyiGOHJwQQYfsUGzpRdEVE5LsylmmngQvaFk=
22
github.com/blevesearch/bleve_index_api v1.0.1/go.mod h1:fiwKS0xLEm+gBRgv5mumf0dhgFr2mDgZah1pqv1c1M4=
3+
github.com/blevesearch/bleve_index_api v1.0.3-0.20220722100506-ab4b3dba2301 h1:+INeF+UvJos1kgJ7Trm5cjTFIs6oQ+c432napXzGSr0=
4+
github.com/blevesearch/bleve_index_api v1.0.3-0.20220722100506-ab4b3dba2301/go.mod h1:fiwKS0xLEm+gBRgv5mumf0dhgFr2mDgZah1pqv1c1M4=
35
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
46
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
57
github.com/golang/geo v0.0.0-20210211234256-740aa86cb551 h1:gtexQ/VGyN+VVFRXSFiguSNcXmS6rkKT+X7FdIrTtfo=

0 commit comments

Comments
 (0)