-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated slippy tiles to work as it use to.
But kept the ability to have different SRID's.
- Loading branch information
Showing
16 changed files
with
988 additions
and
789 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
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,143 @@ | ||
package slippy | ||
|
||
import ( | ||
"math" | ||
|
||
"github.com/go-spatial/geom" | ||
) | ||
|
||
/* | ||
* | ||
* This file should contain the basic math function for converting | ||
* between coordinates that are internal to the system. | ||
* | ||
* Much of the math here is derived from two sources: | ||
* ref: https://maplibre.org/maplibre-native/docs/book/design/coordinate-system.html#11 | ||
* ref: https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#ECMAScript_(JavaScript/ActionScript,_etc.) | ||
*/ | ||
|
||
const ( | ||
// DefaultTileSize is the tile size used if the given tile size is 0. | ||
DefaultTileSize = 256 | ||
// Lat4326Max is the maximum degree for latitude on an SRID 4326 map | ||
Lat4326Max = 85.05112 | ||
// Lon4326Max is the maximum degree for longitude on an SRID 4326 map | ||
Lon4326Max = 180 | ||
|
||
// floatDrift is used to compare floating point numbers, and to deal with float drift | ||
floatDrift = 0.000001 | ||
) | ||
|
||
// Degree2Radians converts degrees to radians | ||
func Degree2Radians(degree float64) float64 { | ||
return degree * math.Pi / 180 | ||
} | ||
|
||
// Radians2Degree converts radians to degrees | ||
func Radians2Degree(radians float64) float64 { | ||
return radians * 180 / math.Pi | ||
} | ||
|
||
// lat2Num will return the Y coordinate for the tile at the given Z. | ||
// | ||
// Lat is assumed to be in degrees in SRID 3857 coordinates | ||
// If tileSize == 0 then we will use a tileSize of DefaultTileSize | ||
func lat2Num(tileSize uint32, z Zoom, lat float64) (y int) { | ||
if tileSize == 0 { | ||
tileSize = DefaultTileSize | ||
} | ||
// bound it because we have a top of the world problem | ||
if lat < -Lat4326Max { | ||
return int(z.N() - 1) | ||
} | ||
|
||
if lat > Lat4326Max { | ||
return 0 | ||
} | ||
tileY := lat2Px(tileSize, z, lat) | ||
tileY = tileY / float64(tileSize) | ||
// Truncate to get the tile | ||
return int(tileY) | ||
} | ||
|
||
// lat2Px will return the pixel coordinate for the lat. This can return | ||
// a pixel that is outside the extents of the map, this just means | ||
// the drawing is happening in the buffered area usually done for stitching | ||
// purposes. | ||
func lat2Px(tileSize uint32, z Zoom, lat float64) (yPx float64) { | ||
if tileSize == 0 { | ||
tileSize = DefaultTileSize | ||
} | ||
worldSize := float64(tileSize) * z.N() | ||
|
||
// Convert the Degree to radians as most of the math functions work in radians | ||
radLat := Degree2Radians(45 + lat/2) | ||
// normalize lat | ||
latTan := math.Tan(radLat) | ||
latNormalized := math.Log(latTan) | ||
|
||
// compute the pixel value for y: | ||
yPxRaw := (180 - Radians2Degree(latNormalized)) / 360 | ||
yPx = yPxRaw * worldSize | ||
// instead of getting 7.0 we can end up with 6.9999999999, etc... use floatDrift to correct for such cases | ||
return yPx + floatDrift | ||
} | ||
|
||
// lon2Num will return the Y coordinate for the tile at the given Z. | ||
// | ||
// Lat is assumed to be in degrees in SRID 3857 coordinates | ||
// If tileSize == 0 then we will use a tileSize of DefaultTileSize | ||
func lon2Num(tileSize uint32, z Zoom, lon float64) (x int) { | ||
if tileSize == 0 { | ||
tileSize = DefaultTileSize | ||
} | ||
|
||
if lon <= -Lon4326Max { | ||
return 0 | ||
} | ||
|
||
if lon >= Lon4326Max { | ||
return int(z.N() - 1) | ||
} | ||
|
||
tileX := lon2Px(tileSize, z, lon) | ||
tileX = tileX / float64(tileSize) | ||
// Truncate to get the tile | ||
return int(tileX) | ||
|
||
} | ||
|
||
// lonPx will return the pixel coordinate for the lon. This can return | ||
// a pixels that is outside the extents of the map, this just means | ||
// the drawing is happening in the buffered area usually done for stitching | ||
// purposes. | ||
func lon2Px(tileSize uint32, z Zoom, lon float64) (xPx float64) { | ||
if tileSize == 0 { | ||
tileSize = DefaultTileSize | ||
} | ||
worldSize := float64(tileSize) * z.N() | ||
lonNormalized := 180 + lon | ||
// compute the pixel value for y: | ||
xPxRaw := lonNormalized / 360 | ||
xPx = xPxRaw * worldSize | ||
// instead of getting 7.0 we can end up with 6.9999999999, etc... use floatDrift to correct for such cases | ||
return xPx + floatDrift | ||
} | ||
|
||
func PtFromLatLon(lat, lon float64) geom.Point { | ||
return geom.Point{lon, lat} | ||
} | ||
|
||
func x2deg(z Zoom, x int) float64 { | ||
n := z.N() | ||
long := float64(x) / n | ||
long = long * 360.0 | ||
long = long - 180.0 | ||
return long | ||
} | ||
|
||
func y2deg(z Zoom, y int) float64 { | ||
n := math.Pi - 2.0*math.Pi*float64(y)/z.N() | ||
lat := 180.0 / math.Pi * math.Atan(0.5*(math.Exp(n)-math.Exp(-n))) | ||
return lat | ||
} |
Oops, something went wrong.