Skip to content

Commit fbde5c2

Browse files
added overwriting nan-values across bands
1 parent 6250729 commit fbde5c2

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

detectree2/preprocessing/tiling.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import shutil
1515
import warnings # noqa: F401
1616
from pathlib import Path
17-
from typing import List, Tuple, Any
17+
from typing import Any, List, Tuple
1818

1919
import cv2
2020
import geopandas as gpd
@@ -169,6 +169,9 @@ def process_tile(img_path: str,
169169
out_sumbands).sum() > nan_threshold * totalpix:
170170
return None
171171

172+
# Apply nan mask
173+
out_img[np.broadcast_to((nan_mask == 1)[None, :, :], out_img.shape)] = nodata
174+
172175
out_meta = data.meta.copy()
173176
out_meta.update({
174177
"driver": "GTiff",
@@ -262,7 +265,12 @@ def process_tile_ms(img_path: str,
262265
else:
263266
overlapping_crowns = None
264267

265-
out_img, out_transform = mask(data, shapes=coords, crop=True)
268+
if data.nodata is not None:
269+
nodata = data.nodata
270+
else:
271+
nodata = 0
272+
273+
out_img, out_transform = mask(data, shapes=coords, nodata=nodata, crop=True)
266274

267275
out_sumbands = np.sum(out_img, axis=0)
268276
zero_mask = np.where(out_sumbands == 0, 1, 0)
@@ -277,13 +285,16 @@ def process_tile_ms(img_path: str,
277285
if sumzero > nan_threshold * totalpix or sumnan > nan_threshold * totalpix:
278286
return None
279287

288+
# Apply nan mask
289+
out_img[np.broadcast_to((nan_mask == 1)[None, :, :], out_img.shape)] = nodata
290+
280291
out_meta = data.meta.copy()
281292
out_meta.update({
282293
"driver": "GTiff",
283294
"height": out_img.shape[1],
284295
"width": out_img.shape[2],
285296
"transform": out_transform,
286-
"nodata": None,
297+
"nodata": nodata,
287298
})
288299
if dtype_bool:
289300
out_meta.update({"dtype": "uint8"})

0 commit comments

Comments
 (0)