rgeo 2.4.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (97) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +6 -0
  3. data/README.md +21 -11
  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 +41 -29
  11. data/ext/geos_c_impl/factory.c +441 -351
  12. data/ext/geos_c_impl/factory.h +98 -55
  13. data/ext/geos_c_impl/geometry.c +563 -384
  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 +99 -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 +62 -65
  23. data/ext/geos_c_impl/point.h +4 -5
  24. data/ext/geos_c_impl/polygon.c +134 -132
  25. data/ext/geos_c_impl/polygon.h +11 -9
  26. data/ext/geos_c_impl/preface.h +10 -12
  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 +57 -102
  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 +299 -99
  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 +73 -83
  58. data/lib/rgeo/geographic/factory.rb +98 -125
  59. data/lib/rgeo/geographic/interface.rb +66 -163
  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 +24 -23
  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 +95 -165
  72. data/lib/rgeo/geos/ffi_feature_classes.rb +34 -10
  73. data/lib/rgeo/geos/ffi_feature_methods.rb +105 -126
  74. data/lib/rgeo/geos/interface.rb +20 -59
  75. data/lib/rgeo/geos/utils.rb +3 -3
  76. data/lib/rgeo/geos/zm_factory.rb +53 -95
  77. data/lib/rgeo/geos/zm_feature_methods.rb +30 -32
  78. data/lib/rgeo/geos.rb +8 -8
  79. data/lib/rgeo/impl_helper/basic_geometry_collection_methods.rb +9 -22
  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 +51 -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,23 @@ 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();
368
381
  if (has_3d) {
369
- GEOSWKBWriter_setOutputDimension_r(self_context, wkb_writer, 3);
382
+ GEOSWKBWriter_setOutputDimension(wkb_writer, 3);
370
383
  }
371
384
  self_data->marshal_wkb_writer = wkb_writer;
372
385
  }
@@ -374,10 +387,10 @@ static VALUE method_factory_write_for_marshal(VALUE self, VALUE obj)
374
387
  if (wkb_writer) {
375
388
  geom = rgeo_get_geos_geometry_safe(obj);
376
389
  if (geom) {
377
- str = (char*)GEOSWKBWriter_write_r(self_context, wkb_writer, geom, &size);
390
+ str = (char*)GEOSWKBWriter_write(wkb_writer, geom, &size);
378
391
  if (str) {
379
392
  result = rb_str_new(str, size);
380
- GEOSFree_r(self_context, str);
393
+ GEOSFree(str);
381
394
  }
382
395
  }
383
396
  }
@@ -388,10 +401,10 @@ static VALUE method_factory_write_for_marshal(VALUE self, VALUE obj)
388
401
  static VALUE psych_wkt_generator;
389
402
  #endif
390
403
 
391
- static VALUE method_factory_write_for_psych(VALUE self, VALUE obj)
404
+ static VALUE
405
+ method_factory_write_for_psych(VALUE self, VALUE obj)
392
406
  {
393
407
  RGeo_FactoryData* self_data;
394
- GEOSContextHandle_t self_context;
395
408
  GEOSWKTWriter* wkt_writer;
396
409
  const GEOSGeometry* geom;
397
410
  VALUE result;
@@ -399,23 +412,24 @@ static VALUE method_factory_write_for_psych(VALUE self, VALUE obj)
399
412
  char has_3d;
400
413
 
401
414
  self_data = RGEO_FACTORY_DATA_PTR(self);
402
- self_context = self_data->geos_context;
403
415
  has_3d = self_data->flags & RGEO_FACTORYFLAGS_SUPPORTS_Z_OR_M;
404
416
  #ifndef RGEO_GEOS_SUPPORTS_SETOUTPUTDIMENSION
405
417
  if (has_3d) {
406
418
  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);
419
+ psych_wkt_generator =
420
+ rb_funcall(rb_const_get_at(rgeo_geos_module, rb_intern("Utils")),
421
+ rb_intern("psych_wkt_generator"),
422
+ 0);
410
423
  }
411
424
  return rb_funcall(psych_wkt_generator, rb_intern("generate"), 1, obj);
412
425
  }
413
426
  #endif
414
427
  wkt_writer = self_data->psych_wkt_writer;
415
428
  if (!wkt_writer) {
416
- wkt_writer = GEOSWKTWriter_create_r(self_context);
429
+ wkt_writer = GEOSWKTWriter_create();
430
+ GEOSWKTWriter_setTrim(wkt_writer, 1);
417
431
  if (has_3d) {
418
- GEOSWKTWriter_setOutputDimension_r(self_context, wkt_writer, 3);
432
+ GEOSWKTWriter_setOutputDimension(wkt_writer, 3);
419
433
  }
420
434
  self_data->psych_wkt_writer = wkt_writer;
421
435
  }
