Fix bug where proj4 string was not being written to project file properly

git-svn-id: http://svn.osgeo.org/qgis/trunk@3389 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
timlinux 2005-05-14 15:59:18 +00:00
parent 33ae555c20
commit d979e4aa1b
3 changed files with 87 additions and 154 deletions

View File

@ -216,6 +216,7 @@ void QgsProjectProperties::apply()
// write the currently selected projections _name_ to project settings
QgsProject::instance()->writeEntry("SpatialRefSys","/ProjectSRSName",projectionSelector->getSelectedName());
// write the currently selected projections _proj string_ to project settings
std::cout << "SpatialRefSys/ProjectSRSProj4String: " << projectionSelector->getCurrentProj4String() << std::endl;
QgsProject::instance()->writeEntry("SpatialRefSys","/ProjectSRSProj4String",projectionSelector->getCurrentProj4String());
// Set the map units to the projected coordinates if we are projecting
if (isProjected())

View File

@ -29,7 +29,7 @@
#include <qlineedit.h>
#include <qmessagebox.h>
#include <qregexp.h>
#include <qprogressdialog.h>
#include <qprogressdialog.h>
#include <qfileinfo.h>
#include <qdir.h>
#include <qtextstream.h>
@ -43,10 +43,10 @@
QgsProjectionSelector::QgsProjectionSelector( QWidget* parent , const char* name , WFlags fl )
: QgsProjectionSelectorBase( parent, "Projection Selector", fl )
: QgsProjectionSelectorBase( parent, "Projection Selector", fl )
{
// Get the package data path and set the full path name to the sqlite3 spatial reference
// database.
// Get the package data path and set the full path name to the sqlite3 spatial reference
// database.
#if defined(Q_OS_MACX) || defined(WIN32)
QString PKGDATAPATH = qApp->applicationDirPath() + "/share/qgis";
#endif
@ -58,8 +58,7 @@ QgsProjectionSelector::QgsProjectionSelector( QWidget* parent , const char* name
}
QgsProjectionSelector::~QgsProjectionSelector()
{
}
{}
void QgsProjectionSelector::setSelectedSRSName(QString theSRSNAme)
{
//get the srid given the wkt so we can pick the correct list item
@ -68,7 +67,7 @@ void QgsProjectionSelector::setSelectedSRSName(QString theSRSNAme)
#endif
//now delegate off to the rest of the work
QListViewItemIterator myIterator (lstCoordinateSystems);
while (myIterator.current())
while (myIterator.current())
{
if (myIterator.current()->text(0)==theSRSNAme)
{
@ -84,7 +83,7 @@ void QgsProjectionSelector::setSelectedSRSID(long theSRSID)
{
QString mySRSIDString=QString::number(theSRSID);
QListViewItemIterator myIterator (lstCoordinateSystems);
while (myIterator.current())
while (myIterator.current())
{
if (myIterator.current()->text(1)==mySRSIDString)
{
@ -111,7 +110,7 @@ QString QgsProjectionSelector::getSelectedName()
return QString::null;
}
}
// Returns the whole wkt for the selected projection node
// Returns the whole proj4 string for the selected projection node
QString QgsProjectionSelector::getCurrentProj4String()
{
// Only return the projection if there is a node in the tree
@ -120,46 +119,47 @@ QString QgsProjectionSelector::getCurrentProj4String()
// system
//
// Get the selected node
QListViewItem *lvi = lstCoordinateSystems->currentItem();
if(lvi)
QListViewItem *myItem = lstCoordinateSystems->currentItem();
if(myItem)
{
// Make sure the selected node is a srs and not a top-level projection node
std::cout << lvi->text(1) << std::endl;
if(lvi->text(1).length() > 0)
if(myItem->text(1).length() > 0)
{
QString myDatabaseFileName;
QString mySrsId = myItem->text(1);
std::cout << " QgsProjectionSelector::getCurrentProj4String : mySrsId = " << mySrsId << std::endl;
std::cout << " QgsProjectionSelector::getCurrentProj4String : USER_PROJECTION_START_ID = " << USER_PROJECTION_START_ID << std::endl;
//
// Determine if this is a user projection or a system on
// user projection defs all have srs_id >= 100000
//
if (lvi->text(1).toLong() >= USER_PROJECTION_START_ID)
if (mySrsId.toLong() >= USER_PROJECTION_START_ID)
{
myDatabaseFileName = QDir::homeDirPath () + "/.qgis/qgis.db";
QFileInfo myFileInfo;
myFileInfo.setFile(myDatabaseFileName);
if ( !myFileInfo.exists( ) )
if ( !myFileInfo.exists( ) ) //its unlikely that this condition will ever be reached
{
std::cout << " QgsSpatialRefSys::createFromSrid failed : users qgis.db not found" << std::endl;
std::cout << " QgsProjectionSelector::getCurrentProj4String : users qgis.db not found" << std::endl;
return NULL;
}
}
else //must be a system projection then
{
myDatabaseFileName=mSrsDatabaseFileName;
myDatabaseFileName = mSrsDatabaseFileName;
}
//
// set up the database
// XXX We could probabaly hold the database open for the life of this object,
// assuming that it will never be used anywhere else. Given the low overhead,
// opening it each time seems to be a reasonable approach at this time.
std::cout << "QgsProjectionSelector::getCurrentProj4String db = " << myDatabaseFileName << std::endl;
sqlite3 *db;
char *zErrMsg = 0;
int rc;
rc = sqlite3_open(myDatabaseFileName, &db);
if(rc)
if(rc)
{
std::cout << "Can't open database: " << sqlite3_errmsg(db) << std::endl;
// XXX This will likely never happen since on open, sqlite creates the
std::cout << "Can't open database: " << sqlite3_errmsg(db) << std::endl;
// XXX This will likely never happen since on open, sqlite creates the
// database if it does not exist.
assert(rc == 0);
}
@ -167,30 +167,37 @@ QString QgsProjectionSelector::getCurrentProj4String()
const char *pzTail;
sqlite3_stmt *ppStmt;
char *pzErrmsg;
QString sql = "select srtext from tbl_srs where srs_id = ";
sql += lvi->text(1);
QString sql = "select parameters from tbl_srs where srs_id = ";
sql += mySrsId;
#ifdef QGISDEBUG
std::cout << "Finding selected wkt using : " << sql << std::endl;
std::cout << "Selection sql : " << sql << std::endl;
#endif
rc = sqlite3_prepare(db, (const char *)sql, sql.length(), &ppStmt, &pzTail);
// XXX Need to free memory from the error msg if one is set
QString wkt;
QString myProjString;
if(rc == SQLITE_OK)
{
// get the first row of the result set
if(sqlite3_step(ppStmt) == SQLITE_ROW)
{
// get the wkt
wkt = (char*)sqlite3_column_text(ppStmt, 0);
myProjString = (char*)sqlite3_column_text(ppStmt, 0);
}
}
// close the statement
sqlite3_finalize(ppStmt);
// close the database
sqlite3_close(db);
// return the srs wkt
return wkt;
#ifdef QGISDEBUG
std::cout << "Item selected : " << myItem->text(0) << std::endl;
std::cout << "Item selected full string : " << myProjString << std::endl;
#endif
assert(myProjString.length() > 0);
return myProjString;
}
else
{
// No node is selected, return null
return NULL;
}
}
else
@ -213,7 +220,7 @@ long QgsProjectionSelector::getCurrentSRID()
if(lvi)
{
// Make sure the selected node is a srs and not a top-level projection node
std::cout << lvi->text(1) << std::endl;
std::cout << lvi->text(1) << std::endl;
if(lvi->text(1).length() > 0)
{
QString myDatabaseFileName;
@ -238,17 +245,17 @@ long QgsProjectionSelector::getCurrentSRID()
}
//
// set up the database
// XXX We could probabaly hold the database open for the life of this object,
// XXX We could probabaly hold the database open for the life of this object,
// assuming that it will never be used anywhere else. Given the low overhead,
// opening it each time seems to be a reasonable approach at this time.
sqlite3 *db;
char *zErrMsg = 0;
int rc;
rc = sqlite3_open(myDatabaseFileName, &db);
if(rc)
if(rc)
{
std::cout << "Can't open database: " << sqlite3_errmsg(db) << std::endl;
// XXX This will likely never happen since on open, sqlite creates the
std::cout << "Can't open database: " << sqlite3_errmsg(db) << std::endl;
// XXX This will likely never happen since on open, sqlite creates the
// database if it does not exist.
assert(rc == 0);
}
@ -270,7 +277,7 @@ long QgsProjectionSelector::getCurrentSRID()
// get the first row of the result set
if(sqlite3_step(ppStmt) == SQLITE_ROW)
{
// get the wkt
// get the wkt
mySrid = (char*)sqlite3_column_text(ppStmt, 0);
}
}
@ -320,7 +327,7 @@ void QgsProjectionSelector::getUserProjList()
if ( !myFileInfo.exists( ) )
{
#ifdef QGISDEBUG
std::cout << "Users qgis.db not found...skipping" << std::endl;
std::cout << "Users qgis.db not found...skipping" << std::endl;
#endif
return;
}
@ -332,12 +339,12 @@ void QgsProjectionSelector::getUserProjList()
int myResult;
//check the db is available
myResult = sqlite3_open(QString(myQGisSettingsDir+"qgis.db").latin1(), &myDatabase);
if(myResult)
if(myResult)
{
std::cout << "Can't open database: " << sqlite3_errmsg(myDatabase) << std::endl;
// XXX This will likely never happen since on open, sqlite creates the
std::cout << "Can't open database: " << sqlite3_errmsg(myDatabase) << std::endl;
// XXX This will likely never happen since on open, sqlite creates the
// database if it does not exist. But we checked earlier for its existance
// and aborted in that case. This is because we may be runnig from read only
// and aborted in that case. This is because we may be runnig from read only
// media such as live cd and dont want to force trying to create a db.
assert(myResult == 0);
}
@ -362,7 +369,7 @@ void QgsProjectionSelector::getUserProjList()
// close the sqlite3 statement
sqlite3_finalize(myPreparedStatement);
sqlite3_close(myDatabase);
}
}
void QgsProjectionSelector::getProjList()
{
@ -374,10 +381,10 @@ void QgsProjectionSelector::getProjList()
mProjList = new QListViewItem(lstCoordinateSystems,"Projected Coordinate System");
//bail out in case the projections db does not exist
//this is neccessary in case the pc is running linux with a
//read only filesystem because otherwise sqlite will try
//this is neccessary in case the pc is running linux with a
//read only filesystem because otherwise sqlite will try
//to create the db file on the fly
QFileInfo myFileInfo;
myFileInfo.setFile(mSrsDatabaseFileName);
if ( !myFileInfo.exists( ) )
@ -390,10 +397,10 @@ void QgsProjectionSelector::getProjList()
char *zErrMsg = 0;
int rc;
rc = sqlite3_open(mSrsDatabaseFileName, &db);
if(rc)
if(rc)
{
std::cout << "Can't open database: " << sqlite3_errmsg(db) << std::endl;
// XXX This will likely never happen since on open, sqlite creates the
std::cout << "Can't open database: " << sqlite3_errmsg(db) << std::endl;
// XXX This will likely never happen since on open, sqlite creates the
// database if it does not exist.
assert(rc == 0);
}
@ -415,7 +422,7 @@ void QgsProjectionSelector::getProjList()
sqlite3_finalize(ppStmt);
// Set up the query to retreive the projection information needed to populate the list
//note I am giving the full field names for clarity here and in case someown
//note I am giving the full field names for clarity here and in case someown
//changes the underlying view TS
sql = "select description,srs_id,is_geo, name,parameters from vw_srs";
#ifdef QGISDEBUG
@ -426,14 +433,14 @@ void QgsProjectionSelector::getProjList()
if(rc == SQLITE_OK)
{
#ifdef QGISDEBUG
std::cout << "SQL for projection list executed ok..." << std::endl;
std::cout << "SQL for projection list executed ok..." << std::endl;
#endif
QListViewItem *newItem;
// set up the progress dialog
int myProgress = 1;
QProgressDialog myProgressBar( "Building Projections List...", 0, myEntriesCount,
this, "progress", TRUE );
this, "progress", TRUE );
// set initial value to 1
myProgressBar.setProgress(myProgress);
while(sqlite3_step(ppStmt) == SQLITE_ROW)
@ -446,7 +453,7 @@ void QgsProjectionSelector::getProjList()
// check to see if the srs is geographic
int isGeo = sqlite3_column_int(ppStmt, 2);
if(isGeo)
{
{
// this is a geographic coordinate system
// Add it to the tree
newItem = new QListViewItem(mGeoList, (char *)sqlite3_column_text(ppStmt,0));
@ -475,7 +482,7 @@ void QgsProjectionSelector::getProjList()
}
//Only enable thse lines temporarily if you want to generate a script
//to update proj an ellipoid fields in the srs.db
//updateProjAndEllipsoidAcronyms(QString((char *)sqlite3_column_text(ppStmt, 1)).toLong(),
//updateProjAndEllipsoidAcronyms(QString((char *)sqlite3_column_text(ppStmt, 1)).toLong(),
// QString((char *)sqlite3_column_text(ppStmt, 4))) ;
}
// update the progress bar to 100% -- just for eye candy purposes (some people hate to
@ -486,14 +493,14 @@ void QgsProjectionSelector::getProjList()
sqlite3_finalize(ppStmt);
// close the database
sqlite3_close(db);
}
}
//this is a little helper function to populate the (well give you a sql script to populate)
//the projection_acronym and ellipsoid_acronym fields in the srs.db backend
//To cause it to be run, uncomment or add the line:
// updateProjAndEllipsoidAcronyms(QString((char *)sqlite3_column_text(ppStmt, 1)).toLong(),
// updateProjAndEllipsoidAcronyms(QString((char *)sqlite3_column_text(ppStmt, 1)).toLong(),
// QString((char *)sqlite3_column_text(ppStmt, 4))) ;
//to the above method. NOTE it will cause a huge slow down in population of the proj selector dialog so
//to the above method. NOTE it will cause a huge slow down in population of the proj selector dialog so
//remember to disable it again!
void QgsProjectionSelector::updateProjAndEllipsoidAcronyms(int theSrsid,QString theProj4String)
{
@ -504,16 +511,16 @@ void QgsProjectionSelector::updateProjAndEllipsoidAcronyms(int theSrsid,QString
myFile.open( IO_WriteOnly | IO_Append );
QTextStream myStream( &myFile );
QRegExp myProjRegExp( "proj=[a-zA-Z]* " );
QRegExp myProjRegExp( "proj=[a-zA-Z]* " );
int myStart= 0;
int myLength=0;
myStart = myProjRegExp.search(theProj4String, myStart);
QString myProjectionAcronym;
QString myProjectionAcronym;
if (myStart==-1)
{
std::cout << "proj string supplied has no +proj argument" << std::endl;
@ -524,9 +531,9 @@ void QgsProjectionSelector::updateProjAndEllipsoidAcronyms(int theSrsid,QString
myLength = myProjRegExp.matchedLength();
myProjectionAcronym = theProj4String.mid(myStart+PROJ_PREFIX_LEN,myLength-(PROJ_PREFIX_LEN+1));//+1 for space
}
QRegExp myEllipseRegExp( "ellps=[a-zA-Z0-9\-]* " );
QRegExp myEllipseRegExp( "ellps=[a-zA-Z0-9\-]* " );
myStart= 0;
myLength=0;
myStart = myEllipseRegExp.search(theProj4String, myStart);
@ -544,7 +551,7 @@ void QgsProjectionSelector::updateProjAndEllipsoidAcronyms(int theSrsid,QString
//now create the update statement
QString mySql = "update tbl_srs set projection_acronym='" + myProjectionAcronym +
QString mySql = "update tbl_srs set projection_acronym='" + myProjectionAcronym +
"', ellipsoid_acronym='" + myEllipsoidAcronym + "' where " +
"srs_id=" + QString::number(theSrsid)+";";
@ -552,92 +559,18 @@ void QgsProjectionSelector::updateProjAndEllipsoidAcronyms(int theSrsid,QString
//tmporary hack
myStream << mySql << "\n";
myFile.close();
//std::cout
//std::cout
}
// New coordinate system selected from the list
void QgsProjectionSelector::coordinateSystemSelected( QListViewItem * theItem )
{
QString myDatabaseFileName;
QString mySrsId = theItem->text(1);
std::cout << " QgsProjectionSelector::coordinateSystemSelected : mySrsId = " << mySrsId << std::endl;
std::cout << " QgsProjectionSelector::coordinateSystemSelected : USER_PROJECTION_START_ID = " << USER_PROJECTION_START_ID << std::endl;
//
// Determine if this is a user projection or a system on
// user projection defs all have srs_id >= 100000
//
if (mySrsId.toLong() >= USER_PROJECTION_START_ID)
{
myDatabaseFileName = QDir::homeDirPath () + "/.qgis/qgis.db";
QFileInfo myFileInfo;
myFileInfo.setFile(myDatabaseFileName);
if ( !myFileInfo.exists( ) ) //its unlikely that this condition will ever be reached
{
std::cout << " QgsProjectionSelector::coordinateSystemSelected : users qgis.db not found" << std::endl;
return;
}
}
else //must be a system projection then
{
myDatabaseFileName = mSrsDatabaseFileName;
}
std::cout << "QgsProjectionSelector::coordinateSystemSelected db = " << myDatabaseFileName << std::endl;
if(theItem->text(1).length() > 0)
{
sqlite3 *db;
char *zErrMsg = 0;
int rc;
rc = sqlite3_open(myDatabaseFileName, &db);
if(rc)
{
std::cout << "Can't open database: " << sqlite3_errmsg(db) << std::endl;
// XXX This will likely never happen since on open, sqlite creates the
// database if it does not exist.
assert(rc == 0);
}
// prepare the sql statement
const char *pzTail;
sqlite3_stmt *ppStmt;
char *pzErrmsg;
QString sql = "select parameters from tbl_srs where srs_id = ";
sql += mySrsId;
#ifdef QGISDEBUG
std::cout << "Selection sql : " << sql << std::endl;
#endif
rc = sqlite3_prepare(db, (const char *)sql, sql.length(), &ppStmt, &pzTail);
// XXX Need to free memory from the error msg if one is set
QString myProjString;
if(rc == SQLITE_OK)
{
if(sqlite3_step(ppStmt) == SQLITE_ROW)
QString myProjString = getCurrentProj4String();
if (myProjString)
{
myProjString = (char*)sqlite3_column_text(ppStmt, 0);
teProjection->setText(myProjString);
}
}
// close the statement
sqlite3_finalize(ppStmt);
// close the database
sqlite3_close(db);
#ifdef QGISDEBUG
std::cout << "Item selected : " << theItem->text(0) << std::endl;
std::cout << "Item selected full string : " << myProjString << std::endl;
#endif
assert(myProjString.length() > 0);
// reformat the wkt to improve the display in the textedit
// box
myProjString = myProjString.replace(",", ", ");
teProjection->setText(myProjString);
// let anybody who's listening know about the change
// XXX Is this appropriate here if the dialog is cancelled??
emit wktSelected(myProjString);
}
}
void QgsProjectionSelector::pbnFind_clicked()
@ -655,12 +588,12 @@ void QgsProjectionSelector::pbnFind_clicked()
int myResult;
//check the db is available
myResult = sqlite3_open(mSrsDatabaseFileName, &myDatabase);
if(myResult)
if(myResult)
{
std::cout << "Can't open database: " << sqlite3_errmsg(myDatabase) << std::endl;
// XXX This will likely never happen since on open, sqlite creates the
std::cout << "Can't open database: " << sqlite3_errmsg(myDatabase) << std::endl;
// XXX This will likely never happen since on open, sqlite creates the
// database if it does not exist. But we checked earlier for its existance
// and aborted in that case. This is because we may be runnig from read only
// and aborted in that case. This is because we may be runnig from read only
// media such as live cd and dont want to force trying to create a db.
assert(myResult == 0);
}

View File

@ -64,7 +64,6 @@ private:
void coordinateSystemSelected(QListViewItem*);
signals:
void wktSelected(QString theWKT);
void sridSelected(QString theSRID);
//! Refresh any listening canvases
void refresh();