geom

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2023 License: BSD-2-Clause Imports: 3 Imported by: 1

README

go-geom

Build Status GoDoc Go Report Card

Package geom implements efficient geometry types for geospatial applications.

Key features

  • OpenGeo Consortium-style geometries.
  • Support for 2D and 3D geometries, measures (time and/or distance), and unlimited extra dimensions.
  • Encoding and decoding of common geometry formats (GeoJSON, KML, WKB, and others) including sql.Scanner and driver.Value interface implementations for easy database integration.
  • 2D and 3D topology functions.
  • Efficient, cache-friendly internal representation.

Detailed features

Geometry types
Encoding and decoding
Geometry functions
  • XY 2D geometry functions
  • XYZ 3D geometry functions

License

Documentation

Overview

Package geom implements efficient geometry types for geospatial applications.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bounds

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

A Bounds represents a multi-dimensional bounding box.

func NewBounds

func NewBounds(layout Layout) *Bounds

NewBounds creates a new Bounds.

func (*Bounds) Clone

func (b *Bounds) Clone() *Bounds

Clone returns a deep copy of b.

func (*Bounds) Extend

func (b *Bounds) Extend(g T) *Bounds

Extend extends b to include geometry g.

func (*Bounds) IsEmpty

func (b *Bounds) IsEmpty() bool

IsEmpty returns true if b is empty.

func (*Bounds) Layout

func (b *Bounds) Layout() Layout

Layout returns b's layout.

func (*Bounds) Max

func (b *Bounds) Max(dim int) float64

Max returns the maximum value in dimension dim.

func (*Bounds) Min

func (b *Bounds) Min(dim int) float64

Min returns the minimum value in dimension dim.

func (*Bounds) Overlaps

func (b *Bounds) Overlaps(layout Layout, b2 *Bounds) bool

Overlaps returns true if b overlaps b2 in layout.

func (*Bounds) OverlapsPoint

func (b *Bounds) OverlapsPoint(layout Layout, point Coord) bool

OverlapsPoint determines if the bounding box overlaps the point (point is within or on the border of the bounds)

func (*Bounds) Polygon

func (b *Bounds) Polygon() *Polygon

Polygon returns b as a two-dimensional Polygon.

func (*Bounds) Set

func (b *Bounds) Set(args ...float64) *Bounds

Set sets the minimum and maximum values. args must be an even number of values: the first half are the minimum values for each dimension and the second half are the maximum values for each dimension. If necessary, the layout of b will be extended to cover all the supplied dimensions implied by args.

func (*Bounds) SetCoords

func (b *Bounds) SetCoords(min, max Coord) *Bounds

SetCoords sets the minimum and maximum values of the Bounds.

type Coord

type Coord []float64

A Coord represents an N-dimensional coordinate.

func (Coord) Clone

func (c Coord) Clone() Coord

Clone returns a deep copy of c.

func (Coord) Equal

func (c Coord) Equal(layout Layout, other Coord) bool

Equal compares that all ordinates are the same in this and the other coords. It is assumed that this coord and other coord both have the same (provided) layout.

func (Coord) Set

func (c Coord) Set(other Coord)

Set copies the ordinate data from the other coord to this coord.

func (Coord) X

func (c Coord) X() float64

X returns the x coordinate of the coordinate. X is assumed to be the first ordinate.

func (Coord) Y

func (c Coord) Y() float64

Y returns the x coordinate of the coordinate. Y is assumed to be the second ordinate.

type ErrLayoutMismatch

type ErrLayoutMismatch struct {
	Got  Layout
	Want Layout
}

An ErrLayoutMismatch is returned when geometries with different layouts cannot be combined.

func (ErrLayoutMismatch) Error

func (e ErrLayoutMismatch) Error() string

type ErrStrideMismatch

type ErrStrideMismatch struct {
	Got  int
	Want int
}

An ErrStrideMismatch is returned when the stride does not match the expected stride.

func (ErrStrideMismatch) Error

func (e ErrStrideMismatch) Error() string

type ErrUnsupportedLayout

type ErrUnsupportedLayout Layout

An ErrUnsupportedLayout is returned when the requested layout is not supported.

