primitives

package module
v0.0.0-...-4c5d44b Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2024 License: MIT Imports: 3 Imported by: 0

README

primitives

Geometric primitives (forked from https://github.com/paulmach/orb)

Documentation

Index

Constants

View Source
const EarthRadius = 6378137.0 // meters

EarthRadius is the radius of the earth in meters. It is used in geo distance calculations. To keep things consistent, this value matches WGS84 Web Mercator (EPSG:3857).

Variables

AllGeometries lists all possible types and values that a geometry interface can be. It should be used only for testing to verify functions that accept a Geometry will work in all cases.

View Source
var DefaultRoundingFactor = 1e6 // 6 decimal places

DefaultRoundingFactor is the default rounding factor used by the Round func.

View Source
var EpsilonTolerance = 1e-6

Epsilon tolerance.

View Source
var Mercator = struct {
	ToWGS84 TransformFunc
}{
	ToWGS84: func(p Point) {
		lon := 180.0 * p.X() / earthRadiusPi
		lat := 180.0 / math.Pi * (2*math.Atan(math.Exp(p.Y()/EarthRadius)) - math.Pi/2.0)
		if p.Dimension() > 0 {
			p[0] = lon
		}
		if p.Dimension() > 0 {
			p[1] = lat
		}
	},
}

Mercator EPSG:3857 performs the Spherical Pseudo-Mercator projection used by most web maps.

View Source
var WGS84 = struct {
	// ToMercator projections from WGS to Mercator, used by most web maps
	ToMercator TransformFunc
}{
	ToMercator: func(p Point) {
		x := earthRadiusPi / 180.0 * p.Lon()
		y := math.Log(math.Tan((90.0+p.Lat())*math.Pi/360.0)) * EarthRadius
		y = math.Max(-earthRadiusPi, math.Min(y, earthRadiusPi))
		if p.Dimension() > 0 {
			p[0] = x
		}
		if p.Dimension() > 0 {
			p[1] = y
		}
	},
}

WGS84 EPSG:4326 is what common uses lon/lat projection.

Functions

func CartesianDistance2d

func CartesianDistance2d(a, b Point) float64

Distance returns the distance between xy 2d points on the plan.

func CartesianDistance3d

func CartesianDistance3d(a, b Point) float64

Distance returns the distance between xyz 32d points on the plan.

func DistanceEarth2d

func DistanceEarth2d(p1, p2 Point) float64

Distance returns the distance between two lon/lat wgs84 points on the earth.

func DistanceHaversine2d

func DistanceHaversine2d(p1, p2 Point) float64

DistanceHaversine computes the distance between two lon/lat wgs84 points using the haversine formula.

func Length

func Length(g Geometry, dfp ...DistanceFunc) float64

Length returns the length of the geometry.

func Transform

func Transform(g Geometry, tf TransformFunc)

Transform transforms the geometry inplace.

Types

type Bound

type Bound struct {
	Min, Max [2]float64
}

A Bound represents a closed box or rectangle. To create a bound with two points you can do something like:

orb.MultiPoint{p1, p2}.Bound()

func (Bound) Bottom

func (b Bound) Bottom() float64

Bottom returns the bottom of the bound.

func (Bound) Bound

func (b Bound) Bound() Bound

Bound returns the the same bound.

func (Bound) Center

func (b Bound) Center() Point

Center returns the center of the bounds by "averaging" the x and y coords.

func (Bound) Clone

func (b Bound) Clone() Geometry

Clone performs deep clone.

func (Bound) Contains

func (b Bound) Contains(point Point) bool

Contains determines if the point is within the bound. Points on the boundary are considered within.

func (Bound) Dimension

func (b Bound) Dimension() Dimension

Dimension returns XY.

func (Bound) Equal

func (b Bound) Equal(g Geometry) bool

Equal returns if two bounds are equal.

func (Bound) Extend

func (b Bound) Extend(point Point) Bound

Extend grows the bound to include the new point.

func (Bound) GeoJSONType

func (b Bound) GeoJSONType() string

GeoJSONType returns the GeoJSON type for the object.

func (Bound) Intersects

func (b Bound) Intersects(bound Bound) bool

Intersects determines if two bounds intersect. Returns true if they are touching.

func (Bound) IsEmpty

func (b Bound) IsEmpty() bool

IsEmpty returns true if it contains zero area or if it's in some malformed negative state where the left point is larger than the right. This can be caused by padding too much negative.

func (Bound) IsZero

func (b Bound) IsZero() bool

IsZero return true if the bound just includes just null island.

func (Bound) Left

func (b Bound) Left() float64

Left returns the left of the bound.

func (Bound) LeftTop

func (b Bound) LeftTop() Point

LeftTop returns the upper left point of the bound.

func (Bound) Pad

func (b Bound) Pad(d float64) Bound

Pad extends the bound in all directions by the given value.

func (Bound) Right

func (b Bound) Right() float64

Right returns the right of the bound.

func (Bound) RightBottom

func (b Bound) RightBottom() Point

RightBottom return the lower right point of the bound.

func (Bound) ToPolygon

func (b Bound) ToPolygon() Polygon

ToPolygon converts the bound into a Polygon object.

func (Bound) ToRing

func (b Bound) ToRing() Ring

ToRing converts the bound into a loop defined by the boundary of the box.

func (Bound) Top

func (b Bound) Top() float64

Top returns the top of the bound.

func (Bound) Union

func (b Bound) Union(other Bound) Bound

Union extends this bound to contain the union of this and the given bound.

type Collection

type Collection []Geometry

A Collection is a collection of geometries that is also a Geometry.

func (Collection) Bound

func (c Collection) Bound() Bound

Bound returns the bounding box of all the Geometries combined.

func (Collection) Clone

func (c Collection) Clone() Geometry

Clone returns a deep copy of the collection.

func (Collection) Dimension

func (c Collection) Dimension() Dimension

Dimension returns the max of the dimension of the collection.

func (Collection) Equal

func (c Collection) Equal(g Geometry) bool

Equal compares two collections. Returns true if lengths are the same and all the sub geometries are the same and in the same order.

func (Collection) GeoJSONType

func (c Collection) GeoJSONType() string

GeoJSONType returns the geometry collection type.

type Dimension

type Dimension int8

Dim defines space dimension (0 = NOME, 1 = X, 2 = XY, 3 = XYZ, 4 = XYZM)

const (
	// Dimension NONE
	NONE Dimension = 0

	// Dimension X
	X Dimension = 1

	// Dimension XY
	XY Dimension = 2

	// Dimension XYZ
	XYZ Dimension = 3

	// Dimension XYZM
	XYZM Dimension = 4
)

Constants to define dimension.

type DistanceFunc

type DistanceFunc func(Point, Point) float64

A DistanceFunc is a function that computes the distance between two points.

type Geometry

type Geometry interface {
	GeoJSONType() string
	Dimension() Dimension // e.g. 0 = NONE, 1 = X, 2 = XY, 3 = XYZ, 4 = XYZM
	Bound() Bound
	Clone() Geometry
	Equal(Geometry) bool
	// contains filtered or unexported methods
}

Geometry is an interface that represents the shared attributes of a geometry.

type LineString

type LineString []Point

LineString represents a set of points to be thought of as a polyline.

func (LineString) Bound

func (ls LineString) Bound() Bound

Bound returns a rect around the line string. Uses rectangular coordinates.

func (LineString) Clone

func (ls LineString) Clone() Geometry

Clone performs deep clone.

func (LineString) Dimension

func (ls LineString) Dimension() Dimension

Dimension returns the max of the dimension.

func (LineString) Equal

func (ls LineString) Equal(g Geometry) bool

Equal compares two line strings. Returns true if lengths are the same and all points are Equal.

func (LineString) GeoJSONType

func (ls LineString) GeoJSONType() string

GeoJSONType returns the GeoJSON type for the object.

func (LineString) Reverse

func (ls LineString) Reverse()

Reverse will reverse the line string. This is done inplace, ie. it modifies the original data.

type MultiLineString

type MultiLineString []LineString

MultiLineString is a set of polylines.

func (MultiLineString) Bound

func (mls MultiLineString) Bound() Bound

Bound returns a bound around all the line strings.

func (MultiLineString) Clone

func (mls MultiLineString) Clone() Geometry

Clone performs deep clone.

func (MultiLineString) Dimension

func (mls MultiLineString) Dimension() Dimension

Dimension returns the max of the dimension.

func (MultiLineString) Equal

func (mls MultiLineString) Equal(g Geometry) bool

Equal compares two multi line strings. Returns true if lengths are the same and all points are Equal.

func (MultiLineString) GeoJSONType

func (mls MultiLineString) GeoJSONType() string

GeoJSONType returns the GeoJSON type for the object.

type MultiPoint

type MultiPoint []Point

A MultiPoint represents a set of points in the 2D Eucledian or Cartesian plane.

func (MultiPoint) Bound

func (mp MultiPoint) Bound() Bound

Bound returns a bound around the points. Uses rectangular coordinates.

func (MultiPoint) Clone

func (mp MultiPoint) Clone() Geometry

Clone performs deep clone.

func (MultiPoint) Dimension

func (mp MultiPoint) Dimension() Dimension

Dimension returns the max of the dimension.

func (MultiPoint) Equal

func (mp MultiPoint) Equal(g Geometry) bool

Equal compares two MultiPoint objects. Returns true if lengths are the same and all points are Equal, and in the same order.

func (MultiPoint) GeoJSONType

func (mp MultiPoint) GeoJSONType() string

GeoJSONType returns the GeoJSON type for the object.

type MultiPolygon

type MultiPolygon []Polygon

MultiPolygon is a set of polygons.

func (MultiPolygon) Bound

func (mp MultiPolygon) Bound() Bound

Bound returns a bound around the multi-polygon.

func (MultiPolygon) Clone

func (mp MultiPolygon) Clone() Geometry

Clone performs deep clone.

func (MultiPolygon) Dimension

func (mp MultiPolygon) Dimension() Dimension

Dimension returns the max of the dimension.

func (MultiPolygon) Equal

func (mp MultiPolygon) Equal(g Geometry) bool

Equal compares two multi-polygons.

func (MultiPolygon) GeoJSONType

func (mp MultiPolygon) GeoJSONType() string

GeoJSONType returns the GeoJSON type for the object.

type Orientation

type Orientation int8

Orientation defines the order of the points in a polygon or closed ring.

const (
	// CCW stands for Counter Clock Wise
	CCW Orientation = 1

	// CW stands for Clock Wise
	CW Orientation = -1
)

Constants to define orientation. They follow the right hand rule for orientation.

type Point

type Point []float64

A Point represents a set of coordinate.

func Centroid

func Centroid(g Geometry) Point

Centroid returns the cartesian centroid 3d of the geometry.

func (Point) Bound

func (p Point) Bound() Bound

Bound returns a single point bound of the point.

func (Point) Clone

func (p Point) Clone() Geometry

Clone performs deep clone.

func (Point) Dimension

func (p Point) Dimension() Dimension

Dimension returns the dimension.

func (Point) Equal

func (p Point) Equal(g Geometry) bool

Equal checks if the point represents the same point or vector.

func (Point) GeoJSONType

func (p Point) GeoJSONType() string

GeoJSONType returns the GeoJSON type for the object.

func (Point) Lat

func (p Point) Lat() float64

Lat returns the vertical, latitude coordinate of the point (= Y).

func (Point) Lon

func (p Point) Lon() float64

Lon returns the horizontal, longitude coordinate of the point (= X).

func (Point) M

func (p Point) M() float64

M returns the measurement dimension of the point.

func (Point) Point

func (p Point) Point() Point

Point returns itself so it implements the Pointer interface.

func (Point) Polar

func (p Point) Polar() Polar

Polar returns Polar representation.

func (Point) X

func (p Point) X() float64

X returns the horizontal coordinate of the point.

func (Point) Y

func (p Point) Y() float64

Y returns the vertical coordinate of the point.

func (Point) Z

func (p Point) Z() float64

Z returns the height coordinate of the point.

type Polar

type Polar struct {
	R, A float64
}

A Polar represents a point 2d defined by ray and angle.

func (Polar) Angle

func (p Polar) Angle() float64

Angle returns the angle.

func (Polar) Bound

func (p Polar) Bound() Bound

Bound returns a single point bound of the point.

func (Polar) Clone

func (p Polar) Clone() Geometry

Clone performs deep clone.

func (Polar) Dimension

func (p Polar) Dimension() Dimension

Dimension returns XY.

func (Polar) Equal

func (p Polar) Equal(g Geometry) bool

Equal checks if the polar represents the same polar.

func (Polar) GeoJSONType

func (p Polar) GeoJSONType() string

GeoJSONType returns the GeoJSON type for the object.

func (Polar) Point

func (p Polar) Point() Point

Point returns Point representation.

func (Polar) Ray

func (p Polar) Ray() float64

Ray returns the distance.

type Polygon

type Polygon []Ring

Polygon is a closed area. The first LineString is the outer ring. The others are the holes. Each LineString is expected to be closed ie. the first point matches the last.

func (Polygon) Bound

func (p Polygon) Bound() Bound

Bound returns a bound around the polygon.

func (Polygon) Clone

func (p Polygon) Clone() Geometry

Clone performs deep clone.

func (Polygon) Dimension

func (p Polygon) Dimension() Dimension

Dimension returns the max of the dimension.

func (Polygon) Equal

func (p Polygon) Equal(g Geometry) bool

Equal compares two polygons. Returns true if lengths are the same and all points are Equal.

func (Polygon) GeoJSONType

func (p Polygon) GeoJSONType() string

GeoJSONType returns the GeoJSON type for the object.

type Ring

type Ring LineString

Ring represents a set of ring on the earth.

func (Ring) Bound

func (r Ring) Bound() Bound

Bound returns a rect around the ring. Uses rectangular coordinates.

func (Ring) Clone

func (r Ring) Clone() Geometry

Clone performs deep clone.

func (Ring) Closed

func (r Ring) Closed() bool

Closed will return true if the ring is a real ring. ie. 4+ points and the first and last points match. NOTE: this will not check for self-intersection.

func (Ring) Dimension

func (r Ring) Dimension() Dimension

Dimension returns the max of the dimension.

func (Ring) Equal

func (r Ring) Equal(g Geometry) bool

Equal compares two rings. Returns true if lengths are the same and all points are Equal.

func (Ring) GeoJSONType

func (r Ring) GeoJSONType() string

GeoJSONType returns the GeoJSON type for the object.

func (Ring) Orientation

func (r Ring) Orientation() Orientation

Orientation returns 1 if the the ring is in couter-clockwise order, return -1 if the ring is the clockwise order and 0 if the ring is degenerate and had no area.

func (Ring) Reverse

func (r Ring) Reverse()

Reverse changes the direction of the ring. This is done inplace, ie. it modifies the original data.

type Segment

type Segment [2]Point

A Segment represents a simple line with only 2 points .

func (Segment) Bound

func (s Segment) Bound() Bound

Bound returns a rect around the line string. Uses rectangular coordinates.

func (Segment) Clone

func (s Segment) Clone() Geometry

Clone performs deep clone.

func (Segment) Dimension

func (s Segment) Dimension() Dimension

Dimension returns the max of the dimension.

func (Segment) Equal

func (s Segment) Equal(g Geometry) bool

Equal compares two line strings. Returns true if lengths are the same and all points are Equal.

func (Segment) GeoJSONType

func (s Segment) GeoJSONType() string

GeoJSONType returns the GeoJSON type for the object.

func (Segment) Intersects

func (s Segment) Intersects(segment Segment) Point

Intersects computes 2d intersection

func (Segment) Reverse

func (s Segment) Reverse()

Reverse will reverse the segment. This is done inplace, ie. it modifies the original data.

type TransformFunc

type TransformFunc func(Point)

A TransformFunc is a function that transform the coordinates of the point inplace.

func Move

func Move(move Point) TransformFunc

Move transformation

func Round

func Round(factor ...int) TransformFunc

Round transformation

func Scale

func Scale(v float64) TransformFunc

Scale transformation

Jump to

Keyboard shortcuts

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