Class: Proj::Options

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

Overview

Wraps a hash of key-value pairs as a null-terminated array of “KEY=VALUE” C string pointers suitable for PROJ option parameters.

Many PROJ C API functions accept an optional array of “KEY=VALUE” strings to control their behavior. This class builds the required pointer array and keeps the underlying FFI memory alive so that the garbage collector cannot free it before the C call returns.

Examples:

options = Proj::Options.new("MULTILINE": "YES", "INDENTATION_WIDTH": 4)
Api.proj_as_wkt(context, pj, :PJ_WKT2_2019, options)

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Options

Creates a new Options instance.

Parameters:

  • options (Hash{String, Symbol => String, Integer, nil}) (defaults to: {})

    Key-value pairs. Entries with nil values are removed.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/proj/options.rb', line 18

def initialize(options = {})
  options = options.compact
  @string_ptrs = options.map do |key, value|
    FFI::MemoryPointer.from_string("#{key}=#{value}")
  end

  if @string_ptrs.empty?
    @pointer = nil
  else
    @pointer = FFI::MemoryPointer.new(:pointer, options.size + 1)
    # PROJ expects a NULL-terminated char* array.
    @pointer.write_array_of_pointer(@string_ptrs + [FFI::Pointer::NULL])
  end
end

Instance Method Details

#to_ptrFFI::Pointer

Returns the pointer to the null-terminated string array, or a NULL pointer if no options were provided.

Returns:

  • (FFI::Pointer)


37
38
39
# File 'lib/proj/options.rb', line 37

def to_ptr
  @pointer || FFI::Pointer::NULL
end