maps4fs 1.0.8__py3-none-any.whl → 1.0.9__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.

Potentially problematic release.


This version of maps4fs might be problematic. Click here for more details.

@@ -9,6 +9,7 @@ from typing import TYPE_CHECKING, Any
9
9
 
10
10
  import osmnx as ox # type: ignore
11
11
  from pyproj import Transformer
12
+ from shapely.geometry import Polygon, box # type: ignore
12
13
 
13
14
  from maps4fs.generator.qgis import save_scripts
14
15
 
@@ -16,7 +17,7 @@ if TYPE_CHECKING:
16
17
  from maps4fs.generator.game import Game
17
18
 
18
19
 
19
- # pylint: disable=R0801, R0903, R0902
20
+ # pylint: disable=R0801, R0903, R0902, R0904
20
21
  class Component:
21
22
  """Base class for all map generation components.
22
23
 
@@ -281,3 +282,97 @@ class Component:
281
282
  """
282
283
  class_name = self.__class__.__name__.lower()
283
284
  save_scripts(qgis_layers, class_name, self.scripts_directory)
285
+
286
+ def get_polygon_center(self, polygon_points: list[tuple[int, int]]) -> tuple[int, int]:
287
+ """Calculates the center of a polygon defined by a list of points.
288
+
289
+ Arguments:
290
+ polygon_points (list[tuple[int, int]]): The points of the polygon.
291
+
292
+ Returns:
293
+ tuple[int, int]: The center of the polygon.
294
+ """
295
+ polygon = Polygon(polygon_points)
296
+ center = polygon.centroid
297
+ return int(center.x), int(center.y)
298
+
299
+ def absolute_to_relative(
300
+ self, point: tuple[int, int], center: tuple[int, int]
301
+ ) -> tuple[int, int]:
302
+ """Converts a pair of absolute coordinates to relative coordinates.
303
+
304
+ Arguments:
305
+ point (tuple[int, int]): The absolute coordinates.
306
+ center (tuple[int, int]): The center coordinates.
307
+
308
+ Returns:
309
+ tuple[int, int]: The relative coordinates.
310
+ """
311
+ cx, cy = center
312
+ x, y = point
313
+ return x - cx, y - cy
314
+
315
+ def top_left_coordinates_to_center(self, top_left: tuple[int, int]) -> tuple[int, int]:
316
+ """Converts a pair of coordinates from the top-left system to the center system.
317
+ In top-left system, the origin (0, 0) is in the top-left corner of the map, while in the
318
+ center system, the origin is in the center of the map.
319
+
320
+ Arguments:
321
+ top_left (tuple[int, int]): The coordinates in the top-left system.
322
+
323
+ Returns:
324
+ tuple[int, int]: The coordinates in the center system.
325
+ """
326
+ x, y = top_left
327
+ cs_x = x - self.map_width // 2
328
+ cs_y = y - self.map_height // 2
329
+
330
+ return cs_x, cs_y
331
+
332
+ def fit_polygon_into_bounds(
333
+ self, polygon_points: list[tuple[int, int]], margin: int = 0
334
+ ) -> list[tuple[int, int]]:
335
+ """Fits a polygon into the bounds of the map.
336
+
337
+ Arguments:
338
+ polygon_points (list[tuple[int, int]]): The points of the polygon.
339
+ margin (int, optional): The margin to add to the polygon. Defaults to 0.
340
+
341
+ Returns:
342
+ list[tuple[int, int]]: The points of the polygon fitted into the map bounds.
343
+ """
344
+ min_x = min_y = 0
345
+ max_x, max_y = self.map_width, self.map_height
346
+
347
+ # Create a polygon from the given points
348
+ polygon = Polygon(polygon_points)
349
+
350
+ if margin:
351
+ polygon = polygon.buffer(margin, join_style="mitre")
352
+
353
+ # Create a bounding box for the map bounds
354
+ bounds = box(min_x, min_y, max_x, max_y)
355
+
356
+ # Intersect the polygon with the bounds to fit it within the map
357
+ fitted_polygon = polygon.intersection(bounds)
358
+
359
+ if not isinstance(fitted_polygon, Polygon):
360
+ raise ValueError("The fitted polygon is not a valid polygon.")
361
+
362
+ # Return the fitted polygon points
363
+ return list(fitted_polygon.exterior.coords)
364
+
365
+ def get_infolayer_path(self, layer_name: str) -> str | None:
366
+ """Returns the path to the info layer file.
367
+
368
+ Arguments:
369
+ layer_name (str): The name of the layer.
370
+
371
+ Returns:
372
+ str | None: The path to the info layer file or None if the layer does not exist.
373
+ """
374
+ info_layer_path = os.path.join(self.info_layers_directory, f"{layer_name}.json")
375
+ if not os.path.isfile(info_layer_path):
376
+ self.logger.warning("Info layer %s does not exist", info_layer_path)
377
+ return None
378
+ return info_layer_path
maps4fs/generator/grle.py CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  import json
4
4
  import os
5
+ from xml.etree import ElementTree as ET
5
6
 
6
7
  import cv2
7
8
  import numpy as np
@@ -27,6 +28,9 @@ class GRLE(Component):
27
28
  def preprocess(self) -> None:
28
29
  """Gets the path to the map I3D file from the game instance and saves it to the instance
29
30
  attribute. If the game does not support I3D files, the attribute is set to None."""
31
+
32
+ self.farmland_margin = self.kwargs.get("farmland_margin", 0)
33
+
30
34
  try:
31
35
  grle_schema_path = self.game.grle_schema
32
36
  except ValueError:
@@ -55,15 +59,22 @@ class GRLE(Component):
55
59
 
56
60
  height = int(self.map_height * info_layer["height_multiplier"])
57
61
  width = int(self.map_width * info_layer["width_multiplier"])
62
+ channels = info_layer["channels"]
58
63
  data_type = info_layer["data_type"]
59
64
 
60
65
  # Create the InfoLayer PNG file with zeros.
61
- info_layer_data = np.zeros((height, width), dtype=data_type)
66
+ if channels == 1:
67
+ info_layer_data = np.zeros((height, width), dtype=data_type)
68
+ else:
69
+ info_layer_data = np.zeros((height, width, channels), dtype=data_type)
70
+ self.logger.debug("Shape of %s: %s.", info_layer["name"], info_layer_data.shape)
62
71
  cv2.imwrite(file_path, info_layer_data) # pylint: disable=no-member
63
72
  self.logger.debug("InfoLayer PNG file %s created.", file_path)
64
73
  else:
65
74
  self.logger.warning("Invalid InfoLayer schema: %s.", info_layer)
66
75
 
76
+ self._add_farmlands()
77
+
67
78
  def previews(self) -> list[str]:
68
79
  """Returns a list of paths to the preview images (empty list).
69
80
  The component does not generate any preview images so it returns an empty list.
@@ -72,3 +83,72 @@ class GRLE(Component):
72
83
  list[str]: An empty list.
73
84
  """
74
85
  return []
86
+
87
+ # pylint: disable=R0801, R0914
88
+ def _add_farmlands(self) -> None:
89
+ """Adds farmlands to the InfoLayer PNG file."""
90
+
91
+ textures_info_layer_path = self.get_infolayer_path("textures")
92
+ if not textures_info_layer_path:
93
+ return
94
+
95
+ with open(textures_info_layer_path, "r", encoding="utf-8") as textures_info_layer_file:
96
+ textures_info_layer = json.load(textures_info_layer_file)
97
+
98
+ fields: list[list[tuple[int, int]]] | None = textures_info_layer.get("fields")
99
+ if not fields:
100
+ self.logger.warning("Fields data not found in textures info layer.")
101
+ return
102
+
103
+ self.logger.info("Found %s fields in textures info layer.", len(fields))
104
+
105
+ info_layer_farmlands_path = os.path.join(
106
+ self.game.weights_dir_path(self.map_directory), "infoLayer_farmlands.png"
107
+ )
108
+
109
+ if not os.path.isfile(info_layer_farmlands_path):
110
+ self.logger.warning("InfoLayer PNG file %s not found.", info_layer_farmlands_path)
111
+ return
112
+
113
+ # pylint: disable=no-member
114
+ image = cv2.imread(info_layer_farmlands_path, cv2.IMREAD_UNCHANGED)
115
+ farmlands_xml_path = os.path.join(self.map_directory, "map/config/farmlands.xml")
116
+ if not os.path.isfile(farmlands_xml_path):
117
+ self.logger.warning("Farmlands XML file %s not found.", farmlands_xml_path)
118
+ return
119
+
120
+ tree = ET.parse(farmlands_xml_path)
121
+ farmlands_xml = tree.find("farmlands")
122
+
123
+ for field_id, field in enumerate(fields, start=1):
124
+ try:
125
+ fitted_field = self.fit_polygon_into_bounds(field, self.farmland_margin)
126
+ except ValueError as e:
127
+ self.logger.warning("Field %s could not be fitted into the map bounds.", field_id)
128
+ self.logger.debug("Error: %s", e)
129
+ continue
130
+
131
+ field_np = np.array(fitted_field, np.int32)
132
+ field_np = field_np.reshape((-1, 1, 2))
133
+
134
+ # Infolayer image is 1/2 of the size of the map image, that's why we need to divide
135
+ # the coordinates by 2.
136
+ field_np = field_np // 2
137
+
138
+ # pylint: disable=no-member
139
+ cv2.fillPoly(image, [field_np], field_id) # type: ignore
140
+
141
+ # Add the field to the farmlands XML.
142
+ farmland = ET.SubElement(farmlands_xml, "farmland") # type: ignore
143
+ farmland.set("id", str(field_id))
144
+ farmland.set("priceScale", "1")
145
+ farmland.set("npcName", "FORESTER")
146
+
147
+ tree.write(farmlands_xml_path)
148
+
149
+ self.logger.info("Farmlands added to the farmlands XML file: %s.", farmlands_xml_path)
150
+
151
+ cv2.imwrite(info_layer_farmlands_path, image) # pylint: disable=no-member
152
+ self.logger.info(
153
+ "Farmlands added to the InfoLayer PNG file: %s.", info_layer_farmlands_path
154
+ )
maps4fs/generator/i3d.py CHANGED
@@ -6,8 +6,6 @@ import json
6
6
  import os
7
7
  from xml.etree import ElementTree as ET
8
8
 
9
- from shapely.geometry import Polygon, box # type: ignore
10
-
11
9
  from maps4fs.generator.component import Component
12
10
 
13
11
  DEFAULT_HEIGHT_SCALE = 2000
@@ -103,15 +101,14 @@ class I3d(Component):
103
101
  if tree is None:
104
102
  return
105
103
 
106
- textures_info_layer_path = os.path.join(self.info_layers_directory, "textures.json")
107
- if not os.path.isfile(textures_info_layer_path):
108
- self.logger.warning("Textures info layer not found: %s.", textures_info_layer_path)
104
+ textures_info_layer_path = self.get_infolayer_path("textures")
105
+ if not textures_info_layer_path:
109
106
  return
110
107
 
111
108
  with open(textures_info_layer_path, "r", encoding="utf-8") as textures_info_layer_file:
112
109
  textures_info_layer = json.load(textures_info_layer_file)
113
110
 
114
- fields: list[tuple[int, int]] | None = textures_info_layer.get("fields")
111
+ fields: list[list[tuple[int, int]]] | None = textures_info_layer.get("fields")
115
112
  if not fields:
116
113
  self.logger.warning("Fields data not found in textures info layer.")
117
114
  return
@@ -130,7 +127,7 @@ class I3d(Component):
130
127
  for field_id, field in enumerate(fields, start=1):
131
128
  # Convert the top-left coordinates to the center coordinates system.
132
129
  try:
133
- fitted_field = self.fit_polygon_into_bounds(field) # type: ignore
130
+ fitted_field = self.fit_polygon_into_bounds(field)
134
131
  except ValueError as e:
135
132
  self.logger.warning(
136
133
  "Field %s could not be fitted into the map bounds.", field_id
@@ -233,81 +230,6 @@ class I3d(Component):
233
230
 
234
231
  return teleport_indicator_node, node_id
235
232
 
236
- def get_polygon_center(self, polygon_points: list[tuple[int, int]]) -> tuple[int, int]:
237
- """Calculates the center of a polygon defined by a list of points.
238
-
239
- Arguments:
240
- polygon_points (list[tuple[int, int]]): The points of the polygon.
241
-
242
- Returns:
243
- tuple[int, int]: The center of the polygon.
244
- """
245
- polygon = Polygon(polygon_points)
246
- center = polygon.centroid
247
- return int(center.x), int(center.y)
248
-
249
- def absolute_to_relative(
250
- self, point: tuple[int, int], center: tuple[int, int]
251
- ) -> tuple[int, int]:
252
- """Converts a pair of absolute coordinates to relative coordinates.
253
-
254
- Arguments:
255
- point (tuple[int, int]): The absolute coordinates.
256
- center (tuple[int, int]): The center coordinates.
257
-
258
- Returns:
259
- tuple[int, int]: The relative coordinates.
260
- """
261
- cx, cy = center
262
- x, y = point
263
- return x - cx, y - cy
264
-
265
- def top_left_coordinates_to_center(self, top_left: tuple[int, int]) -> tuple[int, int]:
266
- """Converts a pair of coordinates from the top-left system to the center system.
267
- In top-left system, the origin (0, 0) is in the top-left corner of the map, while in the
268
- center system, the origin is in the center of the map.
269
-
270
- Arguments:
271
- top_left (tuple[int, int]): The coordinates in the top-left system.
272
-
273
- Returns:
274
- tuple[int, int]: The coordinates in the center system.
275
- """
276
- x, y = top_left
277
- cs_x = x - self.map_width // 2
278
- cs_y = y - self.map_height // 2
279
-
280
- return cs_x, cs_y
281
-
282
- def fit_polygon_into_bounds(
283
- self, polygon_points: list[tuple[int, int]]
284
- ) -> list[tuple[int, int]]:
285
- """Fits a polygon into the bounds of the map.
286
-
287
- Arguments:
288
- polygon_points (list[tuple[int, int]]): The points of the polygon.
289
-
290
- Returns:
291
- list[tuple[int, int]]: The points of the polygon fitted into the map bounds.
292
- """
293
- min_x = min_y = 0
294
- max_x, max_y = self.map_width, self.map_height
295
-
296
- # Create a polygon from the given points
297
- polygon = Polygon(polygon_points)
298
-
299
- # Create a bounding box for the map bounds
300
- bounds = box(min_x, min_y, max_x, max_y)
301
-
302
- # Intersect the polygon with the bounds to fit it within the map
303
- fitted_polygon = polygon.intersection(bounds)
304
-
305
- if not isinstance(fitted_polygon, Polygon):
306
- raise ValueError("The fitted polygon is not a valid polygon.")
307
-
308
- # Return the fitted polygon points
309
- return list(fitted_polygon.exterior.coords)
310
-
311
233
  @staticmethod
312
234
  def create_user_attribute_node(node_id: int) -> ET.Element:
313
235
  """Creates an XML user attribute node with given node ID.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: maps4fs
3
- Version: 1.0.8
3
+ Version: 1.0.9
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
@@ -69,8 +69,9 @@ Requires-Dist: pympler
69
69
  🚜 Supports Farming Simulator 22 and 25<br>
70
70
  🔷 Generates *.obj files for background terrain based on the real-world height map<br>
71
71
  📄 Generates scripts to download high-resolution satellite images from [QGIS](https://qgis.org/download/) in one click<br>
72
- 🧰 Modder Toolbox to help you with various tasks 🆕<br>
72
+ 🧰 Modder Toolbox to help you with various tasks <br>
73
73
  🌾 Automatically generates fields 🆕<br>
74
+ 🌽 Automatically generates farmlands 🆕<br>
74
75
 
75
76
  <p align="center">
76
77
  <img src="https://github.com/user-attachments/assets/cf8f5752-9c69-4018-bead-290f59ba6976"><br>
@@ -81,13 +82,15 @@ Requires-Dist: pympler
81
82
  🗒️ True-to-life blueprints for fast and precise modding.<br><br>
82
83
  <img width="480" src="https://github.com/user-attachments/assets/1a8802d2-6a3b-4bfa-af2b-7c09478e199b"><br>
83
84
  🌾 Field generation with one click.<br><br>
85
+ <img width="480" src="https://github.com/user-attachments/assets/4d1fa879-5d60-438b-a84e-16883bcef0ec"><br>
86
+ 🌽 Automatic farmlands generation based on the fields.<br><br>
84
87
  <img src="https://github.com/user-attachments/assets/cce45575-c917-4a1b-bdc0-6368e32ccdff"><br>
85
88
  📏 Almost any possible map sizes.
86
89
  </p>
87
90
 
88
91
  📹 A complete step-by-step video tutorial is here!
89
92
  <a href="https://www.youtube.com/watch?v=Nl_aqXJ5nAk" target="_blank"><img src="https://github.com/user-attachments/assets/4845e030-0e73-47ab-a5a3-430308913060"/></a>
90
- <i>How to Generate a Map for Farming Simulator 25 and 22 from a real place using maps4FS</i>
93
+ <p align="center"><i>How to Generate a Map for Farming Simulator 25 and 22 from a real place using maps4FS.</i></p>
91
94
 
92
95
  ## Quick Start
93
96
  There are several ways to use the tool. You obviously need the **first one**, but you can choose any of the others depending on your needs.<br>
@@ -472,6 +475,10 @@ You can also apply some advanced settings to the map generation process. Note th
472
475
 
473
476
  - Fields padding - this value (in meters) will be applied to each field, making it smaller. It's useful when the fields are too close to each other and you want to make them smaller. By default, it's set to 0.
474
477
 
478
+ ### Farmlands Advanced settings
479
+
480
+ - Farmlands margin - this value (in meters) will be applied to each farmland, making it bigger. You can use the value to adjust how much the farmland should be bigger than the actual field. By default, it's set to 3.
481
+
475
482
  ## Resources
476
483
  In this section, you'll find a list of the resources that you need to create a map for the Farming Simulator.<br>
477
484
  To create a basic map, you only need the Giants Editor. But if you want to create a background terrain - the world around the map, so it won't look like it's floating in the void - you also need Blender and the Blender Exporter Plugins. To create realistic textures for the background terrain, the QGIS is required to obtain high-resolution satellite images.<br>
@@ -2,12 +2,12 @@ maps4fs/__init__.py,sha256=da4jmND2Ths9AffnkAKgzLHNkvKFOc_l21gJisPXqWY,155
2
2
  maps4fs/logger.py,sha256=B-NEYpMjPAAqlV4VpfTi6nbBFnEABVtQOaYe6nMpidg,1489
3
3
  maps4fs/generator/__init__.py,sha256=zZMLEkGzb4z0xql650gOtGSvcgX58DnJ2yN3vC2daRk,43
4
4
  maps4fs/generator/background.py,sha256=ogd5TmAWL5zhZtTCOH8YHGKqc0SGQqOsWuVg3AaZO0I,14015
5
- maps4fs/generator/component.py,sha256=swOocaEOP3XtZgHfgDJ0ROALWoLgCJwMq8ubl0d2WrI,11085
5
+ maps4fs/generator/component.py,sha256=ac0l1nUeRnMejNgJ26PO0Jjb2ELYSaagM0mzNjM_w5o,14582
6
6
  maps4fs/generator/config.py,sha256=kspXIT2o-_28EU0RQsROHCjkgQdqQnvreAKP5QAC5Ws,4279
7
7
  maps4fs/generator/dem.py,sha256=cCJLE20-XKaQx5lwIFNEgmQ5kfhE24QmVrAyMVwsU_A,16459
8
8
  maps4fs/generator/game.py,sha256=4I6edxTeZf41Vgvx6BaucEflMEHomRRvdMZRJAPm0d4,7450
9
- maps4fs/generator/grle.py,sha256=qy1tGxDNCBql1dxYBwN2Iu0g4XFFPCDvlvx9bEUoXWM,3090
10
- maps4fs/generator/i3d.py,sha256=0rZyVLQBn1R0orIOfVvZewGMVWR-mAoqwqa6vebypZQ,13952
9
+ maps4fs/generator/grle.py,sha256=5Ck44CKI-yxnxgy8K_18L0pFAfln5e9e7DVkpNZVwC0,6404
10
+ maps4fs/generator/i3d.py,sha256=CPscvhuoBRfGJSmcCGHyjM5FKlDXAbYALJkvhGv4UrA,11091
11
11
  maps4fs/generator/map.py,sha256=gDZUZ2wimoeA8mHVOCnZvrIBeK7b99OIWFd_LjruqBc,4677
12
12
  maps4fs/generator/path_steps.py,sha256=twhoP0KOYWOpOJfYrSWPHygtIeM-r5cIlePg1SHVyHk,3589
13
13
  maps4fs/generator/qgis.py,sha256=Es8hLuqN_KH8lDfnJE6He2rWYbAKJ3RGPn-o87S6CPI,6116
@@ -16,8 +16,8 @@ maps4fs/generator/tile.py,sha256=z1-xEVjgFNf2WzLkgwoGGq8nREJpjPljeC9lmb5xPKA,199
16
16
  maps4fs/toolbox/__init__.py,sha256=zZMLEkGzb4z0xql650gOtGSvcgX58DnJ2yN3vC2daRk,43
17
17
  maps4fs/toolbox/background.py,sha256=9BXWNqs_n3HgqDiPztWylgYk_QM4YgBpe6_ZNQAWtSc,2154
18
18
  maps4fs/toolbox/dem.py,sha256=z9IPFNmYbjiigb3t02ZenI3Mo8odd19c5MZbjDEovTo,3525
19
- maps4fs-1.0.8.dist-info/LICENSE.md,sha256=pTKD_oUexcn-yccFCTrMeLkZy0ifLRa-VNcDLqLZaIw,10749
20
- maps4fs-1.0.8.dist-info/METADATA,sha256=IOBr8g1SaorXmDjL1DR33Lm431WpWb8j5PLCUfxXNUk,27836
21
- maps4fs-1.0.8.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
22
- maps4fs-1.0.8.dist-info/top_level.txt,sha256=Ue9DSRlejRQRCaJueB0uLcKrWwsEq9zezfv5dI5mV1M,8
23
- maps4fs-1.0.8.dist-info/RECORD,,
19
+ maps4fs-1.0.9.dist-info/LICENSE.md,sha256=pTKD_oUexcn-yccFCTrMeLkZy0ifLRa-VNcDLqLZaIw,10749
20
+ maps4fs-1.0.9.dist-info/METADATA,sha256=2cnIMdJN45eVlg9tBVPAwJsoBWVz1qavS6U9B4-iVAU,28327
21
+ maps4fs-1.0.9.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
22
+ maps4fs-1.0.9.dist-info/top_level.txt,sha256=Ue9DSRlejRQRCaJueB0uLcKrWwsEq9zezfv5dI5mV1M,8
23
+ maps4fs-1.0.9.dist-info/RECORD,,