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>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* Attachment "cea (1).tif" added.
Original GeoTIF sample
* Attachment "cea_2.tif" added.
GeoTIF after being imported
* Attachment "cea.png" added.
Original GeoTIF sample
* Attachment "cea_2.png" added.
GeoTIF after being imported
--
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Comment:
Link to the fixed branch https://github.com/Opa-/django/tree/ticket_26432
--
Ticket URL: <https://code.djangoproject.com/ticket/26432#comment:1>
* status: new => assigned
* cc: Opa- (added)
* owner: nobody => Opa-
--
Ticket URL: <https://code.djangoproject.com/ticket/26432#comment:2>
Old description:
New 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
--
--
Ticket URL: <https://code.djangoproject.com/ticket/26432#comment:3>
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
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>
Old 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]))
> }}}
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 I ran into
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:5>
* needs_tests: 0 => 1
* stage: Unreviewed => Accepted
Comment:
The pull request currently lacks a test. Please uncheck "Needs test" when
you add one. Thanks!
--
Ticket URL: <https://code.djangoproject.com/ticket/26432#comment:6>
* needs_tests: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/26432#comment:7>
Comment (by Opa-):
I've just commited tests for the patch ;)
I'm checking the length of the row & columns of the numpy + some arbitrary
values.
--
Ticket URL: <https://code.djangoproject.com/ticket/26432#comment:8>
* needs_better_patch: 0 => 1
Comment:
Please uncheck "Patch needs improvement" when you address the review
comments.
--
Ticket URL: <https://code.djangoproject.com/ticket/26432#comment:9>
* needs_better_patch: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/26432#comment:10>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"461f74ab19b7b0f393c475a55b4880227a8e4413" 461f74ab]:
{{{
#!CommitTicketReference repository=""
revision="461f74ab19b7b0f393c475a55b4880227a8e4413"
Fixed #26432 -- Fixed size tuple order when using numpy reshape on a
GDALBand.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/26432#comment:11>
Comment (by Tim Graham <timograham@…>):
In [changeset:"a3265af808bdf8fb466545a64608ff42de30f40d" a3265af8]:
{{{
#!CommitTicketReference repository=""
revision="a3265af808bdf8fb466545a64608ff42de30f40d"
Refs #26432 -- Skipped a raster test as needed.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/26432#comment:12>