-
Notifications
You must be signed in to change notification settings - Fork 165
/
polygon.c
359 lines (320 loc) · 8.9 KB
/
polygon.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
Polygon methods for GEOS wrapper
*/
#include "preface.h"
#ifdef RGEO_GEOS_SUPPORTED
#include <geos_c.h>
#include <ruby.h>
#include "coordinates.h"
#include "factory.h"
#include "geometry.h"
#include "globals.h"
#include "line_string.h"
#include "polygon.h"
RGEO_BEGIN_C
static VALUE
method_polygon_eql(VALUE self, VALUE rhs)
{
VALUE result;
RGeo_GeometryData* self_data;
result = rgeo_geos_klasses_and_factories_eql(self, rhs);
if (RTEST(result)) {
self_data = RGEO_GEOMETRY_DATA_PTR(self);
result = rgeo_geos_geometries_strict_eql(self_data->geom,
RGEO_GEOMETRY_DATA_PTR(rhs)->geom);
}
return result;
}
static VALUE
method_polygon_hash(VALUE self)
{
st_index_t hash;
RGeo_GeometryData* self_data;
VALUE factory;
self_data = RGEO_GEOMETRY_DATA_PTR(self);
factory = self_data->factory;
hash = rb_hash_start(0);
hash = rgeo_geos_objbase_hash(factory, rgeo_feature_polygon_module, hash);
hash = rgeo_geos_polygon_hash(self_data->geom, hash);
return LONG2FIX(rb_hash_end(hash));
}
static VALUE
method_polygon_geometry_type(VALUE self)
{
VALUE result;
RGeo_GeometryData* self_data;
result = Qnil;
self_data = RGEO_GEOMETRY_DATA_PTR(self);
if (self_data->geom) {
result = rgeo_feature_polygon_module;
}
return result;
}
static VALUE
method_polygon_area(VALUE self)
{
VALUE result;
RGeo_GeometryData* self_data;
const GEOSGeometry* self_geom;
double area;
result = Qnil;
self_data = RGEO_GEOMETRY_DATA_PTR(self);
self_geom = self_data->geom;
if (self_geom) {
if (GEOSArea(self_geom, &area)) {
result = rb_float_new(area);
}
}
return result;
}
static VALUE
method_polygon_centroid(VALUE self)
{
VALUE result;
RGeo_GeometryData* self_data;
const GEOSGeometry* self_geom;
result = Qnil;
self_data = RGEO_GEOMETRY_DATA_PTR(self);
self_geom = self_data->geom;
if (self_geom) {
result = rgeo_wrap_geos_geometry(
self_data->factory, GEOSGetCentroid(self_geom), rgeo_geos_point_class);
}
return result;
}
static VALUE
method_polygon_point_on_surface(VALUE self)
{
VALUE result;
RGeo_GeometryData* self_data;
const GEOSGeometry* self_geom;
result = Qnil;
self_data = RGEO_GEOMETRY_DATA_PTR(self);
self_geom = self_data->geom;
if (self_geom) {
result = rgeo_wrap_geos_geometry(
self_data->factory, GEOSPointOnSurface(self_geom), rgeo_geos_point_class);
}
return result;
}
static VALUE
method_polygon_coordinates(VALUE self)
{
VALUE result = Qnil;
RGeo_GeometryData* self_data;
const GEOSGeometry* self_geom;
int zCoordinate;
self_data = RGEO_GEOMETRY_DATA_PTR(self);
self_geom = self_data->geom;
if (self_geom) {
zCoordinate = RGEO_FACTORY_DATA_PTR(self_data->factory)->flags &
RGEO_FACTORYFLAGS_SUPPORTS_Z_OR_M;
result = extract_points_from_polygon(self_geom, zCoordinate);
}
return result;
}
static VALUE
method_polygon_exterior_ring(VALUE self)
{
VALUE result;
RGeo_GeometryData* self_data;
const GEOSGeometry* self_geom;
result = Qnil;
self_data = RGEO_GEOMETRY_DATA_PTR(self);
self_geom = self_data->geom;
if (self_geom) {
result = rgeo_wrap_geos_geometry_clone(self_data->factory,
GEOSGetExteriorRing(self_geom),
rgeo_geos_linear_ring_class);
}
return result;
}
static VALUE
method_polygon_num_interior_rings(VALUE self)
{
VALUE result;
RGeo_GeometryData* self_data;
const GEOSGeometry* self_geom;
int num;
result = Qnil;
self_data = RGEO_GEOMETRY_DATA_PTR(self);
self_geom = self_data->geom;
if (self_geom) {
num = GEOSGetNumInteriorRings(self_geom);
if (num >= 0) {
result = INT2NUM(num);
}
}
return result;
}
static VALUE
method_polygon_interior_ring_n(VALUE self, VALUE n)
{
VALUE result;
RGeo_GeometryData* self_data;
const GEOSGeometry* self_geom;
int i;
int num;
result = Qnil;
self_data = RGEO_GEOMETRY_DATA_PTR(self);
self_geom = self_data->geom;
if (self_geom) {
i = RB_NUM2INT(n);
if (i >= 0) {
num = GEOSGetNumInteriorRings(self_geom);
if (i < num) {
result =
rgeo_wrap_geos_geometry_clone(self_data->factory,
GEOSGetInteriorRingN(self_geom, i),
rgeo_geos_linear_ring_class);
}
}
}
return result;
}
static VALUE
method_polygon_interior_rings(VALUE self)
{
VALUE result;
RGeo_GeometryData* self_data;
const GEOSGeometry* self_geom;
int count;
VALUE factory;
int i;
result = Qnil;
self_data = RGEO_GEOMETRY_DATA_PTR(self);
self_geom = self_data->geom;
if (self_geom) {
count = GEOSGetNumInteriorRings(self_geom);
if (count >= 0) {
result = rb_ary_new2(count);
factory = self_data->factory;
for (i = 0; i < count; ++i) {
rb_ary_store(
result,
i,
rgeo_wrap_geos_geometry_clone(factory,
GEOSGetInteriorRingN(self_geom, i),
rgeo_geos_linear_ring_class));
}
}
}
return result;
}
static VALUE
cmethod_create(VALUE module,
VALUE factory,
VALUE exterior,
VALUE interior_array)
{
VALUE linear_ring_type;
GEOSGeometry* exterior_geom;
unsigned int len;
GEOSGeometry** interior_geoms;
unsigned int actual_len;
unsigned int i;
GEOSGeometry* interior_geom;
GEOSGeometry* polygon;
int state = 0;
Check_Type(interior_array, T_ARRAY);
linear_ring_type = rgeo_feature_linear_ring_module;
exterior_geom = rgeo_convert_to_detached_geos_geometry(
exterior, factory, linear_ring_type, NULL, &state);
if (state) {
if (exterior_geom)
GEOSGeom_destroy(exterior_geom);
rb_exc_raise(rb_errinfo());
}
if (!exterior_geom) {
return Qnil;
}
len = (unsigned int)RARRAY_LEN(interior_array);
interior_geoms = RB_ALLOC_N(GEOSGeometry*, len == 0 ? 1 : len);
if (interior_geoms) {
actual_len = 0;
for (i = 0; i < len; ++i) {
interior_geom =
rgeo_convert_to_detached_geos_geometry(rb_ary_entry(interior_array, i),
factory,
linear_ring_type,
NULL,
&state);
if (state || !interior_geom) {
break;
}
if (interior_geom) {
interior_geoms[actual_len++] = interior_geom;
}
}
if (len == actual_len) {
polygon =
GEOSGeom_createPolygon(exterior_geom, interior_geoms, actual_len);
if (polygon) {
RB_FREE(interior_geoms);
// NOTE: we can return safely here, state cannot be other than 0.
return rgeo_wrap_geos_geometry(
factory, polygon, rgeo_geos_polygon_class);
}
}
for (i = 0; i < actual_len; ++i) {
GEOSGeom_destroy(interior_geoms[i]);
}
RB_FREE(interior_geoms);
}
GEOSGeom_destroy(exterior_geom);
if (state) {
rb_exc_raise(rb_errinfo());
}
return Qnil;
}
void
rgeo_init_geos_polygon()
{
VALUE geos_polygon_methods;
// Class methods for CAPIPolygonImpl
rb_define_module_function(
rgeo_geos_polygon_class, "create", cmethod_create, 3);
// CAPIPolygonMethods module
geos_polygon_methods =
rb_define_module_under(rgeo_geos_module, "CAPIPolygonMethods");
rb_define_method(geos_polygon_methods, "rep_equals?", method_polygon_eql, 1);
rb_define_method(geos_polygon_methods, "eql?", method_polygon_eql, 1);
rb_define_method(geos_polygon_methods, "hash", method_polygon_hash, 0);
rb_define_method(
geos_polygon_methods, "geometry_type", method_polygon_geometry_type, 0);
rb_define_method(geos_polygon_methods, "area", method_polygon_area, 0);
rb_define_method(
geos_polygon_methods, "centroid", method_polygon_centroid, 0);
rb_define_method(geos_polygon_methods,
"point_on_surface",
method_polygon_point_on_surface,
0);
rb_define_method(
geos_polygon_methods, "exterior_ring", method_polygon_exterior_ring, 0);
rb_define_method(geos_polygon_methods,
"num_interior_rings",
method_polygon_num_interior_rings,
0);
rb_define_method(
geos_polygon_methods, "interior_ring_n", method_polygon_interior_ring_n, 1);
rb_define_method(
geos_polygon_methods, "interior_rings", method_polygon_interior_rings, 0);
rb_define_method(
geos_polygon_methods, "coordinates", method_polygon_coordinates, 0);
}
st_index_t
rgeo_geos_polygon_hash(const GEOSGeometry* geom, st_index_t hash)
{
unsigned int len;
unsigned int i;
if (geom) {
hash = rgeo_geos_coordseq_hash(GEOSGetExteriorRing(geom), hash);
len = GEOSGetNumInteriorRings(geom);
for (i = 0; i < len; ++i) {
hash = rgeo_geos_coordseq_hash(GEOSGetInteriorRingN(geom, i), hash);
}
}
return hash;
}
RGEO_END_C
#endif