rgeo 2.4.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +6 -0
  3. data/README.md +23 -14
  4. data/ext/geos_c_impl/analysis.c +29 -26
  5. data/ext/geos_c_impl/analysis.h +8 -5
  6. data/ext/geos_c_impl/coordinates.c +27 -21
  7. data/ext/geos_c_impl/coordinates.h +5 -2
  8. data/ext/geos_c_impl/errors.c +19 -10
  9. data/ext/geos_c_impl/errors.h +11 -4
  10. data/ext/geos_c_impl/extconf.rb +43 -29
  11. data/ext/geos_c_impl/factory.c +443 -351
  12. data/ext/geos_c_impl/factory.h +98 -55
  13. data/ext/geos_c_impl/geometry.c +589 -383
  14. data/ext/geos_c_impl/geometry.h +10 -3
  15. data/ext/geos_c_impl/geometry_collection.c +288 -316
  16. data/ext/geos_c_impl/geometry_collection.h +6 -18
  17. data/ext/geos_c_impl/globals.c +98 -21
  18. data/ext/geos_c_impl/globals.h +3 -2
  19. data/ext/geos_c_impl/line_string.c +263 -222
  20. data/ext/geos_c_impl/line_string.h +5 -6
  21. data/ext/geos_c_impl/main.c +8 -9
  22. data/ext/geos_c_impl/point.c +63 -65
  23. data/ext/geos_c_impl/point.h +4 -5
  24. data/ext/geos_c_impl/polygon.c +168 -131
  25. data/ext/geos_c_impl/polygon.h +11 -9
  26. data/ext/geos_c_impl/preface.h +15 -11
  27. data/ext/geos_c_impl/ruby_more.c +67 -0
  28. data/ext/geos_c_impl/ruby_more.h +25 -0
  29. data/lib/rgeo/cartesian/analysis.rb +5 -3
  30. data/lib/rgeo/cartesian/bounding_box.rb +74 -79
  31. data/lib/rgeo/cartesian/calculations.rb +64 -33
  32. data/lib/rgeo/cartesian/factory.rb +59 -104
  33. data/lib/rgeo/cartesian/feature_classes.rb +68 -46
  34. data/lib/rgeo/cartesian/feature_methods.rb +67 -25
  35. data/lib/rgeo/cartesian/interface.rb +6 -41
  36. data/lib/rgeo/cartesian/planar_graph.rb +373 -0
  37. data/lib/rgeo/cartesian/sweepline_intersector.rb +147 -0
  38. data/lib/rgeo/cartesian/valid_op.rb +69 -0
  39. data/lib/rgeo/cartesian.rb +3 -0
  40. data/lib/rgeo/coord_sys/cs/entities.rb +305 -101
  41. data/lib/rgeo/coord_sys/cs/factories.rb +0 -2
  42. data/lib/rgeo/coord_sys/cs/wkt_parser.rb +90 -42
  43. data/lib/rgeo/coord_sys.rb +1 -20
  44. data/lib/rgeo/error.rb +15 -0
  45. data/lib/rgeo/feature/curve.rb +0 -11
  46. data/lib/rgeo/feature/factory.rb +26 -36
  47. data/lib/rgeo/feature/factory_generator.rb +6 -14
  48. data/lib/rgeo/feature/geometry.rb +146 -66
  49. data/lib/rgeo/feature/geometry_collection.rb +16 -9
  50. data/lib/rgeo/feature/line_string.rb +4 -5
  51. data/lib/rgeo/feature/linear_ring.rb +0 -1
  52. data/lib/rgeo/feature/multi_curve.rb +0 -6
  53. data/lib/rgeo/feature/multi_surface.rb +3 -4
  54. data/lib/rgeo/feature/point.rb +4 -5
  55. data/lib/rgeo/feature/polygon.rb +1 -2
  56. data/lib/rgeo/feature/surface.rb +3 -4
  57. data/lib/rgeo/feature/types.rb +71 -87
  58. data/lib/rgeo/geographic/factory.rb +100 -127
  59. data/lib/rgeo/geographic/interface.rb +71 -168
  60. data/lib/rgeo/geographic/projected_feature_classes.rb +21 -9
  61. data/lib/rgeo/geographic/projected_feature_methods.rb +67 -42
  62. data/lib/rgeo/geographic/projected_window.rb +36 -22
  63. data/lib/rgeo/geographic/{proj4_projector.rb → projector.rb} +3 -5
  64. data/lib/rgeo/geographic/simple_mercator_projector.rb +26 -25
  65. data/lib/rgeo/geographic/spherical_feature_classes.rb +29 -9
  66. data/lib/rgeo/geographic/spherical_feature_methods.rb +86 -9
  67. data/lib/rgeo/geographic/spherical_math.rb +17 -20
  68. data/lib/rgeo/geographic.rb +1 -1
  69. data/lib/rgeo/geos/capi_factory.rb +87 -158
  70. data/lib/rgeo/geos/capi_feature_classes.rb +50 -36
  71. data/lib/rgeo/geos/ffi_factory.rb +107 -175
  72. data/lib/rgeo/geos/ffi_feature_classes.rb +34 -10
  73. data/lib/rgeo/geos/ffi_feature_methods.rb +105 -127
  74. data/lib/rgeo/geos/interface.rb +20 -59
  75. data/lib/rgeo/geos/utils.rb +5 -5
  76. data/lib/rgeo/geos/zm_factory.rb +55 -97
  77. data/lib/rgeo/geos/zm_feature_methods.rb +30 -33
  78. data/lib/rgeo/geos.rb +15 -9
  79. data/lib/rgeo/impl_helper/basic_geometry_collection_methods.rb +11 -24
  80. data/lib/rgeo/impl_helper/basic_geometry_methods.rb +1 -2
  81. data/lib/rgeo/impl_helper/basic_line_string_methods.rb +28 -56
  82. data/lib/rgeo/impl_helper/basic_point_methods.rb +2 -14
  83. data/lib/rgeo/impl_helper/basic_polygon_methods.rb +17 -26
  84. data/lib/rgeo/impl_helper/utils.rb +21 -0
  85. data/lib/rgeo/impl_helper/valid_op.rb +350 -0
  86. data/lib/rgeo/impl_helper/validity_check.rb +139 -0
  87. data/lib/rgeo/impl_helper.rb +1 -0
  88. data/lib/rgeo/version.rb +1 -1
  89. data/lib/rgeo/wkrep/wkb_generator.rb +73 -63
  90. data/lib/rgeo/wkrep/wkb_parser.rb +33 -31
  91. data/lib/rgeo/wkrep/wkt_generator.rb +52 -45
  92. data/lib/rgeo/wkrep/wkt_parser.rb +48 -35
  93. data/lib/rgeo.rb +1 -3
  94. metadata +63 -16
  95. data/lib/rgeo/coord_sys/srs_database/entry.rb +0 -107
  96. data/lib/rgeo/coord_sys/srs_database/sr_org.rb +0 -64
  97. data/lib/rgeo/coord_sys/srs_database/url_reader.rb +0 -65
@@ -2,104 +2,91 @@
2
2
  Factory and utility functions for GEOS wrapper
3
3
  */
4
4
 
5
-
6
5
  #include "preface.h"
7
6
 
8
7
  #ifdef RGEO_GEOS_SUPPORTED
9
8
 
10
- #include <ruby.h>
11
9
  #include <geos_c.h>
10
+ #include <ruby.h>
12
11
 
13
- #include "globals.h"
14
-
12
+ #include "errors.h"
15
13
  #include "factory.h"
16
14
  #include "geometry.h"
17
- #include "point.h"
15
+ #include "geometry_collection.h"
16
+ #include "globals.h"
18
17
  #include "line_string.h"
18
+ #include "point.h"
19
19
  #include "polygon.h"
20
- #include "geometry_collection.h"
21
- #include "errors.h"
20
+ #include "ruby_more.h"
22
21
 
23
22
  RGEO_BEGIN_C
24
23
 
25
-
26
24
  /**** RUBY AND GEOS CALLBACKS ****/
27
25
 
28
-
29
- // NOP message handler. GEOS requires that a message handler be set
30
- // for every context handle.
31
-
32
- static void message_handler(const char* fmt, ...)
33
- {
34
- }
35
-
36
-
37
26
  // Destroy function for factory data. We destroy any serialization
38
- // objects that have been created for the factory, and then destroy
39
- // the GEOS context, before freeing the factory data itself.
27
+ // objects that have been created for the factory before freeing
28
+ // the factory data itself.
40
29
 
