-
Notifications
You must be signed in to change notification settings - Fork 10
/
ogr.c
executable file
·5944 lines (4943 loc) · 156 KB
/
ogr.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
/******************************************************************************
* Project: PHP Interface for OGR C API
* Purpose: Implementation of PHP wrapper functions.
* Author: Normand Savard, nsavard@dmsolutions.ca
* Author: Edward Nash, e.nash@dvz-mv.de (OSR binding/PHP7-compatibility)
*
******************************************************************************
* Copyright (c) 2003, DM Solutions Group Inc
* Copyright (c) 2019-2021, DVZ Datenverarbeitungszentrum Mecklenburg-Vorpommern GmbH
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
******************************************************************************
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
/* for NAN: somethimes in math.h, bu not always */
#include "math.h"
#ifndef NAN
#define NAN 0.0/0.0
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_ogr.h"
/* Include OGR C Wrappers declarations */
#include "ogr_srs_api.h"
#include "ogr_api.h"
#include "gdal_version.h"
#include "cpl_error.h"
#include "cpl_conv.h"
#include "cpl_string.h"
/* If you declare any globals in php_ogr.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(ogr)
*/
/* include additional header files for this extension */
#include "php_ogr_shims.h"
#include "php_ogr_api.h"
/* define a macro for returning a (duplicated) CPLString */
#define RETURN_CPL_STRING(str) \
_ZVAL_STRING(return_value, estrdup(str)); \
CPLFree(str); \
return
/* {{{ ogr_module_entry
*/
zend_module_entry ogr_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"ogr",
ogr_functions,
PHP_MINIT(ogr),
PHP_MSHUTDOWN(ogr),
PHP_RINIT(ogr), /* Replace with NULL if there's nothing to do at
request start */
PHP_RSHUTDOWN(ogr), /* Replace with NULL if there's nothing to do at
request end */
PHP_MINFO(ogr),
#if ZEND_MODULE_API_NO >= 20010901
PHP_OGR_VERSION, /* Replace with version number for your extension */
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_OGR
ZEND_GET_MODULE(ogr)
#endif
/* True global resources - no need for thread safety here */
static int le_Datasource;
static int le_SFDriver;
static int le_Layer;
static int le_SpatialReference;
static int le_SpatialReferenceRef;
static int le_CoordinateTransformation;
static int le_Geometry;
static int le_GeometryRef;
static int le_FieldDefn;
static int le_FieldDefnRef;
static int le_Field;
static int le_FieldRef;
static int le_FeatureDefn;
static int le_FeatureDefnRef;
static int le_Feature;
/*Resource destructors function.*/
/* }}} */
/* {{{ ogr_free_Datasource() */
static void
ogr_free_Datasource(zend_resource_t *rsrc TSRMLS_DC)
{
OGRDataSourceH hds = (OGRDataSourceH)rsrc->ptr;
OGR_DS_Destroy( hds );
}
/* }}} */
/* {{{ ogr_free_SFDriver() */
static void
ogr_free_SFDriver(zend_resource_t *rsrc TSRMLS_DC)
{
/* Nothing to do here since the datasource destructor already frees all
* SFDriver.
*/
}
/* }}} */
/* {{{ ogr_free_Layer() */
static void
ogr_free_Layer(zend_resource_t *rsrc TSRMLS_DC)
{
/* Nothing to do here since the datasource destructor already frees all
* layers.
*/
}
/* }}} */
/* {{{ ogr_free_SpatialReference() */
static void
ogr_free_SpatialReference(zend_resource_t *rsrc TSRMLS_DC)
{
OGRSpatialReferenceH hSRS = (OGRSpatialReferenceH)rsrc->ptr;
/* Release rather than Destroy in case OGR still references the object */
OSRRelease( hSRS );
}
/* }}} */
/* {{{ ogr_free_SpatialReferenceRef() */
static void
ogr_free_SpatialReferenceRef(zend_resource_t *rsrc TSRMLS_DC)
{
/* Dummy destructor since this is only a reference on a resource
* and we are not the owner.
*/
}
/* }}} */
/* {{{ ogr_free_CoordinateTransformation() */
static void
ogr_free_CoordinateTransformation(zend_resource_t *rsrc TSRMLS_DC)
{
OGRCoordinateTransformationH hCT = (OGRCoordinateTransformationH)rsrc->ptr;
OCTDestroyCoordinateTransformation(hCT);
}
/* }}} */
/* {{{ ogr_free_Geometry() */
static void
ogr_free_Geometry(zend_resource_t *rsrc TSRMLS_DC)
{
OGRGeometryH hGeom = (OGRGeometryH)rsrc->ptr;
OGR_G_DestroyGeometry( hGeom );
}
/* }}} */
/* {{{ ogr_free_GeometryRef() */
static void
ogr_free_GeometryRef(zend_resource_t *rsrc TSRMLS_DC)
{
/* Dummy destructor since this is only a reference on a resource
* and we are not the owner.
*/
}
/* }}} */
/* {{{ ogr_free_FieldDefn() */
static void
ogr_free_FieldDefn(zend_resource_t *rsrc TSRMLS_DC)
{
OGRFieldDefnH hFieldDefn = (OGRFieldDefnH)rsrc->ptr;
OGR_Fld_Destroy( hFieldDefn );
}
/* }}} */
/* {{{ ogr_free_FieldDefnRef() */
static void
ogr_free_FieldDefnRef(zend_resource_t *rsrc TSRMLS_DC)
{
/* Dummy destructor since this is only a reference on a resource
* and we are not the owner.
*/
}
/* }}} */
/* {{{ ogr_free_Field() */
static void
ogr_free_Field(zend_resource_t *rsrc TSRMLS_DC)
{
/* Nothing to do here since the FieldDefn destructor already frees all
* fields.
*/
}
/* }}} */
/* {{{ ogr_free_FieldRef() */
static void
ogr_free_FieldRef(zend_resource_t *rsrc TSRMLS_DC)
{
/* Dummy destructor since this is only a reference on a resource
* and we are not the owner.
*/
}
/* }}} */
/* {{{ ogr_free_FeatureDefn() */
static void
ogr_free_FeatureDefn(zend_resource_t *rsrc TSRMLS_DC)
{
/* TODO segfaults
OGRFeatureDefnH hFeatureDefn = (OGRFeatureDefnH)rsrc->ptr;
OGR_FD_Destroy( hFeatureDefn );
*/
}
/* }}} */
/* {{{ ogr_free_FeatureDefnRef() */
static void
ogr_free_FeatureDefnRef(zend_resource_t *rsrc TSRMLS_DC)
{
/* Dummy destructor since this is only a reference on a resource
* and we are not the owner.
*/
}
/* }}} */
/* {{{ ogr_free_Feature() */
static void
ogr_free_Feature(zend_resource_t *rsrc TSRMLS_DC)
{
OGRFeatureH hFeature = (OGRFeatureH)rsrc->ptr;
OGR_F_Destroy( hFeature );
}
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(ogr)
{
/* If you have INI entries, uncomment these lines
ZEND_INIT_MODULE_GLOBALS(ogr, php_ogr_init_globals, NULL);
REGISTER_INI_ENTRIES();
*/
le_Datasource = zend_register_list_destructors_ex(ogr_free_Datasource,
NULL, "OGRDatasource",
module_number);
le_SFDriver = zend_register_list_destructors_ex(ogr_free_SFDriver,
NULL, "OGRSFDriver",
module_number);
le_Layer = zend_register_list_destructors_ex(ogr_free_Layer,
NULL, "OGRLayer",
module_number);
le_SpatialReference = zend_register_list_destructors_ex
(ogr_free_SpatialReference,
NULL, "OGRSpatialReference",
module_number);
le_SpatialReferenceRef = zend_register_list_destructors_ex
(ogr_free_SpatialReferenceRef,
NULL, "OGRSpatialReferenceRef",
module_number);
le_CoordinateTransformation = zend_register_list_destructors_ex
(ogr_free_CoordinateTransformation,
NULL, "OGRCoordinateTransformation",
module_number);
le_Geometry = zend_register_list_destructors_ex(ogr_free_Geometry,
NULL, "OGRGeometry",
module_number);
le_GeometryRef = zend_register_list_destructors_ex(ogr_free_GeometryRef,
NULL, "OGRGeometryRef",
module_number);
le_FieldDefn = zend_register_list_destructors_ex(ogr_free_FieldDefn,
NULL, "OGRFieldDefn",
module_number);
le_FieldDefnRef = zend_register_list_destructors_ex(ogr_free_FieldDefnRef,
NULL, "OGRFieldDefnRef",
module_number);
le_Field = zend_register_list_destructors_ex(ogr_free_Field,
NULL, "OGRField",
module_number);
le_FieldRef = zend_register_list_destructors_ex(ogr_free_FieldRef,
NULL, "OGRFieldRef",
module_number);
le_FeatureDefn = zend_register_list_destructors_ex(ogr_free_FeatureDefn,
NULL, "OGRFeatureDefn",
module_number);
le_FeatureDefnRef = zend_register_list_destructors_ex
(ogr_free_FeatureDefnRef,
NULL, "OGRFeatureDefnRef",
module_number);
le_Feature = zend_register_list_destructors_ex(ogr_free_Feature,
NULL, "OGRFeature",
module_number);
/*Register constants*/
#define OGR_CONST_FLAG CONST_CS | CONST_PERSISTENT
REGISTER_LONG_CONSTANT("OGR_VERSION_NUM",
GDAL_VERSION_NUM,
OGR_CONST_FLAG);
REGISTER_STRING_CONSTANT("OGR_RELEASE_NAME",
GDAL_RELEASE_NAME,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("OGRERR_NONE",
OGRERR_NONE,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("OGRERR_NOT_ENOUGH_DATA",
OGRERR_NOT_ENOUGH_DATA,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("OGRERR_NOT_ENOUGH_MEMORY",
OGRERR_NOT_ENOUGH_MEMORY,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("OGRERR_UNSUPPORTED_GEOMETRY_TYPE",
OGRERR_UNSUPPORTED_GEOMETRY_TYPE,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("OGRERR_UNSUPPORTED_OPERATION",
OGRERR_UNSUPPORTED_OPERATION,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("OGRERR_CORRUPT_DATA",
OGRERR_CORRUPT_DATA,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("OGRERR_FAILURE",
OGRERR_FAILURE,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("OGRERR_UNSUPPORTED_SRS",
OGRERR_UNSUPPORTED_SRS,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("wkb25DBit",
wkb25DBit,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("ogrZMarker",
ogrZMarker,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("wkbXDR",
wkbXDR,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("wkbNDR",
wkbNDR,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("wkbUnknown",
wkbUnknown,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("wkbPoint",
wkbPoint,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("wkbLineString",
wkbLineString,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("wkbPolygon",
wkbPolygon,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("wkbMultiPoint",
wkbMultiPoint,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("wkbMultiLineString",
wkbMultiLineString,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("wkbMultiPolygon",
wkbMultiPolygon,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("wkbGeometryCollection",
wkbGeometryCollection,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("wkbNone",
wkbNone,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("wkbLinearRing",
wkbLinearRing,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("wkbPoint25D",
wkbPoint25D,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("wkbLineString25D",
wkbLineString25D,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("wkbPolygon25D",
wkbPolygon25D,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("wkbMultiPoint25D",
wkbMultiPoint25D,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("wkbMultiLineString25D",
wkbMultiLineString25D,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("wkbMultiPolygon25D",
wkbMultiPolygon25D,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("wkbGeometryCollection25D",
wkbGeometryCollection25D,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("OFTInteger",
OFTInteger,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("OFTIntegerList",
OFTIntegerList,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("OFTReal",
OFTReal,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("OFTRealList",
OFTRealList,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("OFTString",
OFTString,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("OFTStringList",
OFTStringList,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("OFTWideString",
OFTWideString,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("OFTWideStringList",
OFTWideStringList,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("OFTBinary",
OFTBinary,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("OJUndefined",
OJUndefined,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("OJLeft",
OJLeft,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("OJRight",
OJRight,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("OGRNullFID",
OGRNullFID,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("OGRUnsetMarker",
OGRUnsetMarker,
OGR_CONST_FLAG);
#if GDAL_VERSION_NUM >= 1630
REGISTER_LONG_CONSTANT("OFTDate",
OFTDate,
OGR_CONST_FLAG);
REGISTER_LONG_CONSTANT("OFTDateTime",
OFTDateTime,
OGR_CONST_FLAG);
#endif
#if GDAL_VERSION_NUM >= 1320
REGISTER_LONG_CONSTANT("OFTTime",
OFTTime,
OGR_CONST_FLAG);
#endif
REGISTER_STRING_CONSTANT("OLCRandomRead",
OLCRandomRead,
OGR_CONST_FLAG);
REGISTER_STRING_CONSTANT("OLCSequentialWrite",
OLCSequentialWrite,
OGR_CONST_FLAG);
REGISTER_STRING_CONSTANT("OLCRandomWrite",
OLCRandomWrite,
OGR_CONST_FLAG);
REGISTER_STRING_CONSTANT("OLCFastSpatialFilter",
OLCFastSpatialFilter,
OGR_CONST_FLAG);
REGISTER_STRING_CONSTANT("OLCFastFeatureCount",
OLCFastFeatureCount,
OGR_CONST_FLAG);
REGISTER_STRING_CONSTANT("OLCFastGetExtent",
OLCFastGetExtent,
OGR_CONST_FLAG);
REGISTER_STRING_CONSTANT("OLCCreateField",
OLCCreateField,
OGR_CONST_FLAG);
REGISTER_STRING_CONSTANT("OLCTransactions",
OLCTransactions,
OGR_CONST_FLAG);
REGISTER_STRING_CONSTANT("ODsCCreateLayer",
ODsCCreateLayer,
OGR_CONST_FLAG);
REGISTER_STRING_CONSTANT("ODrCCreateDataSource",
ODrCCreateDataSource,
OGR_CONST_FLAG);
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(ogr)
{
/* uncomment this line if you have INI entries
UNREGISTER_INI_ENTRIES();
*/
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(ogr)
{
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(ogr)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(ogr)
{
php_info_print_table_start();
php_info_print_table_header(2, "ogr support", "enabled");
php_info_print_table_row(2, "PHP_OGR Version", PHP_OGR_VERSION);
php_info_print_table_row(2, "GDAL/OGR Version", GDAL_RELEASE_NAME);
php_info_print_table_end();
/* Remove comments if you have entries in php.ini
DISPLAY_INI_ENTRIES();
*/
}
/* }}} */
/**********************************************************************
* Support functions
**********************************************************************/
static void php_report_ogr_error(int nErrLevel)
{
if (CPLGetLastErrorMsg() && (CPLGetLastErrorNo() != OGRERR_NONE))
php_error(nErrLevel, "%s", CPLGetLastErrorMsg());
}
static char ** php_array2string(char **papszStrList, zval *refastrvalues)
{
zval_loop_iterator_t tmp = NULL;
_ZVAL_SIMPLE_LOOP_START(refastrvalues, tmp)
convert_to_string_ex(tmp);
papszStrList = (char **) CSLAddString(papszStrList, _ZVAL_LOOP_ITERATOR_STRVAL(tmp));
_ZVAL_SIMPLE_LOOP_END(refastrvalues)
return papszStrList;
}
/**********************************************************************
* Error handling functions
**********************************************************************/
/* }}} */
/* The previous line is meant for vim and emacs, so it can correctly fold and
unfold functions in source code. See the corresponding marks just before
function definition, where the functions purpose is also documented. Please
follow this convention for the convenience of others editing your code.
*/
/* {{{ proto void cplerrorreset()
*/
PHP_FUNCTION(cplerrorreset)
{
if (ZEND_NUM_ARGS() != 0) {
WRONG_PARAM_COUNT;
}
CPLErrorReset();
}
/* }}} */
/* {{{ proto int cplgetlasterrorno()
*/
PHP_FUNCTION(cplgetlasterrorno)
{
if (ZEND_NUM_ARGS() != 0) {
WRONG_PARAM_COUNT;
}
RETURN_LONG(CPLGetLastErrorNo());
}
/* }}} */
/* {{{ proto CPLErr cplgetlasterrortype()
*/
PHP_FUNCTION(cplgetlasterrortype)
{
if (ZEND_NUM_ARGS() != 0) {
WRONG_PARAM_COUNT;
}
RETURN_LONG(CPLGetLastErrorType());
}
/* }}} */
/* {{{ proto int cplgetlasterrormsg()
*/
PHP_FUNCTION(cplgetlasterrormsg)
{
const char *pszMsg;
if (ZEND_NUM_ARGS() != 0) {
WRONG_PARAM_COUNT;
}
if ((pszMsg = CPLGetLastErrorMsg()) != NULL)
_RETURN_DUPLICATED_STRING((char *)pszMsg);
}
/**********************************************************************
* OGR functions
**********************************************************************/
/* }}} */
/* {{{ proto int ogr_g_createfromwkb(string strbydata, resource hsrs,
resource refhnewgeom)
*/
PHP_FUNCTION(ogr_g_createfromwkb)
{
int argc = ZEND_NUM_ARGS();
int hsrs_id = -1;
int refhnewgeom_id = -1;
unsigned char *strbydata = NULL;
strsize_t strbydata_len;
zval *hsrs = NULL;
zval *refhnewgeom = NULL;
OGRGeometryH hNewGeom = NULL;
OGRSpatialReferenceH hSpatialReference = NULL;
OGRErr eErr = OGRERR_FAILURE;
if (zend_parse_parameters(argc TSRMLS_CC, "sr!z/", &strbydata,
&strbydata_len, &hsrs, &refhnewgeom)
== FAILURE)
return;
if (hsrs) {
_ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH, hsrs,
hsrs_id, "OGRSpatialReference",
le_SpatialReference, le_SpatialReferenceRef);
}
eErr = OGR_G_CreateFromWkb(strbydata, hSpatialReference, &hNewGeom,
strbydata_len);
if (eErr != OGRERR_NONE){
php_report_ogr_error(E_WARNING);
}
if (hNewGeom) {
_ZVAL_PTR_DTOR(refhnewgeom);
ZEND_REGISTER_RESOURCE(refhnewgeom, hNewGeom, le_Geometry);
}
RETURN_LONG(eErr);
}
/* }}} */
/* {{{ proto int ogr_g_createfromwkt(string refadata, resource hsrs,
resource refhnewgeom)
*/
PHP_FUNCTION(ogr_g_createfromwkt)
{
int argc = ZEND_NUM_ARGS();
int hsrs_id = -1;
int refhnewgeom_id = -1;
char *refstrdata = NULL;
strsize_t refstrdata_len;
zval *hsrs = NULL;
zval *refhnewgeom = NULL;
OGRGeometryH hNewGeom = NULL;
OGRSpatialReferenceH hSpatialReference = NULL;
OGRErr eErr = OGRERR_FAILURE;
if (zend_parse_parameters(argc TSRMLS_CC, "sr!z/", &refstrdata,
&refstrdata_len, &hsrs, &refhnewgeom)
== FAILURE)
return;
if (hsrs) {
_ZEND_FETCH_RESOURCE2(hSpatialReference, OGRSpatialReferenceH,
hsrs, hsrs_id, "OGRSpatialReference",
le_SpatialReference, le_SpatialReferenceRef);
}
eErr = OGR_G_CreateFromWkt(&refstrdata, hSpatialReference, &hNewGeom);
if (eErr != OGRERR_NONE){
php_report_ogr_error(E_WARNING);
}
if (hNewGeom) {
_ZVAL_PTR_DTOR(refhnewgeom);
ZEND_REGISTER_RESOURCE(refhnewgeom, hNewGeom, le_Geometry);
}
RETURN_LONG(eErr);
}
/* }}} */
/* {{{ proto void ogr_g_destroygeometry(resource hgeom)
*/
PHP_FUNCTION(ogr_g_destroygeometry)
{
int argc = ZEND_NUM_ARGS();
int hgeom_id = -1;
zval *hgeom = NULL;
OGRGeometryH hGeometry = NULL;
if (zend_parse_parameters(argc TSRMLS_CC, "r", &hgeom) == FAILURE)
return;
if (hgeom) {
_ZEND_FETCH_RESOURCE2(hGeometry, OGRGeometryH, hgeom, hgeom_id,
"OGRGeometryH", le_Geometry, le_GeometryRef);
}
if (hGeometry)
_ZEND_FREE_RESOURCE(hgeom);
}
/* }}} */
/* {{{ proto resource ogr_g_creategeometry(int igeometrytype)
*/
PHP_FUNCTION(ogr_g_creategeometry)
{
int argc = ZEND_NUM_ARGS();
zend_long igeometrytype;
OGRGeometryH hGeometry = NULL;
if (zend_parse_parameters(argc TSRMLS_CC, "l", &igeometrytype) == FAILURE)
return;
hGeometry = OGR_G_CreateGeometry(igeometrytype);
if (hGeometry){
ZEND_REGISTER_RESOURCE(return_value, hGeometry, le_Geometry);
}
}
/* }}} */
/* {{{ proto int ogr_g_getdimension(resource hgeom)
*/
PHP_FUNCTION(ogr_g_getdimension)
{
int argc = ZEND_NUM_ARGS();
int hgeom_id = -1;
zval *hgeom = NULL;
OGRGeometryH hGeometry = NULL;
if (zend_parse_parameters(argc TSRMLS_CC, "r", &hgeom) == FAILURE)
return;
if (hgeom) {
_ZEND_FETCH_RESOURCE2(hGeometry, OGRGeometryH, hgeom, hgeom_id,
"OGRGeometryH", le_Geometry, le_GeometryRef);
}
if (hGeometry){
RETURN_LONG(OGR_G_GetDimension(hGeometry));
}
}
/* }}} */
/* {{{ proto int ogr_g_getcoordinatedimension(resource hgeom)
*/
PHP_FUNCTION(ogr_g_getcoordinatedimension)
{
int argc = ZEND_NUM_ARGS();
int hgeom_id = -1;
zval *hgeom = NULL;
OGRGeometryH hGeometry = NULL;
if (zend_parse_parameters(argc TSRMLS_CC, "r", &hgeom) == FAILURE)
return;
if (hgeom) {
_ZEND_FETCH_RESOURCE2(hGeometry, OGRGeometryH, hgeom, hgeom_id,
"OGRGeometryH", le_Geometry, le_GeometryRef);
}
if (hGeometry)
RETURN_LONG(OGR_G_GetCoordinateDimension(hGeometry));
}
/* }}} */
/* {{{ proto resource ogr_g_clone(resource hgeom)
*/
PHP_FUNCTION(ogr_g_clone)
{
int argc = ZEND_NUM_ARGS();
int hgeom_id = -1;
zval *hgeom = NULL;
OGRGeometryH hNewGeom = NULL, hGeometry = NULL;
if (zend_parse_parameters(argc TSRMLS_CC, "r", &hgeom) == FAILURE)
return;
if (hgeom) {
_ZEND_FETCH_RESOURCE2(hGeometry, OGRGeometryH, hgeom, hgeom_id,
"OGRGeometryH", le_Geometry, le_GeometryRef);
}
if (hGeometry)
hNewGeom = OGR_G_Clone(hGeometry);
if (!hNewGeom){
php_report_ogr_error(E_WARNING);
RETURN_NULL();
}
ZEND_REGISTER_RESOURCE(return_value, hNewGeom, le_Geometry);
}
/* }}} */
/* {{{ proto void ogr_g_getenvelope(resource hgeom, object oenvel)
*/
PHP_FUNCTION(ogr_g_getenvelope)
{
int argc = ZEND_NUM_ARGS();
int hgeom_id = -1;
zval *hgeom = NULL;
zval *oenvel = NULL;
OGREnvelope oEnvelope;
OGRGeometryH hGeometry = NULL;
if (zend_parse_parameters(argc TSRMLS_CC, "rz/", &hgeom, &oenvel) ==
FAILURE)
return;
if (hgeom) {
_ZEND_FETCH_RESOURCE2(hGeometry, OGRGeometryH, hgeom, hgeom_id,
"OGRGeometryH", le_Geometry, le_GeometryRef);
}
if (hGeometry)
OGR_G_GetEnvelope(hGeometry, &oEnvelope);
_ZVAL_PTR_DTOR(oenvel);
OBJECT_INIT(oenvel)
add_property_double(oenvel, "minx", oEnvelope.MinX);
add_property_double(oenvel, "maxx", oEnvelope.MaxX);
add_property_double(oenvel, "miny", oEnvelope.MinY);
add_property_double(oenvel, "maxy", oEnvelope.MaxY);
}
/* }}} */
/* {{{ proto int ogr_g_importfromwkb(resource hgeom, string strdata)
*/
PHP_FUNCTION(ogr_g_importfromwkb)
{
int argc = ZEND_NUM_ARGS();
int hgeom_id = -1;
zval *hgeom = NULL;
char *strdata = NULL;
strsize_t strdata_len;
OGRGeometryH hGeometry = NULL;
OGRErr eErr = OGRERR_FAILURE;
if (zend_parse_parameters(argc TSRMLS_CC, "rs", &hgeom, &strdata,
&strdata_len) == FAILURE)
return;
if (hgeom) {
_ZEND_FETCH_RESOURCE2(hGeometry, OGRGeometryH, hgeom, hgeom_id,
"OGRGeometryH", le_Geometry, le_GeometryRef);
}
if (hGeometry)
eErr = OGR_G_ImportFromWkb(hGeometry, strdata, strdata_len);
if (eErr != OGRERR_NONE){
php_report_ogr_error(E_WARNING);
}
RETURN_LONG(eErr);
}
/* }}} */
/* {{{ proto int ogr_g_exporttowkb(resource hgeom, int ibyteorder,
string strdata)
*/
PHP_FUNCTION(ogr_g_exporttowkb)
{
int argc = ZEND_NUM_ARGS();
int hgeom_id = -1;
zend_long ibyteorder;
zval *hgeom = NULL;
zval *strdata = NULL;
char *strwkb = NULL;
int isize;
OGRGeometryH hGeometry = NULL;
OGRErr eErr = OGRERR_FAILURE;
if (zend_parse_parameters(argc TSRMLS_CC, "rlz/", &hgeom, &ibyteorder,
&strdata) == FAILURE)
return;
if (hgeom) {
_ZEND_FETCH_RESOURCE2(hGeometry, OGRGeometryH, hgeom, hgeom_id,
"OGRGeometryH", le_Geometry, le_GeometryRef);
}
if (hGeometry) {
isize = OGR_G_WkbSize(hGeometry);
strwkb = emalloc(isize);
eErr = OGR_G_ExportToWkb(hGeometry, ibyteorder, strwkb);
}
if (eErr != OGRERR_NONE){
php_report_ogr_error(E_WARNING);
}
_ZVAL_PTR_DTOR(strdata);
if (strwkb) {
_ZVAL_STRINGL(strdata, strwkb, isize);
}