@@ -423,24 +437,24 @@ static VALUE method_factory_write_for_psych(VALUE self, VALUE obj)
423
437
  if (wkt_writer) {
424
438
  geom = rgeo_get_geos_geometry_safe(obj);
425
439
  if (geom) {
426
- str = GEOSWKTWriter_write_r(self_context, wkt_writer, geom);
440
+ str = GEOSWKTWriter_write(wkt_writer, geom);
427
441
  if (str) {
428
442
  result = rb_str_new2(str);
429
- GEOSFree_r(self_context, str);
443
+ GEOSFree(str);
430
444
  }
431
445
  }
432
446
  }
433
447
  return result;
434
448
  }
435
449
 
436
-
437
- static VALUE cmethod_factory_geos_version(VALUE klass)
450
+ static VALUE
451
+ cmethod_factory_geos_version(VALUE klass)
438
452
  {
439
453
  return rb_str_new2(GEOS_VERSION);
440
454
  }
441
455
 
442
-
443
- static VALUE cmethod_factory_supports_unary_union(VALUE klass)
456
+ static VALUE
457
+ cmethod_factory_supports_unary_union(VALUE klass)
444
458
  {
445
459
  #ifdef RGEO_GEOS_SUPPORTS_UNARYUNION
446
460
  return Qtrue;
@@ -449,98 +463,93 @@ static VALUE cmethod_factory_supports_unary_union(VALUE klass)
449
463
  #endif
450
464
  }
451
465
 
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)
466
+ static VALUE
467
+ cmethod_factory_create(VALUE klass,
468
+ VALUE flags,
469
+ VALUE srid,
470
+ VALUE buffer_resolution,
471
+ VALUE wkt_generator,
472
+ VALUE wkb_generator,
473
+ VALUE coord_sys_obj)
454
474
  {
455
475
  VALUE result;
456
476
  RGeo_FactoryData* data;
457
- GEOSContextHandle_t context;
458
477
 
459
478
  result = Qnil;
460
479
  data = ALLOC(RGeo_FactoryData);
461
480
  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
- }
481
+ data->flags = RB_NUM2INT(flags);
482
+ data->srid = RB_NUM2INT(srid);
483
+ data->buffer_resolution = RB_NUM2INT(buffer_resolution);
484
+ data->wkt_reader = NULL;
485
+ data->wkb_reader = NULL;
486
+ data->wkt_writer = NULL;
487
+ data->wkb_writer = NULL;
488
+ data->psych_wkt_reader = NULL;
489
+ data->marshal_wkb_reader = NULL;
490
+ data->psych_wkt_writer = NULL;
491
+ data->marshal_wkb_writer = NULL;
492
+ data->wkrep_wkt_generator = wkt_generator;
493
+ data->wkrep_wkb_generator = wkb_generator;
494
+ data->wkrep_wkt_parser = Qnil;
495
+ data->wkrep_wkb_parser = Qnil;
496
+ data->coord_sys_obj = coord_sys_obj;
497
+ result = TypedData_Wrap_Struct(klass, &rgeo_factory_type, data);
487
498
  }
488
499
  return result;
489
500
  }
490
501
 
491
-
492
- static VALUE alloc_factory(VALUE klass)
502
+ static VALUE
503
+ alloc_factory(VALUE klass)
493
504
  {
494
- return cmethod_factory_create(klass, INT2NUM(0), INT2NUM(0), INT2NUM(0), Qnil, Qnil, Qnil, Qnil);
505
+ return cmethod_factory_create(
506
+ klass, INT2NUM(0), INT2NUM(0), INT2NUM(0), Qnil, Qnil, Qnil);
495
507
  }
496
508
 
