Class: GDAL::RasterBand

Inherits:
Object
  • Object
show all
Includes:
Logger, MajorObject, AlgorithmExtensions, ColoringExtensions, Extensions, IOExtensions, GDAL::RasterBandMixins::AlgorithmMethods
Defined in:
lib/gdal/raster_band.rb,
lib/gdal/extensions/raster_band/extensions.rb,
lib/gdal/extensions/raster_band/io_extensions.rb,
lib/gdal/extensions/raster_band/coloring_extensions.rb,
lib/gdal/extensions/raster_band/algorithm_extensions.rb

Overview

rubocop:disable Metrics/ClassLength

Defined Under Namespace

Modules: AlgorithmExtensions, ColoringExtensions, Extensions, IOExtensions

Constant Summary collapse

ALL_VALID =
0x01
PER_DATASET =
0x02
ALPHA =
0x04
NODATA =
0x08

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AlgorithmExtensions

#simple_erase!

Methods included from ColoringExtensions

#colorize!, #colors_as_hex, #colors_as_rgb, #hex_to_rgb

Methods included from IOExtensions

#block_buffer_size, #block_count, #pixel_value, #read_blocks_by_block, #read_lines_by_block, #readlines, #set_pixel_value, #write_xy_narray

Methods included from Extensions

#each_overview, #overviews, #pixel_count, #projected_points, #to_a, #to_na, #to_nna

Methods included from GDAL::RasterBandMixins::AlgorithmMethods

#checksum_image, #compute_proximity!, #fill_nodata!, included, #polygonize, #sieve_filter, #sieve_filter!

Methods included from MajorObject

#all_metadata, #description, #metadata, #metadata_domain_list, #metadata_item, #null?, #set_metadata_item

Constructor Details

#initialize(raster_band, dataset = nil) ⇒ RasterBand

Returns a new instance of RasterBand.

Parameters:



28
29
30
31
32
33
34
35
36
# File 'lib/gdal/raster_band.rb', line 28

def initialize(raster_band, dataset = nil)
  @c_pointer = GDAL._pointer(GDAL::RasterBand, raster_band, autorelease: false)

  # Init the dataset too--the dataset owns the raster band, so when the dataset
  # gets closed, the raster band gets cleaned up. Storing a reference to the
  # dataset here ensures the underlying raster band doesn't get pulled out
  # from under the Ruby object that represents it.
  @dataset = dataset || init_dataset
end

Instance Attribute Details

#c_pointerFFI::Pointer (readonly)

Returns C pointer to the C raster band.

Returns:

  • (FFI::Pointer)

    C pointer to the C raster band.



22
23
24
# File 'lib/gdal/raster_band.rb', line 22

def c_pointer
  @c_pointer
end

#datasetGDAL::Dataset (readonly)

Returns The dataset that owns this RasterBand.

Returns:



25
26
27
# File 'lib/gdal/raster_band.rb', line 25

def dataset
  @dataset
end

Instance Method Details

#access_flagSymbol

The type of access to the raster band this object currently has.

Returns:

  • (Symbol)

    Either :GA_Update or :GA_ReadOnly.



62
63
64
# File 'lib/gdal/raster_band.rb', line 62

def access_flag
  FFI::GDAL::GDAL.GDALGetRasterAccess(@c_pointer)
end

#arbitrary_overviews?Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/gdal/raster_band.rb', line 178

def arbitrary_overviews?
  !FFI::GDAL::GDAL.GDALHasArbitraryOverviews(@c_pointer).zero?
end

#block_sizeHash{x => Integer, y => Integer}

The natural block size is the block size that is most efficient for accessing the format. For many formats this is simply a whole scanline in which case x is set to #x_size, and y is set to 1.

Returns:



122
123
124
125
126
127
128
# File 'lib/gdal/raster_band.rb', line 122

def block_size
  x_pointer = FFI::MemoryPointer.new(:int)
  y_pointer = FFI::MemoryPointer.new(:int)
  FFI::GDAL::GDAL.GDALGetBlockSize(@c_pointer, x_pointer, y_pointer)

  { x: x_pointer.read_int, y: y_pointer.read_int }
end

#category_namesArray<String>

Returns:



131
132
133
134
135
136
# File 'lib/gdal/raster_band.rb', line 131

