Skip to content

Commit

Permalink
QgsVectorFileWriter:
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
wonder committed Jun 8, 2008
1 parent e674fca commit e32b9bf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 34 deletions.
5 changes: 5 additions & 0 deletions python/core/qgsvectorfilewriter.sip
Expand Up @@ -47,5 +47,10 @@ public:
/** 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);
};

50 changes: 16 additions & 34 deletions src/core/qgsvectorfilewriter.cpp
Expand Up @@ -200,7 +200,7 @@ bool QgsVectorFileWriter::addFeature(QgsFeature& feature)
OGR_F_SetFieldString(poFeature, ogrField, mCodec->fromUnicode(attrValue.toString()).data());
break;
default:
//assert(0 && "invalid variant type");
QgsDebugMsg("Invalid variant type for field "+QString::number(ogrField)+": "+QString::number(attrValue.type()));
return false;
}
}
Expand Down Expand Up @@ -321,43 +321,25 @@ bool QgsVectorFileWriter::deleteShapeFile(QString theFileName)
{
//
// Remove old copies that may be lying around
// TODO: should be case-insensitive
//
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())
bool ok = TRUE;

const char* suffixes[] = { ".shp", ".shx", ".dbf", ".prj", ".qix" };
for (int i = 0; i < sizeof(suffixes) / sizeof(char*); i++)
{
if(!QFile::remove(myFileBase + ".prj"))
QString file = myFileBase + suffixes[i];
QFileInfo myInfo(file);
if (myInfo.exists())
{
qDebug("Removing file failed : " + myFileBase.toLocal8Bit() + ".prj");
return false;
if(!QFile::remove(file))
{
QgsDebugMsg("Removing file failed : " + file);
ok = FALSE;
}
}
}
return true;

return ok;
}

0 comments on commit e32b9bf

Please sign in to comment.