497
-
498
- static VALUE method_factory_initialize_copy(VALUE self, VALUE orig)
509
+ static VALUE
510
+ method_factory_initialize_copy(VALUE self, VALUE orig)
499
511
  {
500
512
  RGeo_FactoryData* self_data;
501
513
  RGeo_FactoryData* orig_data;
502
- GEOSContextHandle_t context;
503
514
 
504
515
  // Clear out existing data
505
516
  self_data = RGEO_FACTORY_DATA_PTR(self);
506
- context = self_data->geos_context;
507
517
  if (self_data->wkt_reader) {
508
- GEOSWKTReader_destroy_r(context, self_data->wkt_reader);
518
+ GEOSWKTReader_destroy(self_data->wkt_reader);
509
519
  self_data->wkt_reader = NULL;
510
520
  }
511
521
  if (self_data->wkb_reader) {
512
- GEOSWKBReader_destroy_r(context, self_data->wkb_reader);
522
+ GEOSWKBReader_destroy(self_data->wkb_reader);
513
523
  self_data->wkb_reader = NULL;
514
524
  }
515
525
  if (self_data->wkt_writer) {
516
- GEOSWKTWriter_destroy_r(context, self_data->wkt_writer);
526
+ GEOSWKTWriter_destroy(self_data->wkt_writer);
517
527
  self_data->wkt_writer = NULL;
518
528
  }
519
529
  if (self_data->wkb_writer) {
520
- GEOSWKBWriter_destroy_r(context, self_data->wkb_writer);
530
+ GEOSWKBWriter_destroy(self_data->wkb_writer);
521
531
  self_data->wkb_writer = NULL;
522
532
  }
523
533
  if (self_data->psych_wkt_reader) {
524
- GEOSWKTReader_destroy_r(context, self_data->psych_wkt_reader);
534
+ GEOSWKTReader_destroy(self_data->psych_wkt_reader);
525
535
  self_data->psych_wkt_reader = NULL;
526
536
  }
527
537
  if (self_data->marshal_wkb_reader) {
528
- GEOSWKBReader_destroy_r(context, self_data->marshal_wkb_reader);
538
+ GEOSWKBReader_destroy(self_data->marshal_wkb_reader);
529
539
  self_data->marshal_wkb_reader = NULL;
530
540
  }
531
541
  if (self_data->psych_wkt_writer) {
532
- GEOSWKTWriter_destroy_r(context, self_data->psych_wkt_writer);
542
+ GEOSWKTWriter_destroy(self_data->psych_wkt_writer);
533
543
  self_data->psych_wkt_writer = NULL;
534
544
  }
535
545
  if (self_data->marshal_wkb_writer) {
536
- GEOSWKBWriter_destroy_r(context, self_data->marshal_wkb_writer);
546
+ GEOSWKBWriter_destroy(self_data->marshal_wkb_writer);
537
547
  self_data->marshal_wkb_writer = NULL;
538
548
  }
539
549
  self_data->wkrep_wkt_generator = Qnil;
540
550
  self_data->wkrep_wkb_generator = Qnil;
541
551
  self_data->wkrep_wkt_parser = Qnil;
542
552
  self_data->wkrep_wkb_parser = Qnil;
543
- self_data->proj4_obj = Qnil;
544
553
  self_data->coord_sys_obj = Qnil;
545
554
 
546
555
  // Copy new data from original object
@@ -553,14 +562,13 @@ static VALUE method_factory_initialize_copy(VALUE self, VALUE orig)
553
562
  self_data->wkrep_wkb_generator = orig_data->wkrep_wkb_generator;
554
563
  self_data->wkrep_wkt_parser = orig_data->wkrep_wkt_parser;
555
564
  self_data->wkrep_wkb_parser = orig_data->wkrep_wkb_parser;
556
- self_data->proj4_obj = orig_data->proj4_obj;
557
565
  self_data->coord_sys_obj = orig_data->coord_sys_obj;
558
566
  }
559
567
  return self;
560
568
  }
561
569
 
562
-
563
- static VALUE method_set_wkrep_parsers(VALUE self, VALUE wkt_parser, VALUE wkb_parser)
570
+ static VALUE
571
+ method_set_wkrep_parsers(VALUE self, VALUE wkt_parser, VALUE wkb_parser)
564
572
  {
565
573
  RGeo_FactoryData* self_data;
566
574
 
@@ -571,87 +579,126 @@ static VALUE method_set_wkrep_parsers(VALUE self, VALUE wkt_parser, VALUE wkb_pa
571
579
  return self;
572
580
  }
573
581
 
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)
582
+ static VALUE
583
+ method_get_coord_sys(VALUE self)
582
584
  {
583
585
  return RGEO_FACTORY_DATA_PTR(self)->coord_sys_obj;
584
586
  }
585
587
 
586
-
587
- static VALUE method_get_wkt_generator(VALUE self)
588
+ static VALUE
589
+ method_get_wkt_generator(VALUE self)
588
590
  {
589
591
  return RGEO_FACTORY_DATA_PTR(self)->wkrep_wkt_generator;
590
592
  }
591
593
 
592
-
593
- static VALUE method_get_wkb_generator(VALUE self)
594
+ static VALUE
595
+ method_get_wkb_generator(VALUE self)
594
596
  {
595
597
  return RGEO_FACTORY_DATA_PTR(self)->wkrep_wkb_generator;
596
598
  }
597
599
 
598
-
599
- static VALUE method_get_wkt_parser(VALUE self)
600
+ static VALUE
601
+ method_get_wkt_parser(VALUE self)
600
602
  {
601
603
  return RGEO_FACTORY_DATA_PTR(self)->wkrep_wkt_parser;
602
604
  }
603
605
 
604
-
605
- static VALUE method_get_wkb_parser(VALUE self)
606
+ static VALUE
607
+ method_get_wkb_parser(VALUE self)
606
608
  {
607
609
  return RGEO_FACTORY_DATA_PTR(self)->wkrep_wkb_parser;
608
610
  }
609
611
 
610
-
611
- static VALUE alloc_geometry(VALUE klass)
612
+ static VALUE
613
+ alloc_geometry(VALUE klass)
612
614
  {
613
615
  return rgeo_wrap_geos_geometry(Qnil, NULL, klass);
614
616
  }
615
617
 
616
-
617
618
  /**** INITIALIZATION FUNCTION ****/
618
619
 
619
-
620
- void rgeo_init_geos_factory()
620
+ void
621
+ rgeo_init_geos_factory()
621
622
  {
622
623
  VALUE geos_factory_class;
623
624
 
624
625
  #ifndef RGEO_GEOS_SUPPORTS_SETOUTPUTDIMENSION
625
- /* We favor rb_gc_register_address over rb_gc_register_mark_object because the value changes at runtime */
626
+ /* We favor rb_gc_register_address over rb_gc_register_mark_object because
627
+ * the value changes at runtime */
626
628
  psych_wkt_generator = Qnil;
627
629
  rb_gc_register_address(&psych_wkt_generator);
628
630
  marshal_wkb_generator = Qnil;
629
631
  rb_gc_register_address(&marshal_wkb_generator);
630
632
  #endif
631
633
 
632
- // Add C methods to the factory.
633
- geos_factory_class = rb_define_class_under(rgeo_geos_module, "CAPIFactory", rb_cObject);
634
+ geos_factory_class =
635
+ rb_define_class_under(rgeo_geos_module, "CAPIFactory", rb_cObject);
634
636
  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);
637
+ // Add C constants to the factory.
638
+ rb_define_const(geos_factory_class,
639
+ "FLAG_SUPPORTS_Z",
640
+ INT2FIX(RGEO_FACTORYFLAGS_SUPPORTS_Z));
641
+ rb_define_const(geos_factory_class,
642
+ "FLAG_SUPPORTS_M",
643
+ INT2FIX(RGEO_FACTORYFLAGS_SUPPORTS_M));
644
+ rb_define_const(geos_factory_class,
645
+ "FLAG_SUPPORTS_Z_OR_M",
646
+ INT2FIX(RGEO_FACTORYFLAGS_SUPPORTS_Z_OR_M));
647
+ rb_define_const(geos_factory_class,
648
+ "FLAG_PREPARE_HEURISTIC",
649
+ INT2FIX(RGEO_FACTORYFLAGS_PREPARE_HEURISTIC));
650
+ // Add C methods to the factory.
651
+ rb_define_method(
652
+ geos_factory_class, "initialize_copy", method_factory_initialize_copy, 1);
653
+ rb_define_method(
654
+ geos_factory_class, "_parse_wkt_impl", method_factory_parse_wkt, 1);
655
+ rb_define_method(
656
+ geos_factory_class, "_parse_wkb_impl", method_factory_parse_wkb, 1);
638
657
  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);
658
+ rb_define_method(geos_factory_class,
659
+ "_buffer_resolution",
660
+ method_factory_buffer_resolution,
661
+ 0);
640
662
  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);
663
+ rb_define_method(
664
+ geos_factory_class, "supports_z?", method_factory_supports_z_p, 0);
665
+ rb_define_method(
666
+ geos_factory_class, "supports_m?", method_factory_supports_m_p, 0);
667
+ rb_define_method(geos_factory_class,
668
+ "supports_z_or_m?",
669
+ method_factory_supports_z_or_m_p,
670
+ 0);
671
+ rb_define_method(geos_factory_class,
672
+ "prepare_heuristic?",
673
+ method_factory_prepare_heuristic_p,
674
+ 0);
675
+ rb_define_method(
676
+ geos_factory_class, "_set_wkrep_parsers", method_set_wkrep_parsers, 2);
643
677
  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);
678
+ rb_define_method(
679
+ geos_factory_class, "_wkt_generator", method_get_wkt_generator, 0);
680
+ rb_define_method(
681
+ geos_factory_class, "_wkb_generator", method_get_wkb_generator, 0);
646
682
  rb_define_method(geos_factory_class, "_wkt_parser", method_get_wkt_parser, 0);
647
683
  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);
684
+ rb_define_method(
685
+ geos_factory_class, "read_for_marshal", method_factory_read_for_marshal, 1);
686
+ rb_define_method(geos_factory_class,
687
+ "write_for_marshal",
688
+ method_factory_write_for_marshal,
689
+ 1);
690
+ rb_define_method(
691
+ geos_factory_class, "read_for_psych", method_factory_read_for_psych, 1);
692
+ rb_define_method(
693
+ geos_factory_class, "write_for_psych", method_factory_write_for_psych, 1);
694
+ rb_define_module_function(
695
+ geos_factory_class, "_create", cmethod_factory_create, 6);
696
+ rb_define_module_function(
697
+ geos_factory_class, "_geos_version", cmethod_factory_geos_version, 0);
698
+ rb_define_module_function(geos_factory_class,
699
+ "_supports_unary_union?",
700
+ cmethod_factory_supports_unary_union,
701
+ 0);
655
702
 
656
703
  // Define allocation methods for global class types
657
704
  rb_define_alloc_func(rgeo_geos_geometry_class, alloc_geometry);
@@ -666,15 +713,13 @@ void rgeo_init_geos_factory()
666
713
  rb_define_alloc_func(rgeo_geos_multi_polygon_class, alloc_geometry);
667
714
  }
668
715
 
669
-
670
716
  /**** OTHER PUBLIC FUNCTIONS ****/
671
717
 
672
-
673
- VALUE rgeo_wrap_geos_geometry(VALUE factory, GEOSGeometry* geom, VALUE klass)
718
+ VALUE
719
+ rgeo_wrap_geos_geometry(VALUE factory, GEOSGeometry* geom, VALUE klass)
674
720
  {
675
721
  VALUE result;
676
722
  RGeo_FactoryData* factory_data;
677
- GEOSContextHandle_t factory_context;
678
723
  VALUE klasses;
679
724
  VALUE inferred_klass;
680
725
  char is_collection;
@@ -683,14 +728,14 @@ VALUE rgeo_wrap_geos_geometry(VALUE factory, GEOSGeometry* geom, VALUE klass)
683
728
  result = Qnil;
684
729
  if (geom || !NIL_P(klass)) {
685
730
  factory_data = NIL_P(factory) ? NULL : RGEO_FACTORY_DATA_PTR(factory);
686
- factory_context = factory_data ? factory_data->geos_context : NULL;
687
731
 
688
732
  // We don't allow "empty" points, so replace such objects with
689
733
  // an empty collection.
690
734
  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);
735
+ if (GEOSGeomTypeId(geom) == GEOS_POINT &&
736
+ GEOSGetNumCoordinates(geom) == 0) {
737
+ GEOSGeom_destroy(geom);
738
+ geom = GEOSGeom_createCollection(GEOS_GEOMETRYCOLLECTION, NULL, 0);
694
739
  klass = rgeo_geos_geometry_collection_class;
695
740
  }
696
741
  }
