windmapper 2.2.4__tar.gz → 2.2.5__tar.gz

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.
Files changed (23) hide show
  1. {windmapper-2.2.4 → windmapper-2.2.5}/PKG-INFO +1 -1
  2. {windmapper-2.2.4 → windmapper-2.2.5}/pyproject.toml +1 -1
  3. windmapper-2.2.5/src/windmapper/dem.py +115 -0
  4. {windmapper-2.2.4 → windmapper-2.2.5}/src/windmapper/windmapper_cli.py +47 -174
  5. windmapper-2.2.5/tests/test_dem.py +106 -0
  6. {windmapper-2.2.4 → windmapper-2.2.5}/.github/workflows/main.yml +0 -0
  7. {windmapper-2.2.4 → windmapper-2.2.5}/.gitignore +0 -0
  8. {windmapper-2.2.4 → windmapper-2.2.5}/.readthedocs.yml +0 -0
  9. {windmapper-2.2.4 → windmapper-2.2.5}/CMakeLists.txt +0 -0
  10. {windmapper-2.2.4 → windmapper-2.2.5}/LICENSE +0 -0
  11. {windmapper-2.2.4 → windmapper-2.2.5}/MANIFEST.in +0 -0
  12. {windmapper-2.2.4 → windmapper-2.2.5}/README.md +0 -0
  13. {windmapper-2.2.4 → windmapper-2.2.5}/build.rst +0 -0
  14. {windmapper-2.2.4 → windmapper-2.2.5}/cfg/cli_massSolver.cfg +0 -0
  15. {windmapper-2.2.4 → windmapper-2.2.5}/implementation/implimentation.ipynb +0 -0
  16. {windmapper-2.2.4 → windmapper-2.2.5}/implementation/spack.yaml +0 -0
  17. {windmapper-2.2.4 → windmapper-2.2.5}/implementation/windmapper_config.py +0 -0
  18. {windmapper-2.2.4 → windmapper-2.2.5}/publish.sh +0 -0
  19. {windmapper-2.2.4 → windmapper-2.2.5}/spack-deploy.yaml +0 -0
  20. {windmapper-2.2.4 → windmapper-2.2.5}/src/windmapper/__init__.py +0 -0
  21. {windmapper-2.2.4 → windmapper-2.2.5}/src/windmapper/windmapper_utls/MPI_call_WN_1dir.py +0 -0
  22. {windmapper-2.2.4 → windmapper-2.2.5}/src/windmapper/windmapper_utls/__init__.py +0 -0
  23. {windmapper-2.2.4 → windmapper-2.2.5}/src/windmapper/windmapper_utls/rio_merge.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: windmapper
3
- Version: 2.2.4
3
+ Version: 2.2.5
4
4
  Summary: Windfield library generation
5
5
  Author: Chris Marsh
6
6
  Project-URL: Homepage, https://github.com/Chrismarsh/Windmapper
@@ -4,7 +4,7 @@ build-backend = "scikit_build_core.build"
4
4
 
5
5
  [project]
6
6
  name = "windmapper"
7
- version = "2.2.4"
7
+ version = "2.2.5"
8
8
  authors = [
9
9
  {name="Chris Marsh"}
10
10
  ]
