maps4fs 0.7.9__py3-none-any.whl → 0.8.2__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.
@@ -15,6 +15,7 @@ class Component:
15
15
  """Base class for all map generation components.
16
16
 
17
17
  Args:
18
+ game (Game): The game instance for which the map is generated.
18
19
  coordinates (tuple[float, float]): The latitude and longitude of the center of the map.
19
20
  map_height (int): The height of the map in pixels.
20
21
  map_width (int): The width of the map in pixels.
@@ -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)
maps4fs/generator/dem.py CHANGED
@@ -14,7 +14,7 @@ from maps4fs.generator.component import Component
14
14
 
15
15
  SRTM = "https://elevation-tiles-prod.s3.amazonaws.com/skadi/{latitude_band}/{tile_name}.hgt.gz"
16
16
  DEFAULT_MULTIPLIER = 1
17
- DEFAULT_BLUR_RADIUS = 21
17
+ DEFAULT_BLUR_RADIUS = 35
18
18
 
19
19
 
20
20
  # pylint: disable=R0903
@@ -47,6 +47,8 @@ class DEM(Component):
47
47
  "DEM value multiplier is %s, blur radius is %s.", self.multiplier, self.blur_radius
48
48
  )
49
49
 
50
+ self.auto_process = self.kwargs.get("auto_process", False)
51
+
50
52
  # pylint: disable=no-member
51
53
  def process(self) -> None:
52
54
  """Reads SRTM file, crops it to map size, normalizes and blurs it,
@@ -105,15 +107,12 @@ class DEM(Component):
105
107
  resampled_data.min(),
106
108
  )
107
109
 
108
- resampled_data = resampled_data * self.multiplier
109
- self.logger.debug(
110
- "DEM data multiplied by %s. Shape: %s, dtype: %s. Min: %s, max: %s.",
111
- self.multiplier,
112
- resampled_data.shape,
113
- resampled_data.dtype,
114
- resampled_data.min(),
115
- resampled_data.max(),
116
- )
110
+ if self.auto_process:
111
+ self.logger.debug("Auto processing is enabled, will normalize DEM data.")
112
+ resampled_data = self._normalize_dem(resampled_data)
113
+ else:
114
+ self.logger.debug("Auto processing is disabled, DEM data will not be normalized.")
115
+ resampled_data = resampled_data * self.multiplier
117
116
 
118
117
  self.logger.debug(
119
118
  "DEM data was resampled. Shape: %s, dtype: %s. Min: %s, max: %s.",
@@ -123,7 +122,9 @@ class DEM(Component):
123
122
  resampled_data.max(),
124
123
  )
125
124
 
126
- resampled_data = cv2.GaussianBlur(resampled_data, (self.blur_radius, self.blur_radius), 0)
125
+ resampled_data = cv2.GaussianBlur(
126
+ resampled_data, (self.blur_radius, self.blur_radius), sigmaX=40, sigmaY=40
127
+ )
127
128
  self.logger.debug(
128
129
  "Gaussion blur applied to DEM data with kernel size %s.",
129
130
  self.blur_radius,
@@ -289,3 +290,42 @@ class DEM(Component):
289
290
  """
290
291
  self.logger.debug("Starting DEM previews generation.")
291
292
  return [self.grayscale_preview(), self.colored_preview()]
293
+
294
+ def _get_scaling_factor(self, maximum_deviation: int) -> float:
295
+ ESTIMATED_MAXIMUM_DEVIATION = 1000 # pylint: disable=C0103
296
+ scaling_factor = maximum_deviation / ESTIMATED_MAXIMUM_DEVIATION
297
+ return scaling_factor if scaling_factor < 1 else 1
298
+
299
+ def _normalize_dem(self, data: np.ndarray) -> np.ndarray:
300
+ """Normalize DEM data to 16-bit unsigned integer using max height from settings.
301
+ Args:
302
+ data (np.ndarray): DEM data from SRTM file after cropping.
303
+ Returns:
304
+ np.ndarray: Normalized DEM data.
305
+ """
306
+ self.logger.debug("Starting DEM data normalization.")
307
+ # Calculate the difference between the maximum and minimum values in the DEM data.
308
+
309
+ max_height = data.max() # 1800
310
+ min_height = data.min() # 1700
311
+ max_dev = max_height - min_height # 100
312
+ self.logger.debug(
313
+ "Maximum deviation: %s with maximum at %s and minimum at %s.",
314
+ max_dev,
315
+ max_height,
316
+ min_height,
317
+ )
318
+
319
+ scaling_factor = self._get_scaling_factor(max_dev)
320
+ adjusted_max_height = int(65535 * scaling_factor)
321
+ self.logger.debug(
322
+ f"Maximum deviation: {max_dev}. Scaling factor: {scaling_factor}. "
323
+ f"Adjusted max height: {adjusted_max_height}."
324
+ )
325
+ normalized_data = (
326
+ (data - data.min()) / (data.max() - data.min()) * adjusted_max_height
327
+ ).astype("uint16")
328
+ self.logger.debug(
329
+ f"DEM data was normalized to {normalized_data.min()} - {normalized_data.max()}."
330
+ )
331
+ return normalized_data
maps4fs/generator/game.py CHANGED
@@ -8,6 +8,7 @@ import os
8
8
 
9
9
  from maps4fs.generator.config import Config
10
10
  from maps4fs.generator.dem import DEM
11
+ from maps4fs.generator.i3d import I3d
11
12
  from maps4fs.generator.texture import Texture
12
13
 
13
14
  working_directory = os.getcwd()
@@ -34,7 +35,7 @@ class Game:
34
35
  _map_template_path: str | None = None
35
36
  _texture_schema: str | None = None
36
37
 
37
- components = [Config, Texture, DEM]
38
+ components = [Config, Texture, DEM, I3d]
38
39
 
39
40
  def __init__(self, map_template_path: str | None = None):
40
41
  if map_template_path:
@@ -113,6 +114,16 @@ class Game:
113
114
  str: The path to the weights directory."""
114
115
  raise NotImplementedError
115
116
 
117
+ def i3d_file_path(self, map_directory: str) -> str:
118
+ """Returns the path to the i3d file.
119
+
120
+ Arguments:
121
+ map_directory (str): The path to the map directory.
122
+
123
+ Returns:
124
+ str: The path to the i3d file."""
125
+ raise NotImplementedError
126
+
116
127
  @property
117
128
  def additional_dem_name(self) -> str | None:
118
129
  """Returns the name of the additional DEM file.
@@ -122,6 +133,7 @@ class Game:
122
133
  return self._additional_dem_name
123
134
 
124
135
 
136
+ # pylint: disable=W0223
125
137
  class FS22(Game):
126
138
  """Class used to define the game version FS22."""
127
139
 
@@ -189,3 +201,13 @@ class FS25(Game):
189
201
  Returns:
190
202
  str: The path to the weights directory."""
191
203
  return os.path.join(map_directory, "mapUS", "data")
204
+
205
+ def i3d_file_path(self, map_directory: str) -> str:
206
+ """Returns the path to the i3d file.
207
+
208
+ Arguments:
209
+ map_directory (str): The path to the map directory.
210
+
211
+ Returns:
212
+ str: The path to the i3d file."""
213
+ return os.path.join(map_directory, "mapUS", "mapUS.i3d")
@@ -0,0 +1,89 @@
1
+ """This module contains the Config class for map settings and configuration."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import os
6
+ from xml.etree import ElementTree as ET
7
+
8
+ from maps4fs.generator.component import Component
9
+
10
+ DEFAULT_HEIGHT_SCALE = 2000
11
+ DEFAULT_MAX_LOD_DISTANCE = 10000
12
+ DEFAULT_MAX_LOD_OCCLUDER_DISTANCE = 10000
13
+
14
+
15
+ # pylint: disable=R0903
16
+ class I3d(Component):
17
+ """Component for map i3d file settings and configuration.
18
+
19
+ Args:
20
+ coordinates (tuple[float, float]): The latitude and longitude of the center of the map.
21
+ map_height (int): The height of the map in pixels.
22
+ map_width (int): The width of the map in pixels.
23
+ map_directory (str): The directory where the map files are stored.
24
+ logger (Any, optional): The logger to use. Must have at least three basic methods: debug,
25
+ info, warning. If not provided, default logging will be used.
26
+ """
27
+
28
+ _map_i3d_path: str | None = None
29
+
30
+ def preprocess(self) -> None:
31
+ """Gets the path to the map I3D file from the game instance and saves it to the instance
32
+ attribute. If the game does not support I3D files, the attribute is set to None."""
33
+ try:
34
+ self._map_i3d_path = self.game.i3d_file_path(self.map_directory)
35
+ self.logger.debug("Map I3D path: %s.", self._map_i3d_path)
36
+ except NotImplementedError:
37
+ self.logger.info("I3D file processing is not implemented for this game.")
38
+ self._map_i3d_path = None
39
+
40
+ def process(self) -> None:
41
+ """Updates the map I3D file with the default settings."""
42
+ self._update_i3d_file()
43
+
44
+ def _update_i3d_file(self) -> None:
45
+ """Updates the map I3D file with the default settings."""
46
+ if not self._map_i3d_path:
47
+ self.logger.info("I3D is not obtained, skipping the update.")
48
+ return
49
+ if not os.path.isfile(self._map_i3d_path):
50
+ self.logger.warning("I3D file not found: %s.", self._map_i3d_path)
51
+ return
52
+
53
+ tree = ET.parse(self._map_i3d_path)
54
+
55
+ self.logger.debug("Map I3D file loaded from: %s.", self._map_i3d_path)
56
+
57
+ root = tree.getroot()
58
+ for map_elem in root.iter("Scene"):
59
+ for terrain_elem in map_elem.iter("TerrainTransformGroup"):
60
+ terrain_elem.set("heightScale", str(DEFAULT_HEIGHT_SCALE))
61
+ self.logger.debug(
62
+ "heightScale attribute set to %s in TerrainTransformGroup element.",
63
+ DEFAULT_HEIGHT_SCALE,
64
+ )
65
+ terrain_elem.set("maxLODDistance", str(DEFAULT_MAX_LOD_DISTANCE))
66
+ self.logger.debug(
67
+ "maxLODDistance attribute set to %s in TerrainTransformGroup element.",
68
+ DEFAULT_MAX_LOD_DISTANCE,
69
+ )
70
+
71
+ terrain_elem.set("occMaxLODDistance", str(DEFAULT_MAX_LOD_OCCLUDER_DISTANCE))
72
+ self.logger.debug(
73
+ "occMaxLODDistance attribute set to %s in TerrainTransformGroup element.",
74
+ DEFAULT_MAX_LOD_OCCLUDER_DISTANCE,
75
+ )
76
+
77
+ self.logger.debug("TerrainTransformGroup element updated in I3D file.")
78
+
79
+ tree.write(self._map_i3d_path)
80
+ self.logger.debug("Map I3D file saved to: %s.", self._map_i3d_path)
81
+
82
+ def previews(self) -> list[str]:
83
+ """Returns a list of paths to the preview images (empty list).
84
+ The component does not generate any preview images so it returns an empty list.
85
+
86
+ Returns:
87
+ list[str]: An empty list.
88
+ """
89
+ return []
maps4fs/generator/map.py CHANGED
@@ -47,6 +47,7 @@ class Map:
47
47
  self.logger.debug("Game was set to %s", game.code)
48
48
 
49
49
  self.kwargs = kwargs
50
+ self.logger.debug("Additional arguments: %s", kwargs)
50
51
 
51
52
  os.makedirs(self.map_directory, exist_ok=True)
52
53
  self.logger.debug("Map directory created: %s", self.map_directory)
@@ -57,6 +57,7 @@ class Texture(Component):
57
57
  width: int | None = None,
58
58
  color: tuple[int, int, int] | list[int] | None = None,
59
59
  exclude_weight: bool = False,
60
+ priority: int | None = None,
60
61
  ):
61
62
  self.name = name
62
63
  self.count = count
@@ -64,6 +65,7 @@ class Texture(Component):
64
65
  self.width = width
65
66
  self.color = color if color else (255, 255, 255)
66
67
  self.exclude_weight = exclude_weight
68
+ self.priority = priority
67
69
 
68
70
  def to_json(self) -> dict[str, str | list[str] | bool]: # type: ignore
69
71
  """Returns dictionary with layer data.
@@ -77,6 +79,7 @@ class Texture(Component):
77
79
  "width": self.width,
78
80
  "color": list(self.color),
79
81
  "exclude_weight": self.exclude_weight,
82
+ "priority": self.priority,
80
83
  }
