mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-13 00:03:09 -04:00
Add method to retrieve all valid srs ids from CRS databases
This commit is contained in:
parent
f2ac60ab54
commit
fbf99afd01
@ -438,6 +438,53 @@ template <TYPE>
|
||||
};
|
||||
|
||||
|
||||
%MappedType QList<long>
|
||||
{
|
||||
%TypeHeaderCode
|
||||
#include <QList>
|
||||
%End
|
||||
|
||||
%ConvertFromTypeCode
|
||||
// Create the list.
|
||||
PyObject *l;
|
||||
|
||||
if ((l = PyList_New(sipCpp->size())) == NULL)
|
||||
return NULL;
|
||||
|
||||
// Set the list elements.
|
||||
QList<long>::iterator it = sipCpp->begin();
|
||||
for (int i = 0; it != sipCpp->end(); ++it, ++i)
|
||||
{
|
||||
PyObject *tobj;
|
||||
|
||||
if ((tobj = PyLong_FromLong(*it)) == NULL)
|
||||
{
|
||||
Py_DECREF(l);
|
||||
return NULL;
|
||||
}
|
||||
PyList_SET_ITEM(l, i, tobj);
|
||||
}
|
||||
|
||||
return l;
|
||||
%End
|
||||
|
||||
%ConvertToTypeCode
|
||||
// Check the type if that is all that is required.
|
||||
if (sipIsErr == NULL)
|
||||
return PyList_Check(sipPy);
|
||||
|
||||
QList<long> *qlist = new QList<long>;
|
||||
|
||||
for (int i = 0; i < PyList_GET_SIZE(sipPy); ++i)
|
||||
{
|
||||
*qlist << PyLong_AsLong(PyList_GET_ITEM(sipPy, i));
|
||||
}
|
||||
|
||||
*sipCppPtr = qlist;
|
||||
return sipGetState(sipTransferObj);
|
||||
%End
|
||||
};
|
||||
|
||||
%MappedType QList<qint64>
|
||||
{
|
||||
%TypeHeaderCode
|
||||
|
@ -183,6 +183,8 @@ class QgsCoordinateReferenceSystem
|
||||
// TODO QGIS 3: remove theType and always use EPSG code
|
||||
QgsCoordinateReferenceSystem( const long theId, CrsType theType = PostgisCrsId );
|
||||
|
||||
static QList< long > validSrsIds();
|
||||
|
||||
// static creators
|
||||
|
||||
/** Creates a CRS from a given OGC WMS-format Coordinate Reference System string.
|
||||
|
@ -90,6 +90,66 @@ QgsCoordinateReferenceSystem& QgsCoordinateReferenceSystem::operator=( const Qgs
|
||||
return *this;
|
||||
}
|
||||
|
||||
QList<long> QgsCoordinateReferenceSystem::validSrsIds()
|
||||
{
|
||||
QList<long> results;
|
||||
// check both standard & user defined projection databases
|
||||
QStringList dbs = QStringList() << QgsApplication::srsDatabaseFilePath() << QgsApplication::qgisUserDatabaseFilePath();
|
||||
|
||||
Q_FOREACH ( const QString& db, dbs )
|
||||
{
|
||||
QFileInfo myInfo( db );
|
||||
if ( !myInfo.exists() )
|
||||
{
|
||||
QgsDebugMsg( "failed : " + db + " does not exist!" );
|
||||
continue;
|
||||
}
|
||||
|
||||
sqlite3 *database = nullptr;
|
||||
const char *tail = nullptr;
|
||||
sqlite3_stmt *statement = nullptr;
|
||||
|
||||
//check the db is available
|
||||
int result = openDatabase( db, &database );
|
||||
if ( result != SQLITE_OK )
|
||||
{
|
||||
QgsDebugMsg( "failed : " + db + " could not be opened!" );
|
||||
continue;
|
||||
}
|
||||
|
||||
QString sql = "select srs_id from tbl_srs";
|
||||
result = sqlite3_prepare( database, sql.toUtf8(),
|
||||
sql.toUtf8().length(),
|
||||
&statement, &tail );
|
||||
while ( 1 )
|
||||
{
|
||||
// this one is an infinitive loop, intended to fetch any row
|
||||
int ret = sqlite3_step( statement );
|
||||
|
||||
if ( ret == SQLITE_DONE )
|
||||
{
|
||||
// there are no more rows to fetch - we can stop looping
|
||||
break;
|
||||
}
|
||||
|
||||
if ( ret == SQLITE_ROW )
|
||||
{
|
||||
results.append( sqlite3_column_int( statement, 0 ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
QgsMessageLog::logMessage( QObject::tr( "SQLite error: %2\nSQL: %1" ).arg( sql, sqlite3_errmsg( database ) ), QObject::tr( "SpatiaLite" ) );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
sqlite3_finalize( statement );
|
||||
sqlite3_close( database );
|
||||
}
|
||||
std::sort( results.begin(), results.end() );
|
||||
return results;
|
||||
}
|
||||
|
||||
QgsCoordinateReferenceSystem QgsCoordinateReferenceSystem::fromOgcWmsCrs( const QString& ogcCrs )
|
||||
{
|
||||
QgsCoordinateReferenceSystem crs;
|
||||
|
@ -236,6 +236,15 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
|
||||
//! Assignment operator
|
||||
QgsCoordinateReferenceSystem& operator=( const QgsCoordinateReferenceSystem& srs );
|
||||
|
||||
/**
|
||||
* Returns a list of all valid SRS IDs present in the CRS database. Any of the
|
||||
* returned values can be safely passed to fromSrsId() to create a new, valid
|
||||
* QgsCoordinateReferenceSystem object.
|
||||
* @see fromSrsId()
|
||||
* @note added in QGIS 3.0
|
||||
*/
|
||||
static QList< long > validSrsIds();
|
||||
|
||||
// static creators
|
||||
|
||||
/** Creates a CRS from a given OGC WMS-format Coordinate Reference System string.
|
||||
@ -274,6 +283,7 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
|
||||
* @returns matching CRS, or an invalid CRS if ID could not be found
|
||||
* @note added in QGIS 3.0
|
||||
* @see createFromSrsId()
|
||||
* @see validSrsIds()
|
||||
*/
|
||||
static QgsCoordinateReferenceSystem fromSrsId( long srsId );
|
||||
|
||||
|
@ -73,6 +73,8 @@ class TestQgsCoordinateReferenceSystem: public QObject
|
||||
void setValidationHint();
|
||||
void hasAxisInverted();
|
||||
void createFromProj4Invalid();
|
||||
void validSrsIds();
|
||||
|
||||
private:
|
||||
void debugPrint( QgsCoordinateReferenceSystem &theCrs );
|
||||
// these used by createFromESRIWkt()
|
||||
@ -724,5 +726,19 @@ void TestQgsCoordinateReferenceSystem::createFromProj4Invalid()
|
||||
QVERIFY( !myCrs.createFromProj4( "+proj=longlat +no_defs" ) );
|
||||
}
|
||||
|
||||
void TestQgsCoordinateReferenceSystem::validSrsIds()
|
||||
{
|
||||
QList< long > ids = QgsCoordinateReferenceSystem::validSrsIds();
|
||||
QVERIFY( ids.contains( 3857 ) );
|
||||
QVERIFY( ids.contains( 28356 ) );
|
||||
|
||||
// check that all returns ids are valid
|
||||
Q_FOREACH ( long id, ids )
|
||||
{
|
||||
QgsCoordinateReferenceSystem c = QgsCoordinateReferenceSystem::fromSrsId( id );
|
||||
QVERIFY( c.isValid() );
|
||||
}
|
||||
}
|
||||
|
||||
QGSTEST_MAIN( TestQgsCoordinateReferenceSystem )
|
||||
#include "testqgscoordinatereferencesystem.moc"
|
||||
|
Loading…
x
Reference in New Issue
Block a user