diff options
| author | Hasan Ramezani <hasan.r67@gmail.com> | 2020-03-03 15:25:19 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2020-03-05 09:44:44 +0100 |
| commit | 7e15795bf06d362f20257d2e9db378ba8940dc39 (patch) | |
| tree | 5e058c587b43ca37e7f95cb16891e8d42cacf5be /django | |
| parent | 769cee525222bb155735aba31d6174d73c271f3c (diff) | |
Fixed #30489 -- Fixed RasterField deserialization with pixeltype flags.
Thanks Ivor Bosloper for the original patch.
Diffstat (limited to 'django')
| -rw-r--r-- | django/contrib/gis/db/backends/postgis/const.py | 6 | ||||
| -rw-r--r-- | django/contrib/gis/db/backends/postgis/pgraster.py | 18 |
2 files changed, 13 insertions, 11 deletions
diff --git a/django/contrib/gis/db/backends/postgis/const.py b/django/contrib/gis/db/backends/postgis/const.py index 6091acc7b4..193aa3fc36 100644 --- a/django/contrib/gis/db/backends/postgis/const.py +++ b/django/contrib/gis/db/backends/postgis/const.py @@ -42,5 +42,11 @@ STRUCT_SIZE = { 'd': 8, # Double } +# Pixel type specifies type of pixel values in a band. Storage flag specifies +# whether the band data is stored as part of the datum or is to be found on the +# server's filesystem. There are currently 11 supported pixel value types, so 4 +# bits are enough to account for all. Reserve the upper 4 bits for generic +# flags. # See https://trac.osgeo.org/postgis/wiki/WKTRaster/RFC/RFC1_V0SerialFormat#Pixeltypeandstorageflag +BANDTYPE_PIXTYPE_MASK = 0x0F BANDTYPE_FLAG_HASNODATA = 1 << 6 diff --git a/django/contrib/gis/db/backends/postgis/pgraster.py b/django/contrib/gis/db/backends/postgis/pgraster.py index 1a4dc9aa23..085c130183 100644 --- a/django/contrib/gis/db/backends/postgis/pgraster.py +++ b/django/contrib/gis/db/backends/postgis/pgraster.py @@ -3,8 +3,8 @@ import struct from django.forms import ValidationError from .const import ( - BANDTYPE_FLAG_HASNODATA, GDAL_TO_POSTGIS, GDAL_TO_STRUCT, - POSTGIS_HEADER_STRUCTURE, POSTGIS_TO_GDAL, STRUCT_SIZE, + BANDTYPE_FLAG_HASNODATA, BANDTYPE_PIXTYPE_MASK, GDAL_TO_POSTGIS, + GDAL_TO_STRUCT, POSTGIS_HEADER_STRUCTURE, POSTGIS_TO_GDAL, STRUCT_SIZE, ) @@ -45,13 +45,9 @@ def from_pgraster(data): pixeltypes = [] while data: # Get pixel type for this band - pixeltype, data = chunk(data, 2) - pixeltype = unpack('B', pixeltype)[0] - - # Remove nodata byte from band nodata value if it exists. - has_nodata = pixeltype & BANDTYPE_FLAG_HASNODATA - if has_nodata: - pixeltype &= ~BANDTYPE_FLAG_HASNODATA + pixeltype_with_flags, data = chunk(data, 2) + pixeltype_with_flags = unpack('B', pixeltype_with_flags)[0] + pixeltype = pixeltype_with_flags & BANDTYPE_PIXTYPE_MASK # Convert datatype from PostGIS to GDAL & get pack type and size pixeltype = POSTGIS_TO_GDAL[pixeltype] @@ -68,8 +64,8 @@ def from_pgraster(data): band, data = chunk(data, pack_size * header[10] * header[11]) band_result = {'data': bytes.fromhex(band)} - # If the nodata flag is True, set the nodata value. - if has_nodata: + # Set the nodata value if the nodata flag is set. + if pixeltype_with_flags & BANDTYPE_FLAG_HASNODATA: band_result['nodata_value'] = nodata # Append band data to band list |
