Skip to content

Commit c460a4b

Browse files
committedMay 1, 2013
Merge pull request #533 from leyan/customCRS_API
Custom crs api
2 parents f62955f + cc34d14 commit c460a4b

File tree

9 files changed

+120
-52
lines changed

9 files changed

+120
-52
lines changed
 

‎python/core/qgscoordinatereferencesystem.sip

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,9 @@ class QgsCoordinateReferenceSystem
304304
* @note added in 1.8
305305
*/
306306
static int syncDb();
307+
308+
/*! Save the proj4-string as a custom CRS
309+
* @returns bool true if success else false
310+
*/
311+
bool saveAsUserCRS( QString name );
307312
};

‎src/core/qgscoordinatereferencesystem.cpp

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,17 @@ bool QgsCoordinateReferenceSystem::createFromString( const QString theDefinition
134134
if ( reCrsStr.cap( 1 ).toLower() == "proj4" )
135135
{
136136
result = createFromProj4( reCrsStr.cap( 2 ) );
137+
//TODO: createFromProj4 used to save to the user database any new CRS
138+
// this behavior was changed in order to separate creation and saving.
139+
// Not sure if it necessary to save it here, should be checked by someone
140+
// familiar with the code (should also give a more descriptive name to the generated CRS)
141+
if( srsid() == 0 )
142+
{
143+
QString myName = QString( " * %1 (%2)" )
144+
.arg( QObject::tr( "Generated CRS", "A CRS automatically generated from layer info get this prefix for description" ) )
145+
.arg( toProj4() );
146+
saveAsUserCRS(myName);
147+
}
137148
}
138149
else
139150
{
@@ -460,6 +471,17 @@ bool QgsCoordinateReferenceSystem::createFromWkt( QString theWkt )
460471

461472
createFromProj4( proj4src );
462473
}
474+
//TODO: createFromProj4 used to save to the user database any new CRS
475+
// this behavior was changed in order to separate creation and saving.
476+
// Not sure if it necessary to save it here, should be checked by someone
477+
// familiar with the code (should also give a more descriptive name to the generated CRS)
478+
if( mSrsId == 0 )
479+
{
480+
QString myName = QString( " * %1 (%2)" )
481+
.arg( QObject::tr( "Generated CRS", "A CRS automatically generated from layer info get this prefix for description" ) )
482+
.arg( toProj4() );
483+
saveAsUserCRS(myName);
484+
}
463485

464486
CPLFree( proj4src );
465487

@@ -641,46 +663,6 @@ bool QgsCoordinateReferenceSystem::createFromProj4( const QString theProj4String
641663
{
642664
QgsDebugMsg( "Projection is not found in databases." );
643665
setProj4String( myProj4String );
644-
645-
// Is the SRS is valid now, we know it's a decent +proj string that can be entered into the srs.db
646-
if ( mIsValidFlag )
647-
{
648-
// but the proj.4 parsed string might already be in our database
649-
myRecord = getRecord( "select * from tbl_srs where parameters=" + quotedValue( toProj4() ) + " order by deprecated" );
650-
if ( myRecord.empty() )
651-
{
652-
// It's not, so try to add it
653-
QgsDebugMsg( "Projection appears to be valid. Save to database!" );
654-
mIsValidFlag = saveAsUserCRS();
655-
656-
if ( mIsValidFlag )
657-
{
658-
// but validate that it's there afterwards
659-
myRecord = getRecord( "select * from tbl_srs where parameters=" + quotedValue( toProj4() ) + " order by deprecated" );
660-
}
661-
}
662-
663-
if ( !myRecord.empty() )
664-
{
665-
// take the srid from the record
666-
mySrsId = myRecord["srs_id"].toLong();
667-
QgsDebugMsg( "proj4string match search for srsid returned srsid: " + QString::number( mySrsId ) );
668-
if ( mySrsId > 0 )
669-
{
670-
createFromSrsId( mySrsId );
671-
}
672-
else
673-
{
674-
QgsDebugMsg( QString( "invalid srid %1 found" ).arg( mySrsId ) );
675-
mIsValidFlag = false;
676-
}
677-
}
678-
else
679-
{
680-
QgsDebugMsg( "Couldn't find newly added proj string?" );
681-
mIsValidFlag = false;
682-
}
683-
}
684666
}
685667

686668
return mIsValidFlag;
@@ -1230,6 +1212,18 @@ bool QgsCoordinateReferenceSystem::readXML( QDomNode & theNode )
12301212
//@TODO this srs needs to be validated!!!
12311213
mIsValidFlag = true; //shamelessly hard coded for now
12321214
}
1215+
//TODO: createFromProj4 used to save to the user database any new CRS
1216+
// this behavior was changed in order to separate creation and saving.
1217+
// Not sure if it necessary to save it here, should be checked by someone
1218+
// familiar with the code (should also give a more descriptive name to the generated CRS)
1219+
if( mSrsId == 0 )
1220+
{
1221+
QString myName = QString( " * %1 (%2)" )
1222+
.arg( QObject::tr( "Generated CRS", "A CRS automatically generated from layer info get this prefix for description" ) )
1223+
.arg( toProj4() );
1224+
saveAsUserCRS(myName);
1225+
}
1226+
12331227
}
12341228
}
12351229
else
@@ -1426,7 +1420,7 @@ QString QgsCoordinateReferenceSystem::validationHint()
14261420
/// Copied from QgsCustomProjectionDialog ///
14271421
/// Please refactor into SQL handler !!! ///
14281422

1429-
bool QgsCoordinateReferenceSystem::saveAsUserCRS()
1423+
bool QgsCoordinateReferenceSystem::saveAsUserCRS(QString name)
14301424
{
14311425
if ( ! mIsValidFlag )
14321426
{
@@ -1435,9 +1429,6 @@ bool QgsCoordinateReferenceSystem::saveAsUserCRS()
14351429
}
14361430

14371431
QString mySql;
1438-
QString myName = QString( " * %1 (%2)" )
1439-
.arg( QObject::tr( "Generated CRS", "A CRS automatically generated from layer info get this prefix for description" ) )
1440-
.arg( toProj4() );
14411432

14421433
//if this is the first record we need to ensure that its srs_id is 10000. For
14431434
//any rec after that sqlite3 will take care of the autonumering
@@ -1447,7 +1438,7 @@ bool QgsCoordinateReferenceSystem::saveAsUserCRS()
14471438
{
14481439
mySql = "insert into tbl_srs (srs_id,description,projection_acronym,ellipsoid_acronym,parameters,is_geo) values ("
14491440
+ QString::number( USER_CRS_START_ID )
1450-
+ "," + quotedValue( myName )
1441+
+ "," + quotedValue( name )
14511442
+ "," + quotedValue( projectionAcronym() )
14521443
+ "," + quotedValue( ellipsoidAcronym() )
14531444
+ "," + quotedValue( toProj4() )
@@ -1456,7 +1447,7 @@ bool QgsCoordinateReferenceSystem::saveAsUserCRS()
14561447
else
14571448
{
14581449
mySql = "insert into tbl_srs (description,projection_acronym,ellipsoid_acronym,parameters,is_geo) values ("
1459-
+ quotedValue( myName )
1450+
+ quotedValue( name )
14601451
+ "," + quotedValue( projectionAcronym() )
14611452
+ "," + quotedValue( ellipsoidAcronym() )
14621453
+ "," + quotedValue( toProj4() )

‎src/core/qgscoordinatereferencesystem.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,6 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
151151
* check for a match in entities that have the same ellps and proj entries so
152152
* that it doesn't munch yer cpu so much.
153153
*
154-
* @note If the srs was not matched, we will create a new entry on the users tbl_srs
155-
* for this srs.
156-
*
157154
* @param theProjString A proj4 format string
158155
* @return bool TRUE if success else false
159156
*/
@@ -350,6 +347,12 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
350347
* @note added in 1.8
351348
*/
352349
static int syncDb();
350+
351+
352+
/*! Save the proj4-string as a custom CRS
353+
* @returns bool true if success else false
354+
*/
355+
bool saveAsUserCRS( QString name );
353356

354357
// Mutators -----------------------------------
355358
// We don't want to expose these to the public api since they wont create
@@ -447,9 +450,6 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
447450
//! Work out the projection units and set the appropriate local variable
448451
void setMapUnits();
449452

450-
//! Save the proj4-string as a custom CRS
451-
bool saveAsUserCRS();
452-
453453
//! Helper for getting number of user CRS already in db
454454
long getRecordCount();
455455

‎src/core/qgsdistancearea.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,18 @@ bool QgsDistanceArea::setEllipsoid( const QString& ellipsoid )
212212
QString proj4 = "+proj=longlat +ellps=" + ellipsoid + " +no_defs";
213213
QgsCoordinateReferenceSystem destCRS;
214214
destCRS.createFromProj4( proj4 );
215+
//TODO: createFromProj4 used to save to the user database any new CRS
216+
// this behavior was changed in order to separate creation and saving.
217+
// Not sure if it necessary to save it here, should be checked by someone
218+
// familiar with the code (should also give a more descriptive name to the generated CRS)
219+
if( destCRS.srsid() == 0 )
220+
{
221+
QString myName = QString( " * %1 (%2)" )
222+
.arg( QObject::tr( "Generated CRS", "A CRS automatically generated from layer info get this prefix for description" ) )
223+
.arg( destCRS.toProj4() );
224+
destCRS.saveAsUserCRS(myName);
225+
}
226+
//
215227

216228
// set transformation from project CRS to ellipsoid coordinates
217229
mCoordTransform->setDestCRS( destCRS );

‎src/gui/qgsprojectionselector.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,20 @@ QgsProjectionSelector::QgsProjectionSelector( QWidget* parent, const char *name,
8181
// No? Skip this entry
8282
continue;
8383
}
84+
else
85+
{
86+
//TODO: createFromProj4 used to save to the user database any new CRS
87+
// this behavior was changed in order to separate creation and saving.
88+
// Not sure if it necessary to save it here, should be checked by someone
89+
// familiar with the code (should also give a more descriptive name to the generated CRS)
90+
if( crs.srsid() == 0 )
91+
{
92+
QString myName = QString( " * %1 (%2)" )
93+
.arg( QObject::tr( "Generated CRS", "A CRS automatically generated from layer info get this prefix for description" ) )
94+
.arg( crs.toProj4() );
95+
crs.saveAsUserCRS(myName);
96+
}
97+
}
8498
}
8599
mRecentProjections << QString::number( crs.srsid() );
86100
}

‎src/mapserver/qgssldparser.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,18 @@ void QgsSLDParser::setCrsForLayer( const QDomElement& layerElem, QgsMapLayer* ml
15241524
{
15251525
QgsCoordinateReferenceSystem srs;
15261526
srs.createFromProj4( projString );
1527+
//TODO: createFromProj4 used to save to the user database any new CRS
1528+
// this behavior was changed in order to separate creation and saving.
1529+
// Not sure if it necessary to save it here, should be checked by someone
1530+
// familiar with the code (should also give a more descriptive name to the generated CRS)
1531+
if( srs.srsid() == 0 )
1532+
{
1533+
QString myName = QString( " * %1 (%2)" )
1534+
.arg( QObject::tr( "Generated CRS", "A CRS automatically generated from layer info get this prefix for description" ) )
1535+
.arg( srs.toProj4() );
1536+
srs.saveAsUserCRS(myName);
1537+
}
1538+
15271539
ml->setCrs( srs );
15281540
}
15291541
}

‎src/providers/grass/qgsgrassgislib.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,17 @@ int GRASS_LIB_EXPORT QgsGrassGisLib::G__gisinit( const char * version, const cha
221221
{
222222
fatal( "Cannot create CRS from QGIS_GRASS_CRS: " + crsStr );
223223
}
224+
//TODO: createFromProj4 used to save to the user database any new CRS
225+
// this behavior was changed in order to separate creation and saving.
226+
// Not sure if it necessary to save it here, should be checked by someone
227+
// familiar with the code (should also give a more descriptive name to the generated CRS)
228+
if( mCrs.srsid() == 0 )
229+
{
230+
QString myName = QString( " * %1 (%2)" )
231+
.arg( QObject::tr( "Generated CRS", "A CRS automatically generated from layer info get this prefix for description" ) )
232+
.arg( mCrs.toProj4() );
233+
mCrs.saveAsUserCRS(myName);
234+
}
224235
}
225236
mDistanceArea.setSourceCrs( mCrs.srsid() );
226237

‎src/providers/spatialite/qgsspatialiteprovider.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3200,6 +3200,18 @@ QgsCoordinateReferenceSystem QgsSpatiaLiteProvider::crs()
32003200
if ( !srs.isValid() )
32013201
{
32023202
srs.createFromProj4( mProj4text );
3203+
//TODO: createFromProj4 used to save to the user database any new CRS
3204+
// this behavior was changed in order to separate creation and saving.
3205+
// Not sure if it necessary to save it here, should be checked by someone
3206+
// familiar with the code (should also give a more descriptive name to the generated CRS)
3207+
if( srs.srsid() == 0 )
3208+
{
3209+
QString myName = QString( " * %1 (%2)" )
3210+
.arg( QObject::tr( "Generated CRS", "A CRS automatically generated from layer info get this prefix for description" ) )
3211+
.arg( srs.toProj4() );
3212+
srs.saveAsUserCRS(myName);
3213+
}
3214+
32033215
}
32043216
return srs;
32053217
}

‎src/providers/sqlanywhere/qgssqlanywhereprovider.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,6 +1886,17 @@ QgsSqlAnywhereProvider::checkSrs()
18861886
SaDebugMsg( "Failed to create CRS from Proj4 description. Trying WKT." );
18871887
mCrs.createFromWkt( srsWkt );
18881888
}
1889+
//TODO: createFromProj4 used to save to the user database any new CRS
1890+
// this behavior was changed in order to separate creation and saving.
1891+
// Not sure if it necessary to save it here, should be checked by someone
1892+
// familiar with the code (should also give a more descriptive name to the generated CRS)
1893+
if( mCrs.srsid() == 0 )
1894+
{
1895+
QString myName = QString( " * %1 (%2)" )
1896+
.arg( QObject::tr( "Generated CRS", "A CRS automatically generated from layer info get this prefix for description" ) )
1897+
.arg( mCrs.toProj4() );
1898+
mCrs.saveAsUserCRS(myName);
1899+
}
18891900

18901901
return true;
18911902
} // QgsSqlAnywhereProvider::checkSrs()

0 commit comments

Comments
 (0)
Please sign in to comment.