Skip to content

Commit e32b9bf

Browse files
author
wonder
committed
QgsVectorFileWriter:
- added missing deleteShapeFile() PyQGIS wrapper - added some error handling when adding features - improved deleteShapeFile method git-svn-id: http://svn.osgeo.org/qgis/trunk@8621 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent e674fca commit e32b9bf

File tree

2 files changed

+21
-34
lines changed

2 files changed

+21
-34
lines changed

python/core/qgsvectorfilewriter.sip

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,10 @@ public:
4747
/** close opened shapefile for writing */
4848
~QgsVectorFileWriter();
4949

50+
/** Delete a shapefile (and its accompanying shx / dbf / prf)
51+
* @param QString theFileName - /path/to/file.shp
52+
* @return bool true if the file was deleted successfully
53+
*/
54+
static bool deleteShapeFile(QString theFileName);
5055
};
5156

src/core/qgsvectorfilewriter.cpp

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ bool QgsVectorFileWriter::addFeature(QgsFeature& feature)
200200
OGR_F_SetFieldString(poFeature, ogrField, mCodec->fromUnicode(attrValue.toString()).data());
201201
break;
202202
default:
203-
//assert(0 && "invalid variant type");
203+
QgsDebugMsg("Invalid variant type for field "+QString::number(ogrField)+": "+QString::number(attrValue.type()));
204204
return false;
205205
}
206206
}
@@ -321,43 +321,25 @@ bool QgsVectorFileWriter::deleteShapeFile(QString theFileName)
321321
{
322322
//
323323
// Remove old copies that may be lying around
324+
// TODO: should be case-insensitive
324325
//
325-
QFileInfo myInfo(theFileName);
326326
QString myFileBase = theFileName.replace(".shp","");
327-
if (myInfo.exists())
328-
{
329-
if(!QFile::remove(myFileBase + ".shp"))
330-
{
331-
qDebug("Removing file failed : " + myFileBase.toLocal8Bit() + ".shp");
332-
return false;
333-
}
334-
}
335-
myInfo.setFile(myFileBase + ".shx");
336-
if (myInfo.exists())
337-
{
338-
if(!QFile::remove(myFileBase + ".shx"))
339-
{
340-
qDebug("Removing file failed : " + myFileBase.toLocal8Bit() + ".shx");
341-
return false;
342-
}
343-
}
344-
myInfo.setFile(myFileBase + ".dbf");
345-
if (myInfo.exists())
346-
{
347-
if(!QFile::remove(myFileBase + ".dbf"))
348-
{
349-
qDebug("Removing file failed : " + myFileBase.toLocal8Bit() + ".dbf");
350-
return false;
351-
}
352-
}
353-
myInfo.setFile(myFileBase + ".prj");
354-
if (myInfo.exists())
327+
bool ok = TRUE;
328+
329+
const char* suffixes[] = { ".shp", ".shx", ".dbf", ".prj", ".qix" };
330+
for (int i = 0; i < sizeof(suffixes) / sizeof(char*); i++)
355331
{
356-
if(!QFile::remove(myFileBase + ".prj"))
332+
QString file = myFileBase + suffixes[i];
333+
QFileInfo myInfo(file);
334+
if (myInfo.exists())
357335
{
358-
qDebug("Removing file failed : " + myFileBase.toLocal8Bit() + ".prj");
359-
return false;
336+
if(!QFile::remove(file))
337+
{
338+
QgsDebugMsg("Removing file failed : " + file);
339+
ok = FALSE;
340+
}
360341
}
361342
}
362-
return true;
343+
344+
return ok;
363345
}

0 commit comments

Comments
 (0)