Skip to content

Commit

Permalink
[georef] Don't crash when loading invalid GCP files
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 21, 2015
1 parent 759d85a commit 41fd4ef
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 35 deletions.
84 changes: 50 additions & 34 deletions src/plugins/georeferencer/qgsgeorefplugingui.cpp
Expand Up @@ -271,7 +271,7 @@ void QgsGeorefPluginGui::openRaster()

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

mCanvas->setExtent( mLayer->extent() );
mCanvas->refresh();
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/georeferencer/qgsgeorefplugingui.h
Expand Up @@ -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();

Expand Down

0 comments on commit 41fd4ef

Please sign in to comment.