Skip to content

Instantly share code, notes, and snippets.

@Choumingzhao
Last active August 4, 2023 09:35
Show Gist options
  • Save Choumingzhao/1e486f8e86ebf41ec92f97a9748e9ce7 to your computer and use it in GitHub Desktop.
Save Choumingzhao/1e486f8e86ebf41ec92f97a9748e9ce7 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Mask a raster by another raster(with NoData) using rasterio\n",
"\n",
"An update-to-date solution to following questions:\n",
"- https://gis.stackexchange.com/questions/26893/creating-shapefile-showing-footprints-of-rasters\n",
"- https://gis.stackexchange.com/questions/61512/calculating-image-boundary-footprint-of-satellite-images-using-open-source-too\n",
"\n",
"Variables used:\n",
"- `RASTER1`: the raster to be masked\n",
"- `RASTER2`: the raster to act as mask, usually with NoData (around data)\n",
"- `MASK.geojson`: the footprint of valid data from RASTER2\n",
"- `OUT_RASTER`: the output file path\n",
"\n",
"Steps:\n",
"1. Convert `RASTER2` to vector `MASK.geojson`\n",
"2. Use vector `MASK.geojson` to mask `RASTER1`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!rio shapes RASTER2 --mask --collection > MASK.geojson\n",
"!rio mask RASTER1 OUT_RASTER --geojson-mask MASK.geojson"
]
}
],
"metadata": {
"language_info": {
"name": "python"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment