Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
Filter by Categories
ArcGIS Pro

A guide to nodata values in rasters with python

What are nodata values in rasters

Nodata values in rasters represent missing or invalid data in a spatial grid, such as a digital elevation model (DEM), a land cover map, or a remotely sensed image. In GIS (Geographical Information Systems), nodata values help maintain the integrity of analyses and visualizations by distinguishing between areas with no information and those with legitimate values.

Want to stay ahead of the geospatial curve? Listen to our podcast!

Working with nodata values in python

This guide will provide an overview of nodata values in rasters, their implications, and how to handle them in Python using the rasterio and numpy libraries.

Install rasterio and numpy:

To get started, you need to have rasterio and numpy installed in your Python environment. You can install them using pip:

pip install rasterio numpy

Understanding nodata values:

Nodata values are often assigned a specific numeric value to indicate that data is missing or invalid. Common nodata values include -9999, -999, -32768, or -3.4e38, depending on the dataset and software used. It is essential to know the nodata value for your raster dataset, as it will affect your analysis and interpretation of the data.

Identify nodata values in raster files:

When working with raster data, it is crucial to be able to identify nodata values. To identify nodata values in a raster using rasterio, follow these steps:

import rasterio

# Read the raster file
with"path/to/raster/file.tif") as src:
    raster_data =
    nodata_value = src.nodata

print("Nodata value:", nodata_value)

Visualize raster data with nodata values:

To visualize a raster with nodata values in Python, you can use the matplotlib library:

import matplotlib.pyplot as plt

plt.imshow(raster_data, cmap='viridis', vmin=-1, vmax=1)
plt.colorbar(label='Data Values')
plt.title('Raster with Nodata Values')

Manage nodata values in raster operations:

When performing raster operations and analyses, it’s essential to account for nodata values. Most functions in numpy will automatically handle nodata values by excluding them from calculations if you use numpy masked arrays:

import numpy as np

# Create a masked array where nodata values are masked
masked_data = == nodata_value, raster_data)

# Calculate the mean, excluding nodata values
mean_value =
print("Mean value (excluding nodata values):", mean_value)

Replace nodata values:

In some cases, you may want to replace nodata values with a specific value, either to fill gaps in the data or for better visualization. You can do this using numpy:

# Replace nodata values with a new value (e.g., 0)
new_raster_data = np.where(raster_data == nodata_value, 0, raster_data)

This will create a new numpy array with nodata values replaced with the specified new value.

Remember that when working with nodata values in rasters, it’s crucial to understand their implications, identify them, and manage them appropriately during raster operations and visualization. Proper handling of nodata values helps maintain the accuracy and reliability of your spatial analyses.

Set nodata values:

To set nodata values for a raster dataset, you’ll want to read the dataset, set the nodata value, and then save the modified dataset as a new file. Here’s a code example:

import rasterio

input_raster = "path/to/input_raster.tif"
output_raster = "path/to/output_raster.tif"
new_nodata_value = -9999

# Read the input raster dataset
with as src:
    profile = src.profile
    data =

    # Set the new nodata value in the profile

    # Replace the current nodata value with the new nodata value in the data array
    data[data == src.nodata] = new_nodata_value

    # Write the output raster with the updated nodata value
    with, "w", **profile) as dst:

In this example, replace path/to/input_raster.tif with the path to your input raster file and path/to/output_raster.tif with the path to the output raster file. Set the new_nodata_value variable to the new nodata value you want to use for your raster dataset.

After running this script, you’ll have a new raster file (output_raster.tif) with the specified nodata value.

Further reading

About the Author
I'm Daniel O'Donohue, the voice and creator behind The MapScaping Podcast ( A podcast for the geospatial community ). With a professional background as a geospatial specialist, I've spent years harnessing the power of spatial to unravel the complexities of our world, one layer at a time.