Class: GDAL::GeoTransform
- Inherits:
-
Object
- Object
- GDAL::GeoTransform
- 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
-
#c_pointer ⇒ FFI::Pointer
readonly
C pointer to the C geo-transform.
Class Method Summary collapse
- .from_world_file(filename, extension = nil) ⇒ GDAL::GeoTransform
-
.new_pointer ⇒ FFI::MemoryPointer
A pointer that can be used to hold a GeoTransform.
Instance Method Summary collapse
-
#apply_geo_transform(pixel, line) ⇒ Hash{x_geo => Float, y_geo => Float}
(also: #pixel_to_world)
Converts a (pixel, line) coordinate to a georeferenced (geo_x, geo_y) location.
-
#compose(other_geo_transform) ⇒ GDAL::GeoTransform
Composes this and the given geo_transform.
-
#initialize(geo_transform = nil) ⇒ GeoTransform
constructor
A new instance of GeoTransform.
-
#invert ⇒ GDAL::GeoTransform
Inverts the current 3x2 set of coefficients and returns a new GeoTransform.
- #null? ⇒ Boolean
-
#pixel_height ⇒ Float
AKA Y-pixel size.
- #pixel_height=(new_pixel_height) ⇒ Object
-
#pixel_width ⇒ Float
AKA X-pixel size.
- #pixel_width=(new_pixel_width) ⇒ Object
- #to_world_file(raster_filename, world_extension) ⇒ Boolean
-
#x_origin ⇒ Float
X-coordinate of the center of the upper left pixel.
- #x_origin=(new_x_origin) ⇒ Object
-
#x_rotation ⇒ Float
Rotation about the x-axis.
- #x_rotation=(new_x_rotation) ⇒ Object
-
#y_origin ⇒ Float
Y-coordinate of the center of the upper left pixel.
- #y_origin=(new_y_origin) ⇒ Object
-
#y_rotation ⇒ Float
Rotation about the y-axis.
- #y_rotation=(new_y_rotation) ⇒ Object
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.
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_pointer ⇒ FFI::Pointer (readonly)
Returns 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
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_pointer ⇒ FFI::MemoryPointer
Returns 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
#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.
151 152 153 154 155 156 157 |
# File 'lib/gdal/geo_transform.rb', line 151 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.
165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/gdal/geo_transform.rb', line 165 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 |
#invert ⇒ GDAL::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.
182 183 184 185 186 187 188 |
# File 'lib/gdal/geo_transform.rb', line 182 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
43 44 45 |
# File 'lib/gdal/geo_transform.rb', line 43 def null? @c_pointer.null? end |
#pixel_height ⇒ Float
AKA Y-pixel size. In wikipedia’s World Map definition, this is “E”.
126 127 128 129 130 |
# File 'lib/gdal/geo_transform.rb', line 126 def pixel_height return nil if null? @c_pointer[5].read_double end |
#pixel_height=(new_pixel_height) ⇒ Object
133 134 135 |
# File 'lib/gdal/geo_transform.rb', line 133 def pixel_height=(new_pixel_height) @c_pointer[5].write_double(new_pixel_height) end |
#pixel_width ⇒ Float
AKA X-pixel size. In wikipedia’s World Map definition, this is “A”.
66 67 68 69 70 |
# File 'lib/gdal/geo_transform.rb', line 66 def pixel_width return nil if null? @c_pointer[1].read_double end |
#pixel_width=(new_pixel_width) ⇒ Object
73 74 75 |
# File 'lib/gdal/geo_transform.rb', line 73 def pixel_width=(new_pixel_width) @c_pointer[1].write_double(new_pixel_width) end |
#to_world_file(raster_filename, world_extension) ⇒ Boolean
193 194 195 |
# File 'lib/gdal/geo_transform.rb', line 193 def to_world_file(raster_filename, world_extension) FFI::GDAL::GDAL.GDALWriteWorldFile(raster_filename, world_extension, @c_pointer) end |
#x_origin ⇒ Float
X-coordinate of the center of the upper left pixel. In wikipedia’s World Map definition, this is “C”.
51 52 53 54 55 |
# File 'lib/gdal/geo_transform.rb', line 51 def x_origin return nil if null? @c_pointer[0].read_double end |
#x_origin=(new_x_origin) ⇒ Object
58 59 60 |
# File 'lib/gdal/geo_transform.rb', line 58 def x_origin=(new_x_origin) @c_pointer[0].write_double(new_x_origin) end |
#x_rotation ⇒ Float
Rotation about the x-axis. In wikipedia’s World File definition, this is “B”.
81 82 83 84 85 |
# File 'lib/gdal/geo_transform.rb', line 81 def x_rotation return nil if null? @c_pointer[2].read_double end |
#x_rotation=(new_x_rotation) ⇒ Object
88 89 90 |
# File 'lib/gdal/geo_transform.rb', line 88 def x_rotation=(new_x_rotation) @c_pointer[2].write_double(new_x_rotation) end |
#y_origin ⇒ Float
Y-coordinate of the center of the upper left pixel. In wikipedia’s World Map definition, this is “F”.
96 97 98 99 100 |
# File 'lib/gdal/geo_transform.rb', line 96 def y_origin return nil if null? @c_pointer[3].read_double end |
#y_origin=(new_y_origin) ⇒ Object
103 104 105 |
# File 'lib/gdal/geo_transform.rb', line 103 def y_origin=(new_y_origin) @c_pointer[3].write_double(new_y_origin) end |
#y_rotation ⇒ Float
Rotation about the y-axis. In wikipedia’s World Map definition, this is “D”.
111 112 113 114 115 |
# File 'lib/gdal/geo_transform.rb', line 111 def y_rotation return nil if null? @c_pointer[4].read_double end |
#y_rotation=(new_y_rotation) ⇒ Object
118 119 120 |
# File 'lib/gdal/geo_transform.rb', line 118 def y_rotation=(new_y_rotation) @c_pointer[4].write_double(new_y_rotation) end |