geojson

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2022 License: LGPL-2.1 Imports: 7 Imported by: 0

Documentation

Overview

Package geojson is a library for encoding and decoding GeoJSON into Go structs using the geometries. Supports both the json.Marshaler and json.Unmarshaler interfaces as well as helper functions such as `UnmarshalFeatureCollection` and `UnmarshalFeature`.

Index

Constants

This section is empty.

Variables

View Source
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.

func NewBBox

func NewBBox(b space.Bound) BBox

NewBBox creates a bbox from a a bound.

func (BBox) Bound

func (bb BBox) Bound() space.Bound

Bound returns the space.Bound for the BBox.

func (BBox) Valid

func (bb BBox) Valid() bool

Valid checks if the bbox is present and has at least 4 elements.

type BaseEncoder

type BaseEncoder struct {
}

func (*BaseEncoder) Decode

func (e *BaseEncoder) Decode(s []byte) (space.Geometry, error)

Decode Returns geometry of that decode string.

func (*BaseEncoder) Encode

func (e *BaseEncoder) Encode(g space.Geometry) []byte

Encode Returns string of that encode geometry.

func (*BaseEncoder) Read

func (e *BaseEncoder) Read(r io.Reader) (space.Geometry, error)

Read Returns geometry from reader.

func (*BaseEncoder) ReadBytes

func (e *BaseEncoder) ReadBytes(r io.Reader) ([]byte, error)

Read Returns geometry from reader.

func (*BaseEncoder) ReadGeoJSON

func (e *BaseEncoder) ReadGeoJSON(r io.Reader) (*FeatureCollection, error)

ReadGeoJSON Returns geometry from reader .

func (*BaseEncoder) Write

func (e *BaseEncoder) Write(w io.Writer, g space.Geometry) error

Write write geometry to reader.

func (*BaseEncoder) WriteBytes

func (e *BaseEncoder) WriteBytes(w io.Writer, buf []byte) error

Write write geometry to writer.

func (*BaseEncoder) WriteGeoJSON

func (e *BaseEncoder) WriteGeoJSON(w io.Writer, g *FeatureCollection) error

WriteGeoJSON write geometry to writer .

type Feature

type Feature struct {
	ID         interface{} `json:"id,omitempty"`
	Type       string      `json:"type"`
	BBox       BBox        `json:"bbox,omitempty"`
	Geometry   Geometry    `json:"geometry"`
	Properties Properties  `json:"properties"`
}

A Feature corresponds to GeoJSON feature object

func NewFeature

func NewFeature(geometry Geometry) *Feature

NewFeature creates and initializes a GeoJSON feature given the required attributes.

func UnmarshalFeature

func UnmarshalFeature(data []byte) (*Feature, error)

UnmarshalFeature decodes the data into a GeoJSON feature. Alternately one can call json.Unmarshal(f) directly for the same result.

func (Feature) MarshalJSON

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

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) UnmarshalJSON

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

UnmarshalJSON handles the correct unmarshalling of the data into the geoos.Geometry types.

type FeatureCollection

type FeatureCollection struct {
	Type     string     `json:"type"`
	BBox     BBox       `json:"bbox,omitempty"`
	Features []*Feature `json:"features"`
}

A FeatureCollection correlates to a GeoJSON feature collection.

func GeometryToFeatureCollection

func GeometryToFeatureCollection(geom space.Geometry) *FeatureCollection

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.

func (*FeatureCollection) Append

func (fc *FeatureCollection) Append(feature *Feature) *FeatureCollection

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

func (*FeatureCollection) String added in v1.1.1

func (fc *FeatureCollection) String() string

String returns string.

type GeojsonEncoder

type GeojsonEncoder struct {
	BaseEncoder
}

func (*GeojsonEncoder) Decode

func (e *GeojsonEncoder) Decode(s []byte) (space.Geometry, error)

Decode Returns geometry of that decode string by codeType.

func (*GeojsonEncoder) Encode

func (e *GeojsonEncoder) Encode(g space.Geometry) []byte

Encode Returns string of that encode geometry by codeType.

func (*GeojsonEncoder) Read

func (e *GeojsonEncoder) Read(r io.Reader) (space.Geometry, error)

Read Returns geometry from reader.

func (*GeojsonEncoder) ReadGeoJSON

func (e *GeojsonEncoder) ReadGeoJSON(r io.Reader) (*FeatureCollection, error)

ReadGeoJSON Returns geometry from reader by codeType.

func (*GeojsonEncoder) Write

func (e *GeojsonEncoder) Write(w io.Writer, g space.Geometry) error

Write write geometry to reader.

func (*GeojsonEncoder) WriteGeoJSON

func (e *GeojsonEncoder) WriteGeoJSON(w io.Writer, g *FeatureCollection) error

WriteGeoJSON write geometry to writer by codeType.

type Geometry

type Geometry struct {
	Type        string         `json:"type"`
	Coordinates space.Geometry `json:"coordinates,omitempty"`
	Geometries  []*Geometry    `json:"geometries,omitempty"`
}

A Geometry matches the structure of a GeoJSON Geometry.

func NewGeometry

func NewGeometry(g space.Geometry) *Geometry

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

func UnmarshalGeometry(data []byte) (*Geometry, error)

UnmarshalGeometry decodes the data into a GeoJSON feature. Alternately one can call json.Unmarshal(g) directly for the same result.

func (Geometry) Geometry

func (g Geometry) Geometry() space.Geometry

Geometry returns the space.Geometry for the geojson Geometry. This will convert the "Geometries" into a space.Collection if applicable.

func (Geometry) MarshalJSON

func (g Geometry) MarshalJSON() ([]byte, error)

MarshalJSON will marshal the geometry into the correct json structure.

func (*Geometry) UnmarshalJSON

func (g *Geometry) UnmarshalJSON(data []byte) error

UnmarshalJSON will unmarshal the correct geometry from the json structure.

type LineString

type LineString space.LineString

A LineString is a helper type that will marshal to/from a GeoJSON LineString geometry.

func (LineString) Geometry

func (ls LineString) Geometry() space.Geometry

Geometry will return the Geometry version of the data.

func (LineString) MarshalJSON

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

MarshalJSON will convert the LineString into a GeoJSON LineString geometry.

func (*LineString) UnmarshalJSON

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

UnmarshalJSON will unmarshal the GeoJSON MultiPoint geometry.

type MultiLineString

type MultiLineString space.MultiLineString

A MultiLineString is a helper type that will marshal to/from a GeoJSON MultiLineString geometry.

func (MultiLineString) Geometry

func (mls MultiLineString) Geometry() space.Geometry

Geometry will return the space.Geometry version of the data.

func (MultiLineString) MarshalJSON

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

MarshalJSON will convert the MultiLineString into a GeoJSON MultiLineString geometry.

func (*MultiLineString) UnmarshalJSON

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

UnmarshalJSON will unmarshal the GeoJSON MultiPoint geometry.

type MultiPoint

type MultiPoint space.MultiPoint

A MultiPoint is a helper type that will marshal to/from a GeoJSON MultiPoint geometry.

func (MultiPoint) Geometry

func (mp MultiPoint) Geometry() space.Geometry

Geometry will return the space.Geometry version of the data.

func (MultiPoint) MarshalJSON

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

MarshalJSON will convert the MultiPoint into a GeoJSON MultiPoint geometry.

func (*MultiPoint) UnmarshalJSON

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

UnmarshalJSON will unmarshal the GeoJSON MultiPoint geometry.

type MultiPolygon

type MultiPolygon space.MultiPolygon

A MultiPolygon is a helper type that will marshal to/from a GeoJSON MultiPolygon geometry.

func (MultiPolygon) Geometry

func (mp MultiPolygon) Geometry() space.Geometry

Geometry will return the space.Geometry version of the data.

func (MultiPolygon) MarshalJSON

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

MarshalJSON will convert the MultiPolygon into a GeoJSON MultiPolygon geometry.

func (*MultiPolygon) UnmarshalJSON

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

UnmarshalJSON will unmarshal the GeoJSON MultiPolygon geometry.

type Point

type Point space.Point

A Point is a helper type that will marshal to/from a GeoJSON Point geometry.

func (Point) Geometry

func (p Point) Geometry() space.Geometry

Geometry will return the space.Geometry version of the data.

func (Point) MarshalJSON

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

MarshalJSON will convert the Point into a GeoJSON Point geometry.

func (*Point) UnmarshalJSON

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

UnmarshalJSON will unmarshal the GeoJSON Point geometry.

type Polygon

type Polygon space.Polygon

A Polygon is a helper type that will marshal to/from a GeoJSON Polygon geometry.

func (Polygon) Geometry

func (p Polygon) Geometry() space.Geometry

Geometry will return the space.Geometry version of the data.

func (Polygon) MarshalJSON

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

MarshalJSON will convert the Polygon into a GeoJSON Polygon geometry.

func (*Polygon) UnmarshalJSON

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

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.

Jump to

Keyboard shortcuts

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