81
84
 
82
85
  data = {k: v for k, v in data.items() if v is not None}
@@ -120,11 +123,28 @@ class Texture(Component):
120
123
  self.layers = [self.Layer.from_json(layer) for layer in layers_schema]
121
124
  self.logger.info("Loaded %s layers.", len(self.layers))
122
125
 
126
+ base_layer = self.get_base_layer()
127
+ if base_layer:
128
+ self.logger.info("Base layer found: %s.", base_layer.name)
129
+ else:
130
+ self.logger.warning("No base layer found.")
131
+
123
132
  self._weights_dir = self.game.weights_dir_path(self.map_directory)
124
133
  self.logger.debug("Weights directory: %s.", self._weights_dir)
125
134
  self.info_save_path = os.path.join(self.map_directory, "generation_info.json")
126
135
  self.logger.debug("Generation info save path: %s.", self.info_save_path)
127
136
 
137
+ def get_base_layer(self) -> Layer | None:
138
+ """Returns base layer.
139
+
140
+ Returns:
141
+ Layer | None: Base layer.
142
+ """
143
+ for layer in self.layers:
144
+ if layer.priority == 0:
145
+ return layer
146
+ return None
147
+
128
148
  def process(self):
129
149
  self._prepare_weights()
130
150
  self._read_parameters()
