Skip to content

Instantly share code, notes, and snippets.

@drnextgis
Created July 23, 2018 05:31
Show Gist options
  • Save drnextgis/ec24f73a895d8602a2d55d2b8d56bd3c to your computer and use it in GitHub Desktop.
Save drnextgis/ec24f73a895d8602a2d55d2b8d56bd3c to your computer and use it in GitHub Desktop.
Retain internal mask
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Restore internal mask\n",
"\n",
"## Current situation\n",
"\n",
"* Generating warped mask band is not supported by GDAL: [#689](https://github.com/OSGeo/gdal/issues/689)\n",
"* Example of warping .msk sidecar file: [#754](https://github.com/OSGeo/gdal/issues/754)\n",
"* rasterio.warp.reproject supports alpha band: [#984](https://github.com/mapbox/rasterio/issues/984)\n",
"\n",
"## Potentially possible ways to retain masks\n",
"\n",
"* Create `*.msk` from original `*.tif`, reproject it and then merge them together\n",
"* Convert internal mask to alpha band, reproject `*.tif` and then translate alpha to internal mask\n",
"* Use rasterio `write_mask`, code example [here](https://github.com/drnextgis/telluric/commit/014886e348004883425671bb8cf255293c4b90e9). In this case we call `read_masks(1)` which creates numpy array internally"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from telluric import GeoRaster2\n",
"from rasterio.enums import Resampling"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" % Total % Received % Xferd Average Speed Time Time Time Current\n",
" Dload Upload Total Spent Left Speed\n",
"100 663 100 663 0 0 710 0 --:--:-- --:--:-- --:--:-- 710\n"
]
}
],
"source": [
"!curl -O https://transfer.sh/47mmP/normalized.tif"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"masked_array(\n",
" data=[[[0, 1, 2],\n",
" [3, 4, --]]],\n",
" mask=[[[False, False, False],\n",
" [False, False, True]]],\n",
" fill_value=999999,\n",
" dtype=uint8)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"src = GeoRaster2.open('normalized.tif')\n",
"src.image"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"src = GeoRaster2.open('normalized.tif')\n",
"dst = src.reproject(dimensions=(6, 4), resampling=Resampling.nearest)\n",
"dst._image # non-in-memory raster"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"masked_array(\n",
" data=[[[0, 0, 1, 1, 2, 2],\n",
" [0, 0, 1, 1, 2, 2],\n",
" [3, 3, 4, 4, --, --],\n",
" [3, 3, 4, 4, --, --]]],\n",
" mask=[[[False, False, False, False, False, False],\n",
" [False, False, False, False, False, False],\n",
" [False, False, False, False, True, True],\n",
" [False, False, False, False, True, True]]],\n",
" fill_value=999999,\n",
" dtype=uint8)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dst.image"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment