|
1 | | --- Most recent migration: moped-database/migrations/default/1755539630397_add-project-district-analysis-m-views/up.sql |
| 1 | +-- Most recent migration: moped-database/migrations/default/1758551918596_adjust-projects-per-district-view/up.sql |
2 | 2 |
|
3 | 3 | CREATE OR REPLACE VIEW council_district_project_distribution_analytics AS WITH area_project_buffers AS ( |
4 | 4 | SELECT |
5 | 5 | projects.project_id, |
6 | | - st_buffer(st_transform(projects.geography::geometry, 2277), 33::double precision) AS buffer_geom |
| 6 | + st_buffer(st_transform(projects.geography::geometry, 2277), 100::double precision) AS buffer_geom |
7 | 7 | FROM project_geography projects |
8 | | - WHERE st_geometrytype(projects.geography::geometry) = any(ARRAY['ST_MultiPoint'::text, 'ST_MultiLineString'::text]) |
| 8 | + JOIN moped_project mp ON projects.project_id = mp.project_id |
| 9 | + WHERE mp.is_deleted = false AND (st_geometrytype(projects.geography::geometry) = any(ARRAY['ST_MultiPoint'::text, 'ST_MultiLineString'::text])) |
| 10 | +), |
| 11 | + |
| 12 | +dissolved_project_buffers AS ( |
| 13 | + SELECT |
| 14 | + area_project_buffers.project_id, |
| 15 | + st_unaryunion(st_collect(area_project_buffers.buffer_geom)) AS dissolved_buffer_geom |
| 16 | + FROM area_project_buffers |
| 17 | + GROUP BY area_project_buffers.project_id |
9 | 18 | ), |
10 | 19 |
|
11 | 20 | area_district_aggregates AS ( |
12 | 21 | SELECT |
13 | 22 | project_buffers.project_id, |
14 | 23 | districts.council_district AS council_district_id, |
15 | | - sum(st_area(st_intersection(project_buffers.buffer_geom, st_transform(districts.geography::geometry, 2277)))) AS total_area_in_district_sq_ft |
16 | | - FROM area_project_buffers project_buffers |
17 | | - JOIN layer_council_district districts ON st_intersects(project_buffers.buffer_geom, st_transform(districts.geography::geometry, 2277)) |
| 24 | + sum(st_area(st_intersection(project_buffers.dissolved_buffer_geom, st_transform(districts.geography::geometry, 2277)))) AS total_area_in_district_sq_ft |
| 25 | + FROM dissolved_project_buffers project_buffers |
| 26 | + JOIN layer_council_district districts ON st_intersects(project_buffers.dissolved_buffer_geom, st_transform(districts.geography::geometry, 2277)) |
18 | 27 | GROUP BY project_buffers.project_id, districts.council_district |
19 | 28 | ), |
20 | 29 |
|
|
0 commit comments