#26432: Bug: X/Y inverted when using numpy.reshape() on a GDALRaster's GDALBand
-------------------------------------+-------------------------------------
Reporter: Opa- | Owner: Opa-
Type: Bug | Status: assigned
Component: GIS | Version: 1.9
Severity: Normal | Resolution:
Keywords: GDALRaster numpy | Triage Stage:
GDALBand | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Opa-:
Old description:
> When importing GeoTIF data threw `GDALRaster` with numpy package
> installed all the array is a mess because of a inverted X/Y parameters
> passed to `numpy.reshape()` function.
> I was able to see this bug by importing a GeoTIF via `GDALRaster` then
> the generated datas did not make any sense so I decided to export the
> array to an image again to see if it was me doing shit or if there was a
> bug somewhere. When exporting the GeoTIF again to an image I get
> something really strange. You can see attached the original tif and the
> generated one that is not correct due to this X/Y mismatch. Here is the
> code to reproduce:
> {{{
> from PIL import Image
> from django.contrib.gis.gdal import GDALRaster
> import os, numpy
>
> rst = GDALRaster(os.path.join('/tmp', 'cea.tif'))
> data = rst.bands[0].data()
> rescaled = (255.0 / data.max() * (data - data.min())).astype(numpy.uint8)
> im = Image.fromarray(rescaled)
> im.save('/tmp/cea_2.tif')
> }}}
>
> You can see in the (see
> [http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.reshape.html
> | Numpy Reshape documentation]) that you should pass a `tuple` with the
> `height` and the `width` (in that order). And in the django code, the
> `size` tuple is built with `width` first then `height` at line 111:
> {{{
> size = (self.width - offset[0], self.height - offset[1])
> }}}
> The bug is located in `django.contrib.gis.gdal.raster.band.py` at line
> 148:
> {{{
> data_array, dtype=numpy.dtype(data_array)).reshape(size)
> }}}
> should be replaced by
> {{{
> data_array, dtype=numpy.dtype(data_array)).reshape((size[1], size[0]))
> }}}
New description:
When importing GeoTIF data through `GDALRaster` with `numpy` package
installed all the array is a mess because of a reversed X/Y parameters
passed to the `numpy.reshape()` function.
I was able to see this bug by importing a GeoTIF via `GDALRaster`. The
generated data did not make any sense so I decided to export the array to
an image again to see if it was me doing shit or if there was a bug
somewhere. When exporting the GeoTIF again to an image I get something
really strange. You can see in attachements the original tif and the
generated one that is not correct due to this X/Y mismatch. Here is the
code to reproduce:
{{{
from PIL import Image
from django.contrib.gis.gdal import GDALRaster
import os, numpy
rst = GDALRaster(os.path.join('/tmp', 'cea.tif'))
data = rst.bands[0].data()
rescaled = (255.0 / data.max() * (data - data.min())).astype(numpy.uint8)
im = Image.fromarray(rescaled)
im.save('/tmp/cea_2.tif')
}}}
You can see in the (see
[http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.reshape.html
| Numpy Reshape documentation]) that you should pass a `tuple` with the
`height` and the `width` (in that order). And in the django code, the
`size` tuple is built with `width` first then `height` at line 111:
{{{
size = (self.width - offset[0], self.height - offset[1])
}}}
The bug is located in `django.contrib.gis.gdal.raster.band.py` at line
148:
{{{
data_array, dtype=numpy.dtype(data_array)).reshape(size)
}}}
should be replaced by
{{{
data_array, dtype=numpy.dtype(data_array)).reshape((size[1], size[0]))
}}}
--
--
Ticket URL: <https://code.djangoproject.com/ticket/26432#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/062.5521c2e9d3948fdb4b51faed75815fea%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.