geojson

package module
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 20, 2019 License: Apache-2.0 Imports: 5 Imported by: 0

README

go-geojson

go-geojson is a Go package for working with the GeoJSON format, as standardized by RFC 7946.

This package supports marshalling and unmarshalling of all geometry types: Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon and GeometryCollection.

Usage

go-geojson implements the json.Marshaler and json.Unmarshaler interfaces. This means you can work with GeoJSON in the same way as you would with "regular" JSON.

Unmarshal

The example below demonstrates how to unmarshal a GeoJSON Feature. Once unmarshalled into a geojson.Feature, you have access to the bounding box and properties, and the Geometry object. As the Geometry can be one of several types, a type switch can be used to determine the type and work with it.

var feature geojson.Feature

_ = json.Unmarshal(`
    {
        "type": "Feature",
        "geometry": {
            "type": "LineString",
            "coordinates": [
                [12, 34],
                [56, 78],
                [90, 12]
            ]
        }
    }`, &feature)

switch f := feature.Geometry.(type) {
case *geojson.LineString:
    for _, pos := range *f {
        fmt.Println(pos)
    }
}
Marshal

The easiest way to create GeoJSON objects is using the provided helpers. The example below demonstrates creation of a simple LineString using geojson.NewLineString. The returned geojson.Feature type contains methods to add a bounding box and properties.

linestring := geojson.NewLineString(
    geojson.NewPosition(12, 34),
    geojson.NewPosition(56, 78),
    geojson.NewPosition(90, 12),
).WithBoundingBox( // optionally set bounding box
    geojson.NewPosition(1, 1),
    geojson.NewPosition(100, 100),
)

data, _ := json.Marshal(linestring)

Documentation

Index

Constants

View Source
const (
	TypePropFeature           = "Feature"
	TypePropFeatureCollection = "FeatureCollection"
)

"type" properties.

Variables

This section is empty.

Functions

func LineStringToS2 added in v1.1.0

func LineStringToS2(linestring LineString) (*s2.Polyline, error)

LineStringToS2 returns an S2 Geometry polyline.

func LoopToS2 added in v1.1.1

func LoopToS2(loop []Position) *s2.Loop

LoopToS2 returns an S2 Loop.

func PolygonToS2 added in v1.1.0

func PolygonToS2(polygon Polygon) (*s2.Polygon, error)

PolygonToS2 returns an S2 Geometry polygon.

Types

type BoundingBox

type BoundingBox struct {
	BottomLeft Position
	TopRight   Position
}

BoundingBox represents a bounding box in either 2D or 3D space.

func (BoundingBox) MarshalJSON

func (b BoundingBox) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON encoding of the box. It is an error if only 1 elevation value is set - either both positions or neither must have it.

func (*BoundingBox) UnmarshalJSON

func (b *BoundingBox) UnmarshalJSON(data []byte) error

UnmarshalJSON parses the JSON-encoded data and stores the results.

type Feature

type Feature struct {
	Geometry   Geometry
	BBox       *BoundingBox
	Properties PropertyList
}

Feature consists of a specific geometry type and a list of properties.

func NewGeometryCollection added in v1.1.0

func NewGeometryCollection(geos ...Geometry) *Feature

NewGeometryCollection returns a GeometryCollection Feature.

func NewLineString

func NewLineString(pos1, pos2 Position, others ...Position) *Feature

NewLineString returns a LineString from the supplied positions.

func NewMultiLineString

func NewMultiLineString(ls ...[]Position) *Feature

NewMultiLineString returns a new MultiLineString from the supplied position "strings".

func NewMultiPoint

func NewMultiPoint(pos ...Position) *Feature

NewMultiPoint returns a MultiPoint from the specified set of position.

func NewMultiPolygon

func NewMultiPolygon(p ...[][]Position) *Feature

NewMultiPolygon returns a new MultiPolygon from the supplied polygons.

func NewPoint

func NewPoint(long, lat float64) *Feature

NewPoint returns a Point Feature with the specified longitude and latitude.

func NewPointWithElevation

func NewPointWithElevation(long, lat, elevation float64) *Feature

NewPointWithElevation returns a Point Feature with the specified longitude, latitude and elevation.

func NewPolygon

func NewPolygon(rings ...[]Position) *Feature

NewPolygon returns a new Polygon from the supplied linear rings.

func (*Feature) AddProperty

func (f *Feature) AddProperty(name string, value interface{}) *Feature

AddProperty appends a new property.

func (Feature) MarshalJSON

