Skip to content

Commit b42b295

Browse files
committedApr 15, 2019
Port CRS map unit handling to proj 6
1 parent d831f4e commit b42b295

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed
 

‎src/core/qgscoordinatereferencesystem.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,54 @@ void QgsCoordinateReferenceSystem::setMapUnits()
12391239
OSRFixup( d->mCRS );
12401240
#endif
12411241

1242+
#if PROJ_VERSION_MAJOR>=6
1243+
if ( !d->mPj )
1244+
{
1245+
d->mMapUnits = QgsUnitTypes::DistanceUnknownUnit;
1246+
return;
1247+
}
1248+
1249+
PJ_CONTEXT *context = QgsProjContext::get();
1250+
QgsProjUtils::proj_pj_unique_ptr coordinateSystem( proj_crs_get_coordinate_system( context, d->mPj.get() ) );
1251+
if ( !coordinateSystem )
1252+
{
1253+
d->mMapUnits = QgsUnitTypes::DistanceUnknownUnit;
1254+
return;
1255+
}
1256+
1257+
const int axisCount = proj_cs_get_axis_count( context, coordinateSystem.get() );
1258+
if ( axisCount > 0 )
1259+
{
1260+
const char *outUnitName = nullptr;
1261+
// Read only first axis
1262+
proj_cs_get_axis_info( context, coordinateSystem.get(), 0,
1263+
nullptr,
1264+
nullptr,
1265+
nullptr,
1266+
nullptr,
1267+
&outUnitName,
1268+
nullptr,
1269+
nullptr );
1270+
1271+
const QString unitName( outUnitName );
1272+
if ( unitName.compare( QLatin1String( "degree" ), Qt::CaseInsensitive ) == 0 )
1273+
d->mMapUnits = QgsUnitTypes::DistanceDegrees;
1274+
else if ( unitName.compare( QLatin1String( "metre" ), Qt::CaseInsensitive ) == 0 )
1275+
d->mMapUnits = QgsUnitTypes::DistanceMeters;
1276+
else if ( unitName.compare( QLatin1String( "US survey foot" ), Qt::CaseInsensitive ) == 0 )
1277+
d->mMapUnits = QgsUnitTypes::DistanceFeet;
1278+
// TODO - maybe more values to handle here?
1279+
else
1280+
d->mMapUnits = QgsUnitTypes::DistanceUnknownUnit;
1281+
return;
1282+
}
1283+
else
1284+
{
1285+
d->mMapUnits = QgsUnitTypes::DistanceUnknownUnit;
1286+
return;
1287+
}
1288+
1289+
#else
12421290
if ( OSRIsProjected( d->mCRS ) )
12431291
{
12441292
double toMeter = OSRGetLinearUnits( d->mCRS, &unitName );
@@ -1275,6 +1323,7 @@ void QgsCoordinateReferenceSystem::setMapUnits()
12751323
d->mMapUnits = QgsUnitTypes::DistanceUnknownUnit;
12761324
}
12771325
}
1326+
#endif
12781327
}
12791328

12801329

‎tests/src/core/testqgscoordinatereferencesystem.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -713,9 +713,14 @@ void TestQgsCoordinateReferenceSystem::isGeographic()
713713
void TestQgsCoordinateReferenceSystem::mapUnits()
714714
{
715715
QgsCoordinateReferenceSystem myCrs;
716-
myCrs.createFromSrid( GEOSRID );
717-
QVERIFY( myCrs.mapUnits() == QgsUnitTypes::DistanceDegrees );
716+
myCrs.createFromString( QStringLiteral( "EPSG:4326" ) );
717+
QCOMPARE( myCrs.mapUnits(), QgsUnitTypes::DistanceDegrees );
718+
debugPrint( myCrs );
719+
myCrs.createFromString( QStringLiteral( "EPSG:28355" ) );
720+
QCOMPARE( myCrs.mapUnits(), QgsUnitTypes::DistanceMeters );
718721
debugPrint( myCrs );
722+
myCrs.createFromString( QStringLiteral( "EPSG:26812" ) );
723+
QCOMPARE( myCrs.mapUnits(), QgsUnitTypes::DistanceFeet );
719724

720725
// an invalid crs should return unknown unit
721726
QCOMPARE( QgsCoordinateReferenceSystem().mapUnits(), QgsUnitTypes::DistanceUnknownUnit );

0 commit comments

Comments
 (0)
Please sign in to comment.