schleyfox-rgeo 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (150) hide show
  1. data/History.rdoc +199 -0
  2. data/README.rdoc +172 -0
  3. data/Spatial_Programming_With_RGeo.rdoc +440 -0
  4. data/Version +1 -0
  5. data/ext/geos_c_impl/extconf.rb +84 -0
  6. data/ext/geos_c_impl/factory.c +468 -0
  7. data/ext/geos_c_impl/factory.h +224 -0
  8. data/ext/geos_c_impl/geometry.c +705 -0
  9. data/ext/geos_c_impl/geometry.h +55 -0
  10. data/ext/geos_c_impl/geometry_collection.c +482 -0
  11. data/ext/geos_c_impl/geometry_collection.h +69 -0
  12. data/ext/geos_c_impl/line_string.c +509 -0
  13. data/ext/geos_c_impl/line_string.h +64 -0
  14. data/ext/geos_c_impl/main.c +70 -0
  15. data/ext/geos_c_impl/point.c +193 -0
  16. data/ext/geos_c_impl/point.h +62 -0
  17. data/ext/geos_c_impl/polygon.c +265 -0
  18. data/ext/geos_c_impl/polygon.h +66 -0
  19. data/ext/geos_c_impl/preface.h +50 -0
  20. data/ext/proj4_c_impl/extconf.rb +88 -0
  21. data/ext/proj4_c_impl/main.c +271 -0
  22. data/lib/rgeo.rb +124 -0
  23. data/lib/rgeo/cartesian.rb +60 -0
  24. data/lib/rgeo/cartesian/analysis.rb +118 -0
  25. data/lib/rgeo/cartesian/bounding_box.rb +337 -0
  26. data/lib/rgeo/cartesian/calculations.rb +161 -0
  27. data/lib/rgeo/cartesian/factory.rb +209 -0
  28. data/lib/rgeo/cartesian/feature_classes.rb +173 -0
  29. data/lib/rgeo/cartesian/feature_methods.rb +106 -0
  30. data/lib/rgeo/cartesian/interface.rb +150 -0
  31. data/lib/rgeo/coord_sys.rb +79 -0
  32. data/lib/rgeo/coord_sys/cs/entities.rb +1524 -0
  33. data/lib/rgeo/coord_sys/cs/factories.rb +208 -0
  34. data/lib/rgeo/coord_sys/cs/wkt_parser.rb +308 -0
  35. data/lib/rgeo/coord_sys/proj4.rb +312 -0
  36. data/lib/rgeo/coord_sys/srs_database/active_record_table.rb +194 -0
  37. data/lib/rgeo/coord_sys/srs_database/interface.rb +165 -0
  38. data/lib/rgeo/coord_sys/srs_database/proj4_data.rb +188 -0
  39. data/lib/rgeo/coord_sys/srs_database/sr_org.rb +108 -0
  40. data/lib/rgeo/coord_sys/srs_database/url_reader.rb +108 -0
  41. data/lib/rgeo/error.rb +63 -0
  42. data/lib/rgeo/feature.rb +88 -0
  43. data/lib/rgeo/feature/curve.rb +156 -0
  44. data/lib/rgeo/feature/factory.rb +332 -0
  45. data/lib/rgeo/feature/factory_generator.rb +138 -0
  46. data/lib/rgeo/feature/geometry.rb +614 -0
  47. data/lib/rgeo/feature/geometry_collection.rb +129 -0
  48. data/lib/rgeo/feature/line.rb +66 -0
  49. data/lib/rgeo/feature/line_string.rb +102 -0
  50. data/lib/rgeo/feature/linear_ring.rb +66 -0
  51. data/lib/rgeo/feature/multi_curve.rb +113 -0
  52. data/lib/rgeo/feature/multi_line_string.rb +66 -0
  53. data/lib/rgeo/feature/multi_point.rb +73 -0
  54. data/lib/rgeo/feature/multi_polygon.rb +97 -0
  55. data/lib/rgeo/feature/multi_surface.rb +116 -0
  56. data/lib/rgeo/feature/point.rb +120 -0
  57. data/lib/rgeo/feature/polygon.rb +141 -0
  58. data/lib/rgeo/feature/surface.rb +122 -0
  59. data/lib/rgeo/feature/types.rb +305 -0
  60. data/lib/rgeo/geographic.rb +75 -0
  61. data/lib/rgeo/geographic/factory.rb +287 -0
  62. data/lib/rgeo/geographic/interface.rb +410 -0
  63. data/lib/rgeo/geographic/proj4_projector.rb +98 -0
  64. data/lib/rgeo/geographic/projected_feature_classes.rb +213 -0
  65. data/lib/rgeo/geographic/projected_feature_methods.rb +228 -0
  66. data/lib/rgeo/geographic/projected_window.rb +467 -0
  67. data/lib/rgeo/geographic/simple_mercator_projector.rb +157 -0
  68. data/lib/rgeo/geographic/spherical_feature_classes.rb +212 -0
  69. data/lib/rgeo/geographic/spherical_feature_methods.rb +97 -0
  70. data/lib/rgeo/geographic/spherical_math.rb +206 -0
  71. data/lib/rgeo/geos.rb +72 -0
  72. data/lib/rgeo/geos/factory.rb +301 -0
  73. data/lib/rgeo/geos/impl_additions.rb +76 -0
  74. data/lib/rgeo/geos/interface.rb +139 -0
  75. data/lib/rgeo/geos/zm_factory.rb +275 -0
  76. data/lib/rgeo/geos/zm_impl.rb +432 -0
  77. data/lib/rgeo/impl_helper.rb +53 -0
  78. data/lib/rgeo/impl_helper/basic_geometry_collection_methods.rb +235 -0
  79. data/lib/rgeo/impl_helper/basic_geometry_methods.rb +85 -0
  80. data/lib/rgeo/impl_helper/basic_line_string_methods.rb +197 -0
  81. data/lib/rgeo/impl_helper/basic_point_methods.rb +138 -0
  82. data/lib/rgeo/impl_helper/basic_polygon_methods.rb +121 -0
  83. data/lib/rgeo/impl_helper/math.rb +50 -0
  84. data/lib/rgeo/version.rb +52 -0
  85. data/lib/rgeo/wkrep.rb +72 -0
  86. data/lib/rgeo/wkrep/wkb_generator.rb +267 -0
  87. data/lib/rgeo/wkrep/wkb_parser.rb +315 -0
  88. data/lib/rgeo/wkrep/wkt_generator.rb +275 -0
  89. data/lib/rgeo/wkrep/wkt_parser.rb +496 -0
  90. data/test/common/geometry_collection_tests.rb +238 -0
  91. data/test/common/line_string_tests.rb +324 -0
  92. data/test/common/multi_line_string_tests.rb +209 -0
  93. data/test/common/multi_point_tests.rb +201 -0
  94. data/test/common/multi_polygon_tests.rb +208 -0
  95. data/test/common/point_tests.rb +331 -0
  96. data/test/common/polygon_tests.rb +232 -0
  97. data/test/coord_sys/tc_active_record_table.rb +102 -0
  98. data/test/coord_sys/tc_ogc_cs.rb +356 -0
  99. data/test/coord_sys/tc_proj4.rb +138 -0
  100. data/test/coord_sys/tc_proj4_srs_data.rb +76 -0
  101. data/test/coord_sys/tc_sr_org.rb +70 -0
  102. data/test/coord_sys/tc_url_reader.rb +82 -0
  103. data/test/geos/tc_factory.rb +91 -0
  104. data/test/geos/tc_geometry_collection.rb +62 -0
  105. data/test/geos/tc_line_string.rb +62 -0
  106. data/test/geos/tc_misc.rb +72 -0
  107. data/test/geos/tc_multi_line_string.rb +62 -0
  108. data/test/geos/tc_multi_point.rb +62 -0
  109. data/test/geos/tc_multi_polygon.rb +63 -0
  110. data/test/geos/tc_point.rb +86 -0
  111. data/test/geos/tc_polygon.rb +86 -0
  112. data/test/geos/tc_zmfactory.rb +85 -0
  113. data/test/projected_geographic/tc_geometry_collection.rb +62 -0
  114. data/test/projected_geographic/tc_line_string.rb +62 -0
  115. data/test/projected_geographic/tc_multi_line_string.rb +62 -0
  116. data/test/projected_geographic/tc_multi_point.rb +62 -0
  117. data/test/projected_geographic/tc_multi_polygon.rb +63 -0
  118. data/test/projected_geographic/tc_point.rb +93 -0
  119. data/test/projected_geographic/tc_polygon.rb +62 -0
  120. data/test/simple_cartesian/tc_calculations.rb +145 -0
  121. data/test/simple_cartesian/tc_geometry_collection.rb +69 -0
  122. data/test/simple_cartesian/tc_line_string.rb +70 -0
  123. data/test/simple_cartesian/tc_multi_line_string.rb +67 -0
  124. data/test/simple_cartesian/tc_multi_point.rb +67 -0
  125. data/test/simple_cartesian/tc_multi_polygon.rb +70 -0
  126. data/test/simple_cartesian/tc_point.rb +91 -0
  127. data/test/simple_cartesian/tc_polygon.rb +67 -0
  128. data/test/simple_mercator/tc_geometry_collection.rb +62 -0
  129. data/test/simple_mercator/tc_line_string.rb +62 -0
  130. data/test/simple_mercator/tc_multi_line_string.rb +62 -0
  131. data/test/simple_mercator/tc_multi_point.rb +62 -0
  132. data/test/simple_mercator/tc_multi_polygon.rb +63 -0
  133. data/test/simple_mercator/tc_point.rb +93 -0
  134. data/test/simple_mercator/tc_polygon.rb +62 -0
  135. data/test/simple_mercator/tc_window.rb +219 -0
  136. data/test/spherical_geographic/tc_calculations.rb +203 -0
  137. data/test/spherical_geographic/tc_geometry_collection.rb +70 -0
  138. data/test/spherical_geographic/tc_line_string.rb +70 -0
  139. data/test/spherical_geographic/tc_multi_line_string.rb +67 -0
  140. data/test/spherical_geographic/tc_multi_point.rb +67 -0
  141. data/test/spherical_geographic/tc_multi_polygon.rb +70 -0
  142. data/test/spherical_geographic/tc_point.rb +100 -0
  143. data/test/spherical_geographic/tc_polygon.rb +67 -0
  144. data/test/tc_cartesian_analysis.rb +107 -0
  145. data/test/tc_oneoff.rb +63 -0
  146. data/test/wkrep/tc_wkb_generator.rb +249 -0
  147. data/test/wkrep/tc_wkb_parser.rb +353 -0
  148. data/test/wkrep/tc_wkt_generator.rb +362 -0
  149. data/test/wkrep/tc_wkt_parser.rb +480 -0
  150. metadata +267 -0
@@ -0,0 +1,55 @@
1
+ /*
2
+ -----------------------------------------------------------------------------
3
+
4
+ Geometry base class methods for GEOS wrapper
5
+
6
+ -----------------------------------------------------------------------------
7
+ Copyright 2010 Daniel Azuma
8
+
9
+ All rights reserved.
10
+
11
+ Redistribution and use in source and binary forms, with or without
12
+ modification, are permitted provided that the following conditions are met:
13
+
14
+ * Redistributions of source code must retain the above copyright notice,
15
+ this list of conditions and the following disclaimer.
16
+ * Redistributions in binary form must reproduce the above copyright notice,
17
+ this list of conditions and the following disclaimer in the documentation
18
+ and/or other materials provided with the distribution.
19
+ * Neither the name of the copyright holder, nor the names of any other
20
+ contributors to this software, may be used to endorse or promote products
21
+ derived from this software without specific prior written permission.
22
+
23
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33
+ POSSIBILITY OF SUCH DAMAGE.
34
+ -----------------------------------------------------------------------------
35
+ */
36
+
37
+
38
+ #ifndef RGEO_GEOS_GEOMETRY_INCLUDED
39
+ #define RGEO_GEOS_GEOMETRY_INCLUDED
40
+
41
+ #include "factory.h"
42
+
43
+ RGEO_BEGIN_C
44
+
45
+
46
+ /*
47
+ Initializes the geometry module. This should be called after the factory
48
+ module is initialized, but before any of the other modules.
49
+ */
50
+ void rgeo_init_geos_geometry(RGeo_Globals* globals);
51
+
52
+
53
+ RGEO_END_C
54
+
55
+ #endif
@@ -0,0 +1,482 @@
1
+ /*
2
+ -----------------------------------------------------------------------------
3
+
4
+ Geometry collection methods for GEOS wrapper
5
+
6
+ -----------------------------------------------------------------------------
7
+ Copyright 2010 Daniel Azuma
8
+
9
+ All rights reserved.
10
+
11
+ Redistribution and use in source and binary forms, with or without
12
+ modification, are permitted provided that the following conditions are met:
13
+
14
+ * Redistributions of source code must retain the above copyright notice,
15
+ this list of conditions and the following disclaimer.
16
+ * Redistributions in binary form must reproduce the above copyright notice,
17
+ this list of conditions and the following disclaimer in the documentation
18
+ and/or other materials provided with the distribution.
19
+ * Neither the name of the copyright holder, nor the names of any other
20
+ contributors to this software, may be used to endorse or promote products
21
+ derived from this software without specific prior written permission.
22
+
23
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33
+ POSSIBILITY OF SUCH DAMAGE.
34
+ -----------------------------------------------------------------------------
35
+ */
36
+
37
+
38
+ #include "preface.h"
39
+
40
+ #ifdef RGEO_GEOS_SUPPORTED
41
+
42
+ #include <ruby.h>
43
+ #include <geos_c.h>
44
+
45
+ #include "factory.h"
46
+ #include "geometry.h"
47
+ #include "line_string.h"
48
+ #include "polygon.h"
49
+ #include "geometry_collection.h"
50
+
51
+ RGEO_BEGIN_C
52
+
53
+
54
+ /**** INTERNAL IMPLEMENTATION OF CREATE ****/
55
+
56
+
57
+ // Main implementation of the "create" class method for geometry collections.
58
+ // You must pass in the correct GEOS geometry type ID.
59
+
60
+ static VALUE create_geometry_collection(VALUE module, int type, VALUE factory, VALUE array)
61
+ {
62
+ VALUE result = Qnil;
63
+ Check_Type(array, T_ARRAY);
64
+ unsigned int len = (unsigned int)RARRAY_LEN(array);
65
+ GEOSGeometry** geoms = ALLOC_N(GEOSGeometry*, len == 0 ? 1 : len);
66
+ if (geoms) {
67
+ RGeo_FactoryData* factory_data = RGEO_FACTORY_DATA_PTR(factory);
68
+ GEOSContextHandle_t geos_context = factory_data->geos_context;
69
+ VALUE klass;
70
+ unsigned int i,j;
71
+ VALUE klasses = Qnil;
72
+ VALUE cast_type = Qnil;
73
+ switch (type) {
74
+ case GEOS_MULTIPOINT:
75
+ cast_type = factory_data->globals->feature_point;
76
+ break;
77
+ case GEOS_MULTILINESTRING:
78
+ cast_type = factory_data->globals->feature_line_string;
79
+ break;
80
+ case GEOS_MULTIPOLYGON:
81
+ cast_type = factory_data->globals->feature_polygon;
82
+ break;
83
+ }
84
+ for (i=0; i<len; ++i) {
85
+ GEOSGeometry* geom = rgeo_convert_to_detached_geos_geometry(rb_ary_entry(array, i), factory, cast_type, &klass);
86
+ if (!geom) {
87
+ break;
88
+ }
89
+ geoms[i] = geom;
90
+ if (!NIL_P(klass) && NIL_P(klasses)) {
91
+ klasses = rb_ary_new2(len);
92
+ for (j=0; j<i; ++j) {
93
+ rb_ary_push(klasses, Qnil);
94
+ }
95
+ }
96
+ if (!NIL_P(klasses)) {
97
+ rb_ary_push(klasses, klass);
98
+ }
99
+ }
100
+ if (i != len) {
101
+ for (j=0; j<i; ++j) {
102
+ GEOSGeom_destroy_r(geos_context, geoms[j]);
103
+ }
104
+ }
105
+ else {
106
+ GEOSGeometry* collection = GEOSGeom_createCollection_r(geos_context, type, geoms, len);
107
+ // Due to a limitation of GEOS, the MultiPolygon assertions are not checked.
108
+ // We do that manually here.
109
+ if (collection && type == GEOS_MULTIPOLYGON && (factory_data->flags & 1) == 0) {
110
+ char problem = 0;
111
+ for (i=1; i<len; ++i) {
112
+ for (j=0; j<i; ++j) {
113
+ GEOSGeometry* igeom = geoms[i];
114
+ GEOSGeometry* jgeom = geoms[j];
115
+ problem = GEOSRelatePattern_r(geos_context, igeom, jgeom, "2********");
116
+ if (problem) {
117
+ break;
118
+ }
119
+ problem = GEOSRelatePattern_r(geos_context, igeom, jgeom, "****1****");
120
+ if (problem) {
121
+ break;
122
+ }
123
+ }
124
+ if (problem) {
125
+ break;
126
+ }
127
+ }
128
+ if (problem) {
129
+ GEOSGeom_destroy_r(geos_context, collection);
130
+ collection = NULL;
131
+ }
132
+ }
133
+ if (collection) {
134
+ result = rgeo_wrap_geos_geometry(factory, collection, module);
135
+ RGEO_GEOMETRY_DATA_PTR(result)->klasses = klasses;
136
+ }
137
+ // NOTE: We are assuming that GEOS will do its own cleanup of the
138
+ // element geometries if it fails to create the collection, so we
139
+ // are not doing that ourselves. If that turns out not to be the
140
+ // case, this will be a memory leak.
141
+ }
142
+ free(geoms);
143
+ }
144
+
145
+ return result;
146
+ }
147
+
148
+
149
+ /**** RUBY METHOD DEFINITIONS ****/
150
+
151
+
152
+ static VALUE method_geometry_collection_eql(VALUE self, VALUE rhs)
153
+ {
154
+ VALUE result = rgeo_geos_klasses_and_factories_eql(self, rhs);
155
+ if (RTEST(result)) {
156
+ RGeo_GeometryData* self_data = RGEO_GEOMETRY_DATA_PTR(self);
157
+ result = rgeo_geos_geometry_collections_eql(self_data->geos_context, self_data->geom, RGEO_GEOMETRY_DATA_PTR(rhs)->geom, RGEO_FACTORY_DATA_PTR(self_data->factory)->flags & RGEO_FACTORYFLAGS_SUPPORTS_Z_OR_M);
158
+ }
159
+ return result;
160
+ }
161
+
162
+
163
+ static VALUE method_geometry_collection_geometry_type(VALUE self)
164
+ {
165
+ VALUE result = Qnil;
166
+ RGeo_GeometryData* self_data = RGEO_GEOMETRY_DATA_PTR(self);
167
+ if (self_data->geom) {
168
+ result = RGEO_FACTORY_DATA_PTR(self_data->factory)->globals->feature_geometry_collection;
169
+ }
170
+ return result;
171
+ }
172
+
173
+
174
+ static VALUE method_geometry_collection_num_geometries(VALUE self)
175
+ {
176
+ VALUE result = Qnil;
177
+ RGeo_GeometryData* self_data = RGEO_GEOMETRY_DATA_PTR(self);
178
+ const GEOSGeometry* self_geom = self_data->geom;
179
+ if (self_geom) {
180
+ result = INT2NUM(GEOSGetNumGeometries_r(self_data->geos_context, self_geom));
181
+ }
182
+ return result;
183
+ }
184
+
185
+
186
+ static VALUE method_geometry_collection_geometry_n(VALUE self, VALUE n)
187
+ {
188
+ VALUE result = Qnil;
189
+ RGeo_GeometryData* self_data = RGEO_GEOMETRY_DATA_PTR(self);
190
+ const GEOSGeometry* self_geom = self_data->geom;
191
+ if (self_geom) {
192
+ VALUE klasses = self_data->klasses;
193
+ int i = NUM2INT(n);
194
+ const GEOSGeometry* elem_geom = GEOSGetGeometryN_r(self_data->geos_context, self_geom, i);
195
+ result = rgeo_wrap_geos_geometry_clone(self_data->factory, elem_geom, NIL_P(klasses) ? Qnil : rb_ary_entry(klasses, i));
196
+ }
197
+ return result;
198
+ }
199
+
200
+
201
+ static VALUE method_geometry_collection_each(VALUE self)
202
+ {
203
+ RGeo_GeometryData* self_data = RGEO_GEOMETRY_DATA_PTR(self);
204
+ const GEOSGeometry* self_geom = self_data->geom;
205
+ if (self_geom) {
206
+ GEOSContextHandle_t self_context = self_data->geos_context;
207
+ int len = GEOSGetNumGeometries_r(self_context, self_geom);
208
+ if (len > 0) {
209
+ VALUE klasses = self_data->klasses;
210
+ int i;
211
+ for (i=0; i<len; ++i) {
212
+ VALUE elem;
213
+ const GEOSGeometry* elem_geom = GEOSGetGeometryN_r(self_context, self_geom, i);
214
+ elem = rgeo_wrap_geos_geometry_clone(self_data->factory, elem_geom, NIL_P(klasses) ? Qnil : rb_ary_entry(klasses, i));
215
+ if (!NIL_P(elem)) {
216
+ rb_yield(elem);
217
+ }
218
+ }
219
+ }
220
+ }
221
+ return self;
222
+ }
223
+
224
+
225
+ static VALUE method_multi_point_geometry_type(VALUE self)
226
+ {
227
+ VALUE result = Qnil;
228
+ RGeo_GeometryData* self_data = RGEO_GEOMETRY_DATA_PTR(self);
229
+ if (self_data->geom) {
230
+ result = RGEO_FACTORY_DATA_PTR(self_data->factory)->globals->feature_multi_point;
231
+ }
232
+ return result;
233
+ }
234
+
235
+
236
+ static VALUE method_multi_line_string_geometry_type(VALUE self)
237
+ {
238
+ VALUE result = Qnil;
239
+ RGeo_GeometryData* self_data = RGEO_GEOMETRY_DATA_PTR(self);
240
+ if (self_data->geom) {
241
+ result = RGEO_FACTORY_DATA_PTR(self_data->factory)->globals->feature_multi_line_string;
242
+ }
243
+ return result;
244
+ }
245
+
246
+
247
+ static VALUE method_multi_line_string_length(VALUE self)
248
+ {
249
+ VALUE result = Qnil;
250
+ RGeo_GeometryData* self_data = RGEO_GEOMETRY_DATA_PTR(self);
251
+ const GEOSGeometry* self_geom = self_data->geom;
252
+ if (self_geom) {
253
+ double len;
254
+ if (GEOSLength_r(self_data->geos_context, self_geom, &len)) {
255
+ result = rb_float_new(len);
256
+ }
257
+ }
258
+ return result;
259
+ }
260
+
261
+
262
+ static VALUE method_multi_line_string_is_closed(VALUE self)
263
+ {
264
+ VALUE result = Qnil;
265
+ RGeo_GeometryData* self_data = RGEO_GEOMETRY_DATA_PTR(self);
266
+ const GEOSGeometry* self_geom = self_data->geom;
267
+ if (self_geom) {
268
+ GEOSContextHandle_t self_context = self_data->geos_context;
269
+ result = Qtrue;
270
+ int len = GEOSGetNumGeometries_r(self_context, self_geom);
271
+ if (len > 0) {
272
+ int i;
273
+ for (i=0; i<len; ++i) {
274
+ const GEOSGeometry* geom = GEOSGetGeometryN_r(self_context, self_geom, i);
275
+ if (geom) {
276
+ result = rgeo_is_geos_line_string_closed(self_context, self_geom);
277
+ if (result != Qtrue) {
278
+ break;
279
+ }
280
+ }
281
+ }
282
+ }
283
+ }
284
+ return result;
285
+ }
286
+
287
+
288
+ static VALUE method_multi_polygon_geometry_type(VALUE self)
289
+ {
290
+ VALUE result = Qnil;
291
+ RGeo_GeometryData* self_data = RGEO_GEOMETRY_DATA_PTR(self);
292
+ if (self_data->geom) {
293
+ result = RGEO_FACTORY_DATA_PTR(self_data->factory)->globals->feature_multi_polygon;
294
+ }
295
+ return result;
296
+ }
297
+
298
+
299
+ static VALUE method_multi_polygon_area(VALUE self)
300
+ {
301
+ VALUE result = Qnil;
302
+ RGeo_GeometryData* self_data = RGEO_GEOMETRY_DATA_PTR(self);
303
+ const GEOSGeometry* self_geom = self_data->geom;
304
+ if (self_geom) {
305
+ double area;
306
+ if (GEOSArea_r(self_data->geos_context, self_geom, &area)) {
307
+ result = rb_float_new(area);
308
+ }
309
+ }
310
+ return result;
311
+ }
312
+
313
+
314
+ static VALUE method_multi_polygon_centroid(VALUE self)
315
+ {
316
+ VALUE result = Qnil;
317
+ RGeo_GeometryData* self_data = RGEO_GEOMETRY_DATA_PTR(self);
318
+ const GEOSGeometry* self_geom = self_data->geom;
319
+ if (self_geom) {
320
+ result = rgeo_wrap_geos_geometry(self_data->factory, GEOSGetCentroid_r(self_data->geos_context, self_geom), Qnil);
321
+ }
322
+ return result;
323
+ }
324
+
325
+
326
+ static VALUE method_multi_polygon_point_on_surface(VALUE self)
327
+ {
328
+ VALUE result = Qnil;
329
+ RGeo_GeometryData* self_data = RGEO_GEOMETRY_DATA_PTR(self);
330
+ const GEOSGeometry* self_geom = self_data->geom;
331
+ if (self_geom) {
332
+ result = rgeo_wrap_geos_geometry(self_data->factory, GEOSPointOnSurface_r(self_data->geos_context, self_geom), Qnil);
333
+ }
334
+ return result;
335
+ }
336
+
337
+
338
+ static VALUE cmethod_geometry_collection_create(VALUE module, VALUE factory, VALUE array)
339
+ {
340
+ return create_geometry_collection(module, GEOS_GEOMETRYCOLLECTION, factory, array);
341
+ }
342
+
343
+
344
+ static VALUE cmethod_multi_point_create(VALUE module, VALUE factory, VALUE array)
345
+ {
346
+ return create_geometry_collection(module, GEOS_MULTIPOINT, factory, array);
347
+ }
348
+
349
+
350
+ static VALUE cmethod_multi_line_string_create(VALUE module, VALUE factory, VALUE array)
351
+ {
352
+ return create_geometry_collection(module, GEOS_MULTILINESTRING, factory, array);
353
+ }
354
+
355
+
356
+ static VALUE cmethod_multi_polygon_create(VALUE module, VALUE factory, VALUE array)
357
+ {
358
+ return create_geometry_collection(module, GEOS_MULTIPOLYGON, factory, array);
359
+ }
360
+
361
+
362
+ /**** INITIALIZATION FUNCTION ****/
363
+
364
+
365
+ void rgeo_init_geos_geometry_collection(RGeo_Globals* globals)
366
+ {
367
+ // Create implementation classes
368
+ VALUE geos_geometry_collection_class = rb_define_class_under(globals->geos_module, "GeometryCollectionImpl", globals->geos_geometry);
369
+ globals->geos_geometry_collection = geos_geometry_collection_class;
370
+ globals->feature_geometry_collection = rb_const_get_at(globals->feature_module, rb_intern("GeometryCollection"));
371
+ VALUE geos_multi_point_class = rb_define_class_under(globals->geos_module, "MultiPointImpl", geos_geometry_collection_class);
372
+ globals->geos_multi_point = geos_multi_point_class;
373
+ globals->feature_multi_point = rb_const_get_at(globals->feature_module, rb_intern("MultiPoint"));
374
+ VALUE geos_multi_line_string_class = rb_define_class_under(globals->geos_module, "MultiLineStringImpl", geos_geometry_collection_class);
375
+ globals->geos_multi_line_string = geos_multi_line_string_class;
376
+ globals->feature_multi_line_string = rb_const_get_at(globals->feature_module, rb_intern("MultiLineString"));
377
+ VALUE geos_multi_polygon_class = rb_define_class_under(globals->geos_module, "MultiPolygonImpl", geos_geometry_collection_class);
378
+ globals->geos_multi_polygon = geos_multi_polygon_class;
379
+ globals->feature_multi_polygon = rb_const_get_at(globals->feature_module, rb_intern("MultiPolygon"));
380
+
381
+ // Methods for GeometryCollectionImpl
382
+ rb_define_module_function(geos_geometry_collection_class, "create", cmethod_geometry_collection_create, 2);
383
+ rb_include_module(geos_geometry_collection_class, rb_define_module("Enumerable"));
384
+ rb_define_method(geos_geometry_collection_class, "eql?", method_geometry_collection_eql, 1);
385
+ rb_define_method(geos_geometry_collection_class, "geometry_type", method_geometry_collection_geometry_type, 0);
386
+ rb_define_method(geos_geometry_collection_class, "num_geometries", method_geometry_collection_num_geometries, 0);
387
+ rb_define_method(geos_geometry_collection_class, "size", method_geometry_collection_num_geometries, 0);
388
+ rb_define_method(geos_geometry_collection_class, "geometry_n", method_geometry_collection_geometry_n, 1);
389
+ rb_define_method(geos_geometry_collection_class, "[]", method_geometry_collection_geometry_n, 1);
390
+ rb_define_method(geos_geometry_collection_class, "each", method_geometry_collection_each, 0);
391
+
392
+ // Methods for MultiPointImpl
393
+ rb_define_module_function(geos_multi_point_class, "create", cmethod_multi_point_create, 2);
394
+ rb_define_method(geos_multi_point_class, "geometry_type", method_multi_point_geometry_type, 0);
395
+
396
+ // Methods for MultiLineStringImpl
397
+ rb_define_module_function(geos_multi_line_string_class, "create", cmethod_multi_line_string_create, 2);
398
+ rb_define_method(geos_multi_line_string_class, "geometry_type", method_multi_line_string_geometry_type, 0);
399
+ rb_define_method(geos_multi_line_string_class, "length", method_multi_line_string_length, 0);
400
+ rb_define_method(geos_multi_line_string_class, "is_closed?", method_multi_line_string_is_closed, 0);
401
+
402
+ // Methods for MultiPolygonImpl
403
+ rb_define_module_function(geos_multi_polygon_class, "create", cmethod_multi_polygon_create, 2);
404
+ rb_define_method(geos_multi_polygon_class, "geometry_type", method_multi_polygon_geometry_type, 0);
405
+ rb_define_method(geos_multi_polygon_class, "area", method_multi_polygon_area, 0);
406
+ rb_define_method(geos_multi_polygon_class, "centroid", method_multi_polygon_centroid, 0);
407
+ rb_define_method(geos_multi_polygon_class, "point_on_surface", method_multi_polygon_point_on_surface, 0);
408
+ }
409
+
410
+
411
+ /**** OTHER PUBLIC FUNCTIONS ****/
412
+
413
+
414
+ VALUE rgeo_geos_geometry_collections_eql(GEOSContextHandle_t context, const GEOSGeometry* geom1, const GEOSGeometry* geom2, char check_z)
415
+ {
416
+ VALUE result = Qnil;
417
+ if (geom1 && geom2) {
418
+ int len1 = GEOSGetNumGeometries_r(context, geom1);
419
+ int len2 = GEOSGetNumGeometries_r(context, geom2);
420
+ if (len1 >= 0 && len2 >= 0) {
421
+ if (len1 == len2) {
422
+ result = Qtrue;
423
+ int i;
424
+ for (i=0; i<len1; ++i) {
425
+ const GEOSGeometry* sub_geom1 = GEOSGetGeometryN_r(context, geom1, i);
426
+ const GEOSGeometry* sub_geom2 = GEOSGetGeometryN_r(context, geom2, i);
427
+ if (sub_geom1 && sub_geom2) {
428
+ int type1 = GEOSGeomTypeId_r(context, sub_geom1);
429
+ int type2 = GEOSGeomTypeId_r(context, sub_geom2);
430
+ if (type1 >= 0 && type2 >= 0) {
431
+ if (type1 == type2) {
432
+ switch (type1) {
433
+ case GEOS_POINT:
434
+ case GEOS_LINESTRING:
435
+ case GEOS_LINEARRING:
436
+ result = rgeo_geos_coordseqs_eql(context, sub_geom1, sub_geom2, check_z);
437
+ break;
438
+ case GEOS_POLYGON:
439
+ result = rgeo_geos_polygons_eql(context, sub_geom1, sub_geom2, check_z);
440
+ break;
441
+ case GEOS_GEOMETRYCOLLECTION:
442
+ case GEOS_MULTIPOINT:
443
+ case GEOS_MULTILINESTRING:
444
+ case GEOS_MULTIPOLYGON:
445
+ result = rgeo_geos_geometry_collections_eql(context, sub_geom1, sub_geom2, check_z);
446
+ break;
447
+ default:
448
+ result = Qnil;
449
+ break;
450
+ }
451
+ if (!RTEST(result)) {
452
+ break;
453
+ }
454
+ }
455
+ else {
456
+ result = Qfalse;
457
+ break;
458
+ }
459
+ }
460
+ else {
461
+ result = Qnil;
462
+ break;
463
+ }
464
+ }
465
+ else {
466
+ result = Qnil;
467
+ break;
468
+ }
469
+ }
470
+ }
471
+ else {
472
+ result = Qfalse;
473
+ }
474
+ }
475
+ }
476
+ return result;
477
+ }
478
+
479
+
480
+ RGEO_END_C
481
+
482
+ #endif