Merging rasters of different extent and resolution

115 views Asked by At

I have two raster DEMs that I'm trying to merge. The first DEM has a limited AOI with the rest of the full extent assigned -32767 as NoData value, and is 3m resolution. The second raster is 30m resolution and a much larger extent. I'm trying to drop all NoData values from the first raster and overlay it on the second raster, without resampling to have them the same resolution. This is for a hydrologic flood model that needs the larger raster dem.

This is the code I've made:

# Path to the input raster file
input_raster_path = '/Users/jonathanburton/Library/CloudStorage/OneDrive-TheUniversityofColoradoDenver/Patagonia/DEM/airbus_copy.tif'
# Path to save the cleaned raster
output_raster_path = '/Users/jonathanburton/Library/CloudStorage/OneDrive-TheUniversityofColoradoDenver/Patagonia/DEM/airbus_copy_cleaned.tif'

# Open the input raster file
with rasterio.open(input_raster_path) as src:
    # Read the raster data as a NumPy array
    raster_data = src.read(1, masked=True)

    # Define the nodata value to be removed
    nodata_value = -32767

    # Create a mask for nodata pixels
    mask = raster_data == nodata_value

    # Apply the mask to remove nodata pixels
    cleaned_raster_data = np.ma.masked_array(raster_data, mask=mask)


    # Write the cleaned raster data to a new file
    with rasterio.open(output_raster_path, 'w', driver='GTiff', width=src.width, height=src.height,
                       count=1, dtype=cleaned_raster_data.dtype, crs=src.crs, transform=src.transform) as dst:
        dst.write(cleaned_raster_data, 1)

# Path to the input raster file
input_raster_path = '/Users/jonathanburton/Library/CloudStorage/OneDrive-TheUniversityofColoradoDenver/Patagonia/DEM/airbus_copy.tif'
# Path to save the cleaned raster
output_raster_path = '/Users/jonathanburton/Library/CloudStorage/OneDrive-TheUniversityofColoradoDenver/Patagonia/DEM/airbus_copy_cleaned.tif'

# Open the input raster file
with rasterio.open(input_raster_path) as src:
    # Read the raster data as a NumPy array
    raster_data = src.read(1, masked=True)

    # Define the nodata value to be removed
    nodata_value = -32767

    # Create a mask for nodata pixels
    mask = raster_data == nodata_value

    # Apply the mask to remove nodata pixels
    cleaned_raster_data = np.ma.masked_array(raster_data, mask=mask)

    # Write the cleaned raster data to a new file
    with rasterio.open(output_raster_path, 'w', driver='GTiff', width=src.width, height=src.height,
                       count=1, dtype=cleaned_raster_data.dtype, crs=src.crs, transform=src.transform) as dst:

but this looks like its creating noData values inside the AOI instead where those are supposed to be DEM elevation values, and is not merging the two rasters together.

0

There are 0 answers