Skip to content

Commit 272f974

Browse files
committed
Read population from pixels.population instead of pixel_metadata
All population reads now use the pixels.population column directly, removing the JOIN to pixel_metadata for population data. The enrichment pipeline also writes to pixels.population when enriching population. Truncated the pixel_metadata table (data was duplicated). The pixel_metadata table is retained for non-population covariates.
1 parent ab3dcb1 commit 272f974

5 files changed

Lines changed: 20 additions & 17 deletions

File tree

truecover-backend/db/migrations/add_sampled_only_filter.sql

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,14 @@ BEGIN
7171
SELECT
7272
p.quadkey,
7373
p.geometry,
74-
(pm.metadata->>'population')::numeric AS population,
74+
p.population,
7575
lc.building_count
7676
FROM pixels p
7777
JOIN pixel_area pa ON p.quadkey = pa.quadkey
7878
JOIN campaign_areas ca ON pa.campaign_area_id = ca.id
7979
LEFT JOIN coverage_pixel cp ON p.quadkey = cp.quadkey
8080
AND cp.campaign_id = target_campaign_id
8181
AND (target_indicator_id IS NULL OR cp.indicator_id = target_indicator_id)
82-
LEFT JOIN pixel_metadata pm ON p.quadkey = pm.quadkey
8382
LEFT JOIN LATERAL (
8483
SELECT COUNT(*)::integer AS building_count
8584
FROM locations l

truecover-backend/db/migrations/optimize_pixels_by_campaign.sql

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,11 @@ BEGIN
6464
SELECT
6565
p.quadkey,
6666
p.geometry,
67-
(pm.metadata->>'population')::numeric AS population,
67+
p.population,
6868
lc.building_count
6969
FROM pixels p
7070
JOIN pixel_area pa ON p.quadkey = pa.quadkey
7171
JOIN campaign_areas ca ON pa.campaign_area_id = ca.id
72-
LEFT JOIN pixel_metadata pm ON p.quadkey = pm.quadkey
7372
LEFT JOIN LATERAL (
7473
SELECT COUNT(*)::integer AS building_count
7574
FROM locations l

truecover-backend/routes/campaigns.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -581,10 +581,9 @@ def compute_pixels_for_area(user, area_id):
581581
WITH pixel_stats AS (
582582
SELECT
583583
COUNT(*) as pixel_count,
584-
COALESCE(SUM((pm.metadata->>'population')::numeric), 0) as total_population
584+
COALESCE(SUM(p.population), 0) as total_population
585585
FROM pixel_area pa
586586
JOIN pixels p ON pa.quadkey = p.quadkey
587-
LEFT JOIN pixel_metadata pm ON pa.quadkey = pm.quadkey
588587
WHERE pa.campaign_area_id = %s
589588
),
590589
location_counts AS (
@@ -696,10 +695,9 @@ def compute_all_pixels_for_campaign(user, campaign_id):
696695
WITH pixel_stats AS (
697696
SELECT
698697
COUNT(*) as pixel_count,
699-
COALESCE(SUM((pm.metadata->>'population')::numeric), 0) as total_population
698+
COALESCE(SUM(p.population), 0) as total_population
700699
FROM pixel_area pa
701700
JOIN pixels p ON pa.quadkey = p.quadkey
702-
LEFT JOIN pixel_metadata pm ON pa.quadkey = pm.quadkey
703701
WHERE pa.campaign_area_id = %s
704702
),
705703
location_counts AS (

truecover-backend/temporal/activities/enrichment.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,17 @@ async def enrich_area_pixels(
341341
metadata = pixel_metadata.metadata || EXCLUDED.metadata,
342342
updated_at = NOW()
343343
""", updates)
344+
345+
# Also write to pixels.population column for population data
346+
if metadata_field_name == 'population':
347+
pop_updates = [
348+
(json.loads(metadata_json)[metadata_field_name], quadkey)
349+
for quadkey, metadata_json in updates
350+
]
351+
cursor.executemany("""
352+
UPDATE pixels SET population = %s WHERE quadkey = %s
353+
""", pop_updates)
354+
344355
conn.commit()
345356
total_updated += len(updates)
346357

@@ -354,11 +365,11 @@ async def enrich_area_pixels(
354365
SET cached_population = sub.total_pop
355366
FROM (
356367
SELECT pa.campaign_area_id,
357-
COALESCE(SUM((pm.metadata->>'population')::numeric), 0) as total_pop
368+
COALESCE(SUM(p.population), 0) as total_pop
358369
FROM pixel_area pa
359-
JOIN pixel_metadata pm ON pa.quadkey = pm.quadkey
370+
JOIN pixels p ON pa.quadkey = p.quadkey
360371
JOIN campaign_areas ca2 ON pa.campaign_area_id = ca2.id
361-
WHERE ca2.campaign_id = %s AND pm.metadata ? 'population'
372+
WHERE ca2.campaign_id = %s
362373
GROUP BY pa.campaign_area_id
363374
) sub
364375
WHERE ca.id = sub.campaign_area_id

truecover-backend/temporal/activities/rounds.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,8 @@ async def fetch_coverage_for_sampling(
116116
pop_filter = ""
117117
pop_params = []
118118

119-
if min_population is not None and population_field:
120-
import re
121-
if not re.match(r'^[a-zA-Z_][a-zA-Z0-9_]*$', population_field):
122-
raise ValueError(f"Invalid population field name: {population_field}")
123-
pop_join = "LEFT JOIN pixel_metadata pm ON p.quadkey = pm.quadkey"
124-
pop_filter = f"AND (pm.metadata->>'{population_field}')::float >= %s"
119+
if min_population is not None:
120+
pop_filter = "AND p.population >= %s"
125121
pop_params = [min_population]
126122

127123
if allow_revisit:

0 commit comments

Comments
 (0)