[Django] #26432: Bug: X/Y inverted when using numpy.reshape() on a GDALRaster's GDALBand

8 views
Skip to first unread message

Django

unread,
Mar 31, 2016, 4:34:05 AM3/31/16
to django-...@googlegroups.com
#26432: Bug: X/Y inverted when using numpy.reshape() on a GDALRaster's GDALBand
----------------------------+---------------------------------------
Reporter: Opa- | Owner: nobody
Type: Bug | Status: new
Component: GIS | Version: 1.9
Severity: Normal | Keywords: GDALRaster numpy GDALBand
Triage Stage: Unreviewed | Has patch: 1
Easy pickings: 0 | UI/UX: 0
----------------------------+---------------------------------------
When importing GeoTIF data threw `GDALRaster` all the array is inverted
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]))
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/26432>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 31, 2016, 4:37:28 AM3/31/16
to django-...@googlegroups.com
#26432: Bug: X/Y inverted when using numpy.reshape() on a GDALRaster's GDALBand
---------------------------------------+----------------------------

Reporter: Opa- | Owner: nobody
Type: Bug | Status: new
Component: GIS | Version: 1.9
Severity: Normal | Resolution:

Keywords: GDALRaster numpy GDALBand | Triage Stage: Unreviewed
Has patch: 1 | Easy pickings: 0
UI/UX: 0 |
---------------------------------------+----------------------------
Changes (by Opa-):

* Attachment "cea (1).tif" added.

Original GeoTIF sample

Django

unread,
Mar 31, 2016, 4:38:03 AM3/31/16
to django-...@googlegroups.com
#26432: Bug: X/Y inverted when using numpy.reshape() on a GDALRaster's GDALBand
---------------------------------------+----------------------------

Reporter: Opa- | Owner: nobody
Type: Bug | Status: new
Component: GIS | Version: 1.9
Severity: Normal | Resolution:

Keywords: GDALRaster numpy GDALBand | Triage Stage: Unreviewed
Has patch: 1 | Easy pickings: 0
UI/UX: 0 |
---------------------------------------+----------------------------
Changes (by Opa-):

* Attachment "cea_2.tif" added.

GeoTIF after being imported

Django

unread,
Mar 31, 2016, 4:39:22 AM3/31/16
to django-...@googlegroups.com
#26432: Bug: X/Y inverted when using numpy.reshape() on a GDALRaster's GDALBand
---------------------------------------+----------------------------

Reporter: Opa- | Owner: nobody
Type: Bug | Status: new
Component: GIS | Version: 1.9
Severity: Normal | Resolution:

Keywords: GDALRaster numpy GDALBand | Triage Stage: Unreviewed
Has patch: 1 | Easy pickings: 0
UI/UX: 0 |
---------------------------------------+----------------------------
Changes (by Opa-):

* Attachment "cea.png" added.

Original GeoTIF sample

Django

unread,
Mar 31, 2016, 4:39:35 AM3/31/16
to django-...@googlegroups.com
#26432: Bug: X/Y inverted when using numpy.reshape() on a GDALRaster's GDALBand
---------------------------------------+----------------------------

Reporter: Opa- | Owner: nobody
Type: Bug | Status: new
Component: GIS | Version: 1.9
Severity: Normal | Resolution:

Keywords: GDALRaster numpy GDALBand | Triage Stage: Unreviewed
Has patch: 1 | Easy pickings: 0
UI/UX: 0 |
---------------------------------------+----------------------------
Changes (by Opa-):

* Attachment "cea_2.png" added.

GeoTIF after being imported

--

Django

unread,
Mar 31, 2016, 4:49:07 AM3/31/16
to django-...@googlegroups.com
#26432: Bug: X/Y inverted when using numpy.reshape() on a GDALRaster's GDALBand
-------------------------------------+-------------------------------------

Reporter: Opa- | Owner: nobody
Type: Bug | Status: new
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
-------------------------------------+-------------------------------------
Changes (by Opa-):

* 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>

Django

unread,
Mar 31, 2016, 4:50:31 AM3/31/16
to django-...@googlegroups.com
#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
-------------------------------------+-------------------------------------
Changes (by Opa-):

* status: new => assigned
* cc: Opa- (added)
* owner: nobody => Opa-


--
Ticket URL: <https://code.djangoproject.com/ticket/26432#comment:2>

Django

unread,
Mar 31, 2016, 4:53:11 AM3/31/16
to django-...@googlegroups.com
#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:

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>

Django

unread,
Mar 31, 2016, 4:56:34 AM3/31/16
to django-...@googlegroups.com
#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

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

unread,
Mar 31, 2016, 4:59:01 AM3/31/16
to django-...@googlegroups.com
#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 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>

Django

unread,
Mar 31, 2016, 8:21:47 AM3/31/16
to django-...@googlegroups.com
#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: Accepted
GDALBand |

Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* 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>

Django

unread,
Mar 31, 2016, 10:09:33 AM3/31/16
to django-...@googlegroups.com
#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: Accepted
GDALBand |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Opa-):

* needs_tests: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/26432#comment:7>

Django

unread,
Mar 31, 2016, 10:10:35 AM3/31/16
to django-...@googlegroups.com
#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: Accepted
GDALBand |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Apr 1, 2016, 7:40:43 PM4/1/16
to django-...@googlegroups.com
#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: Accepted
GDALBand |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* 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>

Django

unread,
Apr 11, 2016, 3:40:39 AM4/11/16
to django-...@googlegroups.com
#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: Accepted
GDALBand |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Opa-):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/26432#comment:10>

Django

unread,
Apr 12, 2016, 10:13:41 AM4/12/16
to django-...@googlegroups.com
#26432: Bug: X/Y inverted when using numpy.reshape() on a GDALRaster's GDALBand
-------------------------------------+-------------------------------------
Reporter: Opa- | Owner: Opa-
Type: Bug | Status: closed
Component: GIS | Version: 1.9
Severity: Normal | Resolution: fixed

Keywords: GDALRaster numpy | Triage Stage: Accepted
GDALBand |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* 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>

Django

unread,
Apr 16, 2016, 4:25:43 PM4/16/16
to django-...@googlegroups.com
#26432: Bug: X/Y inverted when using numpy.reshape() on a GDALRaster's GDALBand
-------------------------------------+-------------------------------------
Reporter: Opa- | Owner: Opa-
Type: Bug | Status: closed
Component: GIS | Version: 1.9

Severity: Normal | Resolution: fixed
Keywords: GDALRaster numpy | Triage Stage: Accepted
GDALBand |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Reply all
Reply to author
Forward
0 new messages