maps4fs 0.8.2__py3-none-any.whl → 0.8.4__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.
@@ -0,0 +1,281 @@
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.path_steps import DEFAULT_DISTANCE, get_steps
14
+ from maps4fs.generator.tile import Tile
15
+
16
+ RESIZE_FACTOR = 1 / 4
17
+
18
+
19
+ class Background(Component):
20
+ """Component for creating 3D obj files based on DEM data around the map.
21
+
22
+ Args:
23
+ coordinates (tuple[float, float]): The latitude and longitude of the center of the map.
24
+ map_height (int): The height of the map in pixels.
25
+ map_width (int): The width of the map in pixels.
26
+ map_directory (str): The directory where the map files are stored.
27
+ logger (Any, optional): The logger to use. Must have at least three basic methods: debug,
28
+ info, warning. If not provided, default logging will be used.
29
+ """
30
+
31
+ def preprocess(self) -> None:
32
+ """Prepares the component for processing. Registers the tiles around the map by moving
33
+ clockwise from North, then clockwise."""
34
+ self.tiles: list[Tile] = []
35
+ origin = self.coordinates
36
+
37
+ # Getting a list of 8 tiles around the map starting from the N(North) tile.
38
+ for path_step in get_steps(self.map_height, self.map_width):
39
+ # Getting the destination coordinates for the current tile.
40
+ tile_coordinates = path_step.get_destination(origin)
41
+
42
+ # Create a Tile component, which is needed to save the DEM image.
43
+ tile = Tile(
44
+ game=self.game,
45
+ coordinates=tile_coordinates,
46
+ map_height=path_step.size[1],
47
+ map_width=path_step.size[0],
48
+ map_directory=self.map_directory,
49
+ logger=self.logger,
50
+ tile_code=path_step.code,
51
+ auto_process=False,
52
+ blur_radius=self.kwargs.get("blur_radius"),
53
+ multiplier=1,
54
+ )
55
+
56
+ # Update the origin for the next tile.
57
+ origin = tile_coordinates
58
+ self.tiles.append(tile)
59
+ self.logger.debug(
60
+ "Registered tile: %s, coordinates: %s, size: %s",
61
+ tile.code,
62
+ tile_coordinates,
63
+ path_step.size,
64
+ )
65
+
66
+ def process(self) -> None:
67
+ """Launches the component processing. Iterates over all tiles and processes them
68
+ as a result the DEM files will be saved, then based on them the obj files will be
69
+ generated."""
70
+ for tile in self.tiles:
71
+ tile.process()
72
+
73
+ self.generate_obj_files()
74
+
75
+ def info_sequence(self) -> dict[str, dict[str, str | float | int]]:
76
+ """Returns a dictionary with information about the tiles around the map.
77
+ Adds the EPSG:3857 string to the data for convenient usage in QGIS.
78
+
79
+ Returns:
80
+ dict[str, dict[str, float | int]] -- A dictionary with information about the tiles.
81
+ """
82
+ data = {}
83
+ for tile in self.tiles:
84
+ north, south, east, west = tile.bbox
85
+ epsg3857_string = tile.get_epsg3857_string()
86
+
87
+ tile_entry = {
88
+ "center_latitude": tile.coordinates[0],
89
+ "center_longitude": tile.coordinates[1],
90
+ "epsg3857_string": epsg3857_string,
91
+ "height": tile.map_height,
92
+ "width": tile.map_width,
93
+ "north": north,
94
+ "south": south,
95
+ "east": east,
96
+ "west": west,
97
+ }
98
+ if tile.code is not None:
99
+ data[tile.code] = tile_entry
100
+
101
+ return data # type: ignore
102
+
103
+ def generate_obj_files(self) -> None:
104
+ """Iterates over all tiles and generates 3D obj files based on DEM data.
105
+ If at least one DEM file is missing, the generation will be stopped at all.
106
+ """
107
+ for tile in self.tiles:
108
+ # Read DEM data from the tile.
109
+ dem_path = tile.dem_path
110
+ if not os.path.isfile(dem_path):
111
+ self.logger.warning("DEM file not found, generation will be stopped: %s", dem_path)
112
+ return
113
+
114
+ self.logger.info("DEM file for tile %s found: %s", tile.code, dem_path)
115
+
116
+ base_directory = os.path.dirname(dem_path)
117
+ save_path = os.path.join(base_directory, f"{tile.code}.obj")
118
+ self.logger.debug("Generating obj file for tile %s in path: %s", tile.code, save_path)
119
+
120
+ dem_data = cv2.imread(tile.dem_path, cv2.IMREAD_UNCHANGED) # pylint: disable=no-member
121
+ self.plane_from_np(dem_data, save_path)
122
+
123
+ # pylint: disable=too-many-locals
124
+ def plane_from_np(self, dem_data: np.ndarray, save_path: str) -> None:
125
+ """Generates a 3D obj file based on DEM data.
126
+
127
+ Arguments:
128
+ dem_data (np.ndarray) -- The DEM data as a numpy array.
129
+ save_path (str) -- The path where the obj file will be saved.
130
+ """
131
+ dem_data = cv2.resize( # pylint: disable=no-member
132
+ dem_data, (0, 0), fx=RESIZE_FACTOR, fy=RESIZE_FACTOR
133
+ )
134
+ self.logger.debug(
135
+ "DEM data resized to shape: %s with factor: %s", dem_data.shape, RESIZE_FACTOR
136
+ )
137
+
138
+ rows, cols = dem_data.shape
139
+ x = np.linspace(0, cols - 1, cols)
140
+ y = np.linspace(0, rows - 1, rows)
141
+ x, y = np.meshgrid(x, y)
142
+ z = dem_data
143
+
144
+ vertices = np.column_stack([x.ravel(), y.ravel(), z.ravel()])
145
+ faces = []
146
+
147
+ for i in range(rows - 1):
148
+ for j in range(cols - 1):
149
+ top_left = i * cols + j
150
+ top_right = top_left + 1
151
+ bottom_left = top_left + cols
152
+ bottom_right = bottom_left + 1
153
+
154
+ # Invert the order of vertices to flip the normals
155
+ faces.append([top_left, bottom_right, bottom_left])
156
+ faces.append([top_left, top_right, bottom_right])
157
+
158
+ faces = np.array(faces) # type: ignore
159
+ mesh = trimesh.Trimesh(vertices=vertices, faces=faces)
160
+
161
+ # Simplify the mesh to reduce the number of faces.
162
+ mesh = mesh.simplify_quadric_decimation(face_count=len(faces) // 10)
163
+ self.logger.debug("Mesh simplified to %s faces", len(mesh.faces))
164
+
165
+ mesh.export(save_path)
166
+ self.logger.info("Obj file saved: %s", save_path)
167
+
168
+ def previews(self) -> list[str]:
169
+ """Generates a preview by combining all tiles into one image.
170
+ NOTE: The map itself is not included in the preview, so it will be empty.
171
+
172
+ Returns:
173
+ list[str] -- A list of paths to the preview images."""
174
+
175
+ self.logger.info("Generating a preview image for the background DEM")
176
+
177
+ image_height = self.map_height + DEFAULT_DISTANCE * 2
178
+ image_width = self.map_width + DEFAULT_DISTANCE * 2
179
+ self.logger.debug("Full size of the preview image: %s x %s", image_width, image_height)
180
+
181
+ image = np.zeros((image_height, image_width), np.uint16) # pylint: disable=no-member
182
+ self.logger.debug("Empty image created: %s", image.shape)
183
+
184
+ for tile in self.tiles:
185
+ # pylint: disable=no-member
186
+ tile_image = cv2.imread(tile.dem_path, cv2.IMREAD_UNCHANGED)
187
+
188
+ self.logger.debug(
189
+ "Tile %s image shape: %s, dtype: %s, max: %s, min: %s",
190
+ tile.code,
191
+ tile_image.shape,
192
+ tile_image.dtype,
193
+ tile_image.max(),
194
+ tile_image.min(),
195
+ )
196
+
197
+ tile_height, tile_width = tile_image.shape
198
+ self.logger.debug("Tile %s size: %s x %s", tile.code, tile_width, tile_height)
199
+
200
+ # Calculate the position based on the tile code
201
+ if tile.code == "N":
202
+ x = DEFAULT_DISTANCE
203
+ y = 0
204
+ elif tile.code == "NE":
205
+ x = self.map_width + DEFAULT_DISTANCE
206
+ y = 0
207
+ elif tile.code == "E":
208
+ x = self.map_width + DEFAULT_DISTANCE
209
+ y = DEFAULT_DISTANCE
210
+ elif tile.code == "SE":
211
+ x = self.map_width + DEFAULT_DISTANCE
212
+ y = self.map_height + DEFAULT_DISTANCE
213
+ elif tile.code == "S":
214
+ x = DEFAULT_DISTANCE
215
+ y = self.map_height + DEFAULT_DISTANCE
216
+ elif tile.code == "SW":
217
+ x = 0
218
+ y = self.map_height + DEFAULT_DISTANCE
219
+ elif tile.code == "W":
220
+ x = 0
221
+ y = DEFAULT_DISTANCE
222
+ elif tile.code == "NW":
223
+ x = 0
224
+ y = 0
225
+
226
+ # pylint: disable=possibly-used-before-assignment
227
+ x2 = x + tile_width
228
+ y2 = y + tile_height
229
+
230
+ self.logger.debug(
231
+ "Tile %s position. X from %s to %s, Y from %s to %s", tile.code, x, x2, y, y2
232
+ )
233
+
234
+ # pylint: disable=possibly-used-before-assignment
235
+ image[y:y2, x:x2] = tile_image
236
+
237
+ # Save image to the map directory.
238
+ preview_path = os.path.join(self.previews_directory, "background_dem.png")
239
+
240
+ # pylint: disable=no-member
241
+ image = cv2.normalize(image, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U) # type: ignore
242
+ image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR) # type: ignore
243
+ cv2.imwrite(preview_path, image)
244
+
245
+ return [preview_path]
246
+
247
+
248
+ # Creates tiles around the map.
249
+ # The one on corners 2048x2048, on sides and in the middle map_size x 2048.
250
+ # So 2048 is a distance FROM the edge of the map, but the other size depends on the map size.
251
+ # But for corner tiles it's always 2048.
252
+
253
+ # In the beginning we have coordinates of the central point of the map and it's size.
254
+ # We need to calculate the coordinates of central points all 8 tiles around the map.
255
+
256
+ # Latitude is a vertical line, Longitude is a horizontal line.
257
+
258
+ # 2048
259
+ # | |
260
+ # ____________________|_________|___
261
+ # | | | |
262
+ # | NW | N | NE | 2048
263
+ # |_________|_________|_________|___
264
+ # | | | |
265
+ # | W | C | E |
266
+ # |_________|_________|_________|
267
+ # | | | |
268
+ # | SW | S | SE |
269
+ # |_________|_________|_________|
270
+ #
271
+ # N = C map_height / 2 + 1024; N_width = map_width; N_height = 2048
272
+ # NW = N - map_width / 2 - 1024; NW_width = 2048; NW_height = 2048
273
+ # and so on...
274
+
275
+ # lat, lon = 45.28565000315636, 20.237121355049904
276
+ # dst = 1024
277
+
278
+ # # N
279
+ # destination = distance(meters=dst).destination((lat, lon), 0)
280
+ # lat, lon = destination.latitude, destination.longitude
281
+ # print(lat, lon)
@@ -2,9 +2,13 @@
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
8
12
 
