Used bytes.hex() and bytes.fromhex() in postgis.pgraster to simplify.

This was missed in 93cdd07e8f.
This commit is contained in:
Sergey Fedoseev 2017-11-24 17:52:13 +05:00 committed by Tim Graham
parent e283c1a267
commit 31425f71bc
1 changed files with 7 additions and 11 deletions

View File

@ -1,4 +1,3 @@
import binascii
import struct import struct
from django.forms import ValidationError from django.forms import ValidationError
@ -13,14 +12,14 @@ def pack(structure, data):
""" """
Pack data into hex string with little endian format. 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): def unpack(structure, data):
""" """
Unpack little endian hexlified binary string into a list. 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): def chunk(data, index):
@ -67,7 +66,7 @@ def from_pgraster(data):
# Chunk and unpack band data (pack size times nr of pixels) # Chunk and unpack band data (pack size times nr of pixels)
band, data = chunk(data, pack_size * header[10] * header[11]) 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 the nodata flag is True, set the nodata value.
if has_nodata: if has_nodata:
@ -109,7 +108,7 @@ def to_pgraster(rast):
rast.srs.srid, rast.width, rast.height, rast.srs.srid, rast.width, rast.height,
) )
# Hexlify raster header # Pack raster header.
result = pack(POSTGIS_HEADER_STRUCTURE, rasterheader) result = pack(POSTGIS_HEADER_STRUCTURE, rasterheader)
for band in rast.bands: for band in rast.bands:
@ -135,11 +134,8 @@ def to_pgraster(rast):
# Pack band header # Pack band header
bandheader = pack(structure, (pixeltype, band.nodata_value or 0)) 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 # 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 # Convert raster to hex string before passing it to the DB.
return result.decode() return result.hex()