@@ -239,22 +259,79 @@ class Texture(Component):
239
259
  """
240
260
  self._layers = layers
241
261
 
262
+ def layers_by_priority(self) -> list[Layer]:
263
+ """Returns list of layers sorted by priority: None priority layers are first,
264
+ then layers are sorted by priority (descending).
265
+
266
+ Returns:
267
+ list[Layer]: List of layers sorted by priority.
268
+ """
269
+ return sorted(
270
+ self.layers,
271
+ key=lambda _layer: (
272
+ _layer.priority is not None,
273
+ -_layer.priority if _layer.priority is not None else float("inf"),
274
+ ),
275
+ )
276
+
242
277
  # pylint: disable=no-member
243
278
  def draw(self) -> None:
244
279
  """Iterates over layers and fills them with polygons from OSM data."""
245
- for layer in self.layers:
280
+ layers = self.layers_by_priority()
281
+
282
+ self.logger.debug(
283
+ "Sorted layers by priority: %s.", [(layer.name, layer.priority) for layer in layers]
284
+ )
285
+
286
+ cumulative_image = None
287
+
288
+ for layer in layers:
246
289
  if not layer.tags:
247
290
  self.logger.debug("Layer %s has no tags, there's nothing to draw.", layer.name)
248
291
  continue
292
+ if layer.priority == 0:
293
+ self.logger.debug(
294
+ "Found base layer %s. Postponing that to be the last layer drawn.", layer.name
295
+ )
296
+ continue
249
297
  layer_path = layer.path(self._weights_dir)
250
298
  self.logger.debug("Drawing layer %s.", layer_path)
299
+ layer_image = cv2.imread(layer_path, cv2.IMREAD_UNCHANGED)
300
+
301
+ if cumulative_image is None:
302
+ self.logger.debug("First layer, creating new cumulative image.")
303
+ cumulative_image = layer_image
304
+
305
+ mask = cv2.bitwise_not(cumulative_image)
251
306
 
252
- img = cv2.imread(layer_path, cv2.IMREAD_UNCHANGED)
253
307
  for polygon in self.polygons(layer.tags, layer.width): # type: ignore
254
- cv2.fillPoly(img, [polygon], color=255) # type: ignore
255
- cv2.imwrite(layer_path, img)
308
+ cv2.fillPoly(layer_image, [polygon], color=255) # type: ignore
309
+
310
+ output_image = cv2.bitwise_and(layer_image, mask)
311
+
312
+ cumulative_image = cv2.bitwise_or(cumulative_image, output_image)
313
+
314
+ cv2.imwrite(layer_path, output_image)
256
315
  self.logger.debug("Texture %s saved.", layer_path)
257
316
 
317
+ if cumulative_image is not None:
318
+ self.draw_base_layer(cumulative_image)
319
+
320
+ def draw_base_layer(self, cumulative_image: np.ndarray) -> None:
321
+ """Draws base layer and saves it into the png file.
322
+ Base layer is the last layer to be drawn, it fills the remaining area of the map.
323
+
324
+ Args:
325
+ cumulative_image (np.ndarray): Cumulative image with all layers.
326
+ """
327
+ base_layer = self.get_base_layer()
328
+ if base_layer is not None:
329
+ layer_path = base_layer.path(self._weights_dir)
330
+ self.logger.debug("Drawing base layer %s.", layer_path)
331
+ img = cv2.bitwise_not(cumulative_image)
332
+ cv2.imwrite(layer_path, img)
333
+ self.logger.debug("Base texture %s saved.", layer_path)
334
+
258
335
  def get_relative_x(self, x: float) -> int:
259
336
  """Converts UTM X coordinate to relative X coordinate in map image.
260
337
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: maps4fs
3
- Version: 0.7.9
3
+ Version: 0.8.2
4
4
  Summary: Generate map templates for Farming Simulator from real places.
5
5
  Author-email: iwatkot <iwatkot@gmail.com>
6
6
  License: MIT License
@@ -17,6 +17,7 @@ Requires-Dist: opencv-python
17
17
  Requires-Dist: osmnx<2.0.0
18
18
  Requires-Dist: rasterio
19
19
  Requires-Dist: tqdm
20
+ Requires-Dist: folium
20
21
 
21
22
  <div align="center" markdown>
22
23
  <img src="https://github.com/iwatkot/maps4fs/assets/118521851/ffd7f0a3-e317-4c3f-911f-2c2fb736fbfa">