9
13
  if TYPE_CHECKING:
10
14
  from maps4fs.generator.game import Game
@@ -42,6 +46,8 @@ class Component:
42
46
  self.logger = logger
43
47
  self.kwargs = kwargs
44
48
 
49
+ os.makedirs(self.previews_directory, exist_ok=True)
50
+
45
51
  self.save_bbox()
46
52
  self.preprocess()
47
53
 
@@ -69,21 +75,94 @@ class Component:
69
75
  """
70
76
  raise NotImplementedError
71
77
 
72
- def get_bbox(self, project_utm: bool = False) -> tuple[int, int, int, int]:
78
+ @property
79
+ def previews_directory(self) -> str:
80
+ """The directory where the preview images are stored.
81
+
82
+ Returns:
83
+ str: The directory where the preview images are stored.
84
+ """
85
+ return os.path.join(self.map_directory, "previews")
86
+
87
+ @property
88
+ def generation_info_path(self) -> str:
89
+ """The path to the generation info JSON file.
90
+
91
+ Returns:
92
+ str: The path to the generation info JSON file.
93
+ """
94
+ return os.path.join(self.map_directory, "generation_info.json")
95
+
96
+ def info_sequence(self) -> dict[Any, Any]:
97
+ """Returns the information sequence for the component. Must be implemented in the child
98
+ class. If the component does not have an information sequence, an empty dictionary must be
99
+ returned.
100
+
101
+ Returns:
102
+ dict[Any, Any]: The information sequence for the component.
103
+ """
104
+ return {}
105
+
106
+ def commit_generation_info(self) -> None:
107
+ """Commits the generation info to the generation info JSON file."""
108
+ self.update_generation_info(self.info_sequence())
109
+
110
+ def update_generation_info(self, data: dict[Any, Any]) -> None:
111
+ """Updates the generation info with the provided data.
112
+ If the generation info file does not exist, it will be created.
113
+
114
+ Args:
115
+ data (dict[Any, Any]): The data to update the generation info with.
116
+ """
117
+ if os.path.isfile(self.generation_info_path):
118
+ with open(self.generation_info_path, "r", encoding="utf-8") as file:
119
+ generation_info = json.load(file)
120
+ self.logger.debug("Loaded generation info from %s", self.generation_info_path)
121
+ else:
122
+ self.logger.debug(
123
+ "Generation info file does not exist, creating a new one in %s",
124
+ self.generation_info_path,
125
+ )
126
+ generation_info = {}
127
+
128
+ updated_generation_info = deepcopy(generation_info)
129
+ updated_generation_info[self.__class__.__name__] = data
130
+
131
+ self.logger.debug("Updated generation info, now contains %s fields", len(data))
132
+
133
+ with open(self.generation_info_path, "w", encoding="utf-8") as file:
134
+ json.dump(updated_generation_info, file, indent=4)
135
+
136
+ self.logger.debug("Saved updated generation info to %s", self.generation_info_path)
137
+
138
+ def get_bbox(
139
+ self,
140
+ coordinates: tuple[float, float] | None = None,
141
+ distance: int | None = None,
142
+ project_utm: bool = False,
143
+ ) -> tuple[int, int, int, int]:
73
144
  """Calculates the bounding box of the map from the coordinates and the height and
74
145
  width of the map.
146
+ If coordinates and distance are not provided, the instance variables are used.
75
147
 
76
148
  Args:
149
+ coordinates (tuple[float, float], optional): The latitude and longitude of the center of
150
+ the map. Defaults to None.
151
+ distance (int, optional): The distance from the center of the map to the edge of the
152
+ map. Defaults to None.
77
153
  project_utm (bool, optional): Whether to project the bounding box to UTM.
78
154
 
79
155
  Returns:
80
156
  tuple[int, int, int, int]: The bounding box of the map.
81
157
  """
158
+ coordinates = coordinates or self.coordinates
159
+ distance = distance or int(self.map_height / 2)
160
+
82
161
  north, south, _, _ = ox.utils_geo.bbox_from_point(
83
- self.coordinates, dist=self.map_height / 2, project_utm=project_utm
162
+ coordinates, dist=distance, project_utm=project_utm
84
163
  )
85
164
  _, _, east, west = ox.utils_geo.bbox_from_point(
86
- self.coordinates, dist=self.map_width / 2, project_utm=project_utm
165
+ coordinates, dist=distance, project_utm=project_utm
87
166
  )
88
167
  bbox = north, south, east, west
89
168
  self.logger.debug(
@@ -100,3 +179,21 @@ class Component:
100
179
  """
101
180
  self.bbox = self.get_bbox(project_utm=False)
102
181
  self.logger.debug("Saved bounding box: %s", self.bbox)
182
+
183
+ def get_epsg3857_string(self, bbox: tuple[int, int, int, int] | None = None) -> str:
184
+ """Converts the bounding box to EPSG:3857 string.
185
+ If the bounding box is not provided, the instance variable is used.
186
+
187
+ Args:
188
+ bbox (tuple[int, int, int, int], optional): The bounding box to convert.
189
+
190
+ Returns:
191
+ str: The bounding box in EPSG:3857 string.
192
+ """
193
+ bbox = bbox or self.bbox
194
+ north, south, east, west = bbox
195
+ transformer = Transformer.from_crs("epsg:4326", "epsg:3857")
196
+ epsg3857_north, epsg3857_west = transformer.transform(north, west)
197
+ epsg3857_south, epsg3857_east = transformer.transform(south, east)
198
+
199
+ return f"{epsg3857_north},{epsg3857_south},{epsg3857_east},{epsg3857_west} [EPSG:3857]"
@@ -55,3 +55,34 @@ class Config(Component):
55
55
  list[str]: An empty list.
56
56
  """
57
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(distance=self.map_height)
71
+ south, west, north, east = bbox
72
+ epsg3857_string = self.get_epsg3857_string(bbox=bbox)
73
+
74
+ overview_data = {
75
+ "epsg3857_string": epsg3857_string,
76
+ "south": south,
77
+ "west": west,
78
+ "north": north,
79
+ "east": east,
80
+ "height": self.map_height * 2,
81
+ "width": self.map_width * 2,
82
+ }
83
+
84
+ data = {
85
+ "Overview": overview_data,
86
+ }
87
+
88
+ return data # type: ignore
maps4fs/generator/dem.py CHANGED
@@ -19,7 +19,7 @@ DEFAULT_BLUR_RADIUS = 35
19
19
 
20
20
  # pylint: disable=R0903
21
21
  class DEM(Component):
22
- """Component for map settings and configuration.
22
+ """Component for processing Digital Elevation Model data.
23
23
 
24
24
  Args:
25
25
  coordinates (tuple[float, float]): The latitude and longitude of the center of the map.
@@ -40,7 +40,10 @@ class DEM(Component):
40
40
 
41
41
  self.multiplier = self.kwargs.get("multiplier", DEFAULT_MULTIPLIER)
42
42
  blur_radius = self.kwargs.get("blur_radius", DEFAULT_BLUR_RADIUS)
43
- if blur_radius % 2 == 0:
43
+ if blur_radius is None or blur_radius <= 0:
44
+ # We'll disable blur if the radius is 0 or negative.
45
+ blur_radius = 0
46
+ elif blur_radius % 2 == 0:
44
47
  blur_radius += 1
45
48
  self.blur_radius = blur_radius
46
49
  self.logger.debug(
@@ -49,12 +52,21 @@ class DEM(Component):
49
52
 
50
53
  self.auto_process = self.kwargs.get("auto_process", False)
51
54
 
52
- # pylint: disable=no-member
53
- def process(self) -> None:
54
- """Reads SRTM file, crops it to map size, normalizes and blurs it,
55
- saves to map directory."""
56
- north, south, east, west = self.bbox
55
+ @property
56
+ def dem_path(self) -> str:
57
+ """Returns path to the DEM file.
58
+
59
+ Returns:
60
+ str: Path to the DEM file.
61
+ """
62
+ return self._dem_path
63
+
64
+ def get_output_resolution(self) -> tuple[int, int]:
65
+ """Get output resolution for DEM data.
57
66
 
67
+ Returns:
68
+ tuple[int, int]: Output resolution for DEM data.
69
+ """
58
70
  dem_height = int((self.map_height / 2) * self.game.dem_multipliyer + 1)
59
71
  dem_width = int((self.map_width / 2) * self.game.dem_multipliyer + 1)
60
72
  self.logger.debug(
@@ -63,7 +75,15 @@ class DEM(Component):
63
75
  dem_height,
64
76
  dem_width,
65
77
  )
66
- dem_output_resolution = (dem_width, dem_height)
78
+ return dem_width, dem_height
79
+
80
+ # pylint: disable=no-member
81
+ def process(self) -> None:
82
+ """Reads SRTM file, crops it to map size, normalizes and blurs it,
83
+ saves to map directory."""
84
+ north, south, east, west = self.bbox
85
+
86
+ dem_output_resolution = self.get_output_resolution()
67
87
  self.logger.debug("DEM output resolution: %s.", dem_output_resolution)
68
88
 
69
89
  tile_path = self._srtm_tile()
@@ -122,13 +142,15 @@ class DEM(Component):
122
142
  resampled_data.max(),
123
143
  )
124
144
 
125
- resampled_data = cv2.GaussianBlur(
126
- resampled_data, (self.blur_radius, self.blur_radius), sigmaX=40, sigmaY=40
127
- )
128
- self.logger.debug(
129
- "Gaussion blur applied to DEM data with kernel size %s.",
130
- self.blur_radius,
131
- )
145
+ if self.blur_radius > 0:
146
+ resampled_data = cv2.GaussianBlur(
147
+ resampled_data, (self.blur_radius, self.blur_radius), sigmaX=40, sigmaY=40
148
+ )
149
+ self.logger.debug(
150
+ "Gaussion blur applied to DEM data with kernel size %s.",
151
+ self.blur_radius,
152
+ )
153
+
132
154
  self.logger.debug(
133
155
  "DEM data was blurred. Shape: %s, dtype: %s. Min: %s, max: %s.",
134
156
  resampled_data.shape,
@@ -141,10 +163,20 @@ class DEM(Component):
141
163
  self.logger.debug("DEM data was saved to %s.", self._dem_path)
142
164
 
143
165
  if self.game.additional_dem_name is not None:
144
- dem_directory = os.path.dirname(self._dem_path)
145
- additional_dem_path = os.path.join(dem_directory, self.game.additional_dem_name)
146
- shutil.copyfile(self._dem_path, additional_dem_path)
147
- self.logger.debug("Additional DEM data was copied to %s.", additional_dem_path)
166
+ self.make_copy(self.game.additional_dem_name)
167
+
168
+ def make_copy(self, dem_name: str) -> None:
169
+ """Copies DEM data to additional DEM file.
170
+
171
+ Args:
172
+ dem_name (str): Name of the additional DEM file.
173
+ """
174
+ dem_directory = os.path.dirname(self._dem_path)
175
+
176
+ additional_dem_path = os.path.join(dem_directory, dem_name)
177
+
178
+ shutil.copyfile(self._dem_path, additional_dem_path)
179
+ self.logger.debug("Additional DEM data was copied to %s.", additional_dem_path)
148
180
 
149
181
  def _tile_info(self, lat: float, lon: float) -> tuple[str, str]:
150
182
  """Returns latitude band and tile name for SRTM tile from coordinates.
