-
Notifications
You must be signed in to change notification settings - Fork 0
/
shp_doctor.c
1616 lines (1602 loc) · 43.9 KB
/
shp_doctor.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
/*
/ shp_doctor
/
/ an analysis / sanitizing tool for SHAPEFILES
/
/ version 1.0, 2008 October 13
/
/ Author: Sandro Furieri a.furieri@lqt.it
/
/ Copyright (C) 2008 Alessandro Furieri
/
/ This program is free software: you can redistribute it and/or modify
/ it under the terms of the GNU General Public License as published by
/ the Free Software Foundation, either version 3 of the License, or
/ (at your option) any later version.
/
/ This program 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 General Public License for more details.
/
/ You should have received a copy of the GNU General Public License
/ along with this program. If not, see <http://www.gnu.org/licenses/>.
/
*/
#if defined(_WIN32) && !defined(__MINGW32__)
/* MSVC strictly requires this include [off_t] */
#include <sys/types.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <float.h>
#include <errno.h>
#if defined(_WIN32) && !defined(__MINGW32__)
#include "config-msvc.h"
#else
#include "config.h"
#endif
#ifdef SPATIALITE_AMALGAMATION
#include <spatialite/sqlite3.h>
#else
#include <sqlite3.h>
#endif
#include <spatialite/gaiageo.h>
#include <spatialite.h>
#define ARG_NONE 0
#define ARG_IN_PATH 1
#if defined(_WIN32) && !defined(__MINGW32__)
#define strcasecmp _stricmp
#endif /* not WIN32 */
static void
do_analyze (char *base_path, int ignore_shape, int ignore_extent)
{
/* analyzing a SHAPEFILE */
FILE *fl_shx = NULL;
FILE *fl_shp = NULL;
FILE *fl_dbf = NULL;
char path[1024];
int rd;
unsigned char buf_shx[256];
unsigned char *buf_shp = NULL;
int buf_size = 1024;
int shape;
int x_shape;
unsigned char bf[1024];
unsigned char *buf_dbf = NULL;
int dbf_size;
int dbf_reclen = 0;
int dbf_recno;
int off_dbf;
int current_row;
int skpos;
int offset;
int off_shp;
int sz;
int ind;
double x;
double y;
int points;
int n;
int n1;
int base;
int start;
int end;
int iv;
double first_x;
double first_y;
double last_x;
double last_y;
int repeated;
double shp_minx;
double shp_miny;
double shp_maxx;
double shp_maxy;
int err_dbf = 0;
int err_geo = 0;
char field_name[16];
char *sys_err;
int first_coord_err;
char *err_open = "ERROR: unable to open '%s' for reading: %s\n";
char *err_header = "Invalid %s header\n";
char *err_read = "ERROR: invalid read on %s (entity #%d)\n";
char *err_shape =
"ERROR: invalid shape-type=%d [expected %d] (entity #%d)\n";
char *null_shape = "WARNING: NULL shape (entity #%d)\n";
int endian_arch = gaiaEndianArch ();
printf ("\nshp_doctor\n\n");
printf
("==================================================================\n");
printf ("input SHP base-path: %s\n", base_path);
if (ignore_shape)
{
printf ("-option: ignoring shape-type as declared by each entity\n");
printf ("\talways using the Shapefile's shape-type by default\n");
}
if (ignore_extent)
{
printf ("-option: ignoring BBOX (extent) declarations\n");
printf ("\tsuppressing coords consistency check\n");
}
printf
("==================================================================\n\n");
/* opening the SHP file */
sprintf (path, "%s.shp", base_path);
fl_shp = fopen (path, "rb");
if (!fl_shp)
{
sys_err = strerror (errno);
printf (err_open, path, sys_err);
}
/* opening the SHX file */
sprintf (path, "%s.shx", base_path);
fl_shx = fopen (path, "rb");
if (!fl_shx)
{
sys_err = strerror (errno);
printf (err_open, path, sys_err);
}
/* opening the DBF file */
sprintf (path, "%s.dbf", base_path);
fl_dbf = fopen (path, "rb");
if (!fl_dbf)
{
sys_err = strerror (errno);
printf (err_open, path, sys_err);
}
if (!fl_shp || !fl_shx || !fl_dbf)
goto no_file;
/* reading SHX file header */
rd = fread (buf_shx, sizeof (unsigned char), 100, fl_shx);
if (rd != 100)
{
printf (err_header, "SHX");
goto error;
}
if (gaiaImport32 (buf_shx + 0, GAIA_BIG_ENDIAN, endian_arch) != 9994) /* checks the SHX magic number */
{
printf (err_header, "SHX");
goto error;
}
/* reading SHP file header */
buf_shp = malloc (sizeof (unsigned char) * buf_size);
rd = fread (buf_shp, sizeof (unsigned char), 100, fl_shp);
if (rd != 100)
{
printf (err_header, "SHP");
goto error;
}
if (gaiaImport32 (buf_shp + 0, GAIA_BIG_ENDIAN, endian_arch) != 9994) /* checks the SHP magic number */
{
printf (err_header, "SHP");
goto error;
}
shape = gaiaImport32 (buf_shp + 32, GAIA_LITTLE_ENDIAN, endian_arch);
if (shape == GAIA_SHP_POINT || shape == GAIA_SHP_POINTM
|| shape == GAIA_SHP_POINTZ || shape == GAIA_SHP_POLYLINE
|| shape == GAIA_SHP_POLYLINEM || shape == GAIA_SHP_POLYLINEZ
|| shape == GAIA_SHP_POLYGON || shape == GAIA_SHP_POLYGONM
|| shape == GAIA_SHP_POLYGONZ || shape == GAIA_SHP_MULTIPOINT
|| shape == GAIA_SHP_MULTIPOINTM || shape == GAIA_SHP_MULTIPOINTZ)
;
else
goto unsupported;
if (shape == GAIA_SHP_POINT)
printf ("shape-type=%d POINT\n\n", shape);
if (shape == GAIA_SHP_POINTZ)
printf ("shape-type=%d POINT-Z\n\n", shape);
if (shape == GAIA_SHP_POINTM)
printf ("shape-type=%d POINT-M\n\n", shape);
if (shape == GAIA_SHP_POLYLINE)
printf ("shape-type=%d POLYLINE\n\n", shape);
if (shape == GAIA_SHP_POLYLINEZ)
printf ("shape-type=%d POLYLINE-Z\n\n", shape);
if (shape == GAIA_SHP_POLYLINEM)
printf ("shape-type=%d POLYLINE-M\n\n", shape);
if (shape == GAIA_SHP_POLYGON)
printf ("shape-type=%d POLYGON\n\n", shape);
if (shape == GAIA_SHP_POLYGONZ)
printf ("shape-type=%d POLYGON-Z\n\n", shape);
if (shape == GAIA_SHP_POLYGONM)
printf ("shape-type=%d POLYGON-M\n\n", shape);
if (shape == GAIA_SHP_MULTIPOINT)
printf ("shape-type=%d MULTIPOINT\n\n", shape);
if (shape == GAIA_SHP_MULTIPOINTZ)
printf ("shape-type=%d MULTIPOINT-Z\n\n", shape);
if (shape == GAIA_SHP_MULTIPOINTM)
printf ("shape-type=%d MULTIPOINT-M\n\n", shape);
x_shape = shape;
shp_minx = gaiaImport64 (buf_shp + 36, GAIA_LITTLE_ENDIAN, endian_arch);
shp_miny = gaiaImport64 (buf_shp + 44, GAIA_LITTLE_ENDIAN, endian_arch);
shp_maxx = gaiaImport64 (buf_shp + 52, GAIA_LITTLE_ENDIAN, endian_arch);
shp_maxy = gaiaImport64 (buf_shp + 60, GAIA_LITTLE_ENDIAN, endian_arch);
if (!ignore_extent)
{
printf ("shape-extent:\tMIN(x=%1.6f y=%1.6f)\n", shp_minx, shp_miny);
printf ("\t\tMAX(x=%1.6f y=%1.6f)\n\n", shp_maxx, shp_maxy);
}
/* reading DBF file header */
rd = fread (bf, sizeof (unsigned char), 32, fl_dbf);
if (rd != 32)
{
printf (err_header, "DBF");
goto error;
}
if (*bf != 0x03) /* checks the DBF magic number */
{
printf (err_header, "DBF");
goto error;
}
dbf_recno = gaiaImport32 (bf + 4, GAIA_LITTLE_ENDIAN, endian_arch);
dbf_size = gaiaImport16 (bf + 8, GAIA_LITTLE_ENDIAN, endian_arch);
dbf_reclen = gaiaImport16 (bf + 10, GAIA_LITTLE_ENDIAN, endian_arch);
printf ("DBF header summary:\n");
printf ("========================================\n");
printf (" # records = %d\n", dbf_recno);
printf ("record-length = %d\n", dbf_reclen);
printf ("\nDBF fields:\n");
printf ("========================================\n");
dbf_size--;
off_dbf = 0;
for (ind = 32; ind < dbf_size; ind += 32)
{
/* fetches DBF fields definitions */
rd = fread (bf, sizeof (unsigned char), 32, fl_dbf);
if (rd != 32)
goto error;
memcpy (field_name, bf, 11);
field_name[11] = '\0';
printf ("name=%-10s offset=%4d type=%c size=%3d decimals=%2d",
field_name, off_dbf, *(bf + 11), *(bf + 16), *(bf + 17));
switch (*(bf + 11))
{
case 'C':
printf (" CHARACTER\n");
if (*(bf + 16) < 1)
{
printf ("\t\tERROR: length=0 ???\n");
err_dbf = 1;
}
break;
case 'N':
printf (" NUMBER\n");
if (*(bf + 16) > 18)
printf ("\t\tWARNING: expected size is MAX 18 !!!\n");
break;
case 'L':
printf (" LOGICAL\n");
if (*(bf + 16) != 1)
{
printf ("\t\tERROR: expected length is 1 !!!\n");
err_dbf = 1;
}
break;
case 'D':
printf (" DATE\n");
if (*(bf + 16) != 8)
{
printf ("\t\tERROR: expected length is 8 !!!\n");
err_dbf = 1;
}
break;
case 'F':
printf (" FLOAT\n");
break;
default:
printf (" UNKNOWN\n");
{
printf ("\t\tERROR: unsupported data type\n");
err_dbf = 1;
}
break;
};
off_dbf += *(bf + 16);
}
buf_dbf = malloc (sizeof (unsigned char) * dbf_reclen);
buf_shp = malloc (sizeof (unsigned char) * buf_size);
printf ("\nTesting SHP entities:\n");
printf ("========================================\n");
current_row = 0;
while (1)
{
/* reading entities from shapefile */
/* positioning and reading the SHX file */
offset = 100 + (current_row * 8); /* 100 bytes for the header + current row displacement; each SHX row = 8 bytes */
skpos = fseek (fl_shx, offset, SEEK_SET);
if (skpos != 0)
goto eof;
rd = fread (bf, sizeof (unsigned char), 8, fl_shx);
if (rd != 8)
goto eof;
off_shp = gaiaImport32 (bf, GAIA_BIG_ENDIAN, endian_arch);
/* positioning and reading the DBF file */
offset = dbf_size + (current_row * dbf_reclen);
skpos = fseek (fl_dbf, offset, SEEK_SET);
if (skpos != 0)
{
printf (err_read, "DBF", current_row + 1);
goto error;
}
rd = fread (buf_dbf, sizeof (unsigned char), dbf_reclen, fl_dbf);
if (rd != dbf_reclen)
{
printf (err_read, "DBF", current_row + 1);
goto error;
}
/* positioning and reading corresponding SHP entity - geometry */
offset = off_shp * 2;
skpos = fseek (fl_shp, offset, SEEK_SET);
if (skpos != 0)
{
printf (err_read, "SHP", current_row + 1);
goto error;
}
rd = fread (buf_shp, sizeof (unsigned char), 12, fl_shp);
if (rd != 12)
{
printf (err_read, "SHP", current_row + 1);
goto error;
}
sz = gaiaImport32 (buf_shp + 4, GAIA_BIG_ENDIAN, endian_arch);
shape = gaiaImport32 (buf_shp + 8, GAIA_LITTLE_ENDIAN, endian_arch);
if (ignore_shape)
shape = x_shape;
if (shape != x_shape)
{
if (shape == 0)
printf (null_shape, current_row + 1);
else
{
printf (err_shape, shape, x_shape, current_row + 1);
err_geo = 1;
}
}
if ((sz * 2) > buf_size)
{
/* current buffer is too small; we need to allocate a bigger buffer */
free (buf_shp);
buf_size = sz * 2;
buf_shp = malloc (sizeof (unsigned char) * buf_size);
}
if (shape == GAIA_SHP_POINT || shape == GAIA_SHP_POINTZ
|| shape == GAIA_SHP_POINTM)
{
/* shape point */
rd = fread (buf_shp, sizeof (unsigned char), 16, fl_shp);
if (rd != 16)
{
printf (err_read, "SHP point-entity", current_row + 1);
goto error;
}
x = gaiaImport64 (buf_shp, GAIA_LITTLE_ENDIAN, endian_arch);
y = gaiaImport64 (buf_shp + 8, GAIA_LITTLE_ENDIAN, endian_arch);
if (!ignore_extent)
{
if (x < shp_minx || x > shp_maxx || y < shp_miny
|| y > shp_maxy)
{
printf
("WARNING: coords outside shp-extent (entity #%d)\n",
current_row + 1);
printf ("\tx=%1.6f y=%1.6f\n", x, y);
}
}
}
if (shape == GAIA_SHP_POLYLINE || shape == GAIA_SHP_POLYLINEZ
|| shape == GAIA_SHP_POLYLINEM)
{
/* shape polyline */
rd = fread (buf_shp, sizeof (unsigned char), 32, fl_shp);
if (rd != 32)
{
printf (err_read, "SHP polyline-entity", current_row + 1);
goto error;
}
rd = fread (buf_shp, sizeof (unsigned char), (sz * 2) - 36,
fl_shp);
if (rd != (sz * 2) - 36)
{
printf (err_read, "SHP polyline-entity", current_row + 1);
goto error;
}
n = gaiaImport32 (buf_shp, GAIA_LITTLE_ENDIAN, endian_arch);
n1 = gaiaImport32 (buf_shp + 4, GAIA_LITTLE_ENDIAN,
endian_arch);
base = 8 + (n * 4);
start = 0;
first_coord_err = 1;
for (ind = 0; ind < n; ind++)
{
if (ind < (n - 1))
end =
gaiaImport32 (buf_shp + 8 + ((ind + 1) * 4),
GAIA_LITTLE_ENDIAN, endian_arch);
else
end = n1;
points = end - start;
points = 0;
repeated = 0;
for (iv = start; iv < end; iv++)
{
x = gaiaImport64 (buf_shp + base + (iv * 16),
GAIA_LITTLE_ENDIAN, endian_arch);
y = gaiaImport64 (buf_shp + base + (iv * 16) + 8,
GAIA_LITTLE_ENDIAN, endian_arch);
if (points != 0)
{
if (last_x == x && last_y == y)
repeated = 1;
}
last_x = x;
last_y = y;
if (!ignore_extent && first_coord_err)
{
if (x < shp_minx || x > shp_maxx
|| y < shp_miny || y > shp_maxy)
{
printf
("WARNING: coords outside shp-extent (entity #%d)\n",
current_row + 1);
printf ("\tx=%1.6f y=%1.6f\n", x, y);
first_coord_err = 0;
}
}
start++;
points++;
}
if (points < 2)
{
printf
("ERROR: illegal polyline [%d vertices] (entity #%d)\n",
points, current_row + 1);
err_geo = 1;
}
if (repeated)
{
printf ("WARNING: repeated vertices (entity #%d)\n",
current_row + 1);
err_geo = 1;
}
}
}
if (shape == GAIA_SHP_POLYGON || shape == GAIA_SHP_POLYGONZ
|| shape == GAIA_SHP_POLYGONM)
{
/* shape polygon */
rd = fread (buf_shp, sizeof (unsigned char), 32, fl_shp);
if (rd != 32)
{
printf (err_read, "SHP polygon-entity", current_row + 1);
goto error;
}
rd = fread (buf_shp, sizeof (unsigned char), (sz * 2) - 36,
fl_shp);
if (rd != (sz * 2) - 36)
{
printf (err_read, "SHP polygon-entity", current_row + 1);
goto error;
}
n = gaiaImport32 (buf_shp, GAIA_LITTLE_ENDIAN, endian_arch);
n1 = gaiaImport32 (buf_shp + 4, GAIA_LITTLE_ENDIAN,
endian_arch);
base = 8 + (n * 4);
start = 0;
first_coord_err = 1;
repeated = 0;
for (ind = 0; ind < n; ind++)
{
if (ind < (n - 1))
end =
gaiaImport32 (buf_shp + 8 + ((ind + 1) * 4),
GAIA_LITTLE_ENDIAN, endian_arch);
else
end = n1;
points = end - start;
points = 0;
for (iv = start; iv < end; iv++)
{
x = gaiaImport64 (buf_shp + base + (iv * 16),
GAIA_LITTLE_ENDIAN, endian_arch);
y = gaiaImport64 (buf_shp + base + (iv * 16) + 8,
GAIA_LITTLE_ENDIAN, endian_arch);
if (points == 0)
{
first_x = x;
first_y = y;
}
else
{
if (last_x == x && last_y == y)
repeated = 1;
}
last_x = x;
last_y = y;
if (!ignore_extent && first_coord_err)
{
if (x < shp_minx || x > shp_maxx
|| y < shp_miny || y > shp_maxy)
{
printf
("WARNING: coords outside shp-extent (entity #%d)\n",
current_row + 1);
printf ("\tx=%1.6f y=%1.6f\n", x, y);
first_coord_err = 0;
}
}
start++;
points++;
}
if (points < 3)
{
printf
("ERROR: illegal ring [%d vertices] (entity #%d)\n",
points, current_row + 1);
err_geo = 1;
}
else
{
if (first_x == last_x && first_y == last_y)
{
if (points < 4)
{
printf
("ERROR: illegal ring [%d vertices] (entity #%d)\n",
points, current_row + 1);
err_geo = 1;
}
}
else
{
printf
("WARNING: unclosed ring (entity #%d)\n",
current_row + 1);
err_geo = 1;
}
}
if (repeated)
{
printf ("WARNING: repeated vertices (entity #%d)\n",
current_row + 1);
err_geo = 1;
}
}
}
if (shape == GAIA_SHP_MULTIPOINT || shape == GAIA_SHP_MULTIPOINTZ
|| shape == GAIA_SHP_MULTIPOINTM)
{
/* shape multipoint */
rd = fread (buf_shp, sizeof (unsigned char), 32, fl_shp);
if (rd != 32)
{
printf (err_read, "SHP multipoint-entity",
current_row + 1);
goto error;
}
rd = fread (buf_shp, sizeof (unsigned char), (sz * 2) - 36,
fl_shp);
if (rd != (sz * 2) - 36)
{
printf (err_read, "SHP multipoint-entity",
current_row + 1);
goto error;
}
n = gaiaImport32 (buf_shp, GAIA_LITTLE_ENDIAN, endian_arch);
first_coord_err = 1;
for (iv = 0; iv < n; iv++)
{
x = gaiaImport64 (buf_shp + 4 + (iv * 16),
GAIA_LITTLE_ENDIAN, endian_arch);
y = gaiaImport64 (buf_shp + 4 + (iv * 16) + 8,
GAIA_LITTLE_ENDIAN, endian_arch);
if (!ignore_extent && first_coord_err)
{
if (x < shp_minx || x > shp_maxx || y < shp_miny
|| y > shp_maxy)
{
printf
("WARNING: coords outside shp-extent (entity #%d)\n",
current_row + 1);
printf ("\tx=%1.6f y=%1.6f\n", x, y);
first_coord_err = 0;
}
}
}
}
current_row++;
}
eof:
printf ("\nShapefile contains %d entities\n", current_row);
if (err_dbf)
{
printf ("\n***** DBF contains unsupported data types ********\n");
printf ("\tyou can try to sane this issue as follows:\n");
printf ("\t- open this DBF using OpenOffice\n");
printf ("\t- then save as a new DBF [using a different name]\n");
printf ("\t- rename the DBF files in order to set the DBF\n");
printf ("\t exported from OpenOffice as the one associated\n");
printf ("\t with Shapefile\n\n");
printf ("\tGOOD LUCK :-)\n");
}
if (err_geo)
{
printf ("\n***** SHP contains invalid geometries ********\n");
}
if (!err_dbf && !err_geo)
printf ("\nValidation passed: no problem found\n");
if (fl_shx)
fclose (fl_shx);
if (fl_shp)
fclose (fl_shp);
if (fl_dbf)
fclose (fl_dbf);
if (buf_dbf)
free (buf_dbf);
if (buf_shp)
free (buf_shp);
return;
no_file:
/* one of shapefile's files can't be accessed */
printf ("\nUnable to analyze this SHP: some required file is missing\n");
if (fl_shx)
fclose (fl_shx);
if (fl_shp)
fclose (fl_shp);
if (fl_dbf)
fclose (fl_dbf);
if (buf_dbf)
free (buf_dbf);
if (buf_shp)
free (buf_shp);
return;
error:
/* the shapefile is invalid or corrupted */
printf ("\nThis Shapefile is corrupted / has an invalid format");
if (buf_shp)
free (buf_shp);
fclose (fl_shx);
fclose (fl_shp);
fclose (fl_dbf);
if (buf_dbf)
free (buf_dbf);
if (buf_shp)
free (buf_shp);
return;
unsupported:
/* the shapefile has an unrecognized shape type */
printf ("\nshape-type=%d is not supported", shape);
if (buf_shp)
free (buf_shp);
fclose (fl_shx);
fclose (fl_shp);
if (fl_dbf)
fclose (fl_dbf);
if (buf_dbf)
free (buf_dbf);
if (buf_shp)
free (buf_shp);
return;
}
static void
do_analyze_no_shx (char *base_path, int ignore_shape, int ignore_extent)
{
/* analyzing a SHAPEFILE [ignoring theSHX] */
FILE *fl_shp = NULL;
FILE *fl_dbf = NULL;
char path[1024];
int rd;
unsigned char *buf_shp = NULL;
int buf_size = 1024;
int shape;
int x_shape;
unsigned char bf[1024];
unsigned char *buf_dbf = NULL;
int dbf_size;
int dbf_reclen = 0;
int dbf_recno;
int off_dbf;
int current_row;
int skpos;
int offset;
int off_shp;
int sz;
int ind;
double x;
double y;
int points;
int n;
int n1;
int base;
int start;
int end;
int iv;
double first_x;
double first_y;
double last_x;
double last_y;
int repeated;
double shp_minx;
double shp_miny;
double shp_maxx;
double shp_maxy;
int err_dbf = 0;
int err_geo = 0;
char field_name[16];
int first_coord_err;
char *sys_err;
char *err_open = "ERROR: unable to open '%s' for reading: %s\n";
char *err_header = "Invalid %s header\n";
char *err_read = "ERROR: invalid read on %s (entity #%d)\n";
char *err_shape =
"ERROR: invalid shape-type=%d [expected %d] (entity #%d)\n";
char *null_shape = "WARNING: NULL shape (entity #%d)\n";
int endian_arch = gaiaEndianArch ();
printf ("\nshp_doctor\n\n");
printf
("==================================================================\n");
printf ("input SHP base-path: %s\n", base_path);
printf ("-option: ignoring the SHX file\n");
printf ("\tassuming plain 1:1 correspondence for SHP and DBF entities\n");
if (ignore_shape)
{
printf ("-option: ignoring shape-type as declared by each entity\n");
printf ("\talways using the Shapefile's shape-type by default\n");
}
if (ignore_extent)
{
printf ("-option: ignoring BBOX (extent) declarations\n");
printf ("\tsuppressing coords consistency check\n");
}
printf
("==================================================================\n\n");
/* opening the SHP file */
sprintf (path, "%s.shp", base_path);
fl_shp = fopen (path, "rb");
if (!fl_shp)
{
sys_err = strerror (errno);
printf (err_open, path, sys_err);
}
/* opening the DBF file */
sprintf (path, "%s.dbf", base_path);
fl_dbf = fopen (path, "rb");
if (!fl_dbf)
{
sys_err = strerror (errno);
printf (err_open, path, sys_err);
}
if (!fl_shp || !fl_dbf)
goto no_file;
/* reading SHP file header */
buf_shp = malloc (sizeof (unsigned char) * buf_size);
rd = fread (buf_shp, sizeof (unsigned char), 100, fl_shp);
if (rd != 100)
{
printf (err_header, "SHP");
goto error;
}
if (gaiaImport32 (buf_shp + 0, GAIA_BIG_ENDIAN, endian_arch) != 9994) /* checks the SHP magic number */
{
printf (err_header, "SHP");
goto error;
}
shape = gaiaImport32 (buf_shp + 32, GAIA_LITTLE_ENDIAN, endian_arch);
if (shape == GAIA_SHP_POINT || shape == GAIA_SHP_POINTM
|| shape == GAIA_SHP_POINTM || shape == GAIA_SHP_POLYLINE
|| shape == GAIA_SHP_POLYLINEZ || shape == GAIA_SHP_POLYLINEM
|| shape == GAIA_SHP_POLYGON || shape == GAIA_SHP_POLYGONZ
|| shape == GAIA_SHP_POLYGONM || shape == GAIA_SHP_MULTIPOINT
|| shape == GAIA_SHP_MULTIPOINTZ || shape == GAIA_SHP_MULTIPOINTM)
;
else
goto unsupported;
if (shape == GAIA_SHP_POINT)
printf ("shape-type=%d POINT\n\n", shape);
if (shape == GAIA_SHP_POINTZ)
printf ("shape-type=%d POINT-Z\n\n", shape);
if (shape == GAIA_SHP_POINTM)
printf ("shape-type=%d POINT-M\n\n", shape);
if (shape == GAIA_SHP_POLYLINE)
printf ("shape-type=%d POLYLINE\n\n", shape);
if (shape == GAIA_SHP_POLYLINEZ)
printf ("shape-type=%d POLYLINE-Z\n\n", shape);
if (shape == GAIA_SHP_POLYLINEM)
printf ("shape-type=%d POLYLINE-M\n\n", shape);
if (shape == GAIA_SHP_POLYGON)
printf ("shape-type=%d POLYGON\n\n", shape);
if (shape == GAIA_SHP_POLYGONZ)
printf ("shape-type=%d POLYGON-Z\n\n", shape);
if (shape == GAIA_SHP_POLYGONM)
printf ("shape-type=%d POLYGON-M\n\n", shape);
if (shape == GAIA_SHP_MULTIPOINT)
printf ("shape-type=%d MULTIPOINT\n\n", shape);
if (shape == GAIA_SHP_MULTIPOINTZ)
printf ("shape-type=%d MULTIPOINT-Z\n\n", shape);
if (shape == GAIA_SHP_MULTIPOINTM)
printf ("shape-type=%d MULTIPOINT-M\n\n", shape);
x_shape = shape;
shp_minx = gaiaImport64 (buf_shp + 36, GAIA_LITTLE_ENDIAN, endian_arch);
shp_miny = gaiaImport64 (buf_shp + 44, GAIA_LITTLE_ENDIAN, endian_arch);
shp_maxx = gaiaImport64 (buf_shp + 52, GAIA_LITTLE_ENDIAN, endian_arch);
shp_maxy = gaiaImport64 (buf_shp + 60, GAIA_LITTLE_ENDIAN, endian_arch);
if (!ignore_extent)
{
printf ("shape-extent:\tMIN(x=%1.6f y=%1.6f)\n", shp_minx, shp_miny);
printf ("\t\tMAX(x=%1.6f y=%1.6f)\n\n", shp_maxx, shp_maxy);
}
/* reading DBF file header */
rd = fread (bf, sizeof (unsigned char), 32, fl_dbf);
if (rd != 32)
{
printf (err_header, "DBF");
goto error;
}
if (*bf != 0x03) /* checks the DBF magic number */
{
printf (err_header, "DBF");
goto error;
}
dbf_recno = gaiaImport32 (bf + 4, GAIA_LITTLE_ENDIAN, endian_arch);
dbf_size = gaiaImport16 (bf + 8, GAIA_LITTLE_ENDIAN, endian_arch);
dbf_reclen = gaiaImport16 (bf + 10, GAIA_LITTLE_ENDIAN, endian_arch);
printf ("DBF header summary:\n");
printf ("========================================\n");
printf (" # records = %d\n", dbf_recno);
printf ("record-length = %d\n", dbf_reclen);
printf ("\nDBF fields:\n");
printf ("========================================\n");
dbf_size--;
off_dbf = 0;
for (ind = 32; ind < dbf_size; ind += 32)
{
/* fetches DBF fields definitions */
rd = fread (bf, sizeof (unsigned char), 32, fl_dbf);
if (rd != 32)
goto error;
memcpy (field_name, bf, 11);
field_name[11] = '\0';
printf ("name=%-10s offset=%4d type=%c size=%3d decimals=%2d",
field_name, off_dbf, *(bf + 11), *(bf + 16), *(bf + 17));
switch (*(bf + 11))
{
case 'C':
printf (" CHARACTER\n");
if (*(bf + 16) < 1)
{
printf ("\t\tERROR: length=0 ???\n");
err_dbf = 1;
}
break;
case 'N':
printf (" NUMBER\n");
if (*(bf + 16) > 18)
printf ("\t\tWARNING: expected size is MAX 18 !!!\n");
break;
case 'L':
printf (" LOGICAL\n");
if (*(bf + 16) != 1)
{
printf ("\t\tERROR: expected length is 1 !!!\n");
err_dbf = 1;
}
break;
case 'D':
printf (" DATE\n");
if (*(bf + 16) != 8)
{
printf ("\t\tERROR: expected length is 8 !!!\n");
err_dbf = 1;
}
break;
case 'F':
printf (" FLOAT\n");
break;
default:
printf (" UNKNOWN\n");
{
printf ("\t\tERROR: unsupported data type\n");
err_dbf = 1;
}
break;
};
off_dbf += *(bf + 16);
}
buf_dbf = malloc (sizeof (unsigned char) * dbf_reclen);
buf_shp = malloc (sizeof (unsigned char) * buf_size);
current_row = 0;
/* positioning the DBF file on first entity */
offset = dbf_size;
skpos = fseek (fl_dbf, offset, SEEK_SET);
if (skpos != 0)
{
printf (err_read, "DBF", current_row + 1);
goto error;
}
off_shp = 100;
printf ("\nTesting SHP entities:\n");
printf ("========================================\n");
while (1)
{
/* reading entities from shapefile */
if (current_row == dbf_recno)
goto eof;
/* sequentially reading the DBF file */
rd = fread (buf_dbf, sizeof (unsigned char), dbf_reclen, fl_dbf);
if (rd != dbf_reclen)
{
printf (err_read, "DBF", current_row + 1);
goto error;
}
/* positioning and reading corresponding SHP entity - geometry */
skpos = fseek (fl_shp, off_shp, SEEK_SET);
if (skpos != 0)
{
printf (err_read, "SHP", current_row + 1);
goto error;
}
rd = fread (buf_shp, sizeof (unsigned char), 12, fl_shp);
if (rd != 12)
{
printf (err_read, "SHP", current_row + 1);
goto error;
}
sz = gaiaImport32 (buf_shp + 4, GAIA_BIG_ENDIAN, endian_arch);
off_shp += (8 + (sz * 2));
shape = gaiaImport32 (buf_shp + 8, GAIA_LITTLE_ENDIAN, endian_arch);
if (ignore_shape)
shape = x_shape;
if (shape != x_shape)
{
if (shape == 0)
printf (null_shape, current_row + 1);
else
{
printf (err_shape, shape, x_shape, current_row + 1);
err_geo = 1;
}
}
if ((sz * 2) > buf_size)
{
/* current buffer is too small; we need to allocate a bigger buffer */
free (buf_shp);
buf_size = sz * 2;
buf_shp = malloc (sizeof (unsigned char) * buf_size);
}
if (shape == GAIA_SHP_POINT || shape == GAIA_SHP_POINTZ
|| shape == GAIA_SHP_POINTM)
{
/* shape point */
rd = fread (buf_shp, sizeof (unsigned char), 16, fl_shp);
if (rd != 16)
{
printf (err_read, "SHP point-entity", current_row + 1);
goto error;
}
x = gaiaImport64 (buf_shp, GAIA_LITTLE_ENDIAN, endian_arch);
y = gaiaImport64 (buf_shp + 8, GAIA_LITTLE_ENDIAN, endian_arch);
if (!ignore_extent)
{
if (x < shp_minx || x > shp_maxx || y < shp_miny
|| y > shp_maxy)
{
printf
("WARNING: coords outside shp-extent (entity #%d)\n",
current_row + 1);
printf ("\tx=%1.6f y=%1.6f\n", x, y);
}
}
}
if (shape == GAIA_SHP_POLYLINE || shape == GAIA_SHP_POLYLINEZ
|| shape == GAIA_SHP_POLYLINEM)