rgeo 2.3.0 → 3.0.0.pre.rc.1

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 (72) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +6 -0
  3. data/README.md +1 -0
  4. data/ext/geos_c_impl/analysis.c +8 -6
  5. data/ext/geos_c_impl/analysis.h +1 -3
  6. data/ext/geos_c_impl/errors.c +10 -8
  7. data/ext/geos_c_impl/errors.h +7 -3
  8. data/ext/geos_c_impl/extconf.rb +3 -0
  9. data/ext/geos_c_impl/factory.c +251 -182
  10. data/ext/geos_c_impl/factory.h +43 -62
  11. data/ext/geos_c_impl/geometry.c +56 -24
  12. data/ext/geos_c_impl/geometry.h +8 -3
  13. data/ext/geos_c_impl/geometry_collection.c +41 -148
  14. data/ext/geos_c_impl/geometry_collection.h +1 -14
  15. data/ext/geos_c_impl/globals.c +91 -0
  16. data/ext/geos_c_impl/globals.h +45 -0
  17. data/ext/geos_c_impl/line_string.c +28 -29
  18. data/ext/geos_c_impl/line_string.h +1 -3
  19. data/ext/geos_c_impl/main.c +10 -9
  20. data/ext/geos_c_impl/point.c +9 -8
  21. data/ext/geos_c_impl/point.h +1 -3
  22. data/ext/geos_c_impl/polygon.c +15 -51
  23. data/ext/geos_c_impl/polygon.h +1 -3
  24. data/ext/geos_c_impl/preface.h +8 -0
  25. data/lib/rgeo/cartesian/analysis.rb +2 -2
  26. data/lib/rgeo/cartesian/calculations.rb +54 -17
  27. data/lib/rgeo/cartesian/factory.rb +0 -7
  28. data/lib/rgeo/cartesian/feature_classes.rb +66 -46
  29. data/lib/rgeo/cartesian/feature_methods.rb +56 -20
  30. data/lib/rgeo/cartesian/interface.rb +0 -6
  31. data/lib/rgeo/cartesian/planar_graph.rb +379 -0
  32. data/lib/rgeo/cartesian/sweepline_intersector.rb +149 -0
  33. data/lib/rgeo/cartesian/valid_op.rb +71 -0
  34. data/lib/rgeo/cartesian.rb +3 -0
  35. data/lib/rgeo/coord_sys/cs/wkt_parser.rb +6 -6
  36. data/lib/rgeo/error.rb +15 -0
  37. data/lib/rgeo/feature/curve.rb +12 -2
  38. data/lib/rgeo/feature/geometry.rb +38 -28
  39. data/lib/rgeo/feature/geometry_collection.rb +13 -5
  40. data/lib/rgeo/feature/line_string.rb +3 -3
  41. data/lib/rgeo/feature/multi_curve.rb +6 -1
  42. data/lib/rgeo/feature/multi_surface.rb +3 -3
  43. data/lib/rgeo/feature/point.rb +4 -4
  44. data/lib/rgeo/feature/surface.rb +3 -3
  45. data/lib/rgeo/geographic/factory.rb +0 -7
  46. data/lib/rgeo/geographic/interface.rb +4 -18
  47. data/lib/rgeo/geographic/proj4_projector.rb +0 -2
  48. data/lib/rgeo/geographic/projected_feature_classes.rb +21 -9
  49. data/lib/rgeo/geographic/projected_feature_methods.rb +63 -30
  50. data/lib/rgeo/geographic/simple_mercator_projector.rb +0 -2
  51. data/lib/rgeo/geographic/spherical_feature_classes.rb +29 -9
  52. data/lib/rgeo/geographic/spherical_feature_methods.rb +68 -2
  53. data/lib/rgeo/geos/capi_factory.rb +21 -31
  54. data/lib/rgeo/geos/capi_feature_classes.rb +64 -11
  55. data/lib/rgeo/geos/ffi_factory.rb +0 -28
  56. data/lib/rgeo/geos/ffi_feature_classes.rb +34 -10
  57. data/lib/rgeo/geos/ffi_feature_methods.rb +53 -10
  58. data/lib/rgeo/geos/interface.rb +18 -10
  59. data/lib/rgeo/geos/zm_factory.rb +0 -12
  60. data/lib/rgeo/geos/zm_feature_methods.rb +30 -5
  61. data/lib/rgeo/impl_helper/basic_geometry_collection_methods.rb +18 -8
  62. data/lib/rgeo/impl_helper/basic_geometry_methods.rb +1 -1
  63. data/lib/rgeo/impl_helper/basic_line_string_methods.rb +37 -26
  64. data/lib/rgeo/impl_helper/basic_point_methods.rb +13 -3
  65. data/lib/rgeo/impl_helper/basic_polygon_methods.rb +8 -3
  66. data/lib/rgeo/impl_helper/valid_op.rb +354 -0
  67. data/lib/rgeo/impl_helper/validity_check.rb +138 -0
  68. data/lib/rgeo/impl_helper.rb +1 -0
  69. data/lib/rgeo/version.rb +1 -1
  70. data/lib/rgeo/wkrep/wkb_generator.rb +1 -1
  71. data/lib/rgeo/wkrep/wkt_generator.rb +6 -6
  72. metadata +30 -7
@@ -11,6 +11,8 @@
11
11
  #include <ruby.h>
12
12
  #include <geos_c.h>
13
13
 
14
+ #include "globals.h"
15
+
14
16
  #include "factory.h"
15
17
  #include "geometry.h"
16
18
  #include "point.h"
@@ -29,7 +31,7 @@ static VALUE method_line_string_geometry_type(VALUE self)
29
31
  result = Qnil;
30
32
  self_data = RGEO_GEOMETRY_DATA_PTR(self);
31
33
  if (self_data->geom) {
32
- result = RGEO_FACTORY_DATA_PTR(self_data->factory)->globals->feature_line_string;
34
+ result = rgeo_feature_line_string_module;
33
35
  }
34
36
  return result;
35
37
  }
@@ -43,7 +45,7 @@ static VALUE method_linear_ring_geometry_type(VALUE self)
43
45
  result = Qnil;
44
46
  self_data = RGEO_GEOMETRY_DATA_PTR(self);
45
47
  if (self_data->geom) {
46
- result = RGEO_FACTORY_DATA_PTR(self_data->factory)->globals->feature_linear_ring;
48
+ result = rgeo_feature_linear_ring_module;
47
49
  }
48
50
  return result;
49
51
  }
@@ -57,7 +59,7 @@ static VALUE method_line_geometry_type(VALUE self)
57
59
  result = Qnil;
58
60
  self_data = RGEO_GEOMETRY_DATA_PTR(self);
59
61
  if (self_data->geom) {
60
- result = RGEO_FACTORY_DATA_PTR(self_data->factory)->globals->feature_line;
62
+ result = rgeo_feature_line_module;
61
63
  }
62
64
  return result;
63
65
  }
@@ -264,7 +266,7 @@ static VALUE method_line_string_project_point(VALUE self, VALUE point)
264
266
  factory_data = RGEO_FACTORY_DATA_PTR(factory);
265
267
 
266
268
  if(self_geom && point) {
267
- geos_point = rgeo_convert_to_geos_geometry(factory, point, factory_data->globals->geos_point);
269
+ geos_point = rgeo_convert_to_geos_geometry(factory, point, rgeo_geos_point_class);
268
270
  location = GEOSProject_r(self_data->geos_context, self_geom, geos_point);
269
271
  result = DBL2NUM(location);
270
272
  }
@@ -290,7 +292,7 @@ static VALUE method_line_string_interpolate_point(VALUE self, VALUE loc_num)
290
292
 
291
293
  if(self_geom) {
292
294
  geos_point = GEOSInterpolate_r(self_data->geos_context, self_geom, location);
293
- result = rgeo_wrap_geos_geometry(factory, geos_point, factory_data->globals->geos_point);
295
+ result = rgeo_wrap_geos_geometry(factory, geos_point, rgeo_geos_point_class);
294
296
  }
295
297
 
296
298
  return result;
@@ -343,7 +345,7 @@ static VALUE method_line_string_eql(VALUE self, VALUE rhs)
343
345
  result = rgeo_geos_klasses_and_factories_eql(self, rhs);
344
346
  if (RTEST(result)) {
345
347
  self_data = RGEO_GEOMETRY_DATA_PTR(self);
346
- result = rgeo_geos_coordseqs_eql(self_data->geos_context, self_data->geom, RGEO_GEOMETRY_DATA_PTR(rhs)->geom, RGEO_FACTORY_DATA_PTR(self_data->factory)->flags & RGEO_FACTORYFLAGS_SUPPORTS_Z_OR_M);
348
+ result = rgeo_geos_geometries_strict_eql(self_data->geos_context, self_data->geom, RGEO_GEOMETRY_DATA_PTR(rhs)->geom);
347
349
  }
348
350
  return result;
349
351
  }
@@ -358,8 +360,7 @@ static VALUE method_line_string_hash(VALUE self)
358
360
  self_data = RGEO_GEOMETRY_DATA_PTR(self);
359
361
  factory = self_data->factory;
360
362
  hash = rb_hash_start(0);
361
- hash = rgeo_geos_objbase_hash(factory,
362
- RGEO_FACTORY_DATA_PTR(factory)->globals->feature_line_string, hash);
363
+ hash = rgeo_geos_objbase_hash(factory, rgeo_feature_line_string_module, hash);
363
364
  hash = rgeo_geos_coordseq_hash(self_data->geos_context, self_data->geom, hash);
364
365
  return LONG2FIX(rb_hash_end(hash));
365
366
  }
@@ -374,8 +375,7 @@ static VALUE method_linear_ring_hash(VALUE self)
374
375
  self_data = RGEO_GEOMETRY_DATA_PTR(self);
375
376
  factory = self_data->factory;
376
377
  hash = rb_hash_start(0);
377
- hash = rgeo_geos_objbase_hash(factory,
378
- RGEO_FACTORY_DATA_PTR(factory)->globals->feature_linear_ring, hash);
378
+ hash = rgeo_geos_objbase_hash(factory, rgeo_feature_linear_ring_module, hash);
379
379
  hash = rgeo_geos_coordseq_hash(self_data->geos_context, self_data->geom, hash);
380
380
  return LONG2FIX(rb_hash_end(hash));
381
381
  }
@@ -390,8 +390,7 @@ static VALUE method_line_hash(VALUE self)
390
390
  self_data = RGEO_GEOMETRY_DATA_PTR(self);
391
391
  factory = self_data->factory;
392
392
  hash = rb_hash_start(0);
393
- hash = rgeo_geos_objbase_hash(factory,
394
- RGEO_FACTORY_DATA_PTR(factory)->globals->feature_line, hash);
393
+ hash = rgeo_geos_objbase_hash(factory, rgeo_feature_line_module, hash);
395
394
  hash = rgeo_geos_coordseq_hash(self_data->geos_context, self_data->geom, hash);
396
395
  return LONG2FIX(rb_hash_end(hash));
397
396
  }
@@ -415,7 +414,7 @@ static GEOSCoordSequence* coord_seq_from_array(VALUE factory, VALUE array, char
415
414
 
416
415
  Check_Type(array, T_ARRAY);
417
416
  factory_data = RGEO_FACTORY_DATA_PTR(factory);
418
- point_type = factory_data->globals->feature_point;
417
+ point_type = rgeo_feature_point_module;
419
418
  len = (unsigned int)RARRAY_LEN(array);
420
419
  has_z = (char)(RGEO_FACTORY_DATA_PTR(factory)->flags & RGEO_FACTORYFLAGS_SUPPORTS_Z_OR_M);
421
420
  dims = has_z ? 3 : 2;
@@ -491,7 +490,7 @@ static VALUE cmethod_create_line_string(VALUE module, VALUE factory, VALUE array
491
490
  factory_data = RGEO_FACTORY_DATA_PTR(factory);
492
491
  geom = GEOSGeom_createLineString_r(factory_data->geos_context, coord_seq);
493
492
  if (geom) {
494
- result = rgeo_wrap_geos_geometry(factory, geom, factory_data->globals->geos_line_string);
493
+ result = rgeo_wrap_geos_geometry(factory, geom, rgeo_geos_line_string_class);
495
494
  }
496
495
  }
497
496
  return result;
@@ -511,7 +510,7 @@ static VALUE cmethod_create_linear_ring(VALUE module, VALUE factory, VALUE array
511
510
  factory_data = RGEO_FACTORY_DATA_PTR(factory);
512
511
  geom = GEOSGeom_createLinearRing_r(factory_data->geos_context, coord_seq);
513
512
  if (geom) {
514
- result = rgeo_wrap_geos_geometry(factory, geom, factory_data->globals->geos_linear_ring);
513
+ result = rgeo_wrap_geos_geometry(factory, geom, rgeo_geos_linear_ring_class);
515
514
  }
516
515
  }
517
516
  return result;
@@ -557,7 +556,7 @@ static VALUE cmethod_create_line(VALUE module, VALUE factory, VALUE start, VALUE
557
556
  result = Qnil;
558
557
  factory_data = RGEO_FACTORY_DATA_PTR(factory);
559
558
  has_z = (char)(factory_data->flags & RGEO_FACTORYFLAGS_SUPPORTS_Z_OR_M);
560
- point_type = factory_data->globals->feature_point;
559
+ point_type = rgeo_feature_point_module;
561
560
  context = factory_data->geos_context;
562
561
 
563
562
  start_geom = rgeo_convert_to_geos_geometry(factory, start, point_type);
@@ -570,7 +569,7 @@ static VALUE cmethod_create_line(VALUE module, VALUE factory, VALUE start, VALUE
570
569
  populate_geom_into_coord_seq(context, end_geom, coord_seq, 1, has_z);
571
570
  geom = GEOSGeom_createLineString_r(context, coord_seq);
572
571
  if (geom) {
573
- result = rgeo_wrap_geos_geometry(factory, geom, factory_data->globals->geos_line);
572
+ result = rgeo_wrap_geos_geometry(factory, geom, rgeo_geos_line_class);
574
573
  }
575
574
  }
576
575
  }
@@ -631,26 +630,26 @@ static VALUE cmethod_linear_ring_copy_from(VALUE klass, VALUE factory, VALUE ori
631
630
  }
632
631
 
633
632
 
634
- void rgeo_init_geos_line_string(RGeo_Globals* globals)
633
+ void rgeo_init_geos_line_string()
635
634
  {
636
635
  VALUE geos_line_string_methods;
637
636
  VALUE geos_linear_ring_methods;
638
637
  VALUE geos_line_methods;
639
638
 
640
639
  // Class methods for CAPILineStringImpl
641
- rb_define_module_function(globals->geos_line_string, "create", cmethod_create_line_string, 2);
642
- rb_define_module_function(globals->geos_line_string, "_copy_from", cmethod_line_string_copy_from, 2);
640
+ rb_define_module_function(rgeo_geos_line_string_class, "create", cmethod_create_line_string, 2);
641
+ rb_define_module_function(rgeo_geos_line_string_class, "_copy_from", cmethod_line_string_copy_from, 2);
643
642
 
644
643
  // Class methods for CAPILinearRingImpl
645
- rb_define_module_function(globals->geos_linear_ring, "create", cmethod_create_linear_ring, 2);
646
- rb_define_module_function(globals->geos_linear_ring, "_copy_from", cmethod_linear_ring_copy_from, 2);
644
+ rb_define_module_function(rgeo_geos_linear_ring_class, "create", cmethod_create_linear_ring, 2);
645
+ rb_define_module_function(rgeo_geos_linear_ring_class, "_copy_from", cmethod_linear_ring_copy_from, 2);
647
646
 
648
647
  // Class methods for CAPILineImpl
649
- rb_define_module_function(globals->geos_line, "create", cmethod_create_line, 3);
650
- rb_define_module_function(globals->geos_line, "_copy_from", cmethod_line_copy_from, 2);
648
+ rb_define_module_function(rgeo_geos_line_class, "create", cmethod_create_line, 3);
649
+ rb_define_module_function(rgeo_geos_line_class, "_copy_from", cmethod_line_copy_from, 2);
651
650
 
652
651
  // CAPILineStringMethods module
653
- geos_line_string_methods = rb_define_module_under(globals->geos_module, "CAPILineStringMethods");
652
+ geos_line_string_methods = rb_define_module_under(rgeo_geos_module, "CAPILineStringMethods");
654
653
  rb_define_method(geos_line_string_methods, "rep_equals?", method_line_string_eql, 1);
655
654
  rb_define_method(geos_line_string_methods, "eql?", method_line_string_eql, 1);
656
655
  rb_define_method(geos_line_string_methods, "hash", method_line_string_hash, 0);
@@ -663,17 +662,17 @@ void rgeo_init_geos_line_string(RGeo_Globals* globals)
663
662
  rb_define_method(geos_line_string_methods, "end_point", method_line_string_end_point, 0);
664
663
  rb_define_method(geos_line_string_methods, "project_point", method_line_string_project_point, 1);
665
664
  rb_define_method(geos_line_string_methods, "interpolate_point", method_line_string_interpolate_point, 1);
666
- rb_define_method(geos_line_string_methods, "is_closed?", method_line_string_is_closed, 0);
667
- rb_define_method(geos_line_string_methods, "is_ring?", method_line_string_is_ring, 0);
665
+ rb_define_method(geos_line_string_methods, "closed?", method_line_string_is_closed, 0);
666
+ rb_define_method(geos_line_string_methods, "ring?", method_line_string_is_ring, 0);
668
667
  rb_define_method(geos_line_string_methods, "coordinates", method_line_string_coordinates, 0);
669
668
 
670
669
  // CAPILinearRingMethods module
671
- geos_linear_ring_methods = rb_define_module_under(globals->geos_module, "CAPILinearRingMethods");
670
+ geos_linear_ring_methods = rb_define_module_under(rgeo_geos_module, "CAPILinearRingMethods");
672
671
  rb_define_method(geos_linear_ring_methods, "geometry_type", method_linear_ring_geometry_type, 0);
673
672
  rb_define_method(geos_linear_ring_methods, "hash", method_linear_ring_hash, 0);
674
673
 
675
674
  // CAPILineMethods module
676
- geos_line_methods = rb_define_module_under(globals->geos_module, "CAPILineMethods");
675
+ geos_line_methods = rb_define_module_under(rgeo_geos_module, "CAPILineMethods");
677
676
  rb_define_method(geos_line_methods, "geometry_type", method_line_geometry_type, 0);
678
677
  rb_define_method(geos_line_methods, "hash", method_line_hash, 0);
679
678
  }
@@ -9,8 +9,6 @@
9
9
  #include <ruby.h>
10
10
  #include <geos_c.h>
11
11
 
12
- #include "factory.h"
13
-
14
12
  RGEO_BEGIN_C
15
13
 
16
14
 
@@ -18,7 +16,7 @@ RGEO_BEGIN_C
18
16
  Initializes the line string module. This should be called after
19
17
  the geometry module is initialized.
20
18
  */
21
- void rgeo_init_geos_line_string(RGeo_Globals* globals);
19
+ void rgeo_init_geos_line_string();
22
20
 
23
21
  /*
24
22
  Determines whether the given GEOS line string is closed.
@@ -9,6 +9,8 @@
9
9
  #include <ruby.h>
10
10
  #include <geos_c.h>
11
11
 
12
+ #include "globals.h"
13
+
12
14
  #include "errors.h"
13
15
 
14
16
  #include "factory.h"
@@ -26,15 +28,14 @@ RGEO_BEGIN_C
26
28
  void Init_geos_c_impl()
27
29
  {
28
30
  #ifdef RGEO_GEOS_SUPPORTED
29
- RGeo_Globals* globals;
30
-
31
- globals = rgeo_init_geos_factory();
32
- rgeo_init_geos_geometry(globals);
33
- rgeo_init_geos_point(globals);
34
- rgeo_init_geos_line_string(globals);
35
- rgeo_init_geos_polygon(globals);
36
- rgeo_init_geos_geometry_collection(globals);
37
- rgeo_init_geos_analysis(globals);
31
+ rgeo_init_geos_globals();
32
+ rgeo_init_geos_factory();
33
+ rgeo_init_geos_geometry();
34
+ rgeo_init_geos_point();
35
+ rgeo_init_geos_line_string();
36
+ rgeo_init_geos_polygon();
37
+ rgeo_init_geos_geometry_collection();
38
+ rgeo_init_geos_analysis();
38
39
  rgeo_init_geos_errors();
39
40
  #endif
40
41
  }
@@ -9,6 +9,8 @@
9
9
  #include <ruby.h>
10
10
  #include <geos_c.h>
11
11
 
12
+ #include "globals.h"
13
+
12
14
  #include "factory.h"
13
15
  #include "geometry.h"
14
16
  #include "point.h"
@@ -26,7 +28,7 @@ static VALUE method_point_geometry_type(VALUE self)
26
28
  result = Qnil;
27
29
  self_data = RGEO_GEOMETRY_DATA_PTR(self);
28
30
  if (self_data->geom) {
29
- result = RGEO_FACTORY_DATA_PTR(self_data->factory)->globals->feature_point;
31
+ result = rgeo_feature_point_module;
30
32
  }
31
33
  return result;
32
34
  }
@@ -153,7 +155,7 @@ static VALUE method_point_eql(VALUE self, VALUE rhs)
153
155
  result = rgeo_geos_klasses_and_factories_eql(self, rhs);
154
156
  if (RTEST(result)) {
155
157
  self_data = RGEO_GEOMETRY_DATA_PTR(self);
156
- result = rgeo_geos_coordseqs_eql(self_data->geos_context, self_data->geom, RGEO_GEOMETRY_DATA_PTR(rhs)->geom, RGEO_FACTORY_DATA_PTR(self_data->factory)->flags & RGEO_FACTORYFLAGS_SUPPORTS_Z_OR_M);
158
+ result = rgeo_geos_geometries_strict_eql(self_data->geos_context, self_data->geom, RGEO_GEOMETRY_DATA_PTR(rhs)->geom);
157
159
  }
158
160
  return result;
159
161
  }
@@ -168,8 +170,7 @@ static VALUE method_point_hash(VALUE self)
168
170
  self_data = RGEO_GEOMETRY_DATA_PTR(self);
169
171
  factory = self_data->factory;
170
172
  hash = rb_hash_start(0);
171
- hash = rgeo_geos_objbase_hash(factory,
172
- RGEO_FACTORY_DATA_PTR(factory)->globals->feature_point, hash);
173
+ hash = rgeo_geos_objbase_hash(factory, rgeo_feature_point_module, hash);
173
174
  hash = rgeo_geos_coordseq_hash(self_data->geos_context, self_data->geom, hash);
174
175
  return LONG2FIX(rb_hash_end(hash));
175
176
  }
@@ -182,15 +183,15 @@ static VALUE cmethod_create(VALUE module, VALUE factory, VALUE x, VALUE y, VALUE
182
183
  }
183
184
 
184
185
 
185
- void rgeo_init_geos_point(RGeo_Globals* globals)
186
+ void rgeo_init_geos_point()
186
187
  {
187
188
  VALUE geos_point_methods;
188
189
 
189
190
  // Class methods for CAPIPointImpl
190
- rb_define_module_function(globals->geos_point, "create", cmethod_create, 4);
191
+ rb_define_module_function(rgeo_geos_point_class, "create", cmethod_create, 4);
191
192
 
192
193
  // CAPIPointMethods module
193
- geos_point_methods = rb_define_module_under(globals->geos_module, "CAPIPointMethods");
194
+ geos_point_methods = rb_define_module_under(rgeo_geos_module, "CAPIPointMethods");
194
195
  rb_define_method(geos_point_methods, "rep_equals?", method_point_eql, 1);
195
196
  rb_define_method(geos_point_methods, "eql?", method_point_eql, 1);
196
197
  rb_define_method(geos_point_methods, "hash", method_point_hash, 0);
@@ -221,7 +222,7 @@ VALUE rgeo_create_geos_point(VALUE factory, double x, double y, double z)
221
222
  if (GEOSCoordSeq_setZ_r(context, coord_seq, 0, z)) {
222
223
  geom = GEOSGeom_createPoint_r(context, coord_seq);
223
224
  if (geom) {
224
- result = rgeo_wrap_geos_geometry(factory, geom, factory_data->globals->geos_point);
225
+ result = rgeo_wrap_geos_geometry(factory, geom, rgeo_geos_point_class);
225
226
  }
226
227
  }
227
228
  }
@@ -8,8 +8,6 @@
8
8
 
9
9
  #include <ruby.h>
10
10
 
11
- #include "factory.h"
12
-
13
11
  RGEO_BEGIN_C
14
12
 
15
13
 
@@ -17,7 +15,7 @@ RGEO_BEGIN_C
17
15
  Initializes the point module. This should be called after
18
16
  the geometry module is initialized.
19
17
  */
20
- void rgeo_init_geos_point(RGeo_Globals* globals);
18
+ void rgeo_init_geos_point();
21
19
 
22
20
  /*
23
21
  Creates a 3d point and returns the ruby object.
@@ -10,6 +10,8 @@
10
10
  #include <ruby.h>
11
11
  #include <geos_c.h>
12
12
 
13
+ #include "globals.h"
14
+
13
15
  #include "factory.h"
14
16
  #include "geometry.h"
15
17
  #include "line_string.h"
@@ -28,7 +30,7 @@ static VALUE method_polygon_eql(VALUE self, VALUE rhs)
28
30
  result = rgeo_geos_klasses_and_factories_eql(self, rhs);
29
31
  if (RTEST(result)) {
30
32
  self_data = RGEO_GEOMETRY_DATA_PTR(self);
31
- result = rgeo_geos_polygons_eql(self_data->geos_context, self_data->geom, RGEO_GEOMETRY_DATA_PTR(rhs)->geom, RGEO_FACTORY_DATA_PTR(self_data->factory)->flags & RGEO_FACTORYFLAGS_SUPPORTS_Z_OR_M);
33
+ result = rgeo_geos_geometries_strict_eql(self_data->geos_context, self_data->geom, RGEO_GEOMETRY_DATA_PTR(rhs)->geom);
32
34
  }
33
35
  return result;
34
36
  }
@@ -43,8 +45,7 @@ static VALUE method_polygon_hash(VALUE self)
43
45
  self_data = RGEO_GEOMETRY_DATA_PTR(self);
44
46
  factory = self_data->factory;
45
47
  hash = rb_hash_start(0);
46
- hash = rgeo_geos_objbase_hash(factory,
47
- RGEO_FACTORY_DATA_PTR(factory)->globals->feature_polygon, hash);
48
+ hash = rgeo_geos_objbase_hash(factory, rgeo_feature_polygon_module, hash);
48
49
  hash = rgeo_geos_polygon_hash(self_data->geos_context, self_data->geom, hash);
49
50
  return LONG2FIX(rb_hash_end(hash));
50
51
  }
@@ -58,7 +59,7 @@ static VALUE method_polygon_geometry_type(VALUE self)
58
59
  result = Qnil;
59
60
  self_data = RGEO_GEOMETRY_DATA_PTR(self);
60
61
  if (self_data->geom) {
61
- result = RGEO_FACTORY_DATA_PTR(self_data->factory)->globals->feature_polygon;
62
+ result = rgeo_feature_polygon_module;
62
63
  }
63
64
  return result;
64
65
  }
@@ -93,7 +94,7 @@ static VALUE method_polygon_centroid(VALUE self)
93
94
  self_data = RGEO_GEOMETRY_DATA_PTR(self);
94
95
  self_geom = self_data->geom;
95
96
  if (self_geom) {
96
- result = rgeo_wrap_geos_geometry(self_data->factory, GEOSGetCentroid_r(self_data->geos_context, self_geom), RGEO_FACTORY_DATA_PTR(self_data->factory)->globals->geos_point);
97
+ result = rgeo_wrap_geos_geometry(self_data->factory, GEOSGetCentroid_r(self_data->geos_context, self_geom), rgeo_geos_point_class);
97
98
  }
98
99
  return result;
99
100
  }
@@ -109,7 +110,7 @@ static VALUE method_polygon_point_on_surface(VALUE self)
109
110
  self_data = RGEO_GEOMETRY_DATA_PTR(self);
110
111
  self_geom = self_data->geom;
111
112
  if (self_geom) {
112
- result = rgeo_wrap_geos_geometry(self_data->factory, GEOSPointOnSurface_r(self_data->geos_context, self_geom), RGEO_FACTORY_DATA_PTR(self_data->factory)->globals->geos_point);
113
+ result = rgeo_wrap_geos_geometry(self_data->factory, GEOSPointOnSurface_r(self_data->geos_context, self_geom), rgeo_geos_point_class);
113
114
  }
114
115
  return result;
115
116
  }
@@ -145,7 +146,7 @@ static VALUE method_polygon_exterior_ring(VALUE self)
145
146
  self_data = RGEO_GEOMETRY_DATA_PTR(self);
146
147
  self_geom = self_data->geom;
147
148
  if (self_geom) {
148
- result = rgeo_wrap_geos_geometry_clone(self_data->factory, GEOSGetExteriorRing_r(self_data->geos_context, self_geom), RGEO_FACTORY_DATA_PTR(self_data->factory)->globals->geos_linear_ring);
149
+ result = rgeo_wrap_geos_geometry_clone(self_data->factory, GEOSGetExteriorRing_r(self_data->geos_context, self_geom), rgeo_geos_linear_ring_class);
149
150
  }
150
151
  return result;
151
152
  }
@@ -191,7 +192,7 @@ static VALUE method_polygon_interior_ring_n(VALUE self, VALUE n)
191
192
  if (i < num) {
192
193
  result = rgeo_wrap_geos_geometry_clone(self_data->factory,
193
194
  GEOSGetInteriorRingN_r(self_context, self_geom, i),
194
- RGEO_FACTORY_DATA_PTR(self_data->factory)->globals->geos_linear_ring);
195
+ rgeo_geos_linear_ring_class);
195
196
  }
196
197
  }
197
198
  }
@@ -219,9 +220,8 @@ static VALUE method_polygon_interior_rings(VALUE self)
219
220
  if (count >= 0) {
220
221
  result = rb_ary_new2(count);
221
222
  factory = self_data->factory;
222
- linear_ring_class = RGEO_FACTORY_DATA_PTR(self_data->factory)->globals->geos_linear_ring;
223
223
  for (i=0; i<count; ++i) {
224
- rb_ary_store(result, i, rgeo_wrap_geos_geometry_clone(factory, GEOSGetInteriorRingN_r(self_context, self_geom, i), linear_ring_class));
224
+ rb_ary_store(result, i, rgeo_wrap_geos_geometry_clone(factory, GEOSGetInteriorRingN_r(self_context, self_geom, i), rgeo_geos_linear_ring_class));
225
225
  }
226
226
  }
227
227
  }
@@ -244,7 +244,7 @@ static VALUE cmethod_create(VALUE module, VALUE factory, VALUE exterior, VALUE i
244
244
 
245
245
  Check_Type(interior_array, T_ARRAY);
246
246
  factory_data = RGEO_FACTORY_DATA_PTR(factory);
247
- linear_ring_type = factory_data->globals->feature_linear_ring;
247
+ linear_ring_type = rgeo_feature_linear_ring_module;
248
248
  exterior_geom = rgeo_convert_to_detached_geos_geometry(exterior, factory, linear_ring_type, NULL);
249
249
  if (exterior_geom) {
250
250
  context = factory_data->geos_context;
@@ -262,7 +262,7 @@ static VALUE cmethod_create(VALUE module, VALUE factory, VALUE exterior, VALUE i
262
262
  polygon = GEOSGeom_createPolygon_r(context, exterior_geom, interior_geoms, actual_len);
263
263
  if (polygon) {
264
264
  free(interior_geoms);
265
- return rgeo_wrap_geos_geometry(factory, polygon, factory_data->globals->geos_polygon);
265
+ return rgeo_wrap_geos_geometry(factory, polygon, rgeo_geos_polygon_class);
266
266
  }
267
267
  }
268
268
  for (i=0; i<actual_len; ++i) {
@@ -276,15 +276,15 @@ static VALUE cmethod_create(VALUE module, VALUE factory, VALUE exterior, VALUE i
276
276
  }
277
277
 
278
278
 
279
- void rgeo_init_geos_polygon(RGeo_Globals* globals)
279
+ void rgeo_init_geos_polygon()
280
280
  {
281
281
  VALUE geos_polygon_methods;
282
282
 
283
283
  // Class methods for CAPIPolygonImpl
284
- rb_define_module_function(globals->geos_polygon, "create", cmethod_create, 3);
284
+ rb_define_module_function(rgeo_geos_polygon_class, "create", cmethod_create, 3);
285
285
 
286
286
  // CAPIPolygonMethods module
287
- geos_polygon_methods = rb_define_module_under(globals->geos_module, "CAPIPolygonMethods");
287
+ geos_polygon_methods = rb_define_module_under(rgeo_geos_module, "CAPIPolygonMethods");
288
288
  rb_define_method(geos_polygon_methods, "rep_equals?", method_polygon_eql, 1);
289
289
  rb_define_method(geos_polygon_methods, "eql?", method_polygon_eql, 1);
290
290
  rb_define_method(geos_polygon_methods, "hash", method_polygon_hash, 0);
@@ -299,42 +299,6 @@ void rgeo_init_geos_polygon(RGeo_Globals* globals)
299
299
  rb_define_method(geos_polygon_methods, "coordinates", method_polygon_coordinates, 0);
300
300
  }
301
301
 
302
-
303
- VALUE rgeo_geos_polygons_eql(GEOSContextHandle_t context, const GEOSGeometry* geom1, const GEOSGeometry* geom2, char check_z)
304
- {
305
- VALUE result;
306
- int len1;
307
- int len2;
308
- int i;
309
-
310
- result = Qnil;
311
- if (geom1 && geom2) {
312
- result = rgeo_geos_coordseqs_eql(context, GEOSGetExteriorRing_r(context, geom1), GEOSGetExteriorRing_r(context, geom2), check_z);
313
- if (RTEST(result)) {
314
- len1 = GEOSGetNumInteriorRings_r(context, geom1);
315
- len2 = GEOSGetNumInteriorRings_r(context, geom2);
316
- if (len1 >= 0 && len2 >= 0) {
317
- if (len1 == len2) {
318
- for (i=0; i<len1; ++i) {
319
- result = rgeo_geos_coordseqs_eql(context, GEOSGetInteriorRingN_r(context, geom1, i), GEOSGetInteriorRingN_r(context, geom2, i), check_z);
320
- if (!RTEST(result)) {
321
- break;
322
- }
323
- }
324
- }
325
- else {
326
- result = Qfalse;
327
- }
328
- }
329
- else {
330
- result = Qnil;
331
- }
332
- }
333
- }
334
- return result;
335
- }
336
-
337
-
338
302
  st_index_t rgeo_geos_polygon_hash(GEOSContextHandle_t context, const GEOSGeometry* geom, st_index_t hash)
339
303
  {
340
304
  unsigned int len;
@@ -9,8 +9,6 @@
9
9
  #include <ruby.h>
10
10
  #include <geos_c.h>
11
11
 
12
- #include "factory.h"
13
-
14
12
  RGEO_BEGIN_C
15
13
 
16
14
 
@@ -18,7 +16,7 @@ RGEO_BEGIN_C
18
16
  Initializes the polygon module. This should be called after
19
17
  the geometry module is initialized.
20
18
  */
21
- void rgeo_init_geos_polygon(RGeo_Globals* globals);
19
+ void rgeo_init_geos_polygon();
22
20
 
23
21
  /*
24
22
  Comopares the values of two GEOS polygons. The two given geometries MUST
@@ -27,6 +27,11 @@
27
27
  #ifdef HAVE_RB_MEMHASH
28
28
  #define RGEO_SUPPORTS_NEW_HASHING
29
29
  #endif
30
+ #ifdef HAVE_RB_GC_MARK_MOVABLE
31
+ #define mark rb_gc_mark_movable
32
+ #else
33
+ #define mark rb_gc_mark
34
+ #endif
30
35
 
31
36
  #ifndef RGEO_SUPPORTS_NEW_HASHING
32
37
  #define st_index_t int
@@ -42,3 +47,6 @@
42
47
  #define RGEO_BEGIN_C
43
48
  #define RGEO_END_C
44
49
  #endif
50
+
51
+ // https://ozlabs.org/~rusty/index.cgi/tech/2008-04-01.html
52
+ #define streq(a, b) (!strcmp((a),(b)))
@@ -22,12 +22,12 @@ module RGeo
22
22
  # == Note
23
23
  #
24
24
  # This method does not ensure a correct result for an invalid geometry.
25
- # You should make sure your ring is valid beforehand using `is_ring?`
25
+ # You should make sure your ring is valid beforehand using `ring?`
26
26
  # if you are using a LineString, or directly `valid?` for a
27
27
  # `linear_ring?`.
28
28
  # This will be subject to changes in v3.
29
29
  def ccw?(ring)
30
- if RGeo::Geos.is_capi_geos?(ring) && RGeo::Geos::Analysis.ccw_supported?
30
+ if RGeo::Geos.capi_geos?(ring) && RGeo::Geos::Analysis.ccw_supported?
31
31
  RGeo::Geos::Analysis.ccw?(ring)
32
32
  else
33
33
  RGeo::Cartesian::Analysis.ring_direction(ring) == 1
@@ -69,41 +69,78 @@ module RGeo
69
69
  end
70
70
 
71
71
  def intersects_segment?(seg)
72
+ !segment_intersection(seg).nil?
73
+ end
74
+
75
+ # If this and the other segment intersect, this method will return the coordinate
76
+ # at which they intersect, otherwise nil.
77
+ # In the case of a partial overlap (parallel segments), this will return
78
+ # a single point on the overlapping portion.
79
+ #
80
+ # @param seg [Segment]
81
+ #
82
+ # @return [RGeo::Feature::Point, nil]
83
+ def segment_intersection(seg)
72
84
  s2 = seg.s
73
85
  # Handle degenerate cases
74
86
  if seg.degenerate?
75
- if @lensq == 0
76
- return @s == s2
87
+ if @lensq == 0 && @s == s2
88
+ return @s
77
89
  else
78
- return contains_point?(s2)
90
+ return contains_point?(s2) ? s2 : nil
79
91
  end
80
92
  elsif @lensq == 0
81
- return seg.contains_point?(@s)
93
+ return seg.contains_point?(@s) ? @s : nil
82
94
  end
95
+
83
96
  # Both segments have nonzero length.
84
97
  sx2 = s2.x
85
98
  sy2 = s2.y
86
99
  dx2 = seg.dx
87
100
  dy2 = seg.dy
88
101
  denom = @dx * dy2 - @dy * dx2
102
+
89
103
  if denom == 0
90
104
  # Segments are parallel. Make sure they are collinear.
91
- return false unless side(s2) == 0
92
- # 1-D check.
93
- ts = (@dx * (sx2 - @sx) + @dy * (sy2 - @sy)) / @lensq
94
- te = (@dx * (sx2 + dx2 - @sx) + @dy * (sy2 + dy2 - @sy)) / @lensq
95
- if ts < te
96
- te >= 0.0 && ts <= 1.0
97
- else
98
- ts >= 0.0 && te <= 1.0
99
- end
105
+ return nil unless side(s2) == 0
106
+
107
+ # return the first point it finds that intersects another line.
108
+ # In many cases, the intersection is actually another line
109
+ # segment, but for now, we will just return a single point.
110
+ return s2 if contains_point?(s2)
111
+ return seg.e if contains_point?(seg.e)
112
+ return @s if seg.contains_point?(@s)
113
+ return @e if seg.contains_point?(@e)
114
+ nil
100
115
  else
101
116
  # Segments are not parallel. Check the intersection of their
102
117
  # containing lines.
103
- t = (dy2 * (sx2 - @sx) + dx2 * (@sy - sy2)) / denom
104
- return false if t < 0.0 || t > 1.0
105
- t2 = (@dy * (sx2 - @sx) + @dx * (@sy - sy2)) / denom
106
- t2 >= 0.0 && t2 <= 1.0
118
+ num1 = dx2 * (@sy - sy2) - (dy2 * (@sx - sx2))
119
+ num2 = @dx * (@sy - sy2) - (@dy * (@sx - sx2))
120
+ cross1 = num1 / denom
121
+ cross2 = num2 / denom
122
+
123
+ return nil if cross1 < 0.0 || cross1 > 1.0
124
+ if cross2 >= 0.0 && cross2 <= 1.0
125
+ x = @sx + (cross1 * @dx)
126
+ y = @sy + (cross1 * @dy)
127
+
128
+ # Check if this segment contains the point.
129
+ # Sometimes round-off errors occur and intersections
130
+ # are recorded as off the line segments.
131
+ #
132
+ # If this is the case, return the closest point from
133
+ # either segment.
134
+ int_pt = @s.factory.point(x, y)
135
+ if contains_point?(int_pt)
136
+ int_pt
137
+ else
138
+ # find closest of @s, @e, seg.s, seg.e
139
+ [@e, seg.s, seg.e].reduce(@s) do |closest, pt|
140
+ int_pt.distance(pt) < int_pt.distance(closest) ? pt : closest
141
+ end
142
+ end
143
+ end
107
144
  end
108
145
  end
109
146