maps4fs 1.6.91__py3-none-any.whl → 1.7.5__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.
- maps4fs/__init__.py +4 -2
- maps4fs/generator/component.py +6 -15
- maps4fs/generator/dtm/dtm.py +276 -48
- maps4fs/generator/dtm/nrw.py +127 -0
- maps4fs/generator/dtm/srtm.py +59 -161
- maps4fs/generator/dtm/usgs.py +6 -222
- maps4fs/generator/settings.py +1 -1
- maps4fs/generator/texture.py +51 -49
- maps4fs/toolbox/custom_osm.py +67 -0
- {maps4fs-1.6.91.dist-info → maps4fs-1.7.5.dist-info}/METADATA +64 -29
- {maps4fs-1.6.91.dist-info → maps4fs-1.7.5.dist-info}/RECORD +14 -12
- {maps4fs-1.6.91.dist-info → maps4fs-1.7.5.dist-info}/WHEEL +1 -1
- {maps4fs-1.6.91.dist-info → maps4fs-1.7.5.dist-info}/LICENSE.md +0 -0
- {maps4fs-1.6.91.dist-info → maps4fs-1.7.5.dist-info}/top_level.txt +0 -0
maps4fs/generator/dtm/srtm.py
CHANGED
@@ -7,7 +7,7 @@ import math
|
|
7
7
|
import os
|
8
8
|
import shutil
|
9
9
|
|
10
|
-
import
|
10
|
+
import requests
|
11
11
|
|
12
12
|
from maps4fs.generator.dtm.dtm import DTMProvider, DTMProviderSettings
|
13
13
|
|
@@ -15,9 +15,6 @@ from maps4fs.generator.dtm.dtm import DTMProvider, DTMProviderSettings
|
|
15
15
|
class SRTM30ProviderSettings(DTMProviderSettings):
|
16
16
|
"""Settings for SRTM 30m provider."""
|
17
17
|
|
18
|
-
easy_mode: bool = True
|
19
|
-
power_factor: int = 0
|
20
|
-
|
21
18
|
|
22
19
|
class SRTM30Provider(DTMProvider):
|
23
20
|
"""Provider of Shuttle Radar Topography Mission (SRTM) 30m data."""
|
@@ -32,22 +29,6 @@ class SRTM30Provider(DTMProvider):
|
|
32
29
|
|
33
30
|
_author = "[iwatkot](https://github.com/iwatkot)"
|
34
31
|
|
35
|
-
_instructions = (
|
36
|
-
"ℹ️ If you don't know how to work with DEM data, it is recommended to use the "
|
37
|
-
"**Easy mode** option. It will automatically change the values in the image, so the "
|
38
|
-
"terrain will be visible in the Giants Editor. If you're an experienced modder, it's "
|
39
|
-
"recommended to disable this option and work with the DEM data in a usual way. \n"
|
40
|
-
"ℹ️ If the terrain height difference in the real world is bigger than 255 meters, "
|
41
|
-
"the [Height scale](https://github.com/iwatkot/maps4fs/blob/main/docs/dem.md#height-scale)"
|
42
|
-
" parameter in the **map.i3d** file will be changed automatically. \n"
|
43
|
-
"⚡ If the **Easy mode** option is disabled, you will probably get completely flat "
|
44
|
-
"terrain, unless you adjust the DEM Multiplier Setting or the Height scale parameter in "
|
45
|
-
"the Giants Editor. \n"
|
46
|
-
"💡 You can use the **Power factor** setting to make the difference between heights "
|
47
|
-
"bigger. Be extremely careful with this setting, and use only low values, otherwise your "
|
48
|
-
"terrain may be completely broken. \n"
|
49
|
-
)
|
50
|
-
|
51
32
|
_settings = SRTM30ProviderSettings
|
52
33
|
|
53
34
|
def __init__(self, *args, **kwargs):
|
@@ -58,169 +39,86 @@ class SRTM30Provider(DTMProvider):
|
|
58
39
|
os.makedirs(self.gz_directory, exist_ok=True)
|
59
40
|
self.data_info: dict[str, int | str | float] | None = None # type: ignore
|
60
41
|
|
61
|
-
def
|
62
|
-
"""
|
42
|
+
def download_tiles(self):
|
43
|
+
"""Download SRTM tiles."""
|
44
|
+
north, south, east, west = self.get_bbox()
|
63
45
|
|
64
|
-
|
65
|
-
|
66
|
-
|
46
|
+
tiles = []
|
47
|
+
# Look at each corner of the bbox in case the bbox spans across multiple tiles
|
48
|
+
for pair in [(north, east), (south, west), (south, east), (north, west)]:
|
49
|
+
tile_parameters = self.get_tile_parameters(*pair)
|
50
|
+
tile_name = tile_parameters["tile_name"]
|
51
|
+
decompressed_tile_path = os.path.join(self.hgt_directory, f"{tile_name}.hgt")
|
67
52
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
53
|
+
if not os.path.isfile(decompressed_tile_path):
|
54
|
+
compressed_tile_path = os.path.join(self.gz_directory, f"{tile_name}.hgt.gz")
|
55
|
+
if not self.get_or_download_tile(compressed_tile_path, **tile_parameters):
|
56
|
+
raise FileNotFoundError(f"Tile {tile_name} not found.")
|
72
57
|
|
73
|
-
|
74
|
-
|
58
|
+
with gzip.open(compressed_tile_path, "rb") as f_in:
|
59
|
+
with open(decompressed_tile_path, "wb") as f_out:
|
60
|
+
shutil.copyfileobj(f_in, f_out)
|
61
|
+
tiles.append(decompressed_tile_path)
|
75
62
|
|
76
|
-
|
77
|
-
if lon < 0:
|
78
|
-
tile_name = f"{latitude_band}W{abs(tile_longitude):03d}"
|
79
|
-
else:
|
80
|
-
tile_name = f"{latitude_band}E{abs(tile_longitude):03d}"
|
63
|
+
return tiles
|
81
64
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
return {"latitude_band": latitude_band, "tile_name": tile_name}
|
65
|
+
# region provider specific helpers
|
66
|
+
def download_tile(self, output_path: str, **kwargs) -> bool:
|
67
|
+
"""Download a tile from the provider.
|
86
68
|
|
87
|
-
|
88
|
-
|
69
|
+
Arguments:
|
70
|
+
output_path (str): Path to save the downloaded tile.
|
89
71
|
|
90
72
|
Returns:
|
91
|
-
|
92
|
-
"""
|
93
|
-
tile_parameters = self.get_tile_parameters(*self.coordinates)
|
94
|
-
tile_name = tile_parameters["tile_name"]
|
95
|
-
decompressed_tile_path = os.path.join(self.hgt_directory, f"{tile_name}.hgt")
|
96
|
-
|
97
|
-
if not os.path.isfile(decompressed_tile_path):
|
98
|
-
compressed_tile_path = os.path.join(self.gz_directory, f"{tile_name}.hgt.gz")
|
99
|
-
if not self.get_or_download_tile(compressed_tile_path, **tile_parameters):
|
100
|
-
raise FileNotFoundError(f"Tile {tile_name} not found.")
|
101
|
-
|
102
|
-
with gzip.open(compressed_tile_path, "rb") as f_in:
|
103
|
-
with open(decompressed_tile_path, "wb") as f_out:
|
104
|
-
shutil.copyfileobj(f_in, f_out)
|
105
|
-
|
106
|
-
data = self.extract_roi(decompressed_tile_path)
|
107
|
-
|
108
|
-
self.data_info = {}
|
109
|
-
self.add_numpy_params(data, "original")
|
110
|
-
|
111
|
-
data = self.signed_to_unsigned(data)
|
112
|
-
self.add_numpy_params(data, "grounded")
|
113
|
-
|
114
|
-
original_deviation = int(self.data_info["original_deviation"])
|
115
|
-
in_game_maximum_height = 65535 // 255
|
116
|
-
if original_deviation > in_game_maximum_height:
|
117
|
-
suggested_height_scale_multiplier = (
|
118
|
-
original_deviation / in_game_maximum_height # type: ignore
|
119
|
-
)
|
120
|
-
suggested_height_scale_value = int(255 * suggested_height_scale_multiplier)
|
121
|
-
else:
|
122
|
-
suggested_height_scale_multiplier = 1
|
123
|
-
suggested_height_scale_value = 255
|
124
|
-
|
125
|
-
self.data_info["suggested_height_scale_multiplier"] = suggested_height_scale_multiplier
|
126
|
-
self.data_info["suggested_height_scale_value"] = suggested_height_scale_value
|
127
|
-
|
128
|
-
self.map.shared_settings.height_scale_multiplier = ( # type: ignore
|
129
|
-
suggested_height_scale_multiplier
|
130
|
-
)
|
131
|
-
self.map.shared_settings.height_scale_value = suggested_height_scale_value # type: ignore
|
132
|
-
|
133
|
-
if self.user_settings.easy_mode: # type: ignore
|
134
|
-
try:
|
135
|
-
data = self.normalize_dem(data)
|
136
|
-
self.add_numpy_params(data, "normalized")
|
137
|
-
|
138
|
-
normalized_deviation = self.data_info["normalized_deviation"]
|
139
|
-
z_scaling_factor = normalized_deviation / original_deviation # type: ignore
|
140
|
-
self.data_info["z_scaling_factor"] = z_scaling_factor
|
141
|
-
|
142
|
-
self.map.shared_settings.mesh_z_scaling_factor = z_scaling_factor # type: ignore
|
143
|
-
self.map.shared_settings.change_height_scale = True # type: ignore
|
144
|
-
|
145
|
-
except Exception as e: # pylint: disable=W0718
|
146
|
-
self.logger.error(
|
147
|
-
"Failed to normalize DEM data. Error: %s. Using original data.", e
|
148
|
-
)
|
149
|
-
|
150
|
-
return data
|
151
|
-
|
152
|
-
def add_numpy_params(
|
153
|
-
self,
|
154
|
-
data: np.ndarray,
|
155
|
-
prefix: str,
|
156
|
-
) -> None:
|
157
|
-
"""Add numpy array parameters to the data_info dictionary.
|
158
|
-
|
159
|
-
Arguments:
|
160
|
-
data (np.ndarray): Numpy array of the tile.
|
161
|
-
prefix (str): Prefix for the parameters.
|
73
|
+
bool: True if the tile was downloaded successfully, False otherwise.
|
162
74
|
"""
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
75
|
+
url = self.formatted_url(**kwargs)
|
76
|
+
response = requests.get(url, stream=True, timeout=10)
|
77
|
+
if response.status_code == 200:
|
78
|
+
with open(output_path, "wb") as file:
|
79
|
+
for chunk in response.iter_content(chunk_size=1024):
|
80
|
+
file.write(chunk)
|
81
|
+
return True
|
82
|
+
return False
|
83
|
+
|
84
|
+
def get_or_download_tile(self, output_path: str, **kwargs) -> str | None:
|
85
|
+
"""Get or download a tile from the provider.
|
170
86
|
|
171
87
|
Arguments:
|
172
|
-
|
88
|
+
output_path (str): Path to save the downloaded tile.
|
173
89
|
|
174
90
|
Returns:
|
175
|
-
|
91
|
+
str: Path to the downloaded tile or None if the tile not exists and was
|
92
|
+
not downloaded.
|
176
93
|
"""
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
return
|
94
|
+
if not os.path.exists(output_path):
|
95
|
+
if not self.download_tile(output_path, **kwargs):
|
96
|
+
return None
|
97
|
+
return output_path
|
181
98
|
|
182
|
-
def
|
183
|
-
"""
|
99
|
+
def get_tile_parameters(self, *args) -> dict[str, str]:
|
100
|
+
"""Returns latitude band and tile name for SRTM tile from coordinates.
|
184
101
|
|
185
102
|
Arguments:
|
186
|
-
|
103
|
+
lat (float): Latitude.
|
104
|
+
lon (float): Longitude.
|
187
105
|
|
188
106
|
Returns:
|
189
|
-
|
107
|
+
dict: Tile parameters.
|
190
108
|
"""
|
191
|
-
|
192
|
-
minimum_height = int(data.min())
|
193
|
-
deviation = maximum_height - minimum_height
|
194
|
-
self.logger.debug(
|
195
|
-
"Maximum height: %s. Minimum height: %s. Deviation: %s.",
|
196
|
-
maximum_height,
|
197
|
-
minimum_height,
|
198
|
-
deviation,
|
199
|
-
)
|
200
|
-
self.logger.debug("Number of unique values in original DEM data: %s.", np.unique(data).size)
|
109
|
+
lat, lon = args
|
201
110
|
|
202
|
-
|
203
|
-
|
204
|
-
scaling_factor = adjusted_maximum_height / maximum_height
|
205
|
-
self.logger.debug(
|
206
|
-
"Adjusted maximum height: %s. Scaling factor: %s.",
|
207
|
-
adjusted_maximum_height,
|
208
|
-
scaling_factor,
|
209
|
-
)
|
111
|
+
tile_latitude = math.floor(lat)
|
112
|
+
tile_longitude = math.floor(lon)
|
210
113
|
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
)
|
217
|
-
data = np.power(data, power_factor).astype(np.uint16)
|
114
|
+
latitude_band = f"N{abs(tile_latitude):02d}" if lat >= 0 else f"S{abs(tile_latitude):02d}"
|
115
|
+
if lon < 0:
|
116
|
+
tile_name = f"{latitude_band}W{abs(tile_longitude):03d}"
|
117
|
+
else:
|
118
|
+
tile_name = f"{latitude_band}E{abs(tile_longitude):03d}"
|
218
119
|
|
219
|
-
normalized_data = np.round(data * scaling_factor).astype(np.uint16)
|
220
120
|
self.logger.debug(
|
221
|
-
"
|
222
|
-
normalized_data.max(),
|
223
|
-
normalized_data.min(),
|
224
|
-
np.unique(normalized_data).size,
|
121
|
+
"Detected tile name: %s for coordinates: lat %s, lon %s.", tile_name, lat, lon
|
225
122
|
)
|
226
|
-
return
|
123
|
+
return {"latitude_band": latitude_band, "tile_name": tile_name}
|
124
|
+
# endregion
|
maps4fs/generator/dtm/usgs.py
CHANGED
@@ -5,12 +5,7 @@ from datetime import datetime
|
|
5
5
|
from zipfile import ZipFile
|
6
6
|
|
7
7
|
import numpy as np
|
8
|
-
import rasterio
|
9
8
|
import requests
|
10
|
-
from rasterio.enums import Resampling
|
11
|
-
from rasterio.merge import merge
|
12
|
-
from rasterio.warp import calculate_default_transform, reproject
|
13
|
-
from rasterio.windows import from_bounds
|
14
9
|
|
15
10
|
from maps4fs.generator.dtm.dtm import DTMProvider, DTMProviderSettings
|
16
11
|
|
@@ -18,7 +13,6 @@ from maps4fs.generator.dtm.dtm import DTMProvider, DTMProviderSettings
|
|
18
13
|
class USGSProviderSettings(DTMProviderSettings):
|
19
14
|
"""Settings for the USGS provider."""
|
20
15
|
|
21
|
-
max_local_elevation: int = 255
|
22
16
|
dataset: tuple | str = (
|
23
17
|
'Digital Elevation Model (DEM) 1 meter',
|
24
18
|
'Alaska IFSAR 5 meter DEM',
|
@@ -43,17 +37,18 @@ class USGSProvider(DTMProvider):
|
|
43
37
|
_author = "[ZenJakey](https://github.com/ZenJakey)"
|
44
38
|
_contributors = "[kbrandwijk](https://github.com/kbrandwijk)"
|
45
39
|
_is_community = True
|
46
|
-
_instructions =
|
47
|
-
"ℹ️ Set the max local elevation to approx the local max elevation for your area in"
|
48
|
-
" meters. This will allow you to use heightScale 255 in GE with minimal tweaking."
|
49
|
-
" Setting this value too low can cause a flat map!"
|
50
|
-
)
|
40
|
+
_instructions = None
|
51
41
|
|
52
42
|
_url = (
|
53
43
|
"https://tnmaccess.nationalmap.gov/api/v1/products?prodFormats=GeoTIFF,IMG"
|
54
44
|
|
55
45
|
)
|
56
46
|
|
47
|
+
def download_tiles(self):
|
48
|
+
download_urls = self.get_download_urls()
|
49
|
+
all_tif_files = self.download_tif_files(download_urls)
|
50
|
+
return all_tif_files
|
51
|
+
|
57
52
|
def __init__(self, *args, **kwargs):
|
58
53
|
super().__init__(*args, **kwargs)
|
59
54
|
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
@@ -138,214 +133,3 @@ class USGSProvider(DTMProvider):
|
|
138
133
|
tif_files.append(file_path)
|
139
134
|
|
140
135
|
return tif_files
|
141
|
-
|
142
|
-
def merge_geotiff(self, input_files: list[str], output_file: str) -> None:
|
143
|
-
"""Merge multiple GeoTIFF files into a single GeoTIFF file.
|
144
|
-
|
145
|
-
Arguments:
|
146
|
-
input_files (list): List of input GeoTIFF files to merge.
|
147
|
-
output_file (str): Path to save the merged GeoTIFF file.
|
148
|
-
"""
|
149
|
-
# Open all input GeoTIFF files as datasets
|
150
|
-
self.logger.debug("Merging tiff files...")
|
151
|
-
datasets = [rasterio.open(file) for file in input_files]
|
152
|
-
|
153
|
-
# Merge datasets
|
154
|
-
mosaic, out_transform = merge(datasets, nodata=0)
|
155
|
-
|
156
|
-
# Get metadata from the first file and update it for the output
|
157
|
-
out_meta = datasets[0].meta.copy()
|
158
|
-
out_meta.update(
|
159
|
-
{
|
160
|
-
"driver": "GTiff",
|
161
|
-
"height": mosaic.shape[1],
|
162
|
-
"width": mosaic.shape[2],
|
163
|
-
"transform": out_transform,
|
164
|
-
"count": mosaic.shape[0], # Number of bands
|
165
|
-
}
|
166
|
-
)
|
167
|
-
|
168
|
-
# Write merged GeoTIFF to the output file
|
169
|
-
with rasterio.open(output_file, "w", **out_meta) as dest:
|
170
|
-
dest.write(mosaic)
|
171
|
-
|
172
|
-
self.logger.debug("GeoTIFF images merged successfully into %s", output_file)
|
173
|
-
|
174
|
-
def reproject_geotiff(self, input_tiff: str, output_tiff: str, target_crs: str) -> None:
|
175
|
-
"""Reproject a GeoTIFF file to a new coordinate reference system (CRS).
|
176
|
-
|
177
|
-
Arguments:
|
178
|
-
input_tiff (str): Path to the input GeoTIFF file.
|
179
|
-
output_tiff (str): Path to save the reprojected GeoTIFF file.
|
180
|
-
target_crs (str): Target CRS (e.g., EPSG:4326 for CRS:84).
|
181
|
-
"""
|
182
|
-
# Open the source GeoTIFF
|
183
|
-
self.logger.debug("Reprojecting GeoTIFF to %s CRS...", target_crs)
|
184
|
-
with rasterio.open(input_tiff) as src:
|
185
|
-
# Get the transform, width, and height of the target CRS
|
186
|
-
transform, width, height = calculate_default_transform(
|
187
|
-
src.crs, target_crs, src.width, src.height, *src.bounds
|
188
|
-
)
|
189
|
-
|
190
|
-
# Update the metadata for the target GeoTIFF
|
191
|
-
kwargs = src.meta.copy()
|
192
|
-
kwargs.update(
|
193
|
-
{"crs": target_crs, "transform": transform, "width": width, "height": height}
|
194
|
-
)
|
195
|
-
|
196
|
-
# Open the destination GeoTIFF file and reproject
|
197
|
-
with rasterio.open(output_tiff, "w", **kwargs) as dst:
|
198
|
-
for i in range(1, src.count + 1): # Iterate over all raster bands
|
199
|
-
reproject(
|
200
|
-
source=rasterio.band(src, i),
|
201
|
-
destination=rasterio.band(dst, i),
|
202
|
-
src_transform=src.transform,
|
203
|
-
src_crs=src.crs,
|
204
|
-
dst_transform=transform,
|
205
|
-
dst_crs=target_crs,
|
206
|
-
resampling=Resampling.nearest, # Choose resampling method
|
207
|
-
)
|
208
|
-
self.logger.debug("Reprojected GeoTIFF saved to %s", output_tiff)
|
209
|
-
|
210
|
-
def extract_roi(self, input_tiff: str) -> np.ndarray: # pylint: disable=W0237
|
211
|
-
"""
|
212
|
-
Crop a GeoTIFF based on given geographic bounding box and save to a new file.
|
213
|
-
|
214
|
-
Arguments:
|
215
|
-
input_tiff (str): Path to the input GeoTIFF file.
|
216
|
-
|
217
|
-
Returns:
|
218
|
-
np.ndarray: Numpy array of the cropped GeoTIFF.
|
219
|
-
"""
|
220
|
-
self.logger.debug("Extracting ROI...")
|
221
|
-
# Open the input GeoTIFF
|
222
|
-
with rasterio.open(input_tiff) as src:
|
223
|
-
|
224
|
-
# Create a rasterio window from the bounding box
|
225
|
-
(north, south, east, west) = self.get_bbox()
|
226
|
-
window = from_bounds(west, south, east, north, transform=src.transform)
|
227
|
-
|
228
|
-
data = src.read(1, window=window)
|
229
|
-
self.logger.debug("Extracted ROI")
|
230
|
-
return data
|
231
|
-
|
232
|
-
# pylint: disable=R0914, R0917, R0913
|
233
|
-
def convert_geotiff_to_geotiff(
|
234
|
-
self,
|
235
|
-
input_tiff: str,
|
236
|
-
output_tiff: str,
|
237
|
-
min_height: float,
|
238
|
-
max_height: float,
|
239
|
-
target_crs: str,
|
240
|
-
) -> None:
|
241
|
-
"""
|
242
|
-
Convert a GeoTIFF to a scaled GeoTIFF with UInt16 values using a specific coordinate
|
243
|
-
system and output size.
|
244
|
-
|
245
|
-
Arguments:
|
246
|
-
input_tiff (str): Path to the input GeoTIFF file.
|
247
|
-
output_tiff (str): Path to save the output GeoTIFF file.
|
248
|
-
min_height (float): Minimum terrain height (input range).
|
249
|
-
max_height (float): Maximum terrain height (input range).
|
250
|
-
target_crs (str): Target CRS (e.g., EPSG:4326 for CRS:84).
|
251
|
-
"""
|
252
|
-
# Open the input GeoTIFF file
|
253
|
-
self.logger.debug("Converting to uint16")
|
254
|
-
with rasterio.open(input_tiff) as src:
|
255
|
-
# Ensure the input CRS matches the target CRS (reprojection may be required)
|
256
|
-
if str(src.crs) != str(target_crs):
|
257
|
-
raise ValueError(
|
258
|
-
f"The GeoTIFF CRS is {src.crs}, but the target CRS is {target_crs}. "
|
259
|
-
"Reprojection may be required."
|
260
|
-
)
|
261
|
-
|
262
|
-
# Read the data from the first band
|
263
|
-
data = src.read(1) # Assuming the input GeoTIFF has only a single band
|
264
|
-
|
265
|
-
# Identify the input file's NoData value
|
266
|
-
input_nodata = src.nodata
|
267
|
-
if input_nodata is None:
|
268
|
-
input_nodata = -999999.0 # Default fallback if no NoData value is defined
|
269
|
-
nodata_value = 0
|
270
|
-
# Replace NoData values (e.g., -999999.0) with the new NoData value
|
271
|
-
# (e.g., 65535 for UInt16)
|
272
|
-
data[data == input_nodata] = nodata_value
|
273
|
-
|
274
|
-
# Scale the data to the 0–65535 range (UInt16), avoiding NoData areas
|
275
|
-
scaled_data = np.clip(
|
276
|
-
(data - min_height) * (65535 / (max_height - min_height)), 0, 65535
|
277
|
-
).astype(np.uint16)
|
278
|
-
scaled_data[data == nodata_value] = (
|
279
|
-
nodata_value # Preserve NoData value in the scaled array
|
280
|
-
)
|
281
|
-
|
282
|
-
# Compute the proper transform to ensure consistency
|
283
|
-
# Get the original transform, width, and height
|
284
|
-
transform = src.transform
|
285
|
-
width = src.width
|
286
|
-
height = src.height
|
287
|
-
left, bottom, right, top = src.bounds
|
288
|
-
|
289
|
-
# Adjust the transform matrix to make sure bounds and transform align correctly
|
290
|
-
transform = rasterio.transform.from_bounds(left, bottom, right, top, width, height)
|
291
|
-
|
292
|
-
# Prepare metadata for the output GeoTIFF
|
293
|
-
metadata = src.meta.copy()
|
294
|
-
metadata.update(
|
295
|
-
{
|
296
|
-
"dtype": rasterio.uint16, # Update dtype for uint16
|
297
|
-
"crs": target_crs, # Update CRS if needed
|
298
|
-
"nodata": nodata_value, # Set the new NoData value
|
299
|
-
"transform": transform, # Use the updated, consistent transform
|
300
|
-
}
|
301
|
-
)
|
302
|
-
|
303
|
-
# Write the scaled data to the output GeoTIFF
|
304
|
-
with rasterio.open(output_tiff, "w", **metadata) as dst:
|
305
|
-
dst.write(scaled_data, 1) # Write the first band
|
306
|
-
|
307
|
-
self.logger.debug(
|
308
|
-
"GeoTIFF successfully converted and saved to %s, with nodata value: %s.",
|
309
|
-
output_tiff,
|
310
|
-
nodata_value,
|
311
|
-
)
|
312
|
-
|
313
|
-
def generate_data(self) -> np.ndarray:
|
314
|
-
"""Generate data from the USGS 1m provider.
|
315
|
-
|
316
|
-
Returns:
|
317
|
-
np.ndarray: Numpy array of the data.
|
318
|
-
"""
|
319
|
-
download_urls = self.get_download_urls()
|
320
|
-
all_tif_files = self.download_tif_files(download_urls)
|
321
|
-
self.merge_geotiff(all_tif_files, os.path.join(self.output_path, "merged.tif"))
|
322
|
-
self.reproject_geotiff(
|
323
|
-
os.path.join(self.output_path, "merged.tif"),
|
324
|
-
os.path.join(self.output_path, "reprojected.tif"),
|
325
|
-
"EPSG:4326",
|
326
|
-
)
|
327
|
-
self.convert_geotiff_to_geotiff(
|
328
|
-
os.path.join(self.output_path, "reprojected.tif"),
|
329
|
-
os.path.join(self.output_path, "translated.tif"),
|
330
|
-
min_height=0,
|
331
|
-
max_height=self.user_settings.max_local_elevation, # type: ignore
|
332
|
-
target_crs="EPSG:4326",
|
333
|
-
)
|
334
|
-
return self.extract_roi(os.path.join(self.output_path, "translated.tif"))
|
335
|
-
|
336
|
-
def get_numpy(self) -> np.ndarray:
|
337
|
-
"""Get numpy array of the tile.
|
338
|
-
|
339
|
-
Returns:
|
340
|
-
np.ndarray: Numpy array of the tile.
|
341
|
-
"""
|
342
|
-
if not self.user_settings:
|
343
|
-
raise ValueError("user_settings is 'none'")
|
344
|
-
if self.user_settings.max_local_elevation <= 0: # type: ignore
|
345
|
-
raise ValueError(
|
346
|
-
"Entered 'max_local_elevation' value is unable to be used. "
|
347
|
-
"Use a value greater than 0."
|
348
|
-
)
|
349
|
-
if not self._data:
|
350
|
-
self._data = self.generate_data()
|
351
|
-
return self._data
|
maps4fs/generator/settings.py
CHANGED