Last active
September 19, 2016 20:27
-
-
Save Nate-Wessel/82e6432f37a24baab0356154ee96e199 to your computer and use it in GitHub Desktop.
generic areal apportionment of average SQL script
This file contains hidden or 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
/* | |
generic areal apportionment of average | |
*/ | |
--table1.id | |
--table1.geom | |
--table1.value | |
--table2.geom | |
--table2.weight | |
WITH dat AS ( | |
SELECT | |
table1.id, | |
-- sum( value * weight * intersection ) | |
SUM( table2.value * table2.weight * ST_Area(ST_Intersection(table1.geom,table2.geom)) ) | |
-- / sum( intersection * weight ) | |
/ | |
SUM( ST_Area(ST_Intersection(table1.geom,table2.geom)) * table2.weight ) AS newval | |
FROM table1 JOIN table2 ON ST_Intersects(table1.geom,table2.geom) | |
-- exclude missing data | |
--WHERE table2.value != -999 AND table2.weight != -999 | |
GROUP BY table1.id | |
) | |
UPDATE table1 SET value = dat.newval | |
FROM dat WHERE dat.id = table1.id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment