Repository navigation
Show effective terrain as a web map layer #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import json | ||
| import unittest | ||
| from unittest.mock import mock_open, patch | ||
|
|
||
| from src.api import data_routes | ||
|
|
||
|
|
||
| class CompiledInfrastructureTests(unittest.TestCase): | ||
| def test_copies_only_finite_infrastructure_values(self) -> None: | ||
| province_data = [ | ||
| { | ||
| "id": 1, | ||
| "terrain_value": 0.4, | ||
| "infrastructure": 12, | ||
| "infrastructure_fill": 0.6, | ||
| "effective_terrain": 0.61, | ||
| }, | ||
| { | ||
| "id": 2, | ||
| "terrain_value": "0.4", | ||
| "infrastructure": float("nan"), | ||
| "infrastructure_fill": None, | ||
| "effective_terrain": True, | ||
| }, | ||
| ] | ||
| with ( | ||
| patch.object(data_routes, "load_province_metadata", return_value={1: {"terrain": "bog"}, 2: {"terrain": "plains"}}), | ||
| patch.object(data_routes, "load_infestation_by_id", return_value={}), | ||
| patch.object(data_routes, "input_file", return_value="province_data.json"), | ||
| patch("builtins.open", mock_open(read_data=json.dumps(province_data))), | ||
| ): | ||
| result = data_routes.build_compiled_provinces("main") | ||
|
|
||
| self.assertEqual( | ||
| {key: result[1][key] for key in ("terrain_value", "infrastructure", "infrastructure_fill", "effective_terrain")}, | ||
| {"terrain_value": 0.4, "infrastructure": 12, "infrastructure_fill": 0.6, "effective_terrain": 0.61}, | ||
| ) | ||
| for field in ("terrain_value", "infrastructure", "infrastructure_fill", "effective_terrain"): | ||
| self.assertNotIn(field, result[2]) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| import json | ||
| import math | ||
| import os | ||
|
|
||
| from ..loader.provinces import load_provinces | ||
| from ..loader.province_metadata import load_province_metadata | ||
| from ..util.dirs import input_file, validate_map | ||
| from .geometry_cache import MapGeometryCache | ||
| from .map_paint_numpy import ( | ||
| load_provinces_array, | ||
| paint_from_rgb_lut, | ||
| rgba_array_to_image, | ||
| ) | ||
|
|
||
|
|
||
| INFRASTRUCTURE_TERRAIN_MIN = 0.30 | ||
| INFRASTRUCTURE_TERRAIN_MAX = 0.75 | ||
| INFRASTRUCTURE_COLOR_STOPS = ( | ||
| (244, 160, 160), | ||
| (140, 70, 170), | ||
| (20, 40, 120), | ||
| ) | ||
| INFRASTRUCTURE_ALPHA = 200 | ||
| SKIP_TERRAINS = {"water", "sea"} | ||
|
|
||
|
|
||
| def infrastructure_to_color(value: float) -> tuple[int, int, int] | None: | ||
| if isinstance(value, bool) or not isinstance(value, (int, float)): | ||
| return None | ||
| try: | ||
| if not math.isfinite(value): | ||
| return None | ||
| except OverflowError: | ||
| return None | ||
| t = max( | ||
| 0.0, | ||
| min( | ||
| 1.0, | ||
| (value - INFRASTRUCTURE_TERRAIN_MIN) | ||
| / (INFRASTRUCTURE_TERRAIN_MAX - INFRASTRUCTURE_TERRAIN_MIN), | ||
| ), | ||
| ) | ||
| stops = INFRASTRUCTURE_COLOR_STOPS | ||
| if t <= 0.5: | ||
| first, second, fraction = stops[0], stops[1], t * 2 | ||
| else: | ||
| first, second, fraction = stops[1], stops[2], (t - 0.5) * 2 | ||
| return tuple( | ||
| math.floor(a + (b - a) * fraction + 0.5) | ||
| for a, b in zip(first, second) | ||
| ) | ||
|
|
||
|
|
||
| def create_infrastructure_map( | ||
| map_name: str, | ||
| filename: str = "infrastructure", | ||
| cache: MapGeometryCache | None = None, | ||
| ): | ||
| validate_map(map_name) | ||
|
|
||
| province_rgb_to_id = load_provinces(map_name) | ||
| province_meta = load_province_metadata(map_name) | ||
| try: | ||
| with open(input_file(map_name, "province_data.json"), "r") as f: | ||
| province_data = json.load(f) | ||
| except (OSError, json.JSONDecodeError): | ||
| province_data = [] | ||
| if not isinstance(province_data, list): | ||
| province_data = [] | ||
|
|
||
| province_by_id = { | ||
| p.get("id"): p | ||
| for p in province_data | ||
| if isinstance(p, dict) and isinstance(p.get("id"), (int, str)) | ||
| } | ||
| rgb_to_rgba = {} | ||
|
|
||
| for rgb, pid in province_rgb_to_id.items(): | ||
| meta = province_meta.get(pid) | ||
| if not isinstance(meta, dict) or meta.get("terrain") in SKIP_TERRAINS: | ||
| continue | ||
| row = province_by_id.get(pid) | ||
| if row is None: | ||
| row = province_by_id.get(str(pid)) | ||
| if not isinstance(row, dict): | ||
| continue | ||
| color = infrastructure_to_color(row.get("effective_terrain")) | ||
| if color is not None: | ||
| rgb_to_rgba[rgb] = (*color, INFRASTRUCTURE_ALPHA) | ||
|
|
||
| provinces = ( | ||
| cache.provinces_rgba | ||
| if cache is not None | ||
| else load_provinces_array(input_file(map_name, "provinces.png")) | ||
| ) | ||
| painted = paint_from_rgb_lut(provinces, rgb_to_rgba, skip_black=False) | ||
| painted_pixels = int((painted[:, :, 3] > 0).sum()) | ||
| output_path = os.path.abspath( | ||
| os.path.join( | ||
| os.path.dirname(input_file(map_name, "dummy")), | ||
| "..", "..", "output", map_name, "maps", f"{filename}.png" | ||
| ) | ||
| ) | ||
| os.makedirs(os.path.dirname(output_path), exist_ok=True) | ||
| rgba_array_to_image(painted).save(output_path, "PNG") | ||
| print(f"🛠 Infrastructure map generated → {output_path} | painted={painted_pixels:,}") |
21 changes: 21 additions & 0 deletions
21
backend/src/scripts/mapgen/mapmodes/infrastructure_mapmode.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import argparse | ||
| import sys | ||
|
|
||
| from ...mapgen.infrastructuregen import create_infrastructure_map | ||
| from ...util.dirs import validate_map | ||
|
|
||
|
|
||
| def main() -> None: | ||
| if hasattr(sys.stdout, "reconfigure"): | ||
| sys.stdout.reconfigure(encoding="utf-8", errors="replace") | ||
|
|
||
| parser = argparse.ArgumentParser(description="Generate infrastructure overlay map PNG") | ||
| parser.add_argument("--map", default="dev", help="Map id (e.g. main, dev)") | ||
| args = parser.parse_args() | ||
|
|
||
| validate_map(args.map) | ||
| create_infrastructure_map(args.map, filename="infrastructure_map") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import unittest | ||
|
|
||
| from .infrastructuregen import ( | ||
| INFRASTRUCTURE_COLOR_STOPS, | ||
| infrastructure_to_color, | ||
| ) | ||
|
|
||
|
|
||
| class InfrastructureColorTests(unittest.TestCase): | ||
| def test_color_stops_and_clamping(self) -> None: | ||
| self.assertEqual(infrastructure_to_color(0.30), INFRASTRUCTURE_COLOR_STOPS[0]) | ||
| self.assertEqual(infrastructure_to_color(0.525), INFRASTRUCTURE_COLOR_STOPS[1]) | ||
| self.assertEqual(infrastructure_to_color(0.75), INFRASTRUCTURE_COLOR_STOPS[2]) | ||
| self.assertEqual(infrastructure_to_color(0.1), INFRASTRUCTURE_COLOR_STOPS[0]) | ||
| self.assertEqual(infrastructure_to_color(0.9), INFRASTRUCTURE_COLOR_STOPS[2]) | ||
|
|
||
| def test_bad_values_have_no_color(self) -> None: | ||
| for value in (None, "0.4", float("nan"), float("inf")): | ||
| with self.subTest(value=value): | ||
| self.assertIsNone(infrastructure_to_color(value)) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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
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
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
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
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
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
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
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.