rgeo 0.1.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. data/History.rdoc +22 -0
  2. data/README.rdoc +124 -0
  3. data/Version +1 -0
  4. data/ext/geos_c_impl/extconf.rb +72 -0
  5. data/ext/geos_c_impl/factory.c +468 -0
  6. data/ext/geos_c_impl/factory.h +217 -0
  7. data/ext/geos_c_impl/geometry.c +644 -0
  8. data/ext/geos_c_impl/geometry.h +65 -0
  9. data/ext/geos_c_impl/geometry_collection.c +580 -0
  10. data/ext/geos_c_impl/geometry_collection.h +79 -0
  11. data/ext/geos_c_impl/globals.h +58 -0
  12. data/ext/geos_c_impl/line_string.c +468 -0
  13. data/ext/geos_c_impl/line_string.h +74 -0
  14. data/ext/geos_c_impl/main.c +65 -0
  15. data/ext/geos_c_impl/point.c +201 -0
  16. data/ext/geos_c_impl/point.h +77 -0
  17. data/ext/geos_c_impl/polygon.c +259 -0
  18. data/ext/geos_c_impl/polygon.h +76 -0
  19. data/ext/geos_c_impl/preface.h +42 -0
  20. data/lib/rgeo.rb +68 -0
  21. data/lib/rgeo/errors.rb +59 -0
  22. data/lib/rgeo/features.rb +89 -0
  23. data/lib/rgeo/features/curve.rb +155 -0
  24. data/lib/rgeo/features/factory.rb +191 -0
  25. data/lib/rgeo/features/geometry.rb +560 -0
  26. data/lib/rgeo/features/geometry_collection.rb +118 -0
  27. data/lib/rgeo/features/line.rb +65 -0
  28. data/lib/rgeo/features/line_string.rb +101 -0
  29. data/lib/rgeo/features/linear_ring.rb +65 -0
  30. data/lib/rgeo/features/multi_curve.rb +112 -0
  31. data/lib/rgeo/features/multi_line_string.rb +65 -0
  32. data/lib/rgeo/features/multi_point.rb +72 -0
  33. data/lib/rgeo/features/multi_polygon.rb +96 -0
  34. data/lib/rgeo/features/multi_surface.rb +115 -0
  35. data/lib/rgeo/features/point.rb +97 -0
  36. data/lib/rgeo/features/polygon.rb +141 -0
  37. data/lib/rgeo/features/surface.rb +121 -0
  38. data/lib/rgeo/geo_json.rb +58 -0
  39. data/lib/rgeo/geo_json/coder.rb +305 -0
  40. data/lib/rgeo/geo_json/entities.rb +284 -0
  41. data/lib/rgeo/geo_json/interface.rb +95 -0
  42. data/lib/rgeo/geography.rb +75 -0
  43. data/lib/rgeo/geography/common/geometry_collection_methods.rb +206 -0
  44. data/lib/rgeo/geography/common/geometry_methods.rb +92 -0
  45. data/lib/rgeo/geography/common/helper.rb +102 -0
  46. data/lib/rgeo/geography/common/line_string_methods.rb +187 -0
  47. data/lib/rgeo/geography/common/point_methods.rb +149 -0
  48. data/lib/rgeo/geography/common/polygon_methods.rb +122 -0
  49. data/lib/rgeo/geography/factories.rb +136 -0
  50. data/lib/rgeo/geography/factory.rb +246 -0
  51. data/lib/rgeo/geography/projected_window.rb +467 -0
  52. data/lib/rgeo/geography/simple_mercator/feature_classes.rb +320 -0
  53. data/lib/rgeo/geography/simple_mercator/feature_methods.rb +291 -0
  54. data/lib/rgeo/geography/simple_mercator/projector.rb +116 -0
  55. data/lib/rgeo/geography/simple_spherical/calculations.rb +70 -0
  56. data/lib/rgeo/geography/simple_spherical/geometry_collection_impl.rb +66 -0
  57. data/lib/rgeo/geography/simple_spherical/geometry_methods.rb +59 -0
  58. data/lib/rgeo/geography/simple_spherical/line_string_impl.rb +104 -0
  59. data/lib/rgeo/geography/simple_spherical/multi_line_string_impl.rb +67 -0
  60. data/lib/rgeo/geography/simple_spherical/multi_point_impl.rb +67 -0
  61. data/lib/rgeo/geography/simple_spherical/multi_polygon_impl.rb +67 -0
  62. data/lib/rgeo/geography/simple_spherical/point_impl.rb +85 -0
  63. data/lib/rgeo/geography/simple_spherical/polygon_impl.rb +66 -0
  64. data/lib/rgeo/geos.rb +72 -0
  65. data/lib/rgeo/geos/factory.rb +260 -0
  66. data/lib/rgeo/geos/impl_additions.rb +57 -0
  67. data/lib/rgeo/geos/interface.rb +74 -0
  68. data/lib/rgeo/version.rb +52 -0
  69. data/tests/geos/tc_factory.rb +91 -0
  70. data/tests/geos/tc_geometry_collection.rb +226 -0
  71. data/tests/geos/tc_line_string.rb +310 -0
  72. data/tests/geos/tc_misc.rb +72 -0
  73. data/tests/geos/tc_multi_line_string.rb +211 -0
  74. data/tests/geos/tc_multi_point.rb +202 -0
  75. data/tests/geos/tc_multi_polygon.rb +210 -0
  76. data/tests/geos/tc_point.rb +305 -0
  77. data/tests/geos/tc_polygon.rb +240 -0
  78. data/tests/simple_mercator/tc_point.rb +303 -0
  79. data/tests/simple_mercator/tc_window.rb +219 -0
  80. data/tests/tc_geojson.rb +230 -0
  81. data/tests/tc_oneoff.rb +61 -0
  82. metadata +162 -0
@@ -0,0 +1,74 @@
1
+ /*
2
+ -----------------------------------------------------------------------------
3
+
4
+ Line string methods for GEOS wrapper
5
+
6
+ -----------------------------------------------------------------------------
7
+ Copyright 2010 Daniel Azuma
8
+
9
+ All rights reserved.
10
+
11
+ Redistribution and use in source and binary forms, with or without
12
+ modification, are permitted provided that the following conditions are met:
13
+
14
+ * Redistributions of source code must retain the above copyright notice,
15
+ this list of conditions and the following disclaimer.
16
+ * Redistributions in binary form must reproduce the above copyright notice,
17
+ this list of conditions and the following disclaimer in the documentation
18
+ and/or other materials provided with the distribution.
19
+ * Neither the name of the copyright holder, nor the names of any other
20
+ contributors to this software, may be used to endorse or promote products
21
+ derived from this software without specific prior written permission.
22
+
23
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33
+ POSSIBILITY OF SUCH DAMAGE.
34
+ -----------------------------------------------------------------------------
35
+ */
36
+
37
+
38
+ #ifndef RGEO_GEOS_LINE_STRING_INCLUDED
39
+ #define RGEO_GEOS_LINE_STRING_INCLUDED
40
+
41
+ #include <ruby.h>
42
+ #include <geos_c.h>
43
+
44
+ #include "factory.h"
45
+
46
+ #ifdef __cplusplus
47
+ extern "C" {
48
+ #if 0
49
+ }
50
+ #endif
51
+ #endif
52
+
53
+
54
+ /*
55
+ Initializes the line string module. This should be called after
56
+ the geometry module is initialized.
57
+ */
58
+ void rgeo_init_geos_line_string(RGeo_Globals* globals);
59
+
60
+ /*
61
+ Determines whether the given GEOS line string is closed.
62
+ Returns Qtrue if true, Qfalse if false, or Qnil on an error.
63
+ */
64
+ VALUE rgeo_is_geos_line_string_closed(GEOSContextHandle_t context, const GEOSGeometry* geom);
65
+
66
+
67
+ #ifdef __cplusplus
68
+ #if 0
69
+ {
70
+ #endif
71
+ }
72
+ #endif
73
+
74
+ #endif
@@ -0,0 +1,65 @@
1
+ /*
2
+ -----------------------------------------------------------------------------
3
+
4
+ Main initializer for GEOS wrapper
5
+
6
+ -----------------------------------------------------------------------------
7
+ Copyright 2010 Daniel Azuma
8
+
9
+ All rights reserved.
10
+
11
+ Redistribution and use in source and binary forms, with or without
12
+ modification, are permitted provided that the following conditions are met:
13
+
14
+ * Redistributions of source code must retain the above copyright notice,
15
+ this list of conditions and the following disclaimer.
16
+ * Redistributions in binary form must reproduce the above copyright notice,
17
+ this list of conditions and the following disclaimer in the documentation
18
+ and/or other materials provided with the distribution.
19
+ * Neither the name of the copyright holder, nor the names of any other
20
+ contributors to this software, may be used to endorse or promote products
21
+ derived from this software without specific prior written permission.
22
+
23
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33
+ POSSIBILITY OF SUCH DAMAGE.
34
+ -----------------------------------------------------------------------------
35
+ */
36
+
37
+
38
+ #include "preface.h"
39
+
40
+ #ifdef RGEO_GEOS_SUPPORTED
41
+
42
+ #include <ruby.h>
43
+ #include <geos_c.h>
44
+
45
+ #include "factory.h"
46
+ #include "geometry.h"
47
+ #include "point.h"
48
+ #include "line_string.h"
49
+ #include "polygon.h"
50
+ #include "geometry_collection.h"
51
+
52
+ #endif
53
+
54
+
55
+ void Init_geos_c_impl()
56
+ {
57
+ #ifdef RGEO_GEOS_SUPPORTED
58
+ RGeo_Globals* globals = rgeo_init_geos_factory();
59
+ rgeo_init_geos_geometry(globals);
60
+ rgeo_init_geos_point(globals);
61
+ rgeo_init_geos_line_string(globals);
62
+ rgeo_init_geos_polygon(globals);
63
+ rgeo_init_geos_geometry_collection(globals);
64
+ #endif
65
+ }
@@ -0,0 +1,201 @@
1
+ /*
2
+ -----------------------------------------------------------------------------
3
+
4
+ Point methods for GEOS wrapper
5
+
6
+ -----------------------------------------------------------------------------
7
+ Copyright 2010 Daniel Azuma
8
+
9
+ All rights reserved.
10
+
11
+ Redistribution and use in source and binary forms, with or without
12
+ modification, are permitted provided that the following conditions are met:
13
+
14
+ * Redistributions of source code must retain the above copyright notice,
15
+ this list of conditions and the following disclaimer.
16
+ * Redistributions in binary form must reproduce the above copyright notice,
17
+ this list of conditions and the following disclaimer in the documentation
18
+ and/or other materials provided with the distribution.
19
+ * Neither the name of the copyright holder, nor the names of any other
20
+ contributors to this software, may be used to endorse or promote products
21
+ derived from this software without specific prior written permission.
22
+
23
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33
+ POSSIBILITY OF SUCH DAMAGE.
34
+ -----------------------------------------------------------------------------
35
+ */
36
+
37
+
38
+ #include "preface.h"
39
+
40
+ #ifdef RGEO_GEOS_SUPPORTED
41
+
42
+ #include <ruby.h>
43
+ #include <geos_c.h>
44
+
45
+ #include "factory.h"
46
+ #include "geometry.h"
47
+ #include "point.h"
48
+
49
+ #ifdef __cplusplus
50
+ extern "C" {
51
+ #if 0
52
+ }
53
+ #endif
54
+ #endif
55
+
56
+
57
+ static VALUE method_point_geometry_type(VALUE self)
58
+ {
59
+ VALUE result = Qnil;
60
+ const GEOSGeometry* self_geom = RGEO_GET_GEOS_GEOMETRY(self);
61
+ if (self_geom) {
62
+ result = rb_const_get_at(RGEO_GLOBALS_FROM_GEOMETRY(self)->features_module, rb_intern("Point"));
63
+ }
64
+ return result;
65
+ }
66
+
67
+
68
+ static VALUE method_point_x(VALUE self)
69
+ {
70
+ VALUE result = Qnil;
71
+ const GEOSGeometry* self_geom = RGEO_GET_GEOS_GEOMETRY(self);
72
+ if (self_geom) {
73
+ const GEOSCoordSequence* coord_seq = GEOSGeom_getCoordSeq_r(RGEO_CONTEXT_FROM_GEOMETRY(self), self_geom);
74
+ if (coord_seq) {
75
+ double val;
76
+ if (GEOSCoordSeq_getX_r(RGEO_CONTEXT_FROM_GEOMETRY(self), coord_seq, 0, &val)) {
77
+ result = rb_float_new(val);
78
+ }
79
+ }
80
+ }
81
+ return result;
82
+ }
83
+
84
+
85
+ static VALUE method_point_y(VALUE self)
86
+ {
87
+ VALUE result = Qnil;
88
+ const GEOSGeometry* self_geom = RGEO_GET_GEOS_GEOMETRY(self);
89
+ if (self_geom) {
90
+ const GEOSCoordSequence* coord_seq = GEOSGeom_getCoordSeq_r(RGEO_CONTEXT_FROM_GEOMETRY(self), self_geom);
91
+ if (coord_seq) {
92
+ double val;
93
+ if (GEOSCoordSeq_getY_r(RGEO_CONTEXT_FROM_GEOMETRY(self), coord_seq, 0, &val)) {
94
+ result = rb_float_new(val);
95
+ }
96
+ }
97
+ }
98
+ return result;
99
+ }
100
+
101
+
102
+ static VALUE method_point_z(VALUE self)
103
+ {
104
+ VALUE result = Qnil;
105
+ const GEOSGeometry* self_geom = RGEO_GET_GEOS_GEOMETRY(self);
106
+ if (self_geom && GEOSHasZ_r(RGEO_CONTEXT_FROM_GEOMETRY(self), self_geom) == 1) {
107
+ const GEOSCoordSequence* coord_seq = GEOSGeom_getCoordSeq_r(RGEO_CONTEXT_FROM_GEOMETRY(self), self_geom);
108
+ if (coord_seq) {
109
+ double val;
110
+ if (GEOSCoordSeq_getZ_r(RGEO_CONTEXT_FROM_GEOMETRY(self), coord_seq, 0, &val)) {
111
+ result = rb_float_new(val);
112
+ }
113
+ }
114
+ }
115
+ return result;
116
+ }
117
+
118
+
119
+ static VALUE method_point_eql(VALUE self, VALUE rhs)
120
+ {
121
+ VALUE result = rgeo_geos_klasses_and_factories_eql(self, rhs);
122
+ if (RTEST(result)) {
123
+ result = rgeo_geos_coordseqs_eql(RGEO_CONTEXT_FROM_GEOMETRY(self), RGEO_GET_GEOS_GEOMETRY(self), RGEO_GET_GEOS_GEOMETRY(rhs));
124
+ }
125
+ return result;
126
+ }
127
+
128
+
129
+ static VALUE cmethod_create(VALUE module, VALUE factory, VALUE x, VALUE y)
130
+ {
131
+ return rgeo_create_geos_point_2d(factory, rb_num2dbl(x), rb_num2dbl(y));
132
+ }
133
+
134
+
135
+ static VALUE cmethod_create3d(VALUE module, VALUE factory, VALUE x, VALUE y, VALUE z)
136
+ {
137
+ return rgeo_create_geos_point_3d(factory, rb_num2dbl(x), rb_num2dbl(y), rb_num2dbl(z));
138
+ }
139
+
140
+
141
+ void rgeo_init_geos_point(RGeo_Globals* globals)
142
+ {
143
+ VALUE geos_point_class = rb_define_class_under(globals->geos_module, "PointImpl", rb_const_get_at(globals->geos_module, rb_intern("GeometryImpl")));
144
+
145
+ rb_define_module_function(geos_point_class, "create", cmethod_create, 3);
146
+ rb_define_module_function(geos_point_class, "create3d", cmethod_create3d, 4);
147
+
148
+ rb_define_method(geos_point_class, "eql?", method_point_eql, 1);
149
+ rb_define_method(geos_point_class, "geometry_type", method_point_geometry_type, 0);
150
+ rb_define_method(geos_point_class, "x", method_point_x, 0);
151
+ rb_define_method(geos_point_class, "y", method_point_y, 0);
152
+ rb_define_method(geos_point_class, "z", method_point_z, 0);
153
+ }
154
+
155
+
156
+ VALUE rgeo_create_geos_point_2d(VALUE factory, double x, double y)
157
+ {
158
+ VALUE result = Qnil;
159
+ GEOSCoordSequence* coord_seq = GEOSCoordSeq_create_r(RGEO_CONTEXT_FROM_FACTORY(factory), 1, 2);
160
+ if (coord_seq) {
161
+ if (GEOSCoordSeq_setX_r(RGEO_CONTEXT_FROM_FACTORY(factory), coord_seq, 0, x)) {
162
+ if (GEOSCoordSeq_setY_r(RGEO_CONTEXT_FROM_FACTORY(factory), coord_seq, 0, y)) {
163
+ GEOSGeometry* geom = GEOSGeom_createPoint_r(RGEO_CONTEXT_FROM_FACTORY(factory), coord_seq);
164
+ if (geom) {
165
+ result = rgeo_wrap_geos_geometry(factory, geom, rb_const_get_at(RGEO_GLOBALS_FROM_FACTORY(factory)->geos_module, rb_intern("PointImpl")));
166
+ }
167
+ }
168
+ }
169
+ }
170
+ return result;
171
+ }
172
+
173
+
174
+ VALUE rgeo_create_geos_point_3d(VALUE factory, double x, double y, double z)
175
+ {
176
+ VALUE result = Qnil;
177
+ GEOSCoordSequence* coord_seq = GEOSCoordSeq_create_r(RGEO_CONTEXT_FROM_FACTORY(factory), 1, 2);
178
+ if (coord_seq) {
179
+ if (GEOSCoordSeq_setX_r(RGEO_CONTEXT_FROM_FACTORY(factory), coord_seq, 0, x)) {
180
+ if (GEOSCoordSeq_setY_r(RGEO_CONTEXT_FROM_FACTORY(factory), coord_seq, 0, y)) {
181
+ if (GEOSCoordSeq_setZ_r(RGEO_CONTEXT_FROM_FACTORY(factory), coord_seq, 0, z)) {
182
+ GEOSGeometry* geom = GEOSGeom_createPoint_r(RGEO_CONTEXT_FROM_FACTORY(factory), coord_seq);
183
+ if (geom) {
184
+ result = rgeo_wrap_geos_geometry(factory, geom, rb_const_get_at(RGEO_GLOBALS_FROM_FACTORY(factory)->geos_module, rb_intern("PointImpl")));
185
+ }
186
+ }
187
+ }
188
+ }
189
+ }
190
+ return result;
191
+ }
192
+
193
+
194
+ #ifdef __cplusplus
195
+ #if 0
196
+ {
197
+ #endif
198
+ }
199
+ #endif
200
+
201
+ #endif
@@ -0,0 +1,77 @@
1
+ /*
2
+ -----------------------------------------------------------------------------
3
+
4
+ Point methods for GEOS wrapper
5
+
6
+ -----------------------------------------------------------------------------
7
+ Copyright 2010 Daniel Azuma
8
+
9
+ All rights reserved.
10
+
11
+ Redistribution and use in source and binary forms, with or without
12
+ modification, are permitted provided that the following conditions are met:
13
+
14
+ * Redistributions of source code must retain the above copyright notice,
15
+ this list of conditions and the following disclaimer.
16
+ * Redistributions in binary form must reproduce the above copyright notice,
17
+ this list of conditions and the following disclaimer in the documentation
18
+ and/or other materials provided with the distribution.
19
+ * Neither the name of the copyright holder, nor the names of any other
20
+ contributors to this software, may be used to endorse or promote products
21
+ derived from this software without specific prior written permission.
22
+
23
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33
+ POSSIBILITY OF SUCH DAMAGE.
34
+ -----------------------------------------------------------------------------
35
+ */
36
+
37
+
38
+ #ifndef RGEO_GEOS_POINT_INCLUDED
39
+ #define RGEO_GEOS_POINT_INCLUDED
40
+
41
+ #include <ruby.h>
42
+
43
+ #include "factory.h"
44
+
45
+ #ifdef __cplusplus
46
+ extern "C" {
47
+ #if 0
48
+ }
49
+ #endif
50
+ #endif
51
+
52
+
53
+ /*
54
+ Initializes the point module. This should be called after
55
+ the geometry module is initialized.
56
+ */
57
+ void rgeo_init_geos_point(RGeo_Globals* globals);
58
+
59
+ /*
60
+ Creates a 2d point and returns the ruby object.
61
+ */
62
+ VALUE rgeo_create_geos_point_2d(VALUE factory, double x, double y);
63
+
64
+ /*
65
+ Creates a 3d point and returns the ruby object.
66
+ */
67
+ VALUE rgeo_create_geos_point_3d(VALUE factory, double x, double y, double z);
68
+
69
+
70
+ #ifdef __cplusplus
71
+ #if 0
72
+ {
73
+ #endif
74
+ }
75
+ #endif
76
+
77
+ #endif
@@ -0,0 +1,259 @@
1
+ /*
2
+ -----------------------------------------------------------------------------
3
+
4
+ Polygon methods for GEOS wrapper
5
+
6
+ -----------------------------------------------------------------------------
7
+ Copyright 2010 Daniel Azuma
8
+
9
+ All rights reserved.
10
+
11
+ Redistribution and use in source and binary forms, with or without
12
+ modification, are permitted provided that the following conditions are met:
13
+
14
+ * Redistributions of source code must retain the above copyright notice,
15
+ this list of conditions and the following disclaimer.
16
+ * Redistributions in binary form must reproduce the above copyright notice,
17
+ this list of conditions and the following disclaimer in the documentation
18
+ and/or other materials provided with the distribution.
19
+ * Neither the name of the copyright holder, nor the names of any other
20
+ contributors to this software, may be used to endorse or promote products
21
+ derived from this software without specific prior written permission.
22
+
23
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33
+ POSSIBILITY OF SUCH DAMAGE.
34
+ -----------------------------------------------------------------------------
35
+ */
36
+
37
+
38
+ #include "preface.h"
39
+
40
+ #ifdef RGEO_GEOS_SUPPORTED
41
+
42
+ #include <ruby.h>
43
+ #include <geos_c.h>
44
+
45
+ #include "factory.h"
46
+ #include "geometry.h"
47
+ #include "line_string.h"
48
+ #include "polygon.h"
49
+
50
+ #ifdef __cplusplus
51
+ extern "C" {
52
+ #if 0
53
+ }
54
+ #endif
55
+ #endif
56
+
57
+
58
+ static VALUE method_polygon_eql(VALUE self, VALUE rhs)
59
+ {
60
+ VALUE result = rgeo_geos_klasses_and_factories_eql(self, rhs);
61
+ if (RTEST(result)) {
62
+ result = rgeo_geos_polygons_eql(RGEO_CONTEXT_FROM_GEOMETRY(self), RGEO_GET_GEOS_GEOMETRY(self), RGEO_GET_GEOS_GEOMETRY(rhs));
63
+ }
64
+ return result;
65
+ }
66
+
67
+
68
+ static VALUE method_polygon_geometry_type(VALUE self)
69
+ {
70
+ VALUE result = Qnil;
71
+ const GEOSGeometry* self_geom = RGEO_GET_GEOS_GEOMETRY(self);
72
+ if (self_geom) {
73
+ result = rb_const_get_at(RGEO_GLOBALS_FROM_GEOMETRY(self)->features_module, rb_intern("Polygon"));
74
+ }
75
+ return result;
76
+ }
77
+
78
+
79
+ static VALUE method_polygon_area(VALUE self)
80
+ {
81
+ VALUE result = Qnil;
82
+ const GEOSGeometry* self_geom = RGEO_GET_GEOS_GEOMETRY(self);
83
+ if (self_geom) {
84
+ double area;
85
+ if (GEOSArea_r(RGEO_CONTEXT_FROM_GEOMETRY(self), self_geom, &area)) {
86
+ result = rb_float_new(area);
87
+ }
88
+ }
89
+ return result;
90
+ }
91
+
92
+
93
+ static VALUE method_polygon_centroid(VALUE self)
94
+ {
95
+ VALUE result = Qnil;
96
+ const GEOSGeometry* self_geom = RGEO_GET_GEOS_GEOMETRY(self);
97
+ if (self_geom) {
98
+ result = rgeo_wrap_geos_geometry(RGEO_FACTORY_FROM_GEOMETRY(self), GEOSGetCentroid_r(RGEO_CONTEXT_FROM_GEOMETRY(self), self_geom), rb_const_get_at(RGEO_GLOBALS_FROM_GEOMETRY(self)->geos_module, rb_intern("PointImpl")));
99
+ }
100
+ return result;
101
+ }
102
+
103
+
104
+ static VALUE method_polygon_point_on_surface(VALUE self)
105
+ {
106
+ VALUE result = Qnil;
107
+ const GEOSGeometry* self_geom = RGEO_GET_GEOS_GEOMETRY(self);
108
+ if (self_geom) {
109
+ result = rgeo_wrap_geos_geometry(RGEO_FACTORY_FROM_GEOMETRY(self), GEOSPointOnSurface_r(RGEO_CONTEXT_FROM_GEOMETRY(self), self_geom), rb_const_get_at(RGEO_GLOBALS_FROM_GEOMETRY(self)->geos_module, rb_intern("PointImpl")));
110
+ }
111
+ return result;
112
+ }
113
+
114
+
115
+ static VALUE method_polygon_exterior_ring(VALUE self)
116
+ {
117
+ VALUE result = Qnil;
118
+ const GEOSGeometry* self_geom = RGEO_GET_GEOS_GEOMETRY(self);
119
+ if (self_geom) {
120
+ result = rgeo_wrap_geos_geometry_clone(RGEO_FACTORY_FROM_GEOMETRY(self), GEOSGetExteriorRing_r(RGEO_CONTEXT_FROM_GEOMETRY(self), self_geom), rb_const_get_at(RGEO_GLOBALS_FROM_GEOMETRY(self)->geos_module, rb_intern("LinearRingImpl")));
121
+ }
122
+ return result;
123
+ }
124
+
125
+
126
+ static VALUE method_polygon_num_interior_rings(VALUE self)
127
+ {
128
+ VALUE result = Qnil;
129
+ const GEOSGeometry* self_geom = RGEO_GET_GEOS_GEOMETRY(self);
130
+ if (self_geom) {
131
+ int num = GEOSGetNumInteriorRings_r(RGEO_CONTEXT_FROM_GEOMETRY(self), self_geom);
132
+ if (num >= 0) {
133
+ result = INT2NUM(num);
134
+ }
135
+ }
136
+ return result;
137
+ }
138
+
139
+
140
+ static VALUE method_polygon_interior_ring_n(VALUE self, VALUE n)
141
+ {
142
+ VALUE result = Qnil;
143
+ const GEOSGeometry* self_geom = RGEO_GET_GEOS_GEOMETRY(self);
144
+ if (self_geom) {
145
+ result = rgeo_wrap_geos_geometry_clone(RGEO_FACTORY_FROM_GEOMETRY(self), GEOSGetInteriorRingN_r(RGEO_CONTEXT_FROM_GEOMETRY(self), self_geom, NUM2INT(n)), rb_const_get_at(RGEO_GLOBALS_FROM_GEOMETRY(self)->geos_module, rb_intern("LinearRingImpl")));
146
+ }
147
+ return result;
148
+ }
149
+
150
+
151
+ static VALUE method_polygon_interior_rings(VALUE self)
152
+ {
153
+ VALUE result = Qnil;
154
+ const GEOSGeometry* self_geom = RGEO_GET_GEOS_GEOMETRY(self);
155
+ if (self_geom) {
156
+ int count = GEOSGetNumInteriorRings_r(RGEO_CONTEXT_FROM_GEOMETRY(self), self_geom);
157
+ if (count >= 0) {
158
+ result = rb_ary_new2(count);
159
+ int i;
160
+ for (i=0; i<count; ++i) {
161
+ rb_ary_store(result, i, rgeo_wrap_geos_geometry_clone(RGEO_FACTORY_FROM_GEOMETRY(self), GEOSGetInteriorRingN_r(RGEO_CONTEXT_FROM_GEOMETRY(self), self_geom, i), rb_const_get_at(RGEO_GLOBALS_FROM_GEOMETRY(self)->geos_module, rb_intern("LinearRingImpl"))));
162
+ }
163
+ }
164
+ }
165
+ return result;
166
+ }
167
+
168
+
169
+ static VALUE cmethod_create(VALUE module, VALUE factory, VALUE exterior, VALUE interior_array)
170
+ {
171
+ Check_Type(interior_array, T_ARRAY);
172
+ GEOSGeometry* exterior_geom = rgeo_convert_to_detached_geos_geometry(RGEO_GLOBALS_FROM_FACTORY(factory), exterior, NULL);
173
+ if (exterior_geom) {
174
+ unsigned int len = (unsigned int)RARRAY_LEN(interior_array);
175
+ GEOSGeometry** interior_geoms = ALLOC_N(GEOSGeometry*, len == 0 ? 1 : len);
176
+ if (interior_geoms) {
177
+ unsigned int actual_len = 0;
178
+ unsigned int i;
179
+ for (i=0; i<len; ++i) {
180
+ GEOSGeometry* interior_geom = rgeo_convert_to_detached_geos_geometry(RGEO_GLOBALS_FROM_FACTORY(factory), rb_ary_entry(interior_array, i), NULL);
181
+ if (interior_geom) {
182
+ interior_geoms[actual_len++] = interior_geom;
183
+ }
184
+ }
185
+ if (len == actual_len) {
186
+ GEOSGeometry* polygon = GEOSGeom_createPolygon_r(RGEO_CONTEXT_FROM_FACTORY(factory), exterior_geom, interior_geoms, actual_len);
187
+ if (polygon) {
188
+ free(interior_geoms);
189
+ return rgeo_wrap_geos_geometry(factory, polygon, rb_const_get_at(RGEO_GLOBALS_FROM_FACTORY(factory)->geos_module, rb_intern("PolygonImpl")));
190
+ }
191
+ }
192
+ for (i=0; i<actual_len; ++i) {
193
+ GEOSGeom_destroy_r(RGEO_CONTEXT_FROM_FACTORY(factory), interior_geoms[i]);
194
+ }
195
+ free(interior_geoms);
196
+ }
197
+ GEOSGeom_destroy_r(RGEO_CONTEXT_FROM_FACTORY(factory), exterior_geom);
198
+ }
199
+ return Qnil;
200
+ }
201
+
202
+
203
+ void rgeo_init_geos_polygon(RGeo_Globals* globals)
204
+ {
205
+ VALUE geos_polygon_class = rb_define_class_under(globals->geos_module, "PolygonImpl", rb_const_get_at(globals->geos_module, rb_intern("GeometryImpl")));
206
+
207
+ rb_define_module_function(geos_polygon_class, "create", cmethod_create, 3);
208
+
209
+ rb_define_method(geos_polygon_class, "eql?", method_polygon_eql, 1);
210
+ rb_define_method(geos_polygon_class, "geometry_type", method_polygon_geometry_type, 0);
211
+ rb_define_method(geos_polygon_class, "area", method_polygon_area, 0);
212
+ rb_define_method(geos_polygon_class, "centroid", method_polygon_centroid, 0);
213
+ rb_define_method(geos_polygon_class, "point_on_surface", method_polygon_point_on_surface, 0);
214
+ rb_define_method(geos_polygon_class, "exterior_ring", method_polygon_exterior_ring, 0);
215
+ rb_define_method(geos_polygon_class, "num_interior_rings", method_polygon_num_interior_rings, 0);
216
+ rb_define_method(geos_polygon_class, "interior_ring_n", method_polygon_interior_ring_n, 1);
217
+ rb_define_method(geos_polygon_class, "interior_rings", method_polygon_interior_rings, 0);
218
+ }
219
+
220
+
221
+ VALUE rgeo_geos_polygons_eql(GEOSContextHandle_t context, const GEOSGeometry* geom1, const GEOSGeometry* geom2)
222
+ {
223
+ VALUE result = Qnil;
224
+ if (geom1 && geom2) {
225
+ result = rgeo_geos_coordseqs_eql(context, GEOSGetExteriorRing_r(context, geom1), GEOSGetExteriorRing_r(context, geom2));
226
+ if (RTEST(result)) {
227
+ int len1 = GEOSGetNumInteriorRings_r(context, geom1);
228
+ int len2 = GEOSGetNumInteriorRings_r(context, geom2);
229
+ if (len1 >= 0 && len2 >= 0) {
230
+ if (len1 == len2) {
231
+ int i;
232
+ for (i=0; i<len1; ++i) {
233
+ result = rgeo_geos_coordseqs_eql(context, GEOSGetInteriorRingN_r(context, geom1, i), GEOSGetInteriorRingN_r(context, geom2, i));
234
+ if (!RTEST(result)) {
235
+ break;
236
+ }
237
+ }
238
+ }
239
+ else {
240
+ result = Qfalse;
241
+ }
242
+ }
243
+ else {
244
+ result = Qnil;
245
+ }
246
+ }
247
+ }
248
+ return result;
249
+ }
250
+
251
+
252
+ #ifdef __cplusplus
253
+ #if 0
254
+ {
255
+ #endif
256
+ }
257
+ #endif
258
+
259
+ #endif