Salome HOME
Merge branch 'BR_LAND_COVER_MAP' of ssh://git.salome-platform.org/modules/hydro into...
[modules/hydro.git] / src / HYDROData / HYDROData_ShapeFile.cxx
index abcf49560bfdaac15e013352b7b6c4c8b6a482fc..357ceedd567be540bd9e3d841d982ea8bab8f91f 100644 (file)
 #include <QColor>
 #include <BRepTopAdaptor_FClass2d.hxx>
 
+#ifdef WIN32
+  #pragma warning( disable: 4996 )
+#endif
+
 HYDROData_ShapeFile::HYDROData_ShapeFile() : myHSHP(NULL)
 { 
 }
@@ -634,4 +638,273 @@ int HYDROData_ShapeFile::TryOpenShapeFile(QString theFileName)
   fclose (pFileSHP);
   fclose (pFileSHX);
   return 0;
+}
+
+
+bool HYDROData_ShapeFile::CheckDBFFileExisting(const QString& theSHPFilePath, QString& thePathToDBFFile)
+{
+  QString aSHPfile = theSHPFilePath.simplified();
+  QString aDBFfile = theSHPFilePath.simplified().replace( ".shp", ".dbf", Qt::CaseInsensitive);
+  FILE* pFileDBF = NULL;
+  pFileDBF = fopen (aDBFfile.toAscii().data(), "r");
+
+  if (pFileDBF == NULL)
+  {
+    return false;
+  }
+  
+  fclose (pFileDBF);
+  thePathToDBFFile = aDBFfile;
+  return true;
+}
+
+
+bool HYDROData_ShapeFile::DBF_OpenDBF(const QString& thePathToDBFFile)
+{
+  myHDBF = DBFOpen( thePathToDBFFile.toAscii().data(), "r" );
+  if(myHDBF != NULL)
+    return true;
+  else
+    return false;
+}
+
+int HYDROData_ShapeFile::DBF_GetNbFields()
+{
+  if(myHDBF == NULL)
+    return 0;
+  return DBFGetFieldCount(myHDBF);
+}
+
+void HYDROData_ShapeFile::DBF_CloseDBF()
+{
+  if(myHDBF != NULL)
+     DBFClose( myHDBF );
+}
+
+QStringList HYDROData_ShapeFile::DBF_GetFieldList()
+{
+  QStringList FieldList;
+  int  nWidth, nDecimals;
+  char chField[12];
+
+  for( int i = 0; i < DBFGetFieldCount(myHDBF); i++ )
+  {
+      DBFFieldType eType;
+      eType = DBFGetFieldInfo( myHDBF, i, chField, &nWidth, &nDecimals );
+      FieldList.append(QString(chField));
+  }
+
+  return FieldList;
+}
+
+void HYDROData_ShapeFile::DBF_GetFieldTypeList(std::vector<DBF_FieldType>& FTVect)
+{
+  int  nWidth, nDecimals;
+  char chField[12];
+  DBF_FieldType FT;
+  for( int i = 0; i < DBFGetFieldCount(myHDBF); i++ )
+  {
+    DBFFieldType eType;
+    eType = DBFGetFieldInfo( myHDBF, i, chField, &nWidth, &nDecimals );
+    if( eType == FTString )
+      FT = DBF_FieldType_String;
+    else if( eType == FTInteger )
+      FT = DBF_FieldType_Integer;
+    else if( eType == FTDouble )
+      FT = DBF_FieldType_Double;
+    else if( eType == FTInvalid )
+      FT = DBF_FieldType_Invalid;
+
+    FTVect.push_back(FT);
+  }
+
+}
+
+int HYDROData_ShapeFile::DBF_GetNbRecords()
+{
+  if(myHDBF == NULL)
+    return 0;
+  return DBFGetRecordCount(myHDBF);
+}
+
+void HYDROData_ShapeFile::DBF_GetAttributeList(int theIndexOfField, std::vector<DBF_AttrValue>& theAttrV)
+{
+  int nWidth, nDecimals;
+  char chField[12];
+  
+  for( int i = 0; i < DBFGetRecordCount(myHDBF); i++ )
+  {      
+    DBFFieldType eType;
+    DBF_AttrValue anAttr;
+    eType = DBFGetFieldInfo( myHDBF, theIndexOfField, chField, &nWidth, &nDecimals );
+
+    if( DBFIsAttributeNULL( myHDBF, i, theIndexOfField ) )
+    {
+      anAttr.myIsNull = true;
+      DBF_FieldType FT = DBF_FieldType_None;
+      if( eType == FTString )
+        FT = DBF_FieldType_String;
+      else if( eType == FTInteger )
+        FT = DBF_FieldType_Integer;
+      else if( eType == FTDouble )
+        FT = DBF_FieldType_Double;
+      else if( eType == FTInvalid )
+        FT = DBF_FieldType_Invalid;
+      anAttr.myFieldType = FT;
+    }
+    else
+    {
+      switch( eType )
+      {
+        case FTString:
+        {
+          const char* chAttr = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
+          anAttr.myIsNull = false;
+          anAttr.myFieldType = DBF_FieldType_String;
+          anAttr.myRawValue = chAttr;
+          anAttr.myStrVal = QString(chAttr);
+          break;
+        }
+       
+        case FTInteger:
+        {
+          int iAttr = DBFReadIntegerAttribute( myHDBF, i, theIndexOfField );
+          anAttr.myIsNull = false;
+          anAttr.myFieldType = DBF_FieldType_Integer;
+          anAttr.myRawValue = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
+          anAttr.myIntVal = iAttr;
+          break;
+        }
+          
+        case FTDouble:
+        {
+          double dAttr = DBFReadDoubleAttribute( myHDBF, i, theIndexOfField );
+          anAttr.myIsNull = false;
+          anAttr.myFieldType = DBF_FieldType_Double;
+          anAttr.myRawValue = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
+          anAttr.myDoubleVal = dAttr;
+          break;
+        }
+        default:
+          break;
+      }
+    }
+    theAttrV.push_back(anAttr);
+  }
+
+}
+
+bool HYDROData_ShapeFile::DBF_WriteFieldAndValues(const QString& theFileName, const QString& theFieldName, DBF_FieldType theType, const std::vector<DBF_AttrValue>& theAttrV, bool bUseRawValue)
+{
+  // Check that given field type is equal to field types of attributes values
+  for (size_t i = 0; i < theAttrV.size(); i++)
+  {
+    if (theAttrV[i].myFieldType != theType)
+      return false;
+  }
+
+  DBFHandle    hDBF;
+  hDBF = DBFCreate( theFileName.toStdString().c_str() );
+  if( hDBF == NULL )
+         return false;
+  
+  if (theType != DBF_FieldType_String && theType != DBF_FieldType_Integer && theType != DBF_FieldType_Double) 
+  {
+    DBFClose( hDBF );
+    return false; //cant handle any other cases
+  }
+
+  int nWidth = 20;
+  switch( theType )
+  {
+    case DBF_FieldType_String:
+    {
+      DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTString, nWidth, 0);
+      break;
+    }
+   
+    case DBF_FieldType_Integer:
+    {
+      DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTInteger, nWidth, 0);
+      break;
+    }
+      
+    case DBF_FieldType_Double:
+    {
+      DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTDouble, nWidth, 0);
+      break;
+    }
+    default:
+      break;
+  }
+
+  if (DBFGetFieldCount( hDBF ) != 1) 
+  {
+    DBFClose( hDBF );
+    return false;
+  }
+  if (DBFGetRecordCount( hDBF ) != 0)
+  {
+    DBFClose( hDBF );
+    return false;
+  }
+  int stat = -1;
+
+  if (bUseRawValue)
+  {
+    for (size_t i = 0; i < theAttrV.size(); i++)
+    {
+      if (!theAttrV[i].myIsNull)
+        stat = DBFWriteStringAttribute(hDBF, (int)i, 0, theAttrV[i].myRawValue.c_str());
+      else
+        stat = DBFWriteNULLAttribute(hDBF, (int)i, 0 );
+
+      if (stat != 1)
+      {
+        DBFClose( hDBF );
+        return false;
+      }
+    }
+  }
+  else
+  {
+    for (size_t i = 0; i < theAttrV.size(); i++)
+    {
+      if (!theAttrV[i].myIsNull)
+        switch( theType )
+        {
+          case DBF_FieldType_String:
+          {
+            stat = DBFWriteStringAttribute(hDBF, (int)i, 0, theAttrV[i].myStrVal.toStdString().c_str());
+            break;
+          }
+         
+          case DBF_FieldType_Integer:
+          {
+            stat = DBFWriteIntegerAttribute(hDBF, (int)i, 0, theAttrV[i].myIntVal);
+            break;
+          }
+            
+          case DBF_FieldType_Double:
+          {
+            stat = DBFWriteDoubleAttribute(hDBF, (int)i, 0, theAttrV[i].myDoubleVal);
+            break;
+          }
+          default:
+            break;
+        }
+      else
+        stat = DBFWriteNULLAttribute(hDBF, (int)i, 0 );
+
+      if (stat != 1)
+      {
+        DBFClose( hDBF );
+        return false;
+      }
+    }
+  }
+
+  DBFClose( hDBF );
+  return true;
+
 }
\ No newline at end of file