@@ -699,38 +744,38 @@ VALUE rgeo_wrap_geos_geometry(VALUE factory, GEOSGeometry* geom, VALUE klass)
699
744
  if (TYPE(klass) != T_CLASS) {
700
745
  inferred_klass = Qnil;
701
746
  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;
747
+ switch (GEOSGeomTypeId(geom)) {
748
+ case GEOS_POINT:
749
+ inferred_klass = rgeo_geos_point_class;
750
+ break;
751
+ case GEOS_LINESTRING:
752
+ inferred_klass = rgeo_geos_line_string_class;
753
+ break;
754
+ case GEOS_LINEARRING:
755
+ inferred_klass = rgeo_geos_linear_ring_class;
756
+ break;
757
+ case GEOS_POLYGON:
758
+ inferred_klass = rgeo_geos_polygon_class;
759
+ break;
760
+ case GEOS_MULTIPOINT:
761
+ inferred_klass = rgeo_geos_multi_point_class;
762
+ is_collection = 1;
763
+ break;
764
+ case GEOS_MULTILINESTRING:
765
+ inferred_klass = rgeo_geos_multi_line_string_class;
766
+ is_collection = 1;
767
+ break;
768
+ case GEOS_MULTIPOLYGON:
769
+ inferred_klass = rgeo_geos_multi_polygon_class;
770
+ is_collection = 1;
771
+ break;
772
+ case GEOS_GEOMETRYCOLLECTION:
773
+ inferred_klass = rgeo_geos_geometry_collection_class;
774
+ is_collection = 1;
775
+ break;
776
+ default:
777
+ inferred_klass = rgeo_geos_geometry_class;
778
+ break;
734
779
  }
735
780
  if (TYPE(klass) == T_ARRAY && is_collection) {
736
781
  klasses = klass;
@@ -740,12 +785,14 @@ VALUE rgeo_wrap_geos_geometry(VALUE factory, GEOSGeometry* geom, VALUE klass)
740
785
  data = ALLOC(RGeo_GeometryData);
741
786
  if (data) {
742
787
  if (geom) {
743
- GEOSSetSRID_r(factory_context, geom, factory_data->srid);
788
+ GEOSSetSRID(geom, factory_data->srid);
744
789
  }
745
- data->geos_context = factory_context;
746
790
  data->geom = geom;
747
- data->prep = factory_data && ((factory_data->flags & RGEO_FACTORYFLAGS_PREPARE_HEURISTIC) != 0) ?
748
- (GEOSPreparedGeometry*)1 : NULL;
791
+ data->prep =
792
+ factory_data &&
793
+ ((factory_data->flags & RGEO_FACTORYFLAGS_PREPARE_HEURISTIC) != 0)
794
+ ? (GEOSPreparedGeometry*)1
795
+ : NULL;
749
796
  data->factory = factory;
750
797
  data->klasses = klasses;
751
798
  result = TypedData_Wrap_Struct(klass, &rgeo_geometry_type, data);
@@ -754,14 +801,17 @@ VALUE rgeo_wrap_geos_geometry(VALUE factory, GEOSGeometry* geom, VALUE klass)
754
801
  return result;
755
802
  }
756
803
 
757
- VALUE rgeo_wrap_geos_geometry_clone(VALUE factory, const GEOSGeometry* geom, VALUE klass)
804
+ VALUE
805
+ rgeo_wrap_geos_geometry_clone(VALUE factory,
806
+ const GEOSGeometry* geom,
807
+ VALUE klass)
758
808
  {
759
809
  VALUE result;
760
810
  GEOSGeometry* clone_geom;
761
811
 
762
812
  result = Qnil;
763
813
  if (geom) {
764
- clone_geom = GEOSGeom_clone_r(RGEO_FACTORY_DATA_PTR(factory)->geos_context, geom);
814
+ clone_geom = GEOSGeom_clone(geom);
765
815
  if (clone_geom) {
766
816
  result = rgeo_wrap_geos_geometry(factory, clone_geom, klass);
767
817
  }
@@ -769,26 +819,49 @@ VALUE rgeo_wrap_geos_geometry_clone(VALUE factory, const GEOSGeometry* geom, VAL
769
819
  return result;
770
820
  }
771
821
 
772
-
773
- const GEOSGeometry* rgeo_convert_to_geos_geometry(VALUE factory, VALUE obj, VALUE type)
822
+ const GEOSGeometry*
823
+ rgeo_convert_to_geos_geometry(VALUE factory, VALUE obj, VALUE type, int* state)
774
824
  {
775
825
  VALUE object;
776
826
 
777
- if (NIL_P(type) && RGEO_GEOMETRY_TYPEDDATA_P(obj) && RGEO_GEOMETRY_DATA_PTR(obj)->factory == factory) {
827
+ if (NIL_P(type) && RGEO_GEOMETRY_TYPEDDATA_P(obj) &&
828
+ RGEO_GEOMETRY_DATA_PTR(obj)->factory == factory) {
778
829
  object = obj;
830
+ } else {
831
+ object =
832
+ rb_funcall(rgeo_feature_module, rb_intern("cast"), 3, obj, factory, type);
779
833
  }
780
- else {
781
- object = rb_funcall(rgeo_feature_module, rb_intern("cast"), 3, obj, factory, type);
834
+ if (NIL_P(object)) {
835
+ rb_protect(
836
+ rb_exc_raise_value,
837
+ rb_exc_new_cstr(rb_eRGeoInvalidGeometry,
838
+ "Unable to cast the geometry to the GEOS Factory"),
839
+ state);
782
840
  }
783
- if (NIL_P(object))
841
+
842
+ if (*state) {
784
843
  return NULL;
844
+ }
845
+
846
+ if (!rgeo_is_geos_object(object)) {
847
+ rb_protect(rb_exc_raise_value,
848
+ rb_exc_new_cstr(rb_eRGeoError, "Not a GEOS Geometry object."),
849
+ state);
850
+ }
851
+
852
+ if (*state) {
853
+ return NULL;
854
+ }
785
855
 
786
- Check_TypedStruct(object, &rgeo_geometry_type);
787
856
  return RGEO_GEOMETRY_DATA_PTR(object)->geom;
788
857
  }
789
858
 
790
-
791
- GEOSGeometry* rgeo_convert_to_detached_geos_geometry(VALUE obj, VALUE factory, VALUE type, VALUE* klasses)
859
+ GEOSGeometry*
860
+ rgeo_convert_to_detached_geos_geometry(VALUE obj,
861
+ VALUE factory,
862
+ VALUE type,
863
+ VALUE* klasses,
864
+ int* state)
792
865
  {
793
866
  VALUE object;
794
867
  GEOSGeometry* geom;
@@ -798,51 +871,84 @@ GEOSGeometry* rgeo_convert_to_detached_geos_geometry(VALUE obj, VALUE factory, V
798
871
  if (klasses) {
799
872
  *klasses = Qnil;
800
873
  }
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);
874
+
875
+ object = rb_protect_funcall(rgeo_feature_module,
876
+ rb_intern("cast"),
877
+ state,
878
+ 5,
879
+ obj,
880
+ factory,
881
+ type,
882
+ ID2SYM(rb_intern("force_new")),
883
+ ID2SYM(rb_intern("keep_subtype")));
884
+
885
+ if (NIL_P(object)) {
886
+ rb_protect(
887
+ rb_exc_raise_value,
888
+ rb_exc_new_cstr(rb_eRGeoInvalidGeometry,
889
+ "Unable to cast the geometry to the GEOS Factory"),
890
+ state);
891
+ }
892
+ if (*state) {
893
+ return NULL;
894
+ }
895
+
896
+ if (!rgeo_is_geos_object(object)) {
897
+ rb_protect(rb_exc_raise_value,
898
+ rb_exc_new_cstr(rb_eRGeoError, "Not a GEOS Geometry object."),
899
+ state);
900
+ }
901
+ if (*state) {
902
+ return NULL;
903
+ }
904
+
905
+ object_data = RGEO_GEOMETRY_DATA_PTR(object);
906
+ geom = object_data->geom;
907
+ if (klasses) {
908
+ *klasses = object_data->klasses;
909
+ if (NIL_P(*klasses)) {
910
+ *klasses = CLASS_OF(object);
815
911
  }
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
912
  }
913
+ prep = object_data->prep;
914
+ if (prep && prep != (GEOSPreparedGeometry*)1 &&
915
+ prep != (GEOSPreparedGeometry*)2) {
916
+ GEOSPreparedGeom_destroy(prep);
917
+ }
918
+ object_data->geom = NULL;
919
+ object_data->prep = NULL;
920
+ object_data->factory = Qnil;
921
+ object_data->klasses = Qnil;
922
+
822
923
  return geom;
823
924
  }
824
925
 
825
-
826
- char rgeo_is_geos_object(VALUE obj)
926
+ char
927
+ rgeo_is_geos_object(VALUE obj)
827
928
  {
828
929
  return RGEO_GEOMETRY_TYPEDDATA_P(obj) ? 1 : 0;
829
930
  }
830
931
 
831
- void rgeo_check_geos_object(VALUE obj)
932
+ void
933
+ rgeo_check_geos_object(VALUE obj)
832
934
  {
833
935
  if (!rgeo_is_geos_object(obj)) {
834
- rb_raise(rgeo_error, "Not a GEOS Geometry object.");
936
+ rb_raise(rb_eRGeoError, "Not a GEOS Geometry object.");
835
937
  }
836
938
  }
837
939
 
838
-
839
- const GEOSGeometry* rgeo_get_geos_geometry_safe(VALUE obj)
940
+ const GEOSGeometry*
941
+ rgeo_get_geos_geometry_safe(VALUE obj)
840
942
  {
841
- return RGEO_GEOMETRY_TYPEDDATA_P(obj) ? (const GEOSGeometry*)(RGEO_GEOMETRY_DATA_PTR(obj)->geom) : NULL;
943
+ return RGEO_GEOMETRY_TYPEDDATA_P(obj)
944
+ ? (const GEOSGeometry*)(RGEO_GEOMETRY_DATA_PTR(obj)->geom)
945
+ : NULL;
842
946
  }
843
947
 
844
-
845
- VALUE rgeo_geos_coordseqs_eql(GEOSContextHandle_t context, const GEOSGeometry* geom1, const GEOSGeometry* geom2, char check_z)
948
+ VALUE
949
+ rgeo_geos_coordseqs_eql(const GEOSGeometry* geom1,
950
+ const GEOSGeometry* geom2,
951
+ char check_z)
846
952
  {
847
953
  VALUE result;
848
954
  const GEOSCoordSequence* cs1;
@@ -854,27 +960,30 @@ VALUE rgeo_geos_coordseqs_eql(GEOSContextHandle_t context, const GEOSGeometry* g
854
960
 
855
961
  result = Qnil;
856
962
  if (geom1 && geom2) {
857
- cs1 = GEOSGeom_getCoordSeq_r(context, geom1);
858
- cs2 = GEOSGeom_getCoordSeq_r(context, geom2);
963
+ cs1 = GEOSGeom_getCoordSeq(geom1);
964
+ cs2 = GEOSGeom_getCoordSeq(geom2);
859
965
  if (cs1 && cs2) {
860
966
  len1 = 0;
861
967
  len2 = 0;
862
- if (GEOSCoordSeq_getSize_r(context, cs1, &len1) && GEOSCoordSeq_getSize_r(context, cs2, &len2)) {
968
+ if (GEOSCoordSeq_getSize(cs1, &len1) &&
969
+ GEOSCoordSeq_getSize(cs2, &len2)) {
863
970
  if (len1 == len2) {
864
971
  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)) {
972
+ for (i = 0; i < len1; ++i) {
973
+ if (GEOSCoordSeq_getX(cs1, i, &val1) &&
974
+ GEOSCoordSeq_getX(cs2, i, &val2)) {
867
975
  if (val1 == val2) {
868
- if (GEOSCoordSeq_getY_r(context, cs1, i, &val1) && GEOSCoordSeq_getY_r(context, cs2, i, &val2)) {
976
+ if (GEOSCoordSeq_getY(cs1, i, &val1) &&
977
+ GEOSCoordSeq_getY(cs2, i, &val2)) {
869
978
  if (val1 == val2) {
870
979
  if (check_z) {
871
980
  val1 = 0;
872
- if (!GEOSCoordSeq_getZ_r(context, cs1, i, &val1)) {
981
+ if (!GEOSCoordSeq_getZ(cs1, i, &val1)) {
873
982
  result = Qnil;
874
983
  break;
875
984
  }
876
985
  val2 = 0;
877
- if (!GEOSCoordSeq_getZ_r(context, cs2, i, &val2)) {
986
+ if (!GEOSCoordSeq_getZ(cs2, i, &val2)) {
878
987
  result = Qnil;
879
988
  break;
880
989
  }
@@ -883,29 +992,24 @@ VALUE rgeo_geos_coordseqs_eql(GEOSContextHandle_t context, const GEOSGeometry* g
883
992
  break;
884
993
  }
885
994
  }
886
- }
887
- else { // Y coords are different
995
+ } else { // Y coords are different
888
996
  result = Qfalse;
889
997
  break;
890
998
  }
891
- }
892
- else { // Failed to get Y coords
999
+ } else { // Failed to get Y coords
893
1000
  result = Qnil;
894
1001
  break;
895
1002
  }
896
- }
897
- else { // X coords are different
1003
+ } else { // X coords are different
898
1004
  result = Qfalse;
899
1005
  break;
900
1006
  }
901
- }
902
- else { // Failed to get X coords
1007
+ } else { // Failed to get X coords
903
1008
  result = Qnil;
904
1009
  break;
905
1010
  }
906
- } // Iteration over coords
907
- }
908
- else { // Lengths are different
1011
+ } // Iteration over coords
1012
+ } else { // Lengths are different
909
1013
  result = Qfalse;
910
1014
  }
911
1015
  }
@@ -914,8 +1018,8 @@ VALUE rgeo_geos_coordseqs_eql(GEOSContextHandle_t context, const GEOSGeometry* g
914
1018
  return result;
915
1019
  }
916
1020
 
917
-
918
- VALUE rgeo_geos_klasses_and_factories_eql(VALUE obj1, VALUE obj2)
1021
+ VALUE
1022
+ rgeo_geos_klasses_and_factories_eql(VALUE obj1, VALUE obj2)
919
1023
  {
920
1024
  VALUE result;
921
1025
  VALUE factory;
@@ -923,24 +1027,25 @@ VALUE rgeo_geos_klasses_and_factories_eql(VALUE obj1, VALUE obj2)
923
1027
  result = Qnil;
924
1028
  if (rb_obj_class(obj1) != rb_obj_class(obj2)) {
925
1029
  result = Qfalse;
926
- }
927
- else {
1030
+ } else {
928
1031
  factory = RGEO_GEOMETRY_DATA_PTR(obj1)->factory;
929
1032
  /* 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);
1033
+ result = rb_funcall(
1034
+ factory, rb_intern("eql?"), 1, RGEO_GEOMETRY_DATA_PTR(obj2)->factory);
931
1035
  }
932
1036
  return result;
933
1037
  }
934
1038
 
935
-
936
- typedef struct {
1039
+ typedef struct
1040
+ {
937
1041
  st_index_t seed_hash;
938
1042
  double x;
939
1043
  double y;
940
1044
  double z;
941
1045
  } RGeo_Coordseq_Hash_Struct;
942
1046
 
943
- st_index_t rgeo_geos_coordseq_hash(GEOSContextHandle_t context, const GEOSGeometry* geom, st_index_t hash)
1047
+ st_index_t
1048
+ rgeo_geos_coordseq_hash(const GEOSGeometry* geom, st_index_t hash)
944
1049
  {
945
1050
  const GEOSCoordSequence* cs;
946
1051
  unsigned int len;
@@ -948,17 +1053,18 @@ st_index_t rgeo_geos_coordseq_hash(GEOSContextHandle_t context, const GEOSGeomet
948
1053
  RGeo_Coordseq_Hash_Struct hash_struct;
949
1054
 
950
1055
  if (geom) {
951
- cs = GEOSGeom_getCoordSeq_r(context, geom);
1056
+ cs = GEOSGeom_getCoordSeq(geom);
952
1057
  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)) {
1058
+ if (GEOSCoordSeq_getSize(cs, &len)) {
1059
+ for (i = 0; i < len; ++i) {
1060
+ if (GEOSCoordSeq_getX(cs, i, &hash_struct.x)) {
1061
+ if (GEOSCoordSeq_getY(cs, i, &hash_struct.y)) {
1062
+ if (!GEOSCoordSeq_getZ(cs, i, &hash_struct.z)) {
958
1063
  hash_struct.z = 0;
959
1064
  }
960
1065
  hash_struct.seed_hash = hash;
961
- hash = rb_memhash(&hash_struct, sizeof(RGeo_Coordseq_Hash_Struct));
1066
+ hash =
1067
+ rb_memhash(&hash_struct, sizeof(RGeo_Coordseq_Hash_Struct));
962
1068
  }
963
1069
  }
964
1070
  }
@@ -968,14 +1074,15 @@ st_index_t rgeo_geos_coordseq_hash(GEOSContextHandle_t context, const GEOSGeomet
968
1074
  return hash;
969
1075
  }
970
1076
 
971
-
972
- typedef struct {
1077
+ typedef struct
1078
+ {
973
1079
  st_index_t seed_hash;
974
1080
  st_index_t h1;
975
1081
  st_index_t h2;
976
1082
  } RGeo_Objbase_Hash_Struct;
977
1083
 
978
- st_index_t rgeo_geos_objbase_hash(VALUE factory, VALUE type_module, st_index_t hash)
1084
+ st_index_t
1085
+ rgeo_geos_objbase_hash(VALUE factory, VALUE type_module, st_index_t hash)
979
1086
  {
980
1087
  ID hash_method;
981
1088
  RGeo_Objbase_Hash_Struct hash_struct;
@@ -987,23 +1094,6 @@ st_index_t rgeo_geos_objbase_hash(VALUE factory, VALUE type_module, st_index_t h
987
1094
  return rb_memhash(&hash_struct, sizeof(RGeo_Objbase_Hash_Struct));
988
1095
  }
989
1096
 
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
1097
  RGEO_END_C
1008
1098
 
1009
1099
  #endif