-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshapetcl.c
3025 lines (2652 loc) · 92.4 KB
/
shapetcl.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
#include <stdio.h>
#include <string.h>
#include "shapefil.h"
#include <tcl.h>
enum {
BASE_POINT,
BASE_ARC,
BASE_POLYGON,
BASE_MULTIPOINT
};
enum {
DIM_XY,
DIM_XYM,
DIM_XYZM
};
/*
* ShapefilePtr
*
* Structure containing shapefile geometry and attribute table handles as well
* as other metadata. Allocated by [shapefile] command on successful open or
* create and subsequently passed to Shapetcl command handlers as ClientData.
*/
struct shapefile_data {
SHPHandle shp;
DBFHandle dbf;
/* Metadata: */
/* True if shapefile is readonly; set by [shapefile] on open/create. */
int readonly;
/* One of the SHPT_ types defined by Shapelib */
int shapeType;
/* Base shape type (point, arc, polygon, or multipoint) */
int baseType;
/* Dimension (XY, XYM, or XYZM) */
int dimType;
/* Base path of shapefile */
char *path;
/* Config Options: */
/* Attempt to write double values that don't fit within field width using
scientific notation. Allows larger values, but may lose sig. digits.
True by default. */
int allowAlternateNotation;
/* Read all four coordinate dimensions (X, Y, Z, and M) regardless of shape
type. Applies to [coords read] and [bounds] commands. False by default.
Only one of getAllCoords and getOnlyXyCoords may be true. */
int getAllCoords;
/* Read only the X and Y coordinate dimensions regardless of shape type.
Applies to [coords read] and [bounds] commands. False by default.
Only one of getAllCoords and getOnlyXyCoords may be true. */
int getOnlyXyCoords;
/* Read all attribute values as strings regardless of field type. Intended
as an aid to debugging attribute table issues. False by default. */
int readRawStrings;
/* Append a final vertex to polygon parts if provided last vertex is not
identifical to first vertex OR if only 3 vertices are given. Appended
vertex is a copy of the part's first vertex. False by default. */
int autoClosePolygons;
/* Allow attribute values that exceed the allocated field width to be
truncated when written, rather than generating an error. If both
allowTruncation and allowAlternateNotation are true, alternate notation
will be attempted before truncating large double values. */
int allowTruncation;
};
typedef struct shapefile_data * ShapefilePtr;
/*
* Counter used to generate unique names for the ensemble command identifiers
* generated by the [shapefile] command. Incremented by [shapefile] after open.
*/
static int COMMAND_COUNT = 0;
TCL_DECLARE_MUTEX(COMMAND_COUNT_MUTEX);
/*
* Size of string buffer to use for formatting and measuring numeric values.
* Should be sufficiently big to fit any encountered value, including decimals.
*/
#define NUMERIC_BUFFER_SIZE 64
int Shapetcl_Init(Tcl_Interp *interp);
int shapefile_cmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]);
int shapefile_typeSupported(int shpType);
int shapefile_typeCode(const char *shpTypeName);
int shapefile_typeBase(int shpType);
int shapefile_typeDimension(int shpType);
int cmd_dispatcher(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]);
int cmd_close(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]);
void shapefile_exit_handler(ClientData clientData);
void shapefile_delete_handler(ClientData clientData);
int cmd_config(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]);
int cmd_file(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]);
int cmd_info(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]);
int cmd_info_count(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]);
int cmd_info_type(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]);
int cmd_info_bounds(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]);
int cmd_fields(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]);
int cmd_fields_add(Tcl_Interp *interp, DBFHandle dbf, int validate, Tcl_Obj *definitions, Tcl_Obj *attrList, ShapefilePtr shapefile);
int cmd_fields_validate(Tcl_Interp *interp, Tcl_Obj *definitions, DBFHandle dbf);
int cmd_fields_validateField(Tcl_Interp *interp, const char *type, const char *name, int width, int precision);
int cmd_fields_validateFieldName(Tcl_Interp *interp, const char *name);
int cmd_fields_description(Tcl_Interp *interp, ShapefilePtr shapefile, int fieldId);
int cmd_fields_index(Tcl_Interp *interp, ShapefilePtr shapefile, const char *fieldName);
int cmd_coordinates(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]);
int cmd_coordinates_write(Tcl_Interp *interp, ShapefilePtr shapefile, int featureId, Tcl_Obj *coordParts);
int cmd_coordinates_writeNull(Tcl_Interp *interp, ShapefilePtr shapefile, int featureId);
int cmd_coordinates_readAll(Tcl_Interp *interp, ShapefilePtr shapefile);
int cmd_coordinates_read(Tcl_Interp *interp, ShapefilePtr shapefile, int featureId);
int cmd_attributes(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]);
int cmd_attributes_write(Tcl_Interp *interp, ShapefilePtr shapefile, int recordId, int validate, Tcl_Obj *attrList);
int cmd_attributes_writeNull(Tcl_Interp *interp, ShapefilePtr shapefile, int recordId);
int cmd_attributes_writeField(Tcl_Interp *interp, ShapefilePtr shapefile, int recordId, int fieldId, int validate, Tcl_Obj *attrValue);
int cmd_attributes_validate(Tcl_Interp *interp, ShapefilePtr shapefile, Tcl_Obj *attrList);
int cmd_attributes_validateField(Tcl_Interp *interp, ShapefilePtr shapefile, int fieldId, Tcl_Obj *attrValue);
int cmd_attributes_readAll(Tcl_Interp *interp, ShapefilePtr shapefile);
int cmd_attributes_read(Tcl_Interp *interp, ShapefilePtr shapefile, int recordId);
int cmd_attributes_readField(Tcl_Interp *interp, ShapefilePtr shapefile, int recordId, int fieldId);
int cmd_attributes_search(Tcl_Interp *interp, ShapefilePtr shapefile, int fieldId, Tcl_Obj *attrValue);
int cmd_write(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]);
/*
* Shapetcl_Init
*
* Invoked by Tcl when the Shapetcl extension is loaded.
*
* Result:
* Registers the [shapefile] command used to open or create shapefiles.
* (Note: the [shapefile] command is created in the ::shapetcl namespace.)
*/
int Shapetcl_Init(Tcl_Interp *interp) {
Tcl_Namespace *shapetclNamespace;
if (Tcl_InitStubs(interp, "8.1", 0) == NULL) {
return TCL_ERROR;
}
if (Tcl_PkgProvide(interp, "shapetcl", "0.1") != TCL_OK) {
return TCL_ERROR;
}
(void)Tcl_CreateObjCommand(interp, "::shapetcl::shapefile", (Tcl_ObjCmdProc *)shapefile_cmd, NULL, NULL);
shapetclNamespace = Tcl_FindNamespace(interp, "shapetcl", NULL, TCL_GLOBAL_ONLY);
Tcl_Export(interp, shapetclNamespace, "shapefile", 0);
return TCL_OK;
}
/*
* shapefile_cmd
*
* Implements the [shapefile] command used to open a new or existing shapefile.
*
* Command Syntax:
* [shapefile PATH ?readonly|readwrite?]
* Open the shapefile at PATH. Default access mode is readwrite.
* [shapefile PATH TYPE FIELDSDEFINITION]
* Create a shapefile at PATH. TYPE defines feature geometry type. FIELDS
* defines initial attribute table format. At least one field is required.
* See the [fields] command for details on FIELDSDEFINITION format.
*
* Result:
* Name of an ensemble command for subsequent operations on the shapefile.
*/
int shapefile_cmd(
ClientData clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]) {
ShapefilePtr shapefile;
const char *path;
int readonly = 1;
SHPHandle shp;
DBFHandle dbf;
int shpType;
Tcl_Obj *cmdNameObj;
Tcl_Namespace *ns;
if (objc < 2 || objc > 4) {
Tcl_WrongNumArgs(interp, 1, objv, "path ?mode?|?type fieldDefinitions?");
return TCL_ERROR;
}
path = Tcl_GetString(objv[1]);
if (objc == 3) {
/* opening an existing file, and an access mode is explicitly set */
const char *mode = Tcl_GetString(objv[2]);
if (strcmp(mode, "readonly") == 0) {
readonly = 1;
} else if (strcmp(mode, "readwrite") == 0) {
readonly = 0;
} else {
Tcl_SetObjResult(interp, Tcl_ObjPrintf("invalid mode \"%s\": should be readonly or readwrite", mode));
return TCL_ERROR;
}
}
if (objc == 4) {
/* create a new file; access must be readwrite. */
readonly = 0;
if ((shpType = shapefile_typeCode(Tcl_GetString(objv[2]))) == -1) {
Tcl_SetObjResult(interp, Tcl_ObjPrintf("unrecognized shape type"));
return TCL_ERROR;
}
if (!shapefile_typeSupported(shpType)) {
Tcl_SetObjResult(interp, Tcl_ObjPrintf("unsupported shape type \"%d\"", shpType));
return TCL_ERROR;
}
/* verify that the attribute table field definition looks sensible */
if (cmd_fields_validate(interp, objv[3], NULL /* no dbf yet */) != TCL_OK) {
return TCL_ERROR;
}
if ((dbf = DBFCreate(path)) == NULL) {
Tcl_SetObjResult(interp, Tcl_ObjPrintf("failed to create attribute table for \"%s\"", path));
return TCL_ERROR;
}
/* add pre-validated fields to the dbf */
if (cmd_fields_add(interp, dbf, 0 /* don't validate */, objv[3], NULL, NULL) != TCL_OK) {
DBFClose(dbf);
return TCL_ERROR;
}
if ((shp = SHPCreate(path, shpType)) == NULL) {
Tcl_SetObjResult(interp, Tcl_ObjPrintf("failed to create shapefile for \"%s\"", path));
DBFClose(dbf);
return TCL_ERROR;
}
}
else {
/* open an existing shapefile */
int shpCount, dbfCount;
if ((dbf = DBFOpen(path, readonly ? "rb" : "rb+")) == NULL) {
Tcl_SetObjResult(interp, Tcl_ObjPrintf("failed to open attribute table for \"%s\"", path));
return TCL_ERROR;
}
if (DBFGetFieldCount(dbf) == 0) {
Tcl_SetObjResult(interp, Tcl_ObjPrintf("attribute table for \"%s\" contains no fields", path));
DBFClose(dbf);
return TCL_ERROR;
}
if ((shp = SHPOpen(path, readonly ? "rb" : "rb+")) == NULL) {
Tcl_SetObjResult(interp, Tcl_ObjPrintf("failed to open shapefile for \"%s\"", path));
DBFClose(dbf);
return TCL_ERROR;
}
/* Only types we don't handle are SHPT_NULL and SHPT_MULTIPATCH */
SHPGetInfo(shp, &shpCount, &shpType, NULL, NULL);
if (!shapefile_typeSupported(shpType)) {
Tcl_SetObjResult(interp, Tcl_ObjPrintf("unsupported shape type \"%d\"", shpType));
DBFClose(dbf);
SHPClose(shp);
return TCL_ERROR;
}
/* Valid shapefiles must have matching number of features and attribute records */
dbfCount = DBFGetRecordCount(dbf);
if (dbfCount != shpCount) {
Tcl_SetObjResult(interp, Tcl_ObjPrintf("shapefile feature count (%d) does not match attribute record count (%d)", shpCount, dbfCount));
DBFClose(dbf);
SHPClose(shp);
return TCL_ERROR;
}
}
if ((shapefile = (ShapefilePtr)ckalloc((unsigned int)sizeof(struct shapefile_data))) == NULL) {
Tcl_SetObjResult(interp, Tcl_ObjPrintf("failed to allocate shapefile command data"));;
DBFClose(dbf);
SHPClose(shp);
return TCL_ERROR;
}
shapefile->shp = shp;
shapefile->dbf = dbf;
shapefile->readonly = readonly;
shapefile->allowAlternateNotation = 0;
shapefile->getAllCoords = 0;
shapefile->getOnlyXyCoords = 0;
shapefile->readRawStrings = 0;
shapefile->autoClosePolygons = 0;
shapefile->allowTruncation = 0;
shapefile->shapeType = shpType;
shapefile->baseType = shapefile_typeBase(shpType);
shapefile->dimType = shapefile_typeDimension(shpType);
/* save the path of the shapefile */
shapefile->path = (char *)ckalloc((unsigned int)(strlen(path) + 1));
strcpy(shapefile->path, path);
ns = Tcl_GetCurrentNamespace(interp);
Tcl_MutexLock(&COMMAND_COUNT_MUTEX);
if (ns->parentPtr == NULL) {
cmdNameObj = Tcl_ObjPrintf("::shapefile%d", COMMAND_COUNT++);
} else {
cmdNameObj = Tcl_ObjPrintf("%s::shapefile%d", ns->fullName, COMMAND_COUNT++);
}
Tcl_MutexUnlock(&COMMAND_COUNT_MUTEX);
if (Tcl_CreateObjCommand(interp, Tcl_GetString(cmdNameObj), (Tcl_ObjCmdProc *)cmd_dispatcher, (ClientData)shapefile, (Tcl_CmdDeleteProc *)shapefile_delete_handler) == NULL) {
Tcl_SetObjResult(interp, Tcl_ObjPrintf("failed to create command for %s", Tcl_GetString(cmdNameObj)));
DBFClose(dbf);
SHPClose(shp);
ckfree((char *)shapefile);
return TCL_ERROR;
}
Tcl_CreateExitHandler((Tcl_ExitProc *)shapefile_exit_handler, (ClientData)shapefile);
Tcl_SetObjResult(interp, cmdNameObj);
return TCL_OK;
}
/*
* shapefile_typeSupported
*
* Boolean check whether Shapetcl supports the specified shapefile type code.
*
* Result:
* 1 if supported (any point/arc/polygon/multipoint xy/xym/xyzm combination)
* 0 if unsupported (null, multipatch, or any other value)
*/
int shapefile_typeSupported(int shpType) {
if ( shpType == SHPT_POINT
|| shpType == SHPT_POINTM
|| shpType == SHPT_POINTZ
|| shpType == SHPT_ARC
|| shpType == SHPT_ARCM
|| shpType == SHPT_ARCZ
|| shpType == SHPT_POLYGON
|| shpType == SHPT_POLYGONM
|| shpType == SHPT_POLYGONZ
|| shpType == SHPT_MULTIPOINT
|| shpType == SHPT_MULTIPOINTM
|| shpType == SHPT_MULTIPOINTZ) {
return 1;
}
return 0;
}
/*
* shapefile_typeCode
*
* Look up the shapefile type code corresponding to the given type name.
*
* Result:
* One of the SHPT_ type codes defined in shapefil.h, or -1 if unrecognized.
*/
int shapefile_typeCode(const char *shpTypeName) {
if (strcmp(shpTypeName, "point") == 0) {
return SHPT_POINT;
} else if (strcmp(shpTypeName, "arc") == 0) {
return SHPT_ARC;
} else if (strcmp(shpTypeName, "polygon") == 0) {
return SHPT_POLYGON;
} else if (strcmp(shpTypeName, "multipoint") == 0) {
return SHPT_MULTIPOINT;
} else if (strcmp(shpTypeName, "pointm") == 0) {
return SHPT_POINTM;
} else if (strcmp(shpTypeName, "arcm") == 0) {
return SHPT_ARCM;
} else if (strcmp(shpTypeName, "polygonm") == 0) {
return SHPT_POLYGONM;
} else if (strcmp(shpTypeName, "multipointm") == 0) {
return SHPT_MULTIPOINTM;
} else if (strcmp(shpTypeName, "pointz") == 0) {
return SHPT_POINTZ;
} else if (strcmp(shpTypeName, "arcz") == 0) {
return SHPT_ARCZ;
} else if (strcmp(shpTypeName, "polygonz") == 0) {
return SHPT_POLYGONZ;
} else if (strcmp(shpTypeName, "multipointz") == 0) {
return SHPT_MULTIPOINTZ;
} else if (strcmp(shpTypeName, "multipatch") == 0) {
return SHPT_MULTIPATCH;
} else if (strcmp(shpTypeName, "null") == 0) {
return SHPT_NULL;
}
return -1;
}
/*
* shapefile_typeBase
*
* Return the base type (BASE_POINT, BASE_ARC, BASE_POLYGON, or BASE_MULTIPOINT)
* of the specified shape type, regardless of dimension.
*/
int shapefile_typeBase(int shpType) {
int base = -1;
switch (shpType) {
case SHPT_POINT:
case SHPT_POINTM:
case SHPT_POINTZ:
base = BASE_POINT;
break;
case SHPT_ARC:
case SHPT_ARCM:
case SHPT_ARCZ:
base = BASE_ARC;
break;
case SHPT_POLYGON:
case SHPT_POLYGONM:
case SHPT_POLYGONZ:
base = BASE_POLYGON;
break;
case SHPT_MULTIPOINT:
case SHPT_MULTIPOINTM:
case SHPT_MULTIPOINTZ:
base = BASE_MULTIPOINT;
break;
}
return base;
}
/*
* shapefile_typeDimension
*
* Return the dimension (DIM_XY, DIM_XYM, or DIM_XYZM) of the specified shape
* type, regardless of base type.
*/
int shapefile_typeDimension(int shpType) {
int dimension = -1;
switch (shpType) {
case SHPT_POINT:
case SHPT_ARC:
case SHPT_POLYGON:
case SHPT_MULTIPOINT:
dimension = DIM_XY;
break;
case SHPT_POINTM:
case SHPT_ARCM:
case SHPT_POLYGONM:
case SHPT_MULTIPOINTM:
dimension = DIM_XYM;
break;
case SHPT_POINTZ:
case SHPT_ARCZ:
case SHPT_POLYGONZ:
case SHPT_MULTIPOINTZ:
dimension = DIM_XYZM;
break;
}
return dimension;
}
/*
* cmd_dispatcher
*
* Ensemble command dispatcher handles the shapefile identifier [$shp] returned
* by shapefile_cmd. The clientData is a ShapefilePtr associated with identifier.
*
* Command Syntax:
* [$shp attributes|close|configure|coordinates|fields|info|mode|write ?args?]
* Invokes the function handler associated with selected subcommand.
* Unambiguous abbreviations such as [$shp attr] or [$shp coord] are valid.
*
* Result:
* Result of the selected subcommand.
*/
int cmd_dispatcher(
ClientData clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]) {
int subcommandIndex;
static const char *subcommandNames[] = {
"attributes",
"close",
"configure",
"coordinates",
"fields",
"info",
"file",
"write",
NULL
};
int result;
if (objc < 2) {
Tcl_WrongNumArgs(interp, 1, objv, "subcommand ?args?");
return TCL_ERROR;
}
/* identify subcommand, or set result to error message w/valid cmd list */
if (Tcl_GetIndexFromObj(interp, objv[1], subcommandNames, "subcommand",
0 /* not TCL_EXACT */, &subcommandIndex) != TCL_OK) {
return TCL_ERROR;
}
switch (subcommandIndex) {
case 0: result = cmd_attributes (clientData, interp, objc, objv); break;
case 1: result = cmd_close (clientData, interp, objc, objv); break;
case 2: result = cmd_config (clientData, interp, objc, objv); break;
case 3: result = cmd_coordinates(clientData, interp, objc, objv); break;
case 4: result = cmd_fields (clientData, interp, objc, objv); break;
case 5: result = cmd_info (clientData, interp, objc, objv); break;
case 6: result = cmd_file (clientData, interp, objc, objv); break;
case 7: result = cmd_write (clientData, interp, objc, objv); break;
default:
Tcl_SetObjResult(interp, Tcl_ObjPrintf("invalid subcommand index (%d)", subcommandIndex));
result = TCL_ERROR;
break;
}
return result;
}
/*
* cmd_close
*
* Implements the [$shp close] command used to close shapefile and save changes.
*
* Command Syntax:
* [$shp close]
*
* Result:
* No Tcl return value. Changes to readwrite shapefiles are written to disk.
* $shp command is deleted and associated resources are released.
*/
int cmd_close(
ClientData clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]) {
ShapefilePtr shapefile = (ShapefilePtr)clientData;
if (objc != 2) {
Tcl_WrongNumArgs(interp, 2, objv, NULL);
return TCL_ERROR;
}
shapefile_exit_handler(shapefile);
Tcl_DeleteCommand(interp, Tcl_GetString(objv[0]));
return TCL_OK;
}
/*
* shapefile_exit_handler
*
* Closes the shapefile, writing changes to disk. Invoked by the [$shp close]
* command. Also invoked as exit handler to ensure any open shapefiles are
* properly flushed and closed when the interpreter exits.
*/
void shapefile_exit_handler(ClientData clientData) {
ShapefilePtr shapefile = (ShapefilePtr)clientData;
SHPClose(shapefile->shp);
shapefile->shp = NULL;
DBFClose(shapefile->dbf);
shapefile->dbf = NULL;
ckfree((void *)shapefile->path);
shapefile->path = NULL;
}
/*
* shapefile_delete_handler
*
* Deletes exit handler and releases shapefile resources. Invoked as delete
* handler when [$shp close] command deletes the associated shapefile command.
*/
void shapefile_delete_handler(ClientData clientData) {
Tcl_DeleteExitHandler((Tcl_ExitProc *)shapefile_exit_handler, clientData);
ckfree((char *)clientData);
}
/*
* cmd_config
*
* Implements the [$shp config] command use to get and set shapefile IO options.
*
* Command Syntax:
* [$shp config option]
* Get the current value of the specified option. Option may be abbreviated.
* [$shp config option 0|1]
* Set the value of the specified option to 0 or 1 (boolean options only).
* Some options are incompatible, in which case setting one to true has the
* side effect of setting the other to false (see get*Coordinates options).
*
* Options (Defaults):
* allowAlternateNotation (0)
* getAllCoordinates (0)
* getOnlyXyCoordinates (0)
* readRawStrings (0)
* autoClosePolygons (0)
* allowTruncation (0)
* (See notes in ShapefilePtr struct definition for option details.)
*
* Result:
* Returns boolean value of specified option.
*/
int cmd_config(
ClientData clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]) {
ShapefilePtr shapefile = (ShapefilePtr)clientData;
int optionValue = 0;
int optionIndex;
static const char *optionNames[] = {
"allowAlternateNotation",
"getAllCoordinates",
"getOnlyXyCoordinates",
"readRawStrings",
"autoClosePolygons",
"allowTruncation",
NULL
};
if (objc < 3 || objc > 4) {
Tcl_WrongNumArgs(interp, 2, objv, "option ?booleanValue?");
return TCL_ERROR;
}
if (Tcl_GetIndexFromObj(interp, objv[2], optionNames, "option", 0, &optionIndex) != TCL_OK) {
return TCL_ERROR;
}
if (objc == 4) {
/* all options expected to be boolean */
if ((Tcl_GetIntFromObj(interp, objv[3], &optionValue) != TCL_OK)
|| (optionValue != 0 && optionValue != 1)) {
Tcl_SetObjResult(interp, Tcl_ObjPrintf("invalid option value \"%s\" (should be 0 or 1)", Tcl_GetString(objv[3])));
return TCL_ERROR;
}
}
switch (optionIndex) {
case 0: /* allowAlternateNotation */
if (objc == 4) {
shapefile->allowAlternateNotation = optionValue;
}
Tcl_SetObjResult(interp, Tcl_NewIntObj(shapefile->allowAlternateNotation));
break;
case 1: /* getAllCoords */
if (objc == 4) {
shapefile->getAllCoords = optionValue;
if (optionValue == 1) {
shapefile->getOnlyXyCoords = 0;
}
}
Tcl_SetObjResult(interp, Tcl_NewIntObj(shapefile->getAllCoords));
break;
case 2: /* getOnlyXyCoords */
if (objc == 4) {
shapefile->getOnlyXyCoords = optionValue;
if (optionValue == 1) {
shapefile->getAllCoords = 0;
}
}
Tcl_SetObjResult(interp, Tcl_NewIntObj(shapefile->getOnlyXyCoords));
break;
case 3: /* readRawStrings */
if (objc == 4) {
shapefile->readRawStrings = optionValue;
}
Tcl_SetObjResult(interp, Tcl_NewIntObj(shapefile->readRawStrings));
break;
case 4: /* autoClosePolygons */
if (objc == 4) {
shapefile->autoClosePolygons = optionValue;
}
Tcl_SetObjResult(interp, Tcl_NewIntObj(shapefile->autoClosePolygons));
break;
case 5: /* allowTruncation */
if (objc == 4) {
shapefile->allowTruncation = optionValue;
}
Tcl_SetObjResult(interp, Tcl_NewIntObj(shapefile->allowTruncation));
break;
}
return TCL_OK;
}
/*
* cmd_file
*
* Implements the [$shp file] command use to query read-only information about
* the shapefile itself. See cmd_info for queries about the shapefile content.
*
* Command Syntax:
* [$shp file mode]
* Get shapefile access mode. Result is one of readonly or readwrite.
* [$shp file path]
* Get shapefile path. Not normalized; returned as initially provided.
*
* Result:
* As described under Command Syntax.
*/
int cmd_file(
ClientData clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]) {
ShapefilePtr shapefile = (ShapefilePtr)clientData;
int actionIndex;
static const char *actionNames[] = {"mode", "path", NULL};
if (objc != 3) {
Tcl_WrongNumArgs(interp, 2, objv, "option");
return TCL_ERROR;
}
if (Tcl_GetIndexFromObj(interp, objv[2], actionNames, "option",
0 /* not TCL_EXACT */, &actionIndex) != TCL_OK) {
return TCL_ERROR;
}
switch (actionIndex) {
case 0: /* mode */
if (shapefile->readonly) {
Tcl_SetObjResult(interp, Tcl_ObjPrintf("readonly"));
} else {
Tcl_SetObjResult(interp, Tcl_ObjPrintf("readwrite"));
}
break;
case 1: /* path */
Tcl_SetObjResult(interp, Tcl_NewStringObj(shapefile->path, -1));
break;
}
return TCL_OK;
}
/*
* cmd_info
*
* Implements the [$shp info] command used to query read-only information about
* the shapefile content. A dispatcher for subcommand actions.
*
* Command Syntax:
* [$shp info bounds]
* See cmd_info_bounds for details.
* [$shp info count]
* See cmd_info_count for details.
* [$shp info type]
* See cmd_info_type for details.
*
* Result:
* Result of the selected subcommand.
*/
int cmd_info(
ClientData clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]) {
int result = TCL_OK;
int optionIndex;
static const char *optionNames[] = {"bounds", "count", "type", NULL};
if (objc < 3) {
Tcl_WrongNumArgs(interp, 2, objv, "option ?args?");
return TCL_ERROR;
}
if (Tcl_GetIndexFromObj(interp, objv[2], optionNames, "option",
0 /* not TCL_EXACT */, &optionIndex) != TCL_OK) {
return TCL_ERROR;
}
switch (optionIndex) {
case 0: /* bounds */
result = cmd_info_bounds(clientData, interp, objc, objv);
break;
case 1: /* count */
result = cmd_info_count(clientData, interp, objc, objv);
break;
case 2: /* type */
result = cmd_info_type(clientData, interp, objc, objv);
break;
}
return result;
}
/*
* cmd_info_count
*
* Implements the [$shp info count] action used to get the feature/record count.
*
* Command Syntax:
* [$shp info count]
*
* Result:
* Number of features in shapefile.
*/
int cmd_info_count(
ClientData clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]) {
ShapefilePtr shapefile = (ShapefilePtr)clientData;
int shpCount, dbfCount;
if (objc != 3) {
Tcl_WrongNumArgs(interp, 3, objv, NULL);
return TCL_ERROR;
}
SHPGetInfo(shapefile->shp, &shpCount, NULL, NULL, NULL);
dbfCount = DBFGetRecordCount(shapefile->dbf);
if (shpCount != dbfCount) {
Tcl_SetObjResult(interp, Tcl_ObjPrintf("shapefile feature count (%d) does not match attribute table record count (%d)", shpCount, dbfCount));
return TCL_ERROR;
}
Tcl_SetObjResult(interp, Tcl_NewIntObj(shpCount));
return TCL_OK;
}
/*
* cmd_info_type
*
* Implements the [$shp info type] action used to query feature type.
*
* Command Syntax:
* [$shp info type]
* Get the geometry type (one of point, multipoint, arc, polygon, pointm,
* multipointm, arcm, polygonm, pointz, multipointz, arcz, or polygonz).
* [$shp info type numeric]
* Get the (non-sequential id) number of the geometry type.
* [$shp info type base]
* Get the base geometry type (one of point, multipoint, arc, or polygon).
* [$shp info type dimension]
* Get the geometry dimension (one of xy, xym, or xyzm).
*
* Result:
* Shapefile geometry type, as described under Command Syntax above.
*/
int cmd_info_type(
ClientData clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]) {
ShapefilePtr shapefile = (ShapefilePtr)clientData;
int actionIndex;
static const char *actionNames[] = {
"base",
"dimensions",
"numeric",
NULL
};
if (objc > 4) {
Tcl_WrongNumArgs(interp, 3, objv, "?option?");
return TCL_ERROR;
}
if (objc == 3) {
/* actionIndex 3 for default case of no special type requested */
actionIndex = 3;
} else {
if (Tcl_GetIndexFromObj(interp, objv[3], actionNames, "option", 0, &actionIndex) != TCL_OK) {
return TCL_ERROR;
}
}
switch (actionIndex) {
case 0:
/* base */
switch (shapefile->baseType) {
case BASE_POINT:
Tcl_SetObjResult(interp, Tcl_ObjPrintf("point"));
break;
case BASE_ARC:
Tcl_SetObjResult(interp, Tcl_ObjPrintf("arc"));
break;
case BASE_POLYGON:
Tcl_SetObjResult(interp, Tcl_ObjPrintf("polygon"));
break;
case BASE_MULTIPOINT:
Tcl_SetObjResult(interp, Tcl_ObjPrintf("multipoint"));
break;
}
break;
case 1:
/* dimension */
switch (shapefile->dimType) {
case DIM_XY:
Tcl_SetObjResult(interp, Tcl_ObjPrintf("xy"));
break;
case DIM_XYM:
Tcl_SetObjResult(interp, Tcl_ObjPrintf("xym"));
break;
case DIM_XYZM:
Tcl_SetObjResult(interp, Tcl_ObjPrintf("xyzm"));
break;
}
break;
case 2:
/* numeric */
Tcl_SetObjResult(interp, Tcl_NewIntObj(shapefile->shapeType));
break;
case 3:
default:
/* normal */
switch (shapefile->shapeType) {
case SHPT_POINT:
Tcl_SetObjResult(interp, Tcl_ObjPrintf("point"));
break;
case SHPT_ARC:
Tcl_SetObjResult(interp, Tcl_ObjPrintf("arc"));
break;
case SHPT_POLYGON:
Tcl_SetObjResult(interp, Tcl_ObjPrintf("polygon"));
break;
case SHPT_MULTIPOINT:
Tcl_SetObjResult(interp, Tcl_ObjPrintf("multipoint"));
break;
case SHPT_POINTM:
Tcl_SetObjResult(interp, Tcl_ObjPrintf("pointm"));
break;
case SHPT_ARCM:
Tcl_SetObjResult(interp, Tcl_ObjPrintf("arcm"));
break;
case SHPT_POLYGONM:
Tcl_SetObjResult(interp, Tcl_ObjPrintf("polygonm"));
break;
case SHPT_MULTIPOINTM:
Tcl_SetObjResult(interp, Tcl_ObjPrintf("multipointm"));
break;
case SHPT_POINTZ:
Tcl_SetObjResult(interp, Tcl_ObjPrintf("pointz"));
break;
case SHPT_ARCZ:
Tcl_SetObjResult(interp, Tcl_ObjPrintf("arcz"));
break;
case SHPT_POLYGONZ:
Tcl_SetObjResult(interp, Tcl_ObjPrintf("polygonz"));
break;
case SHPT_MULTIPOINTZ:
Tcl_SetObjResult(interp, Tcl_ObjPrintf("multipointz"));
break;
}
break;
}
return TCL_OK;
}
/*
* cmd_info_bounds
*
* Implements the [$shp info bounds] action used to get file or feature extent.
*
* Command Syntax:
* [$shp info bounds]
* Get the bounding box of all features in the shapefile.
* [$shp info bounds FEATURE]
* Get the bounding box of the specified feature.
*
* Config Options:
* Bounds are normally given as minimum and maximum XY, XYM, or XYZM coords
* depending on feature type. However, if the getAllCoords or getOnlyXyCoords
* config options are true, the requested coordinate bounds will be returned
* regardless of feature type.
*
* Result:
* List containing the minimum and maximum vertex values.
*
* Example:
* {Xmin Ymin Xmax Ymax}
*/
int cmd_info_bounds(