@@ -0,0 +1,115 @@
1
+ """Prepare a rectangular, fully populated elevation grid for WindNinja."""
2
+
3
+ import math
4
+
5
+ import numpy as np
6
+ from osgeo import gdal
7
+ from pyproj import Transformer
8
+
9
+
10
+ def largest_valid_window(ds, anchor=None):
11
+ """Find the maximum-area valid pixel window, optionally containing (col, row)."""
12
+ band = ds.GetRasterBand(1)
13
+ mask = band.GetMaskBand()
14
+ nodata = band.GetNoDataValue()
15
+ if nodata is None:
16
+ nodata = -9999 # WindNinja's fallback for untagged elevation rasters.
17
+ alpha = None
18
+ for i in range(2, ds.RasterCount + 1):
19
+ other = ds.GetRasterBand(i)
20
+ if other.GetColorInterpretation() == gdal.GCI_AlphaBand:
21
+ alpha = other
22
+ break
23
+
24
+ width = ds.RasterXSize
25
+ heights = np.zeros(width + 1, dtype=np.int32) # trailing zero closes the last run
26
+ best = None
27
+ best_area = 0
28
+ for row in range(ds.RasterYSize):
29
+ z = band.ReadAsArray(0, row, width, 1)[0]
30
+ valid = np.isfinite(z) & (z != nodata)
31
+ valid &= mask.ReadAsArray(0, row, width, 1)[0] != 0
32
+ if alpha is not None:
33
+ valid &= alpha.ReadAsArray(0, row, width, 1)[0] > 0
34
+ heights[:-1] = np.where(valid, heights[:-1] + 1, 0)
35
+
36
+ # Consecutive valid heights form a histogram. A falling edge closes
37
+ # every taller rectangle on the stack at this column (exclusive).
38
+ stack = []
39
+ for right in np.flatnonzero(np.diff(heights, prepend=0)).tolist():
40
+ height = int(heights[right])
41
+ left = right
42
+ while stack and stack[-1][1] > height:
43
+ left, h = stack.pop()
44
+ top = row + 1 - h
45
+ area = (right - left) * h
46
+ if area > best_area and (
47
+ anchor is None or (left <= anchor[0] < right and top <= anchor[1] <= row)
48
+ ):
49
+ best = (left, top, right - left, h)
50
+ best_area = area
51
+ if height and (not stack or stack[-1][1] < height):
52
+ stack.append((left, height))
53
+
54
+ if best is None:
55
+ if anchor is not None:
56
+ raise ValueError(f"DEM anchor at pixel {anchor} has no valid elevation")
57
+ raise ValueError("The DEM contains no valid elevations")
58
+ return best
59
+
60
+
61
+ def prepare_existing_dem(filename, output, skip_mercator_proj=False, anchor_lonlat=None):
62
+ """Select the rectangle on the projected grid from which all tiles are copied."""
63
+ src = gdal.Open(filename)
64
+ srs = src.GetSpatialRef()
65
+ if srs is None:
66
+ raise ValueError(f"DEM has no coordinate system: {filename}")
67
+
68
+ gt = src.GetGeoTransform()
69
+ if skip_mercator_proj:
70
+ if not srs.IsProjected() or not math.isclose(srs.GetLinearUnits(), 1.0):
71
+ raise ValueError("skip_mercator_proj requires a projected DEM in metres")
72
+ target = src.GetProjection()
73
+ else:
74
+ x, y = gdal.ApplyGeoTransform(gt, src.RasterXSize / 2, src.RasterYSize / 2)
75
+ _, lat = Transformer.from_crs(src.GetProjection(), 'EPSG:4326', always_xy=True).transform(x, y)
76
+ target = f"+proj=merc +lat_ts={lat} +datum=WGS84 +units=m"
77
+
78
+ grid = src
79
+ north_up_square = gt[1] > 0 and gt[2] == gt[4] == 0 and math.isclose(gt[1], -gt[5], rel_tol=1e-12)
80
+ if not skip_mercator_proj or not north_up_square:
81
+ # GDAL can suggest non-square cells when the input already uses the
82
+ # chosen CRS. Keep the suggested pixel area while enforcing dx == dy.
83
+ suggested = gdal.AutoCreateWarpedVRT(src, None, target).GetGeoTransform()
84
+ resolution = math.sqrt(abs(suggested[1] * suggested[5] - suggested[2] * suggested[4]))
85
+ nodata = src.GetRasterBand(1).GetNoDataValue()
86
+ grid = gdal.Warp(
87
+ '', src, format='VRT', dstSRS=target, resampleAlg='bilinear',
88
+ xRes=resolution, yRes=resolution, outputType=gdal.GDT_Float32,
89
+ srcNodata=nodata if nodata is not None else -9999, dstNodata=-9999,
90
+ )
91
+
92
+ anchor = None
93
+ if anchor_lonlat is not None:
94
+ transform = Transformer.from_crs('EPSG:4326', grid.GetProjection(), always_xy=True)
95
+ x, y = transform.transform(*anchor_lonlat, errcheck=True)
96
+ col, row = gdal.ApplyGeoTransform(gdal.InvGeoTransform(grid.GetGeoTransform()), x, y)
97
+ anchor = (math.floor(col), math.floor(row))
98
+ if not (0 <= anchor[0] < grid.RasterXSize and 0 <= anchor[1] < grid.RasterYSize):
99
+ raise ValueError(f"DEM anchor {anchor_lonlat} lies outside the projected grid")
100
+
101
+ window = largest_valid_window(grid, anchor)
102
+ x, y, width, height = window
103
+ print(f"Selected valid DEM window at ({x}, {y}): {width} x {height} "
104
+ f"of {grid.RasterXSize} x {grid.RasterYSize} pixels")
105
+ return gdal.Translate(output, grid, format='GTiff', srcWin=window, bandList=[1],
106
+ maskBand='none', creationOptions=['TILED=YES', 'BIGTIFF=IF_SAFER'])
107
+
108
+
109
+ def tile_window(width, height, i, j, count_x, count_y, overlap):
110
+ """Return an overlapping pixel window. Tile (0, 0) is the southwest tile."""
111
+ left = max(0, i * width // count_x - overlap)
112
+ right = min(width, (i + 1) * width // count_x + overlap)
113
+ top = max(0, height - (j + 1) * height // count_y - overlap)
114
+ bottom = min(height, height - j * height // count_y + overlap)
115
+ return left, top, right - left, bottom - top
@@ -17,6 +17,8 @@
17
17
  # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
18
 
19
19
 
20
+ import math
21
+
20
22
  import elevation
21
23
  import pdb, os, shutil
22
24
  import subprocess
@@ -30,6 +32,7 @@ from tqdm import tqdm
30
32
  import cloudpickle
31
33
  from mpi4py import MPI
32
34
  from . import windmapper_utls as wm
35
+ from .dem import prepare_existing_dem, tile_window
33
36
 
34
37
 
35
38
 
@@ -241,126 +244,15 @@ write_farsite_atm = false """
241
244
  name_utm = 'ref-DEM-proj'
242
245
  fic_lcc = user_output_dir + '/' + name_utm + '.tif'
243
246
 
244
- # LCC_proj = '+proj=lcc +lon_0=-90 +lat_1=33 +lat_2=45'
245
- LCC_proj = None
246
247
  if use_existing_dem:
247
-
248
- # if we are using a user-provided dem, ensure there are no NoData values that border the
249
- # DEM which will cause issues and ensure it is rectangular
250
- print('Preparing input DEM')
251
-
252
- # mask data values
253
- print('...',end='')
254
- exec_str = """gdal_calc.py -A %s --outfile %s --NoDataValue 0 --calc="1*(A>-100)" """ % (
255
- dem_filename, user_output_dir + 'out.tif')
256
- subprocess.check_call([exec_str], shell=True)
257
- print('25...', end='')
258
-
259
- # convert to shp file
260
- exec_str = """gdal_polygonize.py -8 -b 1 -f "ESRI Shapefile" %s %s/pols """ % (
261
- user_output_dir + 'out.tif', user_output_dir)
262
- subprocess.check_call([exec_str], shell=True)
263
- print('50...', end='')
264
-
265
- # #
266
- # # driver = ogr.GetDriverByName('ESRI Shapefile')
267
- # # dataSource = driver.Open('windmapper_config/pols/out.shp', 0)
268
- # # layer = dataSource.GetLayer()
269
- # # feat = layer.GetFeature(0)
270
- # # geom = feat.GetGeomFieldRef(0)
271
- # #
272
- # # (minX, maxX, minY, maxY) = geom.GetEnvelope()
273
- #
274
- # with open('%s/pols' % user_output_dir, 'r') as file:
275
- # poly = json.load(file)
276
- #
277
- # coords = poly['features'][0]['geometry']['coordinates'][0]
278
- # c = np.array(coords)
279
- # # [0] = lon, [1] = lat
280
- # bbox = [min(coords, key=lambda x: x[0]),
281
- # max(coords, key=lambda x: x[0]),
282
- # min(coords, key=lambda x: x[1]),
283
- # max(coords, key=lambda x: x[1])]
284
- # bbox = np.array(bbox)
285
- #
286
- # # pick the middle
287
- # lons = np.sort(bbox[:, 0])[1:3]
288
- # lats = np.sort(bbox[:, 1])[1:3]
289
- #
290
- # lat_min = X.lat_min = min(lats)
291
- # lat_max = X.lat_max = max(lats)
292
- # lon_min = X.lon_min = min(lons)
293
- # lon_max = X.lon_max = max(lons)
294
- #
295
- # srs_out = osr.SpatialReference()
296
- # srs_out.ImportFromEPSG(4326)
297
- #
298
- # pts_to_shp([[lat_max, lon_min],
299
- # [lat_max, lon_max],
300
- # [lat_min, lon_max],
301
- # [lat_min, lon_min]],
302
- # os.path.join(user_output_dir, 'shp', 'test.shp'),
303
- # srs_out.ExportToProj4(),
304
- # )
305
- #
306
- #
307
-
308
-
309
- # clip original with the shpfile to get the no data only zone
310
- exec_str = """%sgdalwarp -of GTiff -cutline %s/pols/out.shp -crop_to_cutline -dstalpha %s %s """ % (gdal_prefix,
311
- user_output_dir, dem_filename, fic_lcc)
312
- subprocess.check_call([exec_str], shell=True)
313
- print('75...', end='')
314
- shutil.rmtree("%s/pols" % user_output_dir)
315
- os.remove("%s/out.tif" % user_output_dir)
316
- print('100', end='')
317
- print(' - done')
318
-
319
- # Get the bounding box so we can write out shp file later
320
- # also serves to test that we have a geo referenced input
321
- try:
322
- info = gdal.Info(fic_lcc, format='json')
323
- lon = [info['wgs84Extent']['coordinates'][0][x][0] for x in range(0,4)]
324
- lat = [info['wgs84Extent']['coordinates'][0][x][1] for x in range(0,4)]
325
-
326
- lat_min = X.lat_min = min(lat)
327
- lat_max = X.lat_max = max(lat)
328
- lon_min = X.lon_min = min(lon)
329
- lon_max = X.lon_max = max(lon)
330
-
331
- fac = 0.1 # Expansion factor to make sure that the downloaded SRTM tile is large enough
332
-
333
- # Properties of the bounding box
334
- delta_lat = lat_max - lat_min
335
- delta_lon = lon_max - lon_min
336
-
337
- # This is a small extent than what we have so we ensure perfect coverage
338
- lon_min = lon_min + delta_lon * fac
339
- lat_min = lat_min + delta_lat * fac
340
- lon_max = lon_max - delta_lon * fac
341
- lat_max = lat_max - delta_lat * fac
342
-
343
- except:
344
- print('There is no coordinate defined for this input tif.')
345
- exit(-1)
346
-
347
- # we may wish to skip this for specific inputs
348
- # a tight square UTM domain will be slightly offset in merc causing WN issues
349
- if not skip_mercator_proj:
350
- #ensure we have a rectangular domain
351
- LCC_proj = '+proj=merc +lat_ts=%.30f' % ((lat_min + lat_max) / 2.0)
352
-
353
- # convert this to our custom mercator projection
354
- exec_str = '%sgdalwarp %s %s -overwrite -dstnodata -9999 -t_srs "%s" -r bilinear '
355
- com_string = exec_str % (gdal_prefix, fic_lcc, fic_lcc+'.tmp.tif', LCC_proj)
356
- subprocess.check_call([com_string], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
357
-
358
- os.remove("%s" % fic_lcc)
359
- #ensure we have a float32 dataset
360
- exec_str = '%sgdal_translate -ot Float32 %s %s' % (gdal_prefix, fic_lcc+'.tmp.tif', fic_lcc)
361
- subprocess.check_call([exec_str], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
362
- os.remove(fic_lcc+'.tmp.tif')
363
-
248
+ ds = prepare_existing_dem(dem_filename, fic_lcc, skip_mercator_proj,
249
+ getattr(X, 'dem_anchor_lonlat', None))
250
+ gt = ds.GetGeoTransform()
251
+ xmin, ymax = gt[0], gt[3]
252
+ xmax = xmin + ds.RasterXSize * gt[1]
253
+ ymin = ymax + ds.RasterYSize * gt[5]
254
+ bbox = [[ymax, xmin], [ymax, xmax], [ymin, xmax], [ymin, xmin]]
255
+ bbox_srs = ds.GetProjection()
364
256
  else:
365
257
 
366
258
  # need to ensure that we request a square domain in LCC projection
@@ -424,47 +316,20 @@ write_farsite_atm = false """
424
316
 
425
317
  os.remove(fic_lcc+'.tmp.tif')
426
318
 
427
-
428
- srs_out = osr.SpatialReference()
429
- srs_out.ImportFromEPSG(4326)
430
-
431
- pts_to_shp([[lat_max, lon_min],
432
- [lat_max, lon_max],
433
- [lat_min, lon_max],
434
- [lat_min, lon_min]],
435
- os.path.join(user_output_dir,'shp','user_bbox.shp'),
436
- srs_out.ExportToProj4(),
437
- )
438
-
439
- ds = gdal.Open(fic_lcc)
440
- wkt = ds.GetProjection()
441
- srs = osr.SpatialReference()
442
- srs.ImportFromWkt(wkt)
443
-
444
- if LCC_proj is None:
445
- LCC_proj = srs.ExportToProj4()
446
-
447
- band = ds.GetRasterBand(1)
448
- gt = ds.GetGeoTransform()
449
-
450
- t_4326_to_merc = Transformer.from_crs("epsg:4326", LCC_proj, always_xy=True)
451
- x_merc, y_merc = t_4326_to_merc.transform(
452
- [lon_min, lon_max, lon_min, lon_max],
453
- [lat_min, lat_min, lat_max, lat_max],
454
- )
455
-
456
- xmin = min(x_merc)
457
- xmax = max(x_merc)
458
- ymin = min(y_merc)
459
- ymax = max(y_merc)
460
-
461
-
462
- is_geographic = srs.IsGeographic()
463
- if is_geographic:
464
- raise Exception('Input DEM must be projected ')
465
-
466
- pixel_width = gt[1]
467
- pixel_height = -gt[5]
319
+ ds = gdal.Open(fic_lcc)
320
+ gt = ds.GetGeoTransform()
321
+ bbox = [[lat_max, lon_min], [lat_max, lon_max],
322
+ [lat_min, lon_max], [lat_min, lon_min]]
323
+ bbox_srs = 'EPSG:4326'
324
+ t_4326_to_merc = Transformer.from_crs(bbox_srs, LCC_proj, always_xy=True)
325
+ x_merc, y_merc = t_4326_to_merc.transform(
326
+ [point[1] for point in bbox], [point[0] for point in bbox])
327
+ xmin, xmax = min(x_merc), max(x_merc)
328
+ ymin, ymax = min(y_merc), max(y_merc)
329
+
330
+ cutline = os.path.join(user_output_dir, 'shp', 'user_bbox.shp')
331
+ pts_to_shp(bbox, cutline, bbox_srs)
332
+ pixel_width, pixel_height = gt[1], -gt[5]
468
333
 
469
334
  lenx = xmax-xmin
470
335
  leny = ymax-ymin
@@ -472,8 +337,13 @@ write_farsite_atm = false """
472
337
  len_wn = res_wind * nres
473
338
 
474
339
  # Number of Wind Ninja tiles
475
- nopt_x = int(lenx // len_wn + 1)
476
- nopt_y = int(leny // len_wn + 1)
340
+ if use_existing_dem:
341
+ # Balanced integer pixel windows, including exact multiples of the target size.
342
+ nopt_x = min(ds.RasterXSize, max(1, math.ceil(ds.RasterXSize * pixel_width / len_wn)))
343
+ nopt_y = min(ds.RasterYSize, max(1, math.ceil(ds.RasterYSize * pixel_height / len_wn)))
344
+ else:
345
+ nopt_x = int(lenx // len_wn + 1)
346
+ nopt_y = int(leny // len_wn + 1)
477
347
 
478
348
  nx = lenx/pixel_width / nopt_x
479
349
  ny = leny/pixel_height / nopt_y
@@ -482,7 +352,17 @@ write_farsite_atm = false """
482
352
  # DEM is small enough for WN
483
353
  name_tmp = 'tmp_0_0'
484
354
  fic_tmp = user_output_dir + name_tmp + ".tif"
485
- shutil.copy(fic_lcc, fic_tmp)
355
+ gdal.Translate(fic_tmp, ds)
356
+
357
+ elif use_existing_dem:
358
+ for i, j in itertools.product(range(nopt_x), range(nopt_y)):
359
+ window = tile_window(ds.RasterXSize, ds.RasterYSize, i, j, nopt_x, nopt_y, nadd)
360
+ gdal.Translate(os.path.join(user_output_dir, f'tmp_{i}_{j}.tif'), ds, srcWin=window)
361
+ x, y, w, h = window
362
+ west, north = gdal.ApplyGeoTransform(gt, x, y)
363
+ east, south = gdal.ApplyGeoTransform(gt, x + w, y + h)
364
+ pts_to_shp([[north, west], [north, east], [south, east], [south, west]],
365
+ os.path.join(user_output_dir, 'shp', f'utm_{i}_{j}.shp'), bbox_srs)
486
366
 
487
367
  else:
488
368
 
@@ -625,16 +505,9 @@ write_farsite_atm = false """
625
505
  subprocess.check_call([cmd], stdout=subprocess.PIPE,
626
506
  shell=True)
627
507
 
628
- srs_out = osr.SpatialReference()
629
- srs_out.ImportFromEPSG(4326)
630
- exec_str = '%sgdalwarp -overwrite -te %f %f %f %f -r "cubicspline" -et 0 -cutline %s -crop_to_cutline -dstnodata -9999 -t_srs "%s" %s %s'
631
- com_string = exec_str % (gdal_prefix,
632
- X.lon_min, X.lat_min, X.lon_max, X.lat_max,
633
- os.path.join(user_output_dir,'shp', 'user_bbox.shp'),
634
- srs_out.ExportToProj4(),
635
- name_tif+'.tmp.tif',
636
- name_tif+'.tif')
637
- subprocess.check_call([com_string], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
508
+ gdal.Warp(name_tif + '.tif', name_tif + '.tmp.tif',
509
+ dstSRS='EPSG:4326', cutlineDSName=cutline, cropToCutline=True,
510
+ resampleAlg='cubicspline', errorThreshold=0, dstNodata=-9999)
638
511
  os.remove(name_tif+'.tmp.tif')
639
512
 
640
513
  pbar.update(1)
@@ -662,7 +535,7 @@ def pts_to_shp(points, fname, proj):
662
535
  shp_file = driver.CreateDataSource(fname)
663
536
 
664
537
  srs_out = osr.SpatialReference()
665
- srs_out.ImportFromProj4(proj)
538
+ srs_out.SetFromUserInput(proj)
666
539
 
667
540
  layer = shp_file.CreateLayer('mesh', srs_out, ogr.wkbPolygon)
668
541
  layer.CreateField(ogr.FieldDefn('Extent', ogr.OFTReal))
@@ -0,0 +1,106 @@
1
+ import itertools
2
+ import tempfile
3
+ import unittest
4
+ from pathlib import Path
5
+
6
+ import numpy as np
7
+ from osgeo import gdal, osr
8
+ from pyproj import Transformer
9
+
10
+ from windmapper.dem import largest_valid_window, prepare_existing_dem, tile_window
11
+
12
+
13
+ class TestDemWindows(unittest.TestCase):
14
+ def test_largest_rectangle_against_exhaustive_search(self):
15
+ ds = gdal.GetDriverByName('MEM').Create('', 3, 3, 1, gdal.GDT_Float32)
16
+ band = ds.GetRasterBand(1)
17
+ band.SetNoDataValue(-9999)
18
+ intervals = list(itertools.combinations(range(4), 2))
19
+ for bits in itertools.product([False, True], repeat=9):
20
+ valid = np.array(bits).reshape(3, 3)
21
+ band.WriteArray(np.where(valid, -125, -9999))
22
+ for anchor in (None, (1, 1)):
23
+ candidates = []
24
+ for (left, right), (top, bottom) in itertools.product(intervals, repeat=2):
25
+ if anchor and not (left <= anchor[0] < right and top <= anchor[1] < bottom):
26
+ continue
27
+ if valid[top:bottom, left:right].all():
28
+ candidates.append((left, top, right - left, bottom - top))
29
+ if candidates:
30
+ window = largest_valid_window(ds, anchor)
31
+ self.assertIn(window, candidates)
32
+ self.assertEqual(window[2] * window[3], max(w * h for _, _, w, h in candidates))
33
+ else:
34
+ with self.assertRaises(ValueError):
35
+ largest_valid_window(ds, anchor)
36
+
37
+ def test_masks_and_nonfinite_values(self):
38
+ ds = gdal.GetDriverByName('MEM').Create('', 7, 1, 2, gdal.GDT_Float32)
39
+ band = ds.GetRasterBand(1)
40
+ band.WriteArray(np.array([[17, -120, 0, np.nan, -9999, np.inf, 3]]))
41
+ band.CreateMaskBand(gdal.GMF_PER_DATASET)
42
+ band.GetMaskBand().WriteArray(np.array([[0, 255, 255, 255, 255, 255, 255]], dtype='uint8'))
43
+ alpha = ds.GetRasterBand(2)
44
+ alpha.SetColorInterpretation(gdal.GCI_AlphaBand)
45
+ alpha.WriteArray(np.array([[255, 255, 255, 255, 255, 255, 0]]))
46
+ self.assertEqual(largest_valid_window(ds), (1, 0, 2, 1))
47
+ for x in (0, 3, 4, 5, 6):
48
+ with self.assertRaises(ValueError):
49
+ largest_valid_window(ds, (x, 0))
50
+
51
+ def test_project_crop_and_anchor(self):
52
+ with tempfile.TemporaryDirectory() as tmp:
53
+ src = str(Path(tmp) / 'dem.tif')
54
+ ds = gdal.GetDriverByName('GTiff').Create(src, 16, 12, 1, gdal.GDT_Float32)
55
+ srs = osr.SpatialReference()
56
+ srs.ImportFromEPSG(32608)
57
+ ds.SetProjection(srs.ExportToWkt())
58
+ ds.SetGeoTransform((450000, 30, 0, 7600000, 0, -30))
59
+ band = ds.GetRasterBand(1)
60
+ band.SetNoDataValue(-9999)
61
+ z = np.full((12, 16), -120, dtype=np.float32)
62
+ z[:8, 8:] = -9999
63
+ band.WriteArray(z)
64
+ ds = None
65
+
66
+ to_geo = Transformer.from_crs('EPSG:32608', 'EPSG:4326', always_xy=True)
67
+ anchor = to_geo.transform(450000 + 14.5 * 30, 7600000 - 10.5 * 30)
68
+ out = str(Path(tmp) / 'cropped.tif')
69
+ for native in (True, False):
70
+ cropped = prepare_existing_dem(src, out, native, anchor)
71
+ self.assertTrue((cropped.ReadAsArray() == -120).all())
72
+ if native:
73
+ self.assertEqual((cropped.RasterXSize, cropped.RasterYSize), (16, 4))
74
+ self.assertEqual(cropped.GetGeoTransform(), (450000, 30, 0, 7599760, 0, -30))
75
+ cropped = None
76
+
77
+ with self.assertRaisesRegex(ValueError, 'outside'):
78
+ prepare_existing_dem(src, out, True, (-110, 50))
79
+
80
+ def test_rectify_non_square_cells_in_simulation_crs(self):
81
+ with tempfile.TemporaryDirectory() as tmp:
82
+ src = str(Path(tmp) / 'mercator.tif')
83
+ ds = gdal.GetDriverByName('GTiff').Create(src, 40, 20, 1, gdal.GDT_Float32)
84
+ srs = osr.SpatialReference()
85
+ srs.ImportFromProj4('+proj=merc +lat_ts=0 +datum=WGS84')
86
+ ds.SetProjection(srs.ExportToWkt())
87
+ ds.SetGeoTransform((0, 90, 0, 300, 0, -30))
88
+ ds.GetRasterBand(1).Fill(10)
89
+ ds = None
90
+ for native in (True, False):
91
+ cropped = prepare_existing_dem(src, str(Path(tmp) / 'out.tif'), native)
92
+ gt = cropped.GetGeoTransform()
93
+ self.assertEqual(gt[1], -gt[5])
94
+ self.assertEqual((gt[2], gt[4]), (0, 0))
95
+ self.assertTrue((cropped.ReadAsArray() == 10).all())
96
+ cropped = None
97
+
98
+ def test_tile_overlap_and_coverage(self):
99
+ for nx, ny in [(1, 1), (1, 3), (3, 1), (3, 2)]:
100
+ covered = np.zeros((73, 101), dtype=bool)
101
+ for i, j in itertools.product(range(nx), range(ny)):
102
+ x, y, w, h = tile_window(101, 73, i, j, nx, ny, 25)
103
+ self.assertTrue(0 <= x < x + w <= 101 and 0 <= y < y + h <= 73)
104
+ covered[y:y + h, x:x + w] = True
105
+ self.assertTrue(covered.all())
106
+ self.assertEqual(tile_window(101, 73, 0, 0, 3, 2, 25), (0, 12, 58, 61))
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes