forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathogrutils.cpp
2091 lines (1858 loc) · 74 KB
/
ogrutils.cpp
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: OpenGIS Simple Features Reference Implementation
* Purpose: Utility functions for OGR classes, including some related to
* parsing well known text format vectors.
* Author: Frank Warmerdam, [email protected]
*
******************************************************************************
* Copyright (c) 1999, Frank Warmerdam
* Copyright (c) 2008-2014, Even Rouault <even dot rouault at spatialys.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "cpl_port.h"
#include "ogr_p.h"
#include <cassert>
#include <cmath>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <limits>
#include <sstream>
#include <iomanip>
#include "cpl_conv.h"
#include "cpl_error.h"
#include "cpl_string.h"
#include "cpl_time.h"
#include "cpl_vsi.h"
#include "gdal.h"
#include "ogr_core.h"
#include "ogr_geometry.h"
#include "ogrsf_frmts.h"
// Returns whether a double fits within an int.
// Unable to put this in cpl_port.h as include limit breaks grib.
inline bool CPLIsDoubleAnInt(double d)
{
// Write it this way to detect NaN
if (!(d >= std::numeric_limits<int>::min() &&
d <= std::numeric_limits<int>::max()))
{
return false;
}
return d == static_cast<double>(static_cast<int>(d));
}
namespace
{
// Remove trailing zeros except the last one.
std::string removeTrailingZeros(std::string s)
{
auto pos = s.find('.');
if (pos == std::string::npos)
return s;
// Remove zeros at the end. We know this won't be npos because we
// have a decimal point.
auto nzpos = s.find_last_not_of('0');
s = s.substr(0, nzpos + 1);
// Make sure there is one 0 after the decimal point.
if (s.back() == '.')
s += '0';
return s;
}
// Round a string representing a number by 1 in the least significant digit.
std::string roundup(std::string s)
{
// Remove a negative sign if it exists to make processing
// more straigtforward.
bool negative(false);
if (s[0] == '-')
{
negative = true;
s = s.substr(1);
}
// Go from the back to the front. If we increment a digit other than
// a '9', we're done. If we increment a '9', set it to a '0' and move
// to the next (more significant) digit. If we get to the front of the
// string, add a '1' to the front of the string.
for (int pos = static_cast<int>(s.size() - 1); pos >= 0; pos--)
{
if (s[pos] == '.')
continue;
s[pos]++;
// Incrementing past 9 gets you a colon in ASCII.
if (s[pos] != ':')
break;
else
s[pos] = '0';
if (pos == 0)
s = '1' + s;
}
if (negative)
s = '-' + s;
return s;
}
// This attempts to eliminate what is likely binary -> decimal representation
// error or the result of low-order rounding with calculations. The result
// may be more visually pleasing and takes up fewer places.
std::string intelliround(std::string &s)
{
// If there is no decimal point, just return.
auto dotPos = s.find(".");
if (dotPos == std::string::npos)
return s;
// Don't mess with exponential formatting.
if (s.find_first_of("eE") != std::string::npos)
return s;
size_t iDotPos = static_cast<size_t>(dotPos);
size_t nCountBeforeDot = iDotPos - 1;
if (s[0] == '-')
nCountBeforeDot--;
size_t i = s.size();
// If we don't have ten characters, don't do anything.
if (i <= 10)
return s;
/* -------------------------------------------------------------------- */
/* Trim trailing 00000x's as they are likely roundoff error. */
/* -------------------------------------------------------------------- */
if (s[i - 2] == '0' && s[i - 3] == '0' && s[i - 4] == '0' &&
s[i - 5] == '0' && s[i - 6] == '0')
{
s.resize(s.size() - 1);
}
// I don't understand this case exactly. It's like saying if the
// value is large enough and there are sufficient sig digits before
// a bunch of zeros, remove the zeros and any digits at the end that
// may be nonzero. Perhaps if we can't exactly explain in words what
// we're doing here, we shouldn't do it? Perhaps it should
// be generalized?
// The value "12345.000000011" invokes this case, if anyone
// is interested.
else if (iDotPos < i - 8 && (nCountBeforeDot >= 4 || s[i - 3] == '0') &&
(nCountBeforeDot >= 5 || s[i - 4] == '0') &&
(nCountBeforeDot >= 6 || s[i - 5] == '0') &&
(nCountBeforeDot >= 7 || s[i - 6] == '0') &&
(nCountBeforeDot >= 8 || s[i - 7] == '0') && s[i - 8] == '0' &&
s[i - 9] == '0')
{
s.resize(s.size() - 8);
}
/* -------------------------------------------------------------------- */
/* Trim trailing 99999x's as they are likely roundoff error. */
/* -------------------------------------------------------------------- */
else if (s[i - 2] == '9' && s[i - 3] == '9' && s[i - 4] == '9' &&
s[i - 5] == '9' && s[i - 6] == '9')
{
s.resize(i - 6);
s = roundup(s);
}
else if (iDotPos < i - 9 && (nCountBeforeDot >= 4 || s[i - 3] == '9') &&
(nCountBeforeDot >= 5 || s[i - 4] == '9') &&
(nCountBeforeDot >= 6 || s[i - 5] == '9') &&
(nCountBeforeDot >= 7 || s[i - 6] == '9') &&
(nCountBeforeDot >= 8 || s[i - 7] == '9') && s[i - 8] == '9' &&
s[i - 9] == '9')
{
s.resize(i - 9);
s = roundup(s);
}
return s;
}
} // unnamed namespace
/************************************************************************/
/* OGRFormatDouble() */
/************************************************************************/
void OGRFormatDouble(char *pszBuffer, int nBufferLen, double dfVal,
char chDecimalSep, int nPrecision,
char chConversionSpecifier)
{
OGRWktOptions opts;
opts.precision = nPrecision;
opts.format = (chConversionSpecifier == 'g' || chConversionSpecifier == 'G')
? OGRWktFormat::G
: OGRWktFormat::F;
std::string s = OGRFormatDouble(dfVal, opts);
if (chDecimalSep != '\0' && chDecimalSep != '.')
{
auto pos = s.find('.');
if (pos != std::string::npos)
s.replace(pos, 1, std::string(1, chDecimalSep));
}
if (s.size() + 1 > static_cast<size_t>(nBufferLen))
{
CPLError(CE_Warning, CPLE_AppDefined,
"Truncated double value %s to "
"%s.",
s.data(), s.substr(0, nBufferLen - 1).data());
s.resize(nBufferLen - 1);
}
strcpy(pszBuffer, s.data());
}
/// Simplified OGRFormatDouble that can be made to adhere to provided
/// options.
std::string OGRFormatDouble(double val, const OGRWktOptions &opts)
{
// So to have identical cross platform representation.
if (std::isinf(val))
return (val > 0) ? "inf" : "-inf";
if (std::isnan(val))
return "nan";
std::ostringstream oss;
oss.imbue(std::locale::classic()); // Make sure we output decimal points.
bool l_round(opts.round);
if (opts.format == OGRWktFormat::F ||
(opts.format == OGRWktFormat::Default && fabs(val) < 1))
oss << std::fixed;
else
{
// Uppercase because OGC spec says capital 'E'.
oss << std::uppercase;
l_round = false;
}
oss << std::setprecision(opts.precision);
oss << val;
std::string sval = oss.str();
if (l_round)
sval = intelliround(sval);
return removeTrailingZeros(sval);
}
/************************************************************************/
/* OGRMakeWktCoordinate() */
/* */
/* Format a well known text coordinate, trying to keep the */
/* ASCII representation compact, but accurate. These rules */
/* will have to tighten up in the future. */
/* */
/* Currently a new point should require no more than 64 */
/* characters barring the X or Y value being extremely large. */
/************************************************************************/
void OGRMakeWktCoordinate(char *pszTarget, double x, double y, double z,
int nDimension)
{
std::string wkt =
OGRMakeWktCoordinate(x, y, z, nDimension, OGRWktOptions());
memcpy(pszTarget, wkt.data(), wkt.size() + 1);
}
static bool isInteger(const std::string &s)
{
return s.find_first_not_of("0123456789") == std::string::npos;
}
std::string OGRMakeWktCoordinate(double x, double y, double z, int nDimension,
OGRWktOptions opts)
{
std::string wkt;
// Why do we do this? Seems especially strange since we're ADDING
// ".0" onto values in the case below. The "&&" here also seems strange.
if (opts.format == OGRWktFormat::Default && CPLIsDoubleAnInt(x) &&
CPLIsDoubleAnInt(y))
{
wkt = std::to_string(static_cast<int>(x));
wkt += ' ';
wkt += std::to_string(static_cast<int>(y));
}
else
{
wkt = OGRFormatDouble(x, opts);
// ABELL - Why do we do special formatting?
if (isInteger(wkt))
wkt += ".0";
wkt += ' ';
std::string yval = OGRFormatDouble(y, opts);
if (isInteger(yval))
yval += ".0";
wkt += yval;
}
// Why do we always format Z with type G.
if (nDimension == 3)
{
wkt += ' ';
if (opts.format == OGRWktFormat::Default && CPLIsDoubleAnInt(z))
wkt += std::to_string(static_cast<int>(z));
else
{
opts.format = OGRWktFormat::G;
wkt += OGRFormatDouble(z, opts);
}
}
return wkt;
}
/************************************************************************/
/* OGRMakeWktCoordinateM() */
/* */
/* Format a well known text coordinate, trying to keep the */
/* ASCII representation compact, but accurate. These rules */
/* will have to tighten up in the future. */
/* */
/* Currently a new point should require no more than 64 */
/* characters barring the X or Y value being extremely large. */
/************************************************************************/
void OGRMakeWktCoordinateM(char *pszTarget, double x, double y, double z,
double m, OGRBoolean hasZ, OGRBoolean hasM)
{
std::string wkt =
OGRMakeWktCoordinateM(x, y, z, m, hasZ, hasM, OGRWktOptions());
memcpy(pszTarget, wkt.data(), wkt.size() + 1);
}
std::string OGRMakeWktCoordinateM(double x, double y, double z, double m,
OGRBoolean hasZ, OGRBoolean hasM,
OGRWktOptions opts)
{
std::string wkt;
if (opts.format == OGRWktFormat::Default && CPLIsDoubleAnInt(x) &&
CPLIsDoubleAnInt(y))
{
wkt = std::to_string(static_cast<int>(x));
wkt += ' ';
wkt += std::to_string(static_cast<int>(y));
}
else
{
wkt = OGRFormatDouble(x, opts);
if (isInteger(wkt))
wkt += ".0";
wkt += ' ';
std::string yval = OGRFormatDouble(y, opts);
if (isInteger(yval))
yval += ".0";
wkt += yval;
}
// For some reason we always format Z and M as G-type
opts.format = OGRWktFormat::G;
if (hasZ)
{
/*if( opts.format == OGRWktFormat::Default && CPLIsDoubleAnInt(z) )
wkt += " " + std::to_string(static_cast<int>(z));
else*/
wkt += ' ';
wkt += OGRFormatDouble(z, opts);
}
if (hasM)
{
/*if( opts.format == OGRWktFormat::Default && CPLIsDoubleAnInt(m) )
wkt += " " + std::to_string(static_cast<int>(m));
else*/
wkt += ' ';
wkt += OGRFormatDouble(m, opts);
}
return wkt;
}
/************************************************************************/
/* OGRWktReadToken() */
/* */
/* Read one token or delimiter and put into token buffer. Pre */
/* and post white space is swallowed. */
/************************************************************************/
const char *OGRWktReadToken(const char *pszInput, char *pszToken)
{
if (pszInput == nullptr)
return nullptr;
/* -------------------------------------------------------------------- */
/* Swallow pre-white space. */
/* -------------------------------------------------------------------- */
while (*pszInput == ' ' || *pszInput == '\t' || *pszInput == '\n' ||
*pszInput == '\r')
++pszInput;
/* -------------------------------------------------------------------- */
/* If this is a delimiter, read just one character. */
/* -------------------------------------------------------------------- */
if (*pszInput == '(' || *pszInput == ')' || *pszInput == ',')
{
pszToken[0] = *pszInput;
pszToken[1] = '\0';
++pszInput;
}
/* -------------------------------------------------------------------- */
/* Or if it alpha numeric read till we reach non-alpha numeric */
/* text. */
/* -------------------------------------------------------------------- */
else
{
int iChar = 0;
while (iChar < OGR_WKT_TOKEN_MAX - 1 &&
((*pszInput >= 'a' && *pszInput <= 'z') ||
(*pszInput >= 'A' && *pszInput <= 'Z') ||
(*pszInput >= '0' && *pszInput <= '9') || *pszInput == '.' ||
*pszInput == '+' || *pszInput == '-'))
{
pszToken[iChar++] = *(pszInput++);
}
pszToken[iChar++] = '\0';
}
/* -------------------------------------------------------------------- */
/* Eat any trailing white space. */
/* -------------------------------------------------------------------- */
while (*pszInput == ' ' || *pszInput == '\t' || *pszInput == '\n' ||
*pszInput == '\r')
++pszInput;
return pszInput;
}
/************************************************************************/
/* OGRWktReadPoints() */
/* */
/* Read a point string. The point list must be contained in */
/* brackets and each point pair separated by a comma. */
/************************************************************************/
const char *OGRWktReadPoints(const char *pszInput, OGRRawPoint **ppaoPoints,
double **ppadfZ, int *pnMaxPoints,
int *pnPointsRead)
{
const char *pszOrigInput = pszInput;
*pnPointsRead = 0;
if (pszInput == nullptr)
return nullptr;
/* -------------------------------------------------------------------- */
/* Eat any leading white space. */
/* -------------------------------------------------------------------- */
while (*pszInput == ' ' || *pszInput == '\t')
++pszInput;
/* -------------------------------------------------------------------- */
/* If this isn't an opening bracket then we have a problem. */
/* -------------------------------------------------------------------- */
if (*pszInput != '(')
{
CPLDebug("OGR", "Expected '(', but got %s in OGRWktReadPoints().",
pszInput);
return pszInput;
}
++pszInput;
/* ==================================================================== */
/* This loop reads a single point. It will continue till we */
/* run out of well formed points, or a closing bracket is */
/* encountered. */
/* ==================================================================== */
char szDelim[OGR_WKT_TOKEN_MAX] = {};
do
{
/* --------------------------------------------------------------------
*/
/* Read the X and Y values, verify they are numeric. */
/* --------------------------------------------------------------------
*/
char szTokenX[OGR_WKT_TOKEN_MAX] = {};
char szTokenY[OGR_WKT_TOKEN_MAX] = {};
pszInput = OGRWktReadToken(pszInput, szTokenX);
pszInput = OGRWktReadToken(pszInput, szTokenY);
if ((!isdigit(szTokenX[0]) && szTokenX[0] != '-' &&
szTokenX[0] != '.') ||
(!isdigit(szTokenY[0]) && szTokenY[0] != '-' && szTokenY[0] != '.'))
return nullptr;
/* --------------------------------------------------------------------
*/
/* Do we need to grow the point list to hold this point? */
/* --------------------------------------------------------------------
*/
if (*pnPointsRead == *pnMaxPoints)
{
*pnMaxPoints = *pnMaxPoints * 2 + 10;
*ppaoPoints = static_cast<OGRRawPoint *>(
CPLRealloc(*ppaoPoints, sizeof(OGRRawPoint) * *pnMaxPoints));
if (*ppadfZ != nullptr)
{
*ppadfZ = static_cast<double *>(
CPLRealloc(*ppadfZ, sizeof(double) * *pnMaxPoints));
}
}
/* --------------------------------------------------------------------
*/
/* Add point to list. */
/* --------------------------------------------------------------------
*/
(*ppaoPoints)[*pnPointsRead].x = CPLAtof(szTokenX);
(*ppaoPoints)[*pnPointsRead].y = CPLAtof(szTokenY);
/* --------------------------------------------------------------------
*/
/* Do we have a Z coordinate? */
/* --------------------------------------------------------------------
*/
pszInput = OGRWktReadToken(pszInput, szDelim);
if (isdigit(szDelim[0]) || szDelim[0] == '-' || szDelim[0] == '.')
{
if (*ppadfZ == nullptr)
{
*ppadfZ = static_cast<double *>(
CPLCalloc(sizeof(double), *pnMaxPoints));
}
(*ppadfZ)[*pnPointsRead] = CPLAtof(szDelim);
pszInput = OGRWktReadToken(pszInput, szDelim);
}
else if (*ppadfZ != nullptr)
{
(*ppadfZ)[*pnPointsRead] = 0.0;
}
++(*pnPointsRead);
/* --------------------------------------------------------------------
*/
/* Do we have a M coordinate? */
/* If we do, just skip it. */
/* --------------------------------------------------------------------
*/
if (isdigit(szDelim[0]) || szDelim[0] == '-' || szDelim[0] == '.')
{
pszInput = OGRWktReadToken(pszInput, szDelim);
}
/* --------------------------------------------------------------------
*/
/* Read next delimiter ... it should be a comma if there are */
/* more points. */
/* --------------------------------------------------------------------
*/
if (szDelim[0] != ')' && szDelim[0] != ',')
{
CPLDebug("OGR",
"Corrupt input in OGRWktReadPoints(). "
"Got `%s' when expecting `,' or `)', near `%s' in %s.",
szDelim, pszInput, pszOrigInput);
return nullptr;
}
} while (szDelim[0] == ',');
return pszInput;
}
/************************************************************************/
/* OGRWktReadPointsM() */
/* */
/* Read a point string. The point list must be contained in */
/* brackets and each point pair separated by a comma. */
/************************************************************************/
const char *OGRWktReadPointsM(const char *pszInput, OGRRawPoint **ppaoPoints,
double **ppadfZ, double **ppadfM, int *flags,
int *pnMaxPoints, int *pnPointsRead)
{
const char *pszOrigInput = pszInput;
const bool bNoFlags = !(*flags & OGRGeometry::OGR_G_3D) &&
!(*flags & OGRGeometry::OGR_G_MEASURED);
*pnPointsRead = 0;
if (pszInput == nullptr)
return nullptr;
/* -------------------------------------------------------------------- */
/* Eat any leading white space. */
/* -------------------------------------------------------------------- */
while (*pszInput == ' ' || *pszInput == '\t')
++pszInput;
/* -------------------------------------------------------------------- */
/* If this isn't an opening bracket then we have a problem. */
/* -------------------------------------------------------------------- */
if (*pszInput != '(')
{
CPLDebug("OGR", "Expected '(', but got %s in OGRWktReadPointsM().",
pszInput);
return pszInput;
}
++pszInput;
/* ==================================================================== */
/* This loop reads a single point. It will continue till we */
/* run out of well formed points, or a closing bracket is */
/* encountered. */
/* ==================================================================== */
char szDelim[OGR_WKT_TOKEN_MAX] = {};
do
{
/* --------------------------------------------------------------------
*/
/* Read the X and Y values, verify they are numeric. */
/* --------------------------------------------------------------------
*/
char szTokenX[OGR_WKT_TOKEN_MAX] = {};
char szTokenY[OGR_WKT_TOKEN_MAX] = {};
pszInput = OGRWktReadToken(pszInput, szTokenX);
pszInput = OGRWktReadToken(pszInput, szTokenY);
if ((!isdigit(szTokenX[0]) && szTokenX[0] != '-' &&
szTokenX[0] != '.' && !EQUAL(szTokenX, "nan")) ||
(!isdigit(szTokenY[0]) && szTokenY[0] != '-' &&
szTokenY[0] != '.' && !EQUAL(szTokenY, "nan")))
return nullptr;
/* --------------------------------------------------------------------
*/
/* Do we need to grow the point list to hold this point? */
/* --------------------------------------------------------------------
*/
if (*pnPointsRead == *pnMaxPoints)
{
*pnMaxPoints = *pnMaxPoints * 2 + 10;
*ppaoPoints = static_cast<OGRRawPoint *>(
CPLRealloc(*ppaoPoints, sizeof(OGRRawPoint) * *pnMaxPoints));
if (*ppadfZ != nullptr)
{
*ppadfZ = static_cast<double *>(
CPLRealloc(*ppadfZ, sizeof(double) * *pnMaxPoints));
}
if (*ppadfM != nullptr)
{
*ppadfM = static_cast<double *>(
CPLRealloc(*ppadfM, sizeof(double) * *pnMaxPoints));
}
}
/* --------------------------------------------------------------------
*/
/* Add point to list. */
/* --------------------------------------------------------------------
*/
(*ppaoPoints)[*pnPointsRead].x = CPLAtof(szTokenX);
(*ppaoPoints)[*pnPointsRead].y = CPLAtof(szTokenY);
/* --------------------------------------------------------------------
*/
/* Read the next token. */
/* --------------------------------------------------------------------
*/
pszInput = OGRWktReadToken(pszInput, szDelim);
/* --------------------------------------------------------------------
*/
/* If there are unexpectedly more coordinates, they are Z. */
/* --------------------------------------------------------------------
*/
if (!(*flags & OGRGeometry::OGR_G_3D) &&
!(*flags & OGRGeometry::OGR_G_MEASURED) &&
(isdigit(szDelim[0]) || szDelim[0] == '-' || szDelim[0] == '.' ||
EQUAL(szDelim, "nan")))
{
*flags |= OGRGeometry::OGR_G_3D;
}
/* --------------------------------------------------------------------
*/
/* Get Z if flag says so. */
/* Zero out possible remains from earlier strings. */
/* --------------------------------------------------------------------
*/
if (*flags & OGRGeometry::OGR_G_3D)
{
if (*ppadfZ == nullptr)
{
*ppadfZ = static_cast<double *>(
CPLCalloc(sizeof(double), *pnMaxPoints));
}
if (isdigit(szDelim[0]) || szDelim[0] == '-' || szDelim[0] == '.' ||
EQUAL(szDelim, "nan"))
{
(*ppadfZ)[*pnPointsRead] = CPLAtof(szDelim);
pszInput = OGRWktReadToken(pszInput, szDelim);
}
else
{
(*ppadfZ)[*pnPointsRead] = 0.0;
}
}
else if (*ppadfZ != nullptr)
{
(*ppadfZ)[*pnPointsRead] = 0.0;
}
/* --------------------------------------------------------------------
*/
/* If there are unexpectedly even more coordinates, */
/* they are discarded unless there were no flags originally. */
/* This is for backwards compatibility. Should this be an error? */
/* --------------------------------------------------------------------
*/
if (!(*flags & OGRGeometry::OGR_G_MEASURED) &&
(isdigit(szDelim[0]) || szDelim[0] == '-' || szDelim[0] == '.' ||
EQUAL(szDelim, "nan")))
{
if (bNoFlags)
{
*flags |= OGRGeometry::OGR_G_MEASURED;
}
else
{
pszInput = OGRWktReadToken(pszInput, szDelim);
}
}
/* --------------------------------------------------------------------
*/
/* Get M if flag says so. */
/* Zero out possible remains from earlier strings. */
/* --------------------------------------------------------------------
*/
if (*flags & OGRGeometry::OGR_G_MEASURED)
{
if (*ppadfM == nullptr)
{
*ppadfM = static_cast<double *>(
CPLCalloc(sizeof(double), *pnMaxPoints));
}
if (isdigit(szDelim[0]) || szDelim[0] == '-' || szDelim[0] == '.' ||
EQUAL(szDelim, "nan"))
{
(*ppadfM)[*pnPointsRead] = CPLAtof(szDelim);
pszInput = OGRWktReadToken(pszInput, szDelim);
}
else
{
(*ppadfM)[*pnPointsRead] = 0.0;
}
}
else if (*ppadfM != nullptr)
{
(*ppadfM)[*pnPointsRead] = 0.0;
}
/* --------------------------------------------------------------------
*/
/* If there are still more coordinates and we do not have Z */
/* then we have a case of flags == M and four coordinates. */
/* This is allowed in BNF. */
/* --------------------------------------------------------------------
*/
if (!(*flags & OGRGeometry::OGR_G_3D) &&
(isdigit(szDelim[0]) || szDelim[0] == '-' || szDelim[0] == '.' ||
EQUAL(szDelim, "nan")))
{
*flags |= OGRGeometry::OGR_G_3D;
if (*ppadfZ == nullptr)
{
*ppadfZ = static_cast<double *>(
CPLCalloc(sizeof(double), *pnMaxPoints));
}
(*ppadfZ)[*pnPointsRead] = (*ppadfM)[*pnPointsRead];
(*ppadfM)[*pnPointsRead] = CPLAtof(szDelim);
pszInput = OGRWktReadToken(pszInput, szDelim);
}
/* --------------------------------------------------------------------
*/
/* Increase points index. */
/* --------------------------------------------------------------------
*/
++(*pnPointsRead);
/* --------------------------------------------------------------------
*/
/* The next delimiter should be a comma or an ending bracket. */
/* --------------------------------------------------------------------
*/
if (szDelim[0] != ')' && szDelim[0] != ',')
{
CPLDebug("OGR",
"Corrupt input in OGRWktReadPointsM() "
"Got `%s' when expecting `,' or `)', near `%s' in %s.",
szDelim, pszInput, pszOrigInput);
return nullptr;
}
} while (szDelim[0] == ',');
return pszInput;
}
/************************************************************************/
/* OGRMalloc() */
/* */
/* Cover for CPLMalloc() */
/************************************************************************/
void *OGRMalloc(size_t size)
{
return CPLMalloc(size);
}
/************************************************************************/
/* OGRCalloc() */
/* */
/* Cover for CPLCalloc() */
/************************************************************************/
void *OGRCalloc(size_t count, size_t size)
{
return CPLCalloc(count, size);
}
/************************************************************************/
/* OGRRealloc() */
/* */
/* Cover for CPLRealloc() */
/************************************************************************/
void *OGRRealloc(void *pOld, size_t size)
{
return CPLRealloc(pOld, size);
}
/************************************************************************/
/* OGRFree() */
/* */
/* Cover for CPLFree(). */
/************************************************************************/
void OGRFree(void *pMemory)
{
CPLFree(pMemory);
}
/**
* \fn OGRGeneralCmdLineProcessor(int, char***, int)
* General utility option processing.
*
* This function is intended to provide a variety of generic commandline
* options for all OGR commandline utilities. It takes care of the following
* commandline options:
*
* --version: report version of GDAL in use.
* --license: report GDAL license info.
* --format [format]: report details of one format driver.
* --formats: report all format drivers configured.
* --optfile filename: expand an option file into the argument list.
* --config key value: set system configuration option.
* --debug [on/off/value]: set debug level.
* --pause: Pause for user input (allows time to attach debugger)
* --locale [locale]: Install a locale using setlocale() (debugging)
* --help-general: report detailed help on general options.
*
* The argument array is replaced "in place" and should be freed with
* CSLDestroy() when no longer needed. The typical usage looks something
* like the following. Note that the formats should be registered so that
* the --formats option will work properly.
*
* int main( int argc, char ** argv )
* {
* OGRRegisterAll();
*
* argc = OGRGeneralCmdLineProcessor( argc, &argv, 0 );
* if( argc < 1 )
* exit( -argc );
*
* @param nArgc number of values in the argument list.
* @param ppapszArgv pointer to the argument list array (will be updated in
* place).
* @param nOptions unused.
*
* @return updated nArgc argument count. Return of 0 requests terminate
* without error, return of -1 requests exit with error code.
*/
int OGRGeneralCmdLineProcessor(int nArgc, char ***ppapszArgv,
CPL_UNUSED int nOptions)
{
return GDALGeneralCmdLineProcessor(nArgc, ppapszArgv, GDAL_OF_VECTOR);
}
/************************************************************************/
/* OGRParseDate() */
/* */
/* Parse a variety of text date formats into an OGRField. */
/************************************************************************/
/**
* Parse date string.
*
* This function attempts to parse a date string in a variety of formats
* into the OGRField.Date format suitable for use with OGR. Generally
* speaking this function is expecting values like:
*
* YYYY-MM-DD HH:MM:SS[.sss]+nn
* or YYYY-MM-DDTHH:MM:SS[.sss]Z (ISO 8601 format)
* or YYYY-MM-DDZ
*
* The seconds may also have a decimal portion (parsed as milliseconds). And
* just dates (YYYY-MM-DD) or just times (HH:MM:SS[.sss]) are also supported.
* The date may also be in YYYY/MM/DD format. If the year is less than 100
* and greater than 30 a "1900" century value will be set. If it is less than
* 30 and greater than -1 then a "2000" century value will be set. In
* the future this function may be generalized, and additional control
* provided through nOptions, but an nOptions value of "0" should always do
* a reasonable default form of processing.
*
* The value of psField will be indeterminate if the function fails (returns
* FALSE).
*
* @param pszInput the input date string.
* @param psField the OGRField that will be updated with the parsed result.
* @param nOptions parsing options, for now always 0.
*
* @return TRUE if apparently successful or FALSE on failure.
*/
int OGRParseDate(const char *pszInput, OGRField *psField,
CPL_UNUSED int nOptions)
{
psField->Date.Year = 0;
psField->Date.Month = 0;
psField->Date.Day = 0;
psField->Date.Hour = 0;
psField->Date.Minute = 0;
psField->Date.Second = 0;
psField->Date.TZFlag = 0;
psField->Date.Reserved = 0;
/* -------------------------------------------------------------------- */
/* Do we have a date? */
/* -------------------------------------------------------------------- */
while (*pszInput == ' ')
++pszInput;
bool bGotSomething = false;
if (strstr(pszInput, "-") != nullptr || strstr(pszInput, "/") != nullptr)