PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ wkblify_band()

raster2pgsql.wkblify_band (   options,
  band,
  level,
  xoff,
  yoff,
  read_block_size,
  block_size,
  infile,
  bandidx 
)
Writes band of given GDAL dataset into HEX-encoded WKB for WKT Raster output.

Definition at line 758 of file raster2pgsql.py.

758def wkblify_band(options, band, level, xoff, yoff, read_block_size, block_size, infile, bandidx):
759 """Writes band of given GDAL dataset into HEX-encoded WKB for WKT Raster output."""
760 assert band is not None, "Error: Missing GDAL raster band"
761
762 hexwkb = ''
763
764 if options.register:
765 # Off-db raster
766 # TODO: Do we want to handle options.overview_level? --mloskot
767 # ANSWER:
768 # TODO: Where bandidx and ds come from? --mloskot
769 # ANSWER: Provided by caller method --jorgearevalo
770 hexwkb += wkblify('B', bandidx - 1)
771 filepath = os.path.abspath(infile.replace('\\', '\\\\'))
772 logit('MSG: Out-db raster path=%s\n' % filepath)
773 hexwkb += wkblify(str(len(filepath)) + 's', filepath)
774 hexwkb += wkblify('B', 0)
775 else:
776 # In-db raster
777
778 # Right most column and bottom most row of blocks have
779 # portions that extend beyond the raster
780 read_padding_size = calculate_block_pad_size(band, xoff, yoff, read_block_size)
781 valid_read_block_size = ( read_block_size[0] - read_padding_size[0],
782 read_block_size[1] - read_padding_size[1] )
783
784
785 if read_padding_size[0] > 0 or read_padding_size[1] > 0:
786 target_block_size = (valid_read_block_size[0] / level, valid_read_block_size[1] / level)
787 target_padding_size = (read_padding_size[0] / level, read_padding_size[1] / level)
788 else:
789 target_block_size = block_size
790 target_padding_size = ( 0, 0 )
791
792 logit('MSG: Normalize read_block=%s for level=%d to valid_read_block=%s with padding=%s\n' % \
793 (read_block_size, level, valid_read_block_size, read_padding_size))
794 logit('MSG: Normalize target_block=%s for level=%d to valid_target_block=%s with padding=%s\n' % \
795 (block_size, level, target_block_size, target_padding_size))
796 logit('MSG: ReadAsArray( %d, %d, %s, %s)\n' % \
797 (xoff, yoff, str(valid_read_block_size), str(target_block_size)))
798
799 assert valid_read_block_size[0] > 0 and valid_read_block_size[1] > 0
800 assert target_block_size[0] > 0 and target_block_size[1] > 0
801
802 pixels = band.ReadAsArray(xoff, yoff, valid_read_block_size[0], valid_read_block_size[1],
803 target_block_size[0], target_block_size[1])
804
805 # XXX: Use for debugging only
806 #dump_block_numpy(pixels)
807
808 out_pixels = numpy.zeros((block_size[1], block_size[0]), pt2numpy(band.DataType))
809
810 logit('MSG: Read valid source:\t%d x %d\n' % (len(pixels[0]), len(pixels)))
811 logit('MSG: Write into block:\t%d x %d\n' % (len(out_pixels[0]), len(out_pixels)))
812
813 if target_padding_size[0] > 0 or target_padding_size[1] > 0:
814
815 ysize_read_pixels = len(pixels)
816 nodata_value = fetch_band_nodata(band)
817
818 # Apply columns padding
819 pad_cols = numpy.array([nodata_value] * target_padding_size[0])
820 for row in range (0, ysize_read_pixels):
821 out_line = numpy.append(pixels[row], pad_cols)
822 out_pixels[row] = out_line
823
824 # Fill rows padding with nodata value
825 for row in range(ysize_read_pixels, ysize_read_pixels + target_padding_size[1]):
826 out_pixels[row].fill(nodata_value)
827 else:
828 out_pixels = pixels
829
830 # XXX: Use for debugging only
831 #dump_block_numpy(out_pixels)
832
833 hexwkb = binascii.hexlify(out_pixels)
834
835 check_hex(hexwkb)
836 return hexwkb
837
#define str(s)