maps4fs 0.7.8__py3-none-any.whl → 0.9.93__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- maps4fs/generator/background.py +362 -0
- maps4fs/generator/component.py +176 -4
- maps4fs/generator/config.py +52 -2
- maps4fs/generator/dem.py +181 -35
- maps4fs/generator/game.py +27 -4
- maps4fs/generator/i3d.py +89 -0
- maps4fs/generator/map.py +51 -32
- maps4fs/generator/path_steps.py +83 -0
- maps4fs/generator/qgis.py +196 -0
- maps4fs/generator/texture.py +88 -45
- maps4fs/generator/tile.py +55 -0
- maps4fs/logger.py +27 -3
- maps4fs/toolbox/__init__.py +1 -0
- maps4fs/toolbox/dem.py +112 -0
- maps4fs-0.9.93.dist-info/METADATA +451 -0
- maps4fs-0.9.93.dist-info/RECORD +21 -0
- maps4fs-0.7.8.dist-info/METADATA +0 -218
- maps4fs-0.7.8.dist-info/RECORD +0 -14
- {maps4fs-0.7.8.dist-info → maps4fs-0.9.93.dist-info}/LICENSE.md +0 -0
- {maps4fs-0.7.8.dist-info → maps4fs-0.9.93.dist-info}/WHEEL +0 -0
- {maps4fs-0.7.8.dist-info → maps4fs-0.9.93.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,362 @@
|
|
1
|
+
"""This module contains the Background component, which generates 3D obj files based on DEM data
|
2
|
+
around the map."""
|
3
|
+
|
4
|
+
from __future__ import annotations
|
5
|
+
|
6
|
+
import os
|
7
|
+
|
8
|
+
import cv2
|
9
|
+
import numpy as np
|
10
|
+
import trimesh # type: ignore
|
11
|
+
|
12
|
+
from maps4fs.generator.component import Component
|
13
|
+
from maps4fs.generator.dem import (
|
14
|
+
DEFAULT_BLUR_RADIUS,
|
15
|
+
DEFAULT_MULTIPLIER,
|
16
|
+
DEFAULT_PLATEAU,
|
17
|
+
)
|
18
|
+
from maps4fs.generator.path_steps import DEFAULT_DISTANCE, PATH_FULL_NAME, get_steps
|
19
|
+
from maps4fs.generator.tile import Tile
|
20
|
+
from maps4fs.logger import timeit
|
21
|
+
|
22
|
+
RESIZE_FACTOR = 1 / 4
|
23
|
+
SIMPLIFY_FACTOR = 10
|
24
|
+
FULL_RESIZE_FACTOR = 1 / 8
|
25
|
+
FULL_SIMPLIFY_FACTOR = 20
|
26
|
+
|
27
|
+
|
28
|
+
class Background(Component):
|
29
|
+
"""Component for creating 3D obj files based on DEM data around the map.
|
30
|
+
|
31
|
+
Args:
|
32
|
+
coordinates (tuple[float, float]): The latitude and longitude of the center of the map.
|
33
|
+
map_height (int): The height of the map in pixels.
|
34
|
+
map_width (int): The width of the map in pixels.
|
35
|
+
map_directory (str): The directory where the map files are stored.
|
36
|
+
logger (Any, optional): The logger to use. Must have at least three basic methods: debug,
|
37
|
+
info, warning. If not provided, default logging will be used.
|
38
|
+
"""
|
39
|
+
|
40
|
+
def preprocess(self) -> None:
|
41
|
+
"""Prepares the component for processing. Registers the tiles around the map by moving
|
42
|
+
clockwise from North, then clockwise."""
|
43
|
+
self.tiles: list[Tile] = []
|
44
|
+
origin = self.coordinates
|
45
|
+
|
46
|
+
# Getting a list of 8 tiles around the map starting from the N(North) tile.
|
47
|
+
for path_step in get_steps(self.map_height, self.map_width):
|
48
|
+
# Getting the destination coordinates for the current tile.
|
49
|
+
if path_step.angle is None:
|
50
|
+
# For the case when generating the overview map, which has the same
|
51
|
+
# center as the main map.
|
52
|
+
tile_coordinates = self.coordinates
|
53
|
+
else:
|
54
|
+
tile_coordinates = path_step.get_destination(origin)
|
55
|
+
|
56
|
+
# Create a Tile component, which is needed to save the DEM image.
|
57
|
+
tile = Tile(
|
58
|
+
game=self.game,
|
59
|
+
coordinates=tile_coordinates,
|
60
|
+
map_height=path_step.size[1],
|
61
|
+
map_width=path_step.size[0],
|
62
|
+
map_directory=self.map_directory,
|
63
|
+
logger=self.logger,
|
64
|
+
tile_code=path_step.code,
|
65
|
+
auto_process=False,
|
66
|
+
blur_radius=self.kwargs.get("blur_radius", DEFAULT_BLUR_RADIUS),
|
67
|
+
multiplier=self.kwargs.get("multiplier", DEFAULT_MULTIPLIER),
|
68
|
+
plateau=self.kwargs.get("plateau", DEFAULT_PLATEAU),
|
69
|
+
)
|
70
|
+
|
71
|
+
# Update the origin for the next tile.
|
72
|
+
origin = tile_coordinates
|
73
|
+
self.tiles.append(tile)
|
74
|
+
self.logger.debug(
|
75
|
+
"Registered tile: %s, coordinates: %s, size: %s",
|
76
|
+
tile.code,
|
77
|
+
tile_coordinates,
|
78
|
+
path_step.size,
|
79
|
+
)
|
80
|
+
|
81
|
+
def process(self) -> None:
|
82
|
+
"""Launches the component processing. Iterates over all tiles and processes them
|
83
|
+
as a result the DEM files will be saved, then based on them the obj files will be
|
84
|
+
generated."""
|
85
|
+
for tile in self.tiles:
|
86
|
+
tile.process()
|
87
|
+
|
88
|
+
self.generate_obj_files()
|
89
|
+
|
90
|
+
def info_sequence(self) -> dict[str, dict[str, str | float | int]]:
|
91
|
+
"""Returns a dictionary with information about the tiles around the map.
|
92
|
+
Adds the EPSG:3857 string to the data for convenient usage in QGIS.
|
93
|
+
|
94
|
+
Returns:
|
95
|
+
dict[str, dict[str, float | int]] -- A dictionary with information about the tiles.
|
96
|
+
"""
|
97
|
+
data = {}
|
98
|
+
self.qgis_sequence()
|
99
|
+
|
100
|
+
for tile in self.tiles:
|
101
|
+
north, south, east, west = tile.bbox
|
102
|
+
epsg3857_string = tile.get_epsg3857_string()
|
103
|
+
epsg3857_string_with_margin = tile.get_epsg3857_string(add_margin=True)
|
104
|
+
|
105
|
+
tile_entry = {
|
106
|
+
"center_latitude": tile.coordinates[0],
|
107
|
+
"center_longitude": tile.coordinates[1],
|
108
|
+
"epsg3857_string": epsg3857_string,
|
109
|
+
"epsg3857_string_with_margin": epsg3857_string_with_margin,
|
110
|
+
"height": tile.map_height,
|
111
|
+
"width": tile.map_width,
|
112
|
+
"north": north,
|
113
|
+
"south": south,
|
114
|
+
"east": east,
|
115
|
+
"west": west,
|
116
|
+
}
|
117
|
+
if tile.code is not None:
|
118
|
+
data[tile.code] = tile_entry
|
119
|
+
|
120
|
+
return data # type: ignore
|
121
|
+
|
122
|
+
def qgis_sequence(self) -> None:
|
123
|
+
"""Generates QGIS scripts for creating bounding box layers and rasterizing them."""
|
124
|
+
qgis_layers = [
|
125
|
+
(f"Background_{tile.code}", *tile.get_espg3857_bbox()) for tile in self.tiles
|
126
|
+
]
|
127
|
+
qgis_layers_with_margin = [
|
128
|
+
(f"Background_{tile.code}_margin", *tile.get_espg3857_bbox(add_margin=True))
|
129
|
+
for tile in self.tiles
|
130
|
+
]
|
131
|
+
|
132
|
+
layers = qgis_layers + qgis_layers_with_margin
|
133
|
+
|
134
|
+
self.create_qgis_scripts(layers)
|
135
|
+
|
136
|
+
def generate_obj_files(self) -> None:
|
137
|
+
"""Iterates over all tiles and generates 3D obj files based on DEM data.
|
138
|
+
If at least one DEM file is missing, the generation will be stopped at all.
|
139
|
+
"""
|
140
|
+
for tile in self.tiles:
|
141
|
+
# Read DEM data from the tile.
|
142
|
+
dem_path = tile.dem_path
|
143
|
+
if not os.path.isfile(dem_path):
|
144
|
+
self.logger.warning("DEM file not found, generation will be stopped: %s", dem_path)
|
145
|
+
return
|
146
|
+
|
147
|
+
self.logger.info("DEM file for tile %s found: %s", tile.code, dem_path)
|
148
|
+
|
149
|
+
base_directory = os.path.dirname(dem_path)
|
150
|
+
save_path = os.path.join(base_directory, f"{tile.code}.obj")
|
151
|
+
self.logger.debug("Generating obj file for tile %s in path: %s", tile.code, save_path)
|
152
|
+
|
153
|
+
dem_data = cv2.imread(tile.dem_path, cv2.IMREAD_UNCHANGED) # pylint: disable=no-member
|
154
|
+
self.plane_from_np(tile.code, dem_data, save_path)
|
155
|
+
|
156
|
+
# pylint: disable=too-many-locals
|
157
|
+
@timeit
|
158
|
+
def plane_from_np(self, tile_code: str, dem_data: np.ndarray, save_path: str) -> None:
|
159
|
+
"""Generates a 3D obj file based on DEM data.
|
160
|
+
|
161
|
+
Arguments:
|
162
|
+
tile_code (str) -- The code of the tile.
|
163
|
+
dem_data (np.ndarray) -- The DEM data as a numpy array.
|
164
|
+
save_path (str) -- The path where the obj file will be saved.
|
165
|
+
"""
|
166
|
+
if tile_code == PATH_FULL_NAME:
|
167
|
+
resize_factor = FULL_RESIZE_FACTOR
|
168
|
+
simplify_factor = FULL_SIMPLIFY_FACTOR
|
169
|
+
self.logger.info("Generating a full map obj file")
|
170
|
+
else:
|
171
|
+
resize_factor = RESIZE_FACTOR
|
172
|
+
simplify_factor = SIMPLIFY_FACTOR
|
173
|
+
dem_data = cv2.resize( # pylint: disable=no-member
|
174
|
+
dem_data, (0, 0), fx=resize_factor, fy=resize_factor
|
175
|
+
)
|
176
|
+
self.logger.debug(
|
177
|
+
"DEM data resized to shape: %s with factor: %s", dem_data.shape, resize_factor
|
178
|
+
)
|
179
|
+
|
180
|
+
# Invert the height values.
|
181
|
+
dem_data = dem_data.max() - dem_data
|
182
|
+
|
183
|
+
rows, cols = dem_data.shape
|
184
|
+
x = np.linspace(0, cols - 1, cols)
|
185
|
+
y = np.linspace(0, rows - 1, rows)
|
186
|
+
x, y = np.meshgrid(x, y)
|
187
|
+
z = dem_data
|
188
|
+
|
189
|
+
self.logger.info(
|
190
|
+
"Starting to generate a mesh for tile %s with shape: %s x %s. "
|
191
|
+
"This may take a while...",
|
192
|
+
tile_code,
|
193
|
+
cols,
|
194
|
+
rows,
|
195
|
+
)
|
196
|
+
|
197
|
+
vertices = np.column_stack([x.ravel(), y.ravel(), z.ravel()])
|
198
|
+
faces = []
|
199
|
+
|
200
|
+
for i in range(rows - 1):
|
201
|
+
for j in range(cols - 1):
|
202
|
+
top_left = i * cols + j
|
203
|
+
top_right = top_left + 1
|
204
|
+
bottom_left = top_left + cols
|
205
|
+
bottom_right = bottom_left + 1
|
206
|
+
|
207
|
+
faces.append([top_left, bottom_left, bottom_right])
|
208
|
+
faces.append([top_left, bottom_right, top_right])
|
209
|
+
|
210
|
+
faces = np.array(faces) # type: ignore
|
211
|
+
mesh = trimesh.Trimesh(vertices=vertices, faces=faces)
|
212
|
+
|
213
|
+
# Apply rotation: 180 degrees around Y-axis and Z-axis
|
214
|
+
rotation_matrix_y = trimesh.transformations.rotation_matrix(np.pi, [0, 1, 0])
|
215
|
+
rotation_matrix_z = trimesh.transformations.rotation_matrix(np.pi, [0, 0, 1])
|
216
|
+
mesh.apply_transform(rotation_matrix_y)
|
217
|
+
mesh.apply_transform(rotation_matrix_z)
|
218
|
+
|
219
|
+
self.logger.info("Mesh generated with %s faces, will be simplified", len(mesh.faces))
|
220
|
+
|
221
|
+
# Simplify the mesh to reduce the number of faces.
|
222
|
+
mesh = mesh.simplify_quadric_decimation(face_count=len(faces) // simplify_factor)
|
223
|
+
self.logger.debug("Mesh simplified to %s faces", len(mesh.faces))
|
224
|
+
|
225
|
+
if tile_code == PATH_FULL_NAME:
|
226
|
+
self.mesh_to_stl(mesh)
|
227
|
+
|
228
|
+
mesh.export(save_path)
|
229
|
+
self.logger.info("Obj file saved: %s", save_path)
|
230
|
+
|
231
|
+
def mesh_to_stl(self, mesh: trimesh.Trimesh) -> None:
|
232
|
+
"""Converts the mesh to an STL file and saves it in the previews directory.
|
233
|
+
Uses powerful simplification to reduce the size of the file since it will be used
|
234
|
+
only for the preview.
|
235
|
+
|
236
|
+
Arguments:
|
237
|
+
mesh (trimesh.Trimesh) -- The mesh to convert to an STL file.
|
238
|
+
"""
|
239
|
+
preview_path = os.path.join(self.previews_directory, "background_dem.stl")
|
240
|
+
mesh = mesh.simplify_quadric_decimation(percent=0.05)
|
241
|
+
mesh.export(preview_path)
|
242
|
+
|
243
|
+
self.logger.info("STL file saved: %s", preview_path)
|
244
|
+
|
245
|
+
self.stl_preview_path = preview_path # pylint: disable=attribute-defined-outside-init
|
246
|
+
|
247
|
+
def previews(self) -> list[str]:
|
248
|
+
"""Generates a preview by combining all tiles into one image.
|
249
|
+
NOTE: The map itself is not included in the preview, so it will be empty.
|
250
|
+
|
251
|
+
Returns:
|
252
|
+
list[str] -- A list of paths to the preview images."""
|
253
|
+
|
254
|
+
self.logger.info("Generating a preview image for the background DEM")
|
255
|
+
|
256
|
+
image_height = self.map_height + DEFAULT_DISTANCE * 2
|
257
|
+
image_width = self.map_width + DEFAULT_DISTANCE * 2
|
258
|
+
self.logger.debug("Full size of the preview image: %s x %s", image_width, image_height)
|
259
|
+
|
260
|
+
image = np.zeros((image_height, image_width), np.uint16) # pylint: disable=no-member
|
261
|
+
self.logger.debug("Empty image created: %s", image.shape)
|
262
|
+
|
263
|
+
for tile in self.tiles:
|
264
|
+
# pylint: disable=no-member
|
265
|
+
if tile.code == PATH_FULL_NAME:
|
266
|
+
continue
|
267
|
+
tile_image = cv2.imread(tile.dem_path, cv2.IMREAD_UNCHANGED)
|
268
|
+
|
269
|
+
self.logger.debug(
|
270
|
+
"Tile %s image shape: %s, dtype: %s, max: %s, min: %s",
|
271
|
+
tile.code,
|
272
|
+
tile_image.shape,
|
273
|
+
tile_image.dtype,
|
274
|
+
tile_image.max(),
|
275
|
+
tile_image.min(),
|
276
|
+
)
|
277
|
+
|
278
|
+
tile_height, tile_width = tile_image.shape
|
279
|
+
self.logger.debug("Tile %s size: %s x %s", tile.code, tile_width, tile_height)
|
280
|
+
|
281
|
+
# Calculate the position based on the tile code
|
282
|
+
if tile.code == "N":
|
283
|
+
x = DEFAULT_DISTANCE
|
284
|
+
y = 0
|
285
|
+
elif tile.code == "NE":
|
286
|
+
x = self.map_width + DEFAULT_DISTANCE
|
287
|
+
y = 0
|
288
|
+
elif tile.code == "E":
|
289
|
+
x = self.map_width + DEFAULT_DISTANCE
|
290
|
+
y = DEFAULT_DISTANCE
|
291
|
+
elif tile.code == "SE":
|
292
|
+
x = self.map_width + DEFAULT_DISTANCE
|
293
|
+
y = self.map_height + DEFAULT_DISTANCE
|
294
|
+
elif tile.code == "S":
|
295
|
+
x = DEFAULT_DISTANCE
|
296
|
+
y = self.map_height + DEFAULT_DISTANCE
|
297
|
+
elif tile.code == "SW":
|
298
|
+
x = 0
|
299
|
+
y = self.map_height + DEFAULT_DISTANCE
|
300
|
+
elif tile.code == "W":
|
301
|
+
x = 0
|
302
|
+
y = DEFAULT_DISTANCE
|
303
|
+
elif tile.code == "NW":
|
304
|
+
x = 0
|
305
|
+
y = 0
|
306
|
+
|
307
|
+
# pylint: disable=possibly-used-before-assignment
|
308
|
+
x2 = x + tile_width
|
309
|
+
y2 = y + tile_height
|
310
|
+
|
311
|
+
self.logger.debug(
|
312
|
+
"Tile %s position. X from %s to %s, Y from %s to %s", tile.code, x, x2, y, y2
|
313
|
+
)
|
314
|
+
|
315
|
+
# pylint: disable=possibly-used-before-assignment
|
316
|
+
image[y:y2, x:x2] = tile_image
|
317
|
+
|
318
|
+
# Save image to the map directory.
|
319
|
+
preview_path = os.path.join(self.previews_directory, "background_dem.png")
|
320
|
+
|
321
|
+
# pylint: disable=no-member
|
322
|
+
image = cv2.normalize(image, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U) # type: ignore
|
323
|
+
image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR) # type: ignore
|
324
|
+
cv2.imwrite(preview_path, image)
|
325
|
+
|
326
|
+
return [preview_path, self.stl_preview_path]
|
327
|
+
|
328
|
+
|
329
|
+
# Creates tiles around the map.
|
330
|
+
# The one on corners 2048x2048, on sides and in the middle map_size x 2048.
|
331
|
+
# So 2048 is a distance FROM the edge of the map, but the other size depends on the map size.
|
332
|
+
# But for corner tiles it's always 2048.
|
333
|
+
|
334
|
+
# In the beginning we have coordinates of the central point of the map and it's size.
|
335
|
+
# We need to calculate the coordinates of central points all 8 tiles around the map.
|
336
|
+
|
337
|
+
# Latitude is a vertical line, Longitude is a horizontal line.
|
338
|
+
|
339
|
+
# 2048
|
340
|
+
# | |
|
341
|
+
# ____________________|_________|___
|
342
|
+
# | | | |
|
343
|
+
# | NW | N | NE | 2048
|
344
|
+
# |_________|_________|_________|___
|
345
|
+
# | | | |
|
346
|
+
# | W | C | E |
|
347
|
+
# |_________|_________|_________|
|
348
|
+
# | | | |
|
349
|
+
# | SW | S | SE |
|
350
|
+
# |_________|_________|_________|
|
351
|
+
#
|
352
|
+
# N = C map_height / 2 + 1024; N_width = map_width; N_height = 2048
|
353
|
+
# NW = N - map_width / 2 - 1024; NW_width = 2048; NW_height = 2048
|
354
|
+
# and so on...
|
355
|
+
|
356
|
+
# lat, lon = 45.28565000315636, 20.237121355049904
|
357
|
+
# dst = 1024
|
358
|
+
|
359
|
+
# # N
|
360
|
+
# destination = distance(meters=dst).destination((lat, lon), 0)
|
361
|
+
# lat, lon = destination.latitude, destination.longitude
|
362
|
+
# print(lat, lon)
|
maps4fs/generator/component.py
CHANGED
@@ -2,9 +2,15 @@
|
|
2
2
|
|
3
3
|
from __future__ import annotations
|
4
4
|
|
5
|
+
import json
|
6
|
+
import os
|
7
|
+
from copy import deepcopy
|
5
8
|
from typing import TYPE_CHECKING, Any
|
6
9
|
|
7
10
|
import osmnx as ox # type: ignore
|
11
|
+
from pyproj import Transformer
|
12
|
+
|
13
|
+
from maps4fs.generator.qgis import save_scripts
|
8
14
|
|
9
15
|
if TYPE_CHECKING:
|
10
16
|
from maps4fs.generator.game import Game
|
@@ -15,6 +21,7 @@ class Component:
|
|
15
21
|
"""Base class for all map generation components.
|
16
22
|
|
17
23
|
Args:
|
24
|
+
game (Game): The game instance for which the map is generated.
|
18
25
|
coordinates (tuple[float, float]): The latitude and longitude of the center of the map.
|
19
26
|
map_height (int): The height of the map in pixels.
|
20
27
|
map_width (int): The width of the map in pixels.
|
@@ -41,6 +48,9 @@ class Component:
|
|
41
48
|
self.logger = logger
|
42
49
|
self.kwargs = kwargs
|
43
50
|
|
51
|
+
os.makedirs(self.previews_directory, exist_ok=True)
|
52
|
+
os.makedirs(self.scripts_directory, exist_ok=True)
|
53
|
+
|
44
54
|
self.save_bbox()
|
45
55
|
self.preprocess()
|
46
56
|
|
@@ -68,28 +78,117 @@ class Component:
|
|
68
78
|
"""
|
69
79
|
raise NotImplementedError
|
70
80
|
|
71
|
-
|
81
|
+
@property
|
82
|
+
def previews_directory(self) -> str:
|
83
|
+
"""The directory where the preview images are stored.
|
84
|
+
|
85
|
+
Returns:
|
86
|
+
str: The directory where the preview images are stored.
|
87
|
+
"""
|
88
|
+
return os.path.join(self.map_directory, "previews")
|
89
|
+
|
90
|
+
@property
|
91
|
+
def scripts_directory(self) -> str:
|
92
|
+
"""The directory where the scripts are stored.
|
93
|
+
|
94
|
+
Returns:
|
95
|
+
str: The directory where the scripts are stored.
|
96
|
+
"""
|
97
|
+
return os.path.join(self.map_directory, "scripts")
|
98
|
+
|
99
|
+
@property
|
100
|
+
def generation_info_path(self) -> str:
|
101
|
+
"""The path to the generation info JSON file.
|
102
|
+
|
103
|
+
Returns:
|
104
|
+
str: The path to the generation info JSON file.
|
105
|
+
"""
|
106
|
+
return os.path.join(self.map_directory, "generation_info.json")
|
107
|
+
|
108
|
+
def info_sequence(self) -> dict[Any, Any]:
|
109
|
+
"""Returns the information sequence for the component. Must be implemented in the child
|
110
|
+
class. If the component does not have an information sequence, an empty dictionary must be
|
111
|
+
returned.
|
112
|
+
|
113
|
+
Returns:
|
114
|
+
dict[Any, Any]: The information sequence for the component.
|
115
|
+
"""
|
116
|
+
return {}
|
117
|
+
|
118
|
+
def commit_generation_info(self) -> None:
|
119
|
+
"""Commits the generation info to the generation info JSON file."""
|
120
|
+
self.update_generation_info(self.info_sequence())
|
121
|
+
|
122
|
+
def update_generation_info(self, data: dict[Any, Any]) -> None:
|
123
|
+
"""Updates the generation info with the provided data.
|
124
|
+
If the generation info file does not exist, it will be created.
|
125
|
+
|
126
|
+
Args:
|
127
|
+
data (dict[Any, Any]): The data to update the generation info with.
|
128
|
+
"""
|
129
|
+
if os.path.isfile(self.generation_info_path):
|
130
|
+
with open(self.generation_info_path, "r", encoding="utf-8") as file:
|
131
|
+
generation_info = json.load(file)
|
132
|
+
self.logger.debug("Loaded generation info from %s", self.generation_info_path)
|
133
|
+
else:
|
134
|
+
self.logger.debug(
|
135
|
+
"Generation info file does not exist, creating a new one in %s",
|
136
|
+
self.generation_info_path,
|
137
|
+
)
|
138
|
+
generation_info = {}
|
139
|
+
|
140
|
+
updated_generation_info = deepcopy(generation_info)
|
141
|
+
updated_generation_info[self.__class__.__name__] = data
|
142
|
+
|
143
|
+
self.logger.debug("Updated generation info, now contains %s fields", len(data))
|
144
|
+
|
145
|
+
with open(self.generation_info_path, "w", encoding="utf-8") as file:
|
146
|
+
json.dump(updated_generation_info, file, indent=4)
|
147
|
+
|
148
|
+
self.logger.debug("Saved updated generation info to %s", self.generation_info_path)
|
149
|
+
|
150
|
+
def get_bbox(
|
151
|
+
self,
|
152
|
+
coordinates: tuple[float, float] | None = None,
|
153
|
+
height_distance: int | None = None,
|
154
|
+
width_distance: int | None = None,
|
155
|
+
project_utm: bool = False,
|
156
|
+
) -> tuple[int, int, int, int]:
|
72
157
|
"""Calculates the bounding box of the map from the coordinates and the height and
|
73
158
|
width of the map.
|
159
|
+
If coordinates and distance are not provided, the instance variables are used.
|
74
160
|
|
75
161
|
Args:
|
162
|
+
coordinates (tuple[float, float], optional): The latitude and longitude of the center of
|
163
|
+
the map. Defaults to None.
|
164
|
+
height_distance (int, optional): The distance from the center of the map to the edge of
|
165
|
+
the map in the north-south direction. Defaults to None.
|
166
|
+
width_distance (int, optional): The distance from the center of the map to the edge of
|
167
|
+
the map in the east-west direction. Defaults to None.
|
76
168
|
project_utm (bool, optional): Whether to project the bounding box to UTM.
|
77
169
|
|
78
170
|
Returns:
|
79
171
|
tuple[int, int, int, int]: The bounding box of the map.
|
80
172
|
"""
|
173
|
+
coordinates = coordinates or self.coordinates
|
174
|
+
height_distance = height_distance or int(self.map_height / 2)
|
175
|
+
width_distance = width_distance or int(self.map_width / 2)
|
176
|
+
|
81
177
|
north, south, _, _ = ox.utils_geo.bbox_from_point(
|
82
|
-
|
178
|
+
coordinates, dist=height_distance, project_utm=project_utm
|
83
179
|
)
|
84
180
|
_, _, east, west = ox.utils_geo.bbox_from_point(
|
85
|
-
|
181
|
+
coordinates, dist=width_distance, project_utm=project_utm
|
86
182
|
)
|
87
183
|
bbox = north, south, east, west
|
88
184
|
self.logger.debug(
|
89
|
-
"Calculated bounding box for component: %s: %s, project_utm: %s"
|
185
|
+
"Calculated bounding box for component: %s: %s, project_utm: %s, "
|
186
|
+
"height_distance: %s, width_distance: %s",
|
90
187
|
self.__class__.__name__,
|
91
188
|
bbox,
|
92
189
|
project_utm,
|
190
|
+
height_distance,
|
191
|
+
width_distance,
|
93
192
|
)
|
94
193
|
return bbox
|
95
194
|
|
@@ -99,3 +198,76 @@ class Component:
|
|
99
198
|
"""
|
100
199
|
self.bbox = self.get_bbox(project_utm=False)
|
101
200
|
self.logger.debug("Saved bounding box: %s", self.bbox)
|
201
|
+
|
202
|
+
@property
|
203
|
+
def new_bbox(self) -> tuple[float, float, float, float]:
|
204
|
+
"""This property is used for a new version of osmnx library, where the order of coordinates
|
205
|
+
has been changed to (left, bottom, right, top).
|
206
|
+
|
207
|
+
Returns:
|
208
|
+
tuple[float, float, float, float]: The bounding box of the map in the new order:
|
209
|
+
(left, bottom, right, top).
|
210
|
+
"""
|
211
|
+
# FutureWarning: The expected order of coordinates in `bbox`
|
212
|
+
# will change in the v2.0.0 release to `(left, bottom, right, top)`.
|
213
|
+
north, south, east, west = self.bbox
|
214
|
+
return west, south, east, north
|
215
|
+
|
216
|
+
def get_espg3857_bbox(
|
217
|
+
self, bbox: tuple[float, float, float, float] | None = None, add_margin: bool = False
|
218
|
+
) -> tuple[float, float, float, float]:
|
219
|
+
"""Converts the bounding box to EPSG:3857.
|
220
|
+
If the bounding box is not provided, the instance variable is used.
|
221
|
+
|
222
|
+
Args:
|
223
|
+
bbox (tuple[float, float, float, float], optional): The bounding box to convert.
|
224
|
+
add_margin (bool, optional): Whether to add a margin to the bounding box.
|
225
|
+
|
226
|
+
Returns:
|
227
|
+
tuple[float, float, float, float]: The bounding box in EPSG:3857.
|
228
|
+
"""
|
229
|
+
bbox = bbox or self.bbox
|
230
|
+
north, south, east, west = bbox
|
231
|
+
transformer = Transformer.from_crs("epsg:4326", "epsg:3857")
|
232
|
+
epsg3857_north, epsg3857_west = transformer.transform(north, west)
|
233
|
+
epsg3857_south, epsg3857_east = transformer.transform(south, east)
|
234
|
+
|
235
|
+
if add_margin:
|
236
|
+
MARGIN = 500 # pylint: disable=C0103
|
237
|
+
epsg3857_north = int(epsg3857_north - MARGIN)
|
238
|
+
epsg3857_south = int(epsg3857_south + MARGIN)
|
239
|
+
epsg3857_east = int(epsg3857_east - MARGIN)
|
240
|
+
epsg3857_west = int(epsg3857_west + MARGIN)
|
241
|
+
|
242
|
+
return epsg3857_north, epsg3857_south, epsg3857_east, epsg3857_west
|
243
|
+
|
244
|
+
def get_epsg3857_string(
|
245
|
+
self, bbox: tuple[float, float, float, float] | None = None, add_margin: bool = False
|
246
|
+
) -> str:
|
247
|
+
"""Converts the bounding box to EPSG:3857 string.
|
248
|
+
If the bounding box is not provided, the instance variable is used.
|
249
|
+
|
250
|
+
Args:
|
251
|
+
bbox (tuple[float, float, float, float], optional): The bounding box to convert.
|
252
|
+
add_margin (bool, optional): Whether to add a margin to the bounding box.
|
253
|
+
|
254
|
+
Returns:
|
255
|
+
str: The bounding box in EPSG:3857 string.
|
256
|
+
"""
|
257
|
+
north, south, east, west = self.get_espg3857_bbox(bbox, add_margin=add_margin)
|
258
|
+
return f"{north},{south},{east},{west} [EPSG:3857]"
|
259
|
+
|
260
|
+
def create_qgis_scripts(
|
261
|
+
self, qgis_layers: list[tuple[str, float, float, float, float]]
|
262
|
+
) -> None:
|
263
|
+
"""Creates QGIS scripts from the given layers.
|
264
|
+
Each layer is a tuple where the first element is a name of the layer and the rest are the
|
265
|
+
bounding box coordinates in EPSG:3857.
|
266
|
+
For filenames, the class name is used as a prefix.
|
267
|
+
|
268
|
+
Args:
|
269
|
+
qgis_layers (list[tuple[str, float, float, float, float]]): The list of layers to
|
270
|
+
create scripts for.
|
271
|
+
"""
|
272
|
+
class_name = self.__class__.__name__.lower()
|
273
|
+
save_scripts(qgis_layers, class_name, self.scripts_directory)
|
maps4fs/generator/config.py
CHANGED
@@ -22,13 +22,15 @@ class Config(Component):
|
|
22
22
|
"""
|
23
23
|
|
24
24
|
def preprocess(self) -> None:
|
25
|
+
"""Gets the path to the map XML file and saves it to the instance variable."""
|
25
26
|
self._map_xml_path = self.game.map_xml_path(self.map_directory)
|
26
27
|
self.logger.debug("Map XML path: %s.", self._map_xml_path)
|
27
28
|
|
28
|
-
def process(self):
|
29
|
+
def process(self) -> None:
|
30
|
+
"""Sets the map size in the map.xml file."""
|
29
31
|
self._set_map_size()
|
30
32
|
|
31
|
-
def _set_map_size(self):
|
33
|
+
def _set_map_size(self) -> None:
|
32
34
|
"""Edits map.xml file to set correct map size."""
|
33
35
|
if not os.path.isfile(self._map_xml_path):
|
34
36
|
self.logger.warning("Map XML file not found: %s.", self._map_xml_path)
|
@@ -53,3 +55,51 @@ class Config(Component):
|
|
53
55
|
list[str]: An empty list.
|
54
56
|
"""
|
55
57
|
return []
|
58
|
+
|
59
|
+
def info_sequence(self) -> dict[str, dict[str, str | float | int]]:
|
60
|
+
"""Returns information about the component.
|
61
|
+
Overview section is needed to create the overview file (in-game map).
|
62
|
+
|
63
|
+
Returns:
|
64
|
+
dict[str, dict[str, str | float | int]]: Information about the component.
|
65
|
+
"""
|
66
|
+
# The overview file is exactly 2X bigger than the map size, does not matter
|
67
|
+
# if the map is 2048x2048 or 4096x4096, the overview will be 4096x4096
|
68
|
+
# and the map will be in the center of the overview.
|
69
|
+
# That's why the distance is set to the map height not as a half of it.
|
70
|
+
bbox = self.get_bbox(height_distance=self.map_height, width_distance=self.map_width)
|
71
|
+
south, west, north, east = bbox
|
72
|
+
epsg3857_string = self.get_epsg3857_string(bbox=bbox)
|
73
|
+
epsg3857_string_with_margin = self.get_epsg3857_string(bbox=bbox, add_margin=True)
|
74
|
+
|
75
|
+
self.qgis_sequence()
|
76
|
+
|
77
|
+
overview_data = {
|
78
|
+
"epsg3857_string": epsg3857_string,
|
79
|
+
"epsg3857_string_with_margin": epsg3857_string_with_margin,
|
80
|
+
"south": south,
|
81
|
+
"west": west,
|
82
|
+
"north": north,
|
83
|
+
"east": east,
|
84
|
+
"height": self.map_height * 2,
|
85
|
+
"width": self.map_width * 2,
|
86
|
+
}
|
87
|
+
|
88
|
+
data = {
|
89
|
+
"Overview": overview_data,
|
90
|
+
}
|
91
|
+
|
92
|
+
return data # type: ignore
|
93
|
+
|
94
|
+
def qgis_sequence(self) -> None:
|
95
|
+
"""Generates QGIS scripts for creating bounding box layers and rasterizing them."""
|
96
|
+
bbox = self.get_bbox(height_distance=self.map_height, width_distance=self.map_width)
|
97
|
+
espg3857_bbox = self.get_espg3857_bbox(bbox=bbox)
|
98
|
+
espg3857_bbox_with_margin = self.get_espg3857_bbox(bbox=bbox, add_margin=True)
|
99
|
+
|
100
|
+
qgis_layers = [("Overview_bbox", *espg3857_bbox)]
|
101
|
+
qgis_layers_with_margin = [("Overview_bbox_with_margin", *espg3857_bbox_with_margin)]
|
102
|
+
|
103
|
+
layers = qgis_layers + qgis_layers_with_margin
|
104
|
+
|
105
|
+
self.create_qgis_scripts(layers)
|