geo

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2024 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Overview

Package geo contains the base types for spatial data type operations.

Index

Constants

View Source
const (
	// DefaultSRIDShouldOverwrite implies the parsing function should overwrite
	// the SRID with the defaultSRID.
	DefaultSRIDShouldOverwrite defaultSRIDOverwriteSetting = true
	// DefaultSRIDIsHint implies that the default SRID is only a hint
	// and if the SRID is provided by the given EWKT/EWKB, it should be
	// used instead.
	DefaultSRIDIsHint defaultSRIDOverwriteSetting = false
)
View Source
const DefaultGeoJSONDecimalDigits = 9

DefaultGeoJSONDecimalDigits is the default number of digits coordinates in GeoJSON.

View Source
const GeoHashAutoPrecision = 0

GeoHashAutoPrecision means to calculate the precision of SpatialObjectToGeoHash based on input, up to 32 characters.

View Source
const GeoHashMaxPrecision = 20

GeoHashMaxPrecision is the maximum precision for GeoHashes. 20 is picked as doubles have 51 decimals of precision, and each base32 position can contain 5 bits of data. As we have two points, we use floor((2 * 51) / 5) = 20.

Variables

View Source
var DefaultEWKBEncodingFormat = binary.LittleEndian

DefaultEWKBEncodingFormat is the default encoding format for EWKB.

Functions

func AdjustGeomTSRID

func AdjustGeomTSRID(t geom.T, srid geopb.SRID)

AdjustGeomTSRID adjusts the SRID of a given geom.T. Ideally SetSRID is an interface of geom.T, but that is not the case.

func GeomTContainsEmpty

func GeomTContainsEmpty(g geom.T) bool

GeomTContainsEmpty returns whether a geom.T contains any empty element.

func IsEmptyGeometryError

func IsEmptyGeometryError(err error) bool

IsEmptyGeometryError returns true if the error is of type EmptyGeometryError.

func IsLinearRingCCW

func IsLinearRingCCW(linearRing *geom.LinearRing) bool

IsLinearRingCCW returns whether a given linear ring is counter clock wise. See 2.07 of http://www.faqs.org/faqs/graphics/algorithms-faq/. "Find the lowest vertex (or, if there is more than one vertex with the same lowest coordinate,

the rightmost of those vertices) and then take the cross product of the edges fore and aft of it."

func NewMismatchingSRIDsError

func NewMismatchingSRIDsError(a geopb.SpatialObject, b geopb.SpatialObject) error

NewMismatchingSRIDsError returns the error message for SRIDs of GeospatialTypes a and b being a mismatch.

func NormalizeLatitudeDegrees

func NormalizeLatitudeDegrees(lat float64) float64

NormalizeLatitudeDegrees normalizes latitudes to the range [-90, 90].

func NormalizeLongitudeDegrees

func NormalizeLongitudeDegrees(lng float64) float64

NormalizeLongitudeDegrees normalizes longitude to the range [-180, 180].

func S2RegionsFromGeomT

func S2RegionsFromGeomT(geomRepr geom.T, emptyBehavior EmptyBehavior) ([]s2.Region, error)

S2RegionsFromGeomT converts an geom representation of an object to s2 regions. As S2 does not really handle empty geometries well, we need to ingest emptyBehavior and react appropriately.

func SpatialObjectFitsColumnMetadata

func SpatialObjectFitsColumnMetadata(
	so geopb.SpatialObject, srid geopb.SRID, shapeType geopb.ShapeType,
) error

SpatialObjectFitsColumnMetadata determines whether a GeospatialType is compatible with the given SRID and Shape. Returns an error if the types does not fit.

func SpatialObjectToEWKT

func SpatialObjectToEWKT(so geopb.SpatialObject, maxDecimalDigits int) (geopb.EWKT, error)

SpatialObjectToEWKT transforms a given SpatialObject to EWKT.

func SpatialObjectToGeoHash

