Class: GDAL::GeoTransform

Inherits:
Object
  • Object
show all
Includes:
Extensions
Defined in:
lib/gdal/geo_transform.rb,
lib/gdal/extensions/geo_transform/extensions.rb

Defined Under Namespace

Modules: Extensions

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Extensions

included, #to_a, #world_to_pixel, #world_to_x_pixel, #world_to_y_pixel

Constructor Details

#initialize(geo_transform = nil) ⇒ GeoTransform

Returns a new instance of GeoTransform.

Parameters:

  • geo_transform (FFI::Pointer) (defaults to: nil)


34
35
36
37
38
39
40
41
# File 'lib/gdal/geo_transform.rb', line 34

def initialize(geo_transform = nil)
  @c_pointer = geo_transform ? GDAL._pointer(GDAL::GeoTransform, geo_transform) : self.class.new_pointer

  self.pixel_width ||= 1.0
  self.pixel_height ||= 1.0
  self.x_rotation ||= 0.0
  self.y_rotation ||= 0.0
end

Instance Attribute Details

#c_pointerFFI::Pointer (readonly)

Returns C pointer to the C geo-transform.

Returns:

  • (FFI::Pointer)

    C pointer to the C geo-transform.



13
14
15
# File 'lib/gdal/geo_transform.rb', line 13

def c_pointer
  @c_pointer
end

Class Method Details

.from_world_file(filename, extension = nil) ⇒ GDAL::GeoTransform

Parameters:

  • filename (String)
  • extension (String) (defaults to: nil)

    The file extension to use. When nil, GDAL will try to derive it from the filename.

Returns:



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

def self.from_world_file(filename, extension = nil)
  gt_ptr = new_pointer

  result = if extension
             FFI::GDAL::GDAL.GDALReadWorldFile(filename, extension, gt_ptr)
           else
             FFI::GDAL::GDAL.GDALLoadWorldFile(filename, gt_ptr)
           end

  return nil unless result

  new(gt_ptr)
end

.new_pointerFFI::MemoryPointer

Returns A pointer that can be used to hold a GeoTransform.

Returns:

  • (FFI::MemoryPointer)

    A pointer that can be used to hold a GeoTransform.



8
9
10
# File 'lib/gdal/geo_transform.rb', line 8

def self.new_pointer
  FFI::MemoryPointer.new(:double, 6)
end

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
54
# File 'lib/gdal/geo_transform.rb', line 45

def ==(other)
  return false unless other.is_a?(GDAL::GeoTransform)

  x_origin == other.x_origin &&
    pixel_width == other.pixel_width &&
    x_rotation == other.x_rotation &&
    y_origin == other.y_origin &&
    y_rotation == other.y_rotation &&
    pixel_height == other.pixel_height
end

#apply_geo_transform(pixel, line) ⇒ Hash{x_geo => Float, y_geo => Float} Also known as: pixel_to_world

Converts a (pixel, line) coordinate to a georeferenced (geo_x, geo_y) location. Uses the following algorithm:

geo_x = x_origin + (pixel * pixel_width) + (line * pixel_rotation)
geo_y = y_origin + (pixel * y_rotation) + (line * pixel_height)

This is also the same as doing:

geo_transform.invert.world_to_pixel(pixel, line)

# easting/longitude; :y_geo is the northing/latitude.

Parameters:

  • pixel (Float)

    Input pixel position.

  • line (Float)

    Input line position.

Returns:

  • (Hash{x_geo => Float, y_geo => Float})

    :x_geo is the



164
165
166
167
168
169
170
# File 'lib/gdal/geo_transform.rb', line 164

def apply_geo_transform(pixel, line)
  geo_x_ptr = FFI::MemoryPointer.new(:double)
  geo_y_ptr = FFI::MemoryPointer.new(:double)
  FFI::GDAL::GDAL.GDALApplyGeoTransform(@c_pointer, pixel, line, geo_x_ptr, geo_y_ptr)

  { x_geo: geo_x_ptr.read_double, y_geo: geo_y_ptr.read_double }
end

#compose(other_geo_transform) ⇒ GDAL::GeoTransform

Composes this and the given geo_transform. The resulting GeoTransform is equivalent to applying both GeoTransforms to a point.

Parameters:

Returns:

Raises:



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/gdal/geo_transform.rb', line 178

def compose(other_geo_transform)
  other_ptr = GDAL._pointer(GDAL::GeoTransform, other_geo_transform)

  raise GDAL::NullObject, "Unable to access pointer for '#{other_geo_transform}'" unless other_ptr

  new_gt_ptr = self.class.new_pointer
  FFI::GDAL::GDAL.GDALComposeGeoTransforms(@c_pointer, other_ptr, new_gt_ptr)
  return nil if new_gt_ptr.null?

  GDAL::GeoTransform.new(new_gt_ptr)
end

#invertGDAL::GeoTransform

Inverts the current 3x2 set of coefficients and returns a new GeoTransform. Useful for converting from the GeoTransform equation from pixel to geo to being geo to pixel.

Returns:



195
196
197
198
199
200
201
# File 'lib/gdal/geo_transform.rb', line 195

def invert
  new_geo_transform_ptr = self.class.new_pointer
  success = FFI::GDAL::GDAL.GDALInvGeoTransform(@c_pointer, new_geo_transform_ptr)
  return nil unless success

  self.class.new(new_geo_transform_ptr)
end

#null?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/gdal/geo_transform.rb', line 56

def null?
  @c_pointer.null?
end

#pixel_heightFloat

AKA Y-pixel size. In wikipedia’s World Map definition, this is “E”.

Returns:



139
140
141
142
143
# File 'lib/gdal/geo_transform.rb', line 139

def pixel_height
  return nil if null?

  @c_pointer[5].read_double
end

#pixel_height=(new_pixel_height) ⇒ Object

Parameters:

  • new_pixel_height (Float)


146
147
148
# File 'lib/gdal/geo_transform.rb', line 146

def pixel_height=(new_pixel_height)
  @c_pointer[5].write_double(new_pixel_height)
end

#pixel_widthFloat

AKA X-pixel size. In wikipedia’s World Map definition, this is “A”.

Returns:



79
80
81
82
83
# File 'lib/gdal/geo_transform.rb', line 79

def pixel_width
  return nil if null?

  @c_pointer[1].read_double
end

#pixel_width=(new_pixel_width) ⇒ Object

Parameters:

  • new_pixel_width (Float)


86
87
88
# File 'lib/gdal/geo_transform.rb', line 86

def pixel_width=(new_pixel_width)
  @c_pointer[1].write_double(new_pixel_width)
end

#to_world_file(raster_filename, world_extension) ⇒ Boolean

Parameters:

  • raster_filename (String)

    The target raster file.

  • world_extension (String)

Returns:

  • (Boolean)


206
207
208
# File 'lib/gdal/geo_transform.rb', line 206

def to_world_file(raster_filename, world_extension)
  FFI::GDAL::GDAL.GDALWriteWorldFile(raster_filename, world_extension, @c_pointer)
end

#x_originFloat

X-coordinate of the center of the upper left pixel. In wikipedia’s World Map definition, this is “C”.

Returns:



64
65
66
67
68
# File 'lib/gdal/geo_transform.rb', line 64

def x_origin
  return nil if null?

  @c_pointer[0].read_double
end

#x_origin=(new_x_origin) ⇒ Object

Parameters:



71
72
73
# File 'lib/gdal/geo_transform.rb', line 71

def x_origin=(new_x_origin)
  @c_pointer[0].write_double(new_x_origin)
end

#x_rotationFloat

Rotation about the x-axis. In wikipedia’s World File definition, this is “B”.

Returns:



94
95
96
97
98
# File 'lib/gdal/geo_transform.rb', line 94

def x_rotation
  return nil if null?

  @c_pointer[2].read_double
end

#x_rotation=(new_x_rotation) ⇒ Object

Parameters:



101
102
103
# File 'lib/gdal/geo_transform.rb', line 101

def x_rotation=(new_x_rotation)
  @c_pointer[2].write_double(new_x_rotation)
end

#y_originFloat

Y-coordinate of the center of the upper left pixel. In wikipedia’s World Map definition, this is “F”.

Returns:



109
110
111
112
113
# File 'lib/gdal/geo_transform.rb', line 109

def y_origin
  return nil if null?

  @c_pointer[3].read_double
end

#y_origin=(new_y_origin) ⇒ Object

Parameters:



116
117
118
# File 'lib/gdal/geo_transform.rb', line 116

def y_origin=(new_y_origin)
  @c_pointer[3].write_double(new_y_origin)
end

#y_rotationFloat

Rotation about the y-axis. In wikipedia’s World Map definition, this is “D”.

Returns:



124
125
126
127
128
# File 'lib/gdal/geo_transform.rb', line 124

def y_rotation
  return nil if null?

  @c_pointer[4].read_double
end

#y_rotation=(new_y_rotation) ⇒ Object

Parameters:



131
132
133
# File 'lib/gdal/geo_transform.rb', line 131

def y_rotation=(new_y_rotation)
  @c_pointer[4].write_double(new_y_rotation)
end