41
- static void destroy_factory_func(void* data)
30
+ static void
31
+ destroy_factory_func(void* data)
42
32
  {
43
33
  RGeo_FactoryData* factory_data;
44
- GEOSContextHandle_t context;
45
34
 
46
35
  factory_data = (RGeo_FactoryData*)data;
47
- context = factory_data->geos_context;
48
36
  if (factory_data->wkt_reader) {
49
- GEOSWKTReader_destroy_r(context, factory_data->wkt_reader);
37
+ GEOSWKTReader_destroy(factory_data->wkt_reader);
50
38
  }
51
39
  if (factory_data->wkb_reader) {
52
- GEOSWKBReader_destroy_r(context, factory_data->wkb_reader);
40
+ GEOSWKBReader_destroy(factory_data->wkb_reader);
53
41
  }
54
42
  if (factory_data->wkt_writer) {
55
- GEOSWKTWriter_destroy_r(context, factory_data->wkt_writer);
43
+ GEOSWKTWriter_destroy(factory_data->wkt_writer);
56
44
  }
57
45
  if (factory_data->wkb_writer) {
58
- GEOSWKBWriter_destroy_r(context, factory_data->wkb_writer);
46
+ GEOSWKBWriter_destroy(factory_data->wkb_writer);
59
47
  }
60
48
  if (factory_data->psych_wkt_reader) {
61
- GEOSWKTReader_destroy_r(context, factory_data->psych_wkt_reader);
49
+ GEOSWKTReader_destroy(factory_data->psych_wkt_reader);
62
50
  }
63
51
  if (factory_data->marshal_wkb_reader) {
64
- GEOSWKBReader_destroy_r(context, factory_data->marshal_wkb_reader);
52
+ GEOSWKBReader_destroy(factory_data->marshal_wkb_reader);
65
53
  }
66
54
  if (factory_data->psych_wkt_writer) {
67
- GEOSWKTWriter_destroy_r(context, factory_data->psych_wkt_writer);
55
+ GEOSWKTWriter_destroy(factory_data->psych_wkt_writer);
68
56
  }
69
57
  if (factory_data->marshal_wkb_writer) {
70
- GEOSWKBWriter_destroy_r(context, factory_data->marshal_wkb_writer);
58
+ GEOSWKBWriter_destroy(factory_data->marshal_wkb_writer);
71
59
  }
72
- finishGEOS_r(context);
73
- free(factory_data);
60
+ FREE(factory_data);
74
61
  }
75
62
 
76
-
77
63
  // Destroy function for geometry data. We destroy the internal
78
64
  // GEOS geometry (if present) before freeing the data itself.
79
65
 
80
- static void destroy_geometry_func(void* data)
66
+ static void
67
+ destroy_geometry_func(void* data)
81
68
  {
82
69
  RGeo_GeometryData* geometry_data;
83
70
  const GEOSPreparedGeometry* prep;
84
71
 
85
72
  geometry_data = (RGeo_GeometryData*)data;
86
73
  if (geometry_data->geom) {
87
- GEOSGeom_destroy_r(geometry_data->geos_context, geometry_data->geom);
74
+ GEOSGeom_destroy(geometry_data->geom);
88
75
  }
89
76
  prep = geometry_data->prep;
90
- if (prep && prep != (const GEOSPreparedGeometry*)1 && prep != (const GEOSPreparedGeometry*)2 &&
91
- prep != (const GEOSPreparedGeometry*)3)
92
- {
93
- GEOSPreparedGeom_destroy_r(geometry_data->geos_context, prep);
77
+ if (prep && prep != (const GEOSPreparedGeometry*)1 &&
78
+ prep != (const GEOSPreparedGeometry*)2 &&
79
+ prep != (const GEOSPreparedGeometry*)3) {
80
+ GEOSPreparedGeom_destroy(prep);
94
81
  }
95
- free(geometry_data);
82
+ FREE(geometry_data);
96
83
  }
97
84
 
98
-
99
85
  // Mark function for factory data. This marks the wkt and wkb generator
100
86
  // handles so they don't get collected.
101
87
 
102
- static void mark_factory_func(void* data)
88
+ static void
89
+ mark_factory_func(void* data)
103
90
  {
104
91
  RGeo_FactoryData* factory_data;
105
92
 
@@ -116,19 +103,16 @@ static void mark_factory_func(void* data)
116
103
  if (!NIL_P(factory_data->wkrep_wkb_parser)) {
117
104
  mark(factory_data->wkrep_wkb_parser);
118
105
  }
119
- if (!NIL_P(factory_data->proj4_obj)) {
120
- mark(factory_data->proj4_obj);
121
- }
122
106
  if (!NIL_P(factory_data->coord_sys_obj)) {
123
107
  mark(factory_data->coord_sys_obj);
124
108
  }
125
109
  }
126
110
 
127
-
128
111
  // Mark function for geometry data. This marks the factory and klasses
129
112
  // held by the geometry so those don't get collected.
130
113
 
131
- static void mark_geometry_func(void* data)
114
+ static void
115
+ mark_geometry_func(void* data)
132
116
  {
133
117
  RGeo_GeometryData* geometry_data;
134
118
 
@@ -141,35 +125,36 @@ static void mark_geometry_func(void* data)
141
125
  }
142
126
  }
143
127
 
144
-
145
128
  #ifdef HAVE_RB_GC_MARK_MOVABLE
146
- static void compact_factory_func(void* data)
129
+ static void
130
+ compact_factory_func(void* data)
147
131
  {
148
132
  RGeo_FactoryData* factory_data;
149
133
 
150
134
  factory_data = (RGeo_FactoryData*)data;
151
135
  if (!NIL_P(factory_data->wkrep_wkt_generator)) {
152
- factory_data->wkrep_wkt_generator = rb_gc_location(factory_data->wkrep_wkt_generator);
136
+ factory_data->wkrep_wkt_generator =
137
+ rb_gc_location(factory_data->wkrep_wkt_generator);
153
138
  }
154
139
  if (!NIL_P(factory_data->wkrep_wkb_generator)) {
155
- factory_data->wkrep_wkb_generator = rb_gc_location(factory_data->wkrep_wkb_generator);
140
+ factory_data->wkrep_wkb_generator =
141
+ rb_gc_location(factory_data->wkrep_wkb_generator);
156
142
  }
157
143
  if (!NIL_P(factory_data->wkrep_wkt_parser)) {
158
- factory_data->wkrep_wkt_parser = rb_gc_location(factory_data->wkrep_wkt_parser);
144
+ factory_data->wkrep_wkt_parser =
145
+ rb_gc_location(factory_data->wkrep_wkt_parser);
159
146
  }
160
147
  if (!NIL_P(factory_data->wkrep_wkb_parser)) {
161
- factory_data->wkrep_wkb_parser = rb_gc_location(factory_data->wkrep_wkb_parser);
162
- }
163
- if (!NIL_P(factory_data->proj4_obj)) {
164
- factory_data->proj4_obj = rb_gc_location(factory_data->proj4_obj);
148
+ factory_data->wkrep_wkb_parser =
149
+ rb_gc_location(factory_data->wkrep_wkb_parser);
165
150
  }
166
151
  if (!NIL_P(factory_data->coord_sys_obj)) {
167
152
  factory_data->coord_sys_obj = rb_gc_location(factory_data->coord_sys_obj);
168
153
  }
169
154
  }
170
155
 
171
-
172
- static void compact_geometry_func(void* data)
156
+ static void
157
+ compact_geometry_func(void* data)
173
158
  {
174
159
  RGeo_GeometryData* geometry_data;
175
160
 
@@ -183,70 +168,95 @@ static void compact_geometry_func(void* data)
183
168
  }
184
169
  #endif
185
170
 
186
-
187
- const rb_data_type_t rgeo_factory_type = {
188
- .wrap_struct_name = "RGeo/Factory",
189
- .function = {
190
- .dmark = mark_factory_func,
191
- .dfree = destroy_factory_func,
171
+ const rb_data_type_t rgeo_factory_type = { .wrap_struct_name = "RGeo/Factory",
172
+ .function = {
173
+ .dmark = mark_factory_func,
174
+ .dfree = destroy_factory_func,
192
175
  #ifdef HAVE_RB_GC_MARK_MOVABLE
193
- .dcompact = compact_factory_func,
176
+ .dcompact = compact_factory_func,
194
177
  #endif
195
- }
196
- };
178
+ } };
197
179
 
198
- const rb_data_type_t rgeo_geometry_type = {
199
- .wrap_struct_name = "RGeo/Geometry",
200
- .function = {
201
- .dmark = mark_geometry_func,
202
- .dfree = destroy_geometry_func,
180
+ const rb_data_type_t rgeo_geometry_type = { .wrap_struct_name = "RGeo/Geometry",
181
+ .function = {
182
+ .dmark = mark_geometry_func,
183
+ .dfree = destroy_geometry_func,
203
184
  #ifdef HAVE_RB_GC_MARK_MOVABLE
204
- .dcompact = compact_geometry_func,
185
+ .dcompact = compact_geometry_func,
205
186
  #endif
206
- }
207
- };
208
-
187
+ } };
209
188
 
210
189
  /**** RUBY METHOD DEFINITIONS ****/
211
190
 
212
-
213
- static VALUE method_factory_srid(VALUE self)
191
+ static VALUE
192
+ method_factory_srid(VALUE self)
214
193
  {
215
194
  return INT2NUM(RGEO_FACTORY_DATA_PTR(self)->srid);
216
195
  }
217
196
 
218
-
219
- static VALUE method_factory_buffer_resolution(VALUE self)
197
+ static VALUE
198
+ method_factory_buffer_resolution(VALUE self)
220
199
  {
221
200
  return INT2NUM(RGEO_FACTORY_DATA_PTR(self)->buffer_resolution);
222
201
  }
223
202
 
224
-
225
- static VALUE method_factory_flags(VALUE self)
203
+ static VALUE
204
+ method_factory_flags(VALUE self)
226
205
  {
227
206
  return INT2NUM(RGEO_FACTORY_DATA_PTR(self)->flags);
228
207
  }
229
208
 
209
+ VALUE
210
+ method_factory_supports_z_p(VALUE self)
211
+ {
212
+ return RGEO_FACTORY_DATA_PTR(self)->flags & RGEO_FACTORYFLAGS_SUPPORTS_Z
213
+ ? Qtrue
214
+ : Qfalse;
215
+ }
216
+
217
+ VALUE
218
+ method_factory_supports_m_p(VALUE self)
219
+ {
220
+ return RGEO_FACTORY_DATA_PTR(self)->flags & RGEO_FACTORYFLAGS_SUPPORTS_M
221
+ ? Qtrue
222
+ : Qfalse;
223
+ }
224
+
225
+ VALUE
226
+ method_factory_supports_z_or_m_p(VALUE self)
227
+ {
228
+ return RGEO_FACTORY_DATA_PTR(self)->flags & RGEO_FACTORYFLAGS_SUPPORTS_Z_OR_M
229
+ ? Qtrue
230
+ : Qfalse;
231
+ }
232
+
233
+ VALUE
234
+ method_factory_prepare_heuristic_p(VALUE self)
235
+ {
236
+ return RGEO_FACTORY_DATA_PTR(self)->flags &
237
+ RGEO_FACTORYFLAGS_PREPARE_HEURISTIC
238
+ ? Qtrue
239
+ : Qfalse;
240
+ }
230
241
 
