Last active
October 30, 2017 00:46
-
-
Save n0531m/bebcb5e97fb66b291943f4889a175efb to your computer and use it in GitHub Desktop.
calculate coordinates from latlng
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 is a udf sample to calculate coordinates from latlng | |
# https://developers.google.com/maps/documentation/javascript/examples/map-coordinates | |
CREATE TEMPORARY FUNCTION | |
project (latitude FLOAT64, longitude FLOAT64, zoom FLOAT64) | |
RETURNS STRUCT < worldCoordinate_x float64, | |
worldCoordinate_y float64, | |
pixelCoordinate_x float64, | |
pixelCoordinate_y float64, | |
tileCoordinate_x float64, | |
tileCoordinate_y float64 > | |
LANGUAGE js AS """ | |
var TILE_SIZE = 256; | |
var scale = 1 << zoom; | |
var siny = Math.sin(latitude * Math.PI / 180); | |
siny = Math.min(Math.max(siny, -0.9999), 0.9999); | |
var worldCoordinate_x=TILE_SIZE * (0.5 + longitude / 360); | |
var worldCoordinate_y=TILE_SIZE * (0.5 - Math.log((1 + siny) / (1 - siny)) / (4 * Math.PI)); | |
return { | |
"worldCoordinate_x" : worldCoordinate_x, | |
"worldCoordinate_y" : worldCoordinate_y, | |
"pixelCoordinate_x" : Math.floor(worldCoordinate_x * scale), | |
"pixelCoordinate_y" : Math.floor(worldCoordinate_y * scale), | |
"tileCoordinate_x" : Math.floor(worldCoordinate_x * scale / TILE_SIZE), | |
"tileCoordinate_y" : Math.floor(worldCoordinate_y * scale / TILE_SIZE) | |
}; | |
"""; | |
select project(1.276426,103.797588,16) as projected from | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment