-
Notifications
You must be signed in to change notification settings - Fork 66
/
shputils.c
1125 lines (1041 loc) · 39.3 KB
/
shputils.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: Shapelib
* Purpose:
* Altered "shpdump" and "dbfdump" to allow two files to be appended.
* Other Functions:
* Selecting from the DBF before the write occurs.
* Change the UNITS between Feet and Meters and Shift X,Y.
* Clip and Erase boundary. The program only passes thru the
* data once.
*
* Bill Miller North Carolina - Department of Transportation
* Feb. 1997 -- [email protected]
* There was not a lot of time to debug hidden problems;
* And the code is not very well organized or documented.
* The clip/erase function was not well tested.
* Oct. 2000 -- [email protected]
* Fixed the problem when select is using numbers
* larger than short integer. It now reads long integer.
* NOTE: DBF files created using windows NT will read as a string with
* a length of 381 characters. This is a bug in "dbfopen".
*
* Author: Bill Miller ([email protected])
*
******************************************************************************
* Copyright (c) 1999, Frank Warmerdam
*
* SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
******************************************************************************
*
*/
#include "shapefil.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char infile[80], outfile[80], temp[400];
/* Variables for shape files */
SHPHandle hSHP;
SHPHandle hSHPappend;
int nShapeType, nEntities, iPart;
int nShapeTypeAppend, nEntitiesAppend;
SHPObject *psCShape;
double adfBoundsMin[4], adfBoundsMax[4];
/* Variables for DBF files */
DBFHandle hDBF;
DBFHandle hDBFappend;
DBFFieldType iType;
DBFFieldType jType;
char iszTitle[12];
char jszTitle[12];
int *pt; // TODO(schwehr): Danger. Shadowed
char iszFormat[32], iszField[1024];
char jszFormat[32], jszField[1024];
int ti, iWidth, iDecimals;
int tj, jWidth, jDecimals;
/* -------------------------------------------------------------------- */
/* Variables for the DESCRIBE function */
/* -------------------------------------------------------------------- */
bool ilist = false;
bool iall = false;
/* -------------------------------------------------------------------- */
/* Variables for the SELECT function */
/* -------------------------------------------------------------------- */
bool found = false;
bool newdbf = false;
char selectitem[40], *cpt;
long int selectvalues[150], selcount = 0;
bool iselect = false;
int iselectitem = -1;
bool iunselect = false;
/* -------------------------------------------------------------------- */
/* Variables for the CLIP and ERASE functions */
/* -------------------------------------------------------------------- */
double cxmin, cymin, cxmax, cymax;
bool iclip = false;
bool ierase = false;
bool itouch = false;
bool iinside = false;
bool icut = false;
char clipfile[80];
/* -------------------------------------------------------------------- */
/* Variables for the FACTOR function */
/* -------------------------------------------------------------------- */
double infactor, outfactor, factor = 0; /* NO FACTOR */
bool iunit = false;
/* -------------------------------------------------------------------- */
/* Variables for the SHIFT function */
/* -------------------------------------------------------------------- */
double xshift = 0, yshift = 0; /* NO SHIFT */
/* -------------------------------------------------------------------- */
/* Change the extension. If there is any extension on the */
/* filename, strip it off and add the new extension */
/* -------------------------------------------------------------------- */
void setext(char *pt, const char *ext)
{
int i = (int)(strlen(pt) - 1);
for (; i > 0 && pt[i] != '.' && pt[i] != '/' && pt[i] != '\\'; i--)
{
}
if (pt[i] == '.')
pt[i] = '\0';
strcat(pt, ".");
strcat(pt, ext);
}
/************************************************************************/
/* openfiles() */
/************************************************************************/
void openfiles()
{
/* -------------------------------------------------------------------- */
/* Open the DBF file. */
/* -------------------------------------------------------------------- */
setext(infile, "dbf");
hDBF = DBFOpen(infile, "rb");
if (hDBF == NULL)
{
printf("ERROR: Unable to open the input DBF:%s\n", infile);
exit(1);
}
/* -------------------------------------------------------------------- */
/* Open the append DBF file. */
/* -------------------------------------------------------------------- */
if (strcmp(outfile, ""))
{
setext(outfile, "dbf");
hDBFappend = DBFOpen(outfile, "rb+");
newdbf = false;
if (hDBFappend == NULL)
{
newdbf = true;
hDBFappend = DBFCreate(outfile);
if (hDBFappend == NULL)
{
printf("ERROR: Unable to open the append DBF:%s\n", outfile);
exit(1);
}
}
}
/* -------------------------------------------------------------------- */
/* Open the passed shapefile. */
/* -------------------------------------------------------------------- */
setext(infile, "shp");
hSHP = SHPOpen(infile, "rb");
if (hSHP == NULL)
{
printf("ERROR: Unable to open the input shape file:%s\n", infile);
exit(1);
}
SHPGetInfo(hSHP, &nEntities, &nShapeType, NULL, NULL);
/* -------------------------------------------------------------------- */
/* Open the passed append shapefile. */
/* -------------------------------------------------------------------- */
if (strcmp(outfile, ""))
{
setext(outfile, "shp");
hSHPappend = SHPOpen(outfile, "rb+");
if (hSHPappend == NULL)
{
hSHPappend = SHPCreate(outfile, nShapeType);
if (hSHPappend == NULL)
{
printf("ERROR: Unable to open the append shape file:%s\n",
outfile);
exit(1);
}
}
SHPGetInfo(hSHPappend, &nEntitiesAppend, &nShapeTypeAppend, NULL, NULL);
if (nShapeType != nShapeTypeAppend)
{
puts("ERROR: Input and Append shape files are of different types.");
exit(1);
}
}
}
/* -------------------------------------------------------------------- */
/* Find matching fields in the append file. */
/* Output file must have zero records to add any new fields. */
/* -------------------------------------------------------------------- */
void mergefields()
{
ti = DBFGetFieldCount(hDBF);
tj = DBFGetFieldCount(hDBFappend);
/* Create a pointer array for the max # of fields in the output file */
pt = (int *)malloc((ti + tj + 1) * sizeof(int));
for (int i = 0; i < ti; i++)
{
pt[i] = -1; /* Initial pt values to -1 */
}
/* DBF must be empty before adding items */
const int jRecord = DBFGetRecordCount(hDBFappend);
int j;
for (int i = 0; i < ti; i++)
{
iType = DBFGetFieldInfo(hDBF, i, iszTitle, &iWidth, &iDecimals);
found = false;
for (j = 0; j < tj; j++) /* Search all field names for a match */
{
jType =
DBFGetFieldInfo(hDBFappend, j, jszTitle, &jWidth, &jDecimals);
if (iType == jType && (strcmp(iszTitle, jszTitle) == 0))
{
if (found || newdbf)
{
if (i == j)
pt[i] = j;
printf("Warning: Duplicate field name found (%s)\n",
iszTitle);
/* Duplicate field name
(Try to guess the correct field by position) */
}
else
{
pt[i] = j;
found = true;
}
}
}
if (pt[i] == -1 && (!found)) /* Try to force into an existing field */
{ /* Ignore the field name, width, and decimal places */
jType =
DBFGetFieldInfo(hDBFappend, j, jszTitle, &jWidth, &jDecimals);
if (iType == jType)
{
pt[i] = i;
found = true;
}
}
if ((!found) &&
jRecord == 0) /* Add missing field to the append table */
{ /* The output DBF must be is empty */
pt[i] = tj;
tj++;
if (DBFAddField(hDBFappend, iszTitle, iType, iWidth, iDecimals) ==
-1)
{
printf("Warning: DBFAddField(%s, TYPE:%d, WIDTH:%d DEC:%d, "
"ITEM#:%d of %d) failed.\n",
iszTitle, iType, iWidth, iDecimals, (i + 1), (ti + 1));
pt[i] = -1;
}
}
}
}
/************************************************************************/
/* strncasecmp2() */
/* */
/* Compare two strings up to n characters */
/* If n=0 then s1 and s2 must be an exact match */
/************************************************************************/
int strncasecmp2(char *s1, char *s2, int n)
{
if (n < 1)
n = (int)(strlen(s1) + 1);
for (int i = 0; i < n; i++)
{
if (*s1 != *s2)
{
if (*s1 >= 'a' && *s1 <= 'z')
{
const int j = *s1 - 32;
if (j != *s2)
return (*s1 - *s2);
}
else
{
int j;
if (*s1 >= 'A' && *s1 <= 'Z')
{
j = *s1 + 32;
}
else
{
j = *s1;
}
if (j != *s2)
return (*s1 - *s2);
}
}
s1++;
s2++;
}
return (0);
}
void showitems()
{
printf("Available Items: (%d)", ti);
long int maxrec = DBFGetRecordCount(hDBF);
if (maxrec > 5000 && !iall)
{
maxrec = 5000;
printf(" ** ESTIMATED RANGES (MEAN) For more records use \"All\"");
}
else
{
printf(" RANGES (MEAN)");
}
char stmp[40] = {0};
char slow[40] = {0};
char shigh[40] = {0};
for (int i = 0; i < ti; i++)
{
switch (DBFGetFieldInfo(hDBF, i, iszTitle, &iWidth, &iDecimals))
{
case FTString:
case FTLogical:
case FTDate:
strcpy(slow, "~");
strcpy(shigh, "\0");
printf("\n String %3d %-16s", iWidth, iszTitle);
for (int iRecord = 0; iRecord < maxrec; iRecord++)
{
strncpy(stmp, DBFReadStringAttribute(hDBF, iRecord, i), 39);
if (strcmp(stmp, "!!") > 0)
{
if (strncasecmp2(stmp, slow, 0) < 0)
memcpy(slow, stmp, 39);
if (strncasecmp2(stmp, shigh, 0) > 0)
memcpy(shigh, stmp, 39);
}
}
char *pt = slow + strlen(slow) - 1;
while (*pt == ' ')
{
*pt = '\0';
pt--;
}
pt = shigh + strlen(shigh) - 1;
while (*pt == ' ')
{
*pt = '\0';
pt--;
}
if (strncasecmp2(slow, shigh, 0) < 0)
printf("%s to %s", slow, shigh);
else if (strncasecmp2(slow, shigh, 0) == 0)
printf("= %s", slow);
else
printf("No Values");
break;
case FTInteger:
{
printf("\n Integer %3d %-16s", iWidth, iszTitle);
long int ilow = 1999999999;
long int ihigh = -1999999999;
long int isum = 0;
for (int iRecord = 0; iRecord < maxrec; iRecord++)
{
const long int itmp =
DBFReadIntegerAttribute(hDBF, iRecord, i);
if (ilow > itmp)
ilow = itmp;
if (ihigh < itmp)
ihigh = itmp;
isum = isum + itmp;
}
const double mean = isum / maxrec;
if (ilow < ihigh)
printf("%ld to %ld \t(%.1f)", ilow, ihigh, mean);
else if (ilow == ihigh)
printf("= %ld", ilow);
else
printf("No Values");
break;
}
case FTDouble:
{
printf("\n Real %3d,%d %-16s", iWidth, iDecimals, iszTitle);
double dlow = 999999999999999.0;
double dhigh = -999999999999999.0;
double dsum = 0;
for (int iRecord = 0; iRecord < maxrec; iRecord++)
{
const double dtmp =
DBFReadDoubleAttribute(hDBF, iRecord, i);
if (dlow > dtmp)
dlow = dtmp;
if (dhigh < dtmp)
dhigh = dtmp;
dsum = dsum + dtmp;
}
const double mean = dsum / maxrec;
sprintf(stmp, "%%.%df to %%.%df \t(%%.%df)", iDecimals,
iDecimals, iDecimals);
if (dlow < dhigh)
printf(stmp, dlow, dhigh, mean);
else if (dlow == dhigh)
{
sprintf(stmp, "= %%.%df", iDecimals);
printf(stmp, dlow);
}
else
printf("No Values");
break;
}
case FTInvalid:
break;
}
}
printf("\n");
}
void findselect()
{
/* Find the select field name */
iselectitem = -1;
for (int i = 0; i < ti && iselectitem < 0; i++)
{
iType = DBFGetFieldInfo(hDBF, i, iszTitle, &iWidth, &iDecimals);
if (strncasecmp2(iszTitle, selectitem, 0) == 0)
iselectitem = i;
}
if (iselectitem == -1)
{
printf("Warning: Item not found for selection (%s)\n", selectitem);
iselect = false;
iall = false;
showitems();
printf("Continued... (Selecting entire file)\n");
}
/* Extract all of the select values (by field type) */
}
int selectrec(int iRecord)
{
const long int ty =
DBFGetFieldInfo(hDBF, iselectitem, NULL, &iWidth, &iDecimals);
switch (ty)
{
case FTString:
puts("Invalid Item");
iselect = false;
break;
case FTInteger:
{
const long int value =
DBFReadIntegerAttribute(hDBF, iRecord, iselectitem);
for (int j = 0; j < selcount; j++)
{
if (selectvalues[j] == value)
{
if (iunselect)
return (0); /* Keep this record */
else
return (1); /* Skip this record */
}
}
break;
}
case FTDouble:
puts("Invalid Item");
iselect = false;
break;
}
if (iunselect)
return (1); /* Skip this record */
else
return (0); /* Keep this record */
}
void check_theme_bnd()
{
if ((adfBoundsMin[0] >= cxmin) && (adfBoundsMax[0] <= cxmax) &&
(adfBoundsMin[1] >= cymin) && (adfBoundsMax[1] <= cymax))
{ /** Theme is totally inside clip area **/
if (ierase)
nEntities = 0; /** SKIP THEME **/
else
iclip = false; /** WRITE THEME (Clip not needed) **/
}
if (((adfBoundsMin[0] < cxmin) && (adfBoundsMax[0] < cxmin)) ||
((adfBoundsMin[1] < cymin) && (adfBoundsMax[1] < cymin)) ||
((adfBoundsMin[0] > cxmax) && (adfBoundsMax[0] > cxmax)) ||
((adfBoundsMin[1] > cymax) && (adfBoundsMax[1] > cymax)))
{ /** Theme is totally outside clip area **/
if (ierase)
iclip = false; /** WRITE THEME (Clip not needed) **/
else
nEntities = 0; /** SKIP THEME **/
}
if (nEntities == 0)
puts("WARNING: Theme is outside the clip area."); /** SKIP THEME **/
}
int clip_boundary()
{
/*** FIRST check the boundary of the feature ***/
if (((psCShape->dfXMin < cxmin) && (psCShape->dfXMax < cxmin)) ||
((psCShape->dfYMin < cymin) && (psCShape->dfYMax < cymin)) ||
((psCShape->dfXMin > cxmax) && (psCShape->dfXMax > cxmax)) ||
((psCShape->dfYMin > cymax) && (psCShape->dfYMax > cymax)))
{ /** Feature is totally outside clip area **/
if (ierase)
return (1); /** WRITE RECORD **/
else
return (0); /** SKIP RECORD **/
}
if ((psCShape->dfXMin >= cxmin) && (psCShape->dfXMax <= cxmax) &&
(psCShape->dfYMin >= cymin) && (psCShape->dfYMax <= cymax))
{ /** Feature is totally inside clip area **/
if (ierase)
return (0); /** SKIP RECORD **/
else
return (1); /** WRITE RECORD **/
}
if (iinside)
{ /** INSIDE * Feature might touch the boundary or could be outside **/
if (ierase)
return (1); /** WRITE RECORD **/
else
return (0); /** SKIP RECORD **/
}
if (itouch)
{ /** TOUCH **/
if (((psCShape->dfXMin <= cxmin) || (psCShape->dfXMax >= cxmax)) &&
(psCShape->dfYMin >= cymin) && (psCShape->dfYMax <= cymax))
{ /** Feature intersects the clip boundary only on the X axis **/
if (ierase)
return (0); /** SKIP RECORD **/
else
return (1); /** WRITE RECORD **/
}
if ((psCShape->dfXMin >= cxmin) && (psCShape->dfXMax <= cxmax) &&
((psCShape->dfYMin <= cymin) || (psCShape->dfYMax >= cymax)))
{ /** Feature intersects the clip boundary only on the Y axis **/
if (ierase)
return (0); /** SKIP RECORD **/
else
return (1); /** WRITE RECORD **/
}
for (int j2 = 0; j2 < psCShape->nVertices; j2++)
{ /** At least one vertex must be inside the clip boundary **/
if ((psCShape->padfX[j2] >= cxmin &&
psCShape->padfX[j2] <= cxmax) ||
(psCShape->padfY[j2] >= cymin && psCShape->padfY[j2] <= cymax))
{
if (ierase)
return (0); /** SKIP RECORD **/
else
return (1); /** WRITE RECORD **/
}
}
/** All vertices are outside the clip boundary **/
if (ierase)
return (1); /** WRITE RECORD **/
else
return (0); /** SKIP RECORD **/
} /** End TOUCH **/
if (icut)
{ /** CUT **/
/*** Check each vertex in the feature with the Boundary and "CUT" ***/
/*** THIS CODE WAS NOT COMPLETED! READ NOTE AT THE BOTTOM ***/
int i2 = 0;
bool prev_outside = false;
for (int j2 = 0; j2 < psCShape->nVertices; j2++)
{
bool inside =
psCShape->padfX[j2] >= cxmin && psCShape->padfX[j2] <= cxmax &&
psCShape->padfY[j2] >= cymin && psCShape->padfY[j2] <= cymax;
if (ierase)
inside = !inside;
if (inside)
{
if (i2 != j2)
{
if (prev_outside)
{
/*** AddIntersection(i2); ***/ /*** Add intersection ***/
prev_outside = false;
}
psCShape->padfX[i2] =
psCShape->padfX[j2]; /** move vertex **/
psCShape->padfY[i2] = psCShape->padfY[j2];
}
i2++;
}
else
{
if ((!prev_outside) && (j2 > 0))
{
/*** AddIntersection(i2); ***/ /*** Add intersection (Watch out for j2==i2-1) ***/
/*** Also a polygon may overlap twice and will split into a several parts ***/
prev_outside = true;
}
}
}
printf("Vertices:%d OUT:%d Number of Parts:%d\n",
psCShape->nVertices, i2, psCShape->nParts);
psCShape->nVertices = i2;
if (i2 < 2)
return (0); /** SKIP RECORD **/
/*** (WE ARE NOT CREATING INTERSECTIONS and some lines could be reduced to one point) **/
// if (i2 == 0) return(0); /** SKIP RECORD **/
// else
return (1); /** WRITE RECORD **/
} /** End CUT **/
return 0;
}
#define NKEYS (sizeof(unitkeytab) / sizeof(struct unitkey))
double findunit(char *unit)
{
struct unitkey
{
char *name;
double value;
} unitkeytab[] = {{"CM", 39.37}, {"CENTIMETER", 39.37},
{"CENTIMETERS", 39.37}, /** # of inches * 100 in unit **/
{"METER", 3937}, {"METERS", 3937},
{"KM", 3937000}, {"KILOMETER", 3937000},
{"KILOMETERS", 3937000}, {"INCH", 100},
{"INCHES", 100}, {"FEET", 1200},
{"FOOT", 1200}, {"YARD", 3600},
{"YARDS", 3600}, {"MILE", 6336000},
{"MILES", 6336000}};
double unitfactor = 0;
for (int j = 0; j < (int)NKEYS; j++)
{
if (strncasecmp2(unit, unitkeytab[j].name, 0) == 0)
unitfactor = unitkeytab[j].value;
}
return (unitfactor);
}
/* -------------------------------------------------------------------- */
/* Display a usage message. */
/* -------------------------------------------------------------------- */
void error()
{
puts("The program will append to an existing shape file or it will");
puts("create a new file if needed.");
puts("Only the items in the first output file will be preserved.");
puts("When an item does not match with the append theme then the item");
puts("might be placed to an existing item at the same position and type.");
puts(" OTHER FUNCTIONS:");
puts(" - Describe all items in the dbase file (Use ALL for more than 5000 "
"recs.)");
puts(" - Select a group of shapes from a comma separated selection list.");
puts(" - UnSelect a group of shapes from a comma separated selection "
"list.");
puts(" - Clip boundary extent or by theme boundary.");
puts(" Touch writes all the shapes that touch the boundary.");
puts(" Inside writes all the shapes that are completely within the "
"boundary.");
puts(" Boundary clips are only the min and max of a theme boundary.");
puts(" - Erase boundary extent or by theme boundary.");
puts(" Erase is the direct opposite of the Clip function.");
puts(" - Change coordinate value units between meters and feet.");
puts(" There is no way to determine the input unit of a shape file.");
puts(" Skip this function if the shape file is already in the correct "
"unit.");
puts(" Clip and Erase will be done before the unit is changed.");
puts(" A shift will be done after the unit is changed.");
puts(" - Shift X and Y coordinates.\n");
puts("Finally, There can only be one select or unselect in the command "
"line.");
puts(" There can only be one clip or erase in the command line.");
puts(" There can only be one unit and only one shift in the "
"command line.\n");
puts("Ex: shputils in.shp out.shp SELECT countycode 3,5,9,13,17,27");
puts(" shputils in.shp out.shp CLIP 10 10 90 90 Touch FACTOR "
"Meter Feet");
puts(" shputils in.shp out.shp FACTOR Meter 3.0");
puts(" shputils in.shp out.shp CLIP clip.shp Boundary Touch SHIFT "
"40 40");
puts(" shputils in.shp out.shp SELECT co 112 CLIP clip.shp Boundary "
"Touch\n");
puts("USAGE: shputils <DescribeShape> {ALL}");
puts("USAGE: shputils <InputShape> <OutShape|AppendShape>");
puts(" { <FACTOR> <FEET|MILES|METERS|KM> "
"<FEET|MILES|METERS|KM|factor> }");
puts(" { <SHIFT> <xshift> <yshift> }");
puts(" { <SELECT|UNSEL> <Item> <valuelist> }");
puts(
" { <CLIP|ERASE> <xmin> <ymin> <xmax> <ymax> <TOUCH|INSIDE|CUT> }");
puts(
" { <CLIP|ERASE> <theme> <BOUNDARY> <TOUCH|INSIDE|CUT> }");
puts(" Note: CUT is not complete and does not create intersections.");
puts(" For more information read programmer comment.");
/**** Clip functions for Polygon and Cut is not supported
There are several web pages that describe methods of doing this function.
It seem easy to implement until you start writing code. I don't have the
time to add these functions but a did leave a simple cut routine in the
program that can be called by using CUT instead of TOUCH in the
CLIP or ERASE functions. It does not add the intersection of the line and
the clip box, so polygons could look incomplete and lines will come up short.
Information about clipping lines with a box:
http://www.csclub.uwaterloo.ca/u/mpslager/articles/sutherland/wr.html
Information about finding the intersection of two lines:
http://www.whisqu.se/per/docs/math28.htm
THE CODE LOOKS LIKE THIS:
********************************************************
void Intersect_Lines(float x0,float y0,float x1,float y1,
float x2,float y2,float x3,float y3,
float *xi,float *yi)
{
// this function computes the intersection of the sent lines
// and returns the intersection point, note that the function assumes
// the lines intersect. the function can handle vertical as well
// as horizontal lines. note the function isn't very clever, it simply
// applies the math, but we don't need speed since this is a
// pre-processing step
// The Intersect_lines program came from (http://www.whisqu.se/per/docs/math28.htm)
float a1,b1,c1, // constants of linear equations
a2,b2,c2,
det_inv, // the inverse of the determinant of the coefficientmatrix
m1,m2; // the slopes of each line
// compute slopes, note the cludge for infinity, however, this will
// be close enough
if ((x1-x0)!=0)
m1 = (y1-y0)/(x1-x0);
else
m1 = (float)1e+10; // close enough to infinity
if ((x3-x2)!=0)
m2 = (y3-y2)/(x3-x2);
else
m2 = (float)1e+10; // close enough to infinity
// compute constants
a1 = m1;
a2 = m2;
b1 = -1;
b2 = -1;
c1 = (y0-m1*x0);
c2 = (y2-m2*x2);
// compute the inverse of the determinate
det_inv = 1/(a1*b2 - a2*b1);
// use Kramers rule to compute xi and yi
*xi=((b1*c2 - b2*c1)*det_inv);
*yi=((a2*c1 - a1*c2)*det_inv);
} // end Intersect_Lines
**********************************************************/
exit(1);
}
int main(int argc, char **argv)
{
// Check command line usage.
if (argc < 2)
error();
strcpy(infile, argv[1]);
if (argc > 2)
{
strcpy(outfile, argv[2]);
if (strncasecmp2(outfile, "LIST", 0) == 0)
{
ilist = true;
}
if (strncasecmp2(outfile, "ALL", 0) == 0)
{
iall = true;
}
}
if (ilist || iall || argc == 2)
{
setext(infile, "shp");
printf("DESCRIBE: %s\n", infile);
strcpy(outfile, "");
}
// Look for other functions on the command line. (SELECT, UNIT)
for (int i = 3; i < argc; i++)
{
if ((strncasecmp2(argv[i], "SEL", 3) == 0) ||
(strncasecmp2(argv[i], "UNSEL", 5) == 0))
{
if (strncasecmp2(argv[i], "UNSEL", 5) == 0)
iunselect = true;
i++;
if (i >= argc)
error();
strcpy(selectitem, argv[i]);
i++;
if (i >= argc)
error();
selcount = 0;
strcpy(temp, argv[i]);
cpt = temp;
tj = atoi(cpt);
ti = 0;
while (tj > 0)
{
selectvalues[selcount] = tj;
while (*cpt >= '0' && *cpt <= '9')
cpt++;
while (*cpt > '\0' && (*cpt < '0' || *cpt > '9'))
cpt++;
tj = atoi(cpt);
selcount++;
}
iselect = true;
} /*** End SEL & UNSEL ***/
else if ((strncasecmp2(argv[i], "CLIP", 4) == 0) ||
(strncasecmp2(argv[i], "ERASE", 5) == 0))
{
if (strncasecmp2(argv[i], "ERASE", 5) == 0)
ierase = true;
i++;
if (i >= argc)
error();
strcpy(clipfile, argv[i]);
sscanf(argv[i], "%lf", &cxmin);
i++;
if (i >= argc)
error();
if (strncasecmp2(argv[i], "BOUND", 5) == 0)
{
setext(clipfile, "shp");
hSHP = SHPOpen(clipfile, "rb");
if (hSHP == NULL)
{
printf("ERROR: Unable to open the clip shape file:%s\n",
clipfile);
exit(1);
}
SHPGetInfo(hSHPappend, NULL, NULL, adfBoundsMin, adfBoundsMax);
cxmin = adfBoundsMin[0];
cymin = adfBoundsMin[1];
cxmax = adfBoundsMax[0];
cymax = adfBoundsMax[1];
printf("Theme Clip Boundary: (%lf,%lf) - (%lf,%lf)\n", cxmin,
cymin, cxmax, cymax);
}
else
{ /*** xmin,ymin,xmax,ymax ***/
sscanf(argv[i], "%lf", &cymin);
i++;
if (i >= argc)
error();
sscanf(argv[i], "%lf", &cxmax);
i++;
if (i >= argc)
error();
sscanf(argv[i], "%lf", &cymax);
printf("Clip Box: (%lf,%lf) - (%lf,%lf)\n", cxmin, cymin, cxmax,
cymax);
}
i++;
if (i >= argc)
error();
if (strncasecmp2(argv[i], "CUT", 3) == 0)
icut = true;
else if (strncasecmp2(argv[i], "TOUCH", 5) == 0)
itouch = true;
else if (strncasecmp2(argv[i], "INSIDE", 6) == 0)
iinside = true;
else
error();
iclip = true;
} /*** End CLIP & ERASE ***/
else if (strncasecmp2(argv[i], "FACTOR", 0) == 0)
{
i++;
if (i >= argc)
error();
infactor = findunit(argv[i]);
if (infactor == 0)
error();
iunit = true;
i++;
if (i >= argc)
error();
outfactor = findunit(argv[i]);
if (outfactor == 0)
{
sscanf(argv[i], "%lf", &factor);
if (factor == 0)
error();
}
if (factor == 0)
{
if (infactor == 0)
{
puts(
"ERROR: Input unit must be defined before output unit");
exit(1);
}
factor = infactor / outfactor;
}
printf("Output file coordinate values will be factored by %lg\n",
factor);
} /*** End FACTOR ***/
else if (strncasecmp2(argv[i], "SHIFT", 5) == 0)
{
i++;
if (i >= argc)
error();
sscanf(argv[i], "%lf", &xshift);
i++;
if (i >= argc)
error();
sscanf(argv[i], "%lf", &yshift);
iunit = true;
printf("X Shift: %lg Y Shift: %lg\n", xshift, yshift);
} /*** End SHIFT ***/
else
{
printf("ERROR: Unknown function %s\n", argv[i]);
error();
}
}
// If there is no data in this file let the user know.
openfiles(); /* Open the infile and the outfile for shape and dbf. */
if (DBFGetFieldCount(hDBF) == 0)
{
puts("There are no fields in this table!");
exit(1);
}
// Print out the file bounds.
{
const int iRecord = DBFGetRecordCount(hDBF);
SHPGetInfo(hSHP, NULL, NULL, adfBoundsMin, adfBoundsMax);
printf(
"Input Bounds: (%lg,%lg) - (%lg,%lg) Entities: %d DBF: %d\n",
adfBoundsMin[0], adfBoundsMin[1], adfBoundsMax[0], adfBoundsMax[1],
nEntities, iRecord);
if (strcmp(outfile, "") == 0)
{ /* Describe the shapefile; No other functions */
ti = DBFGetFieldCount(hDBF);
showitems();
exit(0);
}
}
if (iclip)
check_theme_bnd();
{
const int jRecord = DBFGetRecordCount(hDBFappend);
SHPGetInfo(hSHPappend, NULL, NULL, adfBoundsMin, adfBoundsMax);
if (nEntitiesAppend == 0)
puts("New Output File\n");
else
printf(
"Append Bounds: (%lg,%lg)-(%lg,%lg) Entities: %d DBF: %d\n",
adfBoundsMin[0], adfBoundsMin[1], adfBoundsMax[0],
adfBoundsMax[1], nEntitiesAppend, jRecord);
}
/* -------------------------------------------------------------------- */
/* Find matching fields in the append file or add new items. */
/* -------------------------------------------------------------------- */