Created
May 7, 2025 21:35
-
-
Save jj0hns0n/d2ceb4ddbd72f2db3dd6340ff256c29b to your computer and use it in GitHub Desktop.
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
| WITH weights AS ( | |
| SELECT | |
| 0.8 AS w_structure, | |
| 0.4 AS w_population | |
| ), | |
| base AS ( | |
| SELECT | |
| 100.0 - ( | |
| (AVG(DISTINCT legatum_prosperity_score) + | |
| AVG(DISTINCT legatum_safety_and_security) + | |
| AVG(DISTINCT legatum_property_crime)) / 3.0 | |
| ) AS base | |
| FROM h3_l8_union_cities_urban | |
| WHERE adm0_iso = 'SWE' | |
| ), | |
| normalized AS ( | |
| SELECT | |
| t.h3, | |
| ( | |
| ((COALESCE(t.geosure_structure_class, 6.5) - 1) / 11.0) * w.w_structure + | |
| ((COALESCE(t.geosure_population_class, 6.5) - 1) / 11.0) * w.w_population | |
| ) AS risk_raw | |
| FROM h3_l8_union_cities_urban t, weights w | |
| WHERE t.adm0_iso = 'SWE' | |
| ), | |
| rescaled AS ( | |
| SELECT | |
| n.h3, | |
| ROUND(LEAST(GREATEST(b.base + (n.risk_raw * 12.0 - 4.0), 0), 100))::integer AS geosure_adm0_risk | |
| FROM normalized n | |
| CROSS JOIN base b | |
| ) | |
| UPDATE h3_l8_union_cities_urban t | |
| SET geosure_adm0_risk = r.geosure_adm0_risk | |
| FROM rescaled r | |
| WHERE t.h3 = r.h3; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment