Created
November 9, 2012 17:10
-
-
Save andrewxhill/4046900 to your computer and use it in GitHub Desktop.
carbon poly tool sum SQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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