func (f Feature) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON encoding of the Feature.

func (*Feature) UnmarshalJSON

func (f *Feature) UnmarshalJSON(data []byte) error

UnmarshalJSON parses the JSON-encoded data and stores the result.

func (*Feature) WithBoundingBox

func (f *Feature) WithBoundingBox(bottomLeft, topRight Position) *Feature

WithBoundingBox sets the optional bounding box.

func (*Feature) WithProperties

func (f *Feature) WithProperties(props ...Property) *Feature

WithProperties sets the optional properties, removing all existing properties.

type FeatureCollection

type FeatureCollection struct {
	Features []Feature
	BBox     *BoundingBox
}

FeatureCollection is a list of Features.

func NewFeatureCollection

func NewFeatureCollection(features ...*Feature) *FeatureCollection

NewFeatureCollection returns a FeatureCollection consisting of the supplied Features.

func (FeatureCollection) MarshalJSON

func (c FeatureCollection) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON encoding of the FeatureCollection.

func (*FeatureCollection) UnmarshalJSON

func (c *FeatureCollection) UnmarshalJSON(data []byte) error

UnmarshalJSON parses the JSON-encoded data and stores the result.

func (*FeatureCollection) WithBoundingBox

func (c *FeatureCollection) WithBoundingBox(bottomLeft, topRight Position) *FeatureCollection

WithBoundingBox sets the optional bounding box.

type Geometry

type Geometry interface {
	json.Marshaler
	json.Unmarshaler
	Type() GeometryType
	Validate() error
}

Geometry contains the points represented by a particular geometry type.

type GeometryCollection added in v1.1.0

type GeometryCollection []Geometry

GeometryCollection is a heterogeneous collection of Geometry objects.

func (GeometryCollection) MarshalJSON added in v1.1.0

func (c GeometryCollection) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON encoding of the GeometryCollection.

func (GeometryCollection) Type added in v1.1.0

Type returns the geometry type.

func (*GeometryCollection) UnmarshalJSON added in v1.1.0

func (c *GeometryCollection) UnmarshalJSON(data []byte) error

UnmarshalJSON parses the JSON-encoded data and stores the result.

func (GeometryCollection) Validate added in v1.1.0

func (c GeometryCollection) Validate() error

Validate the GeometryCollection.

type GeometryType

type GeometryType string

GeometryType is a supported geometry type.

const (
	PointGeometryType           GeometryType = "Point"
	MultiPointGeometryType      GeometryType = "MultiPoint"
	LineStringGeometryType      GeometryType = "LineString"
	MultiLineStringGeometryType GeometryType = "MultiLineString"
	PolygonGeometryType         GeometryType = "Polygon"
	MultiPolygonGeometryType    GeometryType = "MultiPolygon"
	GeometryCollectionType      GeometryType = "GeometryCollection"
)

Types of geometry.

type LineString

type LineString []Position

LineString is a set of two or more Positions.

func (LineString) MarshalJSON

func (l LineString) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON encoding of the LineString.

func (LineString) Type

func (l LineString) Type() GeometryType

Type returns the geometry type.

func (*LineString) UnmarshalJSON

func (l *LineString) UnmarshalJSON(data []byte) error

UnmarshalJSON parses the JSON-encoded data and stores the result.

func (LineString) Validate added in v1.1.0

func (l LineString) Validate() error

Validate the LineString.

type MultiLineString

type MultiLineString [][]Position

MultiLineString is a set of LineStrings.

func (MultiLineString) MarshalJSON

func (m MultiLineString) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON encoding of the MultiLineString.

func (MultiLineString) Type

func (m MultiLineString) Type() GeometryType

Type returns the geometry type.

func (*MultiLineString) UnmarshalJSON

func (m *MultiLineString) UnmarshalJSON(data []byte) error

UnmarshalJSON parses the JSON-encoded data and stores the result.

func (MultiLineString) Validate added in v1.1.0

func (m MultiLineString) Validate() error

Validate the MultiLineString.

type MultiPoint

type MultiPoint []Position

MultiPoint is a set of Position.

func (MultiPoint) MarshalJSON

func (m MultiPoint) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON encoding of the MultiPoint.

func (MultiPoint) Type

func (m MultiPoint) Type() GeometryType

Type returns the geometry type.

func (*MultiPoint) UnmarshalJSON

func (m *MultiPoint) UnmarshalJSON(data []byte) error

UnmarshalJSON parses the JSON-encoded data and stores the result.

func (MultiPoint) Validate added in v1.1.0

func (m MultiPoint) Validate() error

Validate the MultiPoint.

type MultiPolygon

type MultiPolygon [][][]Position

MultiPolygon is a set of Polygons.

func (MultiPolygon) MarshalJSON

func (m MultiPolygon) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON encoding of the MultiPolygon.

func (MultiPolygon) Type

func (m MultiPolygon) Type() GeometryType

Type returns the geometry type.

func (*MultiPolygon) UnmarshalJSON

func (m *MultiPolygon) UnmarshalJSON(data []byte) error

UnmarshalJSON parses the JSON-encoded data and stores the result.

func (MultiPolygon) Validate added in v1.1.0

func (m MultiPolygon) Validate() error

Validate the MultiPolygon.

type OptionalFloat64

type OptionalFloat64 struct {
	// contains filtered or unexported fields
}

OptionalFloat64 is a type that represents a float64 that can be optionally set.

func NewOptionalFloat64

func NewOptionalFloat64(val float64) OptionalFloat64

NewOptionalFloat64 creates a new OptionalFloat64 set to the specified value.

func (OptionalFloat64) Get

func (o OptionalFloat64) Get() (float64, bool)

Get the float64 value and whether or not it's set.

func (OptionalFloat64) IsSet

func (o OptionalFloat64) IsSet() bool

IsSet returns true if the value is set, and false if not.

func (OptionalFloat64) String

func (o OptionalFloat64) String() string

func (OptionalFloat64) Value

func (o OptionalFloat64) Value() float64

Value returns the value. Should call this method if OptionalFloat64.IsSet() returns true.

type Point

type Point Position

Point is a single set of Position.

func (Point) MarshalJSON

func (p Point) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON encoding of the Point.

func (Point) Type

func (p Point) Type() GeometryType

Type returns the geometry type.

func (*Point) UnmarshalJSON

func (p *Point) UnmarshalJSON(data []byte) error

UnmarshalJSON parses the JSON-encoded data and stores the result.

func (Point) Validate added in v1.1.0

func (p Point) Validate() error

Validate the Point.

type Polygon

type Polygon [][]Position

Polygon is a set of linear rings (closed LineStrings).

func (Polygon) MarshalJSON

func (p Polygon) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON encoding of the Polygon.

func (Polygon) Type

func (p Polygon) Type() GeometryType

Type returns the geometry type.

func (*Polygon) UnmarshalJSON

func (p *Polygon) UnmarshalJSON(data []byte) error

UnmarshalJSON parses the JSON-encoded data and stores the result.

func (Polygon) Validate added in v1.1.0

func (p Polygon) Validate() error

Validate the Polygon.

type Position

type Position struct {
	Longitude float64
	Latitude  float64
	Elevation OptionalFloat64
}

Position represents a longitude and latitude with optional elevation/altitude.

func NewPosition

func NewPosition(long, lat float64) Position

NewPosition from longitude and latitude.

func NewPositionWithElevation

func NewPositionWithElevation(long, lat, elevation float64) Position

NewPositionWithElevation from longitude, latitude and elevation.

func (Position) MarshalJSON

func (p Position) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON encoding of the Position. The JSON encoding is an array of numbers with the longitude followed by the latitude, and optional elevation.

func (Position) String

func (p Position) String() string

func (*Position) UnmarshalJSON

func (p *Position) UnmarshalJSON(data []byte) error

UnmarshalJSON parses the JSON-encoded data and stores the results.

type Property

type Property struct {
	Name  string
	Value interface{}
}

Property represents a single property of arbitrary type.

func (Property) GetValue

func (p Property) GetValue(dest interface{}) error

GetValue assigns the value to dest if the types are equal.

type PropertyList

type PropertyList []Property

PropertyList is a list of Properties.

func NewPropertyList

func NewPropertyList(props ...Property) PropertyList

NewPropertyList creates a new PropertyList from the supplied Properties.

func (*PropertyList) Get

func (l *PropertyList) Get(name string) (*Property, bool)

Get a Property from the list.

func (*PropertyList) GetValue

func (l *PropertyList) GetValue(name string, dest interface{}) error

GetValue assigns a named property to dest if the types are equal.

func (PropertyList) MarshalJSON

func (l PropertyList) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON encoding of the PropertyList.

func (*PropertyList) UnmarshalJSON

func (l *PropertyList) UnmarshalJSON(data []byte) error

UnmarshalJSON parses the JSON-encoded data and stores the result.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL