PostGIS 3.0.6dev-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 750 of file raster2pgsql.py.

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