| 1 |
import struct |
| 2 |
|
| 3 |
from django.core.exceptions import ValidationError |
| 4 |
|
| 5 |
from .const import ( |
| 6 |
BANDTYPE_FLAG_HASNODATA, |
| 7 |
BANDTYPE_PIXTYPE_MASK, |
| 8 |
GDAL_TO_POSTGIS, |
| 9 |
GDAL_TO_STRUCT, |
| 10 |
POSTGIS_HEADER_STRUCTURE, |
| 11 |
POSTGIS_TO_GDAL, |
| 12 |
STRUCT_SIZE, |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
def pack(structure, data): |
| 17 |
""" |
| 18 |
Pack data into hex string with little endian format. |
| 19 |
""" |
| 20 |
return struct.pack("<" + structure, *data) |
| 21 |
|
| 22 |
|
| 23 |
def unpack(structure, data): |
| 24 |
""" |
| 25 |
Unpack little endian hexlified binary string into a list. |
| 26 |
""" |
| 27 |
return struct.unpack("<" + structure, bytes.fromhex(data)) |
| 28 |
|
| 29 |
|
| 30 |
def chunk(data, index): |
| 31 |
""" |
| 32 |
Split a string into two parts at the input index. |
| 33 |
""" |
| 34 |
return data[:index], data[index:] |
| 35 |
|
| 36 |
|
| 37 |
def from_pgraster(data): |
| 38 |
""" |
| 39 |
Convert a PostGIS HEX String into a dictionary. |
| 40 |
""" |
| 41 |
if data is None: |
| 42 |
return |
| 43 |
|
| 44 |
# Split raster header from data |
| 45 |
header, data = chunk(data, 122) |
| 46 |
header = unpack(POSTGIS_HEADER_STRUCTURE, header) |
| 47 |
|
| 48 |
# Parse band data |
| 49 |
bands = [] |
| 50 |
pixeltypes = [] |
| 51 |
while data: |
| 52 |
# Get pixel type for this band |
| 53 |
pixeltype_with_flags, data = chunk(data, 2) |
| 54 |
pixeltype_with_flags = unpack("B", pixeltype_with_flags)[0] |
| 55 |
pixeltype = pixeltype_with_flags & BANDTYPE_PIXTYPE_MASK |
| 56 |
|
| 57 |
# Convert datatype from PostGIS to GDAL & get pack type and size |
| 58 |
pixeltype = POSTGIS_TO_GDAL[pixeltype] |
| 59 |
pack_type = GDAL_TO_STRUCT[pixeltype] |
| 60 |
pack_size = 2 * STRUCT_SIZE[pack_type] |
| 61 |
|
| 62 |
# Parse band nodata value. The nodata value is part of the |
| 63 |
# PGRaster string even if the nodata flag is True, so it always |
| 64 |
# has to be chunked off the data string. |
| 65 |
nodata, data = chunk(data, pack_size) |
| 66 |
nodata = unpack(pack_type, nodata)[0] |
| 67 |
|
| 68 |
# Chunk and unpack band data (pack size times nr of pixels) |
| 69 |
band, data = chunk(data, pack_size * header[10] * header[11]) |
| 70 |
band_result = {"data": bytes.fromhex(band)} |
| 71 |
|
| 72 |
# Set the nodata value if the nodata flag is set. |
| 73 |
if pixeltype_with_flags & BANDTYPE_FLAG_HASNODATA: |
| 74 |
band_result["nodata_value"] = nodata |
| 75 |
|
| 76 |
# Append band data to band list |
| 77 |
bands.append(band_result) |
| 78 |
|
| 79 |
# Store pixeltype of this band in pixeltypes array |
| 80 |
pixeltypes.append(pixeltype) |
| 81 |
|
| 82 |
# Check that all bands have the same pixeltype. |
| 83 |
# This is required by GDAL. PostGIS rasters could have different pixeltypes |
| 84 |
# for bands of the same raster. |
| 85 |
if len(set(pixeltypes)) != 1: |
| 86 |
raise ValidationError("Band pixeltypes are not all equal.") |
| 87 |
|
| 88 |
return { |
| 89 |
"srid": int(header[9]), |
| 90 |
"width": header[10], |
| 91 |
"height": header[11], |
| 92 |
"datatype": pixeltypes[0], |
| 93 |
"origin": (header[5], header[6]), |
| 94 |
"scale": (header[3], header[4]), |
| 95 |
"skew": (header[7], header[8]), |
| 96 |
"bands": bands, |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
def to_pgraster(rast): |
| 101 |
""" |
| 102 |
Convert a GDALRaster into PostGIS Raster format. |
| 103 |
""" |
| 104 |
# Prepare the raster header data as a tuple. The first two numbers are |
| 105 |
# the endianness and the PostGIS Raster Version, both are fixed by |
| 106 |
# PostGIS at the moment. |
| 107 |
rasterheader = ( |
| 108 |
1, |
| 109 |
0, |
| 110 |
len(rast.bands), |
| 111 |
rast.scale.x, |
| 112 |
rast.scale.y, |
| 113 |
rast.origin.x, |
| 114 |
rast.origin.y, |
| 115 |
rast.skew.x, |
| 116 |
rast.skew.y, |
| 117 |
rast.srs.srid, |
| 118 |
rast.width, |
| 119 |
rast.height, |
| 120 |
) |
| 121 |
|
| 122 |
# Pack raster header. |
| 123 |
result = pack(POSTGIS_HEADER_STRUCTURE, rasterheader) |
| 124 |
|
| 125 |
for band in rast.bands: |
| 126 |
# The PostGIS raster band header has exactly two elements, a 8BUI byte |
| 127 |
# and the nodata value. |
| 128 |
# |
| 129 |
# The 8BUI stores both the PostGIS pixel data type and a nodata flag. |
| 130 |
# It is composed as the datatype with BANDTYPE_FLAG_HASNODATA (1 << 6) |
| 131 |
# for existing nodata values: |
| 132 |
# 8BUI_VALUE = PG_PIXEL_TYPE (0-11) | BANDTYPE_FLAG_HASNODATA |
| 133 |
# |
| 134 |
# For example, if the byte value is 71, then the datatype is |
| 135 |
# 71 & ~BANDTYPE_FLAG_HASNODATA = 7 (32BSI) |
| 136 |
# and the nodata value is True. |
| 137 |
structure = "B" + GDAL_TO_STRUCT[band.datatype()] |
| 138 |
|
| 139 |
# Get band pixel type in PostGIS notation |
| 140 |
pixeltype = GDAL_TO_POSTGIS[band.datatype()] |
| 141 |
|
| 142 |
# Set the nodata flag |
| 143 |
if band.nodata_value is not None: |
| 144 |
pixeltype |= BANDTYPE_FLAG_HASNODATA |
| 145 |
|
| 146 |
# Pack band header |
| 147 |
bandheader = pack(structure, (pixeltype, band.nodata_value or 0)) |
| 148 |
|
| 149 |
# Add packed header and band data to result |
| 150 |
result += bandheader + band.data(as_memoryview=True) |
| 151 |
|
| 152 |
return result |