+}
+
+
+void HYDROData_ShapeFile::ReadSHPPolyXY(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName,
+ int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
+{
+
+ Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
+
+ int nParts = anObj->nParts;
+ for ( int i = 0 ; i < nParts ; i++ )
+ {
+ int StartIndex = anObj->panPartStart[i];
+ int EndIndex;
+ if (i != nParts - 1)
+ EndIndex = anObj->panPartStart[i + 1];
+ else
+ EndIndex = anObj->nVertices;
+
+ bool IsClosed = false;
+ HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
+ if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
+ anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] )
+ {
+ IsClosed = true;
+ aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true);
+ }
+ else
+ aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false);
+
+ if (IsClosed)
+ EndIndex--;
+ for ( int k = StartIndex; k < EndIndex ; k++ )
+ {
+ HYDROData_PolylineXY::Point aSectPoint;
+ aSectPoint.SetX( anObj->padfX[k] );
+ aSectPoint.SetY( anObj->padfY[k] );
+ aPolylineXY->AddPoint( i, aSectPoint );
+ }
+
+ }
+
+ aPolylineXY->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
+ aPolylineXY->SetName( theFileName + "_PolyXY_" + QString::number(theInd) );
+
+ aPolylineXY->Update();
+ theEntities.Append(aPolylineXY);
+
+}
+
+void HYDROData_ShapeFile::ReadSHPPoly3D(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName,
+ int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
+{
+ Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
+
+ Handle(HYDROData_Polyline3D) aPolylineObj = Handle(HYDROData_Polyline3D)::DownCast( theDocument->CreateObject( KIND_POLYLINE ) );
+
+ Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( theDocument->CreateObject( KIND_BATHYMETRY ) );
+ HYDROData_Bathymetry::AltitudePoints aAPoints;
+
+ int nParts = anObj->nParts;
+ for ( int i = 0 ; i < nParts ; i++ )
+ {
+ //bool aSectClosure = true;
+ int StartIndex = anObj->panPartStart[i];
+ int EndIndex;
+ if (i != nParts - 1)
+ EndIndex = anObj->panPartStart[i + 1];
+ else
+ EndIndex = anObj->nVertices;
+
+ bool IsClosed = false;
+ HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
+ if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
+ anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] &&
+ anObj->padfZ[StartIndex] == anObj->padfZ[EndIndex - 1])
+ {
+ IsClosed = true;
+ aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true );
+ }
+ else
+ aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false );
+
+ if (IsClosed)
+ EndIndex--;
+ for ( int k = StartIndex ; k < EndIndex ; k++ )
+ {
+ HYDROData_PolylineXY::Point aSectPoint;
+ aSectPoint.SetX( anObj->padfX[k] );
+ aSectPoint.SetY( anObj->padfY[k] );
+ aPolylineXY->AddPoint( i, aSectPoint );
+ aAPoints.Append(gp_XYZ (anObj->padfX[k], anObj->padfY[k], anObj->padfZ[k]));
+ }
+ }
+
+
+ QString aBathName = theFileName + "_bath_" + QString::number(theInd);
+ QString aPolyXYName = theFileName + "_polyXY_" + QString::number(theInd);
+ QString aPoly3DName = theFileName + "_poly3D_" + QString::number(theInd);
+
+ aPolylineXY->SetName( aPolyXYName );
+ aPolylineXY->SetWireColor(HYDROData_PolylineXY::DefaultWireColor());
+ aPolylineXY->Update();
+
+ aBath->SetAltitudePoints(aAPoints);
+ aBath->SetName( aBathName );
+
+ aPolylineObj->SetPolylineXY (aPolylineXY, false);
+ aPolylineObj->SetAltitudeObject(aBath);
+
+ aPolylineObj->SetBorderColor( aPolylineObj->DefaultBorderColor() );
+ aPolylineObj->SetName( aPoly3DName );
+
+ aPolylineObj->Update();
+ theEntities.Append(aPolylineXY);
+ theEntities.Append(aPolylineObj);
+
+}
+
+
+
+int HYDROData_ShapeFile::ImportPolylines(Handle(HYDROData_Document) theDocument, const QString& theFileName,
+ NCollection_Sequence<Handle_HYDROData_Entity>& theEntities, int& theShapeTypeOfFile)
+{
+ //Free();
+ int aStat = TryOpenShapeFile(theFileName);
+ if (aStat != 0)
+ return aStat;
+
+ HYDROData_Iterator anIter( theDocument );
+ int anInd = 0;
+ QStringList anExistingNames;
+ std::vector<int> anAllowedIndexes;
+ for( ; anIter.More(); anIter.Next() )
+ anExistingNames.push_back(anIter.Current()->GetName());
+
+ SHPHandle aHSHP;
+ aHSHP = SHPOpen( theFileName.toAscii().data(), "rb" );
+
+ QFileInfo aFileInfo(theFileName);
+ QString aBaseFileName = aFileInfo.baseName();
+
+ if (!Parse(aHSHP, HYDROData_ShapeFile::ShapeType_Polyline, theShapeTypeOfFile))
+ return 0;
+ if (aHSHP->nShapeType == 3 || aHSHP->nShapeType == 23)
+ {
+ anInd = 0;
+ for (;anAllowedIndexes.size() < mySHPObjects.size();)
+ {
+ if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)))
+ {
+ anAllowedIndexes.push_back(anInd);
+ anInd++;
+ }
+ else
+ anInd++;
+ }
+
+ for (size_t i = 0; i < mySHPObjects.size(); i++ )
+ {
+ ReadSHPPolyXY(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
+ }
+ aStat = 1;
+ }
+ else if (aHSHP->nShapeType == 13)
+ {
+ anInd = 0;
+ for (;anAllowedIndexes.size() < mySHPObjects.size();)
+ {
+ if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)) &&
+ !anExistingNames.contains(aBaseFileName + "_Poly3D_" + QString::number(anInd)) &&
+ !anExistingNames.contains(aBaseFileName + "_Bath_" + QString::number(anInd)))
+ {
+ anAllowedIndexes.push_back(anInd);
+ anInd++;
+ }
+ else
+ anInd++;
+ }
+ for (size_t i = 0; i < mySHPObjects.size(); i++ )
+ ReadSHPPoly3D(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
+ aStat = 1;
+ }
+ else
+ {
+ aStat = 0;
+ }
+
+ for (size_t i = 0; i < mySHPObjects.size(); i++ )
+ free (mySHPObjects[i]);
+
+ mySHPObjects.clear();
+ SHPClose(aHSHP);
+ return aStat;
+}
+
+QString HYDROData_ShapeFile::GetShapeTypeName(int theType)
+{
+ switch (theType)
+ {
+ case 0:
+ return "null shape";
+ case 1:
+ return "point (unsupported by HYDRO)";
+ case 3:
+ return "arc/polyline (supported by HYDRO)";
+ case 5:
+ return "polygon (supported by HYDRO)";
+ case 8:
+ return "multipoint (unsupported by HYDRO)";
+ case 11:
+ return "pointZ (unsupported by HYDRO)";
+ case 13:
+ return "arcZ/polyline (supported by HYDRO)";
+ case 15:
+ return "polygonZ (unsupported by HYDRO)";
+ case 18:
+ return "multipointZ (unsupported by HYDRO)";
+ case 21:
+ return "pointM (unsupported by HYDRO)";
+ case 23:
+ return "arcM/polyline (supported by HYDRO)";
+ case 25:
+ return "polygonM (unsupported by HYDRO)";
+ case 28:
+ return "multipointM (unsupported by HYDRO)";
+ case 31:
+ return "multipatch (unsupported by HYDRO)";
+ default:
+ return "unknown";
+ }
+}
+
+int HYDROData_ShapeFile::TryOpenShapeFile(QString theFileName)
+{
+ QString aSHPfile = theFileName.simplified();
+ QString aSHXfile = theFileName.simplified().replace( ".shp", ".shx", Qt::CaseInsensitive);
+ FILE* pFileSHP = NULL;
+ pFileSHP = fopen (aSHPfile.toAscii().data(), "r");
+ FILE* pFileSHX = NULL;
+ pFileSHX = fopen (aSHXfile.toAscii().data(), "r");
+
+ if (pFileSHP == NULL || pFileSHX == NULL)
+ {
+ if (pFileSHP == NULL)
+ return -1;
+ if (pFileSHX == NULL)
+ return -2;
+ }
+
+ 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;
+