Skip to content

Commit 307c00a

Browse files
author
timlinux
committedNov 15, 2007
Bug fox for #809 - file writer does not over write shapefiles.
Files will now be owritten. Unit tests updated too. git-svn-id: http://svn.osgeo.org/qgis/trunk@7409 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 2f9bbdc commit 307c00a

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed
 

‎src/app/legend/qgslegendlayerfile.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,16 @@ void QgsLegendLayerFile::saveAsShapefileGeneral(bool saveOnlySelection)
343343
shapefileName += ".shp";
344344
}
345345

346+
// overwrite the file - user will already have been prompted
347+
// to verify they want to overwrite by the file dialog above
348+
if (QFile::exists(shapefileName))
349+
{
350+
if (!QgsVectorFileWriter::deleteShapeFile(shapefileName))
351+
{
352+
return;
353+
}
354+
}
355+
// ok if the file existed it should be deleted now so we can continue...
346356
QApplication::setOverrideCursor(Qt::waitCursor);
347357

348358
QgsVectorFileWriter::WriterError error;

‎src/core/qgsvectorfilewriter.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "qgsvectordataprovider.h"
2727

2828
#include <QFile>
29+
#include <QFileInfo>
2930
#include <QTextCodec>
3031

3132
#include <cassert>
@@ -329,3 +330,49 @@ QgsVectorFileWriter::WriterError
329330

330331
return NoError;
331332
}
333+
334+
335+
bool QgsVectorFileWriter::deleteShapeFile(QString theFileName)
336+
{
337+
//
338+
// Remove old copies that may be lying around
339+
//
340+
QFileInfo myInfo(theFileName);
341+
QString myFileBase = theFileName.replace(".shp","");
342+
if (myInfo.exists())
343+
{
344+
if(!QFile::remove(myFileBase + ".shp"))
345+
{
346+
qDebug("Removing file failed : " + myFileBase.toLocal8Bit() + ".shp");
347+
return false;
348+
}
349+
}
350+
myInfo.setFile(myFileBase + ".shx");
351+
if (myInfo.exists())
352+
{
353+
if(!QFile::remove(myFileBase + ".shx"))
354+
{
355+
qDebug("Removing file failed : " + myFileBase.toLocal8Bit() + ".shx");
356+
return false;
357+
}
358+
}
359+
myInfo.setFile(myFileBase + ".dbf");
360+
if (myInfo.exists())
361+
{
362+
if(!QFile::remove(myFileBase + ".dbf"))
363+
{
364+
qDebug("Removing file failed : " + myFileBase.toLocal8Bit() + ".dbf");
365+
return false;
366+
}
367+
}
368+
myInfo.setFile(myFileBase + ".prj");
369+
if (myInfo.exists())
370+
{
371+
if(!QFile::remove(myFileBase + ".prj"))
372+
{
373+
qDebug("Removing file failed : " + myFileBase.toLocal8Bit() + ".prj");
374+
return false;
375+
}
376+
}
377+
return true;
378+
}

‎src/core/qgsvectorfilewriter.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ class CORE_EXPORT QgsVectorFileWriter
7373
/** close opened shapefile for writing */
7474
~QgsVectorFileWriter();
7575

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

7883
OGRGeometry* createEmptyGeometry(QGis::WKBTYPE wkbType);

0 commit comments

Comments
 (0)
Please sign in to comment.