Module: GDAL::RasterBandMixins::AlgorithmMethods

Included in:
GDAL::RasterBand
Defined in:
lib/gdal/raster_band_mixins/algorithm_methods.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
# File 'lib/gdal/raster_band_mixins/algorithm_methods.rb', line 6

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#checksum_image(x_offset, y_offset, x_size, y_size) ⇒ Integer

Computes a 16-bit (0-65535) checksum from a region of raster data. Floating point data is converted to 32-bit integers so decimal portions of the raster data won’t affect the checksum. Real and imaginary components of complex bands influence the result.

Parameters:

Returns:

  • (Integer)

    The checksum value.



98
99
100
101
102
103
104
105
106
# File 'lib/gdal/raster_band_mixins/algorithm_methods.rb', line 98

def checksum_image(x_offset, y_offset, x_size, y_size)
  !!FFI::GDAL::Alg.GDALChecksumImage(
    @c_pointer,
    x_offset,
    y_offset,
    x_size,
    y_size
  )
end

#compute_proximity!(proximity_band, progress_function: nil, progress_arg: nil, **options) ⇒ Object

Computes the proximity of all pixels in the proximity_band to those in this band. By default all non-zero pixels in this band will be considered as “target”, and all proximities will be computed in pixels. Target pixels are set to the value corresponding to a distance of zero.

Note that this modifies the source band in place with the computed values.

Parameters:

  • proximity_band (GDAL::RasterBand, FFI::Pointer)
  • progress_function (Proc, FFI:GDAL::GDAL.ProgressFunc) (defaults to: nil)
  • progress_arg (FFI::Pointer) (defaults to: nil)

    Usually used when when using a FFI::CPL::Progress.GDALCreateScaledProgress.

  • options (Hash)

Options Hash (**options):

  • values (String)

    A list of target pixel values to measure the distance from. If this isn’t provided, proximity will be computed from non-zero pixel values.

  • distunits (String) — default: PIXEL

    Indicates what unit type to use for computing.

  • maxdist (Integer)

    The maximum distance to search.

  • nodata (Integer)

    If not given, it will try to use the nodata value on the proximity_band. If not found there, will use 65535.

  • fixed_buf_val (Integer)

    If set, all pixels within the maxdist threshold are set to this fixed value instead of to a proximity distance.



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/gdal/raster_band_mixins/algorithm_methods.rb', line 133

def compute_proximity!(proximity_band, progress_function: nil, progress_arg: nil, **options)
  proximity_band_ptr = GDAL._pointer(GDAL::RasterBand, proximity_band)
  options_ptr = GDAL::Options.pointer(options)

  FFI::GDAL::Alg.GDALComputeProximity(
    @c_pointer,
    proximity_band_ptr,
    options_ptr,
    progress_function,
    progress_arg
  )
end

#fill_nodata!(mask_band, max_search_distance, smoothing_iterations, progress_function: nil, progress_arg: nil, **options) ⇒ Object

Fill selected raster regions by interpolation from the edges. It interpolates values for all designated nodata pixels (marked by zeroes in mask_band). For each pixel, a four-direction conic search is done to find values to interpolate from (using inverse distance weighting). Once all values are interpolated, zero or more smoothing iterations (3x3 average filters on interpolated pixels) are applied to smooth out artifacts.

This is generally suitable for interpolating missing regions of fairly continuously varying rasters (such as elevation models, for instance). It is also suitable for filling small holes and cracks in more irregularly varying images (like aerial photos). Its is generally not so great for interpolating a raster from sparse point data. See GDAL::Grid for that case.

Note that this alters values of the current raster band.

TODO: document what valid options are.

Parameters:

  • mask_band (GDAL::RasterBand)

    Band that indicates which pixels to be interpolated (it does so using 0-valued pixels).

  • max_search_distance (Float)

    Max number of pixels to search in all directions to find values to interpolate from.

  • smoothing_iterations (Integer)

    The number of 3x3 smoothing filter passes to run. Can be 0.

  • progress_function (Proc, FFI:GDAL::GDAL.ProgressFunc) (defaults to: nil)
  • progress_arg (FFI::Pointer) (defaults to: nil)

    Usually used when when using a FFI::CPL::Progress.GDALCreateScaledProgress.

  • options (Hash)


174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/gdal/raster_band_mixins/algorithm_methods.rb', line 174

def fill_nodata!(mask_band, max_search_distance, smoothing_iterations, progress_function: nil, progress_arg: nil,
  **options)
  mask_band_ptr = GDAL._pointer(GDAL::RasterBand, mask_band)
  options_ptr = GDAL::Options.pointer(options)

  !!FFI::GDAL::Alg.GDALFillNodata(@c_pointer,
                                  mask_band_ptr,
                                  max_search_distance,
                                  0, # deprecated option in GDAL
                                  smoothing_iterations,
                                  options_ptr,
                                  progress_function,
                                  progress_arg)
end

#polygonize(layer, mask_band: nil, pixel_value_field: -1,, use_integer_function: false, progress_function: nil, progress_arg: nil, **options) ⇒ OGR::Layer

Creates vector polygons for all connected regions of pixels in the raster that share a common pixel value. Optionally, each polygon may be labeled with the pixel value in an attribute. Optionally, a mask band can be provided to determine which pixels are eligible for processing.

The C API implements two functions for this: GDALPolygonize and GDALFPolygonize, where the former uses a 32-bit Integer buffer and the latter uses a 32-bit Float buffer. The Integer version may be quicker, but the Float version more accurate. As such, calling polygonize defaults to use the Float version internally, but you can tell it to use the Integer version by using the use_integer_function flag.

Polygon features will be created on layer with Polygon geometries representing the polygons. The geometries will be in the georeferenced coordinate system of the image (based on the GeoTransform) of the source Dataset). It is acceptable for layer to already have other features.

Note that this does not set the coordinate system on the output layer–the application is responsible for doing so.

Parameters:

  • layer (OGR::Layer, FFI::Pointer)

    The layer to write the polygons to.

  • mask_band (GDAL::RasterBand, FFI::Pointer) (defaults to: nil)

    Optional band, where all pixels in the mask with a value other than zero will be considered suitable for collection as polygons.

  • pixel_value_field (Integer) (defaults to: -1,)

    Index of the feature attribute into which the pixel value of the polygon should be written.

  • use_integer_function (Boolean) (defaults to: false)

    Indicates using GDAL’s GDALPolygonize() instead of GDALFPolygonize(); the former uses a 32-bit integer buffer for reading pixel band values, the latter uses a 32-bit float buffer. The integer based function is faster but less precise.

  • options (Hash)
  • progress_function (Proc, FFI:GDAL::GDAL.ProgressFunc) (defaults to: nil)
  • progress_arg (FFI::Pointer) (defaults to: nil)

    Usually used when when using a FFI::CPL::Progress.GDALCreateScaledProgress.

Options Hash (**options):

  • '8CONNECTED' (Integer) — default: 4

    Set to 8 to use 8 connectedness.

Returns:

Raises:



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/gdal/raster_band_mixins/algorithm_methods.rb', line 228

def polygonize(layer, mask_band: nil, pixel_value_field: -1, use_integer_function: false, progress_function: nil,
  progress_arg: nil, **options)
  mask_band_ptr = GDAL._pointer(GDAL::RasterBand, mask_band, warn_on_nil: false)
  layer_ptr = GDAL._pointer(OGR::Layer, layer)
  raise OGR::InvalidLayer, "Invalid layer: #{layer.inspect}" if layer_ptr.null?

  log "Pixel value field: #{pixel_value_field}"

  options_ptr = GDAL::Options.pointer(options)

  function = use_integer_function ? :GDALPolygonize : :GDALFPolygonize

  FFI::GDAL::Alg.send(
    function,
    @c_pointer,             # hSrcBand
    mask_band_ptr,          # hMaskBand
    layer_ptr,              # hOutLayer
    pixel_value_field,      # iPixValField
    options_ptr,            # papszOptions
    progress_function,      # pfnProgress
    progress_arg            # pProgressArg
  )

  layer_ptr.instance_of?(OGR::Layer) ? layer_ptr : OGR::Layer.new(layer_ptr)
end

#sieve_filter(size_threshold, connectedness, destination_band, mask_band: nil, progress_function: nil, progress_arg: nil, **options) ⇒ Object

The same as sieve_filter!, but returns a new GDAL::RasterBand as the result.

Parameters:

See Also:

  • +sieve_filter!


287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/gdal/raster_band_mixins/algorithm_methods.rb', line 287

def sieve_filter(size_threshold, connectedness, destination_band, mask_band: nil, progress_function: nil,
  progress_arg: nil, **options)
  _sieve_filter(size_threshold, connectedness, destination_band, mask_band: mask_band,
                                                                 progress_function: progress_function,
                                                                 progress_arg: progress_arg,
                                                                 **options)

  if destination_band.is_a? GDAL::RasterBand
    destination_band
  else
    GDAL::RasterBand.new(destination_band)
  end
end

#sieve_filter!(size_threshold, connectedness, mask_band: nil, progress_function: nil, progress_arg: nil, **options) ⇒ Object

Removes raster polygons that are smaller than the given threshold (in pixels) and replaces them with the pixel value of the largest neighbor polygon. Polygons are determined as regions of the raster where the pixels all have the same value, and that are contiguous (connected).

If mask_band is given, “nodata” pixels in the band will not be treated as part of a polygon, regardless of their pixel values.

Parameters:

  • size_threshold (Integer)

    Polygons found in the raster with sizes smaller than this will be merged into their largest neighbor.

  • connectedness (Integer)

    4 or 8. 4 indicates that diagonal pixels are not considered directly adjacent for polygon membership purposes; 8 indicates they are.

  • mask_band (GDAL::RasterBand) (defaults to: nil)
    description

    All pixels in this

    band with a value other than 0 will be considered suitable for inclusion in polygons.

  • progress_function (Proc, FFI:GDAL::GDAL.ProgressFunc) (defaults to: nil)
  • progress_arg (FFI::Pointer) (defaults to: nil)

    Usually used when when using a FFI::CPL::Progress.GDALCreateScaledProgress.

  • options (Hash)

    None supported in GDAL as of this writing.



274
275
276
277
278
279
280
# File 'lib/gdal/raster_band_mixins/algorithm_methods.rb', line 274

def sieve_filter!(size_threshold, connectedness, mask_band: nil, progress_function: nil, progress_arg: nil,
  **options)
  _sieve_filter(size_threshold, connectedness, self, mask_band: mask_band,
                                                     progress_function: progress_function,
                                                     progress_arg: progress_arg,
                                                     **options)
end