Skip to content

Instantly share code, notes, and snippets.

@javisantana
Created April 29, 2014 08:31
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 javisantana/11394052 to your computer and use it in GitHub Desktop.
Save javisantana/11394052 to your computer and use it in GitHub Desktop.
// same than CDB_XYZ_Extent but this does not include the points just
// on the tile border for upper bounds, i.e for resolution=1 and tile_size=256
// the values after normalizing to tile coordinates are in range [0, 255]
function CDB_XYZ_Extent_Without_Upper_Border(x, y, z, resolution) {
var initial_resolution = CDB_XYZ_Resolution(0);
var origin_shift = (initial_resolution * tile_size) / 2.0;
var pixres = initial_resolution / Math.pow(2,z);
var tile_geo_size = tile_size * pixres;
var xmin = -origin_shift + x*tile_geo_size;
var xmax = -origin_shift + (x+1)*tile_geo_size - pixres*resolution;
var ymin = origin_shift - y*tile_geo_size;
var ymax = origin_shift - (y+1)*tile_geo_size + pixres*resolution;
return {
xmin: xmin, ymin: ymin, xmax: xmax, ymax: ymax
};
}
var tile_sql = "" +
"WITH " +
"par AS (" +
" SELECT ({xyz_resolution})*{resolution} as res" +
", 256/{resolution} as tile_size" +
", 1.0/(({xyz_resolution})*{resolution}) as resinv " +
", ST_MakeEnvelope({xmin}, {ymin}, {xmax}, {ymax}, {srid}) as ext ) " +
"SELECT xx x__uint8, " +
"yy y__uint8, " +
"array_agg(c) vals__uint8, " +
"array_agg(d) dates__uint16 " +
"FROM ( " +
"select " +
"round((st_x(i.the_geom_webmercator) - st_xmin(p.ext))*resinv) as xx, " +
"round((st_y(i.the_geom_webmercator) - st_ymin(p.ext))*resinv) as yy " +
", {countby} c " +
", floor(({column_conv} - {start})/{step}) d " +
"FROM ({_sql}) i, par p " +
"WHERE i.{gcol} && p.ext " +
"GROUP BY xx, yy, d " +
") cte, par " +
"GROUP BY x__uint8, y__uint8; ";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment