Documentation ¶
Overview ¶
Package geojson is a library for encoding and decoding GeoJSON into Go structs using the geometries in the orb package. Supports both the json.Marshaler and json.Unmarshaler interfaces as well as helper functions such as `UnmarshalFeatureCollection` and `UnmarshalFeature`.
Example (Centroid) ¶
package main import ( "fmt" "log" "github.com/TucarApp/orb" "github.com/TucarApp/orb/geojson" "github.com/TucarApp/orb/planar" "github.com/TucarApp/orb/quadtree" ) type CentroidPoint struct { *geojson.Feature } func (cp CentroidPoint) Point() orb.Point { // this is where you would decide how to define // the representative point of the feature. c, _ := planar.CentroidArea(cp.Geometry) return c } func main() { qt := quadtree.New(orb.Bound{Min: orb.Point{0, 0}, Max: orb.Point{1, 1}}) // feature with center {0.5, 0.5} but centroid {0.25, 0.25} f := geojson.NewFeature(orb.MultiPoint{{0, 0}, {0, 0}, {0, 0}, {1, 1}}) f.Properties["centroid"] = "0.25" err := qt.Add(CentroidPoint{f}) if err != nil { log.Fatalf("unexpected error: %v", err) } // feature with centroid {0.6, 0.6} f = geojson.NewFeature(orb.Point{0.6, 0.6}) f.Properties["centroid"] = "0.6" err = qt.Add(CentroidPoint{f}) if err != nil { log.Fatalf("unexpected error: %v", err) } feature := qt.Find(orb.Point{0.5, 0.5}).(CentroidPoint).Feature fmt.Printf("centroid=%s", feature.Properties["centroid"]) }
Output: centroid=0.6
Example (Unmarshal) ¶
package main import ( "encoding/json" "fmt" "log" "github.com/TucarApp/orb" "github.com/TucarApp/orb/geojson" ) func main() { rawJSON := []byte(` { "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": {"type": "Point", "coordinates": [102.0, 0.5]}, "properties": {"prop0": "value0"} } ] }`) fc := geojson.NewFeatureCollection() err := json.Unmarshal(rawJSON, &fc) if err != nil { log.Fatalf("invalid json: %v", err) } // Geometry will be unmarshalled into the correct geo.Geometry type. point := fc.Features[0].Geometry.(orb.Point) fmt.Println(point) }
Output: [102 0.5]
Index ¶
- Constants
- Variables
- type BBox
- type Feature
- type FeatureCollection
- func (fc *FeatureCollection) Append(feature *Feature) *FeatureCollection
- func (fc FeatureCollection) MarshalBSON() ([]byte, error)
- func (fc FeatureCollection) MarshalJSON() ([]byte, error)
- func (fc *FeatureCollection) UnmarshalBSON(data []byte) error
- func (fc *FeatureCollection) UnmarshalJSON(data []byte) error
- type Geometry
- func (g *Geometry) Geometry() orb.Geometry
- func (g *Geometry) MarshalBSON() ([]byte, error)
- func (g *Geometry) MarshalBSONValue() (bsontype.Type, []byte, error)
- func (g *Geometry) MarshalJSON() ([]byte, error)
- func (g *Geometry) UnmarshalBSON(data []byte) error
- func (g *Geometry) UnmarshalJSON(data []byte) error
- type LineString
- type MultiLineString
- type MultiPoint
- type MultiPolygon
- type Point
- type Polygon
- type Properties
Examples ¶
Constants ¶
const ( TypePoint = "Point" TypeMultiPoint = "MultiPoint" TypeLineString = "LineString" TypeMultiLineString = "MultiLineString" TypePolygon = "Polygon" TypeMultiPolygon = "MultiPolygon" )
A list of the geojson types that are currently supported.
Variables ¶
var CustomJSONMarshaler interface { Marshal(v interface{}) ([]byte, error) } = nil
CustomJSONMarshaler can be set to have the code use a different json marshaler than the default in the standard library. One use case in enabling `github.com/json-iterator/go` with something like this:
import ( jsoniter "github.com/json-iterator/go" "github.com/TucarApp/orb" ) var c = jsoniter.Config{ EscapeHTML: true, SortMapKeys: false, MarshalFloatWith6Digits: true, }.Froze() orb.CustomJSONMarshaler = c orb.CustomJSONUnmarshaler = c
Note that any errors encountered during marshaling will be different.
var CustomJSONUnmarshaler interface { Unmarshal(data []byte, v interface{}) error } = nil
CustomJSONUnmarshaler can be set to have the code use a different json unmarshaler than the default in the standard library. One use case in enabling `github.com/json-iterator/go` with something like this:
import ( jsoniter "github.com/json-iterator/go" "github.com/TucarApp/orb" ) var c = jsoniter.Config{ EscapeHTML: true, SortMapKeys: false, MarshalFloatWith6Digits: true, }.Froze() orb.CustomJSONMarshaler = c orb.CustomJSONUnmarshaler = c
Note that any errors encountered during unmarshaling will be different.
var ErrInvalidGeometry = errors.New("geojson: invalid geometry")
ErrInvalidGeometry will be returned if a the json of the geometry is invalid.
Functions ¶
This section is empty.
Types ¶
type BBox ¶
type BBox []float64
BBox is for the geojson bbox attribute which is an array with all axes of the most southwesterly point followed by all axes of the more northeasterly point.
type Feature ¶
type Feature struct { ID interface{} `json:"id,omitempty"` Type string `json:"type"` BBox BBox `json:"bbox,omitempty"` Geometry orb.Geometry `json:"geometry"` Properties Properties `json:"properties"` }
A Feature corresponds to GeoJSON feature object
func NewFeature ¶
NewFeature creates and initializes a GeoJSON feature given the required attributes.
func UnmarshalFeature ¶
UnmarshalFeature decodes the data into a GeoJSON feature. Alternately one can call json.Unmarshal(f) directly for the same result.
func (Feature) MarshalBSON ¶
MarshalBSON 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) 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) Point ¶
Point implements the orb.Pointer interface so that Features can be used with quadtrees. The point returned is the center of the Bound of the geometry. To represent the geometry with another point you must create a wrapper type.
Example ¶
package main import ( "fmt" "log" "github.com/TucarApp/orb" "github.com/TucarApp/orb/geojson" "github.com/TucarApp/orb/quadtree" ) func main() { f := geojson.NewFeature(orb.Point{1, 1}) f.Properties["key"] = "value" qt := quadtree.New(f.Geometry.Bound().Pad(1)) err := qt.Add(f) // add the feature to a quadtree if err != nil { log.Fatalf("unexpected error: %v", err) } // type assert the feature back into a Feature from // the orb.Pointer interface. feature := qt.Find(orb.Point{0, 0}).(*geojson.Feature) fmt.Printf("key=%s", feature.Properties["key"]) }
Output: key=value
func (*Feature) UnmarshalBSON ¶
UnmarshalBSON will unmarshal a BSON document created with bson.Marshal.
func (*Feature) UnmarshalJSON ¶
UnmarshalJSON handles the correct unmarshalling of the data into the orb.Geometry types.
type FeatureCollection ¶
type FeatureCollection struct { Type string `json:"type"` BBox BBox `json:"bbox,omitempty"` Features []*Feature `json:"features"` // ExtraMembers can be used to encoded/decode extra key/members in // the base of the feature collection. Note that keys of "type", "bbox" // and "features" will not work as those are reserved by the GeoJSON spec. ExtraMembers Properties `json:"-"` }
A FeatureCollection correlates to a GeoJSON feature collection.
Example (ForeignMembers) ¶
package main import ( "encoding/json" "fmt" "log" "github.com/TucarApp/orb/geojson" ) func main() { rawJSON := []byte(` { "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": {"type": "Point", "coordinates": [102.0, 0.5]}, "properties": {"prop0": "value0"} } ], "title": "Title as Foreign Member" }`) fc := geojson.NewFeatureCollection() err := json.Unmarshal(rawJSON, &fc) if err != nil { log.Fatalf("invalid json: %v", err) } fmt.Println(fc.Features[0].Geometry) fmt.Println(fc.ExtraMembers["title"]) data, _ := json.Marshal(fc) fmt.Println(string(data)) }
Output: [102 0.5] Title as Foreign Member {"features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[102,0.5]},"properties":{"prop0":"value0"}}],"title":"Title as Foreign Member","type":"FeatureCollection"}
Example (ForeignMembersCustom) ¶
package main import ( "encoding/json" "fmt" "log" "github.com/TucarApp/orb/geojson" ) // MyFeatureCollection is a depricated/no longer supported way to extract // foreign/extra members from a feature collection. Now an UnmarshalJSON // method, like below, is required for it to work. type MyFeatureCollection struct { geojson.FeatureCollection Title string `json:"title"` } // UnmarshalJSON implemented as below is now required for the extra members // to be decoded directly into the type. func (fc *MyFeatureCollection) UnmarshalJSON(data []byte) error { err := json.Unmarshal(data, &fc.FeatureCollection) if err != nil { return err } fc.Title = fc.ExtraMembers.MustString("title", "") return nil } func main() { // Note: this approach to handling foreign/extra members requires // implementing an `UnmarshalJSON` method on the new type. // See MyFeatureCollection type and its UnmarshalJSON function above. rawJSON := []byte(` { "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": {"type": "Point", "coordinates": [102.0, 0.5]}, "properties": {"prop0": "value0"} } ], "title": "Title as Foreign Member" }`) fc := &MyFeatureCollection{} err := json.Unmarshal(rawJSON, &fc) if err != nil { log.Fatalf("invalid json: %v", err) } fmt.Println(fc.FeatureCollection.Features[0].Geometry) fmt.Println(fc.Features[0].Geometry) fmt.Println(fc.Title) }
Output: [102 0.5] [102 0.5] Title as Foreign Member
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" "github.com/TucarApp/orb" "github.com/TucarApp/orb/geojson" ) func main() { rawJSON := []byte(` { "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": {"type": "Point", "coordinates": [102.0, 0.5]}, "properties": {"prop0": "value0"} } ] }`) fc, _ := geojson.UnmarshalFeatureCollection(rawJSON) // Geometry will be unmarshalled into the correct geo.Geometry type. point := fc.Features[0].Geometry.(orb.Point) fmt.Println(point) }
Output: [102 0.5]
func (*FeatureCollection) Append ¶
func (fc *FeatureCollection) Append(feature *Feature) *FeatureCollection
Append appends a feature to the collection.
func (FeatureCollection) MarshalBSON ¶
func (fc FeatureCollection) MarshalBSON() ([]byte, error)
MarshalBSON converts the feature collection object into a BSON document represented by bytes. It will handle the encoding of all the child features and geometries. Items in the ExtraMembers map will be included in the base of the feature collection object.
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. Items in the ExtraMembers map will be included in the base of the feature collection object.
Example ¶
package main import ( "encoding/json" "fmt" "log" "github.com/TucarApp/orb" "github.com/TucarApp/orb/geojson" ) func main() { fc := geojson.NewFeatureCollection() fc.Append(geojson.NewFeature(orb.Point{1, 2})) _, err := fc.MarshalJSON() if err != nil { log.Fatalf("marshal error: %v", err) } // standard lib encoding/json package will also work data, err := json.MarshalIndent(fc, "", " ") if err != nil { log.Fatalf("marshal error: %v", err) } fmt.Println(string(data)) }
Output: { "features": [ { "type": "Feature", "geometry": { "type": "Point", "coordinates": [ 1, 2 ] }, "properties": null } ], "type": "FeatureCollection" }
func (*FeatureCollection) UnmarshalBSON ¶
func (fc *FeatureCollection) UnmarshalBSON(data []byte) error
UnmarshalBSON will unmarshal a BSON document created with bson.Marshal. Extra/foreign members will be put into the `ExtraMembers` attribute.
func (*FeatureCollection) UnmarshalJSON ¶
func (fc *FeatureCollection) UnmarshalJSON(data []byte) error
UnmarshalJSON decodes the data into a GeoJSON feature collection. Extra/foreign members will be put into the `ExtraMembers` attribute.
type Geometry ¶
type Geometry struct { Type string `json:"type"` Coordinates orb.Geometry `json:"coordinates,omitempty"` Geometries []*Geometry `json:"geometries,omitempty"` }
A Geometry matches the structure of a GeoJSON Geometry.
func NewGeometry ¶
NewGeometry will create a Geometry object but will convert the input into a GoeJSON geometry. For example, it will convert Rings and Bounds into Polygons.
func UnmarshalGeometry ¶
UnmarshalGeometry decodes the JSON data into a GeoJSON feature. Alternately one can call json.Unmarshal(g) directly for the same result.
func (*Geometry) Geometry ¶
Geometry returns the orb.Geometry for the geojson Geometry. This will convert the "Geometries" into a orb.Collection if applicable.
func (*Geometry) MarshalBSON ¶
MarshalBSON will convert the geometry into a BSON document with the structure of a GeoJSON Geometry. This function is used when the geometry is the top level document to be marshalled.
func (*Geometry) MarshalBSONValue ¶
MarshalBSONValue will marshal the geometry into a BSON value with the structure of a GeoJSON Geometry.
func (*Geometry) MarshalJSON ¶
MarshalJSON will marshal the geometry into the correct JSON structure.
func (*Geometry) UnmarshalBSON ¶
UnmarshalBSON will unmarshal a BSON document created with bson.Marshal.
func (*Geometry) UnmarshalJSON ¶
UnmarshalJSON will unmarshal the correct geometry from the JSON structure.
type LineString ¶
type LineString orb.LineString
A LineString is a helper type that will marshal to/from a GeoJSON LineString geometry.
func (LineString) Geometry ¶
func (ls LineString) Geometry() orb.Geometry
Geometry will return the orb.Geometry version of the data.
func (LineString) MarshalBSON ¶
func (ls LineString) MarshalBSON() ([]byte, error)
MarshalBSON will convert the LineString into a GeoJSON LineString geometry.
func (LineString) MarshalJSON ¶
func (ls LineString) MarshalJSON() ([]byte, error)
MarshalJSON will convert the LineString into a GeoJSON LineString geometry.
func (*LineString) UnmarshalBSON ¶
func (ls *LineString) UnmarshalBSON(data []byte) error
UnmarshalBSON will unmarshal the GeoJSON MultiPoint geometry.
func (*LineString) UnmarshalJSON ¶
func (ls *LineString) UnmarshalJSON(data []byte) error
UnmarshalJSON will unmarshal the GeoJSON MultiPoint geometry.
type MultiLineString ¶
type MultiLineString orb.MultiLineString
A MultiLineString is a helper type that will marshal to/from a GeoJSON MultiLineString geometry.
func (MultiLineString) Geometry ¶
func (mls MultiLineString) Geometry() orb.Geometry
Geometry will return the orb.Geometry version of the data.
func (MultiLineString) MarshalBSON ¶
func (mls MultiLineString) MarshalBSON() ([]byte, error)
MarshalBSON will convert the MultiLineString into a GeoJSON MultiLineString geometry.
func (MultiLineString) MarshalJSON ¶
func (mls MultiLineString) MarshalJSON() ([]byte, error)
MarshalJSON will convert the MultiLineString into a GeoJSON MultiLineString geometry.
func (*MultiLineString) UnmarshalBSON ¶
func (mls *MultiLineString) UnmarshalBSON(data []byte) error
UnmarshalBSON will unmarshal the GeoJSON MultiPoint geometry.
func (*MultiLineString) UnmarshalJSON ¶
func (mls *MultiLineString) UnmarshalJSON(data []byte) error
UnmarshalJSON will unmarshal the GeoJSON MultiPoint geometry.
type MultiPoint ¶
type MultiPoint orb.MultiPoint
A MultiPoint is a helper type that will marshal to/from a GeoJSON MultiPoint geometry.
func (MultiPoint) Geometry ¶
func (mp MultiPoint) Geometry() orb.Geometry
Geometry will return the orb.Geometry version of the data.
func (MultiPoint) MarshalBSON ¶
func (mp MultiPoint) MarshalBSON() ([]byte, error)
MarshalBSON will convert the MultiPoint into a GeoJSON MultiPoint geometry BSON.
func (MultiPoint) MarshalJSON ¶
func (mp MultiPoint) MarshalJSON() ([]byte, error)
MarshalJSON will convert the MultiPoint into a GeoJSON MultiPoint geometry.
func (*MultiPoint) UnmarshalBSON ¶
func (mp *MultiPoint) UnmarshalBSON(data []byte) error
UnmarshalBSON will unmarshal the GeoJSON MultiPoint geometry.
func (*MultiPoint) UnmarshalJSON ¶
func (mp *MultiPoint) UnmarshalJSON(data []byte) error
UnmarshalJSON will unmarshal the GeoJSON MultiPoint geometry.
type MultiPolygon ¶
type MultiPolygon orb.MultiPolygon
A MultiPolygon is a helper type that will marshal to/from a GeoJSON MultiPolygon geometry.
func (MultiPolygon) Geometry ¶
func (mp MultiPolygon) Geometry() orb.Geometry
Geometry will return the orb.Geometry version of the data.
func (MultiPolygon) MarshalBSON ¶
func (mp MultiPolygon) MarshalBSON() ([]byte, error)
MarshalBSON will convert the MultiPolygon into a GeoJSON MultiPolygon geometry.
func (MultiPolygon) MarshalJSON ¶
func (mp MultiPolygon) MarshalJSON() ([]byte, error)
MarshalJSON will convert the MultiPolygon into a GeoJSON MultiPolygon geometry.
func (*MultiPolygon) UnmarshalBSON ¶
func (mp *MultiPolygon) UnmarshalBSON(data []byte) error
UnmarshalBSON will unmarshal the GeoJSON MultiPolygon geometry.
func (*MultiPolygon) UnmarshalJSON ¶
func (mp *MultiPolygon) UnmarshalJSON(data []byte) error
UnmarshalJSON will unmarshal the GeoJSON MultiPolygon geometry.
type Point ¶
A Point is a helper type that will marshal to/from a GeoJSON Point geometry.
func (Point) MarshalBSON ¶
MarshalBSON will convert the Point into a BSON value following the GeoJSON Point structure.
func (Point) MarshalJSON ¶
MarshalJSON will convert the Point into a GeoJSON Point geometry.
func (*Point) UnmarshalBSON ¶
UnmarshalBSON will unmarshal GeoJSON Point geometry.
func (*Point) UnmarshalJSON ¶
UnmarshalJSON will unmarshal the GeoJSON Point geometry.
type Polygon ¶
A Polygon is a helper type that will marshal to/from a GeoJSON Polygon geometry.
func (Polygon) MarshalBSON ¶
MarshalBSON will convert the Polygon into a GeoJSON Polygon geometry.
func (Polygon) MarshalJSON ¶
MarshalJSON will convert the Polygon into a GeoJSON Polygon geometry.
func (*Polygon) UnmarshalBSON ¶
UnmarshalBSON will unmarshal the GeoJSON Polygon geometry.
func (*Polygon) UnmarshalJSON ¶
UnmarshalJSON will unmarshal the GeoJSON Polygon geometry.
type Properties ¶
type Properties map[string]interface{}
Properties defines the feature properties with some helper methods.
func (Properties) Clone ¶
func (p Properties) Clone() Properties
Clone returns a shallow copy of the properties.
func (Properties) MustBool ¶
func (p Properties) MustBool(key string, def ...bool) bool
MustBool guarantees the return of a `bool` (with optional default). This function useful when you explicitly want a `bool` in a single value return context, for example:
myFunc(f.Properties.MustBool("param1"), f.Properties.MustBool("optional_param", true))
This function will panic if the value is present but not a bool.
func (Properties) MustFloat64 ¶
func (p Properties) MustFloat64(key string, def ...float64) float64
MustFloat64 guarantees the return of a `float64` (with optional default) This function useful when you explicitly want a `float64` in a single value return context, for example:
myFunc(f.Properties.MustFloat64("param1"), f.Properties.MustFloat64("optional_param", 10.1))
This function will panic if the value is present but not a number.
func (Properties) MustInt ¶
func (p Properties) MustInt(key string, def ...int) int
MustInt guarantees the return of an `int` (with optional default). This function useful when you explicitly want a `int` in a single value return context, for example:
myFunc(f.Properties.MustInt("param1"), f.Properties.MustInt("optional_param", 123))
This function will panic if the value is present but not a number.
func (Properties) MustString ¶
func (p Properties) MustString(key string, def ...string) string
MustString guarantees the return of a `string` (with optional default) This function useful when you explicitly want a `string` in a single value return context, for example:
myFunc(f.Properties.MustString("param1"), f.Properties.MustString("optional_param", "default"))
This function will panic if the value is present but not a string.