Skip to content

Commit 5c5f87d

Browse files
upgrade_shapely_2.0_fix (#731)
* coordinates.read_raster: use `.geoms` in case of MultyPolygon. with shapely 2.0 MultiPolygon is not iterable anymore * pylint * from_raster: fix doc strings and default argument values * test_nightlight: some cleaning up
1 parent c6851cd commit 5c5f87d

File tree

5 files changed

+33
-29
lines changed

5 files changed

+33
-29
lines changed

climada/entity/exposures/base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,8 @@ def set_from_raster(self, *args, **kwargs):
465465
self.__dict__ = Exposures.from_raster(*args, **kwargs).__dict__
466466

467467
@classmethod
468-
def from_raster(cls, file_name, band=1, src_crs=None, window=False,
469-
geometry=False, dst_crs=False, transform=None,
468+
def from_raster(cls, file_name, band=1, src_crs=None, window=None,
469+
geometry=None, dst_crs=None, transform=None,
470470
width=None, height=None, resampling=Resampling.nearest):
471471
"""Read raster data and set latitude, longitude, value and meta
472472
@@ -481,8 +481,8 @@ def from_raster(cls, file_name, band=1, src_crs=None, window=False,
481481
window : rasterio.windows.Windows, optional
482482
window where data is
483483
extracted
484-
geometry : shapely.geometry, optional
485-
consider pixels only in shape
484+
geometry : list of shapely.geometry, optional
485+
consider pixels only within these shape
486486
dst_crs : crs, optional
487487
reproject to given crs
488488
transform : rasterio.Affine

climada/entity/exposures/test/test_nightlight.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,27 +123,31 @@ def test_check_nl_local_file_exists(self):
123123
is produced, the LOGGER messages logged and the ValueError raised. """
124124

125125
# check logger messages by giving a to short req_file
126-
with self.assertLogs('climada.entity.exposures.litpop.nightlight', level = 'WARNING') as cm:
126+
with self.assertLogs('climada.entity.exposures.litpop.nightlight', level='WARNING') as cm:
127127
nightlight.check_nl_local_file_exists(required_files = np.array([0, 0, 1, 1]))
128-
self.assertIn('The parameter \'required_files\' was too short and '
129-
'is ignored.', cm.output[0])
128+
self.assertIn('The parameter \'required_files\' was too short and is ignored',
129+
cm.output[0])
130130

131131
# check logger message: not all files are available
132-
with self.assertLogs('climada.entity.exposures.litpop.nightlight', level = 'DEBUG') as cm:
132+
with self.assertLogs('climada.entity.exposures.litpop.nightlight', level='DEBUG') as cm:
133133
nightlight.check_nl_local_file_exists()
134134
self.assertIn('Not all satellite files available. '
135-
f'Found 5 out of 8 required files in {Path(SYSTEM_DIR)}', cm.output[0])
135+
f'Found 5 out of 8 required files in {Path(SYSTEM_DIR)}', cm.output[0])
136136

137-
# check logger message: no files found in checkpath
138-
with self.assertLogs('climada.entity.exposures.litpop.nightlight', level = 'INFO') as cm:
137+
# check logger message: no files found in checkpath
138+
check_path = Path('climada/entity/exposures')
139+
with self.assertLogs('climada.entity.exposures.litpop.nightlight', level='INFO') as cm:
139140
# using a random path where no files are stored
140-
nightlight.check_nl_local_file_exists(check_path = Path('climada/entity/exposures'))
141-
self.assertIn('No satellite files found locally in climada/entity/exposures', cm.output[0])
141+
nightlight.check_nl_local_file_exists(check_path=check_path)
142+
self.assertIn(f'No satellite files found locally in {check_path}',
143+
cm.output[0])
142144

143145
# test raises with wrong path
146+
check_path = Path('/random/wrong/path')
144147
with self.assertRaises(ValueError) as cm:
145-
nightlight.check_nl_local_file_exists(check_path = '/random/wrong/path')
146-
self.assertEqual('The given path does not exist: /random/wrong/path', str(cm.exception))
148+
nightlight.check_nl_local_file_exists(check_path=check_path)
149+
self.assertEqual(f'The given path does not exist: {check_path}',
150+
str(cm.exception))
147151

148152
# test that files_exist is correct
149153
files_exist = nightlight.check_nl_local_file_exists()

climada/hazard/base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ def check(self):
288288

289289
@classmethod
290290
def from_raster(cls, files_intensity, files_fraction=None, attrs=None,
291-
band=None, haz_type=None, pool=None, src_crs=None, window=False,
292-
geometry=False, dst_crs=False, transform=None, width=None,
291+
band=None, haz_type=None, pool=None, src_crs=None, window=None,
292+
geometry=None, dst_crs=None, transform=None, width=None,
293293
height=None, resampling=Resampling.nearest):
294294
"""Create Hazard with intensity and fraction values from raster files
295295
@@ -320,8 +320,8 @@ def from_raster(cls, files_intensity, files_fraction=None, attrs=None,
320320
window : rasterio.windows.Windows, optional
321321
window where data is
322322
extracted
323-
geometry : shapely.geometry, optional
324-
consider pixels only in shape
323+
geometry : list of shapely.geometry, optional
324+
consider pixels only within these shapes
325325
dst_crs : crs, optional
326326
reproject to given crs
327327
transform : rasterio.Affine

climada/hazard/centroids/centr.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,8 @@ def set_raster_file(self, file_name, band=None, **kwargs):
423423
return self.values_from_raster_files([file_name], band=band, **kwargs)
424424

425425
@classmethod
426-
def from_raster_file(cls, file_name, src_crs=None, window=False,
427-
geometry=False, dst_crs=False, transform=None, width=None,
426+
def from_raster_file(cls, file_name, src_crs=None, window=None,
427+
geometry=None, dst_crs=None, transform=None, width=None,
428428
height=None, resampling=Resampling.nearest):
429429
"""Create a new Centroids object from a raster file
430430
@@ -439,8 +439,8 @@ def from_raster_file(cls, file_name, src_crs=None, window=False,
439439
source CRS. Provide it if error without it.
440440
window : rasterio.windows.Window, optional
441441
window to read
442-
geometry : shapely.geometry, optional
443-
consider pixels only in shape
442+
geometry : list of shapely.geometry, optional
443+
consider pixels only within these shapes
444444
dst_crs : crs, optional
445445
reproject to given crs
446446
transform : rasterio.Affine
@@ -462,8 +462,8 @@ def from_raster_file(cls, file_name, src_crs=None, window=False,
462462
transform, width, height, resampling)
463463
return cls(meta=meta)
464464

465-
def values_from_raster_files(self, file_names, band=None, src_crs=None, window=False,
466-
geometry=False, dst_crs=False, transform=None, width=None,
465+
def values_from_raster_files(self, file_names, band=None, src_crs=None, window=None,
466+
geometry=None, dst_crs=None, transform=None, width=None,
467467
height=None, resampling=Resampling.nearest):
468468
"""Read raster of bands and set 0 values to the masked ones.
469469
@@ -480,8 +480,8 @@ def values_from_raster_files(self, file_names, band=None, src_crs=None, window=F
480480
source CRS. Provide it if error without it.
481481
window : rasterio.windows.Window, optional
482482
window to read
483-
geometry : shapely.geometry, optional
484-
consider pixels only in shape
483+
geometry : list of shapely.geometry, optional
484+
consider pixels only within these shapes
485485
dst_crs : crs, optional
486486
reproject to given crs
487487
transform : rasterio.Affine

climada/util/coordinates.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1946,8 +1946,8 @@ def read_raster(file_name, band=None, src_crs=None, window=None, geometry=None,
19461946
band number to read. Default: 1
19471947
window : rasterio.windows.Window, optional
19481948
window to read
1949-
geometry : shapely.geometry, optional
1950-
consider pixels only in shape
1949+
geometry : list of shapely.geometry, optional
1950+
consider pixels only within these shapes
19511951
dst_crs : crs, optional
19521952
reproject to given crs
19531953
transform : rasterio.Affine

0 commit comments

Comments
 (0)