Skip to content

Commit f579f1a

Browse files
committedOct 16, 2017
Move bounds retrieval to QgsCoordinateReferenceSystem
Allows reuse in scripts/plugins/etc
1 parent 0002168 commit f579f1a

File tree

5 files changed

+120
-49
lines changed

5 files changed

+120
-49
lines changed
 

‎python/core/qgscoordinatereferencesystem.sip

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,17 @@ Returns whether this CRS is correctly initialized and usable
601601
:rtype: QgsUnitTypes.DistanceUnit
602602
%End
603603

604+
QgsRectangle bounds() const;
605+
%Docstring
606+
Returns the approximate bounds for the region the CRS is usable within.
607+
608+
The returned bounds represent the latitude and longitude extent for the
609+
projection in the WGS 84 CRS.
610+
611+
.. versionadded:: 3.0
612+
:rtype: QgsRectangle
613+
%End
614+
604615

605616
void setValidationHint( const QString &html );
606617
%Docstring

‎src/core/qgscoordinatereferencesystem.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,49 @@ QgsUnitTypes::DistanceUnit QgsCoordinateReferenceSystem::mapUnits() const
10611061
return d->mMapUnits;
10621062
}
10631063

1064+
QgsRectangle QgsCoordinateReferenceSystem::bounds() const
1065+
{
1066+
if ( !d->mIsValid )
1067+
return QgsRectangle();
1068+
1069+
//check the db is available
1070+
QString databaseFileName = QgsApplication::srsDatabaseFilePath();
1071+
1072+
sqlite3 *database = nullptr;
1073+
const char *tail = nullptr;
1074+
sqlite3_stmt *stmt = nullptr;
1075+
1076+
int result = openDatabase( databaseFileName, &database );
1077+
if ( result != SQLITE_OK )
1078+
{
1079+
return QgsRectangle();
1080+
}
1081+
1082+
QString sql = QStringLiteral( "select west_bound_lon, north_bound_lat, east_bound_lon, south_bound_lat from tbl_srs "
1083+
"where srs_id=%1" )
1084+
.arg( d->mSrsId );
1085+
result = sqlite3_prepare( database, sql.toUtf8(), sql.toUtf8().length(), &stmt, &tail );
1086+
1087+
QgsRectangle rect;
1088+
if ( result == SQLITE_OK )
1089+
{
1090+
if ( sqlite3_step( stmt ) == SQLITE_ROW )
1091+
{
1092+
double west = sqlite3_column_double( stmt, 0 );
1093+
double north = sqlite3_column_double( stmt, 1 );
1094+
double east = sqlite3_column_double( stmt, 2 );
1095+
double south = sqlite3_column_double( stmt, 3 );
1096+
rect = QgsRectangle( west, north, east, south );
1097+
}
1098+
}
1099+
1100+
// close the sqlite3 statement
1101+
sqlite3_finalize( stmt );
1102+
sqlite3_close( database );
1103+
1104+
return rect;
1105+
}
1106+
10641107

10651108
// Mutators -----------------------------------
10661109

‎src/core/qgscoordinatereferencesystem.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
//qgis includes
3232
#include "qgis.h"
3333
#include "qgsunittypes.h"
34+
#include "qgsrectangle.h"
3435

3536
class QDomNode;
3637
class QDomDocument;
@@ -580,6 +581,16 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
580581
*/
581582
QgsUnitTypes::DistanceUnit mapUnits() const;
582583

584+
/**
585+
* Returns the approximate bounds for the region the CRS is usable within.
586+
*
587+
* The returned bounds represent the latitude and longitude extent for the
588+
* projection in the WGS 84 CRS.
589+
*
590+
* \since QGIS 3.0
591+
*/
592+
QgsRectangle bounds() const;
593+
583594
// Mutators -----------------------------------
584595

585596
/**

‎src/gui/qgsprojectionselectiontreewidget.cpp

Lines changed: 20 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,65 +1019,36 @@ long QgsProjectionSelectionTreeWidget::getLargestCrsIdMatch( const QString &sql
10191019

10201020
void QgsProjectionSelectionTreeWidget::updateBoundsPreview()
10211021
{
1022-
sqlite3 *database = nullptr;
1023-
const char *tail = nullptr;
1024-
sqlite3_stmt *stmt = nullptr;
1025-
10261022
QTreeWidgetItem *lvi = lstCoordinateSystems->currentItem();
10271023
if ( !lvi || lvi->text( QgisCrsIdColumn ).isEmpty() )
10281024
return;
10291025

1030-
int result = sqlite3_open_v2( mSrsDatabaseFileName.toUtf8().data(), &database, SQLITE_OPEN_READONLY, nullptr );
1031-
if ( result )
1032-
{
1033-
QgsDebugMsg( QString( "Can't open * user * database: %1" ).arg( sqlite3_errmsg( database ) ) );
1034-
//no need for assert because user db may not have been created yet
1026+
QgsCoordinateReferenceSystem currentCrs = crs();
1027+
if ( !currentCrs.isValid() )
10351028
return;
1036-
}
1037-
1038-
QString sql = QStringLiteral( "select west_bound_lon, north_bound_lat, east_bound_lon, south_bound_lat from tbl_srs "
1039-
"where srs_id=%2" )
1040-
.arg( lvi->text( QgisCrsIdColumn ) );
1041-
result = sqlite3_prepare( database, sql.toUtf8(), sql.toUtf8().length(), &stmt, &tail );
10421029

1043-
if ( result == SQLITE_OK )
1030+
QgsRectangle rect = currentCrs.bounds();
1031+
if ( !rect.isEmpty() )
10441032
{
1045-
if ( sqlite3_step( stmt ) == SQLITE_ROW )
1046-
{
1047-
double west = sqlite3_column_double( stmt, 0 );
1048-
double north = sqlite3_column_double( stmt, 1 );
1049-
double east = sqlite3_column_double( stmt, 2 );
1050-
double south = sqlite3_column_double( stmt, 3 );
1051-
QgsRectangle rect( west, north, east, south );
1052-
if ( !rect.isEmpty() )
1053-
{
1054-
mPreviewBand->setToGeometry( QgsGeometry::fromRect( rect ), nullptr );
1055-
mPreviewBand->setColor( QColor( 255, 0, 0, 65 ) );
1056-
mAreaCanvas->setExtent( rect );
1057-
mPreviewBand->show();
1058-
mAreaCanvas->zoomOut();
1059-
QString extentString = tr( "Extent: %1" ).arg( rect.toString( 2 ) );
1060-
QString proj4String = tr( "Proj4: %1" ).arg( selectedProj4String() );
1061-
teProjection->setText( extentString + "\n" + proj4String );
1062-
}
1063-
else
1064-
{
1065-
mPreviewBand->hide();
1066-
mAreaCanvas->zoomToFullExtent();
1067-
QString extentString = tr( "Extent: Extent not known" );
1068-
QString proj4String = tr( "Proj4: %1" ).arg( selectedProj4String() );
1069-
teProjection->setText( extentString + "\n" + proj4String );
1070-
}
1071-
}
1072-
1033+
mPreviewBand->setToGeometry( QgsGeometry::fromRect( rect ), nullptr );
1034+
mPreviewBand->setColor( QColor( 255, 0, 0, 65 ) );
1035+
mAreaCanvas->setExtent( rect );
1036+
mPreviewBand->show();
1037+
mAreaCanvas->zoomOut();
1038+
QString extentString = tr( "Extent: %1" ).arg( rect.toString( 2 ) );
1039+
QString proj4String = tr( "Proj4: %1" ).arg( selectedProj4String() );
1040+
teProjection->setText( extentString + "\n" + proj4String );
1041+
}
1042+
else
1043+
{
1044+
mPreviewBand->hide();
1045+
mAreaCanvas->zoomToFullExtent();
1046+
QString extentString = tr( "Extent: Extent not known" );
1047+
QString proj4String = tr( "Proj4: %1" ).arg( selectedProj4String() );
1048+
teProjection->setText( extentString + "\n" + proj4String );
10731049
}
1074-
1075-
// close the sqlite3 statement
1076-
sqlite3_finalize( stmt );
1077-
sqlite3_close( database );
10781050
}
10791051

1080-
10811052
QStringList QgsProjectionSelectionTreeWidget::authorities()
10821053
{
10831054
sqlite3 *database = nullptr;

‎tests/src/core/testqgscoordinatereferencesystem.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class TestQgsCoordinateReferenceSystem: public QObject
7575
void createFromProj4Invalid();
7676
void validSrsIds();
7777
void asVariant();
78+
void bounds();
7879

7980
private:
8081
void debugPrint( QgsCoordinateReferenceSystem &crs );
@@ -764,5 +765,39 @@ void TestQgsCoordinateReferenceSystem::asVariant()
764765
QCOMPARE( fromVar.authid(), original.authid() );
765766
}
766767

768+
void TestQgsCoordinateReferenceSystem::bounds()
769+
{
770+
QgsCoordinateReferenceSystem invalid;
771+
QVERIFY( invalid.bounds().isNull() );
772+
773+
QgsCoordinateReferenceSystem crs3111( "EPSG:3111" );
774+
QgsRectangle bounds = crs3111.bounds();
775+
QGSCOMPARENEAR( bounds.xMinimum(), 140.960000, 0.0001 );
776+
QGSCOMPARENEAR( bounds.xMaximum(), 150.040000, 0.0001 );
777+
QGSCOMPARENEAR( bounds.yMinimum(), -39.200000, 0.0001 );
778+
QGSCOMPARENEAR( bounds.yMaximum(), -33.980000, 0.0001 );
779+
780+
QgsCoordinateReferenceSystem crs28356( "EPSG:28356" );
781+
bounds = crs28356.bounds();
782+
QGSCOMPARENEAR( bounds.xMinimum(), 150.000000, 0.0001 );
783+
QGSCOMPARENEAR( bounds.xMaximum(), 156.000000, 0.0001 );
784+
QGSCOMPARENEAR( bounds.yMinimum(), -58.960000, 0.0001 );
785+
QGSCOMPARENEAR( bounds.yMaximum(), -13.870000, 0.0001 );
786+
787+
QgsCoordinateReferenceSystem crs3857( "EPSG:3857" );
788+
bounds = crs3857.bounds();
789+
QGSCOMPARENEAR( bounds.xMinimum(), -180.000000, 0.0001 );
790+
QGSCOMPARENEAR( bounds.xMaximum(), 180.000000, 0.0001 );
791+
QGSCOMPARENEAR( bounds.yMinimum(), -85.060000, 0.0001 );
792+
QGSCOMPARENEAR( bounds.yMaximum(), 85.060000, 0.0001 );
793+
794+
QgsCoordinateReferenceSystem crs4326( "EPSG:4326" );
795+
bounds = crs4326.bounds();
796+
QGSCOMPARENEAR( bounds.xMinimum(), -180.000000, 0.0001 );
797+
QGSCOMPARENEAR( bounds.xMaximum(), 180.000000, 0.0001 );
798+
QGSCOMPARENEAR( bounds.yMinimum(), -90.00000, 0.0001 );
799+
QGSCOMPARENEAR( bounds.yMaximum(), 90.00000, 0.0001 );
800+
}
801+
767802
QGSTEST_MAIN( TestQgsCoordinateReferenceSystem )
768803
#include "testqgscoordinatereferencesystem.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.