-
Notifications
You must be signed in to change notification settings - Fork 0
/
shp_sanitize.c
4326 lines (4219 loc) · 118 KB
/
shp_sanitize.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_sanitize
/
/ an analysis / sanitizing tool for broken SHAPEFILES
/
/ version 1.0, 2016 April 25
/
/ Author: Sandro Furieri [email protected]
/
/ Copyright (C) 2016 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/>.
/
*/
#ifndef _WIN32
#include <unistd.h>
#endif
#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>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/types.h>
#if defined(_WIN32) && !defined(__MINGW32__)
#include <io.h>
#include <direct.h>
#else
#include <dirent.h>
#endif
#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_DIR 1
#define ARG_OUT_DIR 2
#define SUFFIX_DISCARD 0
#define SUFFIX_SHP 1
#define SUFFIX_SHX 2
#define SUFFIX_DBF 3
#define SHAPEFILE_NO_DATA 1e-38
#if defined(_WIN32) && !defined(__MINGW32__)
#define strcasecmp _stricmp
#endif /* not WIN32 */
struct shp_entry
{
/* an item of the SHP list */
char *base_name;
char *file_name;
int has_shp;
int has_shx;
int has_dbf;
struct shp_entry *next;
};
struct shp_list
{
/* the SHP list */
struct shp_entry *first;
struct shp_entry *last;
};
static struct shp_list *
alloc_shp_list (void)
{
/* allocating an empty SHP list */
struct shp_list *list = malloc (sizeof (struct shp_list));
list->first = NULL;
list->last = NULL;
return list;
}
static void
free_shp_list (struct shp_list *list)
{
/* memory cleanup: freeing an SHP list */
struct shp_entry *pi;
struct shp_entry *pin;
if (list == NULL)
return;
pi = list->first;
while (pi != NULL)
{
pin = pi->next;
if (pi->base_name != NULL)
sqlite3_free (pi->base_name);
if (pi->file_name != NULL)
sqlite3_free (pi->file_name);
free (pi);
pi = pin;
}
free (list);
}
static void
do_add_shapefile (struct shp_list *list, char *base_name, char *file_name,
int suffix)
{
/* adding a possible SHP to the list */
struct shp_entry *pi;
if (list == NULL)
return;
pi = list->first;
while (pi != NULL)
{
/* searching if already defined */
if (strcmp (pi->base_name, base_name) == 0)
{
switch (suffix)
{
case SUFFIX_SHP:
pi->has_shp = 1;
break;
case SUFFIX_SHX:
pi->has_shx = 1;
break;
case SUFFIX_DBF:
pi->has_dbf = 1;
break;
};
sqlite3_free (base_name);
sqlite3_free (file_name);
return;
}
pi = pi->next;
}
/* adding a new SHP entry */
pi = malloc (sizeof (struct shp_entry));
pi->base_name = base_name;
pi->file_name = file_name;
pi->has_shp = 0;
pi->has_shx = 0;
pi->has_dbf = 0;
pi->next = NULL;
switch (suffix)
{
case SUFFIX_SHP:
pi->has_shp = 1;
break;
case SUFFIX_SHX:
pi->has_shx = 1;
break;
case SUFFIX_DBF:
pi->has_dbf = 1;
break;
};
if (list->first == NULL)
list->first = pi;
if (list->last != NULL)
list->last->next = pi;
list->last = pi;
}
static int
test_valid_shp (struct shp_entry *p)
{
/* testing for a valid SHP candidate */
if (p == NULL)
return 0;
if (p->has_shp && p->has_shx && p->has_dbf)
return 1;
return 0;
}
static gaiaShapefilePtr
allocShapefile ()
{
/* allocates and initializes the Shapefile object */
gaiaShapefilePtr shp = malloc (sizeof (gaiaShapefile));
shp->endian_arch = 1;
shp->Path = NULL;
shp->Shape = -1;
shp->EffectiveType = GAIA_UNKNOWN;
shp->EffectiveDims = GAIA_XY;
shp->flShp = NULL;
shp->flShx = NULL;
shp->flDbf = NULL;
shp->Dbf = NULL;
shp->ShpBfsz = 0;
shp->BufShp = NULL;
shp->BufDbf = NULL;
shp->DbfHdsz = 0;
shp->DbfReclen = 0;
shp->DbfSize = 0;
shp->DbfRecno = 0;
shp->ShpSize = 0;
shp->ShxSize = 0;
shp->MinX = DBL_MAX;
shp->MinY = DBL_MAX;
shp->MaxX = -DBL_MAX;
shp->MaxY = -DBL_MAX;
shp->Valid = 0;
shp->IconvObj = NULL;
shp->LastError = NULL;
return shp;
}
static void
freeShapefile (gaiaShapefilePtr shp)
{
/* frees all memory allocations related to the Shapefile object */
if (shp->Path)
free (shp->Path);
if (shp->flShp)
fclose (shp->flShp);
if (shp->flShx)
fclose (shp->flShx);
if (shp->flDbf)
fclose (shp->flDbf);
if (shp->Dbf)
gaiaFreeDbfList (shp->Dbf);
if (shp->BufShp)
free (shp->BufShp);
if (shp->BufDbf)
free (shp->BufDbf);
if (shp->LastError)
free (shp->LastError);
free (shp);
}
static void
openShpRead (gaiaShapefilePtr shp, const char *path, double *MinX, double *MinY,
double *MaxX, double *MaxY, int *mismatching)
{
/* trying to open the shapefile and initial checkings */
FILE *fl_shx = NULL;
FILE *fl_shp = NULL;
FILE *fl_dbf = NULL;
char xpath[1024];
int rd;
unsigned char buf_shx[256];
unsigned char *buf_shp = NULL;
int buf_size = 1024;
int shape;
unsigned char bf[1024];
int dbf_size;
int dbf_reclen = 0;
int off_dbf;
int ind;
char field_name[2048];
char *sys_err;
char errMsg[1024];
double minx;
double miny;
double maxx;
double maxy;
int len;
int endian_arch = gaiaEndianArch ();
gaiaDbfListPtr dbf_list = NULL;
if (shp->flShp != NULL || shp->flShx != NULL || shp->flDbf != NULL)
{
sprintf (errMsg,
"attempting to reopen an already opened Shapefile\n");
goto unsupported_conversion;
}
sprintf (xpath, "%s.shx", path);
fl_shx = fopen (xpath, "rb");
if (!fl_shx)
{
sys_err = strerror (errno);
sprintf (errMsg, "unable to open '%s' for reading: %s", xpath,
sys_err);
goto no_file;
}
sprintf (xpath, "%s.shp", path);
fl_shp = fopen (xpath, "rb");
if (!fl_shp)
{
sys_err = strerror (errno);
sprintf (errMsg, "unable to open '%s' for reading: %s", xpath,
sys_err);
goto no_file;
}
sprintf (xpath, "%s.dbf", path);
fl_dbf = fopen (xpath, "rb");
if (!fl_dbf)
{
sys_err = strerror (errno);
sprintf (errMsg, "unable to open '%s' for reading: %s", xpath,
sys_err);
goto no_file;
}
/* reading SHX file header */
rd = fread (buf_shx, sizeof (unsigned char), 100, fl_shx);
if (rd != 100)
goto error;
if (gaiaImport32 (buf_shx + 0, GAIA_BIG_ENDIAN, endian_arch) != 9994) /* checks the SHX magic number */
goto error;
*MinX = gaiaImport64 (buf_shx + 36, GAIA_LITTLE_ENDIAN, endian_arch);
*MinY = gaiaImport64 (buf_shx + 44, GAIA_LITTLE_ENDIAN, endian_arch);
*MaxX = gaiaImport64 (buf_shx + 52, GAIA_LITTLE_ENDIAN, endian_arch);
*MaxY = gaiaImport64 (buf_shx + 60, GAIA_LITTLE_ENDIAN, endian_arch);
/* 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)
goto error;
if (gaiaImport32 (buf_shp + 0, GAIA_BIG_ENDIAN, endian_arch) != 9994) /* checks the SHP magic number */
goto error;
minx = gaiaImport64 (buf_shp + 36, GAIA_LITTLE_ENDIAN, endian_arch);
miny = gaiaImport64 (buf_shp + 44, GAIA_LITTLE_ENDIAN, endian_arch);
maxx = gaiaImport64 (buf_shp + 52, GAIA_LITTLE_ENDIAN, endian_arch);
maxy = gaiaImport64 (buf_shp + 60, GAIA_LITTLE_ENDIAN, endian_arch);
*mismatching = 0;
if (*MinX != minx || *MinY != miny || *MaxX != maxx || *MaxY != maxy)
{
fprintf (stderr,
"\t\tHEADERS: found mismatching BBOX between .shx and .shp\n");
*mismatching = 1;
}
shape = gaiaImport32 (buf_shp + 32, GAIA_LITTLE_ENDIAN, endian_arch);
if (shape == GAIA_SHP_POINT || shape == GAIA_SHP_POINTZ
|| 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;
/* reading DBF file header */
rd = fread (bf, sizeof (unsigned char), 32, fl_dbf);
if (rd != 32)
goto error;
switch (*bf)
{
/* checks the DBF magic number */
case 0x03:
case 0x83:
break;
case 0x02:
case 0xF8:
sprintf (errMsg, "'%s'\ninvalid magic number %02x [FoxBASE format]",
path, *bf);
goto dbf_bad_magic;
case 0xF5:
sprintf (errMsg,
"'%s'\ninvalid magic number %02x [FoxPro 2.x (or earlier) format]",
path, *bf);
goto dbf_bad_magic;
case 0x30:
case 0x31:
case 0x32:
sprintf (errMsg,
"'%s'\ninvalid magic number %02x [Visual FoxPro format]",
path, *bf);
goto dbf_bad_magic;
case 0x43:
case 0x63:
case 0xBB:
case 0xCB:
sprintf (errMsg, "'%s'\ninvalid magic number %02x [dBASE IV format]",
path, *bf);
goto dbf_bad_magic;
default:
sprintf (errMsg, "'%s'\ninvalid magic number %02x [unknown format]",
path, *bf);
goto dbf_bad_magic;
};
dbf_size = gaiaImport16 (bf + 8, GAIA_LITTLE_ENDIAN, endian_arch);
dbf_reclen = gaiaImport16 (bf + 10, GAIA_LITTLE_ENDIAN, endian_arch);
dbf_size--;
off_dbf = 0;
dbf_list = gaiaAllocDbfList ();
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;
if (*(bf + 11) == 'M')
{
/* skipping any MEMO field */
memcpy (field_name, bf, 11);
field_name[11] = '\0';
off_dbf += *(bf + 16);
fprintf (stderr,
"WARNING: column \"%s\" is of the MEMO type and will be ignored\n",
field_name);
continue;
}
memcpy (field_name, bf, 11);
field_name[11] = '\0';
gaiaAddDbfField (dbf_list, field_name, *(bf + 11), off_dbf,
*(bf + 16), *(bf + 17));
off_dbf += *(bf + 16);
}
if (!gaiaIsValidDbfList (dbf_list))
{
/* invalid DBF */
goto illegal_dbf;
}
len = strlen (path);
shp->Path = malloc (len + 1);
strcpy (shp->Path, path);
shp->ReadOnly = 1;
shp->Shape = shape;
switch (shape)
{
/* setting up a prudential geometry type */
case GAIA_SHP_POINT:
case GAIA_SHP_POINTZ:
case GAIA_SHP_POINTM:
shp->EffectiveType = GAIA_POINT;
break;
case GAIA_SHP_POLYLINE:
case GAIA_SHP_POLYLINEZ:
case GAIA_SHP_POLYLINEM:
shp->EffectiveType = GAIA_MULTILINESTRING;
break;
case GAIA_SHP_POLYGON:
case GAIA_SHP_POLYGONZ:
case GAIA_SHP_POLYGONM:
shp->EffectiveType = GAIA_MULTIPOLYGON;
break;
case GAIA_SHP_MULTIPOINT:
case GAIA_SHP_MULTIPOINTZ:
case GAIA_SHP_MULTIPOINTM:
shp->EffectiveType = GAIA_MULTIPOINT;
break;
}
switch (shape)
{
/* setting up a prudential dimension model */
case GAIA_SHP_POINTZ:
case GAIA_SHP_POLYLINEZ:
case GAIA_SHP_POLYGONZ:
case GAIA_SHP_MULTIPOINTZ:
shp->EffectiveDims = GAIA_XY_Z_M;
break;
case GAIA_SHP_POINTM:
case GAIA_SHP_POLYLINEM:
case GAIA_SHP_POLYGONM:
case GAIA_SHP_MULTIPOINTM:
shp->EffectiveDims = GAIA_XY_M;
break;
default:
shp->EffectiveDims = GAIA_XY;
break;
}
shp->flShp = fl_shp;
shp->flShx = fl_shx;
shp->flDbf = fl_dbf;
shp->Dbf = dbf_list;
/* saving the SHP buffer */
shp->BufShp = buf_shp;
shp->ShpBfsz = buf_size;
/* allocating DBF buffer */
shp->BufDbf = malloc (sizeof (unsigned char) * dbf_reclen);
shp->DbfHdsz = dbf_size + 1;
shp->DbfReclen = dbf_reclen;
shp->Valid = 1;
shp->endian_arch = endian_arch;
return;
unsupported_conversion:
/* illegal charset */
if (shp->LastError)
free (shp->LastError);
len = strlen (errMsg);
shp->LastError = malloc (len + 1);
strcpy (shp->LastError, errMsg);
return;
no_file:
/* one of shapefile's files can't be accessed */
if (shp->LastError)
free (shp->LastError);
len = strlen (errMsg);
shp->LastError = malloc (len + 1);
strcpy (shp->LastError, errMsg);
if (fl_shx)
fclose (fl_shx);
if (fl_shp)
fclose (fl_shp);
if (fl_dbf)
fclose (fl_dbf);
return;
dbf_bad_magic:
/* the DBF has an invalid magin number */
if (shp->LastError)
free (shp->LastError);
len = strlen (errMsg);
shp->LastError = malloc (len + 1);
strcpy (shp->LastError, errMsg);
gaiaFreeDbfList (dbf_list);
if (buf_shp)
free (buf_shp);
fclose (fl_shx);
fclose (fl_shp);
fclose (fl_dbf);
return;
error:
/* the shapefile is invalid or corrupted */
if (shp->LastError)
free (shp->LastError);
sprintf (errMsg, "'%s' is corrupted / has invalid format", path);
len = strlen (errMsg);
shp->LastError = malloc (len + 1);
strcpy (shp->LastError, errMsg);
gaiaFreeDbfList (dbf_list);
if (buf_shp)
free (buf_shp);
fclose (fl_shx);
fclose (fl_shp);
fclose (fl_dbf);
return;
unsupported:
/* the shapefile has an unrecognized shape type */
if (shp->LastError)
free (shp->LastError);
sprintf (errMsg, "'%s' shape=%d is not supported", path, shape);
len = strlen (errMsg);
shp->LastError = malloc (len + 1);
strcpy (shp->LastError, errMsg);
gaiaFreeDbfList (dbf_list);
if (buf_shp)
free (buf_shp);
fclose (fl_shx);
fclose (fl_shp);
if (fl_dbf)
fclose (fl_dbf);
return;
illegal_dbf:
/* the DBF-file contains unsupported data types */
if (shp->LastError)
free (shp->LastError);
sprintf (errMsg, "'%s.dbf' contains unsupported data types", path);
len = strlen (errMsg);
shp->LastError = malloc (len + 1);
strcpy (shp->LastError, errMsg);
gaiaFreeDbfList (dbf_list);
if (buf_shp)
free (buf_shp);
fclose (fl_shx);
fclose (fl_shp);
if (fl_dbf)
fclose (fl_dbf);
return;
}
static int
readShpEntity (gaiaShapefilePtr shp, int current_row, int *shplen, double *minx,
double *miny, double *maxx, double *maxy)
{
/* trying to read an entity from shapefile */
unsigned char buf[512];
int len;
int rd;
int skpos;
int offset;
int off_shp;
int sz;
char errMsg[1024];
int shape;
int endian_arch = gaiaEndianArch ();
/* 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 (shp->flShx, offset, SEEK_SET);
if (skpos != 0)
goto eof;
rd = fread (buf, sizeof (unsigned char), 8, shp->flShx);
if (rd != 8)
goto eof;
off_shp = gaiaImport32 (buf, GAIA_BIG_ENDIAN, shp->endian_arch);
/* positioning and reading the DBF file */
offset = shp->DbfHdsz + (current_row * shp->DbfReclen);
skpos = fseek (shp->flDbf, offset, SEEK_SET);
if (skpos != 0)
goto error;
rd = fread (shp->BufDbf, sizeof (unsigned char), shp->DbfReclen,
shp->flDbf);
if (rd != shp->DbfReclen)
goto error;
if (*(shp->BufDbf) == '*')
goto dbf_deleted;
/* positioning and reading corresponding SHP entity - geometry */
offset = off_shp * 2;
skpos = fseek (shp->flShp, offset, SEEK_SET);
if (skpos != 0)
goto error;
rd = fread (buf, sizeof (unsigned char), 8, shp->flShp);
if (rd != 8)
goto error;
sz = gaiaImport32 (buf + 4, GAIA_BIG_ENDIAN, shp->endian_arch);
if ((sz * 2) > shp->ShpBfsz)
{
/* current buffer is too small; we need to allocate a bigger buffer */
free (shp->BufShp);
shp->ShpBfsz = sz * 2;
shp->BufShp = malloc (sizeof (unsigned char) * shp->ShpBfsz);
}
/* reading the raw Geometry */
rd = fread (shp->BufShp, sizeof (unsigned char), sz * 2, shp->flShp);
if (rd != sz * 2)
goto error;
*shplen = rd;
/* retrieving the feature's BBOX */
shape = gaiaImport32 (shp->BufShp + 0, GAIA_LITTLE_ENDIAN, endian_arch);
*minx = DBL_MAX;
*miny = DBL_MAX;
*maxx = DBL_MAX;
*maxy = DBL_MAX;
if (shape == GAIA_SHP_POINT || shape == GAIA_SHP_POINTZ
|| shape == GAIA_SHP_POINTM)
{
*minx =
gaiaImport64 (shp->BufShp + 4, GAIA_LITTLE_ENDIAN, endian_arch);
*maxx = *minx;
*miny =
gaiaImport64 (shp->BufShp + 12, GAIA_LITTLE_ENDIAN, endian_arch);
*maxy = *miny;
}
if (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)
{
*minx =
gaiaImport64 (shp->BufShp + 4, GAIA_LITTLE_ENDIAN, endian_arch);
*miny =
gaiaImport64 (shp->BufShp + 12, GAIA_LITTLE_ENDIAN, endian_arch);
*maxx =
gaiaImport64 (shp->BufShp + 20, GAIA_LITTLE_ENDIAN, endian_arch);
*maxy =
gaiaImport64 (shp->BufShp + 28, GAIA_LITTLE_ENDIAN, endian_arch);
}
return 1;
eof:
if (shp->LastError)
free (shp->LastError);
shp->LastError = NULL;
return 0;
error:
if (shp->LastError)
free (shp->LastError);
sprintf (errMsg, "'%s' is corrupted / has invalid format", shp->Path);
len = strlen (errMsg);
shp->LastError = malloc (len + 1);
strcpy (shp->LastError, errMsg);
return 0;
dbf_deleted:
if (shp->LastError)
free (shp->LastError);
shp->LastError = NULL;
return -1;
}
struct shp_ring_item
{
/* a RING item [to be reassembled into a (Multi)Polygon] */
gaiaRingPtr Ring;
int IsExterior;
gaiaRingPtr Mother;
struct shp_ring_item *Next;
};
struct shp_ring_collection
{
/* a collection of RING items */
struct shp_ring_item *First;
struct shp_ring_item *Last;
};
static void
shp_free_rings (struct shp_ring_collection *ringsColl)
{
/* memory cleanup: rings collection */
struct shp_ring_item *p;
struct shp_ring_item *pN;
p = ringsColl->First;
while (p)
{
pN = p->Next;
if (p->Ring)
gaiaFreeRing (p->Ring);
free (p);
p = pN;
}
}
static void
shp_add_ring (struct shp_ring_collection *ringsColl, gaiaRingPtr ring)
{
/* inserting a ring into the rings collection */
struct shp_ring_item *p = malloc (sizeof (struct shp_ring_item));
p->Ring = ring;
gaiaMbrRing (ring);
gaiaClockwise (ring);
/* accordingly to SHP rules interior/exterior depends on direction */
p->IsExterior = ring->Clockwise;
p->Mother = NULL;
p->Next = NULL;
/* updating the linked list */
if (ringsColl->First == NULL)
ringsColl->First = p;
if (ringsColl->Last != NULL)
ringsColl->Last->Next = p;
ringsColl->Last = p;
}
static int
shp_check_rings (gaiaRingPtr exterior, gaiaRingPtr candidate)
{
/*
/ speditively checks if the candidate could be an interior Ring
/ contained into the exterior Ring
*/
double z;
double m;
double x0;
double y0;
double x1;
double y1;
int mid;
int ret0;
int ret1;
if (candidate->DimensionModel == GAIA_XY_Z)
{
gaiaGetPointXYZ (candidate->Coords, 0, &x0, &y0, &z);
}
else if (candidate->DimensionModel == GAIA_XY_M)
{
gaiaGetPointXYM (candidate->Coords, 0, &x0, &y0, &m);
}
else if (candidate->DimensionModel == GAIA_XY_Z_M)
{
gaiaGetPointXYZM (candidate->Coords, 0, &x0, &y0, &z, &m);
}
else
{
gaiaGetPoint (candidate->Coords, 0, &x0, &y0);
}
mid = candidate->Points / 2;
if (candidate->DimensionModel == GAIA_XY_Z)
{
gaiaGetPointXYZ (candidate->Coords, mid, &x1, &y1, &z);
}
else if (candidate->DimensionModel == GAIA_XY_M)
{
gaiaGetPointXYM (candidate->Coords, mid, &x1, &y1, &m);
}
else if (candidate->DimensionModel == GAIA_XY_Z_M)
{
gaiaGetPointXYZM (candidate->Coords, mid, &x1, &y1, &z, &m);
}
else
{
gaiaGetPoint (candidate->Coords, mid, &x1, &y1);
}
/* testing if the first point falls on the exterior ring surface */
ret0 = gaiaIsPointOnRingSurface (exterior, x0, y0);
/* testing if the second point falls on the exterior ring surface */
ret1 = gaiaIsPointOnRingSurface (exterior, x1, y1);
if (ret0 || ret1)
return 1;
return 0;
}
static int
shp_mbr_contains (gaiaRingPtr r1, gaiaRingPtr r2)
{
/* checks if the first Ring contains the second one - MBR based */
int ok_1 = 0;
int ok_2 = 0;
int ok_3 = 0;
int ok_4 = 0;
if (r2->MinX >= r1->MinX && r2->MinX <= r1->MaxX)
ok_1 = 1;
if (r2->MaxX >= r1->MinX && r2->MaxX <= r1->MaxX)
ok_2 = 1;
if (r2->MinY >= r1->MinY && r2->MinY <= r1->MaxY)
ok_3 = 1;
if (r2->MaxY >= r1->MinY && r2->MaxY <= r1->MaxY)
ok_4 = 1;
if (ok_1 && ok_2 && ok_3 && ok_4)
return 1;
return 0;
}
static void
shp_arrange_rings (struct shp_ring_collection *ringsColl)
{
/*
/ arranging Rings so to associate any interior ring
/ to the containing exterior ring
*/
struct shp_ring_item *pInt;
struct shp_ring_item *pExt;
pExt = ringsColl->First;
while (pExt != NULL)
{
/* looping on Exterior Rings */
if (pExt->IsExterior)
{
pInt = ringsColl->First;
while (pInt != NULL)
{
/* looping on Interior Rings */
if (pInt->IsExterior == 0 && pInt->Mother == NULL
&& shp_mbr_contains (pExt->Ring, pInt->Ring))
{
/* ok, matches */
if (shp_check_rings (pExt->Ring, pInt->Ring))
pInt->Mother = pExt->Ring;
}
pInt = pInt->Next;
}
}
pExt = pExt->Next;
}
pExt = ringsColl->First;
while (pExt != NULL)
{
if (pExt->IsExterior == 0 && pExt->Mother == NULL)
{
/* orphan ring: promoting to Exterior */
pExt->IsExterior = 1;
}
pExt = pExt->Next;
}
}
static void
shp_build_area (struct shp_ring_collection *ringsColl, gaiaGeomCollPtr geom)
{
/* building the final (Multi)Polygon Geometry */
gaiaPolygonPtr polyg;
struct shp_ring_item *pExt;
struct shp_ring_item *pInt;
pExt = ringsColl->First;
while (pExt != NULL)
{
if (pExt->IsExterior)
{
/* creating a new Polygon */
polyg = gaiaInsertPolygonInGeomColl (geom, pExt->Ring);
pInt = ringsColl->First;
while (pInt != NULL)
{
if (pExt->Ring == pInt->Mother)
{
/* adding an interior ring to current POLYGON */
gaiaAddRingToPolyg (polyg, pInt->Ring);
/* releasing Ring ownership */
pInt->Ring = NULL;
}
pInt = pInt->Next;
}
/* releasing Ring ownership */
pExt->Ring = NULL;
}
pExt = pExt->Next;
}
}
static gaiaGeomCollPtr
do_parse_geometry (const unsigned char *bufshp, int buflen, int eff_dims,
int eff_type, int *nullshape)
{
/* attempting to parse a Geometry from the SHP */
gaiaGeomCollPtr geom = NULL;
int shape;
double x;
double y;
double z;
double m;
int points;
int n;
int n1;
int base;
int baseZ;
int baseM;
int start;
int end;
int iv;
int ind;
int max_size;
int min_size;
int hasM;
int sz;
gaiaLinestringPtr line = NULL;
gaiaRingPtr ring = NULL;
int endian_arch = gaiaEndianArch ();
struct shp_ring_collection ringsColl;
/* initializing the RING collection */
ringsColl.First = NULL;
ringsColl.Last = NULL;
shape = gaiaImport32 (bufshp + 0, GAIA_LITTLE_ENDIAN, endian_arch);
if (shape == GAIA_SHP_NULL)
{
*nullshape = 1;
return NULL;
}
*nullshape = 0;
if (shape == GAIA_SHP_POINT)
{
/* shape point */
x = gaiaImport64 (bufshp + 4, GAIA_LITTLE_ENDIAN, endian_arch);
y = gaiaImport64 (bufshp + 12, GAIA_LITTLE_ENDIAN, endian_arch);
if (eff_dims == GAIA_XY_Z)
{
geom = gaiaAllocGeomCollXYZ ();
gaiaAddPointToGeomCollXYZ (geom, x, y, 0.0);
}
else if (eff_dims == GAIA_XY_M)
{
geom = gaiaAllocGeomCollXYM ();
gaiaAddPointToGeomCollXYM (geom, x, y, 0.0);
}
else if (eff_dims == GAIA_XY_Z_M)
{
geom = gaiaAllocGeomCollXYZM ();
gaiaAddPointToGeomCollXYZM (geom, x, y, 0.0, 0.0);
}
else
{
geom = gaiaAllocGeomColl ();
gaiaAddPointToGeomColl (geom, x, y);
}
geom->DeclaredType = GAIA_POINT;
}
if (shape == GAIA_SHP_POINTZ)
{
/* shape point Z */
x = gaiaImport64 (bufshp + 4, GAIA_LITTLE_ENDIAN, endian_arch);
y = gaiaImport64 (bufshp + 12, GAIA_LITTLE_ENDIAN, endian_arch);
z = gaiaImport64 (bufshp + 20, GAIA_LITTLE_ENDIAN, endian_arch);
if (buflen == 28)
m = 0.0;
else
m = gaiaImport64 (bufshp + 28, GAIA_LITTLE_ENDIAN, endian_arch);
if (eff_dims == GAIA_XY_Z)
{
geom = gaiaAllocGeomCollXYZ ();
gaiaAddPointToGeomCollXYZ (geom, x, y, z);
}
else if (eff_dims == GAIA_XY_M)
{
geom = gaiaAllocGeomCollXYM ();
gaiaAddPointToGeomCollXYM (geom, x, y, m);
}
else if (eff_dims == GAIA_XY_Z_M)
{
geom = gaiaAllocGeomCollXYZM ();
gaiaAddPointToGeomCollXYZM (geom, x, y, z, m);
}
else
{
geom = gaiaAllocGeomColl ();
gaiaAddPointToGeomColl (geom, x, y);
}
geom->DeclaredType = GAIA_POINT;