def category_names
  names = FFI::GDAL::GDAL.GDALGetRasterCategoryNames(@c_pointer)
  return [] if names.null?

  names.get_array_of_string(0)
end

#category_names=(names) ⇒ Object

Parameters:

Raises:



140
141
142
143
144
145
146
# File 'lib/gdal/raster_band.rb', line 140

def category_names=(names)
  names_pointer = GDAL._string_array_to_pointer(names)

  GDAL::CPLErrorHandler.manually_handle("Unable to set category names") do
    FFI::GDAL::GDAL.GDALSetRasterCategoryNames(@c_pointer, names_pointer)
  end
end

#color_interpretationSymbol

Returns One of FFI::GDAL::GDAL::ColorInterp.

Returns:

  • (Symbol)

    One of FFI::GDAL::GDAL::ColorInterp.



75
76
77
# File 'lib/gdal/raster_band.rb', line 75

def color_interpretation
  FFI::GDAL::GDAL.GDALGetRasterColorInterpretation(@c_pointer)
end

#color_interpretation=(new_color_interp) ⇒ Object

Parameters:

Raises:



81
82
83
84
85
# File 'lib/gdal/raster_band.rb', line 81

def color_interpretation=(new_color_interp)
  GDAL::CPLErrorHandler.manually_handle("Unable to set color interpretation") do
    FFI::GDAL::GDAL.GDALSetRasterColorInterpretation(@c_pointer, new_color_interp)
  end
end

#color_tableGDAL::ColorTable

Gets the associated GDAL::ColorTable. Note that it remains owned by the RasterBand and cannot be modified.

Returns:



91
92
93
94
95
96
97
98
# File 'lib/gdal/raster_band.rb', line 91

def color_table
  color_table_ptr = FFI::GDAL::GDAL.GDALGetRasterColorTable(@c_pointer)
  color_table_ptr.autorelease = false

  return nil if color_table_ptr.null?

  ColorTable.new(color_table_ptr)
end

#color_table=(new_color_table) ⇒ Object

Parameters:

Raises:



102
103
104
105
106
107
108
# File 'lib/gdal/raster_band.rb', line 102

def color_table=(new_color_table)
  color_table_pointer = GDAL::ColorTable.new_pointer(new_color_table)

  GDAL::CPLErrorHandler.manually_handle("Unable to set color table") do
    FFI::GDAL::GDAL.GDALSetRasterColorTable(@c_pointer, color_table_pointer)
  end
end

#compute_statistics(approx_ok: false, &progress_block) ⇒ Hash{minimum => Float, maximum => Float, mean => Float, standard_deviation => Float}

Parameters:

  • approx_ok (Boolean) (defaults to: false)

    If true, allows for some approximating, which may speed up calculations.

Returns:



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/gdal/raster_band.rb', line 312

def compute_statistics(approx_ok: false, &progress_block)
  min_ptr = FFI::MemoryPointer.new(:double)
  max_ptr = FFI::MemoryPointer.new(:double)
  mean_ptr = FFI::MemoryPointer.new(:double)
  standard_deviation_ptr = FFI::MemoryPointer.new(:double)

  GDAL::CPLErrorHandler.manually_handle("Unable to compute raster statistics") do
    FFI::GDAL::GDAL::GDALComputeRasterStatistics(
      @c_pointer,                           # hBand
      approx_ok,                            # bApproxOK
      min_ptr,                              # pdfMin
      max_ptr,                              # pdfMax
      mean_ptr,                             # pdfMean
      standard_deviation_ptr,               # pdfStdDev
      progress_block,                       # pfnProgress
      nil                                   # pProgressData
    )
  end

  {
    minimum: min_ptr.read_double,
    maximum: max_ptr.read_double,
    mean: mean_ptr.read_double,
    standard_deviation: standard_deviation_ptr.read_double
  }
end

#copy_whole_raster(destination_band, **options, &progress) ⇒ Boolean

Copies the contents of one raster to another similarly configure band. The two bands must have the same width and height but do not have to be the same data type.

Options:

* :compressed
  * 'YES': forces alignment on the destination_band to achieve the best
    compression.

Parameters:

Options Hash (**options):

  • compress (String)

    Only ‘YES’ is supported.

Returns:

  • (Boolean)


591
592
593
594
595
596
597
598
599
600
601
602
# File 'lib/gdal/raster_band.rb', line 591

def copy_whole_raster(destination_band, **options, &progress)
  destination_pointer = GDAL._pointer(GDAL::RasterBand, destination_band)
  options_ptr = GDAL::Options.pointer(options)

  GDAL::CPLErrorHandler.manually_handle("Unable to copy whole raster") do
    FFI::GDAL::GDAL.GDALRasterBandCopyWholeRaster(@c_pointer,
                                                  destination_pointer,
                                                  options_ptr,
                                                  progress,
                                                  nil)
  end
end

#create_mask_band(*flags) ⇒ Object

Parameters:

  • flags (Array<Symbol>, Symbol)

    Any of the :GMF symbols.

Raises:



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/gdal/raster_band.rb', line 241

def create_mask_band(*flags)
  flag_value = 0

  flag_value = flags.each_with_object(flag_value) do |flag, result|
    result + case flag
             when :GMF_ALL_VALID then 0x01
             when :GMF_PER_DATASET then 0x02
             when :GMF_PER_ALPHA then 0x04
             when :GMF_NODATA then 0x08
             else 0
             end
  end

  GDAL::CPLErrorHandler.manually_handle("Unable to create mask band") do
    FFI::GDAL::GDAL.GDALCreateMaskBand(@c_pointer, flag_value)
  end
end

#data_typeSymbol

The pixel data type for this band.

Returns:

  • (Symbol)

    One of FFI::GDAL::GDAL::DataType.



113
114
115
# File 'lib/gdal/raster_band.rb', line 113

def data_type
  FFI::GDAL::GDAL.GDALGetRasterDataType(@c_pointer)
end

#default_histogram(force: false, &block) {|completion, message| ... } ⇒ Hash{minimum => Float, maximum => Float, buckets => Integer, totals => Array<Integer>}

Gets the default raster histogram. Results are returned as a Hash so some metadata about the histogram can be returned. Example:

{
  :minimum => -0.9,
  :maximum => 255.9,
  :buckets => 256,
  :totals => [
    3954, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 0,
    0, 0, 10, 27, 201, 699, 1766, 3472, 5013, 6464, 7698, 8352,
    9039, 10054, 11378, 13132, 14377, 14371, 14221, 14963, 14740,
    14379, 13724, 12938, 11318, 9828, 8504, 7040, 5700, 4890,
    4128, 3276, 2749, 2322, 1944, 1596, 1266, 1050, 784, 663,
    547, 518, 367, 331, 309, 279, 178, 169, 162, 149, 109, 98,
    90, 89, 82, 85, 74, 75, 42, 40, 39, 35, 39, 36, 36, 27, 20,
    12, 13, 19, 16, 12, 11, 6, 6, 8, 12, 6, 8, 11, 3, 7, 9, 2,
    5, 2, 5, 1, 4, 0, 0, 1, 0, 1, 2, 1, 0, 2, 1, 0, 0, 1, 0, 1,
    1, 1, 0, 2, 1, 2, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  ]
}

Also, you can pass a block to get status on the processing. Conforms to FFI::GDAL::GDAL::GDALProgressFunc.

Parameters:

  • force (Boolean) (defaults to: false)

    Forces the computation of the histogram. If false and the default histogram isn’t available, this returns nil.

  • block (Proc)

    No required, but can be used to output progress info during processing.

Yield Parameters:

  • completion (Float)

    The ration completed as a decimal.

  • message (String)

    Message string to display.

Returns:

  • (Hash{minimum => Float, maximum => Float, buckets => Integer, totals => Array<Integer>})

    Returns nil if no default histogram is available.



480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# File 'lib/gdal/raster_band.rb', line 480

def default_histogram(force: false, &block)
  min_pointer = FFI::MemoryPointer.new(:double)
  max_pointer = FFI::MemoryPointer.new(:double)
  buckets_pointer = FFI::MemoryPointer.new(:int)
  histogram_pointer = FFI::MemoryPointer.new(:pointer)
  progress_proc = block || nil

  handler = GDAL::CPLErrorHandler.new
  handler.on_warning = proc {}
  handler.on_none = proc do
    min = min_pointer.read_double
    max = max_pointer.read_double
    buckets = buckets_pointer.read_int

    totals = if buckets.zero?
               []
             else
               histogram_pointer.get_pointer(0).read_array_of_int(buckets)
             end

    {
      minimum: min,
      maximum: max,
      buckets: buckets,
      totals: totals
    }
  end

  handler.custom_handle do
    FFI::GDAL::GDAL.GDALGetDefaultHistogram(
      @c_pointer,
      min_pointer,
      max_pointer,
      buckets_pointer,
      histogram_pointer,
      force,
      progress_proc,
      nil
    )
  end
end

#default_raster_attribute_tableGDAL::RasterAttributeTable



423
424
425
426
427
428
# File 'lib/gdal/raster_band.rb', line 423

def default_raster_attribute_table
  rat_pointer = FFI::GDAL::GDAL.GDALGetDefaultRAT(@c_pointer)
  return nil if rat_pointer.null?

  GDAL::RasterAttributeTable.new(rat_pointer)
end

#default_raster_attribute_table=(rat_table) ⇒ Object

Raises:



431
432
433
434
435
436
437
# File 'lib/gdal/raster_band.rb', line 431

def default_raster_attribute_table=(rat_table)
  rat_table_ptr = GDAL::RasterAttributeTable.new_pointer(rat_table)

  GDAL::CPLErrorHandler.manually_handle("Unable to set default raster attribute table") do
    FFI::GDAL::GDAL.GDALSetDefaultRAT(@c_pointer, rat_table_ptr)
  end
end

#fill(real_value, imaginary_value = 0) ⇒ Object

Fill this band with constant value. Useful for clearing a band and setting to a default value.

Parameters:

  • real_value (Float)
  • imaginary_value (Float) (defaults to: 0)

Raises:



265
266
267
268
269
# File 'lib/gdal/raster_band.rb', line 265

def fill(real_value, imaginary_value = 0)
  GDAL::CPLErrorHandler.manually_handle("Unable to fill raster") do
    FFI::GDAL::GDAL.GDALFillRaster(@c_pointer, real_value, imaginary_value)
  end
end

#flush_cacheObject

Raises:



39
40
41
42
43
# File 'lib/gdal/raster_band.rb', line 39

def flush_cache
  GDAL::CPLErrorHandler.manually_handle("Unable to flush cache") do
    FFI::GDAL::GDAL.GDALFlushRasterCache(@c_pointer)
  end
end

#histogram(min, max, buckets, include_out_of_range: false, approx_ok: false, &block) {|completion, message| ... } ⇒ Hash{minimum => Float, maximum => Float, buckets => Integer, totals => Array<Integer>}

Computes a histogram using the given inputs. If you just want the default histogram, use #default_histogram.

Parameters:

  • min (Float)

    The lower bound of the histogram.

  • max (Float)

    The upper bound of the histogram.

  • buckets (Integer)
  • include_out_of_range (Boolean) (defaults to: false)

    If true, values below the histogram range will be mapped into the first bucket of the output data; values above the range will be mapped into the last bucket. If false, values outside of the range will be discarded.

  • approx_ok (Boolean) (defaults to: false)
  • block (Proc)

    No required, but can be used to output progress info during processing.

Yield Parameters:

  • completion (Float)

    The ration completed as a decimal.

  • message (String)

    Message string to display.

Returns:

See Also:



543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
# File 'lib/gdal/raster_band.rb', line 543

def histogram(min, max, buckets, include_out_of_range: false,
  approx_ok: false, &block)
  histogram_pointer = FFI::MemoryPointer.new(:pointer, buckets)
  progress_proc = block || nil

  handler = GDAL::CPLErrorHandler.new
  handler.on_warning = proc {}
  handler.on_none = proc do
    totals = if buckets.zero?
               []
             else
               histogram_pointer.read_array_of_int(buckets)
             end

    {
      minimum: min,
      maximum: max,
      buckets: buckets,
      totals: totals
    }
  end

  handler.custom_handle do
    FFI::GDAL::GDAL.GDALGetRasterHistogram(@c_pointer,
                                           min.to_f,
                                           max.to_f,
                                           buckets,
                                           histogram_pointer,
                                           include_out_of_range,
                                           approx_ok,
                                           progress_proc,
                                           nil)
  end
end

#mask_bandGDAL::RasterBand

Returns:



210
211
212
213
214
215
# File 'lib/gdal/raster_band.rb', line 210

def mask_band
  band_pointer = FFI::GDAL::GDAL.GDALGetMaskBand(@c_pointer)
  return nil if band_pointer.null?

  self.class.new(band_pointer)
end

#mask_flagsArray<Symbol>

Returns:

  • (Array<Symbol>)


218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/gdal/raster_band.rb', line 218

def mask_flags
  flag_list = FFI::GDAL::GDAL.GDALGetMaskFlags(@c_pointer).to_s(2).scan(/\d/)
  flags = []

  flag_list.reverse_each.with_index do |flag, i|
    flag = flag.to_i

    if i.zero? && flag == 1
      flags << :GMF_ALL_VALID
    elsif i == 1 && flag == 1
      flags << :GMF_PER_DATASET
    elsif i == 2 && flag == 1
      flags << :GMF_ALPHA
    elsif i == 3 && flag == 1
      flags << :GMF_NODATA
    end
  end

  flags
end

#maximum_valueHash{value => Float, is_tight => Boolean}

The maximum value in the band, not counting NODATA values. For file formats that don’t know this intrinsically, the maximum supported value for the data type will generally be returned.

Returns:

  • (Hash{value => Float, is_tight => Boolean})

    The is_tight value tells whether the maximum is a tight maximum.



746
747
748
749
750
751
# File 'lib/gdal/raster_band.rb', line 746

def maximum_value
  is_tight = FFI::MemoryPointer.new(:bool)
  value = FFI::GDAL::GDAL.GDALGetRasterMaximum(@c_pointer, is_tight)

  { value: value, is_tight: is_tight.read_bytes(1).to_bool }
end

#min_max(approx_ok: false) ⇒ Hash{min => Float, max => Float}

The minimum and maximum values for this band.

Returns:



720
721
722
723
724
725
# File 'lib/gdal/raster_band.rb', line 720

def min_max(approx_ok: false)
  min_max = FFI::MemoryPointer.new(:double, 2)
  FFI::GDAL::GDAL.GDALComputeRasterMinMax(@c_pointer, approx_ok, min_max)

  { min: min_max[0].read_double, max: min_max[1].read_double }
end

#minimum_valueHash{value => Float, is_tight => Boolean}

The minimum value in the band, not counting NODATA values. For file formats that don’t know this intrinsically, the minimum supported value for the data type will generally be returned.

Returns:

  • (Hash{value => Float, is_tight => Boolean})

    The is_tight value tells whether the minimum is a tight minimum.



733
734
735
736
737
738
# File 'lib/gdal/raster_band.rb', line 733

def minimum_value
  is_tight = FFI::MemoryPointer.new(:bool)
  value = FFI::GDAL::GDAL.GDALGetRasterMinimum(@c_pointer, is_tight)

  { value: value, is_tight: is_tight.read_bytes(1).to_bool }
end

#no_data_valueHash{value => Float, is_associated => Boolean}

The no data value for a band is generally a special marker value used to mark pixels that are not valid data. Such pixels should generally not be displayed, nor contribute to analysis operations.

Returns:

  • (Hash{value => Float, is_associated => Boolean})


153
154
155
156
157
158
159
# File 'lib/gdal/raster_band.rb', line 153

def no_data_value
  associated = FFI::MemoryPointer.new(:bool)
  value = FFI::GDAL::GDAL.GDALGetRasterNoDataValue(@c_pointer, associated)
  value = nil if value.to_d == BigDecimal("-10_000_000_000.0")

  { value: value, is_associated: associated.read_bytes(1).to_bool }
end

#no_data_value=(value) ⇒ Object

Sets the no data value for this band. Do nothing if attempting to set to nil, because removing a no data value is impossible until GDAL 2.1.

Parameters:

Raises:



166
167
168
169
170
# File 'lib/gdal/raster_band.rb', line 166

def no_data_value=(value)
  GDAL::CPLErrorHandler.manually_handle("Unable to set NODATA value") do
    FFI::GDAL::GDAL.GDALSetRasterNoDataValue(@c_pointer, value)
  end
end

#numberInteger

The number of band within the associated dataset that this band represents.

Returns:



70
71
72
# File 'lib/gdal/raster_band.rb', line 70

def number
  FFI::GDAL::GDAL.GDALGetBandNumber(@c_pointer)
end

#offsetObject

This value (in combination with the #scale value) is used to transform raw pixel values into the units returned by #units. For example this might be used to store elevations in GUInt16 bands with a precision of 0.1, and starting from -100.

Units value = (raw value * scale) + offset.

For file formats that don’t know this intrinsically a value of 0.0 is returned.

Returns:

  • Float

Raises:

  • GDAL::Error if the underlying call fails.



380
381
382
383
384
385
386
387
# File 'lib/gdal/raster_band.rb', line 380

def offset
  success = FFI::MemoryPointer.new(:bool)
  result = FFI::GDAL::GDAL.GDALGetRasterOffset(@c_pointer, success)

  raise GDAL::Error, "GDALGetRasterOffset failed" unless success.read_bytes(1).to_bool

  result
end

#offset=(new_offset) ⇒ Boolean

Sets the scaling offset. Very few formats support this method.

Parameters:

Returns:

  • (Boolean)


393
394
395
396
397
# File 'lib/gdal/raster_band.rb', line 393

def offset=(new_offset)
  GDAL::CPLErrorHandler.manually_handle("Unable to set raster offset") do
    FFI::GDAL::GDAL.GDALSetRasterOffset(@c_pointer, new_offset)
  end
end

#overview(index) ⇒ GDAL::RasterBand

Parameters:

  • index (Integer)

    Must be between 0 and (#overview_count - 1).

Returns:



184
185
186
187
188
189
190
191
# File 'lib/gdal/raster_band.rb', line 184

def overview(index)
  return nil if overview_count.zero?

  overview_pointer = FFI::GDAL::GDAL.GDALGetOverview(@c_pointer, index)
  return nil if overview_pointer.null?

  self.class.new(overview_pointer)
end

#overview_countInteger

Returns:



173
174
175
# File 'lib/gdal/raster_band.rb', line 173

def overview_count
  FFI::GDAL::GDAL.GDALGetOverviewCount(@c_pointer)
end

#raster_io(access_flag, buffer = nil, x_size: nil, y_size: nil, x_offset: 0, y_offset: 0, buffer_x_size: nil, buffer_y_size: nil, buffer_data_type: data_type, pixel_space: 0, line_space: 0) ⇒ FFI::MemoryPointer

IO access for raster data in this band. Default values are set up to operate on one line at a time, keeping the same aspect ratio.

On buffers… You can use different size buffers from the original x and y size to allow for resampling. Using larger buffers will upsample the raster data; smaller buffers will downsample it.

On pixel_space and line_space.… These values control how data is organized in the buffer.

rubocop:disable Metrics/ParameterLists

Parameters:

  • access_flag (Symbol)

    Must be ‘r’ or ‘w’.

  • buffer (FFI::MemoryPointer) (defaults to: nil)

    Allows for passing in your own buffer, which is really only useful when writing.

  • x_size (Integer) (defaults to: nil)

    The number of pixels per line to operate on. Defaults to the value of GDAL::RasterBand.{{#x_size}.

  • y_size (Integer) (defaults to: nil)

    The number of lines to operate on. Defaults to the value of GDAL::RasterBand.{{#y_size}.

  • x_offset (Integer) (defaults to: 0)

    The pixel number in the line to start operating on. Note that when using this, #x_size - x_offset should be >= 0, otherwise this means you’re telling the method to read past the end of the line. Defaults to 0.

  • y_offset (Integer) (defaults to: 0)

    The line number to start operating on. Note that when using this, #y_size - y_offset should be >= 0, otherwise this means you’re telling the method to read more lines than the raster has. Defaults to 0.

  • buffer_x_size (Integer) (defaults to: nil)

    The width of the buffer image in which to read/write the raster data into/from. Typically this should be the same size as x_size; if it’s different, GDAL will resample accordingly.

  • buffer_y_size (Integer) (defaults to: nil)

    The height of the buffer image in which to read/write the raster data into/from. Typically this should be the same size as y_size; if it’s different, GDAL will resample accordingly.

  • buffer_data_type (FFI::GDAL::GDAL::DataType) (defaults to: data_type)

    Can be used to convert the data to a different type. You must account for this when reading/writing to/from your buffer–your buffer size must be buffer_x_size * buffer_y_size. Defaults to GDAL::RasterBand.{{#data_type}.

  • pixel_space (Integer) (defaults to: 0)

    The byte offset from the start of one pixel value in the buffer to the start of the next pixel value within a line. If defaulted (0), the size of buffer_data_type is used.

  • line_space (Integer) (defaults to: 0)

    The byte offset from the start of one line in the buffer to the start of the next. If defaulted (0), the size of buffer_data_type * +buffer_x_size* is used.

Returns:

  • (FFI::MemoryPointer)

    Pointer to the data that was read/written.



647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
# File 'lib/gdal/raster_band.rb', line 647

def raster_io(access_flag, buffer = nil,
  x_size: nil, y_size: nil, x_offset: 0, y_offset: 0,
  buffer_x_size: nil, buffer_y_size: nil, buffer_data_type: data_type,
  pixel_space: 0, line_space: 0)
  return unless @c_pointer

  x_size ||= self.x_size
  y_size ||= self.y_size

  buffer_x_size ||= x_size
  buffer_y_size ||= y_size
  buffer ||= GDAL._pointer_from_data_type(buffer_data_type, buffer_x_size * buffer_y_size)

  FFI::GDAL::GDAL.GDALRasterIO(
    @c_pointer,
    GDAL._gdal_access_flag(access_flag),
    x_offset,
    y_offset,
    x_size,
    y_size,
    buffer,
    buffer_x_size,
    buffer_y_size,
    buffer_data_type,
    pixel_space,
    line_space
  )

  buffer
end

#raster_sample_overview(desired_samples = 0) ⇒ GDAL::RasterBand

Returns the most reduced overview of this RasterBand that still satisfies the deisred number of samples. Using 0 fetches the most reduced overview. If the band doesn’t have any overviews or none of the overviews have enough samples, it will return the same band.

Parameters:

  • desired_samples (Integer) (defaults to: 0)

    The returned band will have at least this many pixels.

Returns:

  • (GDAL::RasterBand)

    An optimal overview or the same raster band if the raster band has no overviews.



202
203
204
205
206
207
# File 'lib/gdal/raster_band.rb', line 202

def raster_sample_overview(desired_samples = 0)
  band_pointer = FFI::GDAL::GDAL.GDALGetRasterSampleOverview(@c_pointer, desired_samples)
  return nil if band_pointer.null?

  self.class.new(band_pointer)
end

#read_block(x_block_number, y_block_number, image_buffer = nil) ⇒ FFI::MemoryPointer

Read a block of image data, more efficiently than #read. Doesn’t resample or do data type conversion.

Parameters:

  • x_block_number (Integer)

    The horizontal block offset, with 0 indicating the left-most block, 1 the next block, etc.

  • y_block_number (Integer)

    The vertical block offset, with 0 indicating the top-most block, 1 the next block, etc.

  • image_buffer (FFI::Pointer) (defaults to: nil)

    Optional pointer to use for reading the data into. If not provided, one will be created and returned.

Returns:

  • (FFI::MemoryPointer)

    The image buffer that contains the read data. If you passed in image_buffer you don’t need to bother with this return value since that original buffer will contain the data.



691
692
693
694
695
696
697
698
699
# File 'lib/gdal/raster_band.rb', line 691

def read_block(x_block_number, y_block_number, image_buffer = nil)
  image_buffer ||= FFI::MemoryPointer.new(:buffer_out, block_buffer_size)

  GDAL::CPLErrorHandler.manually_handle("Unable to read block") do
    FFI::GDAL::GDAL.GDALReadBlock(@c_pointer, x_block_number, y_block_number, image_buffer)
  end

  image_buffer
end

#scaleObject

The raster value scale. This value (in combination with the #offset value) is used to transform raw pixel values into the units returned by #units. For example this might be used to store elevations in GUInt16 bands with a precision of 0.1, and starting from -100.

Units value = (raw value * scale) + offset

For file formats that don’t know this intrinsically a value of one is returned.

Returns:

  • Float

Raises:

  • GDAL::Error if the underlying call fails.



351
352
353
354
355
356
357
358
# File 'lib/gdal/raster_band.rb', line 351

def scale
  success = FFI::MemoryPointer.new(:bool)
  result = FFI::GDAL::GDAL.GDALGetRasterScale(@c_pointer, success)

  raise GDAL::Error, "GDALGetRasterScale failed" unless success.read_bytes(1).to_bool

  result
end

#scale=(new_scale) ⇒ Object

Parameters:

Raises:



362
363
364
365
366
# File 'lib/gdal/raster_band.rb', line 362

def scale=(new_scale)
  GDAL::CPLErrorHandler.manually_handle("Unable to set raster scale") do
    FFI::GDAL::GDAL.GDALSetRasterScale(@c_pointer, new_scale.to_f)
  end
end

#statistics(approx_ok: true, force: true) ⇒ Hash{minimum: Float, maximum: Float, mean: Float, standard_deviation: Float}

Returns minimum, maximum, mean, and standard deviation of all pixel values in this band.

Parameters:

  • approx_ok (Boolean) (defaults to: true)

    If true, stats may be computed based on overviews or a subset of all tiles.

  • force (Boolean) (defaults to: true)

    If false, stats will only be returned if the calculating can be done without rescanning the image.

Returns:



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/gdal/raster_band.rb', line 280

def statistics(approx_ok: true, force: true)
  min = FFI::MemoryPointer.new(:double)
  max = FFI::MemoryPointer.new(:double)
  mean = FFI::MemoryPointer.new(:double)
  standard_deviation = FFI::MemoryPointer.new(:double)

  handler = GDAL::CPLErrorHandler.new
  handler.on_warning = proc { {} }
  handler.on_none = proc do
    {
      minimum: min.read_double,
      maximum: max.read_double,
      mean: mean.read_double,
      standard_deviation: standard_deviation.read_double
    }
  end

  handler.custom_handle do
    FFI::GDAL::GDAL.GDALGetRasterStatistics(@c_pointer,
                                            approx_ok,
                                            force,
                                            min,
                                            max,
                                            mean,
                                            standard_deviation)
  end
end

#unit_typeString

Returns:



400
401
402
403
404
405
406
# File 'lib/gdal/raster_band.rb', line 400

def unit_type
  # The returned string should not be modified, nor freed by the calling application.
  type, ptr = FFI::GDAL::GDAL.GDALGetRasterUnitType(@c_pointer)
  ptr.autorelease = false

  type
end

#unit_type=(new_unit_type) ⇒ Object

Parameters:

  • new_unit_type (String)

    “” indicates unknown, “m” is meters, “ft” is feet; other non-standard values are allowed.

Raises:



411
412
413
414
415
416
417
418
419
420
# File 'lib/gdal/raster_band.rb', line 411

def unit_type=(new_unit_type)
  unless defined? FFI::GDAL::GDAL::GDALSetRasterUnitType
    warn "GDALSetRasterUnitType is not defined.  Can't call RasterBand#unit_type="
    return
  end

  GDAL::CPLErrorHandler.manually_handle("Unable to set unit type") do
    FFI::GDAL::GDAL.GDALSetRasterUnitType(@c_pointer, new_unit_type)
  end
end

#write_block(x_block_number, y_block_number, data_pointer = nil) ⇒ Object

Parameters:

  • x_block_number (Integer)

    The horizontal block offset, with 0 indicating the left-most block, 1 the next block, etc.

  • y_block_number (Integer)

    The vertical block offset, with 0 indicating the top-most block, 1 the next block, etc.

  • data_pointer (FFI::Pointer) (defaults to: nil)

    Optional pointer to write the data to. If not provided, one will be created and returned.



707
708
709
710
711
712
713
714
715
# File 'lib/gdal/raster_band.rb', line 707

def write_block(x_block_number, y_block_number, data_pointer = nil)
  data_pointer ||= FFI::Buffer.alloc_inout(block_buffer_size)

  GDAL::CPLErrorHandler.manually_handle("Unable to write block") do
    FFI::GDAL::GDAL.GDALWriteBlock(@c_pointer, x_block_number, y_block_number, data_pointer)
  end

  data_pointer
end

#x_sizeInteger

The raster width in pixels.

Returns:



48
49
50
# File 'lib/gdal/raster_band.rb', line 48

def x_size
  FFI::GDAL::GDAL.GDALGetRasterBandXSize(@c_pointer)
end

#y_sizeInteger

The raster height in pixels.

Returns:



55
56
57
# File 'lib/gdal/raster_band.rb', line 55

def y_size
  FFI::GDAL::GDAL.GDALGetRasterBandYSize(@c_pointer)
end