{{ message }}

Instantly share code, notes, and snippets.

# jatorre/v_get_tile.plsql

Created Dec 8, 2010
a plsql function to get a PostGIS geom out of a tile defined by X,Y,Z. Returns on SRS 900913
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
 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;

### strk commented Apr 26, 2012

 Shouldn't resolution be 4^Z rather than 2^Z ? Another thought is that the use of floating point numbers may make it so that adjacent tiles aren't really touching (by very small amount). I'll give that a try, and grid if needed.

### strk commented Apr 26, 2012

 I was clearly wrong, resolution is 2^Z (number of tiles would be 4^Z)

### jatorre commented Apr 26, 2012

 thanks! I did all this in a hurry... Javier de la Torre @jatorre Vizzuality 148 Lafayette St. PH, New York, 10013,USA +1 347 320 7715 www.vizzuality.com … On Apr 26, 2012, at 7:17 AM, strk wrote: I was clearly wrong, resolution is 2^Z (number of tiles would be 4^Z) --- Reply to this email directly or view it on GitHub: https://gist.github.com/734018