Skip to content

Commit

Permalink
Adding Value() ([]byte, error) method (#6)
Browse files Browse the repository at this point in the history
implementation to all the shapes.

Includes changes to the NewCircle API
parameters.
  • Loading branch information
sreekanth-cb authored and Philiphil committed Nov 18, 2022
1 parent a7a4295 commit 4f929f6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 13 deletions.
62 changes: 52 additions & 10 deletions geojson/geojson_shapes_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ func (p *Point) Type() string {
return strings.ToLower(p.Typ)
}

func (p *Point) Value() ([]byte, error) {
return jsoniter.Marshal(p)
}

func NewGeoJsonPoint(v []float64) index.GeoJSON {
rv := &Point{Typ: PointType, Vertices: v}
rv.init()
Expand Down Expand Up @@ -193,6 +197,10 @@ func (p *MultiPoint) Type() string {
return strings.ToLower(p.Typ)
}

func (mp *MultiPoint) Value() ([]byte, error) {
return jsoniter.Marshal(mp)
}

func (p *MultiPoint) Intersects(other index.GeoJSON) (bool, error) {
p.init()

Expand Down Expand Up @@ -268,6 +276,10 @@ func (ls *LineString) Type() string {
return strings.ToLower(ls.Typ)
}

func (ls *LineString) Value() ([]byte, error) {
return jsoniter.Marshal(ls)
}

func (ls *LineString) Marshal() ([]byte, error) {
ls.init()

Expand Down Expand Up @@ -323,6 +335,10 @@ func (mls *MultiLineString) Type() string {
return strings.ToLower(mls.Typ)
}

func (mls *MultiLineString) Value() ([]byte, error) {
return jsoniter.Marshal(mls)
}

func (mls *MultiLineString) Marshal() ([]byte, error) {
mls.init()

Expand Down Expand Up @@ -402,6 +418,10 @@ func (p *Polygon) Type() string {
return strings.ToLower(p.Typ)
}

func (p *Polygon) Value() ([]byte, error) {
return jsoniter.Marshal(p)
}

func (p *Polygon) Marshal() ([]byte, error) {
p.init()

Expand Down Expand Up @@ -465,6 +485,10 @@ func (p *MultiPolygon) Type() string {
return strings.ToLower(p.Typ)
}

func (p *MultiPolygon) Value() ([]byte, error) {
return jsoniter.Marshal(p)
}

func (p *MultiPolygon) Marshal() ([]byte, error) {
p.init()

Expand Down Expand Up @@ -542,6 +566,10 @@ func (gc *GeometryCollection) Type() string {
return strings.ToLower(gc.Typ)
}

func (gc *GeometryCollection) Value() ([]byte, error) {
return jsoniter.Marshal(gc)
}

func (gc *GeometryCollection) Members() []index.GeoJSON {
shapes := make([]index.GeoJSON, 0, len(gc.Shapes))
for _, shape := range gc.Shapes {
Expand Down Expand Up @@ -721,24 +749,35 @@ func (gc *GeometryCollection) UnmarshalJSON(data []byte) error {
type Circle struct {
Typ string `json:"type"`
Vertices []float64 `json:"coordinates"`
RadiusInMeters float64 `json:"radiusInMeters"`
Radius string `json:"radius"`
radiusInMeters float64
s2cap *s2.Cap
}

func NewGeoCircle(points []float64,
radiusInMeter float64) index.GeoJSON {
radius string) index.GeoJSON {
r, err := ParseDistance(radius)
if err != nil {
return nil
}

return &Circle{Typ: CircleType,
Vertices: points,
RadiusInMeters: radiusInMeter}
Radius: radius,
radiusInMeters: r}
}

func (c *Circle) Type() string {
return strings.ToLower(c.Typ)
}

func (c *Circle) Value() ([]byte, error) {
return jsoniter.Marshal(c)
}

func (c *Circle) init() {
if c.s2cap == nil {
c.s2cap = s2Cap(c.Vertices, c.RadiusInMeters)
c.s2cap = s2Cap(c.Vertices, c.radiusInMeters)
}
}

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

func (c *Circle) UnmarshalJSON(data []byte) error {
tmp := struct {
Typ string `json:"type"`
Vertices []float64 `json:"coordinates"`
Radius string `json:"radius"`
RadiusInMeters float64 `json:"radiusInMeters"`
Typ string `json:"type"`
Vertices []float64 `json:"coordinates"`
Radius string `json:"radius"`
}{}

err := jsoniter.Unmarshal(data, &tmp)
Expand All @@ -782,9 +820,9 @@ func (c *Circle) UnmarshalJSON(data []byte) error {
}
c.Typ = tmp.Typ
c.Vertices = tmp.Vertices
c.RadiusInMeters = tmp.RadiusInMeters
c.Radius = tmp.Radius
if tmp.Radius != "" {
c.RadiusInMeters, err = ParseDistance(tmp.Radius)
c.radiusInMeters, err = ParseDistance(tmp.Radius)
}

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

func (e *Envelope) Value() ([]byte, error) {
return jsoniter.Marshal(e)
}

func (e *Envelope) init() {
if e.r == nil {
e.r = s2RectFromBounds(e.Vertices[0], e.Vertices[1])
Expand Down
10 changes: 8 additions & 2 deletions geojson/geojson_shapes_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,15 @@ func NewGeometryCollection(coordinates [][][][][]float64,
// prefix the byte contents with certain glue bytes that
// can be used later while filering the doc values.
func NewGeoCircleShape(cp []float64,
radiusInMeter float64) (*Circle, []byte, error) {
radius string) (*Circle, []byte, error) {
r, err := ParseDistance(radius)
if err != nil {
return nil, nil, err
}
rv := &Circle{Typ: CircleType, Vertices: cp,
RadiusInMeters: radiusInMeter}
Radius: radius,
radiusInMeters: r}

vbytes, err := rv.Marshal()
if err != nil {
return nil, nil, err
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/blevesearch/geo
go 1.12

require (
github.com/blevesearch/bleve_index_api v1.0.1
github.com/blevesearch/bleve_index_api v1.0.3-0.20220722100506-ab4b3dba2301
github.com/golang/geo v0.0.0-20210211234256-740aa86cb551
github.com/google/gofuzz v1.2.0 // indirect
github.com/json-iterator/go v0.0.0-20171115153421-f7279a603ede
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/blevesearch/bleve_index_api v1.0.1 h1:nx9++0hnyiGOHJwQQYfsUGzpRdEVE5LsylmmngQvaFk=
github.com/blevesearch/bleve_index_api v1.0.1/go.mod h1:fiwKS0xLEm+gBRgv5mumf0dhgFr2mDgZah1pqv1c1M4=
github.com/blevesearch/bleve_index_api v1.0.3-0.20220722100506-ab4b3dba2301 h1:+INeF+UvJos1kgJ7Trm5cjTFIs6oQ+c432napXzGSr0=
github.com/blevesearch/bleve_index_api v1.0.3-0.20220722100506-ab4b3dba2301/go.mod h1:fiwKS0xLEm+gBRgv5mumf0dhgFr2mDgZah1pqv1c1M4=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/geo v0.0.0-20210211234256-740aa86cb551 h1:gtexQ/VGyN+VVFRXSFiguSNcXmS6rkKT+X7FdIrTtfo=
Expand Down

0 comments on commit 4f929f6

Please sign in to comment.