Documentation ¶
Overview ¶
Package geojson is a library for encoding and decoding GeoJSON into Go structs. Supports both the json.Marshaler and json.Unmarshaler interfaces as well as helper functions such as `UnmarshalFeatureCollection`, `UnmarshalFeature` and `UnmarshalGeometry`.
Index ¶
- type Feature
- func NewCollectionFeature(geometries ...*Geometry) *Feature
- func NewFeature(geometry *Geometry) *Feature
- func NewLineStringFeature(coordinates [][]float64) *Feature
- func NewMultiLineStringFeature(lines ...[][]float64) *Feature
- func NewMultiPointFeature(coordinates ...[]float64) *Feature
- func NewMultiPolygonFeature(polygons ...[][][]float64) *Feature
- func NewPointFeature(coordinate []float64) *Feature
- func NewPolygonFeature(polygon [][][]float64) *Feature
- func UnmarshalFeature(data []byte) (*Feature, error)
- func (f Feature) MarshalJSON() ([]byte, error)
- func (f *Feature) PropertyBool(key string) (bool, error)
- func (f *Feature) PropertyFloat64(key string) (float64, error)
- func (f *Feature) PropertyInt(key string) (int, error)
- func (f *Feature) PropertyMustBool(key string, def ...bool) bool
- func (f *Feature) PropertyMustFloat64(key string, def ...float64) float64
- func (f *Feature) PropertyMustInt(key string, def ...int) int
- func (f *Feature) PropertyMustString(key string, def ...string) string
- func (f *Feature) PropertyString(key string) (string, error)
- func (f *Feature) SetProperty(key string, value interface{})
- type FeatureCollection
- type Geometry
- func NewCollectionGeometry(geometries ...*Geometry) *Geometry
- func NewLineStringGeometry(coordinates [][]float64) *Geometry
- func NewMultiLineStringGeometry(lines ...[][]float64) *Geometry
- func NewMultiPointGeometry(coordinates ...[]float64) *Geometry
- func NewMultiPolygonGeometry(polygons ...[][][]float64) *Geometry
- func NewPointGeometry(coordinate []float64) *Geometry
- func NewPolygonGeometry(polygon [][][]float64) *Geometry
- func UnmarshalGeometry(data []byte) (*Geometry, error)
- func (g *Geometry) IsCollection() bool
- func (g *Geometry) IsLineString() bool
- func (g *Geometry) IsMultiLineString() bool
- func (g *Geometry) IsMultiPoint() bool
- func (g *Geometry) IsMultiPolygon() bool
- func (g *Geometry) IsPoint() bool
- func (g *Geometry) IsPolygon() bool
- func (g Geometry) MarshalJSON() ([]byte, error)
- func (g *Geometry) Scan(value interface{}) error
- func (g *Geometry) UnmarshalJSON(data []byte) error
- func (g *Geometry) Value() (driver.Value, error)
- type GeometryType
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Feature ¶
type Feature struct { ID interface{} `json:"id,omitempty"` Type string `json:"type"` BoundingBox []float64 `json:"bbox,omitempty"` Geometry *Geometry `json:"geometry"` Properties map[string]interface{} `json:"properties"` CRS map[string]interface{} `json:"crs,omitempty"` // Coordinate Reference System Objects are not currently supported }
A Feature corresponds to GeoJSON feature object
func NewCollectionFeature ¶
NewCollectionFeature creates and initializes a GeoJSON feature with a geometry collection geometry using the given geometries.
func NewFeature ¶
NewFeature creates and initializes a GeoJSON feature given the required attributes.
func NewLineStringFeature ¶
NewLineStringFeature creates and initializes a GeoJSON feature with a line string geometry using the given coordinates.
func NewMultiLineStringFeature ¶
NewMultiLineStringFeature creates and initializes a GeoJSON feature with a multi-line string geometry using the given lines.
func NewMultiPointFeature ¶
NewMultiPointFeature creates and initializes a GeoJSON feature with a multi-point geometry using the given coordinates.
func NewMultiPolygonFeature ¶
NewMultiPolygonFeature creates and initializes a GeoJSON feature with a multi-polygon geometry using the given polygons.
func NewPointFeature ¶
NewPointFeature creates and initializes a GeoJSON feature with a point geometry using the given coordinate.
func NewPolygonFeature ¶
NewPolygonFeature creates and initializes a GeoJSON feature with a polygon geometry using the given polygon.
func UnmarshalFeature ¶
UnmarshalFeature decodes the data into a GeoJSON feature. Alternately one can call json.Unmarshal(f) directly for the same result.
func (Feature) MarshalJSON ¶
MarshalJSON converts the feature object into the proper JSON. It will handle the encoding of all the child geometries. Alternately one can call json.Marshal(f) directly for the same result.
func (*Feature) PropertyBool ¶
PropertyBool type asserts a property to `bool`.
func (*Feature) PropertyFloat64 ¶
PropertyFloat64 type asserts a property to `float64`.
func (*Feature) PropertyInt ¶
PropertyInt type asserts a property to `int`.
func (*Feature) PropertyMustBool ¶
PropertyMustBool guarantees the return of a `bool` (with optional default)
useful when you explicitly want a `bool` in a single value return context:
myFunc(f.PropertyMustBool("param1"), f.PropertyMustBool("optional_param", true))
func (*Feature) PropertyMustFloat64 ¶
PropertyMustFloat64 guarantees the return of a `float64` (with optional default)
useful when you explicitly want a `float64` in a single value return context:
myFunc(f.PropertyMustFloat64("param1"), f.PropertyMustFloat64("optional_param", 10.1))
func (*Feature) PropertyMustInt ¶
PropertyMustInt guarantees the return of a `int` (with optional default)
useful when you explicitly want a `int` in a single value return context:
myFunc(f.PropertyMustInt("param1"), f.PropertyMustInt("optional_param", 123))
func (*Feature) PropertyMustString ¶
PropertyMustString guarantees the return of a `string` (with optional default)
useful when you explicitly want a `string` in a single value return context:
myFunc(f.PropertyMustString("param1"), f.PropertyMustString("optional_param", "default"))
func (*Feature) PropertyString ¶
PropertyString type asserts a property to `string`.
func (*Feature) SetProperty ¶
SetProperty provides the inverse of all the property functions and is here for consistency.
type FeatureCollection ¶
type FeatureCollection struct { Type string `json:"type"` BoundingBox []float64 `json:"bbox,omitempty"` Features []*Feature `json:"features"` CRS map[string]interface{} `json:"crs,omitempty"` // Coordinate Reference System Objects are not currently supported }
A FeatureCollection correlates to a GeoJSON feature collection.
func NewFeatureCollection ¶
func NewFeatureCollection() *FeatureCollection
NewFeatureCollection creates and initializes a new feature collection.
func UnmarshalFeatureCollection ¶
func UnmarshalFeatureCollection(data []byte) (*FeatureCollection, error)
UnmarshalFeatureCollection decodes the data into a GeoJSON feature collection. Alternately one can call json.Unmarshal(fc) directly for the same result.
Example ¶
package main import ( "fmt" geojson "github.com/paulmach/go.geojson" ) func main() { rawFeatureJSON := []byte(` { "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": {"type": "Point", "coordinates": [102.0, 0.5]}, "properties": {"prop0": "value0"} } ] }`) fc, err := geojson.UnmarshalFeatureCollection(rawFeatureJSON) if err != nil { fmt.Printf("error: %v", err) return } fmt.Printf("%s", fc.Features[0].Properties["prop0"]) }
Output: value0
func (*FeatureCollection) AddFeature ¶
func (fc *FeatureCollection) AddFeature(feature *Feature) *FeatureCollection
AddFeature appends a feature to the collection.
func (FeatureCollection) MarshalJSON ¶
func (fc FeatureCollection) MarshalJSON() ([]byte, error)
MarshalJSON converts the feature collection object into the proper JSON. It will handle the encoding of all the child features and geometries. Alternately one can call json.Marshal(fc) directly for the same result.
Example ¶
package main import ( "fmt" geojson "github.com/paulmach/go.geojson" ) func main() { fc := geojson.NewFeatureCollection() fc.AddFeature(geojson.NewPointFeature([]float64{1, 2})) rawJSON, err := fc.MarshalJSON() if err != nil { fmt.Printf("error: %v", err) return } fmt.Printf("%s", string(rawJSON)) }
Output: {"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2]},"properties":{}}]}
type Geometry ¶
type Geometry struct { Type GeometryType `json:"type"` BoundingBox []float64 `json:"bbox,omitempty"` Point []float64 MultiPoint [][]float64 LineString [][]float64 MultiLineString [][][]float64 Polygon [][][]float64 MultiPolygon [][][][]float64 Geometries []*Geometry CRS map[string]interface{} `json:"crs,omitempty"` // Coordinate Reference System Objects are not currently supported }
A Geometry correlates to a GeoJSON geometry object.
func NewCollectionGeometry ¶
NewCollectionGeometry creates and initializes a geometry collection geometry with the given geometries.
func NewLineStringGeometry ¶
NewLineStringGeometry creates and initializes a line string geometry with the given coordinates.
func NewMultiLineStringGeometry ¶
NewMultiLineStringGeometry creates and initializes a multi-line string geometry with the given lines.
func NewMultiPointGeometry ¶
NewMultiPointGeometry creates and initializes a multi-point geometry with the given coordinates.
func NewMultiPolygonGeometry ¶
NewMultiPolygonGeometry creates and initializes a multi-polygon geometry with the given polygons.
func NewPointGeometry ¶
NewPointGeometry creates and initializes a point geometry with the give coordinate.
func NewPolygonGeometry ¶
NewPolygonGeometry creates and initializes a polygon geometry with the given polygon.
func UnmarshalGeometry ¶
UnmarshalGeometry decodes the data into a GeoJSON geometry. Alternately one can call json.Unmarshal(g) directly for the same result.
Example ¶
package main import ( "fmt" geojson "github.com/paulmach/go.geojson" ) func main() { rawGeometryJSON := []byte(`{"type": "Point", "coordinates": [102.0, 0.5]}`) g, err := geojson.UnmarshalGeometry(rawGeometryJSON) if err != nil { fmt.Printf("error: %v", err) return } fmt.Printf("%s", g.Type) }
Output: Point
func (*Geometry) IsCollection ¶
IsCollection returns true with the geometry object is a GeometryCollection type.
func (*Geometry) IsLineString ¶
IsLineString returns true with the geometry object is a LineString type.
func (*Geometry) IsMultiLineString ¶
IsMultiLineString returns true with the geometry object is a LineString type.
func (*Geometry) IsMultiPoint ¶
IsMultiPoint returns true with the geometry object is a MultiPoint type.
func (*Geometry) IsMultiPolygon ¶
IsMultiPolygon returns true with the geometry object is a MultiPolygon type.
func (Geometry) MarshalJSON ¶
MarshalJSON converts the geometry object into the correct JSON. This fulfills the json.Marshaler interface.
func (*Geometry) Scan ¶
Scan implements the sql.Scanner interface allowing geometry structs to be passed into rows.Scan(...interface{}) The columns must be received as GeoJSON Geometry. When using PostGIS a spatial column would need to be wrapped in ST_AsGeoJSON.
func (*Geometry) UnmarshalJSON ¶
UnmarshalJSON decodes the data into a GeoJSON geometry. This fulfills the json.Unmarshaler interface.
type GeometryType ¶
type GeometryType string
A GeometryType serves to enumerate the different GeoJSON geometry types.
const ( GeometryPoint GeometryType = "Point" GeometryMultiPoint GeometryType = "MultiPoint" GeometryLineString GeometryType = "LineString" GeometryMultiLineString GeometryType = "MultiLineString" GeometryPolygon GeometryType = "Polygon" GeometryMultiPolygon GeometryType = "MultiPolygon" GeometryCollection GeometryType = "GeometryCollection" )
The geometry types supported by GeoJSON 1.0