Skip to content

Instantly share code, notes, and snippets.

@feomike
Last active December 16, 2015 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feomike/5469777 to your computer and use it in GitHub Desktop.
Save feomike/5469777 to your computer and use it in GitHub Desktop.
tiling polygons

some other really really long text descriptions

WITH geomext AS(select st_setSRID(cast(st_envelope(s.geom) as geometry),4326)
as geom_ext, 4 as x_gridcnt, 4 as y_gridcnt, s.geom as geom
from (select geom from " + tempTableName + "
where gid = " + str(gid[0]) + ") as s),
grid_dim AS (select (st_xmax(geom_ext) -
st_xmin(geom_ext))/x_gridcnt as g_width,
st_xmin(geom_ext) as xmin, st_xmax(geom_ext) as xmax,
(st_ymax(geom_ext)-st_ymin(geom_ext))/y_gridcnt as g_height,
st_ymin(geom_ext) as ymin, st_ymax(geom_ext) as ymax from geomext),
grid AS (select x, y, st_setSrid(st_makeBox2d(st_point(xmin
(x-1)*g_width, ymin + (y-1)*g_height),
st_point(xmin + x*g_width, ymin + y*g_height)),4326) as grid_geom
from (select generate_series(1,x_gridcnt) from geomext) as x(x)
cross join (select generate_series(1,y_gridcnt) from geomext) as y(y)
cross join grid_dim)
select st_multi(st_intersection(geomext.geom,grid.grid_geom))
as geom into swat.mytemp
from geomext,grid
where st_intersects(geomext.geom,grid.grid_geom) and
st_geometrytype(st_intersection(geomext.geom,grid.grid_geom))
like '%Polygon';
insert into " + tempTableName +"
(geom) select geom from swat.mytemp;
drop table if exists swat.mytemp;
delete from " + tempTableName + " where gid = " + str(gid[0])
select gid from " + tempTableName + " where
st_npoints(geom) > " + verticeThreshhold
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment