Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#26432 closed Bug (fixed)

Bug: X/Y inverted when using numpy.reshape() on a GDALRaster's GDALBand

Reported by: Vincent Letarouilly Owned by: Vincent Letarouilly
Component: GIS Version: 1.9
Severity: Normal Keywords: GDALRaster numpy GDALBand
Cc: Vincent Letarouilly Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Vincent Letarouilly)

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 | 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]))

Attachments (4)

cea (1).tif (68.6 KB ) - added by Vincent Letarouilly 9 years ago.
Original GeoTIF sample
cea_2.tif (68.6 KB ) - added by Vincent Letarouilly 9 years ago.
GeoTIF after being imported
cea.png (58.1 KB ) - added by Vincent Letarouilly 9 years ago.
Original GeoTIF sample
cea_2.png (58.0 KB ) - added by Vincent Letarouilly 9 years ago.
GeoTIF after being imported

Download all attachments as: .zip

Change History (16)

by Vincent Letarouilly, 9 years ago

Attachment: cea (1).tif added

Original GeoTIF sample

by Vincent Letarouilly, 9 years ago

Attachment: cea_2.tif added

GeoTIF after being imported

by Vincent Letarouilly, 9 years ago

Attachment: cea.png added

Original GeoTIF sample

by Vincent Letarouilly, 9 years ago

Attachment: cea_2.png added

GeoTIF after being imported

comment:1 by Vincent Letarouilly, 9 years ago

Version 0, edited 9 years ago by Vincent Letarouilly (next)

comment:2 by Vincent Letarouilly, 9 years ago

Cc: Vincent Letarouilly added
Owner: changed from nobody to Vincent Letarouilly
Status: newassigned

comment:3 by Vincent Letarouilly, 9 years ago

Description: modified (diff)

comment:4 by Vincent Letarouilly, 9 years ago

Description: modified (diff)

comment:5 by Vincent Letarouilly, 9 years ago

Description: modified (diff)

comment:6 by Tim Graham, 9 years ago

Needs tests: set
Triage Stage: UnreviewedAccepted

The pull request currently lacks a test. Please uncheck "Needs test" when you add one. Thanks!

comment:7 by Vincent Letarouilly, 9 years ago

Needs tests: unset

comment:8 by Vincent Letarouilly, 9 years ago

I've just commited tests for the patch ;)
I'm checking the length of the row & columns of the numpy + some arbitrary values.

comment:9 by Tim Graham, 9 years ago

Patch needs improvement: set

Please uncheck "Patch needs improvement" when you address the review comments.

comment:10 by Vincent Letarouilly, 9 years ago

Patch needs improvement: unset

comment:11 by Tim Graham <timograham@…>, 9 years ago

Resolution: fixed
Status: assignedclosed

In 461f74ab:

Fixed #26432 -- Fixed size tuple order when using numpy reshape on a GDALBand.

comment:12 by Tim Graham <timograham@…>, 9 years ago

In a3265af8:

Refs #26432 -- Skipped a raster test as needed.

Note: See TracTickets for help on using tickets.
Back to Top