spatial

package
v0.0.0-...-85aa7e7 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2022 License: Apache-2.0 Imports: 13 Imported by: 0

README

lib/spatial

lib/spatial is a small Golang library for handling geospatial data.

Its focus is on serializing/deserializing into standardized data formats: Currently it supports GeoJSON, WKB and TinyWKB (TWKB).

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorEmptyGeomType = errors.New("empty geometry type")
)

Functions

func LineSegmentToCarthesian

func LineSegmentToCarthesian(p1, p2 Point) (a, b, c float64)

LineSegmentToCarthesian converts a line segment into a carthesian representation. Possible improvement: normalization of values

Types

type BBox

type BBox struct {
	SW, NE Point
}

func (*BBox) ExtendWith

func (b1 *BBox) ExtendWith(b2 BBox)

func (BBox) FullyIn

func (b BBox) FullyIn(b2 BBox) bool

func (BBox) Overlaps

func (b BBox) Overlaps(b2 BBox) bool

In determines if the bboxes overlap.

func (BBox) Segments

func (b BBox) Segments() []Segment

type ChunkedDecoder

type ChunkedDecoder interface {
	ChunkedDecode(io.Reader) (Chunks, error)
}

type ChunkedEncoder

type ChunkedEncoder interface {
	EncodeChunk(io.Writer, *FeatureCollection) error
	Close(io.Writer) error
}

type Chunks

type Chunks interface {
	Next() bool
	Scan(fc *FeatureCollection) error
}

type Clippable

type Clippable interface {
	// TODO: consider returning primitive geom, instead of Geom
	ClipToBBox(BBox) []Geom
}

type Codec

type Codec interface {
	Extensions() []string
}

A Codec needs to be able to tell which file extensions (e.g. "geojson") are commonly used to persist files. Moreover a Codec SHOULD either implement a Decoder or Encoder.

type ConvertFunc

type ConvertFunc func(Point) Point

type Decoder

type Decoder interface {
	Decode(io.Reader, *FeatureCollection) error
}

type Encoder

type Encoder interface {
	Encode(io.Writer, *FeatureCollection) error
}

type Feature

type Feature struct {
	Props    map[string]interface{} `json:"properties"`
	Geometry Geom
}

Feature is a data structure which holds geometry and tags/properties of a geographical feature.

func MergeFeatures

func MergeFeatures(fts []Feature) []Feature

MergeFeatures aggregates features that have the same properties, if possible.

func NewFeature

func NewFeature() Feature

func (Feature) MarshalJSON

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

func (*Feature) MarshalWKB

func (f *Feature) MarshalWKB() ([]byte, error)

func (*Feature) Properties

func (f *Feature) Properties() map[string]interface{}

type FeatureCollection

type FeatureCollection struct {
	Features []Feature `json:"features"`
	SRID     string    `json:"-"`

} // TODO: consider adding properties field

func NewFeatureCollection

func NewFeatureCollection() *FeatureCollection

func (*FeatureCollection) Filter

func (fc *FeatureCollection) Filter(bbox BBox) []Feature

func (FeatureCollection) MarshalJSON

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

Deprecated. Please use geojson.Codec.

func (*FeatureCollection) Reset

func (fc *FeatureCollection) Reset()

Reset removes all features.

type Filterable

type Filterable interface {
	Filter(BBox) []Feature
}

type Geom

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

func GeomFromWKB

func GeomFromWKB(r io.Reader) (Geom, error)

func MustNewGeom

func MustNewGeom(g interface{}) Geom

func NewGeom

func NewGeom(g interface{}) (Geom, error)

func (*Geom) BBox

func (g *Geom) BBox() BBox

func (*Geom) ClipToBBox

func (g *Geom) ClipToBBox(bbox BBox) []Geom

Clips a geometry and returns a cropped copy. Returns a slice, because clip might result in multiple sub-Geoms.

func (*Geom) Copy

func (g *Geom) Copy() Geom

func (*Geom) LineString

func (g *Geom) LineString() (Line, error)

func (Geom) MarshalJSON

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

func (Geom) MarshalWKB

func (g Geom) MarshalWKB() ([]byte, error)

TODO: maybe MarshalWKB could take an io.Writer instead of returning a buffer?

func (*Geom) MustLineString

func (g *Geom) MustLineString() Line

func (*Geom) MustPoint

func (g *Geom) MustPoint() *Point

func (*Geom) MustPolygon

func (g *Geom) MustPolygon() Polygon

func (*Geom) Overlaps

func (g *Geom) Overlaps(bbox BBox) bool

func (*Geom) Point

func (g *Geom) Point() (*Point, error)

Point retruns the geometry as a point (check type with Typ()) first.

func (*Geom) Polygon

func (g *Geom) Polygon() (Polygon, error)

func (Geom) Project

func (g Geom) Project(fn ConvertFunc)

func (Geom) Simplify

func (g Geom) Simplify(e float64) Geom

func (Geom) String

func (g Geom) String() string

func (*Geom) Typ

func (g *Geom) Typ() GeomType

Typ returns the geometries type.

func (*Geom) UnmarshalJSON

func (g *Geom) UnmarshalJSON(buf []byte) error

func (*Geom) UnmarshalJSONCoords

func (g *Geom) UnmarshalJSONCoords(typ string, inner json.RawMessage) error

func (*Geom) UnmarshalWKB

func (g *Geom) UnmarshalWKB(r io.Reader) error

func (*Geom) ValidTopology

func (g *Geom) ValidTopology() bool

type GeomType

type GeomType uint32
const (
	GeomTypeEmpty      GeomType = 0
	GeomTypePoint      GeomType = 1
	GeomTypeLineString GeomType = 2
	GeomTypePolygon    GeomType = 3
	GeomTypeInvalid
)

func (GeomType) String

func (g GeomType) String() string

type Line

type Line []Point

func MergeLines

func MergeLines(l1, l2 Line) Line

MergeLines is deprecated.

func NewLinesFromSegments

func NewLinesFromSegments(segs []Segment) []Line

NewLinesFromSegments creates a line from continous segments.

func (Line) Area

func (l Line) Area() float64

func (Line) BBox

func (l Line) BBox() BBox

func (Line) Center

func (l Line) Center() Point

func (Line) ClipToBBox

func (l Line) ClipToBBox(bbox BBox) []Geom

func (Line) Clockwise

func (l Line) Clockwise() bool

func (Line) Closed

func (l Line) Closed() bool

func (Line) Copy

func (l Line) Copy() Projectable

func (Line) Intersections

func (l Line) Intersections(segments []Segment) []Point

func (Line) IsExtendedBy

func (l1 Line) IsExtendedBy(l2 Line) bool

func (Line) Project

func (l Line) Project(proj ConvertFunc)

func (Line) Reverse

func (l Line) Reverse()

func (Line) Segments

func (l Line) Segments() []Segment

Segments splits a Line into Segments (line with two points).

func (Line) SegmentsWithClosing

func (l Line) SegmentsWithClosing() []Segment

func (Line) Simplify

func (l Line) Simplify(e float64) Line

Simplify returns a simplified copy of the Line using the Ramer-Douglas-Peucker algorithm.

func (Line) String

func (l Line) String() string

type Point

type Point struct{ X, Y float64 }

func (Point) ClipToBBox

func (p Point) ClipToBBox(b BBox) []Geom

func (Point) Copy

func (p Point) Copy() Projectable

func (*Point) HaversineDistance

func (p *Point) HaversineDistance(p2 *Point) float64

DistanceTo returns the distance in meters between the points.

func (Point) InBBox

func (p Point) InBBox(b BBox) bool

func (Point) InPolygon

func (p Point) InPolygon(poly Polygon) bool

func (Point) MarshalJSON

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

func (*Point) Project

func (p *Point) Project(proj ConvertFunc)

func (Point) RoundedCoords

func (p Point) RoundedCoords() Point

func (*Point) SetX

func (p *Point) SetX(x float64)

func (*Point) SetY

func (p *Point) SetY(y float64)

func (Point) String

func (p Point) String() string

func (*Point) UnmarshalJSON

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

type Polygon

type Polygon []Line

Polygon is a data type for storing simple polygons.

func (Polygon) ClipToBBox

func (p Polygon) ClipToBBox(bbox BBox) []Geom

func (Polygon) Copy

func (p Polygon) Copy() Projectable

func (Polygon) FixWinding

func (p Polygon) FixWinding()

func (Polygon) MustJSON

func (p Polygon) MustJSON() []byte

func (Polygon) Project

func (p Polygon) Project(proj ConvertFunc)

func (Polygon) Rewind

func (p Polygon) Rewind()

func (Polygon) String

func (p Polygon) String() string

func (Polygon) ValidTopology

func (p Polygon) ValidTopology() bool

type Projectable

type Projectable interface {
	Project(ConvertFunc)
	Copy() Projectable
}

type PropertyRetriever

type PropertyRetriever interface {
	Properties() map[string]interface{}
}

type RTreeCollection

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

RTreeCollection is a FeatureCollection which is backed by a rtree.

func NewRTreeCollection

func NewRTreeCollection(features ...Feature) *RTreeCollection

func (*RTreeCollection) Add

func (rt *RTreeCollection) Add(feature Feature)

func (*RTreeCollection) Filter

func (rt *RTreeCollection) Filter(bbox BBox) []Feature

type Segment

type Segment [2]Point

func BBoxBorders

func BBoxBorders(sw, ne Point) []Segment

BBoxBorders returns the lines which describe the rectangle of the BBox. Segments are returned in counter-clockwise order.

func (Segment) Bearing

func (s Segment) Bearing() float64

Bearing returns the heading direction between the first and the second point in degrees.

func (*Segment) ClipToBBox

func (s *Segment) ClipToBBox(sw, ne Point) []Segment

ClipToBBox returns 0 or 1 Segment which is inside bbox.

func (Segment) DistanceToPt

func (s Segment) DistanceToPt(p Point) float64

DistanceToPt determines the Segment's perpendicular distance to the point.

func (*Segment) FullyInBBox

func (s *Segment) FullyInBBox(sw, ne Point) bool

func (*Segment) HasPoint

func (s *Segment) HasPoint(pt Point) bool

func (*Segment) Intersection

func (s *Segment) Intersection(s2 Segment) (Point, bool)

LineIntersect returns a point where the two lines intersect and whether the point is on both segments.

func (*Segment) JSON

func (s *Segment) JSON() []byte

func (*Segment) Length

func (s *Segment) Length() float64

func (*Segment) SplitAt

func (s *Segment) SplitAt(p Point) (Segment, Segment)

type Validatable

type Validatable interface {
	ValidTopology() bool
}

type WKBable

type WKBable interface {
	MarshalWKB() ([]byte, error)
}

type WKBableWithProps

type WKBableWithProps interface {
	WKBable
	PropertyRetriever
}

Jump to

Keyboard shortcuts

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