proj4rb 2.0.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,206 +1,204 @@
1
- # encoding: UTF-8
2
- require 'stringio'
3
-
4
- module Proj
5
- # Represents a coordinate reference system.
6
- class Crs < PjObject
7
- # To create a coordinate system, you can use CRS codes, well-known text (WKT) strings
8
- # or old-style Proj4 strings (which are deprecated).
9
- #
10
- # @example
11
- # crs1 = Proj::Crs.new('EPSG:4326')
12
- # crs2 = Proj::Crs.new('urn:ogc:def:crs:EPSG::4326')
13
- # crs3 = Proj::Crs.new('+proj=longlat +datum=WGS84 +no_defs +type=crs')
14
- # crs4 = Proj::Crs.new(<<~EOS)
15
- # GEOGCRS["WGS 84",
16
- # DATUM["World Geodetic System 1984",
17
- # ELLIPSOID["WGS 84",6378137,298.257223563,
18
- # LENGTHUNIT["metre",1]]],
19
- # PRIMEM["Greenwich",0,
20
- # ANGLEUNIT["degree",0.0174532925199433]],
21
- # CS[ellipsoidal,2],
22
- # AXIS["geodetic latitude (Lat)",north,
23
- # ORDER[1],
24
- # ANGLEUNIT["degree",0.0174532925199433]],
25
- # AXIS["geodetic longitude (Lon)",east,
26
- # ORDER[2],
27
- # ANGLEUNIT["degree",0.0174532925199433]],
28
- # USAGE[
29
- # SCOPE["unknown"],
30
- # AREA["World"],
31
- # BBOX[-90,-180,90,180]],
32
- # ID["EPSG",4326]]
33
- # EOS
34
- #
35
- # Notice when using the old-style Proj4 string, the addition of the "+type=crs" value.
36
- #
37
- # @param value [String]. See above
38
- # @param context [Context]. An optional Context that the Crs will use for calculations.
39
- def initialize(value, context=nil)
40
- pointer = Api.proj_create(context || Context.current, value)
41
-
42
- if pointer.null?
43
- Error.check
44
- end
45
-
46
- super(pointer, context)
47
-
48
- if Api.method_defined?(:proj_is_crs) && !Api.proj_is_crs(pointer)
49
- raise(Error, "Invalid crs definition. Proj created an instance of: #{self.proj_type}.")
50
- end
51
- end
52
-
53
- # Get the geodeticCRS / geographicCRS from a CRS.
54
- #
55
- # @return [Crs]
56
- def geodetic_crs
57
- PjObject.new(Api.proj_crs_get_geodetic_crs(self.context, self))
58
- end
59
-
60
- # Get a CRS component from a CompoundCRS.
61
- #
62
- # @return [Crs]
63
- def sub_crs(index)
64
- PjObject.new(Api.proj_crs_get_sub_crs(self.context, self, index))
65
- end
66
-
67
- # Returns the datum of a SingleCRS.
68
- #
69
- # @return [Crs]
70
- def datum
71
- PjObject.new(Api.proj_crs_get_datum(self.context, self))
72
- end
73
-
74
- # Get the horizontal datum from a CRS.
75
- #
76
- # @return [Crs]
77
- def horizontal_datum
78
- PjObject.new(Api.proj_crs_get_horizontal_datum(self.context, self))
79
- end
80
-
81
- # Returns the coordinate system of a SingleCRS.
82
- #
83
- # @return [Crs]
84
- def coordinate_system
85
- PjObject.new(Api.proj_crs_get_coordinate_system(self.context, self))
86
- end
87
-
88
- # Returns the number of axis of the coordinate system.
89
- #
90
- # @return [Integer]
91
- def axis_count
92
- foo = Api.proj_crs_get_coordinate_system(self.context, self)
93
- result = Api.proj_cs_get_axis_count(self.context, self.coordinate_system)
94
- if result == -1
95
- Error.check
96
- end
97
- result
98
- end
99
-
100
- # Returns information on an axis.
101
- #
102
- # @return [Array<Hash>]
103
- def axis_info
104
- self.axis_count.times.map do |index|
105
- p_name = FFI::MemoryPointer.new(:pointer)
106
- p_abbreviation = FFI::MemoryPointer.new(:pointer)
107
- p_direction = FFI::MemoryPointer.new(:pointer)
108
- p_unit_conv_factor = FFI::MemoryPointer.new(:double)
109
- p_unit_name = FFI::MemoryPointer.new(:pointer)
110
- p_unit_auth_name = FFI::MemoryPointer.new(:pointer)
111
- p_unit_code = FFI::MemoryPointer.new(:pointer)
112
-
113
- result = Api.proj_cs_get_axis_info(self.context, self.coordinate_system, index,
114
- p_name, p_abbreviation, p_direction, p_unit_conv_factor, p_unit_name, p_unit_auth_name, p_unit_code)
115
-
116
- unless result
117
- Error.check
118
- end
119
-
120
- {:name => p_name.read_pointer.read_string,
121
- :abbreviation => p_abbreviation.read_pointer.read_string_to_null,
122
- :direction => p_direction.read_pointer.read_string_to_null,
123
- :unit_conv_factor => p_unit_conv_factor.read_double,
124
- :unit_name => p_unit_name.read_pointer.read_string_to_null,
125
- :unit_auth_name => p_unit_auth_name.read_pointer.read_string_to_null,
126
- :unit_code => p_unit_code.read_pointer.read_string_to_null}
127
- end
128
- end
129
-
130
- # Returns the type of the coordinate system.
131
- #
132
- # @return [:PJ_COORDINATE_SYSTEM_TYPE]
133
- def crs_type
134
- foo = Api.proj_crs_get_coordinate_system(self.context, self)
135
- result = Api.proj_cs_get_type(self.context, self.coordinate_system)
136
- if result == :PJ_CS_TYPE_UNKNOWN
137
- Error.check
138
- end
139
- result
140
- end
141
-
142
- # Return the area of use of an object.
143
- #
144
- # @return [Area]
145
- def area
146
- @area ||= Area.for_object(self)
147
- end
148
-
149
- # Get the ellipsoid from a CRS or a GeodeticReferenceFrame.
150
- #
151
- # @return [PjObject]
152
- def ellipsoid
153
- PjObject.new(Api.proj_get_ellipsoid(self.context, self))
154
- end
155
-
156
- # Return the Conversion of a DerivedCRS (such as a ProjectedCRS), or the Transformation from
157
- # the baseCRS to the hubCRS of a BoundCRS.
158
- #
159
- # @return [PjObject]
160
- def operation
161
- pointer = Api.proj_crs_get_coordoperation(self.context, self)
162
- if pointer.null?
163
- Error.check
164
- end
165
- PjObject.new(pointer)
166
- end
167
-
168
- # Get the prime meridian of a CRS or a GeodeticReferenceFrame.
169
- #
170
- # @return [PjObject]
171
- def prime_meridian
172
- PjObject.new(Api.proj_get_prime_meridian(self.context, self))
173
- end
174
-
175
- # A nicely printed out description
176
- #
177
- # @return [String]
178
- def inspect
179
- result = StringIO.new
180
- result.set_encoding('UTF-8')
181
- result << <<~EOS
182
- <#{self.class.name}>: #{self.auth(0)}
183
- #{self.description}
184
- Axis Info [#{self.crs_type}]:
185
- EOS
186
-
187
- self.axis_info.each do |axis_info|
188
- result << "- #{axis_info[:abbreviation]}[#{axis_info[:direction]}]: #{axis_info[:name]} (#{axis_info[:unit_name]})" << "\n"
189
- end
190
-
191
- result << <<~EOS
192
- Area of Use:
193
- - name: #{self.area.name}
194
- - bounds: (#{self.area.west_lon_degree}, #{self.area.south_lat_degree}, #{self.area.east_lon_degree}, #{self.area.north_lat_degree})
195
- Coordinate operation:
196
- - name: ?
197
- - method: ?
198
- Datum: #{self.datum.name}
199
- - Ellipsoid: #{self.ellipsoid.name}
200
- - Prime Meridian: #{self.prime_meridian.name}
201
- EOS
202
-
203
- result.string
204
- end
205
- end
206
- end
1
+ # encoding: UTF-8
2
+ require 'stringio'
3
+
4
+ module Proj
5
+ # Represents a coordinate reference system.
6
+ class Crs < PjObject
7
+ # To create a coordinate system, you can use CRS codes, well-known text (WKT) strings
8
+ # or old-style Proj4 strings (which are deprecated).
9
+ #
10
+ # @example
11
+ # crs1 = Proj::Crs.new('EPSG:4326')
12
+ # crs2 = Proj::Crs.new('urn:ogc:def:crs:EPSG::4326')
13
+ # crs3 = Proj::Crs.new('+proj=longlat +datum=WGS84 +no_defs +type=crs')
14
+ # crs4 = Proj::Crs.new(<<~EOS)
15
+ # GEOGCRS["WGS 84",
16
+ # DATUM["World Geodetic System 1984",
17
+ # ELLIPSOID["WGS 84",6378137,298.257223563,
18
+ # LENGTHUNIT["metre",1]]],
19
+ # PRIMEM["Greenwich",0,
20
+ # ANGLEUNIT["degree",0.0174532925199433]],
21
+ # CS[ellipsoidal,2],
22
+ # AXIS["geodetic latitude (Lat)",north,
23
+ # ORDER[1],
24
+ # ANGLEUNIT["degree",0.0174532925199433]],
25
+ # AXIS["geodetic longitude (Lon)",east,
26
+ # ORDER[2],
27
+ # ANGLEUNIT["degree",0.0174532925199433]],
28
+ # USAGE[
29
+ # SCOPE["unknown"],
30
+ # AREA["World"],
31
+ # BBOX[-90,-180,90,180]],
32
+ # ID["EPSG",4326]]
33
+ # EOS
34
+ #
35
+ # Notice when using the old-style Proj4 string, the addition of the "+type=crs" value.
36
+ #
37
+ # @param value [String]. See above
38
+ # @param context [Context]. An optional Context that the Crs will use for calculations.
39
+ def initialize(value, context=nil)
40
+ pointer = Api.proj_create(context || Context.current, value)
41
+
42
+ if pointer.null?
43
+ Error.check
44
+ end
45
+
46
+ super(pointer, context)
47
+
48
+ if Api.method_defined?(:proj_is_crs) && !Api.proj_is_crs(pointer)
49
+ raise(Error, "Invalid crs definition. Proj created an instance of: #{self.proj_type}.")
50
+ end
51
+ end
52
+
53
+ # Get the geodeticCRS / geographicCRS from a CRS.
54
+ #
55
+ # @return [Crs]
56
+ def geodetic_crs
57
+ PjObject.new(Api.proj_crs_get_geodetic_crs(self.context, self))
58
+ end
59
+
60
+ # Get a CRS component from a CompoundCRS.
61
+ #
62
+ # @return [Crs]
63
+ def sub_crs(index)
64
+ PjObject.new(Api.proj_crs_get_sub_crs(self.context, self, index))
65
+ end
66
+
67
+ # Returns the datum of a SingleCRS.
68
+ #
69
+ # @return [Crs]
70
+ def datum
71
+ PjObject.new(Api.proj_crs_get_datum(self.context, self))
72
+ end
73
+
74
+ # Get the horizontal datum from a CRS.
75
+ #
76
+ # @return [Crs]
77
+ def horizontal_datum
78
+ PjObject.new(Api.proj_crs_get_horizontal_datum(self.context, self))
79
+ end
80
+
81
+ # Returns the coordinate system of a SingleCRS.
82
+ #
83
+ # @return [Crs]
84
+ def coordinate_system
85
+ PjObject.new(Api.proj_crs_get_coordinate_system(self.context, self))
86
+ end
87
+
88
+ # Returns the number of axis of the coordinate system.
89
+ #
90
+ # @return [Integer]
91
+ def axis_count
92
+ result = Api.proj_cs_get_axis_count(self.context, self.coordinate_system)
93
+ if result == -1
94
+ Error.check
95
+ end
96
+ result
97
+ end
98
+
99
+ # Returns information on an axis.
100
+ #
101
+ # @return [Array<Hash>]
102
+ def axis_info
103
+ self.axis_count.times.map do |index|
104
+ p_name = FFI::MemoryPointer.new(:pointer)
105
+ p_abbreviation = FFI::MemoryPointer.new(:pointer)
106
+ p_direction = FFI::MemoryPointer.new(:pointer)
107
+ p_unit_conv_factor = FFI::MemoryPointer.new(:double)
108
+ p_unit_name = FFI::MemoryPointer.new(:pointer)
109
+ p_unit_auth_name = FFI::MemoryPointer.new(:pointer)
110
+ p_unit_code = FFI::MemoryPointer.new(:pointer)
111
+
112
+ result = Api.proj_cs_get_axis_info(self.context, self.coordinate_system, index,
113
+ p_name, p_abbreviation, p_direction, p_unit_conv_factor, p_unit_name, p_unit_auth_name, p_unit_code)
114
+
115
+ unless result
116
+ Error.check
117
+ end
118
+
119
+ {:name => p_name.read_pointer.read_string,
120
+ :abbreviation => p_abbreviation.read_pointer.read_string_to_null,
121
+ :direction => p_direction.read_pointer.read_string_to_null,
122
+ :unit_conv_factor => p_unit_conv_factor.read_double,
123
+ :unit_name => p_unit_name.read_pointer.read_string_to_null,
124
+ :unit_auth_name => p_unit_auth_name.read_pointer.read_string_to_null,
125
+ :unit_code => p_unit_code.read_pointer.read_string_to_null}
126
+ end
127
+ end
128
+
129
+ # Returns the type of the coordinate system.
130
+ #
131
+ # @return [:PJ_COORDINATE_SYSTEM_TYPE]
132
+ def crs_type
133
+ result = Api.proj_cs_get_type(self.context, self.coordinate_system)
134
+ if result == :PJ_CS_TYPE_UNKNOWN
135
+ Error.check
136
+ end
137
+ result
138
+ end
139
+
140
+ # Return the area of use of an object.
141
+ #
142
+ # @return [Area]
143
+ def area
144
+ @area ||= Area.for_object(self)
145
+ end
146
+
147
+ # Get the ellipsoid from a CRS or a GeodeticReferenceFrame.
148
+ #
149
+ # @return [PjObject]
150
+ def ellipsoid
151
+ PjObject.new(Api.proj_get_ellipsoid(self.context, self))
152
+ end
153
+
154
+ # Return the Conversion of a DerivedCRS (such as a ProjectedCRS), or the Transformation from
155
+ # the baseCRS to the hubCRS of a BoundCRS.
156
+ #
157
+ # @return [PjObject]
158
+ def operation
159
+ pointer = Api.proj_crs_get_coordoperation(self.context, self)
160
+ if pointer.null?
161
+ Error.check
162
+ end
163
+ PjObject.new(pointer)
164
+ end
165
+
166
+ # Get the prime meridian of a CRS or a GeodeticReferenceFrame.
167
+ #
168
+ # @return [PjObject]
169
+ def prime_meridian
170
+ PjObject.new(Api.proj_get_prime_meridian(self.context, self))
171
+ end
172
+
173
+ # A nicely printed out description
174
+ #
175
+ # @return [String]
176
+ def inspect
177
+ result = StringIO.new
178
+ result.set_encoding('UTF-8')
179
+ result << <<~EOS
180
+ <#{self.class.name}>: #{self.auth(0)}
181
+ #{self.description}
182
+ Axis Info [#{self.crs_type}]:
183
+ EOS
184
+
185
+ self.axis_info.each do |axis_info|
186
+ result << "- #{axis_info[:abbreviation]}[#{axis_info[:direction]}]: #{axis_info[:name]} (#{axis_info[:unit_name]})" << "\n"
187
+ end
188
+
189
+ result << <<~EOS
190
+ Area of Use:
191
+ - name: #{self.area.name}
192
+ - bounds: (#{self.area.west_lon_degree}, #{self.area.south_lat_degree}, #{self.area.east_lon_degree}, #{self.area.north_lat_degree})
193
+ Coordinate operation:
194
+ - name: ?
195
+ - method: ?
196
+ Datum: #{self.datum.name}
197
+ - Ellipsoid: #{self.ellipsoid.name}
198
+ - Prime Meridian: #{self.prime_meridian.name}
199
+ EOS
200
+
201
+ result.string
202
+ end
203
+ end
204
+ end
@@ -1,42 +1,42 @@
1
- module Proj
2
- class Ellipsoid
3
- attr_reader :id, :major, :ell, :name
4
-
5
- def self.list
6
- pointer_to_array = FFI::Pointer.new(Api::PJ_ELLPS, Api.proj_list_ellps)
7
- result = Array.new
8
- 0.step do |i|
9
- ellipse_info = Api::PJ_ELLPS.new(pointer_to_array[i])
10
- break result if ellipse_info[:id].nil?
11
- result << self.new(ellipse_info[:id], ellipse_info[:major], ellipse_info[:ell], ellipse_info[:name])
12
- end
13
- end
14
-
15
- def self.get(id)
16
- self.list.find {|ellipsoid| ellipsoid.id == id}
17
- end
18
-
19
- def initialize(id, major, ell, name)
20
- @id = id
21
- @major = major
22
- @ell = ell
23
- @name = name
24
- end
25
-
26
- def <=>(other)
27
- self.id <=> other.id
28
- end
29
-
30
- def ==(other)
31
- self.id == other.id
32
- end
33
-
34
- def to_s
35
- self.id
36
- end
37
-
38
- def inspect
39
- "#<#{self.class} id=\"#{id}\", major=\"#{major}\", ell=\"#{ell}\", name=\"#{name}\">"
40
- end
41
- end
1
+ module Proj
2
+ class Ellipsoid
3
+ attr_reader :id, :major, :ell, :name
4
+
5
+ def self.list
6
+ pointer_to_array = FFI::Pointer.new(Api::PJ_ELLPS, Api.proj_list_ellps)
7
+ result = Array.new
8
+ 0.step do |i|
9
+ ellipse_info = Api::PJ_ELLPS.new(pointer_to_array[i])
10
+ break result if ellipse_info[:id].nil?
11
+ result << self.new(ellipse_info[:id], ellipse_info[:major], ellipse_info[:ell], ellipse_info[:name])
12
+ end
13
+ end
14
+
15
+ def self.get(id)
16
+ self.list.find {|ellipsoid| ellipsoid.id == id}
17
+ end
18
+
19
+ def initialize(id, major, ell, name)
20
+ @id = id
21
+ @major = major
22
+ @ell = ell
23
+ @name = name
24
+ end
25
+
26
+ def <=>(other)
27
+ self.id <=> other.id
28
+ end
29
+
30
+ def ==(other)
31
+ self.id == other.id
32
+ end
33
+
34
+ def to_s
35
+ self.id
36
+ end
37
+
38
+ def inspect
39
+ "#<#{self.class} id=\"#{id}\", major=\"#{major}\", ell=\"#{ell}\", name=\"#{name}\">"
40
+ end
41
+ end
42
42
  end
@@ -1,18 +1,18 @@
1
- module Proj
2
- class Error < StandardError
3
- def self.check(errno=nil)
4
- unless errno
5
- errno = Context.current.errno
6
- end
7
-
8
- if errno != 0
9
- message = if Api.method_defined?(:proj_errno_string)
10
- Api.proj_errno_string(errno)
11
- else
12
- Api.pj_strerrno(errno)
13
- end
14
- raise(self, message)
15
- end
16
- end
17
- end
1
+ module Proj
2
+ class Error < StandardError
3
+ def self.check(errno=nil)
4
+ unless errno
5
+ errno = Context.current.errno
6
+ end
7
+
8
+ if errno != 0
9
+ message = if Api.method_defined?(:proj_errno_string)
10
+ Api.proj_errno_string(errno)
11
+ else
12
+ Api.pj_strerrno(errno)
13
+ end
14
+ raise(self, message)
15
+ end
16
+ end
17
+ end
18
18
  end
@@ -1,43 +1,43 @@
1
- module Proj
2
- class Operation
3
- attr_reader :id, :description
4
-
5
- def self.list
6
- pointer_to_array = FFI::Pointer.new(Api::PJ_OPERATIONS, Api.proj_list_operations)
7
- result = Array.new
8
- 0.step do |i|
9
- operation_info = Api::PJ_OPERATIONS.new(pointer_to_array[i])
10
- break result if operation_info[:id].nil?
11
- id = operation_info[:id]
12
- description = operation_info[:descr].read_pointer.read_string.force_encoding('UTF-8')
13
- result << self.new(id, description)
14
- end
15
- result
16
- end
17
-
18
- def self.get(id)
19
- self.list.find {|operation| operation.id == id}
20
- end
21
-
22
- def initialize(id, description)
23
- @id = id
24
- @description = description
25
- end
26
-
27
- def <=>(other)
28
- self.id <=> other.id
29
- end
30
-
31
- def ==(other)
32
- self.id == other.id
33
- end
34
-
35
- def to_s
36
- self.id
37
- end
38
-
39
- def inspect
40
- "#<#{self.class} id=\"#{id}\", major=\"#{major}\", ell=\"#{ell}\", name=\"#{name}\">"
41
- end
42
- end
1
+ module Proj
2
+ class Operation
3
+ attr_reader :id, :description
4
+
5
+ def self.list
6
+ pointer_to_array = FFI::Pointer.new(Api::PJ_OPERATIONS, Api.proj_list_operations)
7
+ result = Array.new
8
+ 0.step do |i|
9
+ operation_info = Api::PJ_OPERATIONS.new(pointer_to_array[i])
10
+ break result if operation_info[:id].nil?
11
+ id = operation_info[:id]
12
+ description = operation_info[:descr].read_pointer.read_string.force_encoding('UTF-8')
13
+ result << self.new(id, description)
14
+ end
15
+ result
16
+ end
17
+
18
+ def self.get(id)
19
+ self.list.find {|operation| operation.id == id}
20
+ end
21
+
22
+ def initialize(id, description)
23
+ @id = id
24
+ @description = description
25
+ end
26
+
27
+ def <=>(other)
28
+ self.id <=> other.id
29
+ end
30
+
31
+ def ==(other)
32
+ self.id == other.id
33
+ end
34
+
35
+ def to_s
36
+ self.id
37
+ end
38
+
39
+ def inspect
40
+ "#<#{self.class} id=\"#{id}\", major=\"#{major}\", ell=\"#{ell}\", name=\"#{name}\">"
41
+ end
42
+ end
43
43
  end