diff --git a/django/contrib/gis/db/backends/postgis/pgraster.py b/django/contrib/gis/db/backends/postgis/pgraster.py index ff06f11895..e6c1b2247c 100644 --- a/django/contrib/gis/db/backends/postgis/pgraster.py +++ b/django/contrib/gis/db/backends/postgis/pgraster.py @@ -1,4 +1,3 @@ -import binascii import struct from django.forms import ValidationError @@ -13,14 +12,14 @@ def pack(structure, data): """ Pack data into hex string with little endian format. """ - return binascii.hexlify(struct.pack('<' + structure, *data)).upper() + return struct.pack('<' + structure, *data) def unpack(structure, data): """ Unpack little endian hexlified binary string into a list. """ - return struct.unpack('<' + structure, binascii.unhexlify(data)) + return struct.unpack('<' + structure, bytes.fromhex(data)) def chunk(data, index): @@ -67,7 +66,7 @@ def from_pgraster(data): # Chunk and unpack band data (pack size times nr of pixels) band, data = chunk(data, pack_size * header[10] * header[11]) - band_result = {'data': binascii.unhexlify(band)} + band_result = {'data': bytes.fromhex(band)} # If the nodata flag is True, set the nodata value. if has_nodata: @@ -109,7 +108,7 @@ def to_pgraster(rast): rast.srs.srid, rast.width, rast.height, ) - # Hexlify raster header + # Pack raster header. result = pack(POSTGIS_HEADER_STRUCTURE, rasterheader) for band in rast.bands: @@ -135,11 +134,8 @@ def to_pgraster(rast): # Pack band header bandheader = pack(structure, (pixeltype, band.nodata_value or 0)) - # Hexlify band data - band_data_hex = binascii.hexlify(band.data(as_memoryview=True)).upper() - # Add packed header and band data to result - result += bandheader + band_data_hex + result += bandheader + band.data(as_memoryview=True) - # Cast raster to string before passing it to the DB - return result.decode() + # Convert raster to hex string before passing it to the DB. + return result.hex()