Class: Proj::Projection Deprecated
- Inherits:
-
Object
- Object
- Proj::Projection
- Defined in:
- lib/projection.rb
Overview
This class is DEPRECATED. It will be removed when Proj 7 is released and removes the underlying API’s this class uses. Code should be ported to use Crs and Transformation objects.
Class Method Summary collapse
Instance Method Summary collapse
- #check_error ⇒ Object
-
#datum ⇒ String
Get the ID of the datum used in this projection.
-
#forward(point) ⇒ Point
Forward projection of a point.
-
#forward_all(collection) ⇒ Enumerable<Point>
Projects all points in a collection.
-
#forwardDeg(point) ⇒ Point
Convenience function for calculating a forward projection with degrees instead of radians.
-
#getDef ⇒ String
Returns projection definitions.
-
#initialize(value) ⇒ Projection
constructor
Projection classes are created using Proj4 strings which consist of a number of parameters.
-
#inverse(point) ⇒ Point
Inverse projection of a point.
-
#inverse_all(collection) ⇒ Enumerable<Point>
Inverse projection of all points in a collection.
-
#inverseDeg(point) ⇒ Point
Convenience function for calculating an inverse projection with the result in degrees instead of radians.
-
#isGeocent? ⇒ Boolean
(also: #isGeocentric?)
Returns if this is a geocentric projection.
-
#isLatLong? ⇒ Boolean
Returns if this is a lat/long projection.
-
#projection ⇒ String
Get the ID of this projection.
- #to_ptr ⇒ Object
-
#to_s ⇒ String
Get definition of projection in typical inspect format (#<Proj::Projection init=… proj=… …>).
-
#transform(other, point) ⇒ Point
Transforms a point from one projection to another.
-
#transform_all(other, collection) ⇒ Enumerable, Point
Transforms all points in a collection from one projection to another.
Constructor Details
#initialize(value) ⇒ Projection
Projection classes are created using Proj4 strings which consist of a number of parameters. For more information please see the +opt arguments section at proj.org/apps/proj.html.
With all variants the plus sign in front of the keys is optional.
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/projection.rb', line 44 def initialize(value) params = self.class.parse(value) p_params = FFI::MemoryPointer.new(:pointer, params.length) params.each_with_index do |param, i| p_param = FFI::MemoryPointer.from_string(param) p_params[i].write_pointer(p_param) end @pointer = Api.pj_init(params.count, p_params) self.check_error ObjectSpace.define_finalizer(self, self.class.finalize(@pointer)) end |
Class Method Details
.finalize(pointer) ⇒ Object
27 28 29 30 31 |
# File 'lib/projection.rb', line 27 def self.finalize(pointer) proc do Api.pj_free(pointer) end end |
.parse(value) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/projection.rb', line 7 def self.parse(value) case value when Array value when String value.strip.split(' ') when Hash array = [] value.each_pair do |key, value| key = "+#{key}" array << (value.nil? ? key : "#{key}=#{value}") end array when Projection value.getDef.split(' ') else raise ArgumentError, "Unknown type #{value.class} for projection definition" end end |
Instance Method Details
#check_error ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/projection.rb', line 58 def check_error ptr = Api.pj_get_errno_ref errno = ptr.read_int if errno != 0 # If we don't reset the error code it hangs around. This doesn't seem documented anyplace? ptr.write_int(0) Error.check(errno) end end |
#datum ⇒ String
Get the ID of the datum used in this projection.
105 106 107 |
# File 'lib/projection.rb', line 105 def datum getDef =~ /\+datum=(.+?) / ? $1 : nil end |
#forward(point) ⇒ Point
Forward projection of a point. Returns a copy of the point object with coordinates projected.
120 121 122 123 124 |
# File 'lib/projection.rb', line 120 def forward(point) struct = Api.pj_fwd(point, self) self.check_error Point.from_pointer(struct) end |
#forward_all(collection) ⇒ Enumerable<Point>
Projects all points in a collection.
138 139 140 141 142 |
# File 'lib/projection.rb', line 138 def forward_all(collection) collection.map do |point| forward(point) end end |
#forwardDeg(point) ⇒ Point
Convenience function for calculating a forward projection with degrees instead of radians.
130 131 132 |
# File 'lib/projection.rb', line 130 def forwardDeg(point) forward(point.to_radians) end |
#getDef ⇒ String
Returns projection definitions
76 77 78 |
# File 'lib/projection.rb', line 76 def getDef Api.pj_get_def(self, 0) end |
#inverse(point) ⇒ Point
Inverse projection of a point. Returns a copy of the point object with coordinates projected.
148 149 150 151 152 |
# File 'lib/projection.rb', line 148 def inverse(point) struct = Api.pj_inv(point, self) self.check_error Point.from_pointer(struct) end |
#inverse_all(collection) ⇒ Enumerable<Point>
Inverse projection of all points in a collection.
167 168 169 170 171 |
# File 'lib/projection.rb', line 167 def inverse_all(collection) collection.map do |point| inverse(point) end end |
#inverseDeg(point) ⇒ Point
Convenience function for calculating an inverse projection with the result in degrees instead of radians.
158 159 160 161 |
# File 'lib/projection.rb', line 158 def inverseDeg(point) result = inverse(point) result.to_degrees end |
#isGeocent? ⇒ Boolean Also known as: isGeocentric?
Returns if this is a geocentric projection
83 84 85 |
# File 'lib/projection.rb', line 83 def isGeocent? Api::pj_is_geocent(self) end |
#isLatLong? ⇒ Boolean
Returns if this is a lat/long projection
91 92 93 |
# File 'lib/projection.rb', line 91 def isLatLong? Api::pj_is_latlong(self) end |
#projection ⇒ String
Get the ID of this projection.
98 99 100 |
# File 'lib/projection.rb', line 98 def projection getDef =~ /\+proj=(.+?) / ? $1 : nil end |
#to_ptr ⇒ Object
69 70 71 |
# File 'lib/projection.rb', line 69 def to_ptr @pointer end |
#to_s ⇒ String
Get definition of projection in typical inspect format (#<Proj::Projection init=… proj=… …>).
112 113 114 |
# File 'lib/projection.rb', line 112 def to_s "#<#{self.class.name}#{getDef}>" end |
#transform(other, point) ⇒ Point
Transforms a point from one projection to another.
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/projection.rb', line 178 def transform(other, point) p_x = FFI::MemoryPointer.new(:double, 1) p_x.write_double(point.x) p_y = FFI::MemoryPointer.new(:double, 1) p_y.write_double(point.y) p_z = FFI::MemoryPointer.new(:double, 1) p_z.write_double(0) Api.pj_transform(self, other, 1, 1, p_x, p_y, p_z) self.check_error Point.new(p_x.read_double, p_y.read_double) end |
#transform_all(other, collection) ⇒ Enumerable, Point
Transforms all points in a collection from one projection to another. The collection object must implement the each, clear, and << methods (just like an Array) for this to work.
201 202 203 204 205 |
# File 'lib/projection.rb', line 201 def transform_all(other, collection) collection.map do |point| transform(other, point) end end |