Skip to content

Commit 1fac288

Browse files
committedMay 13, 2019
Add method to get SingleCrs from crs
1 parent 18fb3f2 commit 1fac288

File tree

3 files changed

+57
-25
lines changed

3 files changed

+57
-25
lines changed
 

‎src/core/qgscoordinatereferencesystem.cpp

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -533,15 +533,9 @@ bool QgsCoordinateReferenceSystem::loadFromDatabase( const QString &db, const QS
533533
QString auth = parts.at( 0 );
534534
QString code = parts.at( 1 );
535535

536-
QgsProjUtils::proj_pj_unique_ptr crs( proj_create_from_database( QgsProjContext::get(), auth.toLatin1(), code.toLatin1(), PJ_CATEGORY_CRS, false, nullptr ) );
537-
if ( crs && proj_get_type( crs.get() ) == PJ_TYPE_COMPOUND_CRS )
538536
{
539-
// freaking take a guess that 0 is horizontal, because we are only mortal
540-
d->mPj.reset( proj_crs_get_sub_crs( QgsProjContext::get(), crs.get(), 0 ) );
541-
}
542-
else
543-
{
544-
d->mPj = std::move( crs );
537+
QgsProjUtils::proj_pj_unique_ptr crs( proj_create_from_database( QgsProjContext::get(), auth.toLatin1(), code.toLatin1(), PJ_CATEGORY_CRS, false, nullptr ) );
538+
d->mPj = QgsProjUtils::crsToSingleCrs( crs.get() );
545539
}
546540

547541
d->mIsValid = static_cast< bool >( d->mPj );
@@ -625,15 +619,9 @@ bool QgsCoordinateReferenceSystem::createFromWkt( const QString &wkt )
625619
PROJ_STRING_LIST warnings = nullptr;
626620
PROJ_STRING_LIST grammerErrors = nullptr;
627621

628-
QgsProjUtils::proj_pj_unique_ptr crs( proj_create_from_wkt( QgsProjContext::get(), wkt.toLatin1().constData(), nullptr, &warnings, &grammerErrors ) );
629-
if ( crs && proj_get_type( crs.get() ) == PJ_TYPE_COMPOUND_CRS )
630-
{
631-
// freaking take a guess that 0 is horizontal, because we are only mortal
632-
d->mPj.reset( proj_crs_get_sub_crs( QgsProjContext::get(), crs.get(), 0 ) );
633-
}
634-
else
635622
{
636-
d->mPj = std::move( crs );
623+
QgsProjUtils::proj_pj_unique_ptr crs( proj_create_from_wkt( QgsProjContext::get(), wkt.toLatin1().constData(), nullptr, &warnings, &grammerErrors ) );
624+
d->mPj = QgsProjUtils::crsToSingleCrs( crs.get() );
637625
}
638626

639627
res = static_cast< bool >( d->mPj );
@@ -1232,20 +1220,14 @@ void QgsCoordinateReferenceSystem::setProj4String( const QString &proj4String )
12321220
d->mProj4 = proj4String;
12331221

12341222
QgsLocaleNumC l;
1235-
const QString trimmed = proj4String.trimmed();
1223+
const QString trimmed = proj4String.trimmed() + QStringLiteral( " +type=crs" );
12361224

12371225
#if PROJ_VERSION_MAJOR>=6
12381226
PJ_CONTEXT *ctx = QgsProjContext::get();
12391227

1240-
QgsProjUtils::proj_pj_unique_ptr crs( proj_create( ctx, trimmed.toLatin1().constData() ) );
1241-
if ( crs && proj_get_type( crs.get() ) == PJ_TYPE_COMPOUND_CRS )
12421228
{
1243-
// freaking take a guess that 0 is horizontal, because we are only mortal
1244-
d->mPj.reset( proj_crs_get_sub_crs( QgsProjContext::get(), crs.get(), 0 ) );
1245-
}
1246-
else
1247-
{
1248-
d->mPj = std::move( crs );
1229+
QgsProjUtils::proj_pj_unique_ptr crs( proj_create( ctx, trimmed.toLatin1().constData() ) );
1230+
d->mPj = QgsProjUtils::crsToSingleCrs( crs.get() );
12491231
}
12501232

12511233
if ( !d->mPj )
@@ -2128,6 +2110,17 @@ int QgsCoordinateReferenceSystem::syncDatabase()
21282110
continue;
21292111
}
21302112

2113+
switch ( proj_get_type( crs.get() ) )
2114+
{
2115+
case PJ_TYPE_VERTICAL_CRS: // don't need these in the CRS db
2116+
continue;
2117+
2118+
default:
2119+
break;
2120+
}
2121+
2122+
crs = QgsProjUtils::crsToSingleCrs( crs.get() );
2123+
21312124
QString proj4( proj_as_proj_string( pjContext, crs.get(), PJ_PROJ_4, nullptr ) );
21322125
proj4 = proj4.trimmed();
21332126

‎src/core/qgsprojutils.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,5 +143,38 @@ bool QgsProjUtils::axisOrderIsSwapped( const PJ *crs )
143143
return false;
144144
}
145145

146+
147+
QgsProjUtils::proj_pj_unique_ptr QgsProjUtils::crsToSingleCrs( const PJ *crs )
148+
{
149+
if ( !crs )
150+
return nullptr;
151+
152+
PJ_CONTEXT *context = QgsProjContext::get();
153+
switch ( proj_get_type( crs ) )
154+
{
155+
case PJ_TYPE_BOUND_CRS:
156+
return QgsProjUtils::proj_pj_unique_ptr( proj_get_source_crs( context, crs ) );
157+
158+
case PJ_TYPE_COMPOUND_CRS:
159+
{
160+
int i = 0;
161+
QgsProjUtils::proj_pj_unique_ptr res( proj_crs_get_sub_crs( context, crs, i ) );
162+
while ( res && ( proj_get_type( res.get() ) == PJ_TYPE_VERTICAL_CRS || proj_get_type( res.get() ) == PJ_TYPE_TEMPORAL_CRS ) )
163+
{
164+
i++;
165+
res.reset( proj_crs_get_sub_crs( context, crs, i ) );
166+
}
167+
return res;
168+
}
169+
170+
// maybe other types to handle??
171+
172+
default:
173+
return QgsProjUtils::proj_pj_unique_ptr( proj_clone( context, crs ) );
174+
}
175+
176+
return nullptr;
177+
}
178+
146179
#endif
147180

‎src/core/qgsprojutils.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ class CORE_EXPORT QgsProjUtils
8787
*/
8888
static bool axisOrderIsSwapped( const PJ *crs );
8989

90+
/**
91+
* Given a PROJ crs (which may be a compound or bound crs, or some other type), extract a single crs
92+
* from it.
93+
*/
94+
static proj_pj_unique_ptr crsToSingleCrs( const PJ *crs );
95+
9096
#endif
9197
#endif
9298
};

0 commit comments

Comments
 (0)
Please sign in to comment.