project

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2021 License: MIT Imports: 3 Imported by: 14

README

orb/project Godoc Reference

Package orb/project has helper function for projection geometries.

Examples

Project orb.Point to Mercator:

sf := orb.Point{-122.416667, 37.783333}
merc := project.Point(sf, project.WGS84.ToMercator)

fmt.Println(merc)
// Output:
// [-1.3627361035049736e+07 4.548863085837512e+06]

Find centroid of polygon in Mercator projection:

poly := orb.Polygon{
	{
		{-122.4163816, 37.7792782},
		{-122.4162786, 37.7787626},
		{-122.4151027, 37.7789118},
		{-122.4152143, 37.7794274},
		{-122.4163816, 37.7792782},
	},
}

merc := project.Polygon(poly, project.WGS84.ToMercator)
centroid, _ := planar.CentroidArea(merc)

centroid = project.Mercator.ToWGS84(centroid)

fmt.Println(centroid)
// Output:
// [-122.41574403384001 37.77909471899779]

Documentation

Overview

Package project defines projections to and from Mercator and WGS84 along with helpers to apply them to orb geometry types.

Index

Examples

Constants

This section is empty.

Variables

View Source
var Mercator = struct {
	ToWGS84 orb.Projection
}{
	ToWGS84: func(p orb.Point) orb.Point {
		return orb.Point{
			180.0 * p[0] / earthRadiusPi,
			180.0 / math.Pi * (2*math.Atan(math.Exp(p[1]/orb.EarthRadius)) - math.Pi/2.0),
		}
	},
}

Mercator 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 orb.Projection
}{
	ToMercator: func(g orb.Point) orb.Point {
		y := math.Log(math.Tan((90.0+g[1])*math.Pi/360.0)) * orb.EarthRadius
		return orb.Point{
			earthRadiusPi / 180.0 * g[0],
			math.Max(-earthRadiusPi, math.Min(y, earthRadiusPi)),
		}
	},
}

WGS84 is what common uses lon/lat projection.

Functions

func Bound

func Bound(bound orb.Bound, proj orb.Projection) orb.Bound

Bound is a helper to project a rectangle.

func Collection

func Collection(c orb.Collection, proj orb.Projection) orb.Collection

Collection is a helper to project a rectangle.

func Geometry

func Geometry(g orb.Geometry, proj orb.Projection) orb.Geometry

Geometry is a helper to project any geomtry.

func LineString

func LineString(ls orb.LineString, proj orb.Projection) orb.LineString

LineString is a helper to project an entire line string.

func MercatorScaleFactor

func MercatorScaleFactor(g orb.Point) float64

MercatorScaleFactor returns the mercator scaling factor for a given degree latitude.

func MultiLineString

func MultiLineString(mls orb.MultiLineString, proj orb.Projection) orb.MultiLineString

MultiLineString is a helper to project an entire multi linestring.

func MultiPoint

func MultiPoint(mp orb.MultiPoint, proj orb.Projection) orb.MultiPoint

MultiPoint is a helper to project an entire multi point.

func MultiPolygon

func MultiPolygon(mp orb.MultiPolygon, proj orb.Projection) orb.MultiPolygon

MultiPolygon is a helper to project an entire multi polygon.

func Point

func Point(p orb.Point, proj orb.Projection) orb.Point

Point is a helper to project an a point

Example (ToMercator)
package main

import (
	"fmt"

	"github.com/paulmach/orb"
	"github.com/paulmach/orb/project"
)

func main() {
	sf := orb.Point{-122.416667, 37.783333}
	merc := project.Geometry(sf, project.WGS84.ToMercator)

	fmt.Println(merc)
}
Output:

[-1.3627361035049736e+07 4.548863085837512e+06]

func Polygon

func Polygon(p orb.Polygon, proj orb.Projection) orb.Polygon

Polygon is a helper to project an entire polygon.

Example
package main

import (
	"fmt"

	"github.com/paulmach/orb"
	"github.com/paulmach/orb/planar"
	"github.com/paulmach/orb/project"
)

func main() {
	poly := orb.Polygon{
		{
			{-122.4163816, 37.7792782},
			{-122.4162786, 37.7787626},
			{-122.4151027, 37.7789118},
			{-122.4152143, 37.7794274},
			{-122.4163816, 37.7792782},
		},
	}

	merc := project.Polygon(poly, project.WGS84.ToMercator)
	centroid, _ := planar.CentroidArea(merc)

	centroid = project.Mercator.ToWGS84(centroid)
	fmt.Println(centroid)
}
Output:

[-122.41574403384001 37.77909471899779]

func Ring

func Ring(r orb.Ring, proj orb.Projection) orb.Ring

Ring is a helper to project an entire ring.

Types

This section is empty.

Jump to

Keyboard shortcuts

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