Class: Proj::Projection Deprecated

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

Overview

Deprecated.

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

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.

Examples:

proj = Projection.new("+proj=utm +zone=21 +units=m")
proj = Projection.new ["+proj=utm", "+zone=21", "+units=m"]
proj = Projection.new("proj" => "utm", "zone" => "21", "units" => "m")

Parameters:

  • value (string, array, hash)

    Parameters can be specified as strings, arrays or hashes.



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_errorObject



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

#datumString

Get the ID of the datum used in this projection.

Returns:

  • (String)


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.

Parameters:

  • point (Point)

    in radians

Returns:

  • (Point)

    in cartesian coordinates



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.

Parameters:

  • collection (Enumerable<Point>)

    Points specified in radians

Returns:

  • (Enumerable<Point>)

    Points specified in cartesian coordinates



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.

Parameters:

  • point (Point)

    in degrees

Returns:

  • (Point)

    in cartesian coordinates



130
131
132
# File 'lib/projection.rb', line 130

def forwardDeg(point)
  forward(point.to_radians)
end

#getDefString

Returns projection definitions

Returns:

  • (String)


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.

Parameters:

  • point (Point)

    in cartesian coordinates

Returns:



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.

Parameters:

  • collection (Enumerable<Point>)

    Points specified in cartesian coordinates

Returns:

  • (Enumerable<Point>)

    Points specified in radians



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.

Parameters:

  • point (Point)

    in cartesian coordinates

Returns:



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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


91
92
93
# File 'lib/projection.rb', line 91

def isLatLong?
  Api::pj_is_latlong(self)
end

#projectionString

Get the ID of this projection.

Returns:

  • (String)


98
99
100
# File 'lib/projection.rb', line 98

def projection
  getDef =~ /\+proj=(.+?) / ?  $1 : nil
end

#to_ptrObject



69
70
71
# File 'lib/projection.rb', line 69

def to_ptr
  @pointer
end

#to_sString

Get definition of projection in typical inspect format (#<Proj::Projection init=… proj=… …>).

Returns:

  • (String)


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.

Parameters:

Returns:



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.

Parameters:

Returns:



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