Class: GDAL::Options

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

Overview

A wrapper for the way GDAL does key/value pair options for methods.

Class Method Summary collapse

Class Method Details

.pointer(hash) ⇒ FFI::MemoryPointer?

Shortcut for if you just want to build the options and get the pointer to them.

Parameters:

  • hash (Hash)

Returns:

  • (FFI::MemoryPointer, nil)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/gdal/options.rb', line 13

def self.pointer(hash)
  return if hash.empty?

  options_ptr = FFI::MemoryPointer.new(:pointer)
  options_ptr.autorelease = false

  hash.each do |key, value|
    # Note, we started off with a MemoryPointer above, but this returns a
    # new FFI::Pointer.
    options_ptr = FFI::CPL::String.CSLAddNameValue(options_ptr, key.to_s.upcase, value.to_s)
    options_ptr.autorelease = false
  end

  FFI::AutoPointer.new(options_ptr, Options.method(:release))
end

.release(pointer) ⇒ Object

Parameters:

  • pointer (FFI::Pointer)


48
49
50
51
52
# File 'lib/gdal/options.rb', line 48

def self.release(pointer)
  return if pointer.nil? || pointer.null?

  FFI::CPL::String.CSLDestroy(pointer)
end

.to_hash(pointer) ⇒ Hash

Takes a GDAL options pointer and turns it into a Ruby Hash.

Parameters:

  • pointer (FFI::Pointer)

Returns:

  • (Hash)


33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/gdal/options.rb', line 33

def self.to_hash(pointer)
  # Docs say passing a null pointer here is ok; will result in 0.
  count = FFI::CPL::String.CSLCount(pointer)

  count.times.each_with_object({}) do |i, o|
    # Docs say that the pointer returned from CSLGetField shouldn't be freed.
    key_and_value, ptr = FFI::CPL::String.CSLGetField(pointer, i)
    ptr.autorelease = false

    key, value = key_and_value.split("=")
    o[key.downcase.to_sym] = value
  end
end