Class: GDAL::Utils::Translate

Inherits:
Object
  • Object
show all
Defined in:
lib/gdal/utils/translate.rb,
lib/gdal/utils/translate/options.rb

Overview

Wrapper for gdal_translate using GDALTranslate C API.

Defined Under Namespace

Classes: Options

Class Method Summary collapse

Class Method Details

.perform(dst_dataset_path:, src_dataset:, options: Options.new) {|GDAL::Dataset| ... } ⇒ GDAL::Dataset

Perform the gdal_translate (GDALTranslate) operation.

Examples:

Translate a dataset.

src_dataset = GDAL::Dataset.open("source.tif", "r")
dataset = GDAL::Utils::Translate.perform(dst_dataset_path: "destination.tif", src_dataset: src_dataset)

# Do something with the dataset.
puts dataset.raster_x_size

# You must close the dataset when you are done with it.
dataset.close
src_dataset.close

Translate a dataset with options.

src_dataset = GDAL::Dataset.open("source.tif", "r")
dataset = GDAL::Utils::Translate.perform(
  dst_dataset_path: "destination.tif",
  src_dataset: src_dataset,
  options: GDAL::Utils::Translate::Options.new(options: ["-of", "GTiff", "-co", "COMPRESS=DEFLATE"])
)

# Do something with the dataset.
puts dataset.raster_x_size

# You must close the dataset when you are done with it.
dataset.close
src_dataset.close

Translate a dataset using block syntax.

src_dataset = GDAL::Dataset.open("source.tif", "r")
GDAL::Utils::Translate.perform(dst_dataset_path: "destination.tif", src_dataset: src_dataset) do |dataset|
  # Do something with the dataset.
  puts dataset.raster_x_size

  # Dataset will be closed automatically.
end
src_dataset.close

Yields:

Raises:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/gdal/utils/translate.rb', line 56

def self.perform(dst_dataset_path:, src_dataset:, options: Options.new, &block)
  result_code_ptr = ::FFI::MemoryPointer.new(:int)
  dst_dataset_ptr = ::FFI::GDAL::Utils.GDALTranslate(
    dst_dataset_path,
    src_dataset.c_pointer,
    options.c_pointer,
    result_code_ptr
  )
  success = result_code_ptr.read_int.zero?

  raise ::GDAL::Error, "GDALTranslate failed." if dst_dataset_ptr.null? || !success

  ::GDAL::Dataset.open(dst_dataset_ptr, "w", &block)
end