geojson

package module
v3.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2022 License: Apache-2.0 Imports: 4 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

  1. The command below adds the latest version of go-geojson as a dependency to your module:

    go get -u github.com/everystreet/go-geojson/v3
    
  2. Then the following line can be used to import the package and use the examples below:

    import "github.com/everystreet/go-geojson/v3"
    

Examples

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[geojson.Geometry]

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

GeoJSON features can be created to contain any of the supported geometry types. Additionally, an optional bounding box and property list can be added. Once the feature is constructed, it can be marshaled to JSON.

feature := geojson.NewFeatureWithBoundingBox(
    geojson.NewLineString(
        geojson.MakePosition(34, 12),
        geojson.MakePosition(78, 56),
        geojson.MakePosition(12, 90),
    ),
    geojson.BoundingBox{
        BottomLeft: geojson.MakePosition(1, 1),
        TopRight:   geojson.MakePosition(100, 100),
    },
    geojson.Property{
        Name:  "foo",
        Value: "bar",
    },
)

data, _ := json.Marshal(feature)

Documentation

Overview

Package geojson is for working with GeoJSON in Go.

Index

Constants

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

"type" properties.

Variables

This section is empty.

Functions

func LineStringToS2

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

LineStringToS2 returns an S2 Geometry polyline.

func LoopToS2

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

LoopToS2 returns an S2 Loop.

func PolygonToS2

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.

func (BoundingBox) Validate

func (b BoundingBox) Validate() error

Validate the bounding box.

type Feature

type Feature[G Geometry] struct {
	// contains filtered or unexported fields
}

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

func NewFeature

func NewFeature[G Geometry](geometry G, properties ...Property) Feature[G]

NewFeature creates a new feature.

func NewFeatureWithBoundingBox

func NewFeatureWithBoundingBox[G Geometry](geometry G, box BoundingBox, properties ...Property) Feature[G]

NewFeatureWithBoundingBox creates a new feature with the supplied bounding box.

func (Feature[G]) BoundingBox

func (f Feature[G]) BoundingBox() *BoundingBox

BoundingBox returns the stored bounding box.

func (Feature[G]) Geometry

func (f Feature[G]) Geometry() Geometry

Geometry returns the stored geometry.

func (Feature[G]) MarshalJSON

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

MarshalJSON returns the JSON encoding of the Feature.

func (Feature[G]) Properties

func (f Feature[G]) Properties() PropertyList

Properties returns the stored properties.

func (*Feature[G]) UnmarshalJSON

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

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

func (Feature[G]) Validate

func (f Feature[G]) Validate() error

Validate the feature geometry.

func (Feature[G]) WithProperties added in v3.1.0

func (f Feature[G]) WithProperties(properties ...Property) Feature[G]

WithProperties returns a copy of f with the supplied properties appended.

type FeatureCollection

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

FeatureCollection is a list of Features.

func NewFeatureCollection

func NewFeatureCollection(features ...Feature[Geometry]) FeatureCollection

NewFeatureCollection creates a new feature collection.

func NewFeatureCollectionWithBoundingBox

func NewFeatureCollectionWithBoundingBox(box BoundingBox, features ...Feature[Geometry]) FeatureCollection

NewFeatureCollectionWithBoundingBox creates a new feature collection with the supplied bounding box.

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.

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

type GeometryCollection []Geometry

GeometryCollection is a heterogeneous collection of Geometry objects.

func NewGeometryCollection

func NewGeometryCollection(geometries ...Geometry) *GeometryCollection

NewGeometryCollection returns a GeometryCollection Feature.

func (GeometryCollection) MarshalJSON

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

MarshalJSON returns the JSON encoding of the GeometryCollection.

func (GeometryCollection) Type

Type returns the geometry type.

func (*GeometryCollection) UnmarshalJSON

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

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

func (GeometryCollection) Validate

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 NewLineString

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

NewLineString returns a LineString from the supplied 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

func (l LineString) Validate() error

Validate the LineString.

type MultiLineString

type MultiLineString [][]Position

MultiLineString is a set of LineStrings.

func NewMultiLineString

func NewMultiLineString(pos ...[]Position) *MultiLineString

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

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

func (m MultiLineString) Validate() error

Validate the MultiLineString.

type MultiPoint

type MultiPoint []Position

MultiPoint is a set of Position.

func NewMultiPoint

func NewMultiPoint(pos ...Position) *MultiPoint

NewMultiPoint returns a multipoint with the specified set of positions.

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

func (m MultiPoint) Validate() error

Validate the MultiPoint.

type MultiPolygon

type MultiPolygon [][][]Position

MultiPolygon is a set of Polygons.

func NewMultiPolygon

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

NewMultiPolygon returns a new MultiPolygon from the supplied 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

func (m MultiPolygon) Validate() error

Validate the MultiPolygon.

type Point

type Point Position

Point is a single set of Position.

func NewPoint

func NewPoint(lat, lng float64) *Point

NewPoint returns a point with the specified longitude and latitude.

func NewPointWithElevation

func NewPointWithElevation(lat, lng, elevation float64) *Point

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

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

func (p Point) Validate() error

Validate the Point.

type Polygon

type Polygon [][]Position

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

func NewPolygon

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

NewPolygon returns a new Polygon from the supplied linear rings.

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

func (p Polygon) Validate() error

Validate the Polygon.

type Position

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

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

func MakePosition

func MakePosition(lat, lng float64) Position

MakePosition from longitude and latitude.

func MakePositionWithElevation

func MakePositionWithElevation(lat, lng, elevation float64) Position

MakePositionWithElevation 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.

func (Position) Validate

func (p Position) Validate() error

Validate the position.

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