func (ErrUnsupportedLayout) Error

func (e ErrUnsupportedLayout) Error() string

type ErrUnsupportedType

type ErrUnsupportedType struct {
	Value interface{}
}

An ErrUnsupportedType is returned when the requested type is not supported.

func (ErrUnsupportedType) Error

func (e ErrUnsupportedType) Error() string

type GeometryCollection

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

A GeometryCollection is a collection of arbitrary geometries with the same SRID.

func NewGeometryCollection

func NewGeometryCollection() *GeometryCollection

NewGeometryCollection returns a new GeometryCollection with the specified geometries.

func (*GeometryCollection) Bounds

func (gc *GeometryCollection) Bounds() *Bounds

Bounds returns the bounds of all the geometries in gc.

func (*GeometryCollection) Empty

func (gc *GeometryCollection) Empty() bool

Empty returns true if the collection is empty.

func (*GeometryCollection) Ends

func (*GeometryCollection) Ends() []int

Ends panics.

func (*GeometryCollection) Endss

func (*GeometryCollection) Endss() [][]int

Endss panics.

func (*GeometryCollection) FlatCoords

func (*GeometryCollection) FlatCoords() []float64

FlatCoords panics.

func (*GeometryCollection) Geom

func (gc *GeometryCollection) Geom(i int) T

Geom returns the ith geometry in gc.

func (*GeometryCollection) Geoms

func (gc *GeometryCollection) Geoms() []T

Geoms returns the geometries in gc.

func (*GeometryCollection) Layout

func (gc *GeometryCollection) Layout() Layout

Layout returns the smallest layout that covers all of the layouts in gc's geometries.

func (*GeometryCollection) MustPush

func (gc *GeometryCollection) MustPush(gs ...T) *GeometryCollection

MustPush pushes gs to gc. It panics on any error.

func (*GeometryCollection) NumGeoms

func (gc *GeometryCollection) NumGeoms() int

NumGeoms returns the number of geometries in gc.

func (*GeometryCollection) Push

func (gc *GeometryCollection) Push(gs ...T) error

Push appends geometries.

func (*GeometryCollection) SRID

func (gc *GeometryCollection) SRID() int

SRID returns gc's SRID.

func (*GeometryCollection) SetSRID

func (gc *GeometryCollection) SetSRID(srid int) *GeometryCollection

SetSRID sets gc's SRID and the SRID of all its elements.

func (*GeometryCollection) Stride

func (gc *GeometryCollection) Stride() int

Stride returns the stride of gc's layout.

type Layout

type Layout int

A Layout describes the meaning of an N-dimensional coordinate. Layout(N) for N > 4 is a valid layout, in which case the first dimensions are interpreted to be X, Y, Z, and M and extra dimensions have no special meaning. M values are considered part of a linear referencing system (e.g. classical time or distance along a path). 1-dimensional layouts are not supported.

const (
	// NoLayout is an unknown layout
	NoLayout Layout = iota
	// XY is a 2D layout (X and Y)
	XY
	// XYZ is 3D layout (X, Y, and Z)
	XYZ
	// XYM is a 2D layout with an M value
	XYM
	// XYZM is a 3D layout with an M value
	XYZM
)

func (Layout) MIndex

func (l Layout) MIndex() int

MIndex returns the index of the M dimension, or -1 if the l does not have an M dimension.

func (Layout) Stride

func (l Layout) Stride() int

Stride returns l's number of dimensions.

func (Layout) String

func (l Layout) String() string

String returns a human-readable string representing l.

func (Layout) ZIndex

func (l Layout) ZIndex() int

ZIndex returns the index of l's Z dimension, or -1 if l does not have a Z dimension.

type LineString

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

A LineString represents a single, unbroken line, linearly interpreted between zero or more control points.

func NewLineString

func NewLineString(l Layout) *LineString

NewLineString returns a new LineString with layout l and no control points.

func NewLineStringFlat

func NewLineStringFlat(layout Layout, flatCoords []float64) *LineString

NewLineStringFlat returns a new LineString with layout l and control points flatCoords.

func (*LineString) Area

func (ls *LineString) Area() float64

Area returns the length of ls, i.e. zero.

func (*LineString) Clone

func (ls *LineString) Clone() *LineString

Clone returns a copy of ls that does not alias ls.

func (*LineString) Coord

func (g *LineString) Coord(i int) Coord

func (*LineString) Coords

func (g *LineString) Coords() []Coord

func (*LineString) Empty

func (ls *LineString) Empty() bool

Empty returns false.

func (*LineString) Interpolate

func (ls *LineString) Interpolate(val float64, dim int) (int, float64)

Interpolate returns the index and delta of val in dimension dim.

func (*LineString) Length

func (ls *LineString) Length() float64

Length returns the length of ls.

func (*LineString) MustSetCoords

func (ls *LineString) MustSetCoords(coords []Coord) *LineString

MustSetCoords is like SetCoords but it panics on any error.

func (*LineString) NumCoords

func (g *LineString) NumCoords() int

func (*LineString) SetCoords

func (ls *LineString) SetCoords(coords []Coord) (*LineString, error)

SetCoords sets the coordinates of ls.

func (*LineString) SetSRID

func (ls *LineString) SetSRID(srid int) *LineString

SetSRID sets the SRID of ls.

func (*LineString) SubLineString

func (ls *LineString) SubLineString(start, stop int) *LineString

SubLineString returns a LineString from starts at index start and stops at index stop of ls. The returned LineString aliases ls.

func (*LineString) Swap

func (ls *LineString) Swap(ls2 *LineString)

Swap swaps the values of ls and ls2.

type LinearRing

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

A LinearRing is a linear ring.

func NewLinearRing

func NewLinearRing(layout Layout) *LinearRing

NewLinearRing returns a new LinearRing with no coordinates.

func NewLinearRingFlat

func NewLinearRingFlat(layout Layout, flatCoords []float64) *LinearRing

NewLinearRingFlat returns a new LinearRing with the given flat coordinates.

func (*LinearRing) Area

func (lr *LinearRing) Area() float64

Area returns the the area.

func (*LinearRing) Clone

func (lr *LinearRing) Clone() *LinearRing

Clone returns a deep copy.

func (*LinearRing) ContainCoord

func (lr *LinearRing) ContainCoord(pt *Coord) bool

ContainCoord checks if LinearRing contains Coord.

func (*LinearRing) Coord

func (g *LinearRing) Coord(i int) Coord

func (*LinearRing) Coords

func (g *LinearRing) Coords() []Coord

func (*LinearRing) Empty

func (lr *LinearRing) Empty() bool

Empty returns false.

func (*LinearRing) Length

func (lr *LinearRing) Length() float64

Length returns the length of the perimeter.

func (*LinearRing) MustSetCoords

func (lr *LinearRing) MustSetCoords(coords []Coord) *LinearRing

MustSetCoords sets the coordinates and panics if there is any error.

func (*LinearRing) NumCoords

func (g *LinearRing) NumCoords() int

func (*LinearRing) SetCoords

func (lr *LinearRing) SetCoords(coords []Coord) (*LinearRing, error)

SetCoords sets the coordinates.

func (*LinearRing) SetSRID

func (lr *LinearRing) SetSRID(srid int) *LinearRing

SetSRID sets the SRID of lr.

func (*LinearRing) Swap

func (lr *LinearRing) Swap(lr2 *LinearRing)

Swap swaps the values of lr and lr2.

type MultiLineString

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

A MultiLineString is a collection of LineStrings.

func NewMultiLineString

func NewMultiLineString(layout Layout) *MultiLineString

NewMultiLineString returns a new MultiLineString with no LineStrings.

func NewMultiLineStringFlat

func NewMultiLineStringFlat(layout Layout, flatCoords []float64, ends []int) *MultiLineString

NewMultiLineStringFlat returns a new MultiLineString with the given flat coordinates.

func (*MultiLineString) Area

func (mls *MultiLineString) Area() float64

Area returns 0.

func (*MultiLineString) Clone

func (mls *MultiLineString) Clone() *MultiLineString

Clone returns a deep copy.

func (*MultiLineString) Coords

