Skip to content

Commit 46eb3f2

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent f6c0be3 commit 46eb3f2

File tree

3 files changed

+52
-21
lines changed

3 files changed

+52
-21
lines changed

ci/test_lsi.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
# def set_env():
2727
# os.environ["NUMBA_DEBUG"] = "0"
2828

29+
2930
def get_ci_path() -> AnyPathType:
3031
"""
3132
Get the path to the CI folder.

lsi/lsi_core.py

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,12 @@ def check_parameters(input_dict: dir) -> None:
193193

194194
# Check if other_dem_path is needed
195195
if (dem_name == DemType.OTHER.value) and (other_dem_path is None):
196-
raise ValueError(f'{"Dem path is needed !"}')
196+
raise ValueError(f"{'Dem path is needed !'}")
197197
if (location == LocationType.GLOBAL.value) and (
198198
landcover_name is LandcoverType.CLC.value
199199
):
200200
raise ValueError(
201-
f'{"Corine Land Cover can not be used for GLOBAL calculations, only in Europe !"}'
201+
f"{'Corine Land Cover can not be used for GLOBAL calculations, only in Europe !'}"
202202
)
203203
return
204204

@@ -224,7 +224,7 @@ def lsi_core(input_dict: dict, ftep) -> None:
224224
LOGGER.info(f"*** Tool version: {__version__} ***")
225225
# --- Check parameters ---
226226
# Catch FTEP-S3 errors (S3 access instability most likely related to networking issues, timeouts, or rate limits)
227-
try:
227+
try:
228228
check_parameters(input_dict)
229229

230230
# --- Extract parameters ---
@@ -289,7 +289,9 @@ def lsi_core(input_dict: dict, ftep) -> None:
289289
# Read GADM layer and overlay with AOI
290290
aoi_gadm = aoi.copy()
291291
aoi_gadm.geometry = aoi_gadm.geometry.buffer(GADM_BUFFER)
292-
with warnings.catch_warnings(): # For cases of polygons with more than 100 parts
292+
with (
293+
warnings.catch_warnings()
294+
): # For cases of polygons with more than 100 parts
293295
warnings.simplefilter("ignore")
294296
gadm = vectors.read(gadm_path, window=aoi_gadm)
295297
gadm = gadm.to_crs(proj_crs)
@@ -393,7 +395,9 @@ def lsi_core(input_dict: dict, ftep) -> None:
393395
lsi_tif = rasters.crop(elsus_path, aoi)
394396
lsi_tif = lsi_tif.rio.write_crs(proj_crs)
395397
lsi_tif = lsi_tif.rio.reproject(
396-
proj_crs, resolution=output_resolution, resampling=Resampling.bilinear
398+
proj_crs,
399+
resolution=output_resolution,
400+
resampling=Resampling.bilinear,
397401
)
398402
lsi_tif.rasters.crop(lsi_tif, aoi)
399403
lsi_tif.name = "Value"
@@ -437,7 +441,9 @@ def lsi_core(input_dict: dict, ftep) -> None:
437441
physio_zones = physio_zones.to_crs(proj_crs)
438442
physio_zones_aoi = gpd.clip(physio_zones, aoi)
439443

440-
vectors.write(physio_zones_aoi, os.path.join(tmp_dir, "physio_zone_AOI.shp"))
444+
vectors.write(
445+
physio_zones_aoi, os.path.join(tmp_dir, "physio_zone_AOI.shp")
446+
)
441447
# Define a mapping of Zones to the Zone_Weights database file
442448
zone_to_dbf = {
443449
0: "Zone0",
@@ -503,7 +509,9 @@ def lsi_core(input_dict: dict, ftep) -> None:
503509
else: # the weights are set to 0 for Zone0
504510
# A random file is chosen (in this case Zone1). which file i is, is not important because at the end
505511
# the weights are set to 0 for Zone0
506-
landcover_w_path = str(elsus_weights_path / "Zone1/Landcover_Z1.dbf")
512+
landcover_w_path = str(
513+
elsus_weights_path / "Zone1/Landcover_Z1.dbf"
514+
)
507515

508516
# -- Compute the Rasters
509517
landcover_dir = landcover_raster_eu(
@@ -523,7 +531,8 @@ def lsi_core(input_dict: dict, ftep) -> None:
523531
# ---- SLOPE CASE ----
524532
# -- Define weights path
525533
slope_w_path = str(
526-
elsus_weights_path / str(str(db_file) + "/Slope_Z" + str(zone) + ".dbf")
534+
elsus_weights_path
535+
/ str(str(db_file) + "/Slope_Z" + str(zone) + ".dbf")
527536
)
528537
# -- Compute the Rasters
529538
slope_dir = slope_raster_eu(
@@ -561,7 +570,9 @@ def lsi_core(input_dict: dict, ftep) -> None:
561570
warnings.simplefilter("ignore")
562571
fw_dbf = gpd.read_file(global_final_weights_dbf_path)
563572
geology_weights = fw_dbf[fw_dbf.Factors == "Geology"].Weights.iloc[0]
564-
lsi_tif = slope_tif + geology_tif * float(geology_weights) + landcover_tif
573+
lsi_tif = (
574+
slope_tif + geology_tif * float(geology_weights) + landcover_tif
575+
)
565576
# lsi_tif = slope_tif + lithology_tif + landcover_tif
566577

567578
elif location == LocationType.GLOBAL.value:
@@ -602,7 +613,9 @@ def lsi_core(input_dict: dict, ftep) -> None:
602613
# Compute Geology layer
603614
with warnings.catch_warnings():
604615
warnings.simplefilter("ignore")
605-
geology_layer = geology_raster(lithology_path, dem, aoi, proj_crs, tmp_dir)
616+
geology_layer = geology_raster(
617+
lithology_path, dem, aoi, proj_crs, tmp_dir
618+
)
606619

607620
# -- 2. Slope
608621
slope_layer = slope_raster(dem_b, aoi, tmp_dir)
@@ -651,7 +664,9 @@ def lsi_core(input_dict: dict, ftep) -> None:
651664
geology_weights = fw_dbf[fw_dbf.Factors == "Geology"].Weights.iloc[0]
652665
aspect_weights = fw_dbf[fw_dbf.Factors == "Slope aspect"].Weights.iloc[0]
653666
elevation_weights = fw_dbf[fw_dbf.Factors == "Elevation"].Weights.iloc[0]
654-
hydro_weights = fw_dbf[fw_dbf.Factors == "Distance from river"].Weights.iloc[0]
667+
hydro_weights = fw_dbf[
668+
fw_dbf.Factors == "Distance from river"
669+
].Weights.iloc[0]
655670
landuse_weights = fw_dbf[fw_dbf.Factors == "Land use"].Weights.iloc[0]
656671

657672
# Final weight
@@ -660,7 +675,9 @@ def lsi_core(input_dict: dict, ftep) -> None:
660675
# Read all layers from temporal directory
661676
slope_layer = rasters.read(os.path.join(tmp_dir, "slope_weight.tif"))
662677
geology_layer = rasters.read(os.path.join(tmp_dir, "geology_weight.tif"))
663-
elevation_layer = rasters.read(os.path.join(tmp_dir, "elevation_weight.tif"))
678+
elevation_layer = rasters.read(
679+
os.path.join(tmp_dir, "elevation_weight.tif")
680+
)
664681
aspect_layer = rasters.read(os.path.join(tmp_dir, "aspect_weight.tif"))
665682
landuse_layer = rasters.read(os.path.join(tmp_dir, "landcover_weight.tif"))
666683
hydro_layer = rasters.read(os.path.join(tmp_dir, "hydro_weight.tif"))
@@ -670,12 +687,16 @@ def lsi_core(input_dict: dict, ftep) -> None:
670687
geology_layer = rasters.collocate(
671688
slope_layer, geology_layer, Resampling.bilinear
672689
)
673-
aspect_layer = rasters.collocate(slope_layer, aspect_layer, Resampling.bilinear)
690+
aspect_layer = rasters.collocate(
691+
slope_layer, aspect_layer, Resampling.bilinear
692+
)
674693
aspect_layer = rasters.crop(aspect_layer, aoi)
675694
landuse_layer = rasters.collocate(
676695
slope_layer, landuse_layer, Resampling.bilinear
677696
)
678-
hydro_layer = rasters.collocate(slope_layer, hydro_layer, Resampling.bilinear)
697+
hydro_layer = rasters.collocate(
698+
slope_layer, hydro_layer, Resampling.bilinear
699+
)
679700

680701
# Calculate lsi
681702
lsi_tif = (
@@ -732,7 +753,9 @@ def lsi_core(input_dict: dict, ftep) -> None:
732753
vectors.write(lsi_vector, os.path.join(output_path, "LandslideRisk.shp"))
733754

734755
# Write in memory
735-
rasters.write(lsi_tif_sieved, os.path.join(output_path, "LandslideRisk.tif"))
756+
rasters.write(
757+
lsi_tif_sieved, os.path.join(output_path, "LandslideRisk.tif")
758+
)
736759

737760
LOGGER.info("-- Computing LSI statistics (FER_LR_av)")
738761

@@ -742,12 +765,16 @@ def lsi_core(input_dict: dict, ftep) -> None:
742765
)
743766

744767
except RasterioIOError as e:
745-
LOGGER.error("Could not open or read the raster. Check if your data is available at your path or try relaunching RUSLE now or later")
768+
LOGGER.error(
769+
"Could not open or read the raster. Check if your data is available at your path or try relaunching RUSLE now or later"
770+
)
746771
print(str(e))
747772
sys.exit(1)
748773

749774
except DataSourceError as e:
750-
LOGGER.error("Could not open or read the vector file. Check if your data is available at your path or try relaunching RUSLE now or later")
775+
LOGGER.error(
776+
"Could not open or read the vector file. Check if your data is available at your path or try relaunching RUSLE now or later"
777+
)
751778
print(str(e))
752779
sys.exit(1)
753780

lsi/src/lsi_calculator.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ def hydro_raster_wbw(
315315

316316
# When computing in the FTEP we use pysheds for fill_depressions to avoid panicking issue
317317
# identified here: gitlab.unistra.fr/sertit/arcgis-pro/lsi/-/issues/2
318-
if ftep:
318+
if ftep:
319319
LOGGER.info("-- -- Preparing the DEM: Filling Pits")
320320
# -- Compute D8 flow directions
321321
grid = Grid.from_raster(dem_b_path)
@@ -342,7 +342,9 @@ def hydro_raster_wbw(
342342
else:
343343
LOGGER.info("-- -- Preparing the DEM: Filling Pits")
344344
# -- Fill pits
345-
filled_pits = wbe.fill_pits(wbe.read_raster(os.path.join(tmp_dir, "dem_d.tif")))
345+
filled_pits = wbe.fill_pits(
346+
wbe.read_raster(os.path.join(tmp_dir, "dem_d.tif"))
347+
)
346348

347349
LOGGER.info("-- -- Preparing the DEM: Filling Depressions")
348350
# Write and read using WbW
@@ -396,8 +398,9 @@ def hydro_raster_wbw(
396398

397399
# No value_field defined as it is already in binary
398400
flowacc_thresh_lines = rasters.rasterize(
399-
dem_b, flowacc_thresh_lines,
400-
)
401+
dem_b,
402+
flowacc_thresh_lines,
403+
)
401404

402405
rasters.write(
403406
flowacc_thresh_lines,

0 commit comments

Comments
 (0)