rgeo 0.1.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. data/History.rdoc +22 -0
  2. data/README.rdoc +124 -0
  3. data/Version +1 -0
  4. data/ext/geos_c_impl/extconf.rb +72 -0
  5. data/ext/geos_c_impl/factory.c +468 -0
  6. data/ext/geos_c_impl/factory.h +217 -0
  7. data/ext/geos_c_impl/geometry.c +644 -0
  8. data/ext/geos_c_impl/geometry.h +65 -0
  9. data/ext/geos_c_impl/geometry_collection.c +580 -0
  10. data/ext/geos_c_impl/geometry_collection.h +79 -0
  11. data/ext/geos_c_impl/globals.h +58 -0
  12. data/ext/geos_c_impl/line_string.c +468 -0
  13. data/ext/geos_c_impl/line_string.h +74 -0
  14. data/ext/geos_c_impl/main.c +65 -0
  15. data/ext/geos_c_impl/point.c +201 -0
  16. data/ext/geos_c_impl/point.h +77 -0
  17. data/ext/geos_c_impl/polygon.c +259 -0
  18. data/ext/geos_c_impl/polygon.h +76 -0
  19. data/ext/geos_c_impl/preface.h +42 -0
  20. data/lib/rgeo.rb +68 -0
  21. data/lib/rgeo/errors.rb +59 -0
  22. data/lib/rgeo/features.rb +89 -0
  23. data/lib/rgeo/features/curve.rb +155 -0
  24. data/lib/rgeo/features/factory.rb +191 -0
  25. data/lib/rgeo/features/geometry.rb +560 -0
  26. data/lib/rgeo/features/geometry_collection.rb +118 -0
  27. data/lib/rgeo/features/line.rb +65 -0
  28. data/lib/rgeo/features/line_string.rb +101 -0
  29. data/lib/rgeo/features/linear_ring.rb +65 -0
  30. data/lib/rgeo/features/multi_curve.rb +112 -0
  31. data/lib/rgeo/features/multi_line_string.rb +65 -0
  32. data/lib/rgeo/features/multi_point.rb +72 -0
  33. data/lib/rgeo/features/multi_polygon.rb +96 -0
  34. data/lib/rgeo/features/multi_surface.rb +115 -0
  35. data/lib/rgeo/features/point.rb +97 -0
  36. data/lib/rgeo/features/polygon.rb +141 -0
  37. data/lib/rgeo/features/surface.rb +121 -0
  38. data/lib/rgeo/geo_json.rb +58 -0
  39. data/lib/rgeo/geo_json/coder.rb +305 -0
  40. data/lib/rgeo/geo_json/entities.rb +284 -0
  41. data/lib/rgeo/geo_json/interface.rb +95 -0
  42. data/lib/rgeo/geography.rb +75 -0
  43. data/lib/rgeo/geography/common/geometry_collection_methods.rb +206 -0
  44. data/lib/rgeo/geography/common/geometry_methods.rb +92 -0
  45. data/lib/rgeo/geography/common/helper.rb +102 -0
  46. data/lib/rgeo/geography/common/line_string_methods.rb +187 -0
  47. data/lib/rgeo/geography/common/point_methods.rb +149 -0
  48. data/lib/rgeo/geography/common/polygon_methods.rb +122 -0
  49. data/lib/rgeo/geography/factories.rb +136 -0
  50. data/lib/rgeo/geography/factory.rb +246 -0
  51. data/lib/rgeo/geography/projected_window.rb +467 -0
  52. data/lib/rgeo/geography/simple_mercator/feature_classes.rb +320 -0
  53. data/lib/rgeo/geography/simple_mercator/feature_methods.rb +291 -0
  54. data/lib/rgeo/geography/simple_mercator/projector.rb +116 -0
  55. data/lib/rgeo/geography/simple_spherical/calculations.rb +70 -0
  56. data/lib/rgeo/geography/simple_spherical/geometry_collection_impl.rb +66 -0
  57. data/lib/rgeo/geography/simple_spherical/geometry_methods.rb +59 -0
  58. data/lib/rgeo/geography/simple_spherical/line_string_impl.rb +104 -0
  59. data/lib/rgeo/geography/simple_spherical/multi_line_string_impl.rb +67 -0
  60. data/lib/rgeo/geography/simple_spherical/multi_point_impl.rb +67 -0
  61. data/lib/rgeo/geography/simple_spherical/multi_polygon_impl.rb +67 -0
  62. data/lib/rgeo/geography/simple_spherical/point_impl.rb +85 -0
  63. data/lib/rgeo/geography/simple_spherical/polygon_impl.rb +66 -0
  64. data/lib/rgeo/geos.rb +72 -0
  65. data/lib/rgeo/geos/factory.rb +260 -0
  66. data/lib/rgeo/geos/impl_additions.rb +57 -0
  67. data/lib/rgeo/geos/interface.rb +74 -0
  68. data/lib/rgeo/version.rb +52 -0
  69. data/tests/geos/tc_factory.rb +91 -0
  70. data/tests/geos/tc_geometry_collection.rb +226 -0
  71. data/tests/geos/tc_line_string.rb +310 -0
  72. data/tests/geos/tc_misc.rb +72 -0
  73. data/tests/geos/tc_multi_line_string.rb +211 -0
  74. data/tests/geos/tc_multi_point.rb +202 -0
  75. data/tests/geos/tc_multi_polygon.rb +210 -0
  76. data/tests/geos/tc_point.rb +305 -0
  77. data/tests/geos/tc_polygon.rb +240 -0
  78. data/tests/simple_mercator/tc_point.rb +303 -0
  79. data/tests/simple_mercator/tc_window.rb +219 -0
  80. data/tests/tc_geojson.rb +230 -0
  81. data/tests/tc_oneoff.rb +61 -0
  82. metadata +162 -0
@@ -0,0 +1,320 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # Mercator geography feature classes
4
+ #
5
+ # -----------------------------------------------------------------------------
6
+ # Copyright 2010 Daniel Azuma
7
+ #
8
+ # All rights reserved.
9
+ #
10
+ # Redistribution and use in source and binary forms, with or without
11
+ # modification, are permitted provided that the following conditions are met:
12
+ #
13
+ # * Redistributions of source code must retain the above copyright notice,
14
+ # this list of conditions and the following disclaimer.
15
+ # * Redistributions in binary form must reproduce the above copyright notice,
16
+ # this list of conditions and the following disclaimer in the documentation
17
+ # and/or other materials provided with the distribution.
18
+ # * Neither the name of the copyright holder, nor the names of any other
19
+ # contributors to this software, may be used to endorse or promote products
20
+ # derived from this software without specific prior written permission.
21
+ #
22
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
+ # POSSIBILITY OF SUCH DAMAGE.
33
+ # -----------------------------------------------------------------------------
34
+ ;
35
+
36
+
37
+ module RGeo
38
+
39
+ module Geography
40
+
41
+ module SimpleMercator
42
+
43
+
44
+ class PointImpl
45
+
46
+
47
+ include Features::Point
48
+ include Common::GeometryMethods
49
+ include GeometryMethods
50
+ include Common::PointMethods
51
+
52
+
53
+ def initialize(factory_, x_, y_)
54
+ _set_factory(factory_)
55
+ _setup(x_, y_)
56
+ end
57
+
58
+
59
+ def _validate_geometry
60
+ @y = 85.0511287 if @y > 85.0511287
61
+ @y = -85.0511287 if @y < -85.0511287
62
+ super
63
+ end
64
+
65
+
66
+ def _make_projection(projection_factory_) # :nodoc:
67
+ rpd_ = Common::Helper::RADIANS_PER_DEGREE
68
+ mpr_ = EQUATORIAL_RADIUS
69
+ projection_factory_.point(@x * rpd_ * mpr_,
70
+ ::Math.log(::Math.tan(::Math::PI / 4.0 + @y * rpd_ / 2.0)) * mpr_)
71
+ end
72
+
73
+
74
+ def scaling_factor
75
+ 1.0 / ::Math.cos(Common::Helper::RADIANS_PER_DEGREE * @y)
76
+ end
77
+
78
+
79
+ def canonical_point
80
+ if @x >= -180.0 && @x < 180.0
81
+ self
82
+ else
83
+ x_ = @x % 360.0
84
+ x_ -= 360.0 if x_ >= 180.0
85
+ PointImpl.new(@factory, x_, @y)
86
+ end
87
+ end
88
+
89
+
90
+ end
91
+
92
+
93
+ class LineStringImpl
94
+
95
+
96
+ include Features::LineString
97
+ include Common::GeometryMethods
98
+ include GeometryMethods
99
+ include NCurveMethods
100
+ include CurveMethods
101
+ include Common::LineStringMethods
102
+ include LineStringMethods
103
+
104
+
105
+ def initialize(factory_, points_)
106
+ _set_factory(factory_)
107
+ _setup(points_)
108
+ end
109
+
110
+
111
+ def _make_projection(projection_factory_) # :nodoc:
112
+ projection_factory_.line_string(@points.map{ |p_| p_.projection })
113
+ end
114
+
115
+
116
+ end
117
+
118
+
119
+ class LinearRingImpl
120
+
121
+
122
+ include Features::Line
123
+ include Common::GeometryMethods
124
+ include GeometryMethods
125
+ include NCurveMethods
126
+ include CurveMethods
127
+ include Common::LineStringMethods
128
+ include LineStringMethods
129
+ include Common::LinearRingMethods
130
+
131
+
132
+ def initialize(factory_, points_)
133
+ _set_factory(factory_)
134
+ _setup(points_)
135
+ end
136
+
137
+
138
+ def _make_projection(projection_factory_) # :nodoc:
139
+ projection_factory_.linear_ring(@points.map{ |p_| p_.projection })
140
+ end
141
+
142
+
143
+ end
144
+
145
+
146
+ class LineImpl
147
+
148
+
149
+ include Features::Line
150
+ include Common::GeometryMethods
151
+ include GeometryMethods
152
+ include NCurveMethods
153
+ include CurveMethods
154
+ include Common::LineStringMethods
155
+ include LineStringMethods
156
+ include Common::LineMethods
157
+
158
+
159
+ def initialize(factory_, start_, end_)
160
+ _set_factory(factory_)
161
+ _setup([start_, end_])
162
+ end
163
+
164
+
165
+ def _make_projection(projection_factory_) # :nodoc:
166
+ projection_factory_.line(start_point.projection, end_point.projection)
167
+ end
168
+
169
+
170
+ end
171
+
172
+
173
+ class PolygonImpl
174
+
175
+
176
+ include Features::Polygon
177
+ include Common::GeometryMethods
178
+ include GeometryMethods
179
+ include NSurfaceMethods
180
+ include SurfaceMethods
181
+ include Common::PolygonMethods
182
+
183
+
184
+ def initialize(factory_, exterior_ring_, interior_rings_)
185
+ _set_factory(factory_)
186
+ _setup(exterior_ring_, interior_rings_)
187
+ end
188
+
189
+
190
+ def _validate_geometry
191
+ super
192
+ unless projection
193
+ raise Errors::InvalidGeometry, 'Polygon failed assertions'
194
+ end
195
+ end
196
+
197
+
198
+ def _make_projection(projection_factory_) # :nodoc:
199
+ projection_factory_.polygon(@exterior_ring.projection,
200
+ @interior_rings.map{ |p_| p_.projection })
201
+ end
202
+
203
+
204
+ end
205
+
206
+
207
+ class GeometryCollectionImpl
208
+
209
+
210
+ include Features::GeometryCollection
211
+ include Common::GeometryMethods
212
+ include GeometryMethods
213
+ include Common::GeometryCollectionMethods
214
+ include GeometryCollectionMethods
215
+
216
+
217
+ def initialize(factory_, elements_)
218
+ _set_factory(factory_)
219
+ _setup(elements_)
220
+ end
221
+
222
+
223
+ def _make_projection(projection_factory_) # :nodoc:
224
+ projection_factory_.collection(@elements.map{ |p_| p_.projection })
225
+ end
226
+
227
+
228
+ end
229
+
230
+
231
+ class MultiPointImpl
232
+
233
+
234
+ include Features::GeometryCollection
235
+ include Common::GeometryMethods
236
+ include GeometryMethods
237
+ include Common::GeometryCollectionMethods
238
+ include GeometryCollectionMethods
239
+ include Common::MultiPointMethods
240
+
241
+
242
+ def initialize(factory_, elements_)
243
+ _set_factory(factory_)
244
+ _setup(elements_)
245
+ end
246
+
247
+
248
+ def _make_projection(projection_factory_) # :nodoc:
249
+ projection_factory_.multi_point(@elements.map{ |p_| p_.projection })
250
+ end
251
+
252
+
253
+ end
254
+
255
+
256
+ class MultiLineStringImpl
257
+
258
+
259
+ include Features::GeometryCollection
260
+ include Common::GeometryMethods
261
+ include GeometryMethods
262
+ include NCurveMethods
263
+ include Common::GeometryCollectionMethods
264
+ include GeometryCollectionMethods
265
+ include Common::MultiLineStringMethods
266
+
267
+
268
+ def initialize(factory_, elements_)
269
+ _set_factory(factory_)
270
+ _setup(elements_)
271
+ end
272
+
273
+
274
+ def _make_projection(projection_factory_) # :nodoc:
275
+ projection_factory_.multi_line_string(@elements.map{ |p_| p_.projection })
276
+ end
277
+
278
+
279
+ end
280
+
281
+
282
+ class MultiPolygonImpl
283
+
284
+
285
+ include Features::GeometryCollection
286
+ include Common::GeometryMethods
287
+ include GeometryMethods
288
+ include NSurfaceMethods
289
+ include Common::GeometryCollectionMethods
290
+ include GeometryCollectionMethods
291
+ include Common::MultiPolygonMethods
292
+
293
+
294
+ def initialize(factory_, elements_)
295
+ _set_factory(factory_)
296
+ _setup(elements_)
297
+ end
298
+
299
+
300
+ def _validate_geometry
301
+ super
302
+ unless projection
303
+ raise Errors::InvalidGeometry, 'MultiPolygon failed assertions'
304
+ end
305
+ end
306
+
307
+
308
+ def _make_projection(projection_factory_) # :nodoc:
309
+ projection_factory_.multi_polygon(@elements.map{ |p_| p_.projection })
310
+ end
311
+
312
+
313
+ end
314
+
315
+
316
+ end
317
+
318
+ end
319
+
320
+ end
@@ -0,0 +1,291 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # Mercator geography common method definitions
4
+ #
5
+ # -----------------------------------------------------------------------------
6
+ # Copyright 2010 Daniel Azuma
7
+ #
8
+ # All rights reserved.
9
+ #
10
+ # Redistribution and use in source and binary forms, with or without
11
+ # modification, are permitted provided that the following conditions are met:
12
+ #
13
+ # * Redistributions of source code must retain the above copyright notice,
14
+ # this list of conditions and the following disclaimer.
15
+ # * Redistributions in binary form must reproduce the above copyright notice,
16
+ # this list of conditions and the following disclaimer in the documentation
17
+ # and/or other materials provided with the distribution.
18
+ # * Neither the name of the copyright holder, nor the names of any other
19
+ # contributors to this software, may be used to endorse or promote products
20
+ # derived from this software without specific prior written permission.
21
+ #
22
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
+ # POSSIBILITY OF SUCH DAMAGE.
33
+ # -----------------------------------------------------------------------------
34
+ ;
35
+
36
+
37
+ module RGeo
38
+
39
+ module Geography
40
+
41
+ module SimpleMercator
42
+
43
+
44
+ module GeometryMethods
45
+
46
+
47
+ def eql?(rhs_)
48
+ if rhs_.respond_to?(:factory) && rhs_.factory.eql?(@factory)
49
+ projection.eql?(rhs_.projection)
50
+ else
51
+ false
52
+ end
53
+ end
54
+
55
+
56
+ def srid
57
+ 4326
58
+ end
59
+
60
+
61
+ def projection
62
+ unless @projection
63
+ projection_factory_ = @factory.projection_factory
64
+ if projection_factory_
65
+ @projection = _make_projection(projection_factory_)
66
+ else
67
+ @projection = nil
68
+ end
69
+ @projection = false unless @projection
70
+ end
71
+ @projection ? @projection : nil
72
+ end
73
+
74
+
75
+ def _make_projection(projection_factory_) # :nodoc:
76
+ nil
77
+ end
78
+
79
+
80
+ def scaling_factor
81
+ 1.0
82
+ end
83
+
84
+
85
+ def envelope
86
+ factory.unproject(projection.envelope)
87
+ end
88
+
89
+
90
+ def is_empty?
91
+ projection.is_empty?
92
+ end
93
+
94
+
95
+ def is_simple?
96
+ projection.is_simple?
97
+ end
98
+
99
+
100
+ def boundary
101
+ factory.unproject(projection.boundary)
102
+ end
103
+
104
+
105
+ def equals?(rhs_)
106
+ projection.equals?(factory.convert(rhs_).projection)
107
+ end
108
+
109
+
110
+ def disjoint?(rhs_)
111
+ projection.disjoint?(factory.convert(rhs_).projection)
112
+ end
113
+
114
+
115
+ def intersects?(rhs_)
116
+ projection.intersects?(factory.convert(rhs_).projection)
117
+ end
118
+
119
+
120
+ def touches?(rhs_)
121
+ projection.touches?(factory.convert(rhs_).projection)
122
+ end
123
+
124
+
125
+ def crosses?(rhs_)
126
+ projection.crosses?(factory.convert(rhs_).projection)
127
+ end
128
+
129
+
130
+ def within?(rhs_)
131
+ projection.within?(factory.convert(rhs_).projection)
132
+ end
133
+
134
+
135
+ def contains?(rhs_)
136
+ projection.contains?(factory.convert(rhs_).projection)
137
+ end
138
+
139
+
140
+ def overlaps?(rhs_)
141
+ projection.overlaps?(factory.convert(rhs_).projection)
142
+ end
143
+
144
+
145
+ def relate(rhs_, pattern_)
146
+ projection.relate(factory.convert(rhs_).projection, pattern_)
147
+ end
148
+
149
+
150
+ def distance(rhs_)
151
+ projection.distance(factory.convert(rhs_).projection) / scaling_factor
152
+ end
153
+
154
+
155
+ def buffer(distance_)
156
+ factory.unproject(projection.buffer(distance_ * scaling_factor))
157
+ end
158
+
159
+
160
+ def convex_hull()
161
+ factory.unproject(projection.convex_hull)
162
+ end
163
+
164
+
165
+ def intersection(rhs_)
166
+ factory.unproject(projection.intersection(factory.convert(rhs_).projection))
167
+ end
168
+
169
+
170
+ def union(rhs_)
171
+ factory.unproject(projection.union(factory.convert(rhs_).projection))
172
+ end
173
+
174
+
175
+ def difference(rhs_)
176
+ factory.unproject(projection.difference(factory.convert(rhs_).projection))
177
+ end
178
+
179
+
180
+ def sym_difference(rhs_)
181
+ factory.unproject(projection.sym_difference(factory.convert(rhs_).projection))
182
+ end
183
+
184
+
185
+ end
186
+
187
+
188
+ module GeometryCollectionMethods
189
+
190
+
191
+ def scaling_factor
192
+ is_empty? ? 1.0 : geometry_n(0).scaling_factor
193
+ end
194
+
195
+
196
+ end
197
+
198
+
199
+ module NCurveMethods
200
+
201
+
202
+ def length
203
+ projection.length / scaling_factor
204
+ end
205
+
206
+
207
+ end
208
+
209
+
210
+ module CurveMethods
211
+
212
+
213
+ def scaling_factor
214
+ is_empty? ? 1.0 : start_point.scaling_factor
215
+ end
216
+
217
+
218
+ end
219
+
220
+
221
+ module LineStringMethods
222
+
223
+
224
+ def _validate_geometry
225
+ size_ = @points.size
226
+ if size_ > 1
227
+ last_ = @points[0]
228
+ (1...size_).each do |i_|
229
+ p_ = @points[i_]
230
+ last_x_ = last_.x
231
+ p_x_ = p_.x
232
+ changed_ = true
233
+ if p_x_ < last_x_ - 180.0
234
+ p_x_ += 360.0 while p_x_ < last_x_ - 180.0
235
+ elsif p_x_ > last_x_ + 180.0
236
+ p_x_ +- 360.0 while p_x_ > last_x_ + 180.0
237
+ else
238
+ changed_ = false
239
+ end
240
+ if changed_
241
+ p_ = factory.point(p_x_, p_.y)
242
+ @points[i_] = p_
243
+ end
244
+ last_ = p_
245
+ end
246
+ end
247
+ super
248
+ end
249
+
250
+
251
+ end
252
+
253
+
254
+ module NSurfaceMethods
255
+
256
+
257
+ def area
258
+ factor_ = scaling_factor
259
+ projection.area / (factor_ * factor_)
260
+ end
261
+
262
+
263
+ def centroid
264
+ factory.unproject(projection.centroid)
265
+ end
266
+
267
+
268
+ def point_on_surface
269
+ factory.unproject(projection.point_on_surface)
270
+ end
271
+
272
+
273
+ end
274
+
275
+
276
+ module SurfaceMethods
277
+
278
+
279
+ def scaling_factor
280
+ is_empty? ? 1.0 : point_on_surface.scaling_factor
281
+ end
282
+
283
+
284
+ end
285
+
286
+
287
+ end
288
+
289
+ end
290
+
291
+ end