forked from go-spatial/tegola
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added basic support for 4326 Datasource.
Add functions to convert a geometry in 4326 to WebMercator and back. Added support to the postgis provider to do the conversion. I don't think this is the right place for the conversion to take place, but is good enought for now. It should live in the feature but that requires the features to know the encoding of the geometries, instead of assuming WebMercator. I'm using the more correct rather then the faster conversion algo. Relates to issue go-spatial#47
- Loading branch information
Showing
5 changed files
with
290 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
Package math contins generic math functions that we need for doing transforms. | ||
this package will augment the go math library. | ||
*/ | ||
package maths | ||
|
||
import ( | ||
"math" | ||
|
||
"github.com/terranodo/tegola" | ||
) | ||
|
||
const ( | ||
WebMercator = tegola.WebMercator | ||
WGS84 = tegola.WGS84 | ||
Deg2Rad = math.Pi / 180 | ||
Rad2Deg = 180 / math.Pi | ||
PiDiv2 = math.Pi / 2.0 | ||
) | ||
|
||
// AreaOfPolygon will calculate the Area of a polygon using the surveyor's formula | ||
// (https://en.wikipedia.org/wiki/Shoelace_formula) | ||
func AreaOfPolygon(p tegola.Polygon) (area float64) { | ||
var points []tegola.Point | ||
for _, l := range p.Sublines() { | ||
points = append(points, l.Subpoints()...) | ||
} | ||
n := len(points) | ||
for i := range points { | ||
j := (i + 1) % n | ||
area += points[i].X() * points[j].Y() | ||
area -= points[j].X() * points[i].Y() | ||
} | ||
return math.Abs(area) / 2.0 | ||
} | ||
|
||
func RadToDeg(rad float64) float64 { | ||
return rad * Rad2Deg | ||
} | ||
|
||
func DegToRad(deg float64) float64 { | ||
return deg * Deg2Rad | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
Package webmercator does the translation to and from WebMercator and WGS84 | ||
Gotten from: http://wiki.openstreetmap.org/wiki/Mercator#C.23 | ||
*/ | ||
package webmercator | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
|
||
"github.com/terranodo/tegola/maths" | ||
) | ||
|
||
const ( | ||
RMajor = 6378137.0 | ||
RMinor = 6356752.3142 | ||
Ratio = RMinor / RMajor | ||
) | ||
|
||
var Eccent float64 | ||
var Com float64 | ||
|
||
func init() { | ||
Eccent = math.Sqrt(1.0 - (Ratio * Ratio)) | ||
Com = 0.5 * Eccent | ||
} | ||
|
||
func LonToX(lon float64) float64 { | ||
return RMajor * maths.DegToRad(lon) | ||
} | ||
|
||
func con(phi float64) float64 { | ||
v := Eccent * math.Sin(phi) | ||
return math.Pow(((1.0 - v) / (1.0 + v)), Com) | ||
} | ||
|
||
func LatToY(lat float64) float64 { | ||
lat = math.Min(89.5, math.Max(lat, -89.5)) | ||
phi := maths.DegToRad(lat) | ||
ts := math.Tan(0.5*((math.Pi*0.5)-phi)) / con(phi) | ||
return 0 - RMajor*math.Log(ts) | ||
} | ||
|
||
func XToLon(x float64) float64 { | ||
return maths.RadToDeg(x) / RMajor | ||
} | ||
|
||
func YToLat(y float64) float64 { | ||
ts := math.Exp(-y / RMajor) | ||
phi := maths.PiDiv2 - 2*math.Atan(ts) | ||
dphi := 1.0 | ||
i := 0 | ||
for (math.Abs(dphi) > 0.000000001) && (i < 15) { | ||
dphi = maths.PiDiv2 - 2*math.Atan(ts*con(phi)) - phi | ||
phi += dphi | ||
i++ | ||
} | ||
return maths.RadToDeg(phi) | ||
} | ||
|
||
func ToLonLat(c ...float64) ([]float64, error) { | ||
if len(c) < 2 { | ||
return c, fmt.Errorf("Coords should have at least 2 coords") | ||
} | ||
crds := []float64{XToLon(c[0]), YToLat(c[1])} | ||
crds = append(crds, c[2:]...) | ||
return crds, nil | ||
} | ||
func ToXY(c ...float64) ([]float64, error) { | ||
if len(c) < 2 { | ||
return c, fmt.Errorf("Coords should have at least 2 coords") | ||
} | ||
crds := []float64{LonToX(c[0]), LatToY(c[1])} | ||
crds = append(crds, c[2:]...) | ||
return crds, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters