Skip to content

Instantly share code, notes, and snippets.

@stvno
Last active September 25, 2015 18:05
Show Gist options
  • Save stvno/d1caca08c7ca50fda95d to your computer and use it in GitHub Desktop.
Save stvno/d1caca08c7ca50fda95d to your computer and use it in GitHub Desktop.
SQL version of pointToTileFraction from https://github.com/mapbox/tilebelt/blob/master/index.js#L155
create or replace function pointToTileFraction(float, float, integer) returns int[3] as $$
declare
lon ALIAS FOR $1;
lat ALIAS FOR $2;
z ALIAS FOR $3;
result int[];
BEGIN
result := array_append(result, floor(pow(2,z) * (lon / 360 + 0.5))::int);
result := array_append(result, floor(pow(2,z) * (0.5-0.25 *(log((1 + sin(lat*pi()/180))/(1 - sin(lat*pi()/180)))/pi())))::int);
result := array_append(result, z);
return result;
END;
$$ LANGUAGE 'plpgsql';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment