Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions backend/oeps/clients/explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ def build_map_config(self, upload: bool = False):
config_dir = Path(self.root_dir, "config")
config_dir.mkdir(parents=True, exist_ok=True)

# begin by creating a lookup for all geodata sources that will be used in the explorer.
# only source files with an "explorer_config" entry will be used
# Lookup of explorer-enabled geodata sources, keyed by registry name.
# Multiple vintages per summary level are allowed (e.g. 2018 + 2020 tracts).
geodata_lookup = {}
for id, data in self.registry.geodata_sources.items():
if data.explorer_config:
data.explorer_config["summary_level"] = data.summary_level.name
geodata_lookup[data.explorer_config["summary_level"]] = data.explorer_config
cfg = {
**data.explorer_config,
"summary_level": data.summary_level.name,
"tables": {},
}
geodata_lookup[id] = cfg

# iterate all variables and create a lookup for all combinations of data sources
# in which each variable has a value
Expand Down Expand Up @@ -90,8 +94,28 @@ def build_map_config(self, upload: bool = False):
"type": "characteristic",
"join": "HEROP_ID",
}
geodata_source = self.registry.geodata_sources[ts.geodata_source]
geodata_lookup[geodata_source.summary_level.name]["tables"][k] = table_entry
# Attach CSV to the matching geography vintage when possible.
# Fall back to any explorer-enabled source at the same summary level
# (legacy behavior for tables whose geodata has no explorer_config).
target_cfg = geodata_lookup.get(ts.geodata_source)
if target_cfg is None:
geodata_source = self.registry.geodata_sources[ts.geodata_source]
level = geodata_source.summary_level.name
target_cfg = next(
(
cfg
for cfg in geodata_lookup.values()
if cfg["summary_level"] == level
),
None,
)
if target_cfg is None:
print(
f"WARNING: no explorer geodata for table source {ds} "
f"(geodata_source={ts.geodata_source}); skipping map table"
)
continue
target_cfg["tables"][k] = table_entry

out_variables = {
k: {
Expand All @@ -105,8 +129,8 @@ def build_map_config(self, upload: bool = False):
if k in variables_to_ds_combos
}

# hacky method for creating the output geodata source list in descending order of
# spatial resolution
# Emit all explorer-enabled sources, coarsest to finest; stable name order
# within each summary level (keeps US Tracts (2020) before US Tracts (2018)).
s_geodata = [
i for i in geodata_lookup.values() if i["summary_level"] == "state"
]
Expand All @@ -119,8 +143,8 @@ def build_map_config(self, upload: bool = False):
]
out_sources = {"sources": []}
for sorted_src_list in [s_geodata, c_geodata, z_geodata, t_geodata]:
if len(sorted_src_list) > 0:
out_sources["sources"].append(sorted_src_list[0])
for src in sorted(sorted_src_list, key=lambda x: x.get("name", "")):
out_sources["sources"].append(src)

if upload:
prefix = "explorer/csvs"
Expand Down
8 changes: 8 additions & 0 deletions backend/oeps/registry/geodata_sources/geo-2018-tract.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
"title": "Census Tract Boundaries, 2018",
"description": "Shapefile of census tract boundaries from the US Census Bureau, 2018.",
"path": "https://herop-geodata.s3.us-east-2.amazonaws.com/census/tract-2018-500k-shp.zip",
"explorer_config": {
"name": "US Tracts (2018)",
"geodata": "Tracts 2018 [tiles]",
"tiles": "herop-lab.0eeozlm3",
"id": "HEROP_ID",
"bounds": [-125.109215, -66.925621, 25.043926, 49.295128],
"tables": {}
},
"format": "shp",
"mediatype": "application/vnd.shp",
"summary_level": "tract",
Expand Down
4 changes: 2 additions & 2 deletions backend/oeps/registry/geodata_sources/geo-2020-tract.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"description": "Shapefile of census tract boundaries from the US Census Bureau, 2020.",
"path": "https://herop-geodata.s3.us-east-2.amazonaws.com/census/tract-2020-500k-shp.zip",
"explorer_config": {
"name": "US Tracts",
"geodata": "Tracts [tiles]",
"name": "US Tracts (2020)",
"geodata": "Tracts 2020 [tiles]",
"tiles": "herop-lab.9abvk6o2",
"id": "HEROP_ID",
"bounds": [-125.109215, -66.925621, 25.043926, 49.295128],
Expand Down
Loading
Loading