/
qgsprojutils.cpp
345 lines (304 loc) · 10.8 KB
/
qgsprojutils.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
/***************************************************************************
qgsprojutils.h
-------------------
begin : March 2019
copyright : (C) 2019 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsprojutils.h"
#include "qgis.h"
#include "qgscoordinatetransform.h"
#include <QString>
#include <QSet>
#include <QRegularExpression>
#if PROJ_VERSION_MAJOR>=6
#include <proj.h>
#else
#include <proj_api.h>
#endif
#if defined(USE_THREAD_LOCAL) && !defined(Q_OS_WIN)
thread_local QgsProjContext QgsProjContext::sProjContext;
#else
QThreadStorage< QgsProjContext * > QgsProjContext::sProjContext;
#endif
QgsProjContext::QgsProjContext()
{
#if PROJ_VERSION_MAJOR>=6
mContext = proj_context_create();
#else
mContext = pj_ctx_alloc();
#endif
}
QgsProjContext::~QgsProjContext()
{
#if PROJ_VERSION_MAJOR>=6
// Call removeFromCacheObjectsBelongingToCurrentThread() before
// destroying the context
QgsCoordinateTransform::removeFromCacheObjectsBelongingToCurrentThread( mContext );
QgsCoordinateReferenceSystem::removeFromCacheObjectsBelongingToCurrentThread( mContext );
proj_context_destroy( mContext );
#else
pj_ctx_free( mContext );
#endif
}
PJ_CONTEXT *QgsProjContext::get()
{
#if defined(USE_THREAD_LOCAL) && !defined(Q_OS_WIN)
return sProjContext.mContext;
#else
PJ_CONTEXT *pContext = nullptr;
if ( sProjContext.hasLocalData() )
{
pContext = sProjContext.localData()->mContext;
}
else
{
sProjContext.setLocalData( new QgsProjContext() );
pContext = sProjContext.localData()->mContext;
}
return pContext;
#endif
}
#if PROJ_VERSION_MAJOR>=6
void QgsProjUtils::ProjPJDeleter::operator()( PJ *object )
{
proj_destroy( object );
}
bool QgsProjUtils::usesAngularUnit( const QString &projDef )
{
const QString crsDef = QStringLiteral( "%1 +type=crs" ).arg( projDef );
PJ_CONTEXT *context = QgsProjContext::get();
QgsProjUtils::proj_pj_unique_ptr projSingleOperation( proj_create( context, crsDef.toUtf8().constData() ) );
if ( !projSingleOperation )
return false;
QgsProjUtils::proj_pj_unique_ptr coordinateSystem( proj_crs_get_coordinate_system( context, projSingleOperation.get() ) );
if ( !coordinateSystem )
return false;
const int axisCount = proj_cs_get_axis_count( context, coordinateSystem.get() );
if ( axisCount > 0 )
{
const char *outUnitAuthName = nullptr;
const char *outUnitAuthCode = nullptr;
// Read only first axis
proj_cs_get_axis_info( context, coordinateSystem.get(), 0,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
&outUnitAuthName,
&outUnitAuthCode );
if ( outUnitAuthName && outUnitAuthCode )
{
const char *unitCategory = nullptr;
if ( proj_uom_get_info_from_database( context, outUnitAuthName, outUnitAuthCode, nullptr, nullptr, &unitCategory ) )
{
return QString( unitCategory ).compare( QLatin1String( "angular" ), Qt::CaseInsensitive ) == 0;
}
}
}
return false;
}
bool QgsProjUtils::axisOrderIsSwapped( const PJ *crs )
{
//ported from https://github.com/pramsey/postgis/blob/7ecf6839c57a838e2c8540001a3cd35b78a730db/liblwgeom/lwgeom_transform.c#L299
if ( !crs )
return false;
PJ_CONTEXT *context = QgsProjContext::get();
QgsProjUtils::proj_pj_unique_ptr pjCs( proj_crs_get_coordinate_system( context, crs ) );
if ( !pjCs )
return false;
const int axisCount = proj_cs_get_axis_count( context, pjCs.get() );
if ( axisCount > 0 )
{
const char *outDirection = nullptr;
// Read only first axis, see if it is degrees / north
proj_cs_get_axis_info( context, pjCs.get(), 0,
nullptr,
nullptr,
&outDirection,
nullptr,
nullptr,
nullptr,
nullptr
);
return QString( outDirection ).compare( QLatin1String( "north" ), Qt::CaseInsensitive ) == 0;
}
return false;
}
QgsProjUtils::proj_pj_unique_ptr QgsProjUtils::crsToSingleCrs( const PJ *crs )
{
if ( !crs )
return nullptr;
PJ_CONTEXT *context = QgsProjContext::get();
switch ( proj_get_type( crs ) )
{
case PJ_TYPE_BOUND_CRS:
return QgsProjUtils::proj_pj_unique_ptr( proj_get_source_crs( context, crs ) );
case PJ_TYPE_COMPOUND_CRS:
{
int i = 0;
QgsProjUtils::proj_pj_unique_ptr res( proj_crs_get_sub_crs( context, crs, i ) );
while ( res && ( proj_get_type( res.get() ) == PJ_TYPE_VERTICAL_CRS || proj_get_type( res.get() ) == PJ_TYPE_TEMPORAL_CRS ) )
{
i++;
res.reset( proj_crs_get_sub_crs( context, crs, i ) );
}
return res;
}
// maybe other types to handle??
default:
return QgsProjUtils::proj_pj_unique_ptr( proj_clone( context, crs ) );
}
return nullptr;
}
bool QgsProjUtils::identifyCrs( const PJ *crs, QString &authName, QString &authCode, IdentifyFlags flags )
{
authName.clear();
authCode.clear();
if ( !crs )
return false;
int *confidence = nullptr;
if ( PJ_OBJ_LIST *crsList = proj_identify( QgsProjContext::get(), crs, nullptr, nullptr, &confidence ) )
{
const int count = proj_list_get_count( crsList );
int bestConfidence = 0;
QgsProjUtils::proj_pj_unique_ptr matchedCrs;
for ( int i = 0; i < count; ++i )
{
if ( confidence[i] >= bestConfidence )
{
QgsProjUtils::proj_pj_unique_ptr candidateCrs( proj_list_get( QgsProjContext::get(), crsList, i ) );
switch ( proj_get_type( candidateCrs.get() ) )
{
case PJ_TYPE_BOUND_CRS:
// proj_identify also matches bound CRSes to the source CRS. But they are not the same as the source CRS, so we don't
// consider them a candidate for a match here (depending on the identify flags, that is!)
if ( flags & FlagMatchBoundCrsToUnderlyingSourceCrs )
break;
else
continue;
default:
break;
}
candidateCrs = QgsProjUtils::crsToSingleCrs( candidateCrs.get() );
const QString authName( proj_get_id_auth_name( candidateCrs.get(), 0 ) );
// if a match is identical confidence, we prefer EPSG codes for compatibility with earlier qgis conversions
if ( confidence[i] > bestConfidence || ( confidence[i] == bestConfidence && authName == QLatin1String( "EPSG" ) ) )
{
bestConfidence = confidence[i];
matchedCrs = std::move( candidateCrs );
}
}
}
proj_list_destroy( crsList );
proj_int_list_destroy( confidence );
if ( matchedCrs && bestConfidence >= 70 )
{
authName = QString( proj_get_id_auth_name( matchedCrs.get(), 0 ) );
authCode = QString( proj_get_id_code( matchedCrs.get(), 0 ) );
}
}
return !authName.isEmpty() && !authCode.isEmpty();
}
bool QgsProjUtils::coordinateOperationIsAvailable( const QString &projDef )
{
if ( projDef.isEmpty() )
return true;
PJ_CONTEXT *context = QgsProjContext::get();
QgsProjUtils::proj_pj_unique_ptr coordinateOperation( proj_create( context, projDef.toUtf8().constData() ) );
if ( !coordinateOperation )
return false;
return static_cast< bool >( proj_coordoperation_is_instantiable( context, coordinateOperation.get() ) );
}
QList<QgsDatumTransform::GridDetails> QgsProjUtils::gridsUsed( const QString &proj )
{
static QRegularExpression sRegex( QStringLiteral( "\\+(?:nad)?grids=(.*?)\\s" ) );
QList< QgsDatumTransform::GridDetails > grids;
QRegularExpressionMatchIterator matches = sRegex.globalMatch( proj );
while ( matches.hasNext() )
{
const QRegularExpressionMatch match = matches.next();
const QString gridName = match.captured( 1 );
QgsDatumTransform::GridDetails grid;
grid.shortName = gridName;
#if PROJ_VERSION_MAJOR>6 || (PROJ_VERSION_MAJOR==6 && PROJ_VERSION_MINOR>=2)
const char *fullName = nullptr;
const char *packageName = nullptr;
const char *url = nullptr;
int directDownload = 0;
int openLicense = 0;
int available = 0;
proj_grid_get_info_from_database( QgsProjContext::get(), gridName.toUtf8().constData(), &fullName, &packageName, &url, &directDownload, &openLicense, &available );
grid.fullName = QString( fullName );
grid.packageName = QString( packageName );
grid.url = QString( url );
grid.directDownload = directDownload;
grid.openLicense = openLicense;
grid.isAvailable = available;
#endif
grids.append( grid );
}
return grids;
}
#if 0
QStringList QgsProjUtils::nonAvailableGrids( const QString &projDef )
{
if ( projDef.isEmpty() )
return QStringList();
PJ_CONTEXT *context = QgsProjContext::get();
QgsProjUtils::proj_pj_unique_ptr op( proj_create( context, projDef.toUtf8().constData() ) ); < ---- - this always fails if grids are missing
if ( !op )
return QStringList();
QStringList res;
for ( int j = 0; j < proj_coordoperation_get_grid_used_count( context, op.get() ); ++j )
{
const char *shortName = nullptr;
int isAvailable = 0;
proj_coordoperation_get_grid_used( context, op.get(), j, &shortName, nullptr, nullptr, nullptr, nullptr, nullptr, &isAvailable );
if ( !isAvailable )
res << QString( shortName );
}
return res;
}
#endif
#endif
QStringList QgsProjUtils::searchPaths()
{
#if PROJ_VERSION_MAJOR>=6
const QString path( proj_info().searchpath );
QStringList paths;
#if PROJ_VERSION_MINOR==1 && PROJ_VERSION_PATCH==0
// -- see https://github.com/OSGeo/proj.4/pull/1497
paths = path.split( ';' );
#else
#ifdef Q_OS_WIN
paths = path.split( ';' );
#else
paths = path.split( ':' );
#endif
#endif
QSet<QString> existing;
// thin out duplicates from paths -- see https://github.com/OSGeo/proj.4/pull/1498
QStringList res;
res.reserve( paths.count() );
for ( const QString &p : qgis::as_const( paths ) )
{
if ( existing.contains( p ) )
continue;
existing.insert( p );
res << p;
}
return res;
#else
return QStringList();
#endif
}