Skip to content

Commit cf43137

Browse files
committed
Fixed no data value mismanagement
1 parent 9c4dcef commit cf43137

File tree

6 files changed

+13
-14
lines changed

6 files changed

+13
-14
lines changed

main_dialog.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ def write_and_load_result(self, result_ga: GeoArray, result_fpath: str, load_in_
391391
return
392392

393393
# add required layer to the map canvas - modified after RasterCalc module
394+
394395
if load_in_TOC:
395396
newLayer = QgsRasterLayer(result_fpath, QFileInfo(result_fpath).baseName())
396397
QgsProject.instance().addMapLayer(newLayer)

pygsf/defaults/constants.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11

22

3-
GRID_NULL_VALUE = -99999.99
3+
GRID_NULL_VALUE = -99999
4+

pygsf/examples/load_band.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
print("Data type: {}".format(band_params["dataType"]))
1616
print("Unit type: {}".format(band_params["unitType"]))
1717
print("Statistics: {}".format(band_params["stats"]))
18-
print("No data value: {}".format(band_params["noData"]))
1918

2019
print("Number of overviews: {}".format(band_params["numOverviews"]))
2120
print("Number of color table entries: {}".format(band_params["numColorTableEntries"]))

pygsf/libs_utils/gdal/gdal.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33

44
from typing import Any, Tuple, Dict, Optional, Union
55

6+
from math import isfinite
7+
68
import numpy as np
79

810
import gdal
911

1012
from .exceptions import *
1113

12-
from ...defaults.constants import *
1314
from ...spatial.rasters.geotransform import GeoTransform
1415

1516

@@ -99,24 +100,19 @@ def read_band(dataset: gdal.Dataset, bnd_ndx: int=1) -> Tuple[dict, 'np.array']:
99100
if grid_values is None:
100101
raise RasterIOException("Unable to read data from rasters")
101102

102-
# transform data into numpy array
103-
104-
data = np.asarray(grid_values)
105-
106103
# if nodatavalue exists, set null values to NaN in numpy array
107104

108-
if noDataVal is not None:
109-
data = np.where(abs(data - noDataVal) > 1e-10, data, np.NaN)
105+
if noDataVal is not None and isfinite(noDataVal):
106+
grid_values = np.where(abs(grid_values - noDataVal) > 1e-10, grid_values, np.NaN)
110107

111108
band_params = dict(
112109
dataType=data_type,
113110
unitType=unit_type,
114111
stats=dStats,
115-
noData=noDataVal,
116112
numOverviews=nOverviews,
117113
numColorTableEntries=nColTableEntries)
118114

119-
return band_params, data
115+
return band_params, grid_values
120116

121117

122118
def try_read_raster_band(raster_source: str, bnd_ndx: int=1) -> Tuple[bool, Union[str, Tuple[GeoTransform, str, Dict, 'np.array']]]:

pygsf/spatial/rasters/fields.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ def magnitude(
168168
Examples:
169169
"""
170170

171-
return np.sqrt(fld_x ** 2 + fld_y ** 2)
171+
magn = np.sqrt(fld_x ** 2 + fld_y ** 2)
172+
173+
return magn
172174

173175

174176
def orients_r(

pygsf/spatial/rasters/io.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def try_write_esrigrid(geoarray: GeoArray, outgrid_flpth: str, esri_nullvalue: N
5959

6060
arr = geoarray.level(level_ndx)
6161
if arr is None:
62-
return False, "Array with index {} does not exist".format((level_ndx))
62+
return False, "Array with index {} does not exist".format(level_ndx)
6363

6464
num_rows, num_cols = arr.shape
6565
llc_x, llc_y = geoarray.level_llc(level_ndx)
@@ -71,7 +71,7 @@ def try_write_esrigrid(geoarray: GeoArray, outgrid_flpth: str, esri_nullvalue: N
7171
outputgrid.write("XLLCORNER %.8f\n" % llc_x)
7272
outputgrid.write("YLLCORNER %.8f\n" % llc_y)
7373
outputgrid.write("CELLSIZE %.8f\n" % cell_size_x)
74-
outputgrid.write("NODATA_VALUE %f\n" % esri_nullvalue)
74+
outputgrid.write("NODATA_VALUE %.8f\n" % esri_nullvalue)
7575

7676
esrigrid_outvalues = np.where(np.isnan(arr), esri_nullvalue, arr)
7777

0 commit comments

Comments
 (0)