@@ -234,14 +266,15 @@ class DEM(Component):
234
266
  Returns:
235
267
  str: Path to the preview image.
236
268
  """
237
- rgb_dem_path = self._dem_path.replace(".png", "_grayscale.png")
269
+ # rgb_dem_path = self._dem_path.replace(".png", "_grayscale.png")
270
+ grayscale_dem_path = os.path.join(self.previews_directory, "dem_grayscale.png")
238
271
 
239
- self.logger.debug("Creating grayscale preview of DEM data in %s.", rgb_dem_path)
272
+ self.logger.debug("Creating grayscale preview of DEM data in %s.", grayscale_dem_path)
240
273
 
241
274
  dem_data = cv2.imread(self._dem_path, cv2.IMREAD_GRAYSCALE)
242
275
  dem_data_rgb = cv2.cvtColor(dem_data, cv2.COLOR_GRAY2RGB)
243
- cv2.imwrite(rgb_dem_path, dem_data_rgb)
244
- return rgb_dem_path
276
+ cv2.imwrite(grayscale_dem_path, dem_data_rgb)
277
+ return grayscale_dem_path
245
278
 
246
279
  def colored_preview(self) -> str:
247
280
  """Converts DEM image to colored RGB image and saves it to the map directory.
@@ -251,7 +284,8 @@ class DEM(Component):
251
284
  list[str]: List with a single path to the DEM file
252
285
  """
253
286
 
254
- colored_dem_path = self._dem_path.replace(".png", "_colored.png")
287
+ # colored_dem_path = self._dem_path.replace(".png", "_colored.png")
288
+ colored_dem_path = os.path.join(self.previews_directory, "dem_colored.png")
255
289
 
256
290
  self.logger.debug("Creating colored preview of DEM data in %s.", colored_dem_path)
257
291
 
@@ -292,6 +326,15 @@ class DEM(Component):
292
326
  return [self.grayscale_preview(), self.colored_preview()]
293
327
 
294
328
  def _get_scaling_factor(self, maximum_deviation: int) -> float:
329
+ """Calculate scaling factor for DEM data normalization.
330
+ NOTE: Needs reconsideration for the implementation.
331
+
332
+ Args:
333
+ maximum_deviation (int): Maximum deviation in DEM data.
334
+
335
+ Returns:
336
+ float: Scaling factor for DEM data normalization.
337
+ """
295
338
  ESTIMATED_MAXIMUM_DEVIATION = 1000 # pylint: disable=C0103
296
339
  scaling_factor = maximum_deviation / ESTIMATED_MAXIMUM_DEVIATION
297
340
  return scaling_factor if scaling_factor < 1 else 1
maps4fs/generator/game.py CHANGED
@@ -6,6 +6,7 @@ from __future__ import annotations
6
6
 
7
7
  import os
8
8
 
9
+ from maps4fs.generator.background import Background
9
10
  from maps4fs.generator.config import Config
10
11
  from maps4fs.generator.dem import DEM
11
12
  from maps4fs.generator.i3d import I3d
@@ -35,7 +36,7 @@ class Game:
35
36
  _map_template_path: str | None = None
36
37
  _texture_schema: str | None = None
37
38
 
38
- components = [Config, Texture, DEM, I3d]
39
+ components = [Config, Texture, DEM, I3d, Background]
39
40
 
40
41
  def __init__(self, map_template_path: str | None = None):
41
42
  if map_template_path: