Skip to content

Commit ec038ae

Browse files
committed
update distance tool CLI functions
1 parent f46ab6a commit ec038ae

1 file changed

Lines changed: 46 additions & 43 deletions

File tree

eis_toolkit/cli.py

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,8 +1340,6 @@ def distance_to_anomaly_cli(
13401340
13411341
Uses only the first band of the raster.
13421342
"""
1343-
# from sys import platform
1344-
13451343
from eis_toolkit.raster_processing.distance_to_anomaly import distance_to_anomaly
13461344

13471345
if second_threshold_criteria_value is not None:
@@ -1350,29 +1348,27 @@ def distance_to_anomaly_cli(
13501348
threshold_criteria_value = first_threshold_criteria_value
13511349

13521350
with ProgressLog.reading_input_files():
1353-
raster = rasterio.open(input_raster)
1354-
# # Use optimized version if Windows
1355-
# if platform == "win32":
1356-
# out_image, out_meta = distance_to_anomaly_gdal(
1357-
# anomaly_raster_profile=raster.profile,
1358-
# anomaly_raster_data=raster.read(1),
1359-
# threshold_criteria_value=threshold_criteria_value,
1360-
# threshold_criteria=get_enum_values(threshold_criteria),
1361-
# max_distance=max_distance,
1362-
# )
1363-
# else:
1364-
with ProgressLog.running_algorithm():
1365-
out_image, out_meta = distance_to_anomaly(
1366-
anomaly_raster_profile=raster.profile,
1367-
anomaly_raster_data=raster.read(1),
1351+
with rasterio.open(input_raster) as raster:
1352+
raster_array = raster.read(1)
1353+
profile = raster.profile.copy()
1354+
1355+
# Create nodata mask
1356+
mask = (raster_array == profile["nodata"]) | np.isnan(raster_array)
1357+
1358+
with ProgressLog.running_algorithm():
1359+
out_image, out_profile = distance_to_anomaly(
1360+
anomaly_raster_profile=profile,
1361+
anomaly_raster_data=raster_array,
13681362
threshold_criteria_value=threshold_criteria_value,
13691363
threshold_criteria=get_enum_values(threshold_criteria),
13701364
max_distance=max_distance,
13711365
)
1372-
raster.close()
1366+
1367+
# Apply nodata mask after processing
1368+
out_image[mask] = out_profile["nodata"]
13731369

13741370
with ProgressLog.saving_output_files(output_raster):
1375-
with rasterio.open(output_raster, "w", **out_meta) as dest:
1371+
with rasterio.open(output_raster, "w", **out_profile) as dest:
13761372
dest.write(out_image, 1)
13771373

13781374
ProgressLog.finish()
@@ -1395,8 +1391,6 @@ def proximity_to_anomaly_cli(
13951391
13961392
Uses only the first band of the raster.
13971393
"""
1398-
# from sys import platform
1399-
14001394
from eis_toolkit.raster_processing.proximity_to_anomaly import proximity_to_anomaly
14011395

14021396
if second_threshold_criteria_value is not None:
@@ -1405,31 +1399,28 @@ def proximity_to_anomaly_cli(
14051399
threshold_criteria_value = first_threshold_criteria_value
14061400

14071401
with ProgressLog.reading_input_files():
1408-
raster = rasterio.open(input_raster)
1409-
# Use optimized version if Windows
1410-
# if platform == "win32":
1411-
# out_image, out_meta = proximity_to_anomaly_gdal(
1412-
# anomaly_raster_profile=raster.profile,
1413-
# anomaly_raster_data=raster.read(1),
1414-
# threshold_criteria_value=threshold_criteria_value,
1415-
# threshold_criteria=get_enum_values(threshold_criteria),
1416-
# max_distance=max_distance,
1417-
# scaling_range=(anomaly_value, max_distance_value),
1418-
# )
1419-
# else:
1420-
with ProgressLog.running_algorithm():
1421-
out_image, out_meta = proximity_to_anomaly(
1422-
anomaly_raster_profile=raster.profile,
1423-
anomaly_raster_data=raster.read(1),
1402+
with rasterio.open(input_raster) as raster:
1403+
raster_array = raster.read(1)
1404+
profile = raster.profile.copy()
1405+
1406+
# Create nodata mask
1407+
mask = (raster_array == profile["nodata"]) | np.isnan(raster_array)
1408+
1409+
with ProgressLog.running_algorithm():
1410+
out_image, out_profile = proximity_to_anomaly(
1411+
anomaly_raster_profile=profile,
1412+
anomaly_raster_data=raster_array,
14241413
threshold_criteria_value=threshold_criteria_value,
14251414
threshold_criteria=get_enum_values(threshold_criteria),
14261415
max_distance=max_distance,
14271416
scaling_range=(anomaly_value, max_distance_value),
14281417
)
1429-
raster.close()
1418+
1419+
# Apply nodata mask
1420+
out_image[mask] = out_profile["nodata"]
14301421

14311422
with ProgressLog.saving_output_files(output_raster):
1432-
with rasterio.open(output_raster, "w", **out_meta) as dest:
1423+
with rasterio.open(output_raster, "w", **out_profile) as dest:
14331424
dest.write(out_image, 1)
14341425

14351426
ProgressLog.finish()
@@ -2225,13 +2216,19 @@ def distance_computation_cli(
22252216
profile["crs"] = geodataframe.crs
22262217
profile["driver"] = "GTiff"
22272218
profile["dtype"] = "float32"
2219+
mask = None
22282220
else:
22292221
with rasterio.open(base_raster) as raster:
22302222
profile = raster.profile.copy()
2223+
raster_array = raster.read(1)
2224+
mask = (raster_array == profile["nodata"]) | np.isnan(raster_array)
22312225

22322226
with ProgressLog.running_algorithm():
2233-
out_image = distance_computation(geodataframe=geodataframe, raster_profile=profile, max_distance=max_distance)
2234-
profile["count"] = 1
2227+
out_image, out_profile = distance_computation(geodataframe=geodataframe, raster_profile=profile, max_distance=max_distance)
2228+
2229+
# Apply nodata mask
2230+
if mask:
2231+
out_image[mask] = out_profile["nodata"]
22352232

22362233
with ProgressLog.saving_output_files(output_raster):
22372234
with rasterio.open(output_raster, "w", **profile) as dst:
@@ -2307,18 +2304,24 @@ def proximity_computation_cli(
23072304
profile["crs"] = geodataframe.crs
23082305
profile["driver"] = "GTiff"
23092306
profile["dtype"] = "float32"
2307+
mask = None
23102308
else:
23112309
with rasterio.open(base_raster) as raster:
23122310
profile = raster.profile.copy()
2311+
raster_array = raster.read(1)
2312+
mask = (raster_array == out_profile["nodata"]) | np.isnan(raster_array)
23132313

23142314
with ProgressLog.running_algorithm():
2315-
out_image = proximity_computation(
2315+
out_image, out_profile = proximity_computation(
23162316
geodataframe=geodataframe,
23172317
raster_profile=profile,
23182318
maximum_distance=max_distance,
23192319
scale_range=(geometries_value, max_distance_value),
23202320
)
2321-
profile["count"] = 1
2321+
2322+
# Apply nodata mask
2323+
if mask:
2324+
out_image[mask] = out_profile["nodata"]
23222325

23232326
with ProgressLog.saving_output_files(output_raster):
23242327
with rasterio.open(output_raster, "w", **profile) as dst:

0 commit comments

Comments
 (0)