Documentation ¶
Overview ¶
Package geojson is a convenient way to create and consume GeoJSON.
GeoJSON is the top level JSON that includes everything required to create or decode GeoJSON.
Example ¶
package main import ( "encoding/json" "fmt" "log" "github.com/losinggeneration/geojson" ) func main() { g := geojson.GeoJSON{ Feature: &geojson.Feature{ ID: "MyFeature", Geometry: &geojson.Geometry{ Point: &geojson.Point{ Coordinates: geojson.Position{10, 10}, }, }, Properties: geojson.Properties{ "prop1": "A property", "prop2": "A very palpable property", }, }, } j, err := json.MarshalIndent(g, "", "\t") if err != nil { log.Fatal(err) } fmt.Print(string(j)) }
Output: { "type": "Feature", "id": "MyFeature", "geometry": { "type": "Point", "coordinates": [ 10, 10 ] }, "properties": { "prop1": "A property", "prop2": "A very palpable property" } }
Index ¶
- Variables
- type BoundingBox
- type CRS
- type CRSLink
- type CRSName
- type Feature
- type FeatureCollection
- type GeoJSON
- type Geometry
- type GeometryCollection
- type LineString
- type MultiLineString
- type MultiPoint
- type MultiPolygon
- type Object
- type Point
- type Polygon
- type Position
- type Positions
- type Properties
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrMultipleCRSs happens if both a Name & Link are specified on a // CRS struct ErrMultipleCRSs = errors.New("cannot specify multiple crss") // ErrInvalidCRS is for when an unknown CRS.Type is specified ErrInvalidCRS = errors.New("invalid crs specified") )
var ( // ErrNoGeometry happens when no Geometry has been specified during, for instance // a MarshalJSON operation ErrNoGeometry = errors.New("no geometry specified") // ErrMultipleGeometries happens when more than one Geometry has been specified // and usually happens during MarshalJSON ErrMultipleGeometries = errors.New("cannot specify multiple geometries") // ErrInvalidGeometry can happen during UnmarshalJSON when Type is an unknown // value ErrInvalidGeometry = errors.New("invalid geometry specified") )
var ErrInvalidGeoJSON = errors.New("invalid GeoJSON to unmarshal")
ErrInvalidGeoJSON occurs if there's a problem with the GeoJSON Type specified
var ErrOddBoundingBox = errors.New("bounding box length must be even")
ErrOddBoundingBox is returned from MarshalJSON if the number of values passed to a BoundingBox is not an odd amount.
Functions ¶
This section is empty.
Types ¶
type BoundingBox ¶
type BoundingBox []float64
BoundingBox represents a GeoJSON bounding box
func (BoundingBox) MarshalJSON ¶
func (b BoundingBox) MarshalJSON() ([]byte, error)
MarshalJSON will verify the BoundingBox is minimally valid and marshal the to an array of float64's.
Currently the BoundingBox only verifies that the length is a multiple of 2. More accurate validation would involve knowing about the geometries, features, or feature collections.
type CRS ¶
type CRS struct { // Name is the CRSName of the CRS Name *CRSName // Link is the CRSLink of the CRS Link *CRSLink // contains filtered or unexported fields }
CRS is the main struct that is used to generate a valid GeoJSON CRS object
Only one of Name or Link may be specified
func (CRS) MarshalJSON ¶
MarshalJSON will take a CRS and properly verify the struct and conditionally marshal a CRS name or link
func (*CRS) UnmarshalJSON ¶
UnmarshalJSON will take raw CRS JSON and properly fill out the CRS with either Name or Link set based on the parsed JSON
type CRSLink ¶
type CRSLink struct { // Href is a valid URI to a CRS on the Web Href string `json:"href"` // Type is the optional hint as to the format to be used for the CRS Type string `json:"type,omitempty"` }
CRSLink is a link to a CRS
type CRSName ¶
type CRSName struct { // Name is a string identifying the coordinate system using OGC CRS URNs Name string `json:"name"` }
CRSName is a Named CRS
type Feature ¶
type Feature struct { // Object is the common GeoJSON object Object // ID is the optional commonly used identifier for the Feature ID interface{} `json:"id,omitempty"` // Geometry represents a GeoJSON Geometry object with one of the Geometry // types filled in Geometry *Geometry `json:"geometry"` // Properties represent user defined key/values for a Feature Properties Properties `json:"properties"` }
Feature is a GeoJSON feature object that includes Geometry and Properties
func (Feature) MarshalJSON ¶
MarshalJSON will correctly marshal a Feature (with Type) into JSON
type FeatureCollection ¶
type FeatureCollection struct { // Object is the common GeoJSON object Object // Features is the set of Feature objects to group together Features []Feature `json:"features"` }
FeatureCollection contains multiple features
func (FeatureCollection) MarshalJSON ¶
func (f FeatureCollection) MarshalJSON() ([]byte, error)
MarshalJSON will correctly marshal a FeatureCollection (with Type) into JSON
type GeoJSON ¶
type GeoJSON struct { // Object is the common object properties of a GeoJSON object Object // Geometry is when the GeoJSON is of a Geometry type, this can/should be filled in *Geometry // Feature is when the GeoJSON is of a Feature type, this can/should be filled in *Feature // FeatureCollection is when the GeoJSON is of a FeatureCollection type, this can/should be filled in *FeatureCollection }
GeoJSON is the top level for any valid GeoJSON.
func (GeoJSON) MarshalJSON ¶
MarshalJSON will take a GeoJSON object and marshal it into a GeoJSON object based upon which type is filled in: Geometry, Feature, or FeatureCollection. This will marshal to a null JSON value if all the above types are nil.
func (*GeoJSON) UnmarshalJSON ¶
UnmarshalJSON will take a GeoJSON string and, based on the type, fill in the appropriate GeoJSON object type.
type Geometry ¶
type Geometry struct { // Object is the common GeoJSON object properties Object // Point if set, represents a GeoJSON Point geometry object Point *Point `json:",omitempty"` // MultiPoint if set, represents a GeoJSON MultiPoint geometry object MultiPoint *MultiPoint `json:",omitempty"` // LineString if set, represents a GeoJSON LineString geometry object LineString *LineString `json:",omitempty"` // MultiLineString if set, represents a GeoJSON MultiLineString geometry object MultiLineString *MultiLineString `json:",omitempty"` // Polygon if set, represents a GeoJSON Polygon geometry object Polygon *Polygon `json:",omitempty"` // MultiPolygon if set, represents a GeoJSON MultiPolygon geometry object MultiPolygon *MultiPolygon `json:",omitempty"` // GeometryCollection if set, represents a GeoJSON GeometryCollection geometry object GeometryCollection *GeometryCollection `json:",omitempty"` // contains filtered or unexported fields }
Geometry is the top-level object that will appropriately marshal & unmarshal into GeoJSON
func (Geometry) MarshalJSON ¶
MarshalJSON will take a general Geometry object and appropriately marshal the object into GeoJSON based on the geometry type that's filled in.
func (*Geometry) UnmarshalJSON ¶
UnmarshalJSON will take a geometry GeoJSON string and appropriately fill in the specific geometry type
type GeometryCollection ¶
type GeometryCollection struct { // Object is the common GeoJSON object properties Object // Geometries are the Geometry objects to include in the collection Geometries []Geometry `json:"geometries"` }
GeometryCollection is a set of Geometry objects grouped together
type LineString ¶
type LineString struct { // Object is the common GeoJSON object properties Object // Coordinates are the multiple positions that make up a Line // Positions length must be >= 2 Coordinates Positions `json:"coordinates"` }
LineString is a GeoJSON object that is a group of positions that make a line
type MultiLineString ¶
type MultiLineString struct { // Object is the common GeoJSON object properties Object // Coordinates are multiple lines of multiple positions Coordinates []Positions `json:"coordinates"` }
MultiLineString is a GeoJSON object that is a group of positions that make multiple lines
type MultiPoint ¶
type MultiPoint struct { // Object is the common GeoJSON object properties Object // Coordinates are the multiple positions Coordinates Positions `json:"coordinates"` }
MultiPoint is a group of GeoJSON point objects
type MultiPolygon ¶
type MultiPolygon struct { // Object is the common GeoJSON object properties Object // Coordinates are defined by multiple Polygon positions Coordinates [][]Positions `json:"coordinates"` }
MultiPolygon represents a GeoJSON object of multiple Polygons
type Object ¶
type Object struct { // Type specifies what type of GeoJSON object this is Type string `json:"type"` // BoundingBox specifies a bounding box for a specific GeoJSON object BoundingBox *BoundingBox `json:"bbox,omitempty"` // CRS specifies a CRS for a specific GeoJSON object and all children that // do not specify one themselves CRS *CRS `json:"crs,omitempty"` }
Object is a common GeoJSON object with properties common to most all GeoJSON objects
type Point ¶
type Point struct { // Object is the common GeoJSON object properties Object // Coordinates is the position of the Point Coordinates Position `json:"coordinates"` }
Point is a specific GeoJSON point object
type Polygon ¶
type Polygon struct { // Object is the common GeoJSON object properties Object // Coordinates are multiple closed LineStrings of 4 or more positions. // Multiple rings may be specified, but the first must be an exterior Coordinates []Positions `json:"coordinates"` }
Polygon is a so called LineRing which is a closed LineString of 4 or more positions. Multiple rings may be specified, but the first must be an exterior ring with any others being holes on the interior of the first LineRing
type Position ¶
type Position []float64
Position is single GeoJSON position. It's the building block for multi-position types. Positions should be specified in an x, y, z ordering. This would be: [longitude, latitude, altitude] for a geographic position.
type Properties ¶
type Properties map[string]interface{}
Properties are a general way to specify a free-form JSON object