Skip to content

Instantly share code, notes, and snippets.

@jatorre
Created December 8, 2010 22:17
Show Gist options
  • Select an option

  • Save jatorre/734018 to your computer and use it in GitHub Desktop.

Select an option

Save jatorre/734018 to your computer and use it in GitHub Desktop.
a plsql function to get a PostGIS geom out of a tile defined by X,Y,Z. Returns on SRS 900913
CREATE OR REPLACE FUNCTION v_get_tile(x integer,y integer,z integer)
RETURNS geometry AS
$BODY$
DECLARE
origin_shift CONSTANT FLOAT := 20037508.342789244;
initial_resolution CONSTANT FLOAT := 156543.03392804062;
res float;
minx float;
miny float;
maxx float;
maxy float;
BEGIN
res := initial_resolution / (power(2,z));
minx := (x*256)*res - origin_shift;
miny := -((y*256)*res - origin_shift);
maxx := ((x+1)*256)*res - origin_shift;
maxy := -(((y+1)*256)*res - origin_shift);
RETURN (ST_GeomFromText('POLYGON(('||minx||' '||maxy||','||maxx||' '||maxy||','||maxx||' '||miny||','||minx||' '||miny||','||minx||' '||maxy||'))',900913));END;
$BODY$
LANGUAGE 'plpgsql' IMMUTABLE STRICT
COST 100;
@jatorre
Copy link
Copy Markdown
Author

jatorre commented Apr 26, 2012 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment