<s> import binascii <EOL> import struct <EOL> from django . forms import ValidationError <EOL> from . const import ( <EOL> GDAL_TO_POSTGIS , GDAL_TO_STRUCT , POSTGIS_HEADER_STRUCTURE , POSTGIS_TO_GDAL , <EOL> STRUCT_SIZE , <EOL> ) <EOL> def pack ( structure , data ) : <EOL> """<STR_LIT>""" <EOL> return binascii . hexlify ( struct . pack ( '<STR_LIT:<>' + structure , * data ) ) . upper ( ) <EOL> def unpack ( structure , data ) : <EOL> """<STR_LIT>""" <EOL> return struct . unpack ( '<STR_LIT:<>' + structure , binascii . unhexlify ( data ) ) <EOL> def chunk ( data , index ) : <EOL> """<STR_LIT>""" <EOL> return data [ : index ] , data [ index : ] <EOL> def get_pgraster_srid ( data ) : <EOL> """<STR_LIT>""" <EOL> if data is None : <EOL> return <EOL> return unpack ( '<STR_LIT:i>' , data [ <NUM_LIT> : <NUM_LIT> ] ) [ <NUM_LIT:0> ] <EOL> def from_pgraster ( data ) : <EOL> """<STR_LIT>""" <EOL> if data is None : <EOL> return <EOL> header , data = chunk ( data , <NUM_LIT> ) <EOL> header = unpack ( POSTGIS_HEADER_STRUCTURE , header ) <EOL> bands = [ ] <EOL> pixeltypes = [ ] <EOL> while data : <EOL> pixeltype , data = chunk ( data , <NUM_LIT:2> ) <EOL> pixeltype = unpack ( '<STR_LIT:B>' , pixeltype ) [ <NUM_LIT:0> ] <EOL> has_nodata = pixeltype >= <NUM_LIT:64> <EOL> if has_nodata : <EOL> pixeltype -= <NUM_LIT:64> <EOL> pixeltype = POSTGIS_TO_GDAL [ pixeltype ] <EOL> pack_type = GDAL_TO_STRUCT [ pixeltype ] <EOL> pack_size = <NUM_LIT:2> * STRUCT_SIZE [ pack_type ] <EOL> nodata , data = chunk ( data , pack_size ) <EOL> nodata = unpack ( pack_type , nodata ) [ <NUM_LIT:0> ] <EOL> band , data = chunk ( data , pack_size * header [ <NUM_LIT:10> ] * header [ <NUM_LIT:11> ] ) <EOL> band_result = { '<STR_LIT:data>' : binascii . unhexlify ( band ) } <EOL> if has_nodata : <EOL> band_result [ '<STR_LIT>' ] = nodata <EOL> bands . append ( band_result ) <EOL> pixeltypes . append ( pixeltype ) <EOL> if len ( set ( pixeltypes ) ) != <NUM_LIT:1> : <EOL> raise ValidationError ( "<STR_LIT>" ) <EOL> return { <EOL> '<STR_LIT>' : int ( header [ <NUM_LIT:9> ] ) , <EOL> '<STR_LIT:width>' : header [ <NUM_LIT:10> ] , '<STR_LIT>' : header [ <NUM_LIT:11> ] , <EOL> '<STR_LIT>' : pixeltypes [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : ( header [ <NUM_LIT:5> ] , header [ <NUM_LIT:6> ] ) , <EOL> '<STR_LIT>' : ( header [ <NUM_LIT:3> ] , header [ <NUM_LIT:4> ] ) , <EOL> '<STR_LIT>' : ( header [ <NUM_LIT:7> ] , header [ <NUM_LIT:8> ] ) , <EOL> '<STR_LIT>' : bands , <EOL> } <EOL> def to_pgraster ( rast ) : <EOL> """<STR_LIT>""" <EOL> if rast is None or rast == '<STR_LIT>' : <EOL> return <EOL> rasterheader = ( <EOL> <NUM_LIT:1> , <NUM_LIT:0> , len ( rast . bands ) , rast . scale . x , rast . scale . y , <EOL> rast . origin . x , rast . origin . y , rast . skew . x , rast . skew . y , <EOL> rast . srs . srid , rast . width , rast . height , <EOL> ) <EOL> result = pack ( POSTGIS_HEADER_STRUCTURE , rasterheader ) <EOL> for band in rast . bands : <EOL> structure = '<STR_LIT:B>' + GDAL_TO_STRUCT [ band . datatype ( ) ] <EOL> pixeltype = GDAL_TO_POSTGIS [ band . datatype ( ) ] <EOL> if band . nodata_value is not None : <EOL> pixeltype += <NUM_LIT:64> <EOL> bandheader = pack ( structure , ( pixeltype , band . nodata_value or <NUM_LIT:0> ) ) <EOL> band_data_hex = binascii . hexlify ( band . data ( as_memoryview = True ) ) . upper ( ) <EOL> result += bandheader + band_data_hex <EOL> return result . decode ( ) </s>