func (g *MultiLineString) Coords() [][]Coord

func (*MultiLineString) Empty

func (mls *MultiLineString) Empty() bool

Empty returns true if the collection is empty.

func (*MultiLineString) Ends

func (g *MultiLineString) Ends() []int

func (*MultiLineString) Length

func (mls *MultiLineString) Length() float64

Length returns the sum of the length of the LineStrings.

func (*MultiLineString) LineString

func (mls *MultiLineString) LineString(i int) *LineString

LineString returns the ith LineString.

func (*MultiLineString) MustSetCoords

func (mls *MultiLineString) MustSetCoords(coords [][]Coord) *MultiLineString

MustSetCoords sets the coordinates and panics on any error.

func (*MultiLineString) NumLineStrings

func (mls *MultiLineString) NumLineStrings() int

NumLineStrings returns the number of LineStrings.

func (*MultiLineString) Push

func (mls *MultiLineString) Push(ls *LineString) error

Push appends a LineString.

func (*MultiLineString) SetCoords

func (mls *MultiLineString) SetCoords(coords [][]Coord) (*MultiLineString, error)

SetCoords sets the coordinates.

func (*MultiLineString) SetSRID

func (mls *MultiLineString) SetSRID(srid int) *MultiLineString

SetSRID sets the SRID of mls.

func (*MultiLineString) Swap

func (mls *MultiLineString) Swap(mls2 *MultiLineString)

Swap swaps the values of mls and mls2.

type MultiPoint

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

A MultiPoint is a collection of Points.

func NewMultiPoint

func NewMultiPoint(layout Layout) *MultiPoint

NewMultiPoint returns a new, empty, MultiPoint.

func NewMultiPointFlat

func NewMultiPointFlat(layout Layout, flatCoords []float64) *MultiPoint

NewMultiPointFlat returns a new MultiPoint with the given flat coordinates.

func (*MultiPoint) Area

func (mp *MultiPoint) Area() float64

Area returns zero.

func (*MultiPoint) Clone

func (mp *MultiPoint) Clone() *MultiPoint

Clone returns a deep copy.

func (*MultiPoint) Coord

func (g *MultiPoint) Coord(i int) Coord

func (*MultiPoint) Coords

func (g *MultiPoint) Coords() []Coord

func (*MultiPoint) Empty

func (mp *MultiPoint) Empty() bool

Empty returns true if the collection is empty.

func (*MultiPoint) Length

func (mp *MultiPoint) Length() float64

Length returns zero.

func (*MultiPoint) MustSetCoords

func (mp *MultiPoint) MustSetCoords(coords []Coord) *MultiPoint

MustSetCoords sets the coordinates and panics on any error.

func (*MultiPoint) NumCoords

func (g *MultiPoint) NumCoords() int

func (*MultiPoint) NumPoints

func (mp *MultiPoint) NumPoints() int

NumPoints returns the number of Points.

func (*MultiPoint) Point

func (mp *MultiPoint) Point(i int) *Point

Point returns the ith Point.

func (*MultiPoint) Push

func (mp *MultiPoint) Push(p *Point) error

Push appends a point.

func (*MultiPoint) SetCoords

func (mp *MultiPoint) SetCoords(coords []Coord) (*MultiPoint, error)

SetCoords sets the coordinates.

func (*MultiPoint) SetSRID

func (mp *MultiPoint) SetSRID(srid int) *MultiPoint

SetSRID sets the SRID of mp.

func (*MultiPoint) Swap

func (mp *MultiPoint) Swap(mp2 *MultiPoint)

Swap swaps the values of mp and mp2.

type MultiPolygon

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

A MultiPolygon is a collection of Polygons.

func NewMultiPolygon

func NewMultiPolygon(layout Layout) *MultiPolygon

NewMultiPolygon returns a new MultiPolygon with no Polygons.

func NewMultiPolygonFlat

func NewMultiPolygonFlat(layout Layout, flatCoords []float64, endss [][]int) *MultiPolygon

NewMultiPolygonFlat returns a new MultiPolygon with the given flat coordinates.

func (*MultiPolygon) Area

func (mp *MultiPolygon) Area() float64

Area returns the sum of the area of the individual Polygons.

func (*MultiPolygon) Clone

func (mp *MultiPolygon) Clone() *MultiPolygon

Clone returns a deep copy.

func (*MultiPolygon) Coords

func (g *MultiPolygon) Coords() [][][]Coord

func (*MultiPolygon) Empty

func (mp *MultiPolygon) Empty() bool

Empty returns true if the collection is empty.

func (*MultiPolygon) Endss

func (g *MultiPolygon) Endss() [][]int

func (*MultiPolygon) Length

func (mp *MultiPolygon) Length() float64

Length returns the sum of the perimeters of the Polygons.

func (*MultiPolygon) MustSetCoords

func (mp *MultiPolygon) MustSetCoords(coords [][][]Coord) *MultiPolygon

MustSetCoords sets the coordinates and panics on any error.

func (*MultiPolygon) NumPolygons

func (mp *MultiPolygon) NumPolygons() int

NumPolygons returns the number of Polygons.

func (*MultiPolygon) Polygon

func (mp *MultiPolygon) Polygon(i int) *Polygon

Polygon returns the ith Polygon.

func (*MultiPolygon) Push

func (mp *MultiPolygon) Push(p *Polygon) error

Push appends a Polygon.

func (*MultiPolygon) SetCoords

func (mp *MultiPolygon) SetCoords(coords [][][]Coord) (*MultiPolygon, error)

SetCoords sets the coordinates.

func (*MultiPolygon) SetSRID

func (mp *MultiPolygon) SetSRID(srid int) *MultiPolygon

SetSRID sets the SRID of mp.

func (*MultiPolygon) Swap

func (mp *MultiPolygon) Swap(mp2 *MultiPolygon)

Swap swaps the values of mp and mp2.

type Point

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

A Point represents a single point.

func NewPoint

func NewPoint(l Layout) *Point

NewPoint allocates a new Point with layout l and all values zero.

func NewPointFlat

func NewPointFlat(l Layout, flatCoords []float64) *Point

NewPointFlat allocates a new Point with layout l and flat coordinates flatCoords.

func (*Point) Area

func (p *Point) Area() float64

Area returns p's area, i.e. zero.

func (*Point) Bounds

func (g *Point) Bounds() *Bounds

func (*Point) Clone

func (p *Point) Clone() *Point

Clone returns a copy of p that does not alias p.

func (*Point) Coords

func (g *Point) Coords() Coord

func (*Point) Empty

func (p *Point) Empty() bool

Empty returns false.

func (*Point) Ends

func (g *Point) Ends() []int

func (*Point) Endss

func (g *Point) Endss() [][]int

func (*Point) FlatCoords

func (g *Point) FlatCoords() []float64

func (*Point) Layout

func (g *Point) Layout() Layout

func (*Point) Length

func (p *Point) Length() float64

Length returns the length of p, i.e. zero.

func (*Point) M

func (p *Point) M() float64

M returns p's M-coordinate, or zero if p has no M-coordinate.

func (*Point) MustSetCoords

func (p *Point) MustSetCoords(coords Coord) *Point

MustSetCoords is like SetCoords but panics on any error.

func (*Point) NumCoords

func (g *Point) NumCoords() int

func (*Point) Reserve

func (g *Point) Reserve(n int)

func (*Point) SRID

func (g *Point) SRID() int

func (*Point) SetCoords

func (p *Point) SetCoords(coords Coord) (*Point, error)

SetCoords sets the coordinates of p.

func (*Point) SetSRID

func (p *Point) SetSRID(srid int) *Point

SetSRID sets the SRID of p.

func (*Point) Stride

func (g *Point) Stride() int

func (*Point) Swap

func (p *Point) Swap(p2 *Point)

Swap swaps the values of p and p2.

func (*Point) X

func (p *Point) X() float64

X returns p's X-coordinate.

func (*Point) Y

func (p *Point) Y() float64

Y returns p's Y-coordinate.

func (*Point) Z

func (p *Point) Z() float64

Z returns p's Z-coordinate, or zero if p has no Z-coordinate.

type Polygon

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

