Skip to content

Instantly share code, notes, and snippets.

@Kirill888
Created March 5, 2020 00:00
Show Gist options
  • Save Kirill888/6757ea0a1617cc1070fe74a97a3d69a4 to your computer and use it in GitHub Desktop.
Save Kirill888/6757ea0a1617cc1070fe74a97a3d69a4 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'1.1.3'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"import rasterio\n",
"from affine import Affine\n",
"import rasterio.warp\n",
"rasterio.__version__"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [ 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [ 0, 0, 88, 88, 88, 88, 0, 0],\n",
" [ 0, 0, 88, 88, 88, 88, 0, 0],\n",
" [ 0, 0, 88, 88, 88, 88, 0, 0],\n",
" [ 0, 0, 88, 88, 88, 88, 0, 0],\n",
" [ 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [ 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int8)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"src = np.zeros((8, 8), dtype='int8')\n",
"src[2:-2,2:-2] = 88\n",
"src"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [ 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [ 0, 88, 88, 88, 88, 0, 0, 0],\n",
" [ 0, 88, 88, 88, 88, 0, 0, 0],\n",
" [ 0, 88, 88, 88, 88, 0, 0, 0],\n",
" [ 0, 88, 88, 88, 88, 0, 0, 0],\n",
" [ 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [ 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int8)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Shift image left by 1 pixel and Change `nodata` value to -1\n",
"crs = 'epsg:3857'\n",
"A = Affine.scale(10)\n",
"\n",
"dst = np.zeros_like(src)\n",
"rasterio.warp.reproject(src, dst, \n",
" src_crs=crs, dst_crs=crs,\n",
" src_transform=A, src_nodata=0,\n",
" dst_transform=A*Affine.translation(1,0), #move pixels left by 1\n",
" dst_nodata=-1)\n",
"\n",
"# Expect edges of the array to have -1, but it's 0 instead\n",
"# Any negative value is clamped to 0\n",
"dst"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[11, 11, 11, 11, 11, 11, 11, 11],\n",
" [11, 11, 11, 11, 11, 11, 11, 11],\n",
" [11, 88, 88, 88, 88, 11, 11, 11],\n",
" [11, 88, 88, 88, 88, 11, 11, 11],\n",
" [11, 88, 88, 88, 88, 11, 11, 11],\n",
" [11, 88, 88, 88, 88, 11, 11, 11],\n",
" [11, 11, 11, 11, 11, 11, 11, 11],\n",
" [11, 11, 11, 11, 11, 11, 11, 11]], dtype=int8)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dst = np.zeros_like(src)\n",
"rasterio.warp.reproject(src, dst, \n",
" src_crs=crs, dst_crs=crs,\n",
" src_transform=A, src_nodata=0,\n",
" dst_transform=A*Affine.translation(1,0), #move pixels left by 1\n",
" dst_nodata=11)\n",
"# For positive `dst_nodata` things work as expected\n",
"dst"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"-----------------------------------------------------"
]
}
],
"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.6.9"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment