Skip to content

Commit

Permalink
New WebUI
Browse files Browse the repository at this point in the history
* WebUI update.

* Docstrings and typing.

* Docstrings and typing.
  • Loading branch information
iwatkot authored Nov 23, 2024
1 parent 2d2c68a commit fc8e337
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 30 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ maps/
.mypy_cache/
.pytest_cache/
htmlcov/
tests/data/
tests/data/
osmps/
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ WORKDIR /usr/src/app
COPY data /usr/src/app/data
COPY webui /usr/src/app/webui

RUN pip install "opencv-python" "osmnx<2.0.0" "rasterio" "tqdm" "streamlit" "maps4fs"
RUN pip install "opencv-python" "folium" "osmnx<2.0.0" "rasterio" "tqdm" "streamlit" "maps4fs"

EXPOSE 8501

Expand Down
6 changes: 4 additions & 2 deletions maps4fs/generator/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ class Config(Component):
"""

def preprocess(self) -> None:
"""Gets the path to the map XML file and saves it to the instance variable."""
self._map_xml_path = self.game.map_xml_path(self.map_directory)
self.logger.debug("Map XML path: %s.", self._map_xml_path)

def process(self):
def process(self) -> None:
"""Sets the map size in the map.xml file."""
self._set_map_size()

def _set_map_size(self):
def _set_map_size(self) -> None:
"""Edits map.xml file to set correct map size."""
if not os.path.isfile(self._map_xml_path):
self.logger.warning("Map XML file not found: %s.", self._map_xml_path)
Expand Down
4 changes: 4 additions & 0 deletions maps4fs/generator/i3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class I3d(Component):
_map_i3d_path: str | None = None

def preprocess(self) -> None:
"""Gets the path to the map I3D file from the game instance and saves it to the instance
attribute. If the game does not support I3D files, the attribute is set to None."""
try:
self._map_i3d_path = self.game.i3d_file_path(self.map_directory)
self.logger.debug("Map I3D path: %s.", self._map_i3d_path)
Expand All @@ -35,9 +37,11 @@ def preprocess(self) -> None:
self._map_i3d_path = None

def process(self) -> None:
"""Updates the map I3D file with the default settings."""
self._update_i3d_file()

def _update_i3d_file(self) -> None:
"""Updates the map I3D file with the default settings."""
if not self._map_i3d_path:
self.logger.info("I3D is not obtained, skipping the update.")
return
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "maps4fs"
version = "0.8.0"
version = "0.8.1"
description = "Generate map templates for Farming Simulator from real places."
authors = [{name = "iwatkot", email = "iwatkot@gmail.com"}]
license = {text = "MIT License"}
Expand All @@ -21,6 +21,7 @@ dependencies = [
"osmnx<2.0.0",
"rasterio",
"tqdm",
"folium",
]

[project.urls]
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ osmnx<2.0.0
rasterio
tqdm
streamlit
folium
maps4fs
2 changes: 2 additions & 0 deletions webui/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
WORKING_DIRECTORY = os.getcwd()
ARCHIVES_DIRECTORY = os.path.join(WORKING_DIRECTORY, "archives")
MAPS_DIRECTORY = os.path.join(WORKING_DIRECTORY, "maps")
OSMPS_DIRECTORY = os.path.join(WORKING_DIRECTORY, "osmps")
os.makedirs(ARCHIVES_DIRECTORY, exist_ok=True)
os.makedirs(MAPS_DIRECTORY, exist_ok=True)
os.makedirs(OSMPS_DIRECTORY, exist_ok=True)
72 changes: 72 additions & 0 deletions webui/osmp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import os

import config
import folium
import osmnx as ox


def get_preview(center_lat: float, center_lon: float, size_meters: int) -> str:
"""Generate an HTML file with OpenStreetMap data centered at the given point and size in meters.
Arguments:
center_lat (float): Latitude of the central point.
center_lon (float): Longitude of the central point.
size_meters (int): Width of the bounding box in meters.
output_file (str): Path to the output HTML file.
Returns:
str: Path to the HTML file where the OpenStreetMap data is saved.
"""
save_path = get_save_path(center_lat, center_lon, size_meters)
if os.path.isfile(save_path):
return save_path
# Calculate the bounding box
center = (center_lat, center_lon)

north, south, east, west = ox.utils_geo.bbox_from_point(
center, size_meters / 2, project_utm=False
)

# Create a map centered at the given point
m = folium.Map(location=[center_lat, center_lon], max_bounds=True)

# Draw the bounding box
folium.Rectangle(
bounds=[[south, west], [north, east]], color="blue", fill=True, fill_opacity=0.2
).add_to(m)

m.fit_bounds([[south, west], [north, east]])

# Save the map as an HTML file
m.save(save_path)
return save_path


def get_save_path(lat: float, lon: float, size_meters: int) -> str:
"""Return the path to the HTML file where the OpenStreetMap data is saved.
Arguments:
lat (float): Latitude of the central point.
lon (float): Longitude of the central point.
size_meters (int): Width of the bounding box in meters.
Returns:
str: Path to the HTML file.
"""
return os.path.join(
config.OSMPS_DIRECTORY,
f"{format_coordinates(lat, lon)}_{size_meters}.html",
)


def format_coordinates(lat: float, lon: float) -> str:
"""Return a string representation of the coordinates.
Arguments:
lat (float): Latitude.
lon (float): Longitude.
Returns:
str: String representation of the coordinates.
"""
return f"{lat:.6f}_{lon:.6f}"
Loading

0 comments on commit fc8e337

Please sign in to comment.