-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathGeoShape.cpp
More file actions
546 lines (497 loc) · 15.7 KB
/
GeoShape.cpp
File metadata and controls
546 lines (497 loc) · 15.7 KB
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
#include "stdafx.h"
#include "GeoShape.h"
#include "GeographLibHelper.h"
#include "GeometryHelper.h"
// *******************************************************
// TestAreaCalc()
// *******************************************************
void TestAreaCalc()
{
std::vector<Point2D> points;
points.push_back(Point2D(5.1, 42.6));
points.push_back(Point2D(7.3, 41.6));
points.push_back(Point2D(4.2, 40.4));
double area = GeographLibHelper::CalcPolyGeodesicArea(points);
Debug::WriteLine("Test area calc: %f", area);
};
// *******************************************************
// GetGeodesicArea()
// *******************************************************
double GeoShape::GetGeodesicArea(bool closingPoint, double x, double y)
{
unsigned int result;
double perimeter = 0.0, area = 0.0;
if (GetPolyPointCount(closingPoint) > 1)
{
recalc:
if (_areaRecalcIsNeeded || !IsDynamic())
{
_areaRecalcIsNeeded = false;
poly.Clear();
for (int i = GetFirstPolyPointIndex(); i < (int)_points.size(); i++)
{
poly.AddPoint(_points[i]->y, _points[i]->x);
}
result = poly.Compute(true, true, perimeter, area);
if (closingPoint)
result = poly.TestPoint(y, x, true, true, perimeter, area);
}
else
{
if (closingPoint)
{
result = poly.TestPoint(y, x, true, true, perimeter, area);
if (perimeter == 0 && area == 0)
{
_areaRecalcIsNeeded = true;
goto recalc;
}
}
else
{
result = poly.Compute(true, true, perimeter, area);
}
}
}
return abs(area);
}
// ****************************************************************
// GetMeasuringPolyCenter()
// ****************************************************************
IPoint* GeoShape::GetPolygonCenter(Gdiplus::PointF* data, int length)
{
IPoint* pnt = NULL;
if (HasPolygon() && length > 2)
{
// let's draw fill and area
Gdiplus::PointF* polyData = &data[0];
// find position for label
CComPtr<IShape> shp = NULL;
ComHelper::CreateShape(&shp);
if (shp)
{
long pointIndex;
VARIANT_BOOL vb;
shp->Create(ShpfileType::SHP_POLYGON, &vb);
int sz = length;
for (int i = 0; i < sz; i++) {
shp->AddPoint(polyData[i].X, polyData[i].Y, &pointIndex);
}
shp->AddPoint(polyData->X, polyData->Y, &pointIndex); // close it
shp->get_Centroid(&pnt);
// make sure that centroid lies within extents of shapes; otherwise place it at the center
CComPtr<IExtents> ext = NULL;
shp->get_Extents(&ext);
double x, y;
pnt->get_X(&x);
pnt->get_Y(&y);
ext->PointIsWithin(x, y, &vb);
if (!vb)
{
pnt->Release();
ext->get_Center(&pnt);
}
}
}
return pnt;
}
// ***************************************************************
// GetWgs84Projection()
// ***************************************************************
IGeoProjection* GeoShape::GetWgs84Projection()
{
return _mapCallback ? _mapCallback->_GetWgs84Projection() : NULL;
}
// ***************************************************************
// GetMapProjection()
// ***************************************************************
IGeoProjection* GeoShape::GetMapProjection()
{
return _mapCallback ? _mapCallback->_GetMapProjection() : NULL;
}
// ***************************************************************
// GetTransformationMode()
// ***************************************************************
tkTransformationMode GeoShape::GetTransformationMode()
{
return _mapCallback ? _mapCallback->_GetTransformationMode() : tmNotDefined;
}
// ***************************************************************
// ProjToPixel()
// ***************************************************************
void GeoShape::ProjToPixel(double projX, double projY, double& pixelX, double& pixelY)
{
if (_mapCallback) {
_mapCallback->_ProjectionToPixel(projX, projY, &pixelX, &pixelY);
}
else {
CallbackHelper::AssertionFailed("Map pointer is not initialized for measuring tool.");
}
}
// ***************************************************************
// PixelToProj()
// ***************************************************************
void GeoShape::PixelToProj(double pixelX, double pixelY, double& projX, double& projY)
{
if (_mapCallback) {
_mapCallback->_PixelToProjection(pixelX, pixelY, &projX, &projY);
}
else {
CallbackHelper::AssertionFailed("Map pointer is not initialized for measuring tool.");
}
}
// *******************************************************
// UpdateLatLng()
// *******************************************************
void GeoShape::UpdateLatLng(int pointIndex)
{
if (pointIndex < 0 || pointIndex >= (int)_points.size()) return;
MeasurePoint* pnt = _points[pointIndex];
double x = pnt->Proj.x, y = pnt->Proj.y;
if (TransformPoint(x, y))
{
pnt->x = x;
pnt->y = y;
}
}
// *******************************************************
// Clear()
// *******************************************************
void GeoShape::Clear()
{
for (size_t i = 0; i < _points.size(); i++)
delete _points[i];
_points.clear();
poly.Clear();
}
// *******************************************************
// GetTransformedPoints()
// *******************************************************
// transforms input data to decimal degrees
bool GeoShape::TransformPoint(double& x, double& y) {
VARIANT_BOOL vb;
switch (GetTransformationMode())
{
case tkTransformationMode::tmDoTransformation:
GetMapProjection()->Transform(&x, &y, &vb);
return true;
case tkTransformationMode::tmWgs84Complied:
return true;
}
return false;
}
// *******************************************************
// GetArea()
// *******************************************************
double GeoShape::GetArea(bool closingPoint, double x, double y)
{
if (GetPolyPointCount(closingPoint) < 3)
return false;
if (GetTransformationMode() == tmNotDefined)
{
return GetEuclidianArea(closingPoint, x, y); // x, y are projected
}
else
{
double xDeg = x, yDeg = y;
if (closingPoint) TransformPoint(xDeg, yDeg);
return GetGeodesicArea(closingPoint, xDeg, yDeg); // x, y are decimal degrees
}
}
// *******************************************************
// GetEuclidianArea()
// *******************************************************
double GeoShape::GetEuclidianArea(bool closingPoint, double x, double y)
{
double val = 0.0;
if (GetPolyPointCount(closingPoint) > 2)
{
VARIANT_BOOL vb;
_areaCalcShape->Create(ShpfileType::SHP_POLYGON, &vb); // this will clear points
long pointIndex = -1;
for (size_t i = GetFirstPolyPointIndex(); i < _points.size(); i++)
{
_areaCalcShape->AddPoint(_points[i]->Proj.x, _points[i]->Proj.y, &pointIndex);
}
if (closingPoint)
_areaCalcShape->AddPoint(x, y, &pointIndex);
_areaCalcShape->AddPoint(_points[0]->Proj.x, _points[0]->Proj.y, &pointIndex); // we need to close the poly
_areaCalcShape->get_Area(&val);
}
return val;
}
// *******************************************************
// GetNumParts()
// *******************************************************
int GeoShape::GetNumParts()
{
int count = 0;
for (size_t i = 0; i < _points.size(); i++) {
if (_points[i]->Part == PartBegin)
count++;
}
if (count == 0) count = 1;
return count;
}
// *******************************************************
// GetPartForPoint()
// *******************************************************
int GeoShape::GetPartForPoint(int pointIndex) {
int part = -1;
for (int i = 0; i <= pointIndex; i++) {
if (_points[i]->Part == PartBegin)
part++;
}
return part;
}
// *******************************************************
// GetPartStart()
// *******************************************************
int GeoShape::GetPartStart(int partIndex) {
int count = -1;
for (size_t i = 0; i < _points.size(); i++) {
if (_points[i]->Part == PartBegin)
count++;
if (count == partIndex)
return i;
}
return 0;
}
// *******************************************************
// GetPartStart()
// *******************************************************
int GeoShape::SeekPartEnd(int startSearchFrom) {
for (int i = startSearchFrom; i < (int)_points.size(); i++) {
if (_points[i]->Part == PartEnd)
return i;
}
return _points.size() - 1;
}
// *******************************************************
// GetPartStart()
// *******************************************************
int GeoShape::SeekPartStart(int startSearchFrom) {
for (int i = startSearchFrom; i >= 0; i--) {
if (_points[i]->Part == PartBegin)
return i;
}
return 0;
}
// *******************************************************
// GetCloseIndex()
// *******************************************************
int GeoShape::GetCloseIndex(int startIndex)
{
bool polygon = ShapeUtility::Convert2D(GetShapeType2D()) == SHP_POLYGON;
int closeIndex = -1;
if (polygon) {
if (_points[startIndex]->Part == PartBegin) closeIndex = SeekPartEnd(startIndex);
if (_points[startIndex]->Part == PartEnd) closeIndex = SeekPartStart(startIndex);
}
return closeIndex;
}
// ****************************************************************************/
// GetExtents()
// ****************************************************************************/
void GeoShape::GetExtents(Extent& extent)
{
double xMin = DBL_MAX, xMax = -DBL_MAX, yMin = DBL_MAX, yMax = -DBL_MAX;
for (size_t i = 0; i < _points.size(); i++)
{
if (_points[i]->Proj.x > xMax) xMax = _points[i]->Proj.x;
if (_points[i]->Proj.x < xMin) xMin = _points[i]->Proj.x;
if (_points[i]->Proj.y > yMax) yMax = _points[i]->Proj.y;
if (_points[i]->Proj.y > yMin) yMin = _points[i]->Proj.y;
}
extent.left = xMin;
extent.right = xMax;
extent.top = yMax;
extent.bottom = yMin;
}
// *******************************************************
// GetDistance()
// *******************************************************
double GeoShape::GetDistance()
{
if (GetTransformationMode() == tmNotDefined)
{
return GetEuclidianDistance(); // if there us undefined or incompatible projection; return distance on plane
}
else
{
return GetGeodesicDistance();
}
}
// *******************************************************
// GetEuclidianDistance()
// *******************************************************
// in map units specified by current projection
double GeoShape::GetEuclidianDistance()
{
double dist = 0.0;
if (_points.size() > 0)
{
for (size_t i = 0; i < _points.size() - 1; i++)
{
dist += _points[i]->Proj.GetDistance(_points[i + 1]->Proj);
}
}
return dist;
}
// *******************************************************
// GetGeodesicDistance()
// *******************************************************
// in meters with decimal degrees as input
double GeoShape::GetGeodesicDistance()
{
IUtils* utils = GetUtils();
double dist = 0.0;
if (_points.size() > 0)
{
for (size_t i = 0; i < _points.size() - 1; i++)
{
double val;
utils->GeodesicDistance(_points[i]->y, _points[i]->x, _points[i + 1]->y, _points[i + 1]->x, &val);
dist += val;
}
}
return dist;
}
// ***************************************************************
// GetSegmentAngle()
// ***************************************************************
double GeoShape::GetSegmentAngle(int segmentIndex, long& errorCode)
{
if (segmentIndex < 0 || segmentIndex >= (long)_points.size() - 1)
{
errorCode = tkINDEX_OUT_OF_BOUNDS;
return 0.0;
}
else
{
int i = segmentIndex;
double xScr, yScr, xScr2, yScr2;
ProjToPixel(_points[i]->Proj.x, _points[i]->Proj.y, xScr, yScr);
ProjToPixel(_points[i + 1]->Proj.x, _points[i + 1]->Proj.y, xScr2, yScr2);
double x = xScr - xScr2;
double y = yScr - yScr2;
double angle = 360.0 - GeometryHelper::GetPointAngle(x, y) * 180.0 / pi_;
if (angle == 360.0) angle = 0.0;
return angle;
}
}
// **************************************************************
// GetSegmentLength()
// **************************************************************
double GeoShape::GetSegmentLength(int segmentIndex, long& errorCode)
{
if (segmentIndex < 0 || segmentIndex >= (long)_points.size() - 1)
{
errorCode = tkINDEX_OUT_OF_BOUNDS;
return 0.0;
}
else
{
double result;
int i = segmentIndex;
if (GetTransformationMode() == tmNotDefined)
{
result = _points[i]->Proj.GetDistance(_points[i + 1]->Proj);;
}
else
{
IUtils* utils = GetUtils();
utils->GeodesicDistance(_points[i]->y, _points[i]->x, _points[i + 1]->y, _points[i + 1]->x, &result);
}
return result;
}
}
// ****************************************************************************/
// GetAzimuth()
// ****************************************************************************/
double GetAzimuth(MeasurePoint* pnt1, MeasurePoint* pnt2)
{
double x = pnt2->Proj.x - pnt1->Proj.x;
double y = pnt2->Proj.y - pnt1->Proj.y;
return GeometryHelper::GetPointAngle(x, y) / pi_*180.0;
}
// ****************************************************************************/
// GetBearing()
// ****************************************************************************/
double GeoShape::GetBearing(int vertexIndex, bool clockwise)
{
double az1 = GetAzimuth(_points[vertexIndex - 1], _points[vertexIndex]);
double az2 = GetAzimuth(_points[vertexIndex], _points[vertexIndex + 1]);
if (az2 - 180 > az1) az1 = az1 + 360;
if (az1 - 180 > az2) az2 = az2 + 360;
return clockwise ? 180 + (az1 - az2) : 180 + (az2 - az1);
}
// ****************************************************************************/
// GetBearingLabelAngle()
// ****************************************************************************/
double GeoShape::GetBearingLabelAngle(int vertexIndex, bool clockwise)
{
double az;
double bearing = GetBearing(vertexIndex, clockwise);
if (clockwise)
{
az = GetAzimuth(_points[vertexIndex], _points[vertexIndex + 1]);
az += bearing / 2.0;
}
else
{
az = GetAzimuth(_points[vertexIndex], _points[vertexIndex + 1]);
az -= bearing / 2.0;
}
if (az > 360.0) az -= 360.0;
if (az < 0.0) az += 360.0;
return az;
}
// **************************************************************
// GetDymanicLineDistance()
// **************************************************************
double GeoShape::GetDynamicLineDistance()
{
double dist;
double xLng, yLat;
PixelToProj(_mousePoint.x, _mousePoint.y, xLng, yLat);
if (TransformPoint(xLng, yLat))
{
GetUtils()->GeodesicDistance(_points[_points.size() - 1]->y, _points[_points.size() - 1]->x, yLat, xLng, &dist);
}
else
{
dist = _points[_points.size() - 1]->Proj.GetDistance(xLng, yLat);
}
return dist;
}
// *******************************************************
// SetMousePosition()
// *******************************************************
void GeoShape::SetMousePosition(double xScreen, double yScreen)
{
_mousePoint.x = xScreen;
_mousePoint.y = yScreen;
}
// *******************************************************
// FindSegementWithPoint()
// *******************************************************
int GeoShape::FindSegmentWithPoint(double xProj, double yProj)
{
for (size_t i = 0; i < _points.size() - 1; i++)
{
if (GeometryHelper::PointOnSegment(_points[i]->Proj.x, _points[i]->Proj.y,
_points[i + 1]->Proj.x, _points[i + 1]->Proj.y, xProj, yProj))
return i;
}
return -1;
}
// *******************************************************
// GetPolygonPointCount()
// *******************************************************
int GeoShape::GetPolyPointCount(bool dynamicPoly)
{
int size = _points.size() - GetFirstPolyPointIndex();
if (dynamicPoly) size++;
return size;
}