Skip to content
Permalink
Browse files

[georef] Don't crash when loading invalid GCP files

  • Loading branch information
nyalldawson committed Jun 21, 2015
1 parent 759d85a commit 41fd4ef680afc8784326829fbce88192a428d5e7
Showing with 51 additions and 35 deletions.
  1. +50 −34 src/plugins/georeferencer/qgsgeorefplugingui.cpp
  2. +1 −1 src/plugins/georeferencer/qgsgeorefplugingui.h
@@ -271,7 +271,7 @@ void QgsGeorefPluginGui::openRaster()

// load previously added points
mGCPpointsFileName = mRasterFileName + ".points";
loadGCPs();
( void )loadGCPs();

mCanvas->setExtent( mLayer->extent() );
mCanvas->refresh();
@@ -581,7 +581,14 @@ void QgsGeorefPluginGui::loadGCPsDialog()
if ( mGCPpointsFileName.isEmpty() )
return;

loadGCPs();
if ( !loadGCPs() )
{
mMessageBar->pushMessage( tr( "Invalid GCP file" ), tr( "GCP file could not be read." ), QgsMessageBar::WARNING, messageTimeout() );
}
else
{
mMessageBar->pushMessage( tr( "GCPs loaded" ), tr( "GCP file successfully loaded." ), QgsMessageBar::INFO, messageTimeout() );
}
}

void QgsGeorefPluginGui::saveGCPsDialog()
@@ -1195,48 +1202,57 @@ void QgsGeorefPluginGui::writeSettings()
}

// GCP points
void QgsGeorefPluginGui::loadGCPs( /*bool verbose*/ )
bool QgsGeorefPluginGui::loadGCPs( /*bool verbose*/ )
{
QFile pointFile( mGCPpointsFileName );
if ( pointFile.open( QIODevice::ReadOnly ) )
if ( !pointFile.open( QIODevice::ReadOnly ) )
{
clearGCPData();
return false;
}

QTextStream points( &pointFile );
QString line = points.readLine();
int i = 0;
while ( !points.atEnd() )
clearGCPData();

QTextStream points( &pointFile );
QString line = points.readLine();
int i = 0;
while ( !points.atEnd() )
{
line = points.readLine();
QStringList ls;
if ( line.contains( QRegExp( "," ) ) ) // in previous format "\t" is delimiter of points in new - ","
{
line = points.readLine();
QStringList ls;
if ( line.contains( QRegExp( "," ) ) ) // in previous format "\t" is delimiter of points in new - ","
{
// points from new georeferencer
ls = line.split( "," );
}
else
{
// points from prev georeferencer
ls = line.split( "\t" );
}
// points from new georeferencer
ls = line.split( "," );
}
else
{
// points from prev georeferencer
ls = line.split( "\t" );
}

QgsPoint mapCoords( ls.at( 0 ).toDouble(), ls.at( 1 ).toDouble() ); // map x,y
QgsPoint pixelCoords( ls.at( 2 ).toDouble(), ls.at( 3 ).toDouble() ); // pixel x,y
if ( ls.count() == 5 )
{
bool enable = ls.at( 4 ).toInt();
addPoint( pixelCoords, mapCoords, enable, false/*, verbose*/ ); // enabled
}
else
addPoint( pixelCoords, mapCoords, true, false );
if ( ls.count() < 4 )
{
return false;
}

++i;
QgsPoint mapCoords( ls.at( 0 ).toDouble(), ls.at( 1 ).toDouble() ); // map x,y
QgsPoint pixelCoords( ls.at( 2 ).toDouble(), ls.at( 3 ).toDouble() ); // pixel x,y
if ( ls.count() == 5 )
{
bool enable = ls.at( 4 ).toInt();
addPoint( pixelCoords, mapCoords, enable, false/*, verbose*/ ); // enabled
}
else
addPoint( pixelCoords, mapCoords, true, false );

mInitialPoints = mPoints;
// showMessageInLog(tr("GCP points loaded from"), mGCPpointsFileName);
mCanvas->refresh();
++i;
}

mInitialPoints = mPoints;
// showMessageInLog(tr("GCP points loaded from"), mGCPpointsFileName);
mCanvas->refresh();

return true;
}

void QgsGeorefPluginGui::saveGCPs()
@@ -151,7 +151,7 @@ class QgsGeorefPluginGui : public QMainWindow, private Ui::QgsGeorefPluginGuiBas
void writeSettings();

// gcp points
void loadGCPs( /*bool verbose = true*/ );
bool loadGCPs( /*bool verbose = true*/ );
void saveGCPs();
QgsGeorefPluginGui::SaveGCPs checkNeedGCPSave();

0 comments on commit 41fd4ef

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