Skip to content

Instantly share code, notes, and snippets.

@andrewxhill
Created September 11, 2012 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save andrewxhill/3701439 to your computer and use it in GitHub Desktop.
Save andrewxhill/3701439 to your computer and use it in GitHub Desktop.
Rasters to polygons for CartoDB
--Upload your new raster data
--In my case the data goes into a table, night_light
--In some cases, I find it useful to downsample the total number of variables in a band, or in some cases merge data from multiple bands into one.
--Here is an example how one such process would work
UPDATE night_light SET rast = ST_MapAlgebraExpr(rast,'8BUI','CASE WHEN [rast] = 0 THEN 10 WHEN [rast] = 255 THEN 11 ELSE floor([rast]/32)::int END');
--Create a new table, light_polygons
--Add a numeric column called, rast_value
--Add a numeric column called, band
--Bin raster data by value and store it in your new table
SET statement_timeout TO 0; INSERT INTO light_polygons (the_geom, rast_value, band) (select st_multi(st_makevalid((geomval).geom)) as the_geom, (geomval).val as rast_value, 1 as band from (select ST_DumpAsPolygons(rast) as geomval from night_light) f );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment