-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: ♻️ 🚚 break map features out into individual files for easie…
…r maintenance and easier to read code
- Loading branch information
1 parent
ab587fe
commit 6ab68ef
Showing
6 changed files
with
153 additions
and
125 deletions.
There are no files selected for viewing
This file contains 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 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,30 @@ | ||
from typing import TypeVar, TypeAlias, Sequence, Hashable, Callable | ||
|
||
import overpy | ||
import pydantic | ||
|
||
from render_map import mapping | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
def choose_item_from_list(list_: Sequence[T], criterion: Hashable) -> T: | ||
"""Choose an item in such a way that it is fully deterministic and reproducible. The items must also be chosen uniformly. | ||
This is done by effectively using a poor-man's hash function with a co-domain of the length of the list. | ||
Args: | ||
list_: The list to choose from. | ||
criterion: The criterion to choose the item by. | ||
Returns: | ||
An item from the list. | ||
Raises: | ||
ValueError: If the list is empty. | ||
""" | ||
if len(list_) == 0: | ||
raise ValueError("List must not be empty.") | ||
|
||
index = hash(criterion) % len(list_) | ||
return list_[index] |
Empty file.
55 changes: 55 additions & 0 deletions
55
render_map/auto_populate/map_features/auto_populate_gas_stations.py
This file contains 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,55 @@ | ||
import overpy | ||
|
||
from render_map import mapping | ||
from render_map.auto_populate import auto_populate_utils | ||
from render_map.auto_populate.map_features import map_features_utils | ||
|
||
GAS_STATION_QUERY = """[out:json]; | ||
(node["amenity"="fuel"](around:{radius},{lat},{lon}); | ||
node["amenity"="charging_station"](around:{radius},{lat},{lon}); | ||
node[name="Buc-ee's"](around:{radius},{lat},{lon}); | ||
node[brand="Buc-ee's"](around:{radius},{lat},{lon}); | ||
way[brand="Buc-ee's"][shop="convenience"](around:{radius},{lat},{lon}); | ||
); | ||
(._;>;); | ||
out meta; | ||
""" | ||
GAS_STATIONS: list[tuple[str, mapping.map_icons.MapIcon]] = [ | ||
("Red Rocket", mapping.map_icons.MapIcon.ROCKET), | ||
("Poseidon Energy", mapping.map_icons.MapIcon.POSEIDON), | ||
# ("Petro-Chico", mapping.map_icons.MapIcon.SOMBRERO), | ||
("Gas Station", mapping.map_icons.MapIcon.GAS_STATION), | ||
] | ||
|
||
|
||
def choose_gas_station_name_zoom_icon(node: overpy.Node | overpy.Way) -> map_features_utils.NameZoomIcon: | ||
"""Choose the game name and map zoom level for a supermarket, based on the properties of the supermarket in the | ||
real world. | ||
Args: | ||
node: The node in OpenStreetMap representing the supermarket. | ||
Returns: | ||
Game name and map zoom level for the supermarket. | ||
""" | ||
name_from_node = node.tags.get("name", "") | ||
brand_from_node = node.tags.get("brand", "") | ||
# Womb-ee's is a fictional gas station chain in the Fallout: Houston campaign. | ||
# It is a parody of Buc-ee's, a real gas station chain in Texas. | ||
if "buc-ee" in name_from_node.lower() or "buc-ee" in brand_from_node.lower(): | ||
return "Womb-ee's", mapping.ZoomLevel.WASTELAND, mapping.map_icons.MapIcon.BEAVER | ||
# If the gas station is not named in OpenStreetMap, we'll (unfairly) assume it's not very important. | ||
if name_from_node is None: | ||
return None, mapping.ZoomLevel.WASTELAND, mapping.map_icons.MapIcon.GAS_STATION | ||
|
||
name, icon = auto_populate_utils.choose_item_from_list(GAS_STATIONS, name_from_node) | ||
|
||
# # We want only about a quarter of the gas stations to be visible from the large wasteland map. | ||
# zoom_level = mapping.ZoomLevel.TOWN if hash((name_from_node, node.id)) % 4 else mapping.ZoomLevel.WASTELAND | ||
zoom_level = mapping.ZoomLevel.TOWN | ||
return name, zoom_level, icon | ||
|
||
|
||
GasStationFeatureMetadata = map_features_utils.FeaturePopulateMetadata( | ||
feature_type_name="gas stations", query=GAS_STATION_QUERY, choose_name_function=choose_gas_station_name_zoom_icon | ||
) |
42 changes: 42 additions & 0 deletions
42
render_map/auto_populate/map_features/auto_populate_super_markets.py
This file contains 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,42 @@ | ||
import overpy | ||
|
||
from render_map import mapping | ||
from render_map.auto_populate.map_features import map_features_utils | ||
|
||
SUPER_MARKET_QUERY = """[out:json]; | ||
(node["building"="supermarket"](around:{radius},{lat},{lon}); | ||
node["shop"="supermarket"](around:{radius},{lat},{lon}); | ||
way["shop"="supermarket"](around:{radius},{lat},{lon}); | ||
); | ||
(._;>;); | ||
out meta; | ||
""" | ||
|
||
|
||
def choose_supermarket_name_zoom_icon(node: overpy.Node) -> map_features_utils.NameZoomIcon: | ||
"""Choose the game name and map zoom level for a supermarket, based on the properties of the supermarket in the | ||
real world. | ||
Args: | ||
node: The node in OpenStreetMap representing the supermarket. | ||
Returns: | ||
Game name and map zoom level for the supermarket. | ||
""" | ||
name_from_node = node.tags.get("name", None) | ||
# If the supermarket is not named in OpenStreetMap, we'll (unfairly) assume it's not a very important supermarket. | ||
if name_from_node is None: | ||
return None, mapping.ZoomLevel.WASTELAND, mapping.map_icons.MapIcon.SUPER_DUPER_MART | ||
# Super-Duper Mart is implied to be a chain of very large supermarkets, likely wholesale. In the video games, there | ||
# is only one Super-Duper Mart in its corresponding city metro-area. | ||
if "walmart" in name_from_node.lower() or "sam's" in name_from_node.lower() or "costco" in name_from_node.lower(): | ||
# Only a quarter of the supermarkets should be visible from the large wasteland map. | ||
zoom_level = mapping.ZoomLevel.TOWN if node.id % 4 else mapping.ZoomLevel.WASTELAND | ||
return "Super-Duper Mart", zoom_level, mapping.map_icons.MapIcon.SUPER_DUPER_MART | ||
# TODO: Provide more plausible and generic names for super markets. | ||
return "Supermarket", mapping.ZoomLevel.TOWN, mapping.map_icons.MapIcon.SUPER_DUPER_MART | ||
|
||
|
||
SuperMarketFeatureMetadata = map_features_utils.FeaturePopulateMetadata( | ||
feature_type_name="supermarkets", query=SUPER_MARKET_QUERY, choose_name_function=choose_supermarket_name_zoom_icon | ||
) |
16 changes: 16 additions & 0 deletions
16
render_map/auto_populate/map_features/map_features_utils.py
This file contains 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,16 @@ | ||
import pydantic | ||
import overpy | ||
from typing import Callable, TypeAlias | ||
|
||
from render_map import mapping | ||
|
||
NameZoomIcon: TypeAlias = tuple[str | None, mapping.ZoomLevel, mapping.map_icons.MapIcon] | ||
|
||
|
||
@pydantic.dataclasses.dataclass | ||
class FeaturePopulateMetadata: | ||
"""Metadata for a feature type to populate the map with.""" | ||
|
||
feature_type_name: str | ||
query: str | ||
choose_name_function: Callable[[overpy.Node | overpy.Way], NameZoomIcon] |