forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathogrpoint.cpp
769 lines (642 loc) · 22.3 KB
/
ogrpoint.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
/******************************************************************************
*
* Project: OpenGIS Simple Features Reference Implementation
* Purpose: The Point geometry class.
* Author: Frank Warmerdam, [email protected]
*
******************************************************************************
* Copyright (c) 1999, Frank Warmerdam
* Copyright (c) 2008-2011, 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_geometry.h"
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <limits>
#include <new>
#include "cpl_conv.h"
#include "ogr_core.h"
#include "ogr_p.h"
#include "ogr_spatialref.h"
/************************************************************************/
/* GetEmptyNonEmptyFlag() */
/************************************************************************/
static int GetEmptyNonEmptyFlag(double x, double y)
{
if (std::isnan(x) || std::isnan(y))
return 0;
return OGRGeometry::OGR_G_NOT_EMPTY_POINT;
}
/************************************************************************/
/* OGRPoint() */
/************************************************************************/
/**
* \brief Create an empty point.
*/
OGRPoint::OGRPoint() : x(0.0), y(0.0), z(0.0), m(0.0)
{
flags = 0;
}
/************************************************************************/
/* OGRPoint() */
/************************************************************************/
/**
* \brief Create a point.
* @param xIn x
* @param yIn y
* @param zIn z
*/
OGRPoint::OGRPoint(double xIn, double yIn, double zIn)
: x(xIn), y(yIn), z(zIn), m(0.0)
{
flags = GetEmptyNonEmptyFlag(xIn, yIn) | OGR_G_3D;
}
/************************************************************************/
/* OGRPoint() */
/************************************************************************/
/**
* \brief Create a point.
* @param xIn x
* @param yIn y
*/
OGRPoint::OGRPoint(double xIn, double yIn) : x(xIn), y(yIn), z(0.0), m(0.0)
{
flags = GetEmptyNonEmptyFlag(xIn, yIn);
}
/************************************************************************/
/* OGRPoint() */
/************************************************************************/
/**
* \brief Create a point.
* @param xIn x
* @param yIn y
* @param zIn z
* @param mIn m
*/
OGRPoint::OGRPoint(double xIn, double yIn, double zIn, double mIn)
: x(xIn), y(yIn), z(zIn), m(mIn)
{
flags = GetEmptyNonEmptyFlag(xIn, yIn) | OGR_G_3D | OGR_G_MEASURED;
}
/************************************************************************/
/* createXYM() */
/************************************************************************/
/**
* \brief Create a XYM point.
* @param x x
* @param y y
* @param m m
* @since GDAL 3.1
*/
OGRPoint *OGRPoint::createXYM(double x, double y, double m)
{
auto p = new OGRPoint(x, y, 0, m);
p->flags &= ~OGR_G_3D;
return p;
}
/************************************************************************/
/* OGRPoint( const OGRPoint& ) */
/************************************************************************/
/**
* \brief Copy constructor.
*
* Note: before GDAL 2.1, only the default implementation of the constructor
* existed, which could be unsafe to use.
*
* @since GDAL 2.1
*/
OGRPoint::OGRPoint(const OGRPoint &) = default;
/************************************************************************/
/* ~OGRPoint() */
/************************************************************************/
OGRPoint::~OGRPoint() = default;
/************************************************************************/
/* operator=( const OGRPoint& ) */
/************************************************************************/
/**
* \brief Assignment operator.
*
* Note: before GDAL 2.1, only the default implementation of the operator
* existed, which could be unsafe to use.
*
* @since GDAL 2.1
*/
OGRPoint &OGRPoint::operator=(const OGRPoint &other)
{
if (this != &other)
{
OGRGeometry::operator=(other);
x = other.x;
y = other.y;
z = other.z;
m = other.m;
}
return *this;
}
/************************************************************************/
/* clone() */
/* */
/* Make a new object that is a copy of this object. */
/************************************************************************/
OGRPoint *OGRPoint::clone() const
{
return new (std::nothrow) OGRPoint(*this);
}
/************************************************************************/
/* empty() */
/************************************************************************/
void OGRPoint::empty()
{
x = 0.0;
y = 0.0;
z = 0.0;
m = 0.0;
flags &= ~OGR_G_NOT_EMPTY_POINT;
}
/************************************************************************/
/* getDimension() */
/************************************************************************/
int OGRPoint::getDimension() const
{
return 0;
}
/************************************************************************/
/* getGeometryType() */
/************************************************************************/
OGRwkbGeometryType OGRPoint::getGeometryType() const
{
if ((flags & OGR_G_3D) && (flags & OGR_G_MEASURED))
return wkbPointZM;
else if (flags & OGR_G_MEASURED)
return wkbPointM;
else if (flags & OGR_G_3D)
return wkbPoint25D;
else
return wkbPoint;
}
/************************************************************************/
/* getGeometryName() */
/************************************************************************/
const char *OGRPoint::getGeometryName() const
{
return "POINT";
}
/************************************************************************/
/* flattenTo2D() */
/************************************************************************/
void OGRPoint::flattenTo2D()
{
z = 0.0;
m = 0.0;
flags &= ~OGR_G_3D;
setMeasured(FALSE);
}
/************************************************************************/
/* setCoordinateDimension() */
/************************************************************************/
void OGRPoint::setCoordinateDimension(int nNewDimension)
{
if (nNewDimension == 2)
flattenTo2D();
else if (nNewDimension == 3)
flags |= OGR_G_3D;
setMeasured(FALSE);
}
/************************************************************************/
/* WkbSize() */
/* */
/* Return the size of this object in well known binary */
/* representation including the byte order, and type information. */
/************************************************************************/
size_t OGRPoint::WkbSize() const
{
if ((flags & OGR_G_3D) && (flags & OGR_G_MEASURED))
return 37;
else if ((flags & OGR_G_3D) || (flags & OGR_G_MEASURED))
return 29;
else
return 21;
}
/************************************************************************/
/* importFromWkb() */
/* */
/* Initialize from serialized stream in well known binary */
/* format. */
/************************************************************************/
OGRErr OGRPoint::importFromWkb(const unsigned char *pabyData, size_t nSize,
OGRwkbVariant eWkbVariant,
size_t &nBytesConsumedOut)
{
nBytesConsumedOut = 0;
OGRwkbByteOrder eByteOrder = wkbNDR;
flags = 0;
OGRErr eErr =
importPreambleFromWkb(pabyData, nSize, eByteOrder, eWkbVariant);
pabyData += 5;
if (eErr != OGRERR_NONE)
return eErr;
if (nSize != static_cast<size_t>(-1))
{
if ((nSize < 37) && ((flags & OGR_G_3D) && (flags & OGR_G_MEASURED)))
return OGRERR_NOT_ENOUGH_DATA;
else if ((nSize < 29) &&
((flags & OGR_G_3D) || (flags & OGR_G_MEASURED)))
return OGRERR_NOT_ENOUGH_DATA;
else if (nSize < 21)
return OGRERR_NOT_ENOUGH_DATA;
}
nBytesConsumedOut = 5 + 8 * (2 + ((flags & OGR_G_3D) ? 1 : 0) +
((flags & OGR_G_MEASURED) ? 1 : 0));
/* -------------------------------------------------------------------- */
/* Get the vertex. */
/* -------------------------------------------------------------------- */
memcpy(&x, pabyData, 8);
pabyData += 8;
memcpy(&y, pabyData, 8);
pabyData += 8;
if (OGR_SWAP(eByteOrder))
{
CPL_SWAPDOUBLE(&x);
CPL_SWAPDOUBLE(&y);
}
if (flags & OGR_G_3D)
{
memcpy(&z, pabyData, 8);
pabyData += 8;
if (OGR_SWAP(eByteOrder))
CPL_SWAPDOUBLE(&z);
}
else
{
z = 0;
}
if (flags & OGR_G_MEASURED)
{
memcpy(&m, pabyData, 8);
/*pabyData += 8; */
if (OGR_SWAP(eByteOrder))
{
CPL_SWAPDOUBLE(&m);
}
}
else
{
m = 0;
}
// Detect coordinates are not NaN --> NOT EMPTY.
if (!(CPLIsNan(x) && CPLIsNan(y)))
flags |= OGR_G_NOT_EMPTY_POINT;
return OGRERR_NONE;
}
/************************************************************************/
/* exportToWkb() */
/* */
/* Build a well known binary representation of this object. */
/************************************************************************/
OGRErr OGRPoint::exportToWkb(OGRwkbByteOrder eByteOrder,
unsigned char *pabyData,
OGRwkbVariant eWkbVariant) const
{
/* -------------------------------------------------------------------- */
/* Set the byte order. */
/* -------------------------------------------------------------------- */
pabyData[0] =
DB2_V72_UNFIX_BYTE_ORDER(static_cast<unsigned char>(eByteOrder));
pabyData += 1;
/* -------------------------------------------------------------------- */
/* Set the geometry feature type. */
/* -------------------------------------------------------------------- */
GUInt32 nGType = getGeometryType();
if (eWkbVariant == wkbVariantPostGIS1)
{
nGType = wkbFlatten(nGType);
if (Is3D())
// Explicitly set wkb25DBit.
nGType =
static_cast<OGRwkbGeometryType>(nGType | wkb25DBitInternalUse);
if (IsMeasured())
nGType = static_cast<OGRwkbGeometryType>(nGType | 0x40000000);
}
else if (eWkbVariant == wkbVariantIso)
{
nGType = getIsoGeometryType();
}
if (eByteOrder == wkbNDR)
{
CPL_LSBPTR32(&nGType);
}
else
{
CPL_MSBPTR32(&nGType);
}
memcpy(pabyData, &nGType, 4);
pabyData += 4;
/* -------------------------------------------------------------------- */
/* Copy in the raw data. Swap if needed. */
/* -------------------------------------------------------------------- */
if (IsEmpty() && eWkbVariant == wkbVariantIso)
{
const double dNan = std::numeric_limits<double>::quiet_NaN();
memcpy(pabyData, &dNan, 8);
if (OGR_SWAP(eByteOrder))
CPL_SWAPDOUBLE(pabyData);
pabyData += 8;
memcpy(pabyData, &dNan, 8);
if (OGR_SWAP(eByteOrder))
CPL_SWAPDOUBLE(pabyData);
pabyData += 8;
if (flags & OGR_G_3D)
{
memcpy(pabyData, &dNan, 8);
if (OGR_SWAP(eByteOrder))
CPL_SWAPDOUBLE(pabyData);
pabyData += 8;
}
if (flags & OGR_G_MEASURED)
{
memcpy(pabyData, &dNan, 8);
if (OGR_SWAP(eByteOrder))
CPL_SWAPDOUBLE(pabyData);
}
}
else
{
memcpy(pabyData, &x, 8);
if (OGR_SWAP(eByteOrder))
CPL_SWAPDOUBLE(pabyData);
pabyData += 8;
memcpy(pabyData, &y, 8);
if (OGR_SWAP(eByteOrder))
CPL_SWAPDOUBLE(pabyData);
pabyData += 8;
if (flags & OGR_G_3D)
{
memcpy(pabyData, &z, 8);
if (OGR_SWAP(eByteOrder))
CPL_SWAPDOUBLE(pabyData);
pabyData += 8;
}
if (flags & OGR_G_MEASURED)
{
memcpy(pabyData, &m, 8);
if (OGR_SWAP(eByteOrder))
CPL_SWAPDOUBLE(pabyData);
}
}
return OGRERR_NONE;
}
/************************************************************************/
/* importFromWkt() */
/* */
/* Instantiate point from well known text format ``POINT */
/* (x,y)''. */
/************************************************************************/
OGRErr OGRPoint::importFromWkt(const char **ppszInput)
{
int bHasZ = FALSE;
int bHasM = FALSE;
bool bIsEmpty = false;
OGRErr eErr = importPreambleFromWkt(ppszInput, &bHasZ, &bHasM, &bIsEmpty);
flags = 0;
if (eErr != OGRERR_NONE)
return eErr;
if (bHasZ)
flags |= OGR_G_3D;
if (bHasM)
flags |= OGR_G_MEASURED;
if (bIsEmpty)
{
return OGRERR_NONE;
}
else
{
flags |= OGR_G_NOT_EMPTY_POINT;
}
const char *pszInput = *ppszInput;
/* -------------------------------------------------------------------- */
/* Read the point list which should consist of exactly one point. */
/* -------------------------------------------------------------------- */
OGRRawPoint *poPoints = nullptr;
double *padfZ = nullptr;
double *padfM = nullptr;
int nMaxPoint = 0;
int nPoints = 0;
int flagsFromInput = flags;
pszInput = OGRWktReadPointsM(pszInput, &poPoints, &padfZ, &padfM,
&flagsFromInput, &nMaxPoint, &nPoints);
if (pszInput == nullptr || nPoints != 1)
{
CPLFree(poPoints);
CPLFree(padfZ);
CPLFree(padfM);
return OGRERR_CORRUPT_DATA;
}
if ((flagsFromInput & OGR_G_3D) && !(flags & OGR_G_3D))
{
flags |= OGR_G_3D;
bHasZ = TRUE;
}
if ((flagsFromInput & OGR_G_MEASURED) && !(flags & OGR_G_MEASURED))
{
flags |= OGR_G_MEASURED;
bHasM = TRUE;
}
x = poPoints[0].x;
y = poPoints[0].y;
CPLFree(poPoints);
if (bHasZ)
{
if (padfZ != nullptr)
z = padfZ[0];
}
if (bHasM)
{
if (padfM != nullptr)
m = padfM[0];
}
CPLFree(padfZ);
CPLFree(padfM);
*ppszInput = pszInput;
return OGRERR_NONE;
}
/************************************************************************/
/* exportToWkt() */
/* */
/* Translate this structure into its well known text format */
/* equivalent. */
/************************************************************************/
std::string OGRPoint::exportToWkt(const OGRWktOptions &opts, OGRErr *err) const
{
std::string wkt = getGeometryName() + wktTypeString(opts.variant);
if (IsEmpty())
{
wkt += "EMPTY";
}
else
{
wkt += "(";
bool measured = ((opts.variant == wkbVariantIso) && IsMeasured());
wkt += OGRMakeWktCoordinateM(x, y, z, m, Is3D(), measured, opts);
wkt += ")";
}
if (err)
*err = OGRERR_NONE;
return wkt;
}
/************************************************************************/
/* getEnvelope() */
/************************************************************************/
void OGRPoint::getEnvelope(OGREnvelope *psEnvelope) const
{
psEnvelope->MinX = getX();
psEnvelope->MaxX = getX();
psEnvelope->MinY = getY();
psEnvelope->MaxY = getY();
}
/************************************************************************/
/* getEnvelope() */
/************************************************************************/
void OGRPoint::getEnvelope(OGREnvelope3D *psEnvelope) const
{
psEnvelope->MinX = getX();
psEnvelope->MaxX = getX();
psEnvelope->MinY = getY();
psEnvelope->MaxY = getY();
psEnvelope->MinZ = getZ();
psEnvelope->MaxZ = getZ();
}
/**
* \fn double OGRPoint::getX() const;
*
* \brief Fetch X coordinate.
*
* Relates to the SFCOM IPoint::get_X() method.
*
* @return the X coordinate of this point.
*/
/**
* \fn double OGRPoint::getY() const;
*
* \brief Fetch Y coordinate.
*
* Relates to the SFCOM IPoint::get_Y() method.
*
* @return the Y coordinate of this point.
*/
/**
* \fn double OGRPoint::getZ() const;
*
* \brief Fetch Z coordinate.
*
* Relates to the SFCOM IPoint::get_Z() method.
*
* @return the Z coordinate of this point, or zero if it is a 2D point.
*/
/**
* \fn void OGRPoint::setX( double xIn );
*
* \brief Assign point X coordinate.
*
* There is no corresponding SFCOM method.
*/
/**
* \fn void OGRPoint::setY( double yIn );
*
* \brief Assign point Y coordinate.
*
* There is no corresponding SFCOM method.
*/
/**
* \fn void OGRPoint::setZ( double zIn );
*
* \brief Assign point Z coordinate.
* Calling this method will force the geometry
* coordinate dimension to 3D (wkbPoint|wkbZ).
*
* There is no corresponding SFCOM method.
*/
/************************************************************************/
/* Equal() */
/************************************************************************/
OGRBoolean OGRPoint::Equals(const OGRGeometry *poOther) const
{
if (poOther == this)
return TRUE;
if (poOther->getGeometryType() != getGeometryType())
return FALSE;
const auto poOPoint = poOther->toPoint();
if (flags != poOPoint->flags)
return FALSE;
if (IsEmpty())
return TRUE;
// Should eventually test the SRS.
if (poOPoint->getX() != getX() || poOPoint->getY() != getY() ||
poOPoint->getZ() != getZ())
return FALSE;
return TRUE;
}
/************************************************************************/
/* transform() */
/************************************************************************/
OGRErr OGRPoint::transform(OGRCoordinateTransformation *poCT)
{
if (poCT->Transform(1, &x, &y, &z))
{
assignSpatialReference(poCT->GetTargetCS());
return OGRERR_NONE;
}
return OGRERR_FAILURE;
}
/************************************************************************/
/* swapXY() */
/************************************************************************/
void OGRPoint::swapXY()
{
std::swap(x, y);
}
/************************************************************************/
/* Within() */
/************************************************************************/
OGRBoolean OGRPoint::Within(const OGRGeometry *poOtherGeom) const
{
if (!IsEmpty() && poOtherGeom != nullptr &&
wkbFlatten(poOtherGeom->getGeometryType()) == wkbCurvePolygon)
{
const auto poCurve = poOtherGeom->toCurvePolygon();
return poCurve->Contains(this);
}
return OGRGeometry::Within(poOtherGeom);
}
/************************************************************************/
/* Intersects() */
/************************************************************************/
OGRBoolean OGRPoint::Intersects(const OGRGeometry *poOtherGeom) const
{
if (!IsEmpty() && poOtherGeom != nullptr &&
wkbFlatten(poOtherGeom->getGeometryType()) == wkbCurvePolygon)
{
const auto poCurve = poOtherGeom->toCurvePolygon();
return poCurve->Intersects(this);
}
return OGRGeometry::Intersects(poOtherGeom);
}