Skip to content

Instantly share code, notes, and snippets.

@talos
Created September 27, 2016 15:31
Show Gist options
  • Save talos/0708837aa1f3cff95376e2d931e7930b to your computer and use it in GitHub Desktop.
Save talos/0708837aa1f3cff95376e2d931e7930b to your computer and use it in GitHub Desktop.
Create a grid in PostGIS for CARTO based off of arbitrary envelope and Projection
INSERT INTO <my_table_name> (cartodb_id, the_geom, the_geom_webmercator)
WITH inputs AS (SELECT
ST_Transform(ST_SetSRID(ST_MakeEnvelope(
-126.21093749999999, 23.241346102386135,
-63.28125, 50.064191736659104
), 4326), 5070) geom,
500000 width,
500000 height
), rast AS (SELECT
(st_pixelaspolygons(
st_addband(
st_makeemptyraster(
((st_xmax(geom) - st_xmin(geom)) / width)::int,
((st_ymax(geom) - st_ymin(geom)) / height)::int,
st_xmin(geom), st_ymax(geom), width::numeric, -height::numeric,
0.0, -- skew x
0.0, -- skew y
st_srid(geom) -- srid
), 1, '2BUI', 1
), 1, False
)).geom FROM inputs
)
SELECT row_number() over () as cartodb_id,
st_transform(geom, 4326) as the_geom,
st_transform(geom, 3857) AS the_geom_webmercator
FROM rast
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment