proj

package module
v10.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2024 License: MIT Imports: 5 Imported by: 4

README

go-proj

GoDoc

Package go-proj provides an interface to PROJ.

Features

  • High performance bulk transformation of coordinates.
  • Idiomatic Go API, including complete error handling.
  • Supports PROJ versions 6 and upwards.
  • Compatible with all geometry libraries.
  • Convenience functions for handling coordinates as []float64s.
  • Automatically handles C memory management.
  • Well tested.

Install

$ go get github.com/twpayne/go-proj/v10

You must also install the PROJ development headers and libraries. These are typically in the package libproj-dev on Debian-like systems, proj-devel on RedHat-like systems, and proj in Homebrew.

Example

func ExamplePJ_Forward() {
	pj, err := proj.NewCRSToCRS("EPSG:4326", "EPSG:3857", nil)
	if err != nil {
		panic(err)
	}
	defer pj.Destroy()

	// Start with Zürich's WGS84 latitude/longitude.
	zurich4326 := proj.NewCoord(47.374444, 8.541111, 408, 0)
	fmt.Printf("initial: x=%.6f y=%.6f z=%.6f\n", zurich4326.X(), zurich4326.Y(), zurich4326.Z())

	// Convert Zürich's WGS84 latitude/longitude to Web Mercator.
	zurich3857, err := pj.Forward(zurich4326)
	if err != nil {
		panic(err)
	}
	fmt.Printf("forward: x=%.6f y=%.6f z=%.6f\n", zurich3857.X(), zurich3857.Y(), zurich3857.Z())

	// ...and convert back.
	zurich4326After, err := pj.Inverse(zurich3857)
	if err != nil {
		panic(err)
	}
	fmt.Printf("inverse: x=%.6f y=%.6f z=%.6f", zurich4326After.X(), zurich4326After.Y(), zurich4326After.Z())

	// Output:
	// initial: x=47.374444 y=8.541111 z=408.000000
	// forward: x=950792.127329 y=6003408.475803 z=408.000000
	// inverse: x=47.374444 y=8.541111 z=408.000000
}

Comparisons with other PROJ bindings

There are many existing bindings for PROJ. Generally speaking, these:

  • Only transform one coordinate a time, making them extremely slow when transforming large number of coordinates.

  • Are tied to a single geometry representation.

  • Do not handle errors during transformation.

  • Are no longer maintained.

These existing bindings include:

License

MIT

Documentation

Overview

Package proj provides an interface to PROJ. See https://proj.org.

Index

Examples

Constants

View Source
const (
	VersionMajor = C.PROJ_VERSION_MAJOR
	VersionMinor = C.PROJ_VERSION_MINOR
	VersionPatch = C.PROJ_VERSION_PATCH
)

Version.

Variables

This section is empty.

Functions

func CoordsToFloat64Slices added in v10.2.0

func CoordsToFloat64Slices(coords []Coord) [][]float64

CoordsToFloat64Slices is a convenience function that converts a slice of Coords to a slice of []float64s. For performance, the returned []float64s alias coords.

Types

type Area

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

An Area is an area.

func NewArea

func NewArea(westLonDegree, southLatDegree, eastLonDegree, northLatDegree float64) *Area

NewArea returns a new Area.

func (*Area) Destroy

func (a *Area) Destroy()

Destroy frees all resources associated with a.

type Bounds

type Bounds struct {
	XMin float64
	YMin float64
	XMax float64
	YMax float64
}

type Context

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

A Context is a context.

func NewContext

func NewContext() *Context

NewContext returns a new Context.

func (*Context) Destroy

func (c *Context) Destroy()

Destroy frees all resources associated with c.

func (*Context) Lock added in v10.0.1

func (c *Context) Lock()

func (*Context) New

func (c *Context) New(definition string) (*PJ, error)

New returns a new PJ with the given definition.

func (*Context) NewCRSToCRS

func (c *Context) NewCRSToCRS(sourceCRS, targetCRS string, area *Area) (*PJ, error)

NewCRSToCRS returns a new PJ from sourceCRS to targetCRS and optional area.

func (*Context) NewCRSToCRSFromPJ added in v10.1.0

func (c *Context) NewCRSToCRSFromPJ(sourcePJ, targetPJ *PJ, area *Area, options string) (*PJ, error)

NewCRSToCRSFromPJ returns a new PJ from two CRSs.

func (*Context) NewFromArgs

func (c *Context) NewFromArgs(args ...string) (*PJ, error)

NewFromArgs returns a new PJ from args.

func (*Context) SetSearchPaths added in v10.4.0

func (c *Context) SetSearchPaths(paths []string)

SetSearchPaths sets the paths PROJ should be exploring to find the PROJ Data files.

func (*Context) Unlock added in v10.0.1

func (c *Context) Unlock()

type Coord

type Coord [4]float64

A coord is a coordinate.

func Float64SlicesToCoords added in v10.2.0

func Float64SlicesToCoords(float64Slices [][]float64) []Coord

Float64Slices is a convenience function that converts a slice of []float64s to a slice of Coords.

func NewCoord

func NewCoord(x, y, z, m float64) Coord

NewCoord returns a new Coord.

func (Coord) DegToRad

func (c Coord) DegToRad() Coord

DegToRad returns a new Coord with the first two elements transformed from degrees to radians.

func (*Coord) M

func (c *Coord) M() float64

M returns c's M coordinate.

func (Coord) RadToDeg

func (c Coord) RadToDeg() Coord

RadToDeg returns a new Coord with the first two elements transformed from radians to degrees.

func (*Coord) X

func (c *Coord) X() float64

X returns c's X coordinate.

func (*Coord) Y

func (c *Coord) Y() float64

Y returns c's Y coordinate.

func (*Coord) Z

func (c *Coord) Z() float64

Z returns c's Z coordinate.

type Direction

type Direction C.PJ_DIRECTION

A Direction is a direction.

const (
	DirectionFwd   Direction = C.PJ_FWD
	DirectionIdent Direction = C.PJ_IDENT
	DirectionInv   Direction = C.PJ_INV
)

Directions.

type Error

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

An Error is an error.

func (*Error) Error

func (e *Error) Error() string

type PJ

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

A PJ is a projection or a transformation.

func New added in v10.1.0

func New(definition string) (*PJ, error)

New returns a PJ with the given definition.

func NewCRSToCRS

func NewCRSToCRS(sourceCRS, targetCRS string, area *Area) (*PJ, error)

NewCRSToCRS returns a new PJ from sourceCRS to targetCRS and optional area.

func NewCRSToCRSFromPJ added in v10.1.0

func NewCRSToCRSFromPJ(sourcePJ, targetPJ *PJ, area *Area, options string) (*PJ, error)

NewCRSToCRSFromPJ returns a new PJ from two CRSs.

func NewFromArgs added in v10.1.0

func NewFromArgs(args ...string) (*PJ, error)

NewFromArgs returns a PJ with the given args.

func (*PJ) Destroy

func (pj *PJ) Destroy()

Destroy releases all resources associated with pj.

func (*PJ) Forward

func (pj *PJ) Forward(coord Coord) (Coord, error)

Forward transforms coord in the forward direction.

Example
pj, err := proj.NewCRSToCRS("EPSG:4326", "EPSG:3857", nil)
if err != nil {
	panic(err)
}
defer pj.Destroy()

// Start with Zürich's WGS84 latitude/longitude.
zurich4326 := proj.NewCoord(47.374444, 8.541111, 408, 0)
fmt.Printf("initial: x=%.6f y=%.6f z=%.6f\n", zurich4326.X(), zurich4326.Y(), zurich4326.Z())

// Convert Zürich's WGS84 latitude/longitude to Web Mercator.
zurich3857, err := pj.Forward(zurich4326)
if err != nil {
	panic(err)
}
fmt.Printf("forward: x=%.6f y=%.6f z=%.6f\n", zurich3857.X(), zurich3857.Y(), zurich3857.Z())

// ...and convert back.
zurich4326After, err := pj.Inverse(zurich3857)
if err != nil {
	panic(err)
}
fmt.Printf("inverse: x=%.6f y=%.6f z=%.6f", zurich4326After.X(), zurich4326After.Y(), zurich4326After.Z())
Output:

initial: x=47.374444 y=8.541111 z=408.000000
forward: x=950792.127329 y=6003408.475803 z=408.000000
inverse: x=47.374444 y=8.541111 z=408.000000

func (*PJ) ForwardArray

func (pj *PJ) ForwardArray(coords []Coord) error

ForwardArray transforms coords in the forward direction.

func (*PJ) ForwardBounds

func (pj *PJ) ForwardBounds(bounds Bounds, densifyPoints int) (Bounds, error)

ForwardBounds transforms bounds in the forward direction.

func (*PJ) ForwardFlatCoords

func (pj *PJ) ForwardFlatCoords(flatCoords []float64, stride, zIndex, mIndex int) error

ForwardFlatCoords transforms flatCoords in the forward direction.

func (*PJ) ForwardFloat64Slice added in v10.2.0

func (pj *PJ) ForwardFloat64Slice(float64Slice []float64) ([]float64, error)

ForwardFloat64Slice transforms float64 in place in the forward direction.

func (*PJ) ForwardFloat64Slices added in v10.2.0

func (pj *PJ) ForwardFloat64Slices(float64Slices [][]float64) error

ForwardFloat64Slices transforms float64Slices in the forward direction.

func (*PJ) Geod

func (pj *PJ) Geod(a, b Coord) (float64, float64, float64)

Geod returns the distance, forward azimuth, and reverse azimuth between a and b.

func (*PJ) GetLastUsedOperation

func (pj *PJ) GetLastUsedOperation() (*PJ, error)

GetLastUsedOperation returns the operation used in the last call to Trans.

func (*PJ) Info

func (pj *PJ) Info() PJInfo

Info returns information about pj.

func (*PJ) Inverse

func (pj *PJ) Inverse(coord Coord) (Coord, error)

Inverse transforms coord in the inverse direction.

func (*PJ) InverseArray

func (pj *PJ) InverseArray(coords []Coord) error

InverseArray transforms coords in the inverse direction.

func (*PJ) InverseBounds

func (pj *PJ) InverseBounds(bounds Bounds, densifyPoints int) (Bounds, error)

InverseBounds transforms bounds in the forward direction.

func (*PJ) InverseFlatCoords

func (pj *PJ) InverseFlatCoords(flatCoords []float64, stride, zIndex, mIndex int) error

InverseFlatCoords transforms flatCoords in the inverse direction.

func (*PJ) InverseFloat64Slice added in v10.2.0

func (pj *PJ) InverseFloat64Slice(float64Slice []float64) ([]float64, error)

InverseFloat64Slice transforms float64 in place in the forward direction.

func (*PJ) InverseFloat64Slices added in v10.2.0

func (pj *PJ) InverseFloat64Slices(float64Slices [][]float64) error

InverseFloat64Slices transforms float64Slices in the inverse direction.

func (*PJ) IsCRS added in v10.1.0

func (pj *PJ) IsCRS() bool

IsCRS returns whether pj is a CRS.

func (*PJ) LPDist

func (pj *PJ) LPDist(a, b Coord) float64

LPDist returns the geodesic distance between a and b in geodetic coordinates.

func (*PJ) LPZDist

func (pj *PJ) LPZDist(a, b Coord) float64

LPZDist returns the geodesic distance between a and b in geodetic coordinates, taking height above the ellipsoid into account.

func (*PJ) NormalizeForVisualization added in v10.1.0

func (pj *PJ) NormalizeForVisualization() (*PJ, error)

Returns a new PJ instance whose axis order is the one expected for visualization purposes. If the axis order of its source or target CRS is northing, easting, then an axis swap operation will be inserted.

The axis order of geographic CRS will be longitude, latitude[, height], and the one of projected CRS will be easting, northing [, height].

func (*PJ) Trans

func (pj *PJ) Trans(direction Direction, coord Coord) (Coord, error)

Trans transforms a single Coord in place.

func (*PJ) TransArray

func (pj *PJ) TransArray(direction Direction, coords []Coord) error

TransArray transforms an array of Coords.

func (*PJ) TransBounds

func (pj *PJ) TransBounds(direction Direction, bounds Bounds, densifyPoints int) (Bounds, error)

TransBounds transforms bounds.

func (*PJ) TransFlatCoords

func (pj *PJ) TransFlatCoords(direction Direction, flatCoords []float64, stride, zIndex, mIndex int) error

TransFlatCoords transforms an array of flat coordinates.

func (*PJ) TransFloat64Slice added in v10.2.0

func (pj *PJ) TransFloat64Slice(direction Direction, float64Slice []float64) ([]float64, error)

TransFloat64Slice transforms a []float64 in place.

func (*PJ) TransFloat64Slices added in v10.2.0

func (pj *PJ) TransFloat64Slices(direction Direction, float64Slices [][]float64) error

TransFloat64Slices transforms float64Slices.

func (*PJ) TransGeneric

func (pj *PJ) TransGeneric(direction Direction, x *float64, sx, nx int, y *float64, sy, ny int, z *float64, sz, nz int, m *float64, sm, nm int) error

TransGeneric transforms a series of coordinates.

type PJInfo

type PJInfo struct {
	ID          string
	Description string
	Definition  string
	HasInverse  bool
	Accuracy    float64
}

A PJInfo contains information about a PJ.

Jump to

Keyboard shortcuts

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