Skip to content

Instantly share code, notes, and snippets.

@Kirill888
Created May 24, 2017 00:56
Show Gist options
  • Save Kirill888/863f4f32483a9df46eec076bbdcdd556 to your computer and use it in GitHub Desktop.
Save Kirill888/863f4f32483a9df46eec076bbdcdd556 to your computer and use it in GitHub Desktop.
rasterio reproject failure example
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import rasterio\n",
"import rasterio.warp\n",
"import numpy as np\n",
"import affine"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'1.0a8'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rasterio.__version__"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def mk_test_file(dd, file_name):\n",
" (count, H, W) = dd.shape\n",
" params = dict(width=W,\n",
" height=H,\n",
" count=count,\n",
" dtype='uint8',\n",
" crs='EPSG:4326',\n",
" transform=affine.Affine(25,0,0,\n",
" 0,-25,0))\n",
" with rasterio.open(file_name, 'w', driver='HDF4Image', **params) as f_out:\n",
" f_out.write(dd)\n",
"\n",
"\n",
"dd = np.zeros((3,100,100), dtype='uint8')\n",
"dd.shape\n",
"file_name=\"test-3-bands.hdf\"\n",
"\n",
"mk_test_file(dd, file_name)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-rw-r--r-- 1 kirill staff 33612 24 May 10:53 test-3-bands.hdf\r\n"
]
}
],
"source": [
"!ls -l test-*"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"ERROR:rasterio._gdal:CPLE_IllegalArg in Temporary destination dataset for _reproject(): GDALDataset::GetRasterBand(2) - Illegal band #\n",
"\n",
"ERROR:rasterio._gdal:CPLE_IllegalArg in panDstBands[0] = 2 ... out of range for dataset.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Warping first band succeeds\n",
"OK\n",
"Warping band 2 or higher fails\n"
]
},
{
"ename": "CPLE_IllegalArgError",
"evalue": "panDstBands[0] = 2 ... out of range for dataset.",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mCPLE_IllegalArgError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-5-bc5d2d4b6ff6>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Warping band 2 or higher fails\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 14\u001b[0;31m \u001b[0mrasterio\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwarp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreproject\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrasterio\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mband\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdst\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mparams\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 15\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"This never runs\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/Users/kirill/.local/anaconda3/envs/rio/lib/python3.6/site-packages/rasterio/env.py\u001b[0m in \u001b[0;36mwrapper\u001b[0;34m(*args, **kwds)\u001b[0m\n\u001b[1;32m 268\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mwrapper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwds\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 269\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mEnv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 270\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwds\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 271\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mwrapper\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/Users/kirill/.local/anaconda3/envs/rio/lib/python3.6/site-packages/rasterio/warp.py\u001b[0m in \u001b[0;36mreproject\u001b[0;34m(source, destination, src_transform, gcps, src_crs, src_nodata, dst_transform, dst_crs, dst_nodata, resampling, init_dest_nodata, **kwargs)\u001b[0m\n\u001b[1;32m 268\u001b[0m _reproject(source, destination, src_transform, gcps, src_crs, src_nodata,\n\u001b[1;32m 269\u001b[0m \u001b[0mdst_transform\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdst_crs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdst_nodata\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mresampling\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 270\u001b[0;31m init_dest_nodata, **kwargs)\n\u001b[0m\u001b[1;32m 271\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 272\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32mrasterio/_warp.pyx\u001b[0m in \u001b[0;36mrasterio._warp._reproject (rasterio/_warp.cpp:7877)\u001b[0;34m()\u001b[0m\n",
"\u001b[0;32mrasterio/_err.pyx\u001b[0m in \u001b[0;36mrasterio._err.exc_wrap_int (rasterio/_err.c:2098)\u001b[0;34m()\u001b[0m\n",
"\u001b[0;31mCPLE_IllegalArgError\u001b[0m: panDstBands[0] = 2 ... out of range for dataset."
]
}
],
"source": [
"with rasterio.open(file_name, \"r\") as f:\n",
" dst = np.empty((1, f.height, f.width), dtype=f.dtypes[0])\n",
"\n",
"\n",
" params = dict(dst_crs=f.crs,\n",
" dst_transform=affine.Affine(10,0,0,\n",
" 0, -10, 0))\n",
"\n",
" print(\"Warping first band succeeds\")\n",
" rasterio.warp.reproject(rasterio.band(f,1), dst, **params)\n",
" print(\"OK\")\n",
"\n",
" print(\"Warping band 2 or higher fails\")\n",
" rasterio.warp.reproject(rasterio.band(f,2), dst, **params)\n",
" print(\"This never runs\")"
]
}
],
"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.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment