Skip to content

Commit

Permalink
feat: auto-populate markets from template
Browse files Browse the repository at this point in the history
  • Loading branch information
qthequartermasterman committed Dec 24, 2023
1 parent 66dd58c commit 303485e
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 108 deletions.
102 changes: 1 addition & 101 deletions docs/Background/geography/loot_locations.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,104 +13,4 @@ included in the list solely to contain a geotag to be used on the auto-rendered

Super Markets are a great place to find food, medicine, and other useful items.

<geotag name="Supermarket" zoom=TOWN latitude=29.8779870 longitude=-95.6488309 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.6000278 longitude=-95.6233281 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7670296 longitude=-95.5515031 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.6216449 longitude=-95.5578334 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.6902257 longitude=-95.4151183 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7392004 longitude=-95.4111693 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.6875540 longitude=-95.4913350 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.8225916 longitude=-95.5232432 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.8210878 longitude=-95.5249442 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7896319 longitude=-95.4112065 icon=SUPER_DUPER_MART />
<geotag name="Super-Duper Mart" zoom=WASTELAND latitude=29.8606530 longitude=-95.7164480 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.8711790 longitude=-95.4061220 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7543255 longitude=-95.3616311 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7711432 longitude=-95.5991940 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.6723340 longitude=-95.5300380 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7504960 longitude=-95.7983050 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7117132 longitude=-95.3128462 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7524669 longitude=-95.4999989 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.8781696 longitude=-95.7220263 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7794337 longitude=-95.7170091 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7892877 longitude=-95.7167418 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7911709 longitude=-95.7213141 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7123729 longitude=-95.7718631 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7809732 longitude=-95.7490959 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.5973965 longitude=-95.5253515 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.5636313 longitude=-95.5655009 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.5826319 longitude=-95.5834046 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.5886311 longitude=-95.5995381 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.5431629 longitude=-95.5481738 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.6078877 longitude=-95.4775703 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.8368658 longitude=-95.6446754 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.8356222 longitude=-95.6483971 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.8322662 longitude=-95.7046406 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.8316729 longitude=-95.7085700 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7495946 longitude=-95.4618721 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.8105410 longitude=-95.5256538 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.8020916 longitude=-95.4159097 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.8229583 longitude=-95.4097761 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.5945796 longitude=-95.4663316 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.8809901 longitude=-95.7002784 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.8126507 longitude=-95.7701896 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.8657949 longitude=-95.6702815 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.8667256 longitude=-95.6839984 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7058516 longitude=-95.5468867 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7381154 longitude=-95.6247919 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.8784732 longitude=-95.6671319 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7013659 longitude=-95.5773981 icon=SUPER_DUPER_MART />
<geotag name="Super-Duper Mart" zoom=WASTELAND latitude=29.7439900 longitude=-95.7930340 icon=SUPER_DUPER_MART />
<geotag name="Super-Duper Mart" zoom=WASTELAND latitude=29.6521000 longitude=-95.6174000 icon=SUPER_DUPER_MART />
<geotag name="Super-Duper Mart" zoom=WASTELAND latitude=29.6792670 longitude=-95.5613180 icon=SUPER_DUPER_MART />
<geotag name="Super-Duper Mart" zoom=WASTELAND latitude=29.7354850 longitude=-95.5116920 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.8111878 longitude=-95.6878767 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.8535816 longitude=-95.6824645 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.6464818 longitude=-95.5827063 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.8826714 longitude=-95.3916599 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.9161600 longitude=-95.4043967 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7911983 longitude=-95.5209816 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7527466 longitude=-95.5016279 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7404182 longitude=-95.7753557 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7930173 longitude=-95.7746464 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.8073613 longitude=-95.4088622 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.8045727 longitude=-95.4004585 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7968241 longitude=-95.4864439 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7466025 longitude=-95.4863817 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.5516946 longitude=-95.5843055 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.6894869 longitude=-95.5015487 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7688039 longitude=-95.3966726 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.5889911 longitude=-95.6003083 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.8137026 longitude=-95.3981434 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7350969 longitude=-95.5705068 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.8316216 longitude=-95.4848820 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.9598151 longitude=-95.4787987 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7745983 longitude=-95.4002443 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.5298455 longitude=-95.5108212 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7757349 longitude=-95.7502060 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.5606271 longitude=-95.2768647 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.6638346 longitude=-95.6441845 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7494691 longitude=-95.7503867 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7068749 longitude=-95.8472445 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7362661 longitude=-95.5335481 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.8180432 longitude=-95.5447357 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.9078721 longitude=-95.5857979 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=30.0020036 longitude=-95.5599323 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.8699592 longitude=-95.3325185 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7219216 longitude=-95.5015286 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7220201 longitude=-95.5006682 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.9339541 longitude=-95.4005259 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7823174 longitude=-95.5221864 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7059387 longitude=-95.5467615 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7384958 longitude=-95.5370970 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.8136332 longitude=-95.4605775 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.6031980 longitude=-95.2149315 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.8480550 longitude=-95.3915426 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.6748622 longitude=-95.2910328 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7843221 longitude=-95.3617855 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.8669271 longitude=-95.4858994 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.6501737 longitude=-95.5653613 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7063997 longitude=-95.5539970 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7426202 longitude=-95.5604977 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.7029754 longitude=-95.5541551 icon=SUPER_DUPER_MART />
<geotag name="Supermarket" zoom=TOWN latitude=29.8682858 longitude=-95.4721302 icon=SUPER_DUPER_MART />
<populate_geotag supermarket />
7 changes: 6 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,16 @@ plugins:
- search

hooks:
- render_map/auto_populate/auto_populate_hooks.py
- render_map/plugin_hooks.py

extra:
# GOOGLE_MAPS_API_KEY: !ENV GOOGLE_MAPS_API_KEY # ReadTheDocs doesn't support loading from env variables!
GOOGLE_MAPS_API_KEY: AIzaSyD6gTw8OwC-LyQi3wl_gr8P385pPBfa5tE
# Center and default zoom level for the Google Maps
global_map:
center: [29.7604, -95.3698]
zoom: 9
zoom: 9
auto_populate:
center: [29.7604, -95.3698]
population_radius: 35000 # Radius around the center to auto-populate locations (in meters)
Empty file.
34 changes: 34 additions & 0 deletions render_map/auto_populate/auto_populate_hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from __future__ import annotations

import mkdocs.plugins

from render_map.auto_populate import auto_populate_map

@mkdocs.plugins.event_priority(100)
def on_page_markdown(
markdown: str,
page: mkdocs.plugins.Page,
config: mkdocs.plugins.MkDocsConfig,
**kwargs,
):
"""Find all geotags in the markdown and process them into a list of GeoLink objects.
Args:
markdown: The markdown to search for geotags.
page: The page object.
config: The mkdocs config.
Returns:
A list of GeoLink objects.
"""
# Skip, if page is excluded
if page.file.inclusion.is_excluded():
return

latitude, longitude = config.extra["auto_populate"]["center"]
radius = config.extra["auto_populate"]["population_radius"]

geolinks, markdown = auto_populate_map.find_auto_populate_geotags(markdown, latitude, longitude, radius)

return markdown

Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
"""Automatically populate the map with supermarkets and other landmarks using the Overpass (Open Street Map) API."""
from __future__ import annotations

import bs4
import overpy
import pydantic

from render_map import mapping

RADIUS=35000
Expand All @@ -21,6 +24,65 @@

API = overpy.Overpass()

class AutoPopulateConfig(pydantic.BaseModel):
"""The configuration for the auto-populate plugin."""

supermarket: bool = False

@staticmethod
def tag_name() -> str:
"""The name of the tag to search for."""
return "populate_geotag"

@classmethod
def from_dict(cls, config_dict: dict[str, str|bool]) -> AutoPopulateConfig:
"""Create a config object from a dictionary.
Args:
config_dict: The dictionary to create the config object from.
Returns:
A config object.
"""
for key in cls.model_fields:
if key in config_dict:
config_dict[key] = True
return cls(**config_dict)

def find_auto_populate_geotags(markdown: str, latitude:float, longitude:float, radius:float) -> tuple[list[AutoPopulateConfig], str]:
"""Find all geotags in the markdown and process them into a list of GeoLink objects.
Args:
markdown: The markdown to search for geotags.
latitude: The latitude to search for supermarkets.
longitude: The longitude to search for supermarkets.
radius: The radius in meters to search for supermarkets.
Returns:
A list of GeoLink objects and the markdown with the geotags replaced.
"""
soup = bs4.BeautifulSoup(markdown, "html.parser")
geo_tags = soup.find_all(AutoPopulateConfig.tag_name())

populate_geotags_configs = []
for geo_tag in geo_tags:
result = geo_tag.attrs

# Generate a GeoLink object from the result
geotag_config = AutoPopulateConfig.from_dict(result)
populate_geotags_configs.append(geotag_config)

# Replace the geotag with a span tag with the uuid as the id and the name as the text
new_tag = soup.new_tag("div")
if geotag_config.supermarket:
supermarkets_tags = populate_supermarkets(radius, latitude, longitude)
for tag in supermarkets_tags:
new_tag.append(tag)
new_tag.append(", ")
geo_tag.replace_with(new_tag)
return populate_geotags_configs, str(soup)


def choose_supermarket_name_zoom(node:overpy.Node) -> tuple[str|None, mapping.ZoomLevel]:
"""Choose the game name and map zoom level for a supermarket, based on the properties of the supermarket in the
real world.
Expand All @@ -43,7 +105,7 @@ def choose_supermarket_name_zoom(node:overpy.Node) -> tuple[str|None, mapping.Zo
return "Supermarket", mapping.ZoomLevel.TOWN


def populate_supermarkets(radius:float, latitude:float, longitude:float) -> str:
def populate_supermarkets(radius:float, latitude:float, longitude:float) -> list[bs4.Tag]:
"""Generate geotags for supermarkets in the game world, using the locations of supermarkets in the real world (using
the Overpass API).
Expand All @@ -67,4 +129,4 @@ def populate_supermarkets(radius:float, latitude:float, longitude:float) -> str:
)

)
return "\n".join(geotag.get_tag() for geotag in geotags)
return [geotag.get_tag() for geotag in geotags]
8 changes: 4 additions & 4 deletions render_map/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class GeoLink(pydantic.BaseModel):
zoom: ZoomLevel = pydantic.Field(default=ZoomLevel.WASTELAND, validate_default=True)
uuid: str = pydantic.Field(default_factory=lambda: str(uuid.uuid4()))

def get_tag(self, include_uuid:bool=False) -> str:
def get_tag(self, include_uuid:bool=False) -> bs4.Tag:
"""Get the geotag as a string.
Args:
Expand All @@ -68,12 +68,12 @@ def get_tag(self, include_uuid:bool=False) -> str:
tag["name"] = self.name
tag["latitude"] = str(self.latitude)
tag["longitude"] = str(self.longitude)
tag["icon"] = self.icon.name
tag["zoom"] = str(self.zoom.name)
tag["icon"] = str(map_icons.MapIcon(self.icon).name)
tag["zoom"] = str(ZoomLevel(self.zoom).name)
if include_uuid:
tag["uuid"] = self.uuid

return str(tag)
return tag

GEO_LINKS: list[GeoLink] = []

Expand Down

0 comments on commit 303485e

Please sign in to comment.