@@ -0,0 +1,15 @@
1
+ maps4fs/__init__.py,sha256=da4jmND2Ths9AffnkAKgzLHNkvKFOc_l21gJisPXqWY,155
2
+ maps4fs/logger.py,sha256=CneeHxQywjNUJXqQrUUSeiDxu95FfrfyK_Si1v0gMZ8,1477
3
+ maps4fs/generator/__init__.py,sha256=zZMLEkGzb4z0xql650gOtGSvcgX58DnJ2yN3vC2daRk,43
4
+ maps4fs/generator/component.py,sha256=CL3DNuvKAd44pynLfir_JLnBC0c9Kr233_BbSHQng6Q,3534
5
+ maps4fs/generator/config.py,sha256=ln_XGUdiPnc4zzhMVEueZRf-FGljeHtCHUnJ6FRlE4w,2264
6
+ maps4fs/generator/dem.py,sha256=L91hOxoXnn2LtpaH7fVA6Ke2i7JNfFk_KGMsVWHVNps,13169
7
+ maps4fs/generator/game.py,sha256=mN9VeN4nk-e43oFujIC5GOGAQOYATKrmeIb1kLHuwtI,6773
8
+ maps4fs/generator/i3d.py,sha256=UuQiQRusPQ2f6PvGKxJ_UZeXsx3uG15efmy8Ysnm3F8,3597
9
+ maps4fs/generator/map.py,sha256=xYbSyGrcEB9CRJhVf8iTRCKLK6BvnewtlKO_Ut--Oik,3858
10
+ maps4fs/generator/texture.py,sha256=uH09vnYSTklv2VxV6ys0DM_f8KXHmxQr9l6flwVguLU,18726
11
+ maps4fs-0.8.2.dist-info/LICENSE.md,sha256=-JY0v7p3dwXze61EbYiK7YEJ2aKrjaFZ8y2xYEOrmRY,1068
12
+ maps4fs-0.8.2.dist-info/METADATA,sha256=Ombva3jSyqSsTuzspfzp24DESv1G8lqRWLTuTN0L81w,11866
13
+ maps4fs-0.8.2.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
14
+ maps4fs-0.8.2.dist-info/top_level.txt,sha256=Ue9DSRlejRQRCaJueB0uLcKrWwsEq9zezfv5dI5mV1M,8
15
+ maps4fs-0.8.2.dist-info/RECORD,,
@@ -1,14 +0,0 @@
1
- maps4fs/__init__.py,sha256=da4jmND2Ths9AffnkAKgzLHNkvKFOc_l21gJisPXqWY,155
2
- maps4fs/logger.py,sha256=CneeHxQywjNUJXqQrUUSeiDxu95FfrfyK_Si1v0gMZ8,1477
3
- maps4fs/generator/__init__.py,sha256=zZMLEkGzb4z0xql650gOtGSvcgX58DnJ2yN3vC2daRk,43
4
- maps4fs/generator/component.py,sha256=BU_HNNyKXuEBohQJRTlXmNdSNrjolwX-ZzqqSSuzTrE,3463
5
- maps4fs/generator/config.py,sha256=luDYXkKjgOM4-Mft4mHDPP6v-DVIUyWZAMAsCKyvaoY,2108
6
- maps4fs/generator/dem.py,sha256=61SOBf1-Tr0AQb_HeYrnvDbKR4rR_7PfzL7i99ADZEA,11414
7
- maps4fs/generator/game.py,sha256=dfbLyu1yqbHkBWmJu1uANCX-84vkdlvqR6hA6JCiIsE,6107
8
- maps4fs/generator/map.py,sha256=zLIFAjjz-Y7u_FG-UjYoGiqBreLwuverkYY8Er9L_Qg,3796
9
- maps4fs/generator/texture.py,sha256=cEXQOLJ5Iq_Nb0C9OmU-9PR-E_5cgM1Q62kG49G3VNg,16019
10
- maps4fs-0.7.9.dist-info/LICENSE.md,sha256=-JY0v7p3dwXze61EbYiK7YEJ2aKrjaFZ8y2xYEOrmRY,1068
11
- maps4fs-0.7.9.dist-info/METADATA,sha256=AoopE2APfkWbDK6H25pTqXuObQ_suF7a2kOxbiNGEPg,11844
12
- maps4fs-0.7.9.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
13
- maps4fs-0.7.9.dist-info/top_level.txt,sha256=Ue9DSRlejRQRCaJueB0uLcKrWwsEq9zezfv5dI5mV1M,8
14
- maps4fs-0.7.9.dist-info/RECORD,,