A Polygon represents a polygon as a collection of LinearRings. The first LinearRing is the outer boundary. Subsequent LinearRings are inner boundaries (holes).

func NewPolygon

func NewPolygon(layout Layout) *Polygon

NewPolygon returns a new, empty, Polygon.

Example
unitSquare := NewPolygon(XY).MustSetCoords([][]Coord{
	{{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}},
})
fmt.Printf("unitSquare.Area() == %f", unitSquare.Area())
Output:

unitSquare.Area() == 1.000000

func NewPolygonFlat

func NewPolygonFlat(layout Layout, flatCoords []float64, ends []int) *Polygon

NewPolygonFlat returns a new Polygon with the given flat coordinates.

func (*Polygon) Area

func (p *Polygon) Area() float64

Area returns the area.

func (*Polygon) Clone

func (p *Polygon) Clone() *Polygon

Clone returns a deep copy.

func (*Polygon) Contains

func (p *Polygon) Contains(lr *LinearRing) bool

Contains checks if polygon contains LinearRing.

func (*Polygon) Coords

func (g *Polygon) Coords() [][]Coord

func (*Polygon) Empty

func (p *Polygon) Empty() bool

Empty returns false.

func (*Polygon) Ends

func (g *Polygon) Ends() []int

func (*Polygon) Length

func (p *Polygon) Length() float64

Length returns the perimter.

func (*Polygon) LinearRing

func (p *Polygon) LinearRing(i int) *LinearRing

LinearRing returns the ith LinearRing.

func (*Polygon) MustSetCoords

func (p *Polygon) MustSetCoords(coords [][]Coord) *Polygon

MustSetCoords sets the coordinates and panics on any error.

func (*Polygon) NumLinearRings

func (p *Polygon) NumLinearRings() int

NumLinearRings returns the number of LinearRings.

func (*Polygon) Push

func (p *Polygon) Push(lr *LinearRing) error

Push appends a LinearRing.

func (*Polygon) SetCoords

func (p *Polygon) SetCoords(coords [][]Coord) (*Polygon, error)

SetCoords sets the coordinates.

func (*Polygon) SetSRID

func (p *Polygon) SetSRID(srid int) *Polygon

SetSRID sets the SRID of p.

func (*Polygon) Swap

func (p *Polygon) Swap(p2 *Polygon)

Swap swaps the values of p and p2.

type T

type T interface {
	Layout() Layout
	Stride() int
	Bounds() *Bounds
	FlatCoords() []float64
	Ends() []int
	Endss() [][]int
	SRID() int
}

T is a generic interface implemented by all geometry types.

func Must

func Must(g T, err error) T

Must panics if err is not nil, otherwise it returns g.

Directories

Path Synopsis
Package bigxy contains robust geographic functions on planar (xy) data.
Package bigxy contains robust geographic functions on planar (xy) data.
encoding
ewkb
Package ewkb implements Extended Well Known Binary encoding and decoding.
Package ewkb implements Extended Well Known Binary encoding and decoding.
ewkbhex
Package ewkbhex implements Extended Well Known Binary encoding and decoding of strings.
Package ewkbhex implements Extended Well Known Binary encoding and decoding of strings.
geojson
Package geojson implements GeoJSON encoding and decoding.
Package geojson implements GeoJSON encoding and decoding.
igc
Package igc implements an IGC parser.
Package igc implements an IGC parser.
kml
Package kml implements KML encoding.
Package kml implements KML encoding.
wkb
Package wkb implements Well Known Binary encoding and decoding.
Package wkb implements Well Known Binary encoding and decoding.
wkbcommon
Package wkbcommon contains code common to WKB and EWKB encoding.
Package wkbcommon contains code common to WKB and EWKB encoding.
wkbhex
Package wkbhex implements Well Known Binary encoding and decoding of strings.
Package wkbhex implements Well Known Binary encoding and decoding of strings.
wkt
internal
xy
Package xy contains low-level planar (xy) geographic functions.
Package xy contains low-level planar (xy) geographic functions.
Package xyz contains operations in 3d coordinate space.
Package xyz contains operations in 3d coordinate space.

Jump to

Keyboard shortcuts

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