forked from libgeos/php-geos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geos.c
executable file
·3439 lines (2775 loc) · 87.8 KB
/
geos.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***********************************************************************
*
* GEOS - Geometry Engine Open Source
* http://trac.osgeo.org/geos
*
* Copyright (C) 2010 Sandro Santilli <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*
***********************************************************************/
/* PHP stuff */
#include "php.h"
#include "ext/standard/info.h" /* for php_info_... */
#include "Zend/zend_exceptions.h" /* for zend_throw_exception_object */
/* GEOS stuff */
#include "geos_c.h"
/* Own stuff */
#include "php_geos.h"
#include "arginfo.h"
static ZEND_DECLARE_MODULE_GLOBALS(geos);
static PHP_GINIT_FUNCTION(geos);
PHP_MINIT_FUNCTION(geos);
PHP_MSHUTDOWN_FUNCTION(geos);
PHP_RINIT_FUNCTION(geos);
PHP_RSHUTDOWN_FUNCTION(geos);
PHP_MINFO_FUNCTION(geos);
PHP_FUNCTION(GEOSVersion);
PHP_FUNCTION(GEOSPolygonize);
PHP_FUNCTION(GEOSLineMerge);
#ifdef HAVE_GEOS_SHARED_PATHS
PHP_FUNCTION(GEOSSharedPaths);
#endif
#ifdef HAVE_GEOS_RELATE_PATTERN_MATCH
PHP_FUNCTION(GEOSRelateMatch);
#endif
#if PHP_VERSION_ID < 50399
#define zend_function_entry function_entry
#endif
#if PHP_VERSION_ID >= 70000
# define GEOS_PHP_DTOR_OBJECT zend_object
# define zend_object_value zend_object *
# define zend_uint size_t
# define MAKE_STD_ZVAL(x) x = emalloc(sizeof(zval))
# define GEOS_PHP_RETURN_STRING(x) { RETVAL_STRING((x)); efree((x)); return; }
# define GEOS_PHP_RETURN_STRINGL(x,s) { RETVAL_STRINGL((x),(s)); efree((x)); return; }
# define GEOS_PHP_ADD_ASSOC_ARRAY(a,k,v) { add_assoc_string((a), (k), (v)); efree((v)); }
# define GEOS_PHP_ADD_ASSOC_ZVAL(a,k,v) { add_assoc_zval((a), (k), (v)); efree((v)); }
# define GEOS_PHP_HASH_GET_CUR_KEY(s,k,i) zend_hash_get_current_key((s), (k), (i))
# define GEOS_PHP_HASH_GET_CUR_DATA(h,d) ( d = zend_hash_get_current_data((h)) )
# define GEOS_PHP_ZVAL zval *
#else /* PHP_VERSION_ID < 70000 */
# define GEOS_PHP_DTOR_OBJECT void
# define GEOS_PHP_RETURN_STRING(x) RETURN_STRING((x),0)
# define GEOS_PHP_RETURN_STRINGL(x,s) RETURN_STRINGL((x),(s),0)
# define GEOS_PHP_ADD_ASSOC_ARRAY(a,k,v) add_assoc_string((a), (k), (v), 0)
# define GEOS_PHP_ADD_ASSOC_ZVAL(a,k,v) add_assoc_zval((a), (k), (v))
# define GEOS_PHP_HASH_GET_CUR_KEY(s,k,i) zend_hash_get_current_key((s), (k), (i), 0)
# define zend_string char
# define ZSTR_VAL(x) (x)
# define GEOS_PHP_HASH_GET_CUR_DATA(h,d) zend_hash_get_current_data((h),(void**)&(d))
# define GEOS_PHP_ZVAL zval **
#endif
static zend_function_entry geos_functions[] = {
PHP_FE(GEOSVersion, arginfo_GEOSVersion)
PHP_FE(GEOSPolygonize, arginfo_GEOSPolygonize)
PHP_FE(GEOSLineMerge, arginfo_GEOSLineMerge)
# ifdef HAVE_GEOS_SHARED_PATHS
PHP_FE(GEOSSharedPaths, arginfo_GEOSSharedPaths)
# endif
# ifdef HAVE_GEOS_RELATE_PATTERN_MATCH
PHP_FE(GEOSRelateMatch, arginfo_GEOSRelateMatch)
# endif
#ifdef PHP_FE_END
PHP_FE_END
#else
{NULL, NULL, NULL}
#endif
};
zend_module_entry geos_module_entry = {
STANDARD_MODULE_HEADER,
PHP_GEOS_EXTNAME,
geos_functions,
PHP_MINIT(geos), /* module init function */
PHP_MSHUTDOWN(geos), /* module shutdown function */
PHP_RINIT(geos), /* request init function */
PHP_RSHUTDOWN(geos), /* request shutdown function */
PHP_MINFO(geos), /* module info function */
PHP_GEOS_VERSION,
PHP_MODULE_GLOBALS(geos), /* globals descriptor */
PHP_GINIT(geos), /* globals ctor */
NULL, /* globals dtor */
NULL, /* post deactivate */
STANDARD_MODULE_PROPERTIES_EX
};
#ifdef COMPILE_DL_GEOS
ZEND_GET_MODULE(geos)
#endif
/* -- Utility functions ---------------------- */
static void noticeHandler(const char *fmt, ...)
{
TSRMLS_FETCH();
char message[256];
va_list args;
va_start(args, fmt);
vsnprintf(message, sizeof(message) - 1, fmt, args);
va_end(args);
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "%s", message);
}
static void errorHandler(const char *fmt, ...)
{
TSRMLS_FETCH();
char message[256];
va_list args;
va_start(args, fmt);
vsnprintf(message, sizeof(message) - 1, fmt, args);
va_end(args);
/* TODO: use a GEOSException ? */
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C),
1 TSRMLS_CC, "%s", message);
}
typedef struct Proxy_t {
#if PHP_VERSION_ID >= 70000
int id;
void* relay;
zend_object std;
#else
zend_object std;
void* relay;
#endif
} Proxy;
#if PHP_VERSION_ID >= 70000
static inline Proxy *php_geos_fetch_object(zend_object *obj) {
return (Proxy *)((char *) obj - XtOffsetOf(Proxy, std));
}
# define Z_GEOS_OBJ_P(zv) (Proxy *)((char *) (Z_OBJ_P(zv)) - XtOffsetOf(Proxy, std))
#else
# ifdef Z_OBJ
# define Z_GEOS_OBJ_P(zv) (Proxy*)Z_OBJ(*val TSRMLS_CC)
# else
# define Z_GEOS_OBJ_P(zv) (Proxy*)zend_object_store_get_object(val TSRMLS_CC)
# endif
#endif
static void
setRelay(zval* val, void* obj) {
TSRMLS_FETCH();
Proxy* proxy = Z_GEOS_OBJ_P(val);
proxy->relay = obj;
}
static inline void *
getRelay(zval* val, zend_class_entry* ce) {
TSRMLS_FETCH();
Proxy* proxy = Z_GEOS_OBJ_P(val);
if ( proxy->std.ce != ce ) {
php_error_docref(NULL TSRMLS_CC, E_ERROR,
"Relay object is not an %s", ce->name);
}
if ( ! proxy->relay ) {
php_error_docref(NULL TSRMLS_CC, E_ERROR,
"Relay object for object of type %s is not set", ce->name);
}
return proxy->relay;
}
static long getZvalAsLong(GEOS_PHP_ZVAL val)
{
long ret;
zval tmp;
#if PHP_VERSION_ID >= 70000
tmp = *val;
#else
tmp = **val;
#endif
zval_copy_ctor(&tmp);
convert_to_long(&tmp);
ret = Z_LVAL(tmp);
zval_dtor(&tmp);
return ret;
}
static long getZvalAsDouble(GEOS_PHP_ZVAL val)
{
double ret;
zval tmp;
#if PHP_VERSION_ID >= 70000
tmp = *val;
#else
tmp = **val;
#endif
zval_copy_ctor(&tmp);
convert_to_double(&tmp);
ret = Z_DVAL(tmp);
zval_dtor(&tmp);
return ret;
}
static zend_object_value
Gen_create_obj (zend_class_entry *type,
void (*dtor)(GEOS_PHP_DTOR_OBJECT *object TSRMLS_DC),
zend_object_handlers* handlers)
{
TSRMLS_FETCH();
#if PHP_VERSION_ID >= 70000
Proxy *obj = (Proxy *) ecalloc(1, sizeof(Proxy) + zend_object_properties_size(type));
zend_object_std_init(&obj->std, type TSRMLS_CC);
object_properties_init(&obj->std, type);
obj->std.handlers = handlers;
/* TODO: install the destructor ? (dtor) ! */
/* TODO: do not allocate a full Proxy if we're going to use an object */
return &obj->std;
#else /* PHP_VERSION_ID < 70000 */
zend_object_value retval;
Proxy *obj = (Proxy *)ecalloc(1, sizeof(Proxy));
obj->std.ce = type;
ALLOC_HASHTABLE(obj->std.properties);
zend_hash_init(obj->std.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
#if PHP_VERSION_ID < 50399
zend_hash_copy(obj->std.properties, &type->default_properties,
(copy_ctor_func_t)zval_add_ref, NULL, sizeof(zval *));
#else
object_properties_init(&(obj->std), type);
#endif
retval.handle = zend_objects_store_put(obj, NULL, dtor, NULL TSRMLS_CC);
retval.handlers = handlers;
return retval;
#endif /* PHP_VERSION_ID < 70000 */
}
/* -- class GEOSGeometry -------------------- */
PHP_METHOD(Geometry, __construct);
PHP_METHOD(Geometry, __toString);
PHP_METHOD(Geometry, project);
PHP_METHOD(Geometry, interpolate);
PHP_METHOD(Geometry, buffer);
#ifdef HAVE_GEOS_OFFSET_CURVE
PHP_METHOD(Geometry, offsetCurve);
#endif
PHP_METHOD(Geometry, envelope);
PHP_METHOD(Geometry, intersection);
PHP_METHOD(Geometry, convexHull);
PHP_METHOD(Geometry, difference);
PHP_METHOD(Geometry, symDifference);
PHP_METHOD(Geometry, boundary);
PHP_METHOD(Geometry, union); /* also does union cascaded */
PHP_METHOD(Geometry, pointOnSurface);
PHP_METHOD(Geometry, centroid);
PHP_METHOD(Geometry, relate);
#ifdef HAVE_GEOS_RELATE_BOUNDARY_NODE_RULE
PHP_METHOD(Geometry, relateBoundaryNodeRule);
#endif
PHP_METHOD(Geometry, simplify); /* also does topology-preserving */
PHP_METHOD(Geometry, normalize);
#ifdef HAVE_GEOS_GEOM_SET_PRECISION
PHP_METHOD(Geometry, setPrecision);
#endif
#ifdef HAVE_GEOS_GEOM_GET_PRECISION
PHP_METHOD(Geometry, getPrecision);
#endif
#ifdef HAVE_GEOS_GEOM_EXTRACT_UNIQUE_POINTS
PHP_METHOD(Geometry, extractUniquePoints);
#endif
PHP_METHOD(Geometry, disjoint);
PHP_METHOD(Geometry, touches);
PHP_METHOD(Geometry, intersects);
PHP_METHOD(Geometry, crosses);
PHP_METHOD(Geometry, within);
PHP_METHOD(Geometry, contains);
PHP_METHOD(Geometry, overlaps);
#ifdef HAVE_GEOS_COVERS
PHP_METHOD(Geometry, covers);
#endif
#ifdef HAVE_GEOS_COVERED_BY
PHP_METHOD(Geometry, coveredBy);
#endif
PHP_METHOD(Geometry, equals);
PHP_METHOD(Geometry, equalsExact);
PHP_METHOD(Geometry, isEmpty);
#ifdef HAVE_GEOS_IS_VALID_DETAIL
PHP_METHOD(Geometry, checkValidity);
#endif
PHP_METHOD(Geometry, isSimple);
PHP_METHOD(Geometry, isRing);
PHP_METHOD(Geometry, hasZ);
#ifdef HAVE_GEOS_IS_CLOSED
PHP_METHOD(Geometry, isClosed);
#endif
PHP_METHOD(Geometry, typeName);
PHP_METHOD(Geometry, typeId);
PHP_METHOD(Geometry, getSRID);
PHP_METHOD(Geometry, setSRID);
PHP_METHOD(Geometry, numGeometries);
PHP_METHOD(Geometry, geometryN);
PHP_METHOD(Geometry, numInteriorRings);
#ifdef HAVE_GEOS_GEOM_GET_NUM_POINTS
PHP_METHOD(Geometry, numPoints);
#endif
#ifdef HAVE_GEOS_GEOM_GET_X
PHP_METHOD(Geometry, getX);
#endif
#ifdef HAVE_GEOS_GEOM_GET_Y
PHP_METHOD(Geometry, getY);
#endif
PHP_METHOD(Geometry, interiorRingN);
PHP_METHOD(Geometry, exteriorRing);
PHP_METHOD(Geometry, numCoordinates);
PHP_METHOD(Geometry, dimension);
#ifdef HAVE_GEOS_GEOM_GET_COORDINATE_DIMENSION
PHP_METHOD(Geometry, coordinateDimension);
#endif
#ifdef HAVE_GEOS_GEOM_GET_POINT_N
PHP_METHOD(Geometry, pointN);
#endif
#ifdef HAVE_GEOS_GEOM_GET_START_POINT
PHP_METHOD(Geometry, startPoint);
#endif
#ifdef HAVE_GEOS_GEOM_GET_END_POINT
PHP_METHOD(Geometry, endPoint);
#endif
PHP_METHOD(Geometry, area);
PHP_METHOD(Geometry, length);
PHP_METHOD(Geometry, distance);
PHP_METHOD(Geometry, hausdorffDistance);
#ifdef HAVE_GEOS_SNAP
PHP_METHOD(Geometry, snapTo);
#endif
#ifdef HAVE_GEOS_NODE
PHP_METHOD(Geometry, node);
#endif
#ifdef HAVE_GEOS_DELAUNAY_TRIANGULATION
PHP_METHOD(Geometry, delaunayTriangulation);
#endif
#ifdef HAVE_GEOS_VORONOI_DIAGRAM
PHP_METHOD(Geometry, voronoiDiagram);
#endif
#ifdef HAVE_GEOS_CLIP_BY_RECT
PHP_METHOD(Geometry, clipByRect);
#endif
static zend_function_entry Geometry_methods[] = {
PHP_ME(Geometry, __construct, arginfo_Geometry_construct, 0)
PHP_ME(Geometry, __toString, arginfo_Geometry_toString, 0)
PHP_ME(Geometry, project, arginfo_Geometry_project, 0)
PHP_ME(Geometry, interpolate, arginfo_Geometry_interpolate, 0)
PHP_ME(Geometry, buffer, arginfo_Geometry_buffer, 0)
# ifdef HAVE_GEOS_OFFSET_CURVE
PHP_ME(Geometry, offsetCurve, arginfo_Geometry_offsetCurve, 0)
# endif
PHP_ME(Geometry, envelope, arginfo_Geometry_envelope, 0)
PHP_ME(Geometry, intersection, arginfo_Geometry_intersection, 0)
PHP_ME(Geometry, convexHull, arginfo_Geometry_convexHull, 0)
PHP_ME(Geometry, difference, arginfo_Geometry_difference, 0)
PHP_ME(Geometry, symDifference, arginfo_Geometry_symDifference, 0)
PHP_ME(Geometry, boundary, arginfo_Geometry_boundary, 0)
PHP_ME(Geometry, union, arginfo_Geometry_union, 0)
PHP_ME(Geometry, pointOnSurface, arginfo_Geometry_pointOnSurface, 0)
PHP_ME(Geometry, centroid, arginfo_Geometry_centroid, 0)
PHP_ME(Geometry, relate, arginfo_Geometry_relate, 0)
# ifdef HAVE_GEOS_RELATE_BOUNDARY_NODE_RULE
PHP_ME(Geometry, relateBoundaryNodeRule, arginfo_Geometry_relateBoundaryNodeRule, 0)
# endif
PHP_ME(Geometry, simplify, arginfo_Geometry_simplify, 0)
PHP_ME(Geometry, normalize, arginfo_Geometry_normalize, 0)
# ifdef HAVE_GEOS_GEOM_SET_PRECISION
PHP_ME(Geometry, setPrecision, arginfo_Geometry_setPrecision, 0)
# endif
# if HAVE_GEOS_GEOM_GET_PRECISION
PHP_ME(Geometry, getPrecision, arginfo_Geometry_getPrecision, 0)
# endif
# ifdef HAVE_GEOS_GEOM_EXTRACT_UNIQUE_POINTS
PHP_ME(Geometry, extractUniquePoints, arginfo_Geometry_extractUniquePoints, 0)
# endif
PHP_ME(Geometry, disjoint, arginfo_Geometry_disjoint, 0)
PHP_ME(Geometry, touches, arginfo_Geometry_touches, 0)
PHP_ME(Geometry, intersects, arginfo_Geometry_intersects, 0)
PHP_ME(Geometry, crosses, arginfo_Geometry_crosses, 0)
PHP_ME(Geometry, within, arginfo_Geometry_within, 0)
PHP_ME(Geometry, contains, arginfo_Geometry_contains, 0)
PHP_ME(Geometry, overlaps, arginfo_Geometry_overlaps, 0)
# ifdef HAVE_GEOS_COVERS
PHP_ME(Geometry, covers, arginfo_Geometry_covers, 0)
# endif
# ifdef HAVE_GEOS_COVERED_BY
PHP_ME(Geometry, coveredBy, arginfo_Geometry_coveredBy, 0)
# endif
PHP_ME(Geometry, equals, arginfo_Geometry_equals, 0)
PHP_ME(Geometry, equalsExact, arginfo_Geometry_equalsExact, 0)
PHP_ME(Geometry, isEmpty, arginfo_Geometry_isEmpty, 0)
# ifdef HAVE_GEOS_IS_VALID_DETAIL
PHP_ME(Geometry, checkValidity, arginfo_Geometry_checkValidity, 0)
# endif
PHP_ME(Geometry, isSimple, arginfo_Geometry_isSimple, 0)
PHP_ME(Geometry, isRing, arginfo_Geometry_isRing, 0)
PHP_ME(Geometry, hasZ, arginfo_Geometry_hasZ, 0)
# ifdef HAVE_GEOS_IS_CLOSED
PHP_ME(Geometry, isClosed, arginfo_Geometry_isClosed, 0)
# endif
PHP_ME(Geometry, typeName, arginfo_Geometry_typeName, 0)
PHP_ME(Geometry, typeId, arginfo_Geometry_typeId, 0)
PHP_ME(Geometry, getSRID, arginfo_Geometry_getSRID, 0)
PHP_ME(Geometry, setSRID, arginfo_Geometry_setSRID, 0)
PHP_ME(Geometry, numGeometries, arginfo_Geometry_numGeometries, 0)
PHP_ME(Geometry, geometryN, arginfo_Geometry_geometryN, 0)
PHP_ME(Geometry, numInteriorRings, arginfo_Geometry_numInteriorRings, 0)
# ifdef HAVE_GEOS_GEOM_GET_NUM_POINTS
PHP_ME(Geometry, numPoints, arginfo_Geometry_numPoints, 0)
# endif
# ifdef HAVE_GEOS_GEOM_GET_X
PHP_ME(Geometry, getX, arginfo_Geometry_getX, 0)
# endif
# ifdef HAVE_GEOS_GEOM_GET_Y
PHP_ME(Geometry, getY, arginfo_Geometry_getY, 0)
# endif
PHP_ME(Geometry, interiorRingN, arginfo_Geometry_interiorRingN, 0)
PHP_ME(Geometry, exteriorRing, arginfo_Geometry_exteriorRing, 0)
PHP_ME(Geometry, numCoordinates, arginfo_Geometry_numCoordinates, 0)
PHP_ME(Geometry, dimension, arginfo_Geometry_dimension, 0)
# ifdef HAVE_GEOS_GEOM_GET_COORDINATE_DIMENSION
PHP_ME(Geometry, coordinateDimension, arginfo_Geometry_coordinateDimension, 0)
# endif
# ifdef HAVE_GEOS_GEOM_GET_POINT_N
PHP_ME(Geometry, pointN, arginfo_Geometry_pointN, 0)
# endif
# ifdef HAVE_GEOS_GEOM_GET_START_POINT
PHP_ME(Geometry, startPoint, arginfo_Geometry_startPoint, 0)
# endif
# ifdef HAVE_GEOS_GEOM_GET_END_POINT
PHP_ME(Geometry, endPoint, arginfo_Geometry_endPoint, 0)
# endif
PHP_ME(Geometry, area, arginfo_Geometry_area, 0)
PHP_ME(Geometry, length, arginfo_Geometry_length, 0)
PHP_ME(Geometry, distance, arginfo_Geometry_distance, 0)
PHP_ME(Geometry, hausdorffDistance, arginfo_Geometry_hausdorffDistance, 0)
# if HAVE_GEOS_SNAP
PHP_ME(Geometry, snapTo, arginfo_Geometry_snapTo, 0)
# endif
# ifdef HAVE_GEOS_NODE
PHP_ME(Geometry, node, arginfo_Geometry_node, 0)
# endif
# ifdef HAVE_GEOS_DELAUNAY_TRIANGULATION
PHP_ME(Geometry, delaunayTriangulation, arginfo_Geometry_delaunayTriangulation, 0)
# endif
# ifdef HAVE_GEOS_VORONOI_DIAGRAM
PHP_ME(Geometry, voronoiDiagram, arginfo_Geometry_voronoiDiagram, 0)
# endif
# ifdef HAVE_GEOS_CLIP_BY_RECT
PHP_ME(Geometry, clipByRect, arginfo_Geometry_clipByRect, 0)
# endif
{NULL, NULL, NULL}
};
static zend_class_entry *Geometry_ce_ptr;
static zend_object_handlers Geometry_object_handlers;
/* Geometry serializer */
static GEOSWKBWriter* Geometry_serializer = 0;
static GEOSWKBWriter* getGeometrySerializer()
{
TSRMLS_FETCH();
if ( ! Geometry_serializer ) {
Geometry_serializer = GEOSWKBWriter_create_r(GEOS_G(handle));
GEOSWKBWriter_setIncludeSRID_r(GEOS_G(handle), Geometry_serializer, 1);
GEOSWKBWriter_setOutputDimension_r(GEOS_G(handle), Geometry_serializer, 3);
}
return Geometry_serializer;
}
static void delGeometrySerializer()
{
TSRMLS_FETCH();
if ( Geometry_serializer ) {
GEOSWKBWriter_destroy_r(GEOS_G(handle), Geometry_serializer);
}
}
/* Geometry deserializer */
static GEOSWKBReader* Geometry_deserializer = 0;
static GEOSWKBReader* getGeometryDeserializer()
{
TSRMLS_FETCH();
if ( ! Geometry_deserializer ) {
Geometry_deserializer = GEOSWKBReader_create_r(GEOS_G(handle));
}
return Geometry_deserializer;
}
static void delGeometryDeserializer()
{
TSRMLS_FETCH();
if ( Geometry_deserializer ) {
GEOSWKBReader_destroy_r(GEOS_G(handle), Geometry_deserializer);
}
}
/* Serializer function for GEOSGeometry */
static int
Geometry_serialize(zval *object, unsigned char **buffer, zend_uint *buf_len,
zend_serialize_data *data TSRMLS_DC)
{
GEOSWKBWriter *serializer;
GEOSGeometry *geom;
char* ret;
size_t retsize;
serializer = getGeometrySerializer();
geom = (GEOSGeometry*)getRelay(object, Geometry_ce_ptr);
/* NOTE: we might be fine using binary here */
ret = (char*)GEOSWKBWriter_writeHEX_r(GEOS_G(handle), serializer, geom, &retsize);
/* we'll probably get an exception if ret is null */
if ( ! ret ) return FAILURE;
*buffer = (unsigned char*)estrndup(ret, retsize);
GEOSFree_r(GEOS_G(handle), ret);
*buf_len = retsize;
return SUCCESS;
}
static int
Geometry_deserialize(GEOS_PHP_ZVAL object, zend_class_entry *ce, const unsigned char *buf,
zend_uint buf_len, zend_unserialize_data *data TSRMLS_DC)
{
GEOSWKBReader* deserializer;
GEOSGeometry* geom;
deserializer = getGeometryDeserializer();
geom = GEOSWKBReader_readHEX_r(GEOS_G(handle), deserializer, buf, buf_len);
/* check zend_class_entry being what we expect! */
if ( ce != Geometry_ce_ptr ) {
php_error_docref(NULL TSRMLS_CC, E_ERROR,
"Geometry_deserialize called with unexpected zend_class_entry");
return FAILURE;
}
#if PHP_VERSION_ID >= 70000
object_init_ex(object, ce);
setRelay(object, geom);
#else
object_init_ex(*object, ce);
setRelay(*object, geom);
#endif
return SUCCESS;
}
/*
* Push components of the given geometry
* to the given array zval.
* Components geometries are cloned.
* NOTE: collection components are not descended into
*/
static void
dumpGeometry(GEOSGeometry* g, zval* array)
{
TSRMLS_FETCH();
int ngeoms, i;
ngeoms = GEOSGetNumGeometries_r(GEOS_G(handle), g);
for (i=0; i<ngeoms; ++i)
{
zval *tmp;
GEOSGeometry* cc;
const GEOSGeometry* c = GEOSGetGeometryN_r(GEOS_G(handle), g, i);
if ( ! c ) continue; /* should get an exception */
/* we _need_ to clone as this one is owned by 'g' */
cc = GEOSGeom_clone_r(GEOS_G(handle), c);
if ( ! cc ) continue; /* should get an exception */
MAKE_STD_ZVAL(tmp);
object_init_ex(tmp, Geometry_ce_ptr);
setRelay(tmp, cc);
add_next_index_zval(array, tmp);
#if PHP_VERSION_ID >= 70000
efree(tmp);
#endif
}
}
static void
Geometry_dtor (GEOS_PHP_DTOR_OBJECT *object TSRMLS_DC)
{
#if PHP_VERSION_ID < 70000
Proxy *obj = (Proxy *)object;
#else
Proxy *obj = php_geos_fetch_object(object);
#endif
GEOSGeom_destroy_r(GEOS_G(handle), (GEOSGeometry*)obj->relay);
#if PHP_VERSION_ID >= 70000
//zend_object_std_dtor(&obj->std);
#else
zend_hash_destroy(obj->std.properties);
FREE_HASHTABLE(obj->std.properties);
efree(obj);
#endif
}
static zend_object_value
Geometry_create_obj (zend_class_entry *type TSRMLS_DC)
{
return Gen_create_obj(type, Geometry_dtor, &Geometry_object_handlers);
}
PHP_METHOD(Geometry, __construct)
{
php_error_docref(NULL TSRMLS_CC, E_ERROR,
"GEOSGeometry can't be constructed using new, check WKTReader");
}
PHP_METHOD(Geometry, __toString)
{
GEOSGeometry *geom;
GEOSWKTWriter *writer;
char *wkt;
char *ret;
geom = (GEOSGeometry*)getRelay(getThis(), Geometry_ce_ptr);
writer = GEOSWKTWriter_create_r(GEOS_G(handle));
/* NOTE: if we get an exception before reaching
* GEOSWKTWriter_destory below we'll be leaking memory.
* One fix could be storing the object in a refcounted
* zval.
*/
# ifdef HAVE_GEOS_WKT_WRITER_SET_TRIM
GEOSWKTWriter_setTrim_r(GEOS_G(handle), writer, 1);
# endif
wkt = GEOSWKTWriter_write_r(GEOS_G(handle), writer, geom);
/* we'll probably get an exception if wkt is null */
if ( ! wkt ) RETURN_NULL();
GEOSWKTWriter_destroy_r(GEOS_G(handle), writer);
ret = estrdup(wkt);
GEOSFree_r(GEOS_G(handle), wkt);
GEOS_PHP_RETURN_STRING(ret);
}
PHP_METHOD(Geometry, project)
{
GEOSGeometry *this;
GEOSGeometry *other;
zval *zobj;
zend_bool normalized = 0;
double ret;
this = (GEOSGeometry*)getRelay(getThis(), Geometry_ce_ptr);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o|b", &zobj,
&normalized) == FAILURE) {
RETURN_NULL();
}
other = getRelay(zobj, Geometry_ce_ptr);
if ( normalized ) {
ret = GEOSProjectNormalized_r(GEOS_G(handle), this, other);
} else {
ret = GEOSProject_r(GEOS_G(handle), this, other);
}
if ( ret < 0 ) RETURN_NULL(); /* should get an exception first */
RETURN_DOUBLE(ret);
}
PHP_METHOD(Geometry, interpolate)
{
GEOSGeometry *this;
double dist;
GEOSGeometry *ret;
zend_bool normalized = 0;
this = (GEOSGeometry*)getRelay(getThis(), Geometry_ce_ptr);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d|b",
&dist, &normalized) == FAILURE) {
RETURN_NULL();
}
if ( normalized ) {
ret = GEOSInterpolateNormalized_r(GEOS_G(handle), this, dist);
} else {
ret = GEOSInterpolate_r(GEOS_G(handle), this, dist);
}
if ( ! ret ) RETURN_NULL(); /* should get an exception first */
/* return_value is a zval */
object_init_ex(return_value, Geometry_ce_ptr);
setRelay(return_value, ret);
}
/**
* GEOSGeometry::buffer(dist, [<styleArray>])
*
* styleArray keys supported:
* 'quad_segs'
* Type: int
* Number of segments used to approximate
* a quarter circle (defaults to 8).
* 'endcap'
* Type: long
* Endcap style (defaults to GEOSBUF_CAP_ROUND)
* 'join'
* Type: long
* Join style (defaults to GEOSBUF_JOIN_ROUND)
* 'mitre_limit'
* Type: double
* mitre ratio limit (only affects joins with GEOSBUF_JOIN_MITRE style)
* 'miter_limit' is also accepted as a synonym for 'mitre_limit'.
* 'single_sided'
* Type: bool
* If true buffer lines only on one side, so that the input line
* will be a portion of the boundary of the returned polygon.
* Only applies to lineal input. Defaults to false.
*/
PHP_METHOD(Geometry, buffer)
{
GEOSGeometry *this;
double dist;
GEOSGeometry *ret;
GEOSBufferParams *params;
static const double default_mitreLimit = 5.0;
static const int default_endCapStyle = GEOSBUF_CAP_ROUND;
static const int default_joinStyle = GEOSBUF_JOIN_ROUND;
static const int default_quadSegs = 8;
long int quadSegs = default_quadSegs;
long int endCapStyle = default_endCapStyle;
long int joinStyle = default_joinStyle;
double mitreLimit = default_mitreLimit;
long singleSided = 0;
zval *style_val = NULL;
GEOS_PHP_ZVAL data;
HashTable *style;
zend_string *key;
ulong index;
this = (GEOSGeometry*)getRelay(getThis(), Geometry_ce_ptr);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d|a",
&dist, &style_val) == FAILURE) {
RETURN_NULL();
}
params = GEOSBufferParams_create_r(GEOS_G(handle));
if ( style_val )
{
style = HASH_OF(style_val);
while(GEOS_PHP_HASH_GET_CUR_KEY(style, &key, &index)
== HASH_KEY_IS_STRING)
{
if(!strcmp(ZSTR_VAL(key), "quad_segs"))
{
GEOS_PHP_HASH_GET_CUR_DATA(style, data);
quadSegs = getZvalAsLong(data);
GEOSBufferParams_setQuadrantSegments_r(GEOS_G(handle), params, quadSegs);
}
else if(!strcmp(ZSTR_VAL(key), "endcap"))
{
GEOS_PHP_HASH_GET_CUR_DATA(style, data);
endCapStyle = getZvalAsLong(data);
GEOSBufferParams_setEndCapStyle_r(GEOS_G(handle), params, endCapStyle);
}
else if(!strcmp(ZSTR_VAL(key), "join"))
{
GEOS_PHP_HASH_GET_CUR_DATA(style, data);
joinStyle = getZvalAsLong(data);
GEOSBufferParams_setJoinStyle_r(GEOS_G(handle), params, joinStyle);
}
else if(!strcmp(ZSTR_VAL(key), "mitre_limit"))
{
GEOS_PHP_HASH_GET_CUR_DATA(style, data);
mitreLimit = getZvalAsDouble(data);
GEOSBufferParams_setMitreLimit_r(GEOS_G(handle), params, mitreLimit);
}
else if(!strcmp(ZSTR_VAL(key), "single_sided"))
{
GEOS_PHP_HASH_GET_CUR_DATA(style, data);
singleSided = getZvalAsLong(data);
GEOSBufferParams_setSingleSided_r(GEOS_G(handle), params, singleSided);
}
zend_hash_move_forward(style);
}
}
ret = GEOSBufferWithParams_r(GEOS_G(handle), this, params, dist);
GEOSBufferParams_destroy_r(GEOS_G(handle), params);
if ( ! ret ) RETURN_NULL(); /* should get an exception first */
/* return_value is a zval */
object_init_ex(return_value, Geometry_ce_ptr);
setRelay(return_value, ret);
}
/**
* GEOSGeometry::offsetCurve(dist, [<styleArray>])
*
* styleArray keys supported:
* 'quad_segs'
* Type: int
* Number of segments used to approximate
* a quarter circle (defaults to 8).
* 'join'
* Type: long
* Join style (defaults to GEOSBUF_JOIN_ROUND)
* 'mitre_limit'
* Type: double
* mitre ratio limit (only affects joins with GEOSBUF_JOIN_MITRE style)
* 'miter_limit' is also accepted as a synonym for 'mitre_limit'.
*/
#ifdef HAVE_GEOS_OFFSET_CURVE
PHP_METHOD(Geometry, offsetCurve)
{
GEOSGeometry *this;
double dist;
GEOSGeometry *ret;
static const double default_mitreLimit = 5.0;
static const int default_joinStyle = GEOSBUF_JOIN_ROUND;
static const int default_quadSegs = 8;
long int quadSegs = default_quadSegs;
long int joinStyle = default_joinStyle;
double mitreLimit = default_mitreLimit;
zval *style_val = NULL;
GEOS_PHP_ZVAL data;
HashTable *style;
zend_string *key;
ulong index;
this = (GEOSGeometry*)getRelay(getThis(), Geometry_ce_ptr);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d|a",
&dist, &style_val) == FAILURE) {
RETURN_NULL();
}
if ( style_val )
{
style = HASH_OF(style_val);
while(GEOS_PHP_HASH_GET_CUR_KEY(style, &key, &index)
== HASH_KEY_IS_STRING)
{
if(!strcmp(ZSTR_VAL(key), "quad_segs"))
{
GEOS_PHP_HASH_GET_CUR_DATA(style, data);
quadSegs = getZvalAsLong(data);
}
else if(!strcmp(ZSTR_VAL(key), "join"))
{
GEOS_PHP_HASH_GET_CUR_DATA(style, data);
joinStyle = getZvalAsLong(data);
}
else if(!strcmp(ZSTR_VAL(key), "mitre_limit"))
{
GEOS_PHP_HASH_GET_CUR_DATA(style, data);
mitreLimit = getZvalAsDouble(data);
}
zend_hash_move_forward(style);
}
}
ret = GEOSOffsetCurve_r(GEOS_G(handle), this, dist, quadSegs, joinStyle, mitreLimit);