forked from MapWindow/MapWinGIS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectionHelper.cpp
More file actions
165 lines (139 loc) · 4.34 KB
/
ProjectionHelper.cpp
File metadata and controls
165 lines (139 loc) · 4.34 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
#include "stdafx.h"
#include "ProjectionHelper.h"
// ***************************************************************
// IsEmpty()
// ***************************************************************
bool ProjectionHelper::IsEmpty(IGeoProjection* gp)
{
if (!gp) return true;
VARIANT_BOOL isEmpty;
gp->get_IsEmpty(&isEmpty);
return isEmpty ? true : false;
}
// ***************************************************************
// IsGeographic()
// ***************************************************************
bool ProjectionHelper::IsGeographic(IGeoProjection* gp)
{
if (!gp) return false;
VARIANT_BOOL geograhpic;
gp->get_IsGeographic(&geograhpic);
return geograhpic ? true : false;
}
// ***************************************************************
// IsSame()
// ***************************************************************
bool ProjectionHelper::IsSame(IGeoProjection* gp1, IGeoProjection* gp2, IExtents* bounds, int sampleSize)
{
if (!gp1 || !gp2 || !bounds) return false;
VARIANT_BOOL vb;
gp1->get_IsSameExt(gp2, bounds, sampleSize, &vb);
return vb ? true: false;
}
// ***************************************************************
// ToString()
// ***************************************************************
CString ProjectionHelper::ToString(IGeoProjection* gp)
{
if (!gp) return "";
CComBSTR str;
if (!ProjectionHelper::IsEmpty(gp)) {
gp->ExportToProj4(&str);
}
else {
str = L"";
}
USES_CONVERSION;
return str.Length() > 0 ? OLE2A(str) : "<empty>";
}
// ***************************************************************
// GetSpatialReference()
// ***************************************************************
void ProjectionHelper::GetSpatialReference(IGeoProjection* gp, OGRSpatialReference& result)
{
if (!gp) return;
CComBSTR bstr;
gp->ExportToProj4(&bstr);
USES_CONVERSION;
CString s = OLE2A(bstr);
result.importFromProj4(s);
}
// ***************************************************************
// ImportFromEsri()
// ***************************************************************
OGRErr ProjectionHelper::ImportFromEsri(OGRSpatialReference* sr, CString proj)
{
if (!sr) {
return false;
}
char* s = proj.GetBuffer();
return sr->importFromESRI(&s);
}
// ***************************************************************
// ImportFromWkt()
// ***************************************************************
OGRErr ProjectionHelper::ImportFromWkt(OGRSpatialReference* sr, CString proj)
{
if (!sr) {
return false;
}
char* s = proj.GetBuffer();
return sr->importFromWkt(&s);
}
// ***************************************************************
// ExportFromWkt()
// ***************************************************************
OGRErr ProjectionHelper::ExportToWkt(OGRSpatialReference* sr, CString& proj)
{
if (!sr) {
return false;
}
char* s = NULL;
OGRErr err = sr->exportToWkt(&s);
proj = s;
if (s) {
CPLFree(s);
}
return err;
}
// ************************************************************
// SupportsWorldWideTransform
// ************************************************************
bool ProjectionHelper::SupportsWorldWideTransform(IGeoProjection* mapProjection, IGeoProjection* wgsProjection)
{
VARIANT_BOOL isGeograpic;
mapProjection->get_IsGeographic(&isGeograpic);
if (isGeograpic)
{
return true;
}
double TOLERANCE = 0.00001;
VARIANT_BOOL vb1, vb2, vb3;
double minLng = -180.0, maxLng = 180.0, minLat = -85.05112878, maxLat = 85.05112878;
double x1 = minLng, x2 = maxLng, y1 = minLat, y2 = maxLat;
double x3 = 0.0, y3 = 0.0;
m_globalSettings.suppressGdalErrors = true;
wgsProjection->Transform(&x1, &y1, &vb1);
wgsProjection->Transform(&x2, &y2, &vb2);
wgsProjection->Transform(&x3, &y3, &vb3);
m_globalSettings.suppressGdalErrors = false;
if ((y3 > y2 && y3 > y1) || (y3 < y2 && y3 < y1))
{
// this check that direction of Y axis is the same on its whole length;
// for Lambert Conformal Conic, that's not the case
return false;
}
if (vb1 && vb2 && vb3)
{
mapProjection->Transform(&x1, &y1, &vb1);
mapProjection->Transform(&x2, &y2, &vb1);
if (abs(x1 - minLng) < TOLERANCE &&
abs(x2 - maxLng) < TOLERANCE &&
abs(y1 - minLat) < TOLERANCE &&
abs(y2 - maxLat) < TOLERANCE)
{
return true;
}
}
return false;
}