231
- static VALUE method_factory_parse_wkt(VALUE self, VALUE str)
242
+ static VALUE
243
+ method_factory_parse_wkt(VALUE self, VALUE str)
232
244
  {
233
245
  RGeo_FactoryData* self_data;
234
- GEOSContextHandle_t self_context;
235
246
  GEOSWKTReader* wkt_reader;
236
247
  VALUE result;
237
248
  GEOSGeometry* geom;
238
249
 
239
250
  Check_Type(str, T_STRING);
240
251
  self_data = RGEO_FACTORY_DATA_PTR(self);
241
- self_context = self_data->geos_context;
242
252
  wkt_reader = self_data->wkt_reader;
243
253
  if (!wkt_reader) {
244
- wkt_reader = GEOSWKTReader_create_r(self_context);
254
+ wkt_reader = GEOSWKTReader_create();
245
255
  self_data->wkt_reader = wkt_reader;
246
256
  }
247
257
  result = Qnil;
248
258
  if (wkt_reader) {
249
- geom = GEOSWKTReader_read_r(self_context, wkt_reader, RSTRING_PTR(str));
259
+ geom = GEOSWKTReader_read(wkt_reader, RSTRING_PTR(str));
250
260
  if (geom) {
251
261
  result = rgeo_wrap_geos_geometry(self, geom, Qnil);
252
262
  }
@@ -254,26 +264,31 @@ static VALUE method_factory_parse_wkt(VALUE self, VALUE str)
254
264
  return result;
255
265
  }
256
266
 
257
-
258
- static VALUE method_factory_parse_wkb(VALUE self, VALUE str)
267
+ static VALUE
268
+ method_factory_parse_wkb(VALUE self, VALUE str)
259
269
  {
260
270
  RGeo_FactoryData* self_data;
261
- GEOSContextHandle_t self_context;
262
271
  GEOSWKBReader* wkb_reader;
263
272
  VALUE result;
264
273
  GEOSGeometry* geom;
274
+ char* c_str;
265
275
 
266
276
  Check_Type(str, T_STRING);
267
277
  self_data = RGEO_FACTORY_DATA_PTR(self);
268
- self_context = self_data->geos_context;
269
278
  wkb_reader = self_data->wkb_reader;
270
279
  if (!wkb_reader) {
271
- wkb_reader = GEOSWKBReader_create_r(self_context);
280
+ wkb_reader = GEOSWKBReader_create();
272
281
  self_data->wkb_reader = wkb_reader;
273
282
  }
274
283
  result = Qnil;
275
284
  if (wkb_reader) {
276
- geom = GEOSWKBReader_read_r(self_context, wkb_reader, (unsigned char*)RSTRING_PTR(str), (size_t)RSTRING_LEN(str));
285
+ c_str = RSTRING_PTR(str);
286
+ if (c_str[0] == '\x00' || c_str[0] == '\x01')
287
+ geom = GEOSWKBReader_read(
288
+ wkb_reader, (unsigned char*)c_str, (size_t)RSTRING_LEN(str));
289
+ else
290
+ geom = GEOSWKBReader_readHEX(
291
+ wkb_reader, (unsigned char*)c_str, (size_t)RSTRING_LEN(str));
277
292
  if (geom) {
278
293
  result = rgeo_wrap_geos_geometry(self, geom, Qnil);
279
294
  }
@@ -281,26 +296,25 @@ static VALUE method_factory_parse_wkb(VALUE self, VALUE str)
281
296
  return result;
282
297
  }
283
298
 
284
-
285
- static VALUE method_factory_read_for_marshal(VALUE self, VALUE str)
299
+ static VALUE
300
+ method_factory_read_for_marshal(VALUE self, VALUE str)
286
301
  {
287
302
  RGeo_FactoryData* self_data;
288
- GEOSContextHandle_t self_context;
289
303
  GEOSWKBReader* wkb_reader;
290
304
  VALUE result;
291
305
  GEOSGeometry* geom;
292
306
 
293
307
  Check_Type(str, T_STRING);
294
308
  self_data = RGEO_FACTORY_DATA_PTR(self);
295
- self_context = self_data->geos_context;
296
309
  wkb_reader = self_data->marshal_wkb_reader;
297
310
  if (!wkb_reader) {
298
- wkb_reader = GEOSWKBReader_create_r(self_context);
311
+ wkb_reader = GEOSWKBReader_create();
299
312
  self_data->marshal_wkb_reader = wkb_reader;
300
313
  }
301
314
  result = Qnil;
302
315
  if (wkb_reader) {
303
- geom = GEOSWKBReader_read_r(self_context, wkb_reader, (unsigned char*)RSTRING_PTR(str), (size_t)RSTRING_LEN(str));
316
+ geom = GEOSWKBReader_read(
317
+ wkb_reader, (unsigned char*)RSTRING_PTR(str), (size_t)RSTRING_LEN(str));
304
318
  if (geom) {
305
319
  result = rgeo_wrap_geos_geometry(self, geom, Qnil);
306
320
  }
@@ -308,25 +322,24 @@ static VALUE method_factory_read_for_marshal(VALUE self, VALUE str)
308
322
  return result;
309
323
  }
310
324
 
311
- static VALUE method_factory_read_for_psych(VALUE self, VALUE str)
325
+ static VALUE
326
+ method_factory_read_for_psych(VALUE self, VALUE str)
312
327
  {
313
328
  RGeo_FactoryData* self_data;
314
- GEOSContextHandle_t self_context;
315
329
  GEOSWKTReader* wkt_reader;
316
330
  VALUE result;
317
331
  GEOSGeometry* geom;
318
332
 
319
333
  Check_Type(str, T_STRING);
320
334
  self_data = RGEO_FACTORY_DATA_PTR(self);
321
- self_context = self_data->geos_context;
322
335
  wkt_reader = self_data->psych_wkt_reader;
323
336
  if (!wkt_reader) {
324
- wkt_reader = GEOSWKTReader_create_r(self_context);
337
+ wkt_reader = GEOSWKTReader_create();
325
338
  self_data->psych_wkt_reader = wkt_reader;
326
339
  }
327
340
  result = Qnil;
328
341
  if (wkt_reader) {
329
- geom = GEOSWKTReader_read_r(self_context, wkt_reader, RSTRING_PTR(str));
342
+ geom = GEOSWKTReader_read(wkt_reader, RSTRING_PTR(str));
330
343
  if (geom) {
331
344
  result = rgeo_wrap_geos_geometry(self, geom, Qnil);
332
345
  }
@@ -338,10 +351,10 @@ static VALUE method_factory_read_for_psych(VALUE self, VALUE str)
338
351
  static VALUE marshal_wkb_generator;
339
352
  #endif
340
353
 
341
- static VALUE method_factory_write_for_marshal(VALUE self, VALUE obj)
354
+ static VALUE
355
+ method_factory_write_for_marshal(VALUE self, VALUE obj)
342
356
  {
343
357
  RGeo_FactoryData* self_data;
344
- GEOSContextHandle_t self_context;
345
358
  GEOSWKBWriter* wkb_writer;
346
359
  const GEOSGeometry* geom;
347
360
  VALUE result;
@@ -350,23 +363,24 @@ static VALUE method_factory_write_for_marshal(VALUE self, VALUE obj)
350
363
  char has_3d;
351
364
 
352
365
  self_data = RGEO_FACTORY_DATA_PTR(self);
353
- self_context = self_data->geos_context;
354
366
  has_3d = self_data->flags & RGEO_FACTORYFLAGS_SUPPORTS_Z_OR_M;
355
367
  #ifndef RGEO_GEOS_SUPPORTS_SETOUTPUTDIMENSION
356
368
  if (has_3d) {
357
369
  if (NIL_P(marshal_wkb_generator)) {
358
- marshal_wkb_generator = rb_funcall(
359
- rb_const_get_at(rgeo_geos_module, rb_intern("Utils")),
360
- rb_intern("marshal_wkb_generator"), 0);
370
+ marshal_wkb_generator =
371
+ rb_funcall(rb_const_get_at(rgeo_geos_module, rb_intern("Utils")),
372
+ rb_intern("marshal_wkb_generator"),
373
+ 0);
361
374
  }
362
375
  return rb_funcall(marshal_wkb_generator, rb_intern("generate"), 1, obj);
363
376
  }
364
377
  #endif
365
378
  wkb_writer = self_data->marshal_wkb_writer;
366
379
  if (!wkb_writer) {
367
- wkb_writer = GEOSWKBWriter_create_r(self_context);
380
+ wkb_writer = GEOSWKBWriter_create();
381
+ GEOSWKBWriter_setOutputDimension(wkb_writer, 2);
368
382
  if (has_3d) {
369
- GEOSWKBWriter_setOutputDimension_r(self_context, wkb_writer, 3);
383
+ GEOSWKBWriter_setOutputDimension(wkb_writer, 3);
370
384
  }
371
385
  self_data->marshal_wkb_writer = wkb_writer;
372
386
  }
@@ -374,10 +388,10 @@ static VALUE method_factory_write_for_marshal(VALUE self, VALUE obj)
374
388
  if (wkb_writer) {
375
389
  geom = rgeo_get_geos_geometry_safe(obj);
376
390
  if (geom) {
377
- str = (char*)GEOSWKBWriter_write_r(self_context, wkb_writer, geom, &size);
391
+ str = (char*)GEOSWKBWriter_write(wkb_writer, geom, &size);
378
392
  if (str) {
379
393
  result = rb_str_new(str, size);
380
- GEOSFree_r(self_context, str);
394
+ GEOSFree(str);
381
395
  }
382
396
  }
383
397
  }
@@ -388,10 +402,10 @@ static VALUE method_factory_write_for_marshal(VALUE self, VALUE obj)
388
402
  static VALUE psych_wkt_generator;
389
403
  #endif
390
404
 
391
- static VALUE method_factory_write_for_psych(VALUE self, VALUE obj)
405
+ static VALUE
406
+ method_factory_write_for_psych(VALUE self, VALUE obj)
392
407
  {
393
408
  RGeo_FactoryData* self_data;
394
- GEOSContextHandle_t self_context;
395
409
  GEOSWKTWriter* wkt_writer;
396
410
  const GEOSGeometry* geom;
397
411
  VALUE result;
@@ -399,23 +413,25 @@ static VALUE method_factory_write_for_psych(VALUE self, VALUE obj)
399
413
  char has_3d;
400
414
 
401
415
  self_data = RGEO_FACTORY_DATA_PTR(self);
402
- self_context = self_data->geos_context;
403
416
  has_3d = self_data->flags & RGEO_FACTORYFLAGS_SUPPORTS_Z_OR_M;
404
417
  #ifndef RGEO_GEOS_SUPPORTS_SETOUTPUTDIMENSION
405
418
  if (has_3d) {
406
419
  if (NIL_P(psych_wkt_generator)) {
407
- psych_wkt_generator = rb_funcall(
408
- rb_const_get_at(rgeo_geos_module, rb_intern("Utils")),
409
- rb_intern("psych_wkt_generator"), 0);
420
+ psych_wkt_generator =
421
+ rb_funcall(rb_const_get_at(rgeo_geos_module, rb_intern("Utils")),
422
+ rb_intern("psych_wkt_generator"),
423
+ 0);
410
424
  }
411
425
  return rb_funcall(psych_wkt_generator, rb_intern("generate"), 1, obj);
412
426
  }
413
427
  #endif
414
428
  wkt_writer = self_data->psych_wkt_writer;
415
429
  if (!wkt_writer) {
416
- wkt_writer = GEOSWKTWriter_create_r(self_context);
430
+ wkt_writer = GEOSWKTWriter_create();
431
+ GEOSWKTWriter_setOutputDimension(wkt_writer, 2);
432
+ GEOSWKTWriter_setTrim(wkt_writer, 1);
417
433
  if (has_3d) {
418
- GEOSWKTWriter_setOutputDimension_r(self_context, wkt_writer, 3);
434
+ GEOSWKTWriter_setOutputDimension(wkt_writer, 3);
419
435
  }
420
436
  self_data->psych_wkt_writer = wkt_writer;
421
437
  }
@@ -423,24 +439,24 @@ static VALUE method_factory_write_for_psych(VALUE self, VALUE obj)
423
439
  if (wkt_writer) {
424
440
  geom = rgeo_get_geos_geometry_safe(obj);
425
441
  if (geom) {
426
- str = GEOSWKTWriter_write_r(self_context, wkt_writer, geom);
442
+ str = GEOSWKTWriter_write(wkt_writer, geom);
427
443
  if (str) {
428
444
  result = rb_str_new2(str);
429
- GEOSFree_r(self_context, str);
445
+ GEOSFree(str);
430
446
  }
431
447
  }
432
448
  }
433
449
  return result;
434
450
  }
435
451
 
436
-
437
- static VALUE cmethod_factory_geos_version(VALUE klass)
452
+ static VALUE
453
+ cmethod_factory_geos_version(VALUE klass)
438
454
  {
439
455
  return rb_str_new2(GEOS_VERSION);
440
456
  }
441
457
 
442
-
443
- static VALUE cmethod_factory_supports_unary_union(VALUE klass)
458
+ static VALUE
459
+ cmethod_factory_supports_unary_union(VALUE klass)
444
460
  {
445
461
  #ifdef RGEO_GEOS_SUPPORTS_UNARYUNION
446
462
  return Qtrue;
@@ -449,98 +465,93 @@ static VALUE cmethod_factory_supports_unary_union(VALUE klass)
449
465
  #endif
450
466
  }
451
467
 
452
- static VALUE cmethod_factory_create(VALUE klass, VALUE flags, VALUE srid, VALUE buffer_resolution,
453
- VALUE wkt_generator, VALUE wkb_generator, VALUE proj4_obj, VALUE coord_sys_obj)
468
+ static VALUE
469
+ cmethod_factory_create(VALUE klass,
470
+ VALUE flags,
471
+ VALUE srid,
472
+ VALUE buffer_resolution,
473
+ VALUE wkt_generator,
474
+ VALUE wkb_generator,
475
+ VALUE coord_sys_obj)
454
476
  {
455
477
  VALUE result;
456
478
  RGeo_FactoryData* data;
457
- GEOSContextHandle_t context;
458
479
 
459
480
  result = Qnil;
460
481
  data = ALLOC(RGeo_FactoryData);
461
482
  if (data) {
462
- context = initGEOS_r(message_handler, message_handler);
463
- if (context) {
464
- data->geos_context = context;
465
- data->flags = NUM2INT(flags);
466
- data->srid = NUM2INT(srid);
467
- data->buffer_resolution = NUM2INT(buffer_resolution);
468
- data->wkt_reader = NULL;
469
- data->wkb_reader = NULL;
470
- data->wkt_writer = NULL;
471
- data->wkb_writer = NULL;
472
- data->psych_wkt_reader = NULL;
473
- data->marshal_wkb_reader = NULL;
474
- data->psych_wkt_writer = NULL;
475
- data->marshal_wkb_writer = NULL;
476
- data->wkrep_wkt_generator = wkt_generator;
477
- data->wkrep_wkb_generator = wkb_generator;
478
- data->wkrep_wkt_parser = Qnil;
479
- data->wkrep_wkb_parser = Qnil;
480
- data->proj4_obj = proj4_obj;
481
- data->coord_sys_obj = coord_sys_obj;
482
- result = TypedData_Wrap_Struct(klass, &rgeo_factory_type, data);
483
- }
484
- else {
485
- free(data);
486
- }
483
+ data->flags = RB_NUM2INT(flags);
484
+ data->srid = RB_NUM2INT(srid);
485
+ data->buffer_resolution = RB_NUM2INT(buffer_resolution);
486
+ data->wkt_reader = NULL;
487
+ data->wkb_reader = NULL;
488
+ data->wkt_writer = NULL;
489
+ data->wkb_writer = NULL;
490
+ data->psych_wkt_reader = NULL;
491
+ data->marshal_wkb_reader = NULL;
492
+ data->psych_wkt_writer = NULL;
493
+ data->marshal_wkb_writer = NULL;
494
+ data->wkrep_wkt_generator = wkt_generator;
495
+ data->wkrep_wkb_generator = wkb_generator;
496
+ data->wkrep_wkt_parser = Qnil;
497
+ data->wkrep_wkb_parser = Qnil;
498
+ data->coord_sys_obj = coord_sys_obj;
499
+ result = TypedData_Wrap_Struct(klass, &rgeo_factory_type, data);
487
500
  }
488
501
  return result;
489
502
  }
490
503
 
491
-
492
- static VALUE alloc_factory(VALUE klass)
504
+ static VALUE
505
+ alloc_factory(VALUE klass)
493
506
  {
494
- return cmethod_factory_create(klass, INT2NUM(0), INT2NUM(0), INT2NUM(0), Qnil, Qnil, Qnil, Qnil);
507
+ return cmethod_factory_create(
508
+ klass, INT2NUM(0), INT2NUM(0), INT2NUM(0), Qnil, Qnil, Qnil);
495
509
  }
496
510
 
497
-
498
- static VALUE method_factory_initialize_copy(VALUE self, VALUE orig)
511
+ static VALUE
512
+ method_factory_initialize_copy(VALUE self, VALUE orig)
499
513
  {
500
514
  RGeo_FactoryData* self_data;
501
515
  RGeo_FactoryData* orig_data;
502
- GEOSContextHandle_t context;
503
516
 
504
517
  // Clear out existing data
505
518
  self_data = RGEO_FACTORY_DATA_PTR(self);
506
- context = self_data->geos_context;
507
519
  if (self_data->wkt_reader) {
508
- GEOSWKTReader_destroy_r(context, self_data->wkt_reader);
520
+ GEOSWKTReader_destroy(self_data->wkt_reader);
509
521
  self_data->wkt_reader = NULL;
510
522
  }
511
523
  if (self_data->wkb_reader) {
512
- GEOSWKBReader_destroy_r(context, self_data->wkb_reader);
524
+ GEOSWKBReader_destroy(self_data->wkb_reader);
513
525
  self_data->wkb_reader = NULL;
514
526
  }
515
527
  if (self_data->wkt_writer) {
516
- GEOSWKTWriter_destroy_r(context, self_data->wkt_writer);
528
+ GEOSWKTWriter_destroy(self_data->wkt_writer);
517
529
  self_data->wkt_writer = NULL;
518
530
  }
519
531
  if (self_data->wkb_writer) {
520
- GEOSWKBWriter_destroy_r(context, self_data->wkb_writer);
532
+ GEOSWKBWriter_destroy(self_data->wkb_writer);
521
533
  self_data->wkb_writer = NULL;
522
534
  }
523
535
  if (self_data->psych_wkt_reader) {
524
- GEOSWKTReader_destroy_r(context, self_data->psych_wkt_reader);
536
+ GEOSWKTReader_destroy(self_data->psych_wkt_reader);
525
537
  self_data->psych_wkt_reader = NULL;
526
538
  }
527
539
  if (self_data->marshal_wkb_reader) {
528
- GEOSWKBReader_destroy_r(context, self_data->marshal_wkb_reader);
540
+ GEOSWKBReader_destroy(self_data->marshal_wkb_reader);
529
541
  self_data->marshal_wkb_reader = NULL;
530
542
  }
531
543
  if (self_data->psych_wkt_writer) {
532
- GEOSWKTWriter_destroy_r(context, self_data->psych_wkt_writer);
544
+ GEOSWKTWriter_destroy(self_data->psych_wkt_writer);
533
545
  self_data->psych_wkt_writer = NULL;
534
546
  }
535
547
  if (self_data->marshal_wkb_writer) {
536
- GEOSWKBWriter_destroy_r(context, self_data->marshal_wkb_writer);
548
+ GEOSWKBWriter_destroy(self_data->marshal_wkb_writer);
537
549
  self_data->marshal_wkb_writer = NULL;
538
550
  }
539
551
  self_data->wkrep_wkt_generator = Qnil;
540
552
  self_data->wkrep_wkb_generator = Qnil;
541
553
  self_data->wkrep_wkt_parser = Qnil;
542
554
  self_data->wkrep_wkb_parser = Qnil;
543
- self_data->proj4_obj = Qnil;
544
555
  self_data->coord_sys_obj = Qnil;
545
556
 
546
557
  // Copy new data from original object
@@ -553,14 +564,13 @@ static VALUE method_factory_initialize_copy(VALUE self, VALUE orig)
553
564
  self_data->wkrep_wkb_generator = orig_data->wkrep_wkb_generator;
554
565
  self_data->wkrep_wkt_parser = orig_data->wkrep_wkt_parser;
555
566
  self_data->wkrep_wkb_parser = orig_data->wkrep_wkb_parser;
556
- self_data->proj4_obj = orig_data->proj4_obj;
557
567
  self_data->coord_sys_obj = orig_data->coord_sys_obj;
558
568
  }
559
569
  return self;
560
570
  }
561
571
 
562
-
563
- static VALUE method_set_wkrep_parsers(VALUE self, VALUE wkt_parser, VALUE wkb_parser)
572
+ static VALUE
573
+ method_set_wkrep_parsers(VALUE self, VALUE wkt_parser, VALUE wkb_parser)
564
574
  {
565
575
  RGeo_FactoryData* self_data;
566
576
 
@@ -571,87 +581,126 @@ static VALUE method_set_wkrep_parsers(VALUE self, VALUE wkt_parser, VALUE wkb_pa
571
581
  return self;
572
582
  }
573
583
 
574
-
575
- static VALUE method_get_proj4(VALUE self)
576
- {
577
- return RGEO_FACTORY_DATA_PTR(self)->proj4_obj;
578
- }
579
-
580
-
581
- static VALUE method_get_coord_sys(VALUE self)
584
+ static VALUE
585
+ method_get_coord_sys(VALUE self)
582
586
  {
583
587
  return RGEO_FACTORY_DATA_PTR(self)->coord_sys_obj;
584
588
  }
585
589
 
586
-
587
- static VALUE method_get_wkt_generator(VALUE self)
590
+ static VALUE
591
+ method_get_wkt_generator(VALUE self)
588
592
  {
589
593
  return RGEO_FACTORY_DATA_PTR(self)->wkrep_wkt_generator;
590
594
  }
591
595
 
592
-
593
- static VALUE method_get_wkb_generator(VALUE self)
596
+ static VALUE
597
+ method_get_wkb_generator(VALUE self)
594
598
  {
595
599
  return RGEO_FACTORY_DATA_PTR(self)->wkrep_wkb_generator;
596
600
  }
597
601
 
598
-
599
- static VALUE method_get_wkt_parser(VALUE self)
602
+ static VALUE
603
+ method_get_wkt_parser(VALUE self)
600
604
  {
601
605
  return RGEO_FACTORY_DATA_PTR(self)->wkrep_wkt_parser;
602
606
  }
603
607
 
604
-
605
- static VALUE method_get_wkb_parser(VALUE self)
608
+ static VALUE
609
+ method_get_wkb_parser(VALUE self)
606
610
  {
607
611
  return RGEO_FACTORY_DATA_PTR(self)->wkrep_wkb_parser;
608
612
  }
609
613
 
610
-
611
- static VALUE alloc_geometry(VALUE klass)
614
+ static VALUE
615
+ alloc_geometry(VALUE klass)
612
616
  {
613
617
  return rgeo_wrap_geos_geometry(Qnil, NULL, klass);
614
618
  }
615
619
 
616
-
617
620
  /**** INITIALIZATION FUNCTION ****/
618
621
 
619
-
620
- void rgeo_init_geos_factory()
622
+ void
623
+ rgeo_init_geos_factory()
621
624
  {
622
625
  VALUE geos_factory_class;
623
626
 
624
627
  #ifndef RGEO_GEOS_SUPPORTS_SETOUTPUTDIMENSION
625
- /* We favor rb_gc_register_address over rb_gc_register_mark_object because the value changes at runtime */
628
+ /* We favor rb_gc_register_address over rb_gc_register_mark_object because
629
+ * the value changes at runtime */
626
630
  psych_wkt_generator = Qnil;
627
631
  rb_gc_register_address(&psych_wkt_generator);
628
632
  marshal_wkb_generator = Qnil;
629
633
  rb_gc_register_address(&marshal_wkb_generator);
630
634
  #endif
631
635
 
632
- // Add C methods to the factory.
633
- geos_factory_class = rb_define_class_under(rgeo_geos_module, "CAPIFactory", rb_cObject);
636
+ geos_factory_class =
637
+ rb_define_class_under(rgeo_geos_module, "CAPIFactory", rb_cObject);
634
638
  rb_define_alloc_func(geos_factory_class, alloc_factory);
635
- rb_define_method(geos_factory_class, "initialize_copy", method_factory_initialize_copy, 1);
636
- rb_define_method(geos_factory_class, "_parse_wkt_impl", method_factory_parse_wkt, 1);
637
- rb_define_method(geos_factory_class, "_parse_wkb_impl", method_factory_parse_wkb, 1);
639
+ // Add C constants to the factory.
640
+ rb_define_const(geos_factory_class,
641
+ "FLAG_SUPPORTS_Z",
642
+ INT2FIX(RGEO_FACTORYFLAGS_SUPPORTS_Z));
643
+ rb_define_const(geos_factory_class,
644
+ "FLAG_SUPPORTS_M",
645
+ INT2FIX(RGEO_FACTORYFLAGS_SUPPORTS_M));
646
+ rb_define_const(geos_factory_class,
647
+ "FLAG_SUPPORTS_Z_OR_M",
648
+ INT2FIX(RGEO_FACTORYFLAGS_SUPPORTS_Z_OR_M));
649
+ rb_define_const(geos_factory_class,
650
+ "FLAG_PREPARE_HEURISTIC",
651
+ INT2FIX(RGEO_FACTORYFLAGS_PREPARE_HEURISTIC));
652
+ // Add C methods to the factory.
653
+ rb_define_method(
654
+ geos_factory_class, "initialize_copy", method_factory_initialize_copy, 1);
655
+ rb_define_method(
656
+ geos_factory_class, "_parse_wkt_impl", method_factory_parse_wkt, 1);
657
+ rb_define_method(
658
+ geos_factory_class, "_parse_wkb_impl", method_factory_parse_wkb, 1);
638
659
  rb_define_method(geos_factory_class, "_srid", method_factory_srid, 0);
639
- rb_define_method(geos_factory_class, "_buffer_resolution", method_factory_buffer_resolution, 0);
660
+ rb_define_method(geos_factory_class,
661
+ "_buffer_resolution",
662
+ method_factory_buffer_resolution,
663
+ 0);
640
664
  rb_define_method(geos_factory_class, "_flags", method_factory_flags, 0);
641
- rb_define_method(geos_factory_class, "_set_wkrep_parsers", method_set_wkrep_parsers, 2);
642
- rb_define_method(geos_factory_class, "_proj4", method_get_proj4, 0);
665
+ rb_define_method(
666
+ geos_factory_class, "supports_z?", method_factory_supports_z_p, 0);
667
+ rb_define_method(
668
+ geos_factory_class, "supports_m?", method_factory_supports_m_p, 0);
669
+ rb_define_method(geos_factory_class,
670
+ "supports_z_or_m?",
671
+ method_factory_supports_z_or_m_p,
672
+ 0);
673
+ rb_define_method(geos_factory_class,
674
+ "prepare_heuristic?",
675
+ method_factory_prepare_heuristic_p,
676
+ 0);
677
+ rb_define_method(
678
+ geos_factory_class, "_set_wkrep_parsers", method_set_wkrep_parsers, 2);
643
679
  rb_define_method(geos_factory_class, "_coord_sys", method_get_coord_sys, 0);
644
- rb_define_method(geos_factory_class, "_wkt_generator", method_get_wkt_generator, 0);
645
- rb_define_method(geos_factory_class, "_wkb_generator", method_get_wkb_generator, 0);
680
+ rb_define_method(
681
+ geos_factory_class, "_wkt_generator", method_get_wkt_generator, 0);
682
+ rb_define_method(
683
+ geos_factory_class, "_wkb_generator", method_get_wkb_generator, 0);
646
684
  rb_define_method(geos_factory_class, "_wkt_parser", method_get_wkt_parser, 0);
647
685
  rb_define_method(geos_factory_class, "_wkb_parser", method_get_wkb_parser, 0);
648
- rb_define_method(geos_factory_class, "read_for_marshal", method_factory_read_for_marshal, 1);
649
- rb_define_method(geos_factory_class, "write_for_marshal", method_factory_write_for_marshal, 1);
650
- rb_define_method(geos_factory_class, "read_for_psych", method_factory_read_for_psych, 1);
651
- rb_define_method(geos_factory_class, "write_for_psych", method_factory_write_for_psych, 1);
652
- rb_define_module_function(geos_factory_class, "_create", cmethod_factory_create, 7);
653
- rb_define_module_function(geos_factory_class, "_geos_version", cmethod_factory_geos_version, 0);
654
- rb_define_module_function(geos_factory_class, "_supports_unary_union?", cmethod_factory_supports_unary_union, 0);
686
+ rb_define_method(
687
+ geos_factory_class, "read_for_marshal", method_factory_read_for_marshal, 1);
688
+ rb_define_method(geos_factory_class,
689
+ "write_for_marshal",
690
+ method_factory_write_for_marshal,
691
+ 1);
692
+ rb_define_method(
693
+ geos_factory_class, "read_for_psych", method_factory_read_for_psych, 1);
694
+ rb_define_method(
695
+ geos_factory_class, "write_for_psych", method_factory_write_for_psych, 1);
696
+ rb_define_module_function(
697
+ geos_factory_class, "_create", cmethod_factory_create, 6);
698
+ rb_define_module_function(
699
+ geos_factory_class, "_geos_version", cmethod_factory_geos_version, 0);
700
+ rb_define_module_function(geos_factory_class,
701
+ "_supports_unary_union?",
702
+ cmethod_factory_supports_unary_union,
703
+ 0);
655
704
 
656
705
  // Define allocation methods for global class types
657
706
  rb_define_alloc_func(rgeo_geos_geometry_class, alloc_geometry);
@@ -666,15 +715,13 @@ void rgeo_init_geos_factory()
666
715
  rb_define_alloc_func(rgeo_geos_multi_polygon_class, alloc_geometry);
667
716
  }
668
717
 
669
-
670
718
  /**** OTHER PUBLIC FUNCTIONS ****/
671
719
 
672
-
673
- VALUE rgeo_wrap_geos_geometry(VALUE factory, GEOSGeometry* geom, VALUE klass)
720
+ VALUE
721
+ rgeo_wrap_geos_geometry(VALUE factory, GEOSGeometry* geom, VALUE klass)
674
722
  {
675
723
  VALUE result;
676
724
  RGeo_FactoryData* factory_data;
677
- GEOSContextHandle_t factory_context;
678
725
  VALUE klasses;
679
726
  VALUE inferred_klass;
680
727
  char is_collection;
@@ -683,14 +730,14 @@ VALUE rgeo_wrap_geos_geometry(VALUE factory, GEOSGeometry* geom, VALUE klass)
683
730
  result = Qnil;
684
731
  if (geom || !NIL_P(klass)) {
685
732
  factory_data = NIL_P(factory) ? NULL : RGEO_FACTORY_DATA_PTR(factory);
686
- factory_context = factory_data ? factory_data->geos_context : NULL;
687
733
 
688
734
  // We don't allow "empty" points, so replace such objects with
689
735
  // an empty collection.
690
736
  if (geom && factory) {
691
- if (GEOSGeomTypeId_r(factory_context, geom) == GEOS_POINT && GEOSGetNumCoordinates_r(factory_context, geom) == 0) {
692
- GEOSGeom_destroy_r(factory_context, geom);
693
- geom = GEOSGeom_createCollection_r(factory_context, GEOS_GEOMETRYCOLLECTION, NULL, 0);
737
+ if (GEOSGeomTypeId(geom) == GEOS_POINT &&
738
+ GEOSGetNumCoordinates(geom) == 0) {
739
+ GEOSGeom_destroy(geom);
740
+ geom = GEOSGeom_createCollection(GEOS_GEOMETRYCOLLECTION, NULL, 0);
694
741
  klass = rgeo_geos_geometry_collection_class;
695
742
  }
696
743
  }
@@ -699,38 +746,38 @@ VALUE rgeo_wrap_geos_geometry(VALUE factory, GEOSGeometry* geom, VALUE klass)
699
746
  if (TYPE(klass) != T_CLASS) {
700
747
  inferred_klass = Qnil;
701
748
  is_collection = 0;
702
- switch (GEOSGeomTypeId_r(factory_context, geom)) {
703
- case GEOS_POINT:
704
- inferred_klass = rgeo_geos_point_class;
705
- break;
706
- case GEOS_LINESTRING:
707
- inferred_klass = rgeo_geos_line_string_class;
708
- break;
709
- case GEOS_LINEARRING:
710
- inferred_klass = rgeo_geos_linear_ring_class;
711
- break;
712
- case GEOS_POLYGON:
713
- inferred_klass = rgeo_geos_polygon_class;
714
- break;
715
- case GEOS_MULTIPOINT:
716
- inferred_klass = rgeo_geos_multi_point_class;
717
- is_collection = 1;
718
- break;
719
- case GEOS_MULTILINESTRING:
720
- inferred_klass = rgeo_geos_multi_line_string_class;
721
- is_collection = 1;
722
- break;
723
- case GEOS_MULTIPOLYGON:
724
- inferred_klass = rgeo_geos_multi_polygon_class;
725
- is_collection = 1;
726
- break;
727
- case GEOS_GEOMETRYCOLLECTION:
728
- inferred_klass = rgeo_geos_geometry_collection_class;
729
- is_collection = 1;
730
- break;
731
- default:
732
- inferred_klass = rgeo_geos_geometry_class;
733
- break;
749
+ switch (GEOSGeomTypeId(geom)) {
750
+ case GEOS_POINT:
751
+ inferred_klass = rgeo_geos_point_class;
752
+ break;
753
+ case GEOS_LINESTRING:
754
+ inferred_klass = rgeo_geos_line_string_class;
755
+ break;
756
+ case GEOS_LINEARRING:
757
+ inferred_klass = rgeo_geos_linear_ring_class;
758
+ break;
759
+ case GEOS_POLYGON:
760
+ inferred_klass = rgeo_geos_polygon_class;
761
+ break;
762
+ case GEOS_MULTIPOINT:
763
+ inferred_klass = rgeo_geos_multi_point_class;
764
+ is_collection = 1;
765
+ break;
766
+ case GEOS_MULTILINESTRING:
767
+ inferred_klass = rgeo_geos_multi_line_string_class;
768
+ is_collection = 1;
769
+ break;
770
+ case GEOS_MULTIPOLYGON:
771
+ inferred_klass = rgeo_geos_multi_polygon_class;
772
+ is_collection = 1;
773
+ break;
774
+ case GEOS_GEOMETRYCOLLECTION:
775
+ inferred_klass = rgeo_geos_geometry_collection_class;
776
+ is_collection = 1;
777
+ break;
778
+ default:
779
+ inferred_klass = rgeo_geos_geometry_class;
780
+ break;
734
781
  }
735
782
  if (TYPE(klass) == T_ARRAY && is_collection) {
736
783
  klasses = klass;
@@ -740,12 +787,14 @@ VALUE rgeo_wrap_geos_geometry(VALUE factory, GEOSGeometry* geom, VALUE klass)
740
787
  data = ALLOC(RGeo_GeometryData);
741
788
  if (data) {
742
789
  if (geom) {
743
- GEOSSetSRID_r(factory_context, geom, factory_data->srid);
790
+ GEOSSetSRID(geom, factory_data->srid);
744
791
  }
745
- data->geos_context = factory_context;
746
792
  data->geom = geom;
747
- data->prep = factory_data && ((factory_data->flags & RGEO_FACTORYFLAGS_PREPARE_HEURISTIC) != 0) ?
748
- (GEOSPreparedGeometry*)1 : NULL;
793
+ data->prep =
794
+ factory_data &&
795
+ ((factory_data->flags & RGEO_FACTORYFLAGS_PREPARE_HEURISTIC) != 0)
796
+ ? (GEOSPreparedGeometry*)1
797
+ : NULL;
749
798
  data->factory = factory;
750
799
  data->klasses = klasses;
751
800
  result = TypedData_Wrap_Struct(klass, &rgeo_geometry_type, data);
@@ -754,14 +803,17 @@ VALUE rgeo_wrap_geos_geometry(VALUE factory, GEOSGeometry* geom, VALUE klass)
754
803
  return result;
755
804
  }
756
805
 
757
- VALUE rgeo_wrap_geos_geometry_clone(VALUE factory, const GEOSGeometry* geom, VALUE klass)
806
+ VALUE
807
+ rgeo_wrap_geos_geometry_clone(VALUE factory,
808
+ const GEOSGeometry* geom,
809
+ VALUE klass)
758
810
  {
759
811
  VALUE result;
760
812
  GEOSGeometry* clone_geom;
761
813
 
762
814
  result = Qnil;
763
815
  if (geom) {
764
- clone_geom = GEOSGeom_clone_r(RGEO_FACTORY_DATA_PTR(factory)->geos_context, geom);
816
+ clone_geom = GEOSGeom_clone(geom);
765
817
  if (clone_geom) {
766
818
  result = rgeo_wrap_geos_geometry(factory, clone_geom, klass);
767
819
  }
@@ -769,26 +821,49 @@ VALUE rgeo_wrap_geos_geometry_clone(VALUE factory, const GEOSGeometry* geom, VAL
769
821
  return result;
770
822
  }
771
823
 
772
-
773
- const GEOSGeometry* rgeo_convert_to_geos_geometry(VALUE factory, VALUE obj, VALUE type)
824
+ const GEOSGeometry*
825
+ rgeo_convert_to_geos_geometry(VALUE factory, VALUE obj, VALUE type, int* state)
774
826
  {
775
827
  VALUE object;
776
828
 
777
- if (NIL_P(type) && RGEO_GEOMETRY_TYPEDDATA_P(obj) && RGEO_GEOMETRY_DATA_PTR(obj)->factory == factory) {
829
+ if (NIL_P(type) && RGEO_GEOMETRY_TYPEDDATA_P(obj) &&
830
+ RGEO_GEOMETRY_DATA_PTR(obj)->factory == factory) {
778
831
  object = obj;
832
+ } else {
833
+ object =
834
+ rb_funcall(rgeo_feature_module, rb_intern("cast"), 3, obj, factory, type);
779
835
  }
780
- else {
781
- object = rb_funcall(rgeo_feature_module, rb_intern("cast"), 3, obj, factory, type);
836
+ if (NIL_P(object)) {
837
+ rb_protect(
838
+ rb_exc_raise_value,
839
+ rb_exc_new_cstr(rb_eRGeoInvalidGeometry,
840
+ "Unable to cast the geometry to the GEOS Factory"),
841
+ state);
782
842
  }
783
- if (NIL_P(object))
843
+
844
+ if (*state) {
784
845
  return NULL;
846
+ }
847
+
848
+ if (!rgeo_is_geos_object(object)) {
849
+ rb_protect(rb_exc_raise_value,
850
+ rb_exc_new_cstr(rb_eRGeoError, "Not a GEOS Geometry object."),
851
+ state);
852
+ }
853
+
854
+ if (*state) {
855
+ return NULL;
856
+ }
785
857
 
786
- Check_TypedStruct(object, &rgeo_geometry_type);
787
858
  return RGEO_GEOMETRY_DATA_PTR(object)->geom;
788
859
  }
789
860
 
790
-
791
- GEOSGeometry* rgeo_convert_to_detached_geos_geometry(VALUE obj, VALUE factory, VALUE type, VALUE* klasses)
861
+ GEOSGeometry*
862
+ rgeo_convert_to_detached_geos_geometry(VALUE obj,
863
+ VALUE factory,
864
+ VALUE type,
865
+ VALUE* klasses,
866
+ int* state)
792
867
  {
793
868
  VALUE object;
794
869
  GEOSGeometry* geom;
@@ -798,51 +873,84 @@ GEOSGeometry* rgeo_convert_to_detached_geos_geometry(VALUE obj, VALUE factory, V
798
873
  if (klasses) {
799
874
  *klasses = Qnil;
800
875
  }
801
- object = rb_funcall(rgeo_feature_module, rb_intern("cast"), 5, obj, factory, type, ID2SYM(rb_intern("force_new")), ID2SYM(rb_intern("keep_subtype")));
802
- geom = NULL;
803
- if (!NIL_P(object)) {
804
- object_data = RGEO_GEOMETRY_DATA_PTR(object);
805
- geom = object_data->geom;
806
- if (klasses) {
807
- *klasses = object_data->klasses;
808
- if (NIL_P(*klasses)) {
809
- *klasses = CLASS_OF(object);
810
- }
811
- }
812
- prep = object_data->prep;
813
- if (prep && prep != (GEOSPreparedGeometry*)1 && prep != (GEOSPreparedGeometry*)2) {
814
- GEOSPreparedGeom_destroy_r(object_data->geos_context, prep);
876
+
877
+ object = rb_protect_funcall(rgeo_feature_module,
878
+ rb_intern("cast"),
879
+ state,
880
+ 5,
881
+ obj,
882
+ factory,
883
+ type,
884
+ ID2SYM(rb_intern("force_new")),
885
+ ID2SYM(rb_intern("keep_subtype")));
886
+
887
+ if (NIL_P(object)) {
888
+ rb_protect(
889
+ rb_exc_raise_value,
890
+ rb_exc_new_cstr(rb_eRGeoInvalidGeometry,
891
+ "Unable to cast the geometry to the GEOS Factory"),
892
+ state);
893
+ }
894
+ if (*state) {
895
+ return NULL;
896
+ }
897
+
898
+ if (!rgeo_is_geos_object(object)) {
899
+ rb_protect(rb_exc_raise_value,
900
+ rb_exc_new_cstr(rb_eRGeoError, "Not a GEOS Geometry object."),
901
+ state);
902
+ }
903
+ if (*state) {
904
+ return NULL;
905
+ }
906
+
907
+ object_data = RGEO_GEOMETRY_DATA_PTR(object);
908
+ geom = object_data->geom;
909
+ if (klasses) {
910
+ *klasses = object_data->klasses;
911
+ if (NIL_P(*klasses)) {
912
+ *klasses = CLASS_OF(object);
815
913
  }
816
- object_data->geos_context = NULL;
817
- object_data->geom = NULL;
818
- object_data->prep = NULL;
819
- object_data->factory = Qnil;
820
- object_data->klasses = Qnil;
821
914
  }
915
+ prep = object_data->prep;
916
+ if (prep && prep != (GEOSPreparedGeometry*)1 &&
917
+ prep != (GEOSPreparedGeometry*)2) {
918
+ GEOSPreparedGeom_destroy(prep);
919
+ }
920
+ object_data->geom = NULL;
921
+ object_data->prep = NULL;
922
+ object_data->factory = Qnil;
923
+ object_data->klasses = Qnil;
924
+
822
925
  return geom;
823
926
  }
824
927
 
825
-
826
- char rgeo_is_geos_object(VALUE obj)
928
+ char
929
+ rgeo_is_geos_object(VALUE obj)
827
930
  {
828
931
  return RGEO_GEOMETRY_TYPEDDATA_P(obj) ? 1 : 0;
829
932
  }
830
933
 
831
- void rgeo_check_geos_object(VALUE obj)
934
+ void
935
+ rgeo_check_geos_object(VALUE obj)
832
936
  {
833
937
  if (!rgeo_is_geos_object(obj)) {
834
- rb_raise(rgeo_error, "Not a GEOS Geometry object.");
938
+ rb_raise(rb_eRGeoError, "Not a GEOS Geometry object.");
835
939
  }
836
940
  }
837
941
 
838
-
839
- const GEOSGeometry* rgeo_get_geos_geometry_safe(VALUE obj)
942
+ const GEOSGeometry*
943
+ rgeo_get_geos_geometry_safe(VALUE obj)
840
944
  {
841
- return RGEO_GEOMETRY_TYPEDDATA_P(obj) ? (const GEOSGeometry*)(RGEO_GEOMETRY_DATA_PTR(obj)->geom) : NULL;
945
+ return RGEO_GEOMETRY_TYPEDDATA_P(obj)
946
+ ? (const GEOSGeometry*)(RGEO_GEOMETRY_DATA_PTR(obj)->geom)
947
+ : NULL;
842
948
  }
843
949
 
844
-
845
- VALUE rgeo_geos_coordseqs_eql(GEOSContextHandle_t context, const GEOSGeometry* geom1, const GEOSGeometry* geom2, char check_z)
950
+ VALUE
951
+ rgeo_geos_coordseqs_eql(const GEOSGeometry* geom1,
952
+ const GEOSGeometry* geom2,
953
+ char check_z)
846
954
  {
847
955
  VALUE result;
848
956
  const GEOSCoordSequence* cs1;
@@ -854,27 +962,30 @@ VALUE rgeo_geos_coordseqs_eql(GEOSContextHandle_t context, const GEOSGeometry* g
854
962
 
855
963
  result = Qnil;
856
964
  if (geom1 && geom2) {
857
- cs1 = GEOSGeom_getCoordSeq_r(context, geom1);
858
- cs2 = GEOSGeom_getCoordSeq_r(context, geom2);
965
+ cs1 = GEOSGeom_getCoordSeq(geom1);
966
+ cs2 = GEOSGeom_getCoordSeq(geom2);
859
967
  if (cs1 && cs2) {
860
968
  len1 = 0;
861
969
  len2 = 0;
862
- if (GEOSCoordSeq_getSize_r(context, cs1, &len1) && GEOSCoordSeq_getSize_r(context, cs2, &len2)) {
970
+ if (GEOSCoordSeq_getSize(cs1, &len1) &&
971
+ GEOSCoordSeq_getSize(cs2, &len2)) {
863
972
  if (len1 == len2) {
864
973
  result = Qtrue;
865
- for (i=0; i<len1; ++i) {
866
- if (GEOSCoordSeq_getX_r(context, cs1, i, &val1) && GEOSCoordSeq_getX_r(context, cs2, i, &val2)) {
974
+ for (i = 0; i < len1; ++i) {
975
+ if (GEOSCoordSeq_getX(cs1, i, &val1) &&
976
+ GEOSCoordSeq_getX(cs2, i, &val2)) {
867
977
  if (val1 == val2) {
868
- if (GEOSCoordSeq_getY_r(context, cs1, i, &val1) && GEOSCoordSeq_getY_r(context, cs2, i, &val2)) {
978
+ if (GEOSCoordSeq_getY(cs1, i, &val1) &&
979
+ GEOSCoordSeq_getY(cs2, i, &val2)) {
869
980
  if (val1 == val2) {
870
981
  if (check_z) {
871
982
  val1 = 0;
872
- if (!GEOSCoordSeq_getZ_r(context, cs1, i, &val1)) {
983
+ if (!GEOSCoordSeq_getZ(cs1, i, &val1)) {
873
984
  result = Qnil;
874
985
  break;
875
986
  }
876
987
  val2 = 0;
877
- if (!GEOSCoordSeq_getZ_r(context, cs2, i, &val2)) {
988
+ if (!GEOSCoordSeq_getZ(cs2, i, &val2)) {
878
989
  result = Qnil;
879
990
  break;
880
991
  }
@@ -883,29 +994,24 @@ VALUE rgeo_geos_coordseqs_eql(GEOSContextHandle_t context, const GEOSGeometry* g
883
994
  break;
884
995
  }
885
996
  }
886
- }
887
- else { // Y coords are different
997
+ } else { // Y coords are different
888
998
  result = Qfalse;
889
999
  break;
890
1000
  }
891
- }
892
- else { // Failed to get Y coords
1001
+ } else { // Failed to get Y coords
893
1002
  result = Qnil;
894
1003
  break;
895
1004
  }
896
- }
897
- else { // X coords are different
1005
+ } else { // X coords are different
898
1006
  result = Qfalse;
899
1007
  break;
900
1008
  }
901
- }
902
- else { // Failed to get X coords
1009
+ } else { // Failed to get X coords
903
1010
  result = Qnil;
904
1011
  break;
905
1012
  }
906
- } // Iteration over coords
907
- }
908
- else { // Lengths are different
1013
+ } // Iteration over coords
1014
+ } else { // Lengths are different
909
1015
  result = Qfalse;
910
1016
  }
911
1017
  }
@@ -914,8 +1020,8 @@ VALUE rgeo_geos_coordseqs_eql(GEOSContextHandle_t context, const GEOSGeometry* g
914
1020
  return result;
915
1021
  }
916
1022
 
917
-
918
- VALUE rgeo_geos_klasses_and_factories_eql(VALUE obj1, VALUE obj2)
1023
+ VALUE
1024
+ rgeo_geos_klasses_and_factories_eql(VALUE obj1, VALUE obj2)
919
1025
  {
920
1026
  VALUE result;
921
1027
  VALUE factory;
@@ -923,24 +1029,25 @@ VALUE rgeo_geos_klasses_and_factories_eql(VALUE obj1, VALUE obj2)
923
1029
  result = Qnil;
924
1030
  if (rb_obj_class(obj1) != rb_obj_class(obj2)) {
925
1031
  result = Qfalse;
926
- }
927
- else {
1032
+ } else {
928
1033
  factory = RGEO_GEOMETRY_DATA_PTR(obj1)->factory;
929
1034
  /* No need to cache the internal here (https://ips.fastruby.io/4x) */
930
- result = rb_funcall(factory, rb_intern("eql?"), 1, RGEO_GEOMETRY_DATA_PTR(obj2)->factory);
1035
+ result = rb_funcall(
1036
+ factory, rb_intern("eql?"), 1, RGEO_GEOMETRY_DATA_PTR(obj2)->factory);
931
1037
  }
932
1038
  return result;
933
1039
  }
934
1040
 
935
-
936
- typedef struct {
1041
+ typedef struct
1042
+ {
937
1043
  st_index_t seed_hash;
938
1044
  double x;
939
1045
  double y;
940
1046
  double z;
941
1047
  } RGeo_Coordseq_Hash_Struct;
942
1048
 
943
- st_index_t rgeo_geos_coordseq_hash(GEOSContextHandle_t context, const GEOSGeometry* geom, st_index_t hash)
1049
+ st_index_t
1050
+ rgeo_geos_coordseq_hash(const GEOSGeometry* geom, st_index_t hash)
944
1051
  {
945
1052
  const GEOSCoordSequence* cs;
946
1053
  unsigned int len;
@@ -948,17 +1055,18 @@ st_index_t rgeo_geos_coordseq_hash(GEOSContextHandle_t context, const GEOSGeomet
948
1055
  RGeo_Coordseq_Hash_Struct hash_struct;
949
1056
 
950
1057
  if (geom) {
951
- cs = GEOSGeom_getCoordSeq_r(context, geom);
1058
+ cs = GEOSGeom_getCoordSeq(geom);
952
1059
  if (cs) {
953
- if (GEOSCoordSeq_getSize_r(context, cs, &len)) {
954
- for (i=0; i<len; ++i) {
955
- if (GEOSCoordSeq_getX_r(context, cs, i, &hash_struct.x)) {
956
- if (GEOSCoordSeq_getY_r(context, cs, i, &hash_struct.y)) {
957
- if (!GEOSCoordSeq_getY_r(context, cs, i, &hash_struct.z)) {
1060
+ if (GEOSCoordSeq_getSize(cs, &len)) {
1061
+ for (i = 0; i < len; ++i) {
1062
+ if (GEOSCoordSeq_getX(cs, i, &hash_struct.x)) {
1063
+ if (GEOSCoordSeq_getY(cs, i, &hash_struct.y)) {
1064
+ if (!GEOSCoordSeq_getZ(cs, i, &hash_struct.z)) {
958
1065
  hash_struct.z = 0;
959
1066
  }
960
1067
  hash_struct.seed_hash = hash;
961
- hash = rb_memhash(&hash_struct, sizeof(RGeo_Coordseq_Hash_Struct));
1068
+ hash =
1069
+ rb_memhash(&hash_struct, sizeof(RGeo_Coordseq_Hash_Struct));
962
1070
  }
963
1071
  }
964
1072
  }
@@ -968,14 +1076,15 @@ st_index_t rgeo_geos_coordseq_hash(GEOSContextHandle_t context, const GEOSGeomet
968
1076
  return hash;
969
1077
  }
970
1078
 
971
-
972
- typedef struct {
1079
+ typedef struct
1080
+ {
973
1081
  st_index_t seed_hash;
974
1082
  st_index_t h1;
975
1083
  st_index_t h2;
976
1084
  } RGeo_Objbase_Hash_Struct;
977
1085
 
978
- st_index_t rgeo_geos_objbase_hash(VALUE factory, VALUE type_module, st_index_t hash)
1086
+ st_index_t
1087
+ rgeo_geos_objbase_hash(VALUE factory, VALUE type_module, st_index_t hash)
979
1088
  {
980
1089
  ID hash_method;
981
1090
  RGeo_Objbase_Hash_Struct hash_struct;
@@ -987,23 +1096,6 @@ st_index_t rgeo_geos_objbase_hash(VALUE factory, VALUE type_module, st_index_t h
987
1096
  return rb_memhash(&hash_struct, sizeof(RGeo_Objbase_Hash_Struct));
988
1097
  }
989
1098
 
990
-
991
- st_index_t rgeo_internal_memhash(const void* ptr, long len)
992
- {
993
- const char* bytes;
994
- st_index_t hval;
995
- long i;
996
-
997
- bytes = (const char*)ptr;
998
- hval = 0x811c9dc5;
999
- for (i=0; i<len; ++i) {
1000
- hval ^= (unsigned int)(*bytes++);
1001
- hval *= 0x01000193;
1002
- }
1003
- return hval;
1004
- }
1005
-
1006
-
1007
1099
  RGEO_END_C
1008
1100
 
1009
1101
  #endif