Skip to content

Commit

Permalink
Bug fox for #809 - file writer does not over write shapefiles.
Browse files Browse the repository at this point in the history
Files will now be owritten. Unit tests updated too.


git-svn-id: http://svn.osgeo.org/qgis/trunk@7409 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
timlinux committed Nov 15, 2007
1 parent 2f9bbdc commit 307c00a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/app/legend/qgslegendlayerfile.cpp
Expand Up @@ -343,6 +343,16 @@ void QgsLegendLayerFile::saveAsShapefileGeneral(bool saveOnlySelection)
shapefileName += ".shp";
}

// overwrite the file - user will already have been prompted
// to verify they want to overwrite by the file dialog above
if (QFile::exists(shapefileName))
{
if (!QgsVectorFileWriter::deleteShapeFile(shapefileName))
{
return;
}
}
// ok if the file existed it should be deleted now so we can continue...
QApplication::setOverrideCursor(Qt::waitCursor);

QgsVectorFileWriter::WriterError error;
Expand Down
47 changes: 47 additions & 0 deletions src/core/qgsvectorfilewriter.cpp
Expand Up @@ -26,6 +26,7 @@
#include "qgsvectordataprovider.h"

#include <QFile>
#include <QFileInfo>
#include <QTextCodec>

#include <cassert>
Expand Down Expand Up @@ -329,3 +330,49 @@ QgsVectorFileWriter::WriterError

return NoError;
}


bool QgsVectorFileWriter::deleteShapeFile(QString theFileName)
{
//
// Remove old copies that may be lying around
//
QFileInfo myInfo(theFileName);
QString myFileBase = theFileName.replace(".shp","");
if (myInfo.exists())
{
if(!QFile::remove(myFileBase + ".shp"))
{
qDebug("Removing file failed : " + myFileBase.toLocal8Bit() + ".shp");
return false;
}
}
myInfo.setFile(myFileBase + ".shx");
if (myInfo.exists())
{
if(!QFile::remove(myFileBase + ".shx"))
{
qDebug("Removing file failed : " + myFileBase.toLocal8Bit() + ".shx");
return false;
}
}
myInfo.setFile(myFileBase + ".dbf");
if (myInfo.exists())
{
if(!QFile::remove(myFileBase + ".dbf"))
{
qDebug("Removing file failed : " + myFileBase.toLocal8Bit() + ".dbf");
return false;
}
}
myInfo.setFile(myFileBase + ".prj");
if (myInfo.exists())
{
if(!QFile::remove(myFileBase + ".prj"))
{
qDebug("Removing file failed : " + myFileBase.toLocal8Bit() + ".prj");
return false;
}
}
return true;
}
5 changes: 5 additions & 0 deletions src/core/qgsvectorfilewriter.h
Expand Up @@ -73,6 +73,11 @@ class CORE_EXPORT QgsVectorFileWriter
/** close opened shapefile for writing */
~QgsVectorFileWriter();

/** Delete a shapefile (and its accompanying shx / dbf / prf)
* @param QString theFileName - /path/to/file.shp
* @return bool true if the file was deleted successfully
*/
static bool deleteShapeFile(QString theFileName);
protected:

OGRGeometry* createEmptyGeometry(QGis::WKBTYPE wkbType);
Expand Down

0 comments on commit 307c00a

Please sign in to comment.