Skip to content
Permalink
Browse files

Port CRS map unit handling to proj 6

  • Loading branch information
nyalldawson committed Apr 8, 2019
1 parent d831f4e commit b42b2956690371fde7f59ea61037a0bf968fa0d0
Showing with 56 additions and 2 deletions.
  1. +49 −0 src/core/qgscoordinatereferencesystem.cpp
  2. +7 −2 tests/src/core/testqgscoordinatereferencesystem.cpp
@@ -1239,6 +1239,54 @@ void QgsCoordinateReferenceSystem::setMapUnits()
OSRFixup( d->mCRS );
#endif

#if PROJ_VERSION_MAJOR>=6
if ( !d->mPj )
{
d->mMapUnits = QgsUnitTypes::DistanceUnknownUnit;
return;
}

PJ_CONTEXT *context = QgsProjContext::get();
QgsProjUtils::proj_pj_unique_ptr coordinateSystem( proj_crs_get_coordinate_system( context, d->mPj.get() ) );
if ( !coordinateSystem )
{
d->mMapUnits = QgsUnitTypes::DistanceUnknownUnit;
return;
}

const int axisCount = proj_cs_get_axis_count( context, coordinateSystem.get() );
if ( axisCount > 0 )
{
const char *outUnitName = nullptr;
// Read only first axis
proj_cs_get_axis_info( context, coordinateSystem.get(), 0,
nullptr,
nullptr,
nullptr,
nullptr,
&outUnitName,
nullptr,
nullptr );

const QString unitName( outUnitName );
if ( unitName.compare( QLatin1String( "degree" ), Qt::CaseInsensitive ) == 0 )
d->mMapUnits = QgsUnitTypes::DistanceDegrees;
else if ( unitName.compare( QLatin1String( "metre" ), Qt::CaseInsensitive ) == 0 )
d->mMapUnits = QgsUnitTypes::DistanceMeters;
else if ( unitName.compare( QLatin1String( "US survey foot" ), Qt::CaseInsensitive ) == 0 )
d->mMapUnits = QgsUnitTypes::DistanceFeet;
// TODO - maybe more values to handle here?
else
d->mMapUnits = QgsUnitTypes::DistanceUnknownUnit;
return;
}
else
{
d->mMapUnits = QgsUnitTypes::DistanceUnknownUnit;
return;
}

#else
if ( OSRIsProjected( d->mCRS ) )
{
double toMeter = OSRGetLinearUnits( d->mCRS, &unitName );
@@ -1275,6 +1323,7 @@ void QgsCoordinateReferenceSystem::setMapUnits()
d->mMapUnits = QgsUnitTypes::DistanceUnknownUnit;
}
}
#endif
}


@@ -713,9 +713,14 @@ void TestQgsCoordinateReferenceSystem::isGeographic()
void TestQgsCoordinateReferenceSystem::mapUnits()
{
QgsCoordinateReferenceSystem myCrs;
myCrs.createFromSrid( GEOSRID );
QVERIFY( myCrs.mapUnits() == QgsUnitTypes::DistanceDegrees );
myCrs.createFromString( QStringLiteral( "EPSG:4326" ) );
QCOMPARE( myCrs.mapUnits(), QgsUnitTypes::DistanceDegrees );
debugPrint( myCrs );
myCrs.createFromString( QStringLiteral( "EPSG:28355" ) );
QCOMPARE( myCrs.mapUnits(), QgsUnitTypes::DistanceMeters );
debugPrint( myCrs );
myCrs.createFromString( QStringLiteral( "EPSG:26812" ) );
QCOMPARE( myCrs.mapUnits(), QgsUnitTypes::DistanceFeet );

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

0 comments on commit b42b295

Please sign in to comment.
You can’t perform that action at this time.