Skip to content

Instantly share code, notes, and snippets.

@andrewxhill
Created November 9, 2012 17:10
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 andrewxhill/4046900 to your computer and use it in GitHub Desktop.
Save andrewxhill/4046900 to your computer and use it in GitHub Desktop.
carbon poly tool sum SQL
WITH qg AS (SELECT ST_GeomFromText('MULTIPOLYGON(((-108.47016601563018 26.598345625936616,-100.38422851563018 25.967916642336085,-100.38422851563018 18.1562854981589,-106.71235351563018 21.790101289728835,-108.47016601563018 26.598345625936616)))',4326) as geom),
cis AS (
SELECT rid,rast FROM carbonintersection, qg
WHERE
ST_Intersects(rast, geom) )
SELECT
SUM((ST_Value(rast, 1, x, y) / 100) * ((ST_Area(ST_Transform(ST_SetSRID(ST_PixelAsPolygon(rast, x, y), 4326), 954009)) / 10000) / 100)) AS total,
ST_Area(geom::geography) as area
FROM
qg,
cis
CROSS JOIN
generate_series(1,10) As x
CROSS JOIN
generate_series(1,10) As y
WHERE
ST_Intersects( ST_Translate(ST_SetSRID(ST_Point(ST_UpperLeftX(rast) + (ST_ScaleX(rast)/2), ST_UpperLeftY(rast) + (ST_ScaleY(rast)/2)), 4326), ST_ScaleX(rast)*x, ST_ScaleY(rast)*y), geom )
GROUP BY geom;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment