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,69 @@
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
+ #ifndef RGEO_GEOS_GEOMETRY_COLLECTION_INCLUDED
39
+ #define RGEO_GEOS_GEOMETRY_COLLECTION_INCLUDED
40
+
41
+ #include <ruby.h>
42
+ #include <geos_c.h>
43
+
44
+ #include "factory.h"
45
+
46
+ RGEO_BEGIN_C
47
+
48
+
49
+ /*
50
+ Initializes the geometry collection module. This should be called after
51
+ the geometry module is initialized.
52
+ */
53
+ void rgeo_init_geos_geometry_collection(RGeo_Globals* globals);
54
+
55
+ /*
56
+ Comopares the contents of two geometry collections. Does not test the
57
+ types of the collections themselves, but tests the types, values, and
58
+ contents of all the contents. The two given geometries MUST be
59
+ collection types-- i.e. GeometryCollection, MultiPoint, MultiLineString,
60
+ or MultiPolygon.
61
+ Returns Qtrue if the contents of the two geometry collections are equal,
62
+ Qfalse if they are inequal, or Qnil if an error occurs.
63
+ */
64
+ VALUE rgeo_geos_geometry_collections_eql(GEOSContextHandle_t context, const GEOSGeometry* geom1, const GEOSGeometry* geom2, char check_z);
65
+
66
+
67
+ RGEO_END_C
68
+
69
+ #endif
@@ -0,0 +1,509 @@
1
+ /*
2
+ -----------------------------------------------------------------------------
3
+
4
+ Line string 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 <string.h>
43
+ #include <ruby.h>
44
+ #include <geos_c.h>
45
+
46
+ #include "factory.h"
47
+ #include "geometry.h"
48
+ #include "point.h"
49
+ #include "line_string.h"
50
+
51
+ RGEO_BEGIN_C
52
+
53
+
54
+ static VALUE method_line_string_geometry_type(VALUE self)
55
+ {
56
+ VALUE result = Qnil;
57
+ RGeo_GeometryData* self_data = RGEO_GEOMETRY_DATA_PTR(self);
58
+ if (self_data->geom) {
59
+ result = RGEO_FACTORY_DATA_PTR(self_data->factory)->globals->feature_line_string;
60
+ }
61
+ return result;
62
+ }
63
+
64
+
65
+ static VALUE method_linear_ring_geometry_type(VALUE self)
66
+ {
67
+ VALUE result = Qnil;
68
+ RGeo_GeometryData* self_data = RGEO_GEOMETRY_DATA_PTR(self);
69
+ if (self_data->geom) {
70
+ result = RGEO_FACTORY_DATA_PTR(self_data->factory)->globals->feature_linear_ring;
71
+ }
72
+ return result;
73
+ }
74
+
75
+
76
+ static VALUE method_line_geometry_type(VALUE self)
77
+ {
78
+ VALUE result = Qnil;
79
+ RGeo_GeometryData* self_data = RGEO_GEOMETRY_DATA_PTR(self);
80
+ if (self_data->geom) {
81
+ result = RGEO_FACTORY_DATA_PTR(self_data->factory)->globals->feature_line;
82
+ }
83
+ return result;
84
+ }
85
+
86
+
87
+ static VALUE method_line_string_length(VALUE self)
88
+ {
89
+ VALUE result = Qnil;
90
+ RGeo_GeometryData* self_data = RGEO_GEOMETRY_DATA_PTR(self);
91
+ const GEOSGeometry* self_geom = self_data->geom;
92
+ if (self_geom) {
93
+ double len;
94
+ if (GEOSLength_r(self_data->geos_context, self_geom, &len)) {
95
+ result = rb_float_new(len);
96
+ }
97
+ }
98
+ return result;
99
+ }
100
+
101
+
102
+ static VALUE method_line_string_num_points(VALUE self)
103
+ {
104
+ VALUE result = Qnil;
105
+ RGeo_GeometryData* self_data = RGEO_GEOMETRY_DATA_PTR(self);
106
+ const GEOSGeometry* self_geom = self_data->geom;
107
+ if (self_geom) {
108
+ result = INT2NUM(GEOSGetNumCoordinates_r(self_data->geos_context, self_geom));
109
+ }
110
+ return result;
111
+ }
112
+
113
+
114
+ static VALUE get_point_from_coordseq(VALUE self, const GEOSCoordSequence* coord_seq, unsigned int i, char has_z)
115
+ {
116
+ VALUE result = Qnil;
117
+ double x, y, z;
118
+ RGeo_GeometryData* self_data = RGEO_GEOMETRY_DATA_PTR(self);
119
+ GEOSContextHandle_t self_context = self_data->geos_context;
120
+ if (GEOSCoordSeq_getX_r(self_context, coord_seq, i, &x)) {
121
+ if (GEOSCoordSeq_getY_r(self_context, coord_seq, i, &y)) {
122
+ if (has_z) {
123
+ if (!GEOSCoordSeq_getZ_r(self_context, coord_seq, i, &z)) {
124
+ z = 0.0;
125
+ }
126
+ }
127
+ else {
128
+ z = 0.0;
129
+ }
130
+ result = rgeo_create_geos_point(self_data->factory, x, y, z);
131
+ }
132
+ }
133
+ return result;
134
+ }
135
+
136
+
137
+ static VALUE method_line_string_point_n(VALUE self, VALUE n)
138
+ {
139
+ VALUE result = Qnil;
140
+ RGeo_GeometryData* self_data = RGEO_GEOMETRY_DATA_PTR(self);
141
+ const GEOSGeometry* self_geom = self_data->geom;
142
+ if (self_geom) {
143
+ GEOSContextHandle_t self_context = self_data->geos_context;
144
+ const GEOSCoordSequence* coord_seq = GEOSGeom_getCoordSeq_r(self_context, self_geom);
145
+ if (coord_seq) {
146
+ char has_z = (char)(RGEO_FACTORY_DATA_PTR(self_data->factory)->flags & RGEO_FACTORYFLAGS_SUPPORTS_Z_OR_M);
147
+ int si = NUM2INT(n);
148
+ if (si >= 0) {
149
+ unsigned int i = si;
150
+ unsigned int size;
151
+ if (GEOSCoordSeq_getSize_r(self_context, coord_seq, &size)) {
152
+ if (i < size) {
153
+ unsigned int dims;
154
+ result = get_point_from_coordseq(self, coord_seq, i, has_z);
155
+ }
156
+ }
157
+ }
158
+ }
159
+ }
160
+ return result;
161
+ }
162
+
163
+
164
+ static VALUE method_line_string_points(VALUE self)
165
+ {
166
+ VALUE result = Qnil;
167
+ RGeo_GeometryData* self_data = RGEO_GEOMETRY_DATA_PTR(self);
168
+ const GEOSGeometry* self_geom = self_data->geom;
169
+ if (self_geom) {
170
+ GEOSContextHandle_t self_context = self_data->geos_context;
171
+ const GEOSCoordSequence* coord_seq = GEOSGeom_getCoordSeq_r(self_context, self_geom);
172
+ if (coord_seq) {
173
+ char has_z = (char)(RGEO_FACTORY_DATA_PTR(self_data->factory)->flags & RGEO_FACTORYFLAGS_SUPPORTS_Z_OR_M);
174
+ unsigned int size;
175
+ if (GEOSCoordSeq_getSize_r(self_context, coord_seq, &size)) {
176
+ result = rb_ary_new2(size);
177
+ double x, y, z;
178
+ unsigned int i;
179
+ for (i=0; i<size; ++i) {
180
+ VALUE point = get_point_from_coordseq(self, coord_seq, i, has_z);
181
+ if (!NIL_P(point)) {
182
+ rb_ary_store(result, i, point);
183
+ }
184
+ }
185
+ }
186
+ }
187
+ }
188
+ return result;
189
+ }
190
+
191
+
192
+ static VALUE method_line_string_start_point(VALUE self)
193
+ {
194
+ return method_line_string_point_n(self, INT2NUM(0));
195
+ }
196
+
197
+
198
+ static VALUE method_line_string_end_point(VALUE self)
199
+ {
200
+ VALUE result = Qnil;
201
+ RGeo_GeometryData* self_data = RGEO_GEOMETRY_DATA_PTR(self);
202
+ const GEOSGeometry* self_geom = self_data->geom;
203
+ if (self_geom) {
204
+ unsigned int n = GEOSGetNumCoordinates_r(self_data->geos_context, self_geom);
205
+ if (n > 0) {
206
+ result = method_line_string_point_n(self, INT2NUM(n-1));
207
+ }
208
+ }
209
+ return result;
210
+ }
211
+
212
+
213
+ static VALUE method_line_string_is_closed(VALUE self)
214
+ {
215
+ VALUE result = Qnil;
216
+ RGeo_GeometryData* self_data = RGEO_GEOMETRY_DATA_PTR(self);
217
+ const GEOSGeometry* self_geom = self_data->geom;
218
+ if (self_geom) {
219
+ result = rgeo_is_geos_line_string_closed(self_data->geos_context, self_geom);
220
+ }
221
+ return result;
222
+ }
223
+
224
+
225
+ static VALUE method_line_string_is_ring(VALUE self)
226
+ {
227
+ VALUE result = Qnil;
228
+ RGeo_GeometryData* self_data = RGEO_GEOMETRY_DATA_PTR(self);
229
+ const GEOSGeometry* self_geom = self_data->geom;
230
+ if (self_geom) {
231
+ char val = GEOSisRing_r(self_data->geos_context, self_geom);
232
+ if (val == 0) {
233
+ result = Qfalse;
234
+ }
235
+ else if (val == 1) {
236
+ result = Qtrue;
237
+ }
238
+ }
239
+ return result;
240
+ }
241
+
242
+
243
+ static VALUE method_line_string_eql(VALUE self, VALUE rhs)
244
+ {
245
+ VALUE result = rgeo_geos_klasses_and_factories_eql(self, rhs);
246
+ if (RTEST(result)) {
247
+ RGeo_GeometryData* self_data = RGEO_GEOMETRY_DATA_PTR(self);
248
+ result = rgeo_geos_coordseqs_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);
249
+ }
250
+ return result;
251
+ }
252
+
253
+
254
+ static GEOSCoordSequence* coord_seq_from_array(VALUE factory, VALUE array, char close)
255
+ {
256
+ Check_Type(array, T_ARRAY);
257
+ RGeo_FactoryData* factory_data = RGEO_FACTORY_DATA_PTR(factory);
258
+ VALUE point_type = factory_data->globals->feature_point;
259
+ unsigned int len = (unsigned int)RARRAY_LEN(array);
260
+ char has_z = (char)(RGEO_FACTORY_DATA_PTR(factory)->flags & RGEO_FACTORYFLAGS_SUPPORTS_Z_OR_M);
261
+ unsigned int dims = has_z ? 3 : 2;
262
+ double* coords = ALLOC_N(double, len == 0 ? 1 : len * dims);
263
+ if (!coords) {
264
+ return NULL;
265
+ }
266
+ GEOSContextHandle_t context = factory_data->geos_context;
267
+ unsigned int i;
268
+ for (i=0; i<len; ++i) {
269
+ char good = 0;
270
+ const GEOSGeometry* entry_geom = rgeo_convert_to_geos_geometry(factory, rb_ary_entry(array, i), point_type);
271
+ if (entry_geom) {
272
+ const GEOSCoordSequence* entry_cs = GEOSGeom_getCoordSeq_r(context, entry_geom);
273
+ if (entry_cs) {
274
+ double x;
275
+ if (GEOSCoordSeq_getX_r(context, entry_cs, 0, &x)) {
276
+ coords[i*dims] = x;
277
+ if (GEOSCoordSeq_getY_r(context, entry_cs, 0, &x)) {
278
+ coords[i*dims+1] = x;
279
+ good = 1;
280
+ if (has_z) {
281
+ if (GEOSCoordSeq_getZ_r(context, entry_cs, 0, &x)) {
282
+ coords[i*dims+2] = x;
283
+ }
284
+ else {
285
+ good = 0;
286
+ }
287
+ }
288
+ }
289
+ }
290
+ }
291
+ }
292
+ if (!good) {
293
+ free(coords);
294
+ return NULL;
295
+ }
296
+ }
297
+ if (len > 0 && close) {
298
+ if (coords[0] == coords[(len-1)*dims] && coords[1] == coords[(len-1)*dims+1]) {
299
+ close = 0;
300
+ }
301
+ }
302
+ else {
303
+ close = 0;
304
+ }
305
+ GEOSCoordSequence* coord_seq = GEOSCoordSeq_create_r(context, len + close, 3);
306
+ if (coord_seq) {
307
+ for (i=0; i<len; ++i) {
308
+ GEOSCoordSeq_setX_r(context, coord_seq, i, coords[i*dims]);
309
+ GEOSCoordSeq_setY_r(context, coord_seq, i, coords[i*dims+1]);
310
+ GEOSCoordSeq_setZ_r(context, coord_seq, i, has_z ? coords[i*dims+2] : 0);
311
+ }
312
+ if (close) {
313
+ GEOSCoordSeq_setX_r(context, coord_seq, len, coords[0]);
314
+ GEOSCoordSeq_setY_r(context, coord_seq, len, coords[1]);
315
+ GEOSCoordSeq_setZ_r(context, coord_seq, len, has_z ? coords[2] : 0);
316
+ }
317
+ }
318
+ free(coords);
319
+ return coord_seq;
320
+ }
321
+
322
+
323
+ static VALUE cmethod_create_line_string(VALUE module, VALUE factory, VALUE array)
324
+ {
325
+ VALUE result = Qnil;
326
+ GEOSCoordSequence* coord_seq = coord_seq_from_array(factory, array, 0);
327
+ if (coord_seq) {
328
+ RGeo_FactoryData* factory_data = RGEO_FACTORY_DATA_PTR(factory);
329
+ GEOSGeometry* geom = GEOSGeom_createLineString_r(factory_data->geos_context, coord_seq);
330
+ if (geom) {
331
+ result = rgeo_wrap_geos_geometry(factory, geom, factory_data->globals->geos_line_string);
332
+ }
333
+ }
334
+ return result;
335
+ }
336
+
337
+
338
+ static VALUE cmethod_create_linear_ring(VALUE module, VALUE factory, VALUE array)
339
+ {
340
+ VALUE result = Qnil;
341
+ GEOSCoordSequence* coord_seq = coord_seq_from_array(factory, array, 1);
342
+ if (coord_seq) {
343
+ RGeo_FactoryData* factory_data = RGEO_FACTORY_DATA_PTR(factory);
344
+ GEOSGeometry* geom = GEOSGeom_createLinearRing_r(factory_data->geos_context, coord_seq);
345
+ if (geom) {
346
+ result = rgeo_wrap_geos_geometry(factory, geom, factory_data->globals->geos_linear_ring);
347
+ }
348
+ }
349
+ return result;
350
+ }
351
+
352
+
353
+ static void populate_geom_into_coord_seq(GEOSContextHandle_t context, const GEOSGeometry* geom, GEOSCoordSequence* coord_seq, unsigned int i, char has_z)
354
+ {
355
+ const GEOSCoordSequence* cs = GEOSGeom_getCoordSeq_r(context, geom);
356
+ double x = 0;
357
+ if (cs) {
358
+ GEOSCoordSeq_getX_r(context, cs, 0, &x);
359
+ }
360
+ GEOSCoordSeq_setX_r(context, coord_seq, i, x);
361
+ x = 0;
362
+ if (cs) {
363
+ GEOSCoordSeq_getY_r(context, cs, 0, &x);
364
+ }
365
+ GEOSCoordSeq_setY_r(context, coord_seq, i, x);
366
+ x = 0;
367
+ if (has_z && cs) {
368
+ GEOSCoordSeq_getZ_r(context, cs, 0, &x);
369
+ }
370
+ GEOSCoordSeq_setZ_r(context, coord_seq, i, x);
371
+ }
372
+
373
+
374
+ static VALUE cmethod_create_line(VALUE module, VALUE factory, VALUE start, VALUE end)
375
+ {
376
+ VALUE result = Qnil;
377
+ RGeo_FactoryData* factory_data = RGEO_FACTORY_DATA_PTR(factory);
378
+ char has_z = (char)(factory_data->flags & RGEO_FACTORYFLAGS_SUPPORTS_Z_OR_M);
379
+ VALUE point_type = factory_data->globals->feature_point;
380
+ GEOSContextHandle_t context = factory_data->geos_context;
381
+
382
+ const GEOSGeometry* start_geom = rgeo_convert_to_geos_geometry(factory, start, point_type);
383
+ if (start_geom) {
384
+ const GEOSGeometry* end_geom = rgeo_convert_to_geos_geometry(factory, end, point_type);
385
+ if (end_geom) {
386
+ GEOSCoordSequence* coord_seq = GEOSCoordSeq_create_r(context, 2, 3);
387
+ if (coord_seq) {
388
+ populate_geom_into_coord_seq(context, start_geom, coord_seq, 0, has_z);
389
+ populate_geom_into_coord_seq(context, end_geom, coord_seq, 1, has_z);
390
+ GEOSGeometry* geom = GEOSGeom_createLineString_r(context, coord_seq);
391
+ if (geom) {
392
+ result = rgeo_wrap_geos_geometry(factory, geom, factory_data->globals->geos_line);
393
+ }
394
+ }
395
+ }
396
+ }
397
+
398
+ return result;
399
+ }
400
+
401
+
402
+ static VALUE impl_copy_from(VALUE klass, VALUE factory, VALUE original, char subtype)
403
+ {
404
+ VALUE result = Qnil;
405
+ const GEOSGeometry* original_geom = RGEO_GEOMETRY_DATA_PTR(original)->geom;
406
+ if (original_geom) {
407
+ GEOSContextHandle_t context = RGEO_FACTORY_DATA_PTR(factory)->geos_context;
408
+ if (subtype == 1 && GEOSGetNumCoordinates_r(context, original_geom) != 2) {
409
+ original_geom = NULL;
410
+ }
411
+ if (original_geom) {
412
+ const GEOSCoordSequence* original_coord_seq = GEOSGeom_getCoordSeq_r(context, original_geom);
413
+ if (original_coord_seq) {
414
+ GEOSCoordSequence* coord_seq = GEOSCoordSeq_clone_r(context, original_coord_seq);
415
+ if (coord_seq) {
416
+ GEOSGeometry* geom = subtype == 2 ? GEOSGeom_createLinearRing_r(context, coord_seq) : GEOSGeom_createLineString_r(context, coord_seq);
417
+ if (geom) {
418
+ result = rgeo_wrap_geos_geometry(factory, geom, klass);
419
+ }
420
+ }
421
+ }
422
+ }
423
+ }
424
+ return result;
425
+ }
426
+
427
+
428
+ static VALUE cmethod_line_string_copy_from(VALUE klass, VALUE factory, VALUE original)
429
+ {
430
+ return impl_copy_from(klass, factory, original, 0);
431
+ }
432
+
433
+
434
+ static VALUE cmethod_line_copy_from(VALUE klass, VALUE factory, VALUE original)
435
+ {
436
+ return impl_copy_from(klass, factory, original, 1);
437
+ }
438
+
439
+
440
+ static VALUE cmethod_linear_ring_copy_from(VALUE klass, VALUE factory, VALUE original)
441
+ {
442
+ return impl_copy_from(klass, factory, original, 2);
443
+ }
444
+
445
+
446
+ void rgeo_init_geos_line_string(RGeo_Globals* globals)
447
+ {
448
+ VALUE geos_line_string_class = rb_define_class_under(globals->geos_module, "LineStringImpl", globals->geos_geometry);
449
+ globals->geos_line_string = geos_line_string_class;
450
+ globals->feature_line_string = rb_const_get_at(globals->feature_module, rb_intern("LineString"));
451
+ VALUE geos_linear_ring_class = rb_define_class_under(globals->geos_module, "LinearRingImpl", geos_line_string_class);
452
+ globals->geos_linear_ring = geos_linear_ring_class;
453
+ globals->feature_linear_ring = rb_const_get_at(globals->feature_module, rb_intern("LinearRing"));
454
+ VALUE geos_line_class = rb_define_class_under(globals->geos_module, "LineImpl", geos_line_string_class);
455
+ globals->geos_line = geos_line_class;
456
+ globals->feature_line = rb_const_get_at(globals->feature_module, rb_intern("Line"));
457
+
458
+ rb_define_module_function(geos_line_string_class, "create", cmethod_create_line_string, 2);
459
+ rb_define_module_function(geos_line_string_class, "_copy_from", cmethod_line_string_copy_from, 2);
460
+ rb_define_method(geos_line_string_class, "eql?", method_line_string_eql, 1);
461
+ rb_define_method(geos_line_string_class, "geometry_type", method_line_string_geometry_type, 0);
462
+ rb_define_method(geos_line_string_class, "length", method_line_string_length, 0);
463
+ rb_define_method(geos_line_string_class, "num_points", method_line_string_num_points, 0);
464
+ rb_define_method(geos_line_string_class, "point_n", method_line_string_point_n, 1);
465
+ rb_define_method(geos_line_string_class, "points", method_line_string_points, 0);
466
+ rb_define_method(geos_line_string_class, "start_point", method_line_string_start_point, 0);
467
+ rb_define_method(geos_line_string_class, "end_point", method_line_string_end_point, 0);
468
+ rb_define_method(geos_line_string_class, "is_closed?", method_line_string_is_closed, 0);
469
+ rb_define_method(geos_line_string_class, "is_ring?", method_line_string_is_ring, 0);
470
+
471
+ rb_define_module_function(geos_linear_ring_class, "create", cmethod_create_linear_ring, 2);
472
+ rb_define_module_function(geos_linear_ring_class, "_copy_from", cmethod_linear_ring_copy_from, 2);
473
+ rb_define_method(geos_linear_ring_class, "geometry_type", method_linear_ring_geometry_type, 0);
474
+
475
+ rb_define_module_function(geos_line_class, "create", cmethod_create_line, 3);
476
+ rb_define_module_function(geos_line_class, "_copy_from", cmethod_line_copy_from, 2);
477
+ rb_define_method(geos_line_class, "geometry_type", method_line_geometry_type, 0);
478
+ }
479
+
480
+
481
+ VALUE rgeo_is_geos_line_string_closed(GEOSContextHandle_t context, const GEOSGeometry* geom)
482
+ {
483
+ char result = Qnil;
484
+ unsigned int n = GEOSGetNumCoordinates_r(context, geom);
485
+ if (n > 0) {
486
+ double x1, x2, y1, y2, z1, z2;
487
+ const GEOSCoordSequence* coord_seq = GEOSGeom_getCoordSeq_r(context, geom);
488
+ if (GEOSCoordSeq_getX_r(context, coord_seq, 0, &x1)) {
489
+ if (GEOSCoordSeq_getX_r(context, coord_seq, n-1, &x2)) {
490
+ if (x1 == x2) {
491
+ if (GEOSCoordSeq_getY_r(context, coord_seq, 0, &y1)) {
492
+ if (GEOSCoordSeq_getY_r(context, coord_seq, n-1, &y2)) {
493
+ result = y1 == y2 ? Qtrue : Qfalse;
494
+ }
495
+ }
496
+ }
497
+ else {
498
+ result = Qfalse;
499
+ }
500
+ }
501
+ }
502
+ }
503
+ return result;
504
+ }
505
+
506
+
507
+ RGEO_END_C
508
+
509
+ #endif