Documentation
¶
Overview ¶
Package spot models GeoJSON geometry types (Point, LineString, Polygon, and their multi/collection variants) plus the additional shape types that the Elasticsearch geo-shape spatial strategy accepts: Envelope and Circle. Shapes unmarshal GeoJSON with type-aware dispatch and marshal back to the canonical form.
Type names are lowercase (e.g. "point", "polygon") as required by Elasticsearch. This differs from RFC 7946, which uses PascalCase, but Elasticsearch accepts only lowercase for geo-shape queries.
Example (BuildPointShape) ¶
package main
import (
"encoding/json"
"fmt"
"github.com/heltonmarx/spot"
)
func main() {
shape := spot.NewShape(spot.WithPoint([]float64{13.4, 52.5}))
data, err := json.Marshal(shape)
if err != nil {
panic(err)
}
fmt.Println(string(data))
}
Output: {"type":"point","coordinates":[13.4,52.5]}
Example (BuildPolygonShape) ¶
package main
import (
"encoding/json"
"fmt"
"github.com/heltonmarx/spot"
)
func main() {
shape := spot.NewShape(spot.WithPolygon([][][]float64{
{
{13.4, 52.5},
{14.4, 52.5},
{14.4, 53.5},
{13.4, 52.5},
},
}))
data, err := json.Marshal(shape)
if err != nil {
panic(err)
}
fmt.Println(string(data))
}
Output: {"type":"polygon","coordinates":[[[13.4,52.5],[14.4,52.5],[14.4,53.5],[13.4,52.5]]]}
Example (GeoShapeQueryDSL) ¶
package main
import (
"encoding/json"
"fmt"
"github.com/heltonmarx/spot"
)
func main() {
shape := spot.NewShape(spot.WithPoint([]float64{13.4, 52.5}))
shapeBytes, err := json.Marshal(shape)
if err != nil {
panic(err)
}
// shapeBytes is a json.RawMessage-compatible value, ready for
// types.GeoShapeFieldQuery{Shape: shapeBytes} from go-elasticsearch/v8.
fmt.Println(string(shapeBytes))
}
Output: {"type":"point","coordinates":[13.4,52.5]}
Index ¶
- Constants
- type Circle
- type Envelope
- type Geometry
- type GeometryCollection
- type LineString
- type MultiLineString
- type MultiPoint
- type MultiPolygon
- type Option
- func WithCircle(radius string, coordinates []float64) Option
- func WithEnvelope(coordinates [][]float64) Option
- func WithGeometryCollection(geometries []Geometry) Option
- func WithLineString(coordinates [][]float64) Option
- func WithMultiLineString(coordinates [][][]float64) Option
- func WithMultiPoint(coordinates [][]float64) Option
- func WithMultiPolygon(coordinates [][][][]float64) Option
- func WithPoint(coordinates []float64) Option
- func WithPolygon(coordinates [][][]float64) Option
- type Point
- type Polygon
- type RFC7946Geometry
- type Shape
- func (m *Shape) IsCircle() bool
- func (m *Shape) IsEnvelope() bool
- func (m *Shape) IsGeometryCollection() bool
- func (m *Shape) IsLineString() bool
- func (m *Shape) IsMultiLineString() bool
- func (m *Shape) IsMultiPoint() bool
- func (m *Shape) IsMultiPolygon() bool
- func (m *Shape) IsPoint() bool
- func (m *Shape) IsPolygon() bool
- func (m *Shape) MarshalJSON() ([]byte, error)
- func (m *Shape) UnmarshalJSON(data []byte) error
Examples ¶
Constants ¶
const ( TypePoint = "point" TypeMultiPoint = "multipoint" TypeLineString = "linestring" TypeMultiLineString = "multilinestring" TypePolygon = "polygon" TypeMultiPolygon = "multipolygon" TypeGeometryCollection = "geometrycollection" TypeEnvelope = "envelope" TypeCircle = "circle" )
The geometry types supported by Elasticsearch.
For more details, see: https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-shape.html#spatial-strategy
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Circle ¶
type Circle struct {
Type string `json:"type"`
Radius string `json:"radius"`
Coordinates []float64 `json:"coordinates"`
}
Circle is specified by a center point and a radius with units, defaulting to meters when no unit suffix is present.
func NewCircle ¶
NewCircle returns a Circle centered at the [lon, lat] coordinates with the given radius (e.g. "25m", "1km"). Panics if fewer than 2 coordinates are supplied or if radius is empty.
func (*Circle) UnmarshalJSON ¶
UnmarshalJSON decodes a circle geometry into m.
type Envelope ¶
Envelope represents a bounding rectangle by the coordinates of its upper left and lower right corners, each a [lon, lat] position.
func NewEnvelope ¶
NewEnvelope returns an Envelope from its two corner coordinates. Panics if exactly two corner positions are not provided.
func (*Envelope) UnmarshalJSON ¶
UnmarshalJSON decodes an envelope geometry into m.
type Geometry ¶
type Geometry interface {
GeoType() string
// contains filtered or unexported methods
}
Geometry is implemented by all concrete geometry types in this package. The unexported isGeometry method seals the interface so that only types defined here can satisfy it.
type GeometryCollection ¶
type GeometryCollection struct {
Type string `json:"type"`
Geometries []Geometry `json:"geometries"`
}
GeometryCollection is a collection of other geometry objects.
func NewGeometryCollection ¶
func NewGeometryCollection(geometries []Geometry) *GeometryCollection
NewGeometryCollection returns a GeometryCollection of the given geometries.
func (*GeometryCollection) GeoType ¶
func (m *GeometryCollection) GeoType() string
GeoType returns the geometry type constant for GeometryCollection.
func (*GeometryCollection) UnmarshalJSON ¶
func (m *GeometryCollection) UnmarshalJSON(data []byte) error
UnmarshalJSON decodes a GeoJSON geometry collection into m, decoding each member geometry according to its own "type".
type LineString ¶
LineString is an array of two or more positions.
func NewLineString ¶
func NewLineString(coordinates [][]float64) *LineString
NewLineString returns a LineString through the given [lon, lat] positions. Panics if fewer than 2 positions are supplied.
func (*LineString) GeoType ¶
func (m *LineString) GeoType() string
GeoType returns the geometry type constant for LineString.
func (*LineString) UnmarshalJSON ¶
func (m *LineString) UnmarshalJSON(data []byte) error
UnmarshalJSON decodes a GeoJSON linestring into m.
type MultiLineString ¶
type MultiLineString struct {
Type string `json:"type"`
Coordinates [][][]float64 `json:"coordinates"`
}
MultiLineString is an array of LineString coordinate arrays.
func NewMultiLineString ¶
func NewMultiLineString(coordinates [][][]float64) *MultiLineString
NewMultiLineString returns a MultiLineString, one [lon, lat] position array per line.
func (*MultiLineString) GeoType ¶
func (m *MultiLineString) GeoType() string
GeoType returns the geometry type constant for MultiLineString.
func (*MultiLineString) UnmarshalJSON ¶
func (m *MultiLineString) UnmarshalJSON(data []byte) error
UnmarshalJSON decodes a GeoJSON multilinestring into m.
type MultiPoint ¶
MultiPoint is an array of positions.
func NewMultiPoint ¶
func NewMultiPoint(coordinates [][]float64) *MultiPoint
NewMultiPoint returns a MultiPoint holding each [lon, lat] position.
func (*MultiPoint) GeoType ¶
func (m *MultiPoint) GeoType() string
GeoType returns the geometry type constant for MultiPoint.
func (*MultiPoint) UnmarshalJSON ¶
func (m *MultiPoint) UnmarshalJSON(data []byte) error
UnmarshalJSON decodes a GeoJSON multipoint into m.
type MultiPolygon ¶
type MultiPolygon struct {
Type string `json:"type"`
Coordinates [][][][]float64 `json:"coordinates"`
}
MultiPolygon represents a GeoJSON object of multiple Polygons.
func NewMultiPolygon ¶
func NewMultiPolygon(coordinates [][][][]float64) *MultiPolygon
NewMultiPolygon returns a MultiPolygon, one Polygon per top-level element.
func (*MultiPolygon) GeoType ¶
func (m *MultiPolygon) GeoType() string
GeoType returns the geometry type constant for MultiPolygon.
func (*MultiPolygon) UnmarshalJSON ¶
func (m *MultiPolygon) UnmarshalJSON(data []byte) error
UnmarshalJSON decodes a GeoJSON multipolygon into m.
type Option ¶
type Option func(*Shape)
Option configures the geometry delivered by NewShape. Options are applied in order; later options overwrite the Type and geometry field set by earlier ones.
func WithCircle ¶
WithCircle sets the Shape to a Circle with the given radius and center coordinates. radius defaults to meters unless it carries a unit suffix.
func WithEnvelope ¶
WithEnvelope sets the Shape to an Envelope from its two corner coordinates.
func WithGeometryCollection ¶
WithGeometryCollection sets the Shape to a GeometryCollection of the given decoded geometries.
func WithLineString ¶
WithLineString sets the Shape to a LineString made of [lon, lat] positions.
func WithMultiLineString ¶
WithMultiLineString sets the Shape to a MultiLineString, one [lon, lat] position array per line.
func WithMultiPoint ¶
WithMultiPoint sets the Shape to a MultiPoint holding each [lon, lat] position.
func WithMultiPolygon ¶
WithMultiPolygon sets the Shape to a MultiPolygon, one polygon per top-level element.
func WithPolygon ¶
WithPolygon sets the Shape to a Polygon. The outer slice is one ring per linear ring (first the exterior, then each hole); every ring is a closed sequence of [lon, lat] positions.
type Point ¶
Point is a single GeoJSON position.
func NewPoint ¶
NewPoint returns a Point positioned at the given [lon, lat] coordinates. Panics if fewer than 2 coordinates are supplied.
func (*Point) UnmarshalJSON ¶
UnmarshalJSON decodes a GeoJSON point into m.
type Polygon ¶
Polygon is an object consisting of one or more linear rings: the first ring is the exterior boundary and each subsequent ring is a hole. Rings are closed [lon, lat] position sequences (first and last position equal).
func NewPolygon ¶
NewPolygon returns a Polygon from its array of linear rings.
func (*Polygon) UnmarshalJSON ¶
UnmarshalJSON decodes a GeoJSON polygon into m.
type RFC7946Geometry ¶
type RFC7946Geometry struct {
// contains filtered or unexported fields
}
RFC7946Geometry wraps a Geometry and marshals it with PascalCase type names as required by RFC 7946. Use it when targeting GeoJSON consumers that follow the standard: MongoDB, PostGIS, Solr, and any RFC 7946-strict API.
Envelope and Circle are Elasticsearch extensions with no RFC 7946 equivalent; their type names are left unchanged.
func RFC7946 ¶
func RFC7946(g Geometry) RFC7946Geometry
RFC7946 wraps g so that MarshalJSON emits RFC 7946-compliant PascalCase type names instead of the Elasticsearch-compatible lowercase names.
func (RFC7946Geometry) MarshalJSON ¶
func (r RFC7946Geometry) MarshalJSON() ([]byte, error)
MarshalJSON marshals the wrapped geometry with PascalCase type names. GeometryCollection members are recursed through the RFC7946 wrapper via a type assertion, avoiding JSON field-name inspection.
type Shape ¶
type Shape struct {
Type string `json:"type"`
Point *Point `json:"-"`
MultiPoint *MultiPoint `json:"-"`
LineString *LineString `json:"-"`
MultiLineString *MultiLineString `json:"-"`
Polygon *Polygon `json:"-"`
MultiPolygon *MultiPolygon `json:"-"`
GeometryCollection *GeometryCollection `json:"-"`
Envelope *Envelope `json:"-"`
Circle *Circle `json:"-"`
}
Shape is a discriminated container that can hold any single geometry: its Type field names the geometry kind, and exactly one concrete geometry field (Point, LineString, Polygon, etc.) is set to carry the value. Use it when the geometry type is not known at compile time — e.g. when unmarshaling arbitrary GeoJSON.
func NewShape ¶
NewShape builds an empty generic Shape, then applies each option in order to set its Type and the corresponding geometry field. Common choices are the WithPoint, WithLineString, WithPolygon, ... helpers. NewShape returns a Shape whose Type is empty until at least one option is supplied.
func (*Shape) IsCircle ¶
IsCircle reports whether m holds a valid Circle, i.e. its type is TypeCircle and its Circle field is set.
func (*Shape) IsEnvelope ¶
IsEnvelope reports whether m holds a valid Envelope, i.e. its type is TypeEnvelope and its Envelope field is set.
func (*Shape) IsGeometryCollection ¶
IsGeometryCollection reports whether m holds a valid GeometryCollection, i.e. its type is TypeGeometryCollection and its GeometryCollection field is set.
func (*Shape) IsLineString ¶
IsLineString reports whether m holds a valid LineString, i.e. its type is TypeLineString and its LineString field is set.
func (*Shape) IsMultiLineString ¶
IsMultiLineString reports whether m holds a valid MultiLineString, i.e. its type is TypeMultiLineString and its MultiLineString field is set.
func (*Shape) IsMultiPoint ¶
IsMultiPoint reports whether m holds a valid MultiPoint, i.e. its type is TypeMultiPoint and its MultiPoint field is set.
func (*Shape) IsMultiPolygon ¶
IsMultiPolygon reports whether m holds a valid MultiPolygon, i.e. its type is TypeMultiPolygon and its MultiPolygon field is set.
func (*Shape) IsPoint ¶
IsPoint reports whether m holds a valid Point, i.e. its type is TypePoint and its Point field is set.
func (*Shape) IsPolygon ¶
IsPolygon reports whether m holds a valid Polygon, i.e. its type is TypePolygon and its Polygon field is set.
func (*Shape) MarshalJSON ¶
MarshalJSON serializes the contained geometry as a GeoJSON object, emitting the raw fields of whichever concrete geometry is set. Returns an error if no geometry type is matched by m.
func (*Shape) UnmarshalJSON ¶
UnmarshalJSON decodes a GeoJSON geometry into m. It reads the "type" field and decodes the remaining fields into the matching concrete geometry, storing the result in the corresponding Shape field. Returns an error if the "type" is not a recognized geometry kind.