Learning Objectives

  • Handle missing values

  • Setting missing values

  • Replacing values


Editing Rasters and Remotely Sensed Data#

Masking Out Certain Values#

The xarray.DataArray.where function masks data by setting nans, as demonstrated by the example below.

import geowombat as gw
from geowombat.data import l8_224078_20200518

# Zeros are replaced with nans
with gw.open(l8_224078_20200518) as src:
    data = src.where(src != 0)

Setting ‘no data’ Values#

Setting missing data values, when not available in the raster profile, can be done using the configuration manager or as an argument in the open command.

import geowombat as gw
from geowombat.data import l8_224078_20200518

# Zeros are replaced with nans
with gw.open(l8_224078_20200518, nodata=0) as src:
    print('gw.open: ',src.attrs['nodatavals'])
    #  replace 0 with nan
    src=src.gw.mask_nodata() 


# Zeros are replaced with nans
with gw.config.update(nodata=0):
  with gw.open(l8_224078_20200518) as src:
    print('gw.config',src.attrs['nodatavals'])
    #  replace 0 with nan
    src=src.gw.mask_nodata() 
gw.open:  (0, 0, 0)
gw.config (0, 0, 0)

Rescaling Values#

Most remotely sensed data is stored as int to minimize space. We are often left to rescale the values back to floating point on the backend. This can be done in a few ways in geowombat. If the sensor you are using has a geowombat profile, please use that - refer to configuration manager docs. If it is not natively supported we can manually set the scaling factor using the gw.config.update

import geowombat as gw
from geowombat.data import l8_224078_20200518
 
# Zeros are replaced with nans
with gw.config.update(scale_factor=0.0001):
  with gw.open(l8_224078_20200518) as src:
    print(src.gw.scale_factor)
0.0001

Replace values#

The GeoWombat replace function mimics pandas.DataFrame.replace.

import geowombat as gw
from geowombat.data import l8_224078_20200518

# Replace 1 with 10
with gw.open(l8_224078_20200518) as src:
    data = src.gw.replace({1: 10})

Note

The replace function is typically used with categorical data.

Updating Values#

Geowombat also accepts normal mathematical expressions such as multiplication and addition:

import geowombat as gw
from geowombat.data import l8_224078_20200518

# Replace 1 with 10
with gw.open(l8_224078_20200518) as src:
    data = src * 0.001 +80
    print(data[0].values)
[[80.    80.    80.    ... 80.    80.    80.   ]
 [80.    80.    80.    ... 80.    80.    80.   ]
 [80.    80.    80.    ... 80.    80.    80.   ]
 ...
 [87.692 87.518 87.513 ... 87.44  87.432 87.415]
 [87.586 87.59  87.61  ... 87.44  87.411 87.425]
 [87.576 87.743 87.77  ... 87.464 87.443 87.406]]