Skip to content

Instantly share code, notes, and snippets.

@j08lue
Created June 20, 2017 09:43
Show Gist options
  • Save j08lue/f577d557dfd63d4af5db562f5abcfee4 to your computer and use it in GitHub Desktop.
Save j08lue/f577d557dfd63d4af5db562f5abcfee4 to your computer and use it in GitHub Desktop.
Rasterio array resampling in Python 2 - fails
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import rasterio\n",
"import rasterio.warp"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import affine"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"arr = (np.random.random((20, 30)) * 100).astype('int')\n",
"fill_value = 42\n",
"newarr = np.full((200, 300), fill_value=fill_value, dtype='int')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"src_crs = {'init': 'EPSG:32632'}"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"src_transform = affine.Affine(600.0, 0.0, 399960.0, 0.0, -600.0, 6100020.0)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"dst_transform = affine.Affine(60.0, 0.0, 399960.0, 0.0, -60.0, 6100020.0)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Exception ValueError: \"Buffer dtype mismatch, expected 'long' but got 'long long'\" in 'rasterio._io.io_auto' ignored\n",
"Exception ValueError: \"Buffer dtype mismatch, expected 'long' but got 'long long'\" in 'rasterio._io.io_auto' ignored\n",
"Exception ValueError: \"Buffer dtype mismatch, expected 'long' but got 'long long'\" in 'rasterio._io.io_auto' ignored\n"
]
}
],
"source": [
"rasterio.warp.reproject(arr, newarr,\n",
" src_transform=src_transform,\n",
" dst_transform=dst_transform,\n",
" src_crs=src_crs,\n",
" dst_crs=src_crs,\n",
" resample=rasterio.warp.Resampling.nearest)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"ename": "AssertionError",
"evalue": "target array contains interpolated data",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mAssertionError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-9-ce68c6ae9cf2>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32massert\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mnewarr\u001b[0m \u001b[1;33m==\u001b[0m \u001b[0mfill_value\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mall\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'target array contains interpolated data'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mAssertionError\u001b[0m: target array contains interpolated data"
]
}
],
"source": [
"assert not (newarr == fill_value).all(), 'target array contains interpolated data'"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment