Class: OGR::CoordinateTransformation

Inherits:
Object
  • Object
show all
Defined in:
lib/ogr/coordinate_transformation.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_srs, destination_srs) ⇒ CoordinateTransformation

Returns a new instance of CoordinateTransformation.

Parameters:

Raises:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ogr/coordinate_transformation.rb', line 38

def initialize(source_srs, destination_srs)
  source_ptr = GDAL._pointer(OGR::SpatialReference, source_srs)
  destination_ptr = GDAL._pointer(OGR::SpatialReference, destination_srs)

  # Input spatial reference system objects are assigned by copy (calling clone() method)
  # and no ownership transfer occurs.
  # NOTE: In GDAL 3, this will cause the GDAL error handler to raise a
  # GDAL::Error; in < 3, this just returns a null pointer, then gets handled
  # by the null-pointer check below.
  pointer = FFI::OGR::SRSAPI.OCTNewCoordinateTransformation(source_ptr, destination_ptr)

  raise GDAL::Error, "Unable to create coordinate transformation" if pointer.null?

  @c_pointer = pointer
end

Instance Attribute Details

#c_pointerFFI::Pointer (readonly)

Returns C pointer that represents the CoordinateTransformation.

Returns:

  • (FFI::Pointer)

    C pointer that represents the CoordinateTransformation.



34
35
36
# File 'lib/ogr/coordinate_transformation.rb', line 34

def c_pointer
  @c_pointer
end

#destination_coordinate_systemOGR::SpatialReference (readonly)



31
32
33
# File 'lib/ogr/coordinate_transformation.rb', line 31

def destination_coordinate_system
  @destination_coordinate_system
end

#source_coordinate_systemOGR::SpatialReference (readonly)



28
29
30
# File 'lib/ogr/coordinate_transformation.rb', line 28

def source_coordinate_system
  @source_coordinate_system
end

Class Method Details

.proj4_normalize(proj4_source) ⇒ String

Parameters:

Returns:



11
12
13
14
15
16
17
18
# File 'lib/ogr/coordinate_transformation.rb', line 11

def self.proj4_normalize(proj4_source)
  if GDAL._supported?(:OCTProj4Normalize)
    FFI::GDAL::GDAL.OCTProj4Normalize(proj4_source)
  else
    raise OGR::UnsupportedOperation,
          "Your version of GDAL/OGR does not support OCTProj4Normalize"
  end
end

.release(pointer) ⇒ Object

Parameters:

  • pointer (FFI::Pointer)


21
22
23
24
25
# File 'lib/ogr/coordinate_transformation.rb', line 21

def self.release(pointer)
  return unless pointer && !pointer.null?

  FFI::OGR::SRSAPI.OCTDestroyCoordinateTransformation(pointer)
end

Instance Method Details

#destroy!Object

Deletes the object and deallocates all related C resources.



55
56
57
58
59
# File 'lib/ogr/coordinate_transformation.rb', line 55

def destroy!
  CoordinateTransformation.release(@c_pointer)

  @c_pointer = nil
end

#transform(x_vertices, y_vertices, z_vertices = []) ⇒ Array<Array<Float>,Array<Float>,Array<Float>>

Transforms points in the #source_coordinate_system space to points in the #destination_coordinate_system (given in #initialize).

Parameters:

  • x_vertices (Array<Float>)
  • y_vertices (Array<Float>)
  • z_vertices (Array<Float>) (defaults to: [])

Returns:

  • (Array<Array<Float>,Array<Float>,Array<Float>>)
    [x1, x2, etc], [y1, y2, etc]

    Will include a 3rd array of Z values if z vertices are given.



69
70
71
72
73
# File 'lib/ogr/coordinate_transformation.rb', line 69

def transform(x_vertices, y_vertices, z_vertices = [])
  _transform(x_vertices, y_vertices, z_vertices) do |point_count, x_ptr, y_ptr, z_ptr|
    FFI::OGR::SRSAPI.OCTTransform(@c_pointer, point_count, x_ptr, y_ptr, z_ptr)
  end
end

#transform_ex(x_vertices, y_vertices, z_vertices = []) ⇒ Hash{points => Array<Array<Float>,Array<Float>,Array<Float>>, success_at => Array}

Returns [[x1, y1], [x2, y2], etc].

Parameters:

  • x_vertices (Array<Float>)
  • y_vertices (Array<Float>)
  • z_vertices (Array<Float>) (defaults to: [])

Returns:

  • (Hash{points => Array<Array<Float>,Array<Float>,Array<Float>>, success_at => Array})
    [x1, y1], [x2, y2], etc


80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ogr/coordinate_transformation.rb', line 80

def transform_ex(x_vertices, y_vertices, z_vertices = [])
  success_ptr = nil

  point_array = _transform(x_vertices, y_vertices, z_vertices) do |point_count, x_ptr, y_ptr, z_ptr|
    success_ptr = FFI::MemoryPointer.new(:bool, point_count)
    FFI::OGR::SRSAPI.OCTTransformEx(@c_pointer, point_count, x_ptr, y_ptr, z_ptr, success_ptr)
  end

  successes = success_ptr.read_array_of_type(FFI::Type::BOOL, :read_char, point_array.first.length).map do |value|
    !value.zero?
  end

  { points: point_array, successes: successes }
end