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,284 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # GeoJSON standard entities
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 GeoJSON
40
+
41
+
42
+ # This is a GeoJSON wrapper entity that corresponds to the GeoJSON
43
+ # "Feature" type. It is an immutable type.
44
+ #
45
+ # This is the default implementation that is generated by
46
+ # RGeo::GeoJSON::EntityFactory. You may replace this implementation
47
+ # by writing your own entity factory. Note that an alternate Feature
48
+ # implementation need not subclass or even duck-type this class.
49
+ # the entity factory mediates all interaction between the GeoJSON
50
+ # engine and features.
51
+
52
+ class Feature
53
+
54
+
55
+ # Create a feature wrapping the given geometry, with the given ID
56
+ # and properties.
57
+
58
+ def initialize(geometry_, id_=nil, properties_={})
59
+ @geometry = geometry_
60
+ @id = id_
61
+ @properties = properties_.dup
62
+ end
63
+
64
+
65
+ def inspect # :nodoc:
66
+ "#<#{self.class}:0x#{object_id.to_s(16)} id=#{@id.inspect} geom=#{@geometry.as_text.inspect}>"
67
+ end
68
+
69
+ def to_s # :nodoc:
70
+ inspect
71
+ end
72
+
73
+ def hash # :nodoc:
74
+ @geometry.hash + @id.hash + @properties.hash
75
+ end
76
+
77
+
78
+ # Two features are equal if their geometries, IDs, and properties
79
+ # are all equal.
80
+ # This methods uses the eql? method to test geometry equality, which
81
+ # may behave differently than the == operator.
82
+
83
+ def eql?(rhs_)
84
+ rhs_.kind_of?(Feature) && @geometry.eql?(rhs_.geometry) && @id.eql?(rhs_.feature_id) && @properties.eql?(rhs_.properties)
85
+ end
86
+
87
+
88
+ # Two features are equal if their geometries, IDs, and properties
89
+ # are all equal.
90
+ # This methods uses the == operator to test geometry equality, which
91
+ # may behave differently than the eql? method.
92
+
93
+ def ==(rhs_)
94
+ rhs_.kind_of?(Feature) && @geometry == rhs_.geometry && @id.eql?(rhs_.feature_id) && @properties.eql?(rhs_.properties)
95
+ end
96
+
97
+
98
+ # Returns the geometry contained in this feature.
99
+
100
+ def geometry
101
+ @geometry
102
+ end
103
+
104
+
105
+ # Returns the ID for this feature, which may be nil.
106
+
107
+ def feature_id
108
+ @id
109
+ end
110
+
111
+
112
+ # Returns a copy of the properties for this feature.
113
+
114
+ def properties
115
+ @properties.dup
116
+ end
117
+
118
+
119
+ end
120
+
121
+
122
+ # This is a GeoJSON wrapper entity that corresponds to the GeoJSON
123
+ # "FeatureCollection" type. It is an immutable type.
124
+ #
125
+ # This is the default implementation that is generated by
126
+ # RGeo::GeoJSON::EntityFactory. You may replace this implementation
127
+ # by writing your own entity factory. Note that an alternate
128
+ # FeatureCollection implementation need not subclass or even
129
+ # duck-type this class. The entity factory mediates all interaction
130
+ # between the GeoJSON engine and feature collections.
131
+
132
+ class FeatureCollection
133
+
134
+ include ::Enumerable
135
+
136
+
137
+ # Create a new FeatureCollection with the given features, which must
138
+ # be provided as an Enumerable.
139
+
140
+ def initialize(features_=[])
141
+ @features = []
142
+ features_.each{ |f_| @features << f_ if f_.kind_of?(Feature) }
143
+ end
144
+
145
+
146
+ def inspect # :nodoc:
147
+ "#<#{self.class}:0x#{object_id.to_s(16)}>"
148
+ end
149
+
150
+ def to_s # :nodoc:
151
+ inspect
152
+ end
153
+
154
+ def hash # :nodoc:
155
+ @features.hash
156
+ end
157
+
158
+
159
+ # Two feature collections are equal if they contain the same
160
+ # features in the same order.
161
+ # This methods uses the eql? method to test geometry equality, which
162
+ # may behave differently than the == operator.
163
+
164
+ def eql?(rhs_)
165
+ rhs_.kind_of?(FeatureCollection) && @features.eql?(rhs_.instance_variable_get(:@features))
166
+ end
167
+
168
+
169
+ # Two feature collections are equal if they contain the same
170
+ # features in the same order.
171
+ # This methods uses the == operator to test geometry equality, which
172
+ # may behave differently than the eql? method.
173
+
174
+ def ==(rhs_)
175
+ rhs_.kind_of?(FeatureCollection) && @features == rhs_.instance_variable_get(:@features)
176
+ end
177
+
178
+
179
+ # Iterates or returns an iterator for the features.
180
+
181
+ def each(&block_)
182
+ @features.each(&block_)
183
+ end
184
+
185
+
186
+ # Returns the number of features contained in this collection.
187
+
188
+ def size
189
+ @features.size
190
+ end
191
+
192
+
193
+ # Access a feature by index.
194
+
195
+ def [](index_)
196
+ @features[index_]
197
+ end
198
+
199
+
200
+ end
201
+
202
+
203
+ # This is the default entity factory. It creates objects of type
204
+ # RGeo::GeoJSON::Feature and RGeo::GeoJSON::FeatureCollection.
205
+ # You may create your own entity factory by duck-typing this class.
206
+
207
+ class EntityFactory
208
+
209
+
210
+ # Create and return a new feature, given geometry, ID, and
211
+ # properties hash.
212
+
213
+ def feature(geometry_, id_=nil, properties_={})
214
+ Feature.new(geometry_, id_, properties_)
215
+ end
216
+
217
+
218
+ # Create and return a new feature collection, given an enumerable
219
+ # of feature objects.
220
+
221
+ def feature_collection(features_=[])
222
+ FeatureCollection.new(features_)
223
+ end
224
+
225
+
226
+ # Returns true if the given object is a feature created by this
227
+ # entity factory.
228
+
229
+ def is_feature?(object_)
230
+ object_.kind_of?(Feature)
231
+ end
232
+
233
+
234
+ # Returns true if the given object is a feature collection created
235
+ # by this entity factory.
236
+
237
+ def is_feature_collection?(object_)
238
+ object_.kind_of?(FeatureCollection)
239
+ end
240
+
241
+
242
+ # Run Enumerable#map on the features contained in the given feature
243
+ # collection.
244
+
245
+ def map_feature_collection(object_, &block_)
246
+ object_.map(&block_)
247
+ end
248
+
249
+
250
+ # Returns the geometry associated with the given feature.
251
+
252
+ def get_feature_geometry(object_)
253
+ object_.geometry
254
+ end
255
+
256
+
257
+ # Returns the ID of the given feature, or nil for no ID.
258
+
259
+ def get_feature_id(object_)
260
+ object_.feature_id
261
+ end
262
+
263
+
264
+ # Returns the properties of the given feature as a hash. Editing
265
+ # this hash does not change the state of the feature.
266
+
267
+ def get_feature_properties(object_)
268
+ object_.properties
269
+ end
270
+
271
+
272
+ # Return the singleton instance of EntityFactory.
273
+
274
+ def self.instance
275
+ @instance ||= self.new
276
+ end
277
+
278
+
279
+ end
280
+
281
+
282
+ end
283
+
284
+ end
@@ -0,0 +1,95 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # GeoJSON toplevel interface
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 GeoJSON
40
+
41
+ class << self
42
+
43
+
44
+ # High-level convenience routine for encoding an object as GeoJSON.
45
+ # Pass the object, which may one of the geometry objects specified
46
+ # in RGeo::Features, or an appropriate GeoJSON wrapper entity such
47
+ # as RGeo::GeoJSON::Feature or RGeo::GeoJSON::FeatureCollection.
48
+ #
49
+ # The only option supported is <tt>:entity_factory</tt>, which lets
50
+ # you override the types of GeoJSON entities supported. See
51
+ # RGeo::GeoJSON::EntityFactory for more information. By default,
52
+ # encode supports objects of type RGeo::GeoJSON::Feature and
53
+ # RGeo::GeoJSON::FeatureCollection.
54
+
55
+ def encode(object_, opts_={})
56
+ Coder.new(nil, opts_).encode(object_)
57
+ end
58
+
59
+
60
+ # High-level convenience routine for decoding an object from GeoJSON.
61
+ # The input may be a JSON hash, a String, or an IO object from which
62
+ # to read the JSON string. You must also provide the
63
+ # RGeo::Features::Factory to use to create geometric objects.
64
+ #
65
+ # The only option supported is <tt>:entity_factory</tt>, which lets
66
+ # you override the types of GeoJSON entities that can be created.
67
+ # See RGeo::GeoJSON::EntityFactory for more information. By default,
68
+ # decode generates objects of type RGeo::GeoJSON::Feature and
69
+ # RGeo::GeoJSON::FeatureCollection.
70
+
71
+ def decode(input_, geo_factory_, opts_={})
72
+ Coder.new(geo_factory_, opts_).decode(input_)
73
+ end
74
+
75
+
76
+ # Creates and returns a coder object of type RGeo::GeoJSON::Coder
77
+ # that encapsulates encoding and decoding settings (principally the
78
+ # RGeo::Features::Factory and the RGeo::GeoJSON::EntityFactory to be
79
+ # used).
80
+ # The geo factory is a required argument. The entity factory is
81
+ # optional. To provide one, pass an option with key
82
+ # <tt>:entity_factory</tt>. It defaults to the default
83
+ # RGeo::GeoJSON::EntityFactory, which generates objects of type
84
+ # RGeo::GeoJSON::Feature or RGeo::GeoJSON::FeatureCollection.
85
+
86
+ def coder(geo_factory_, opts_={})
87
+ Coder.new(geo_factory_, opts_)
88
+ end
89
+
90
+
91
+ end
92
+
93
+ end
94
+
95
+ end
@@ -0,0 +1,75 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # Geographic data for RGeo
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
+
40
+ # The Geography implementation provides geographic features using
41
+ # latitude/longitude coordinates measured in degrees.
42
+
43
+ module Geography
44
+ end
45
+
46
+
47
+ end
48
+
49
+
50
+ # Dependency source files.
51
+ paths_ = [
52
+ 'features',
53
+ 'geography/common/helper',
54
+ 'geography/common/geometry_methods',
55
+ 'geography/common/point_methods',
56
+ 'geography/common/line_string_methods',
57
+ 'geography/common/polygon_methods',
58
+ 'geography/common/geometry_collection_methods',
59
+ 'geography/simple_spherical/calculations',
60
+ 'geography/simple_spherical/geometry_methods',
61
+ 'geography/simple_spherical/point_impl',
62
+ 'geography/simple_spherical/line_string_impl',
63
+ 'geography/simple_spherical/polygon_impl',
64
+ 'geography/simple_spherical/geometry_collection_impl',
65
+ 'geography/simple_spherical/multi_point_impl',
66
+ 'geography/simple_spherical/multi_line_string_impl',
67
+ 'geography/simple_spherical/multi_polygon_impl',
68
+ 'geography/simple_mercator/projector',
69
+ 'geography/simple_mercator/feature_methods',
70
+ 'geography/simple_mercator/feature_classes',
71
+ 'geography/factory',
72
+ 'geography/projected_window',
73
+ 'geography/factories',
74
+ ]
75
+ paths_.each{ |path_| require "rgeo/#{path_}" }