func SpatialObjectToGeoHash(so geopb.SpatialObject, p int) (string, error)

SpatialObjectToGeoHash transforms a given SpatialObject to a GeoHash.

func SpatialObjectToGeoJSON

func SpatialObjectToGeoJSON(
	so geopb.SpatialObject, maxDecimalDigits int, flag SpatialObjectToGeoJSONFlag,
) ([]byte, error)

SpatialObjectToGeoJSON transforms a given SpatialObject to GeoJSON.

func SpatialObjectToKML

func SpatialObjectToKML(so geopb.SpatialObject) (string, error)

SpatialObjectToKML transforms a given SpatialObject to KML.

func SpatialObjectToWKB

func SpatialObjectToWKB(so geopb.SpatialObject, byteOrder binary.ByteOrder) (geopb.WKB, error)

SpatialObjectToWKB transforms a given SpatialObject to WKB.

func SpatialObjectToWKBHex

func SpatialObjectToWKBHex(so geopb.SpatialObject) (string, error)

SpatialObjectToWKBHex transforms a given SpatialObject to WKBHex.

func SpatialObjectToWKT

func SpatialObjectToWKT(so geopb.SpatialObject, maxDecimalDigits int) (geopb.WKT, error)

SpatialObjectToWKT transforms a given SpatialObject to WKT.

func StringToByteOrder

func StringToByteOrder(s string) binary.ByteOrder

StringToByteOrder returns the byte order of string.

func Summary

func Summary(
	t geom.T, hasBoundingBox bool, shape geopb.ShapeType, isGeography bool,
) (string, error)

Summary returns a text summary of the contents of the geometry type.

Flags shown square brackets after the geometry type have the following meaning: M: has M coordinate Z: has Z coordinate B: has a cached bounding box G: is geography S: has spatial reference system

func ToLowerSingleByte

func ToLowerSingleByte(b byte) byte

ToLowerSingleByte returns the lowercase of a given single ASCII byte. A non ASCII byte is returned unchanged.

Types

type CartesianBoundingBox

type CartesianBoundingBox struct {
	geopb.BoundingBox
}

CartesianBoundingBox is the cartesian BoundingBox representation, meant for use for GEOMETRY types.

func BoundingBoxFromGeomTGeometryType

func BoundingBoxFromGeomTGeometryType(g geom.T) *CartesianBoundingBox

BoundingBoxFromGeomTGeometryType returns an appropriate bounding box for a Geometry type.

func NewCartesianBoundingBox

func NewCartesianBoundingBox() *CartesianBoundingBox

NewCartesianBoundingBox returns a properly initialized empty bounding box for carestian plane types.

func ParseCartesianBoundingBox

func ParseCartesianBoundingBox(s string) (CartesianBoundingBox, error)

ParseCartesianBoundingBox parses a box2d string into a bounding box.

func ParseCartesianBoundingBoxFromGeoHash

func ParseCartesianBoundingBoxFromGeoHash(g string, precision int) (CartesianBoundingBox, error)

ParseCartesianBoundingBoxFromGeoHash converts a GeoHash to a CartesianBoundingBox.

func (*CartesianBoundingBox) AddPoint

AddPoint adds a point to the CartesianBoundingBox coordinates. Returns a copy of the CartesianBoundingBox.

func (*CartesianBoundingBox) Buffer

func (b *CartesianBoundingBox) Buffer(deltaX, deltaY float64) *CartesianBoundingBox

Buffer adds deltaX and deltaY to the bounding box on both the Lo and Hi side.

func (*CartesianBoundingBox) Combine

Combine combines two bounding boxes together. Returns a copy of the CartesianBoundingBox.

func (*CartesianBoundingBox) Compare

Compare returns the comparison between two bounding boxes. Compare lower dimensions before higher ones, i.e. X, then Y.

func (*CartesianBoundingBox) Covers

Covers returns whether the BoundingBox covers the other bounding box. Empty bounding boxes never cover.

func (*CartesianBoundingBox) Intersects

Intersects returns whether the BoundingBoxes intersect. Empty bounding boxes never intersect.

func (*CartesianBoundingBox) Repr

func (b *CartesianBoundingBox) Repr() string

Repr is the string representation of the CartesianBoundingBox.

func (*CartesianBoundingBox) ToGeomT

func (b *CartesianBoundingBox) ToGeomT(srid geopb.SRID) geom.T

ToGeomT converts a BoundingBox to a GeomT.

func (*CartesianBoundingBox) WithPoint

WithPoint includes a new point to the CartesianBoundingBox. It will edit any bounding box in place.

type EmptyBehavior

type EmptyBehavior uint8

EmptyBehavior is the behavior to adopt when an empty Geometry is encountered.

const (
	// EmptyBehaviorError will error with EmptyGeometryError when an empty geometry
	// is encountered.
	EmptyBehaviorError EmptyBehavior = 0
	// EmptyBehaviorOmit will omit an entry when an empty geometry is encountered.
	EmptyBehaviorOmit EmptyBehavior = 1
)

type EmptyGeometryError

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

EmptyGeometryError is an error that is returned when the Geometry or any parts of its subgeometries are empty.

func NewEmptyGeometryError

func NewEmptyGeometryError() *EmptyGeometryError

NewEmptyGeometryError returns an error indicating an empty geometry has been found.

func (*EmptyGeometryError) Cause

func (w *EmptyGeometryError) Cause() error

Cause implements the errors.SafeDetailer interface.

func (*EmptyGeometryError) Error

func (w *EmptyGeometryError) Error() string

Error implements the error interface.

func (*EmptyGeometryError) Format

func (w *EmptyGeometryError) Format(s fmt.State, verb rune)

Format implements the errors.Formatter interface.

func (*EmptyGeometryError) FormatError

func (w *EmptyGeometryError) FormatError(p errors.Printer) (next error)

FormatError implements the errors.Formatter interface.

func (*EmptyGeometryError) SafeDetails

func (w *EmptyGeometryError) SafeDetails() []string

SafeDetails implements the SafeDetailer interface.

func (*EmptyGeometryError) Unwrap

func (w *EmptyGeometryError) Unwrap() error

Unwrap implements the SafeDetailer interface.

type FnExclusivity

type FnExclusivity bool

FnExclusivity is used to indicate whether a geo function should have inclusive or exclusive semantics. For example, DWithin == (Distance <= x), while DWithinExclusive == (Distance < x).

const (
	// FnExclusive indicates that the corresponding geo function should have
	// exclusive semantics.
	FnExclusive FnExclusivity = true
	// FnInclusive indicates that the corresponding geo function should have
	// inclusive semantics.
	FnInclusive FnExclusivity = false
)

type Geography

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

Geography is a spherical spatial object.

func MakeGeography

func MakeGeography(spatialObject geopb.SpatialObject) (Geography, error)

MakeGeography returns a new Geography. Assumes the input EWKB is validated and in little endian.

func MakeGeographyFromGeomT

func MakeGeographyFromGeomT(g geom.T) (Geography, error)

MakeGeographyFromGeomT creates a new Geography from a geom.T object.

func MakeGeographyUnsafe

func MakeGeographyUnsafe(spatialObject geopb.SpatialObject) Geography

MakeGeographyUnsafe creates a geometry object that assumes spatialObject is from the DB. It assumes the spatialObject underneath is safe.

func MustMakeGeographyFromGeomT

func MustMakeGeographyFromGeomT(g geom.T) Geography

MustMakeGeographyFromGeomT enforces no error from MakeGeographyFromGeomT.

func MustParseGeography

func MustParseGeography(str string) Geography

MustParseGeography behaves as ParseGeography, but panics if there is an error.

func MustParseGeographyFromEWKB

func MustParseGeographyFromEWKB(ewkb geopb.EWKB) Geography

MustParseGeographyFromEWKB behaves as ParseGeographyFromEWKB, but panics if an error occurs.

func ParseGeography

func ParseGeography(str string) (Geography, error)

ParseGeography parses a Geography from a given text.

func ParseGeographyFromEWKB

func ParseGeographyFromEWKB(ewkb geopb.EWKB) (Geography, error)

ParseGeographyFromEWKB parses the EWKB into a Geography.

func ParseGeographyFromEWKBAndSRID

func ParseGeographyFromEWKBAndSRID(ewkb geopb.EWKB, srid geopb.SRID) (Geography, error)

ParseGeographyFromEWKBAndSRID parses the EWKB into a given Geography with the given SRID set.

func ParseGeographyFromEWKBUnsafe

func ParseGeographyFromEWKBUnsafe(ewkb geopb.EWKB) (Geography, error)

ParseGeographyFromEWKBUnsafe returns a new Geography from an EWKB, without any SRID checks. You should only do this if you trust the EWKB is setup correctly. You most likely want ParseGeographyFromEWKB instead.

func ParseGeographyFromEWKT

func ParseGeographyFromEWKT(
	ewkt geopb.EWKT, srid geopb.SRID, defaultSRIDOverwriteSetting defaultSRIDOverwriteSetting,
) (Geography, error)

ParseGeographyFromEWKT parses the EWKT into a Geography.

func ParseGeographyFromGeoJSON

func ParseGeographyFromGeoJSON(json []byte) (Geography, error)

ParseGeographyFromGeoJSON parses the GeoJSON into a given Geography.

func (*Geography) AsGeomT

func (g *Geography) AsGeomT() (geom.T, error)

AsGeomT returns the Geography as a geom.T object.

func (*Geography) AsGeometry

func (g *Geography) AsGeometry() (Geometry, error)

AsGeometry converts a given Geography to its Geometry form.

func (*Geography) AsS2

func (g *Geography) AsS2(emptyBehavior EmptyBehavior) ([]s2.Region, error)

AsS2 converts a given Geography into it's S2 form.

func (*Geography) BoundingCap

func (g *Geography) BoundingCap() s2.Cap

BoundingCap returns the bounding s2.Cap of the given Geography.

func (*Geography) BoundingRect

func (g *Geography) BoundingRect() s2.Rect

BoundingRect returns the bounding s2.Rect of the given Geography.

func (*Geography) CloneWithSRID

func (g *Geography) CloneWithSRID(srid geopb.SRID) (Geography, error)

CloneWithSRID sets a given Geography's SRID to another, without any transformations. Returns a new Geography object.

func (*Geography) Compare

func (g *Geography) Compare(o Geography) int

Compare compares a Geography against another. It compares using SpaceCurveIndex, followed by the byte representation of the Geography. This must produce the same ordering as the index mechanism.

func (*Geography) EWKB

func (g *Geography) EWKB() geopb.EWKB

EWKB returns the EWKB representation of the Geography.

func (*Geography) EWKBHex

func (g *Geography) EWKBHex() string

EWKBHex returns the EWKBHex representation of the Geography.

func (*Geography) SRID

func (g *Geography) SRID() geopb.SRID

SRID returns the SRID representation of the Geography.

func (*Geography) ShapeType

func (g *Geography) ShapeType() geopb.ShapeType

ShapeType returns the shape type of the Geography.

func (*Geography) SpaceCurveIndex

func (g *Geography) SpaceCurveIndex() uint64

SpaceCurveIndex returns an uint64 index to use representing an index into a space-filling curve. This will return 0 for empty spatial objects.

func (*Geography) SpatialObject

func (g *Geography) SpatialObject() geopb.SpatialObject

SpatialObject returns the SpatialObject representation of the Geography.

func (*Geography) SpatialObjectRef

func (g *Geography) SpatialObjectRef() *geopb.SpatialObject

SpatialObjectRef returns a pointer to the SpatialObject representation of the Geography.

func (*Geography) Spheroid

func (g *Geography) Spheroid() (*geographiclib.Spheroid, error)

Spheroid returns the spheroid represented by the given Geography.

type GeomTIterator

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

GeomTIterator decomposes geom.T objects into individual components (i.e. either a POINT, LINESTRING or POLYGON) and presents them as an iterator. It automatically decomposes MULTI-* and GEOMETRYCOLLECTION objects. This prevents an allocation compared to decomposing the objects into a geom.T array.

func NewGeomTIterator

func NewGeomTIterator(g geom.T, emptyBehavior EmptyBehavior) GeomTIterator

NewGeomTIterator returns a new GeomTIterator.

func (*GeomTIterator) Next

func (it *GeomTIterator) Next() (geom.T, bool, error)

Next returns the next geom.T object, a bool as to whether there is an entry and an error if any.

type Geometry

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

Geometry is planar spatial object.

func MakeGeometry

func MakeGeometry(spatialObject geopb.SpatialObject) (Geometry, error)

MakeGeometry returns a new Geometry. Assumes the input EWKB is validated and in little endian.

func MakeGeometryFromGeomT

func MakeGeometryFromGeomT(g geom.T) (Geometry, error)

MakeGeometryFromGeomT creates a new Geometry object from a geom.T object.

func MakeGeometryFromPointCoords

func MakeGeometryFromPointCoords(x, y float64) (Geometry, error)

MakeGeometryFromPointCoords makes a point from x, y coordinates.

func MakeGeometryUnsafe

func MakeGeometryUnsafe(spatialObject geopb.SpatialObject) Geometry

MakeGeometryUnsafe creates a geometry object that assumes spatialObject is from the DB. It assumes the spatialObject underneath is safe.

func MustParseGeometry

func MustParseGeometry(str string) Geometry

MustParseGeometry behaves as ParseGeometry, but panics if there is an error.

func MustParseGeometryFromEWKB

func MustParseGeometryFromEWKB(ewkb geopb.EWKB) Geometry

MustParseGeometryFromEWKB behaves as ParseGeometryFromEWKB, but panics if an error occurs.

func ParseGeometry

func ParseGeometry(str string) (Geometry, error)

ParseGeometry parses a Geometry from a given text.

func ParseGeometryFromEWKB

func ParseGeometryFromEWKB(ewkb geopb.EWKB) (Geometry, error)

ParseGeometryFromEWKB parses the EWKB into a Geometry.

func ParseGeometryFromEWKBAndSRID

func ParseGeometryFromEWKBAndSRID(ewkb geopb.EWKB, srid geopb.SRID) (Geometry, error)

ParseGeometryFromEWKBAndSRID parses the EWKB into a given Geometry with the given SRID set.

func ParseGeometryFromEWKBUnsafe

func ParseGeometryFromEWKBUnsafe(ewkb geopb.EWKB) (Geometry, error)

ParseGeometryFromEWKBUnsafe returns a new Geometry from an EWKB, without any SRID checks. You should only do this if you trust the EWKB is setup correctly. You most likely want geo.ParseGeometryFromEWKB instead.

func ParseGeometryFromEWKT

func ParseGeometryFromEWKT(
	ewkt geopb.EWKT, srid geopb.SRID, defaultSRIDOverwriteSetting defaultSRIDOverwriteSetting,
) (Geometry, error)

ParseGeometryFromEWKT parses the EWKT into a Geometry.

func ParseGeometryFromGeoJSON

func ParseGeometryFromGeoJSON(json []byte) (Geometry, error)

ParseGeometryFromGeoJSON parses the GeoJSON into a given Geometry.

func ParseGeometryPointFromGeoHash

func ParseGeometryPointFromGeoHash(g string, precision int) (Geometry, error)

ParseGeometryPointFromGeoHash converts a GeoHash to a Geometry Point using a Lng/Lat Point representation of the GeoHash.

func (*Geometry) AsGeography

func (g *Geometry) AsGeography() (Geography, error)

AsGeography converts a given Geometry to its Geography form.

func (*Geometry) AsGeomT

func (g *Geometry) AsGeomT() (geom.T, error)

AsGeomT returns the geometry as a geom.T object.

func (*Geometry) CartesianBoundingBox

func (g *Geometry) CartesianBoundingBox() *CartesianBoundingBox

CartesianBoundingBox returns a Cartesian bounding box.

func (*Geometry) CloneWithSRID

func (g *Geometry) CloneWithSRID(srid geopb.SRID) (Geometry, error)

CloneWithSRID sets a given Geometry's SRID to another, without any transformations. Returns a new Geometry object.

func (*Geometry) Compare

func (g *Geometry) Compare(o Geometry) int

Compare compares a Geometry against another. It compares using SpaceCurveIndex, followed by the byte representation of the Geometry. This must produce the same ordering as the index mechanism.

func (*Geometry) EWKB

func (g *Geometry) EWKB() geopb.EWKB

EWKB returns the EWKB representation of the Geometry.

func (*Geometry) EWKBHex

func (g *Geometry) EWKBHex() string

EWKBHex returns the EWKBHex representation of the Geometry.

func (*Geometry) Empty

func (g *Geometry) Empty() bool

Empty returns whether the given Geometry is empty.

func (*Geometry) SRID

func (g *Geometry) SRID() geopb.SRID

SRID returns the SRID representation of the Geometry.

func (*Geometry) ShapeType

func (g *Geometry) ShapeType() geopb.ShapeType

ShapeType returns the shape type of the Geometry.

func (*Geometry) SpaceCurveIndex

func (g *Geometry) SpaceCurveIndex() uint64

SpaceCurveIndex returns an uint64 index to use representing an index into a space-filling curve. This will return 0 for empty spatial objects, and math.MaxUint64 for any object outside the defined bounds of the given SRID projection.

func (*Geometry) SpatialObject

func (g *Geometry) SpatialObject() geopb.SpatialObject

SpatialObject returns the SpatialObject representation of the Geometry.

func (*Geometry) SpatialObjectRef

func (g *Geometry) SpatialObjectRef() *geopb.SpatialObject

SpatialObjectRef return a pointer to the SpatialObject representation of the Geometry.

type SpatialObjectToGeoJSONFlag

type SpatialObjectToGeoJSONFlag int

SpatialObjectToGeoJSONFlag maps to the ST_AsGeoJSON flags for PostGIS.

const (
	SpatialObjectToGeoJSONFlagIncludeBBox SpatialObjectToGeoJSONFlag = 1 << (iota)
	SpatialObjectToGeoJSONFlagShortCRS
	SpatialObjectToGeoJSONFlagLongCRS
	SpatialObjectToGeoJSONFlagShortCRSIfNot4326

	SpatialObjectToGeoJSONFlagZero = 0
)

These should be kept with ST_AsGeoJSON in PostGIS. 0: means no option 1: GeoJSON BBOX 2: GeoJSON Short CRS (e.g EPSG:4326) 4: GeoJSON Long CRS (e.g urn:ogc:def:crs:EPSG::4326) 8: GeoJSON Short CRS if not EPSG:4326 (default)

Directories

Path Synopsis
Package geographiclib is a wrapper around the GeographicLib library.
Package geographiclib is a wrapper around the GeographicLib library.
Package geoprojbase is a minimal dependency package that contains basic metadata and data structures for SRIDs and their CRS transformations.
Package geoprojbase is a minimal dependency package that contains basic metadata and data structures for SRIDs and their CRS transformations.
Package geos is a wrapper around the spatial data types between the geo package and the GEOS C library.
Package geos is a wrapper around the spatial data types between the geo package and the GEOS C library.

Jump to

Keyboard shortcuts

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