]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/HYDROData_ShapeFile.cxx
Salome HOME
42b452b7bd386c6b8e01c176148a10882e2485f7
[modules/hydro.git] / src / HYDROData / HYDROData_ShapeFile.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <HYDROData_ShapeFile.h>
24 #include <HYDROData_PolylineXY.h>
25 #include <HYDROData_Polyline3D.h>
26 #include <HYDROData_Bathymetry.h>
27 #include <HYDROData_LandCover.h>
28 #include <HYDROData_Profile.h>
29 #include <HYDROData_Iterator.h>
30
31 #include <QFile>
32 #include <QFileInfo>
33 #include <TopoDS.hxx>
34 #include <TopExp_Explorer.hxx>
35 #include <TopoDS_Wire.hxx>
36 #include <TopoDS_Vertex.hxx>
37 #include <TopoDS_Edge.hxx>
38 #include <TopoDS_Face.hxx>
39 #include <BRep_Tool.hxx>
40 #include <Precision.hxx>
41 #include <Handle_Geom_Curve.hxx>
42 #include <Handle_Geom_Line.hxx>
43 #include <Handle_Geom_TrimmedCurve.hxx>
44 #include <Geom_TrimmedCurve.hxx>
45 #include <BRepBuilderAPI_MakeEdge.hxx>
46 #include <BRepBuilderAPI_MakeWire.hxx>
47 #include <BRepBuilderAPI_MakeFace.hxx>
48 #include <gp_Pln.hxx>
49 #include <BRepLib.hxx>
50 #include <ShapeFix_Shape.hxx>
51 #include <TopTools_SequenceOfShape.hxx>
52 #include <QColor>
53
54
55 HYDROData_ShapeFile::HYDROData_ShapeFile() : myHSHP(NULL)
56
57 }
58
59 HYDROData_ShapeFile::~HYDROData_ShapeFile()
60 {
61   Free();
62 }
63
64 void HYDROData_ShapeFile::Export(const QString& aFileName, 
65   NCollection_Sequence<Handle_HYDROData_PolylineXY> aPolyXYSeq,
66   NCollection_Sequence<Handle_HYDROData_Polyline3D> aPoly3DSeq,
67   NCollection_Sequence<Handle_HYDROData_LandCover> aLCSeq,
68   QStringList& aNonExpList)
69 {
70   SHPHandle hSHPHandle;
71   if (!aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty())
72   {
73     hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARC );        
74     for (int i = 1; i <= aPolyXYSeq.Size(); i++)
75       if (WriteObjectPolyXY(hSHPHandle, aPolyXYSeq(i)) != 1)
76         aNonExpList.append(aPolyXYSeq(i)->GetName());
77
78   }
79   else if (aPolyXYSeq.IsEmpty() && !aPoly3DSeq.IsEmpty())
80   {
81     hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARCZ );
82     for (int i = 1; i <= aPoly3DSeq.Size(); i++)
83       if (WriteObjectPoly3D(hSHPHandle, aPoly3DSeq(i)) != 1)
84         aNonExpList.append(aPoly3DSeq(i)->GetName());
85   }
86   else if (aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty() && !aLCSeq.IsEmpty())
87   {
88     hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_POLYGON );
89     for (int i = 1; i <= aLCSeq.Size(); i++)
90       if (WriteObjectLC(hSHPHandle, aLCSeq(i)) != 1)
91         aNonExpList.append(aLCSeq(i)->GetName());
92   }
93   SHPClose( hSHPHandle );
94
95 }
96
97 int HYDROData_ShapeFile::WriteObjectPolyXY(SHPHandle theShpHandle, Handle_HYDROData_PolylineXY thePoly )
98 {
99   SHPObject     *aSHPObj;
100   std::vector<double> x, y;
101   std::vector<int> anPartStart;
102   
103   for (int i = 0; i < thePoly->NbSections(); i++)    
104     if (thePoly->GetSectionType(i) == HYDROData_IPolyline::SECTION_SPLINE)
105       return -1;
106
107   for (int i = 0; i < thePoly->NbSections(); i++)
108   {
109     anPartStart.push_back(x.size());
110     HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPoints(i);
111     for (int j = 1; j <= aPointList.Size(); j++)
112     {
113       x.push_back( aPointList(j).X());
114       y.push_back( aPointList(j).Y()); 
115     }
116     if (thePoly->IsClosedSection(i))
117     {
118       x.push_back( aPointList(1).X());
119       y.push_back( aPointList(1).Y()); 
120     }
121   }
122     
123   aSHPObj = SHPCreateObject( SHPT_ARC, -1, thePoly->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
124   SHPWriteObject( theShpHandle, -1, aSHPObj );
125   SHPDestroyObject( aSHPObj );
126   return 1;
127 }
128
129 int HYDROData_ShapeFile::WriteObjectPoly3D(SHPHandle theShpHandle, Handle_HYDROData_Polyline3D thePoly )
130 {
131   SHPObject     *aSHPObj;
132   std::vector<double> x, y, z;
133   std::vector<int> anPartStart;
134
135   for (int i = 0; i < thePoly->GetPolylineXY()->NbSections(); i++)    
136     if (thePoly->GetPolylineXY()->GetSectionType(i) == HYDROData_IPolyline::SECTION_SPLINE)
137       return -1;
138   
139   for (int i = 0; i < thePoly->GetPolylineXY()->NbSections(); i++)
140   {
141     anPartStart.push_back(x.size());
142     HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPolylineXY()->GetPoints(i);
143     for (int j = 1; j <= aPointList.Size(); j++)
144     {
145       x.push_back( aPointList(j).X());
146       y.push_back( aPointList(j).Y()); 
147       z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(j).X(), aPointList(j).Y())));
148     }
149     if ( thePoly->GetPolylineXY()->IsClosedSection(i))
150     {
151       x.push_back( aPointList(1).X());
152       y.push_back( aPointList(1).Y()); 
153       z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(1).X(), aPointList(1).Y())));
154
155     }
156   }
157   
158   aSHPObj = SHPCreateObject( SHPT_ARCZ, -1, thePoly->GetPolylineXY()->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], &z[0], NULL );
159   SHPWriteObject( theShpHandle, -1, aSHPObj );
160   SHPDestroyObject( aSHPObj );
161   return 1;
162 }
163
164 int HYDROData_ShapeFile::WriteObjectLC(SHPHandle theShpHandle, Handle_HYDROData_LandCover theLC )
165 {
166   TopoDS_Shape aSh = theLC->GetShape();
167   if (aSh.IsNull())
168     return 0;
169   TopExp_Explorer anEdgeEx(aSh, TopAbs_EDGE);
170   for (; anEdgeEx.More(); anEdgeEx.Next()) 
171   {
172     TopoDS_Edge E = TopoDS::Edge(anEdgeEx.Current());
173     double aFP, aLP;
174     Handle_Geom_Curve aCur = BRep_Tool::Curve(E, aFP, aLP);
175     Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCur);
176     if (aLine.IsNull())
177     {
178       Handle(Geom_TrimmedCurve) aTC = Handle(Geom_TrimmedCurve)::DownCast(aCur);
179       if (!aTC.IsNull())
180       {
181         Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aTC->BasisCurve());
182         if (aLine.IsNull())
183           return -1;
184       }
185       else
186         return -1;
187     }
188
189   }
190   if (aSh.ShapeType() == TopAbs_FACE)
191   {
192     ProcessFace(TopoDS::Face(aSh), theShpHandle);
193   }
194   else if (aSh.ShapeType() == TopAbs_COMPOUND)
195   {
196     TopExp_Explorer Ex(aSh, TopAbs_FACE);
197     for (; Ex.More(); Ex.Next()) 
198     {
199       TopoDS_Face aF = TopoDS::Face(Ex.Current());   
200       if (aF.IsNull())
201         continue;
202       ProcessFace(aF, theShpHandle);
203     }
204   }
205   else
206     return 0;
207
208   return 1;
209  
210 }
211
212 void HYDROData_ShapeFile::ProcessFace(TopoDS_Face theFace, SHPHandle theShpHandle)
213 {
214   SHPObject     *aSHPObj;
215   std::vector<double> x, y;
216   std::vector<int> anPartStart;
217   if (theFace.ShapeType() == TopAbs_FACE)
218   {
219     TopExp_Explorer Ex(theFace, TopAbs_WIRE);
220     int NbWires = 0;
221     for (; Ex.More(); Ex.Next()) 
222     {
223       TopoDS_Wire aW = TopoDS::Wire(Ex.Current());   
224       if (aW.IsNull())
225         continue;
226       NbWires++;
227       anPartStart.push_back(x.size());
228       TopExp_Explorer aVEx(aW, TopAbs_VERTEX);
229       NCollection_Sequence<gp_Pnt2d> aPnts;
230       for (; aVEx.More(); aVEx.Next())
231       {
232         TopoDS_Vertex aV = TopoDS::Vertex(aVEx.Current()); 
233         if (aV.IsNull())
234           continue;
235         gp_Pnt P = BRep_Tool::Pnt(aV);
236         aPnts.Append(gp_Pnt2d(P.X(), P.Y()));
237       }
238       NCollection_Sequence<gp_Pnt2d> aNPnts;
239       aNPnts.Append(aPnts.First());
240       for (int j = 1; j <= aPnts.Size() - 1; j++)
241       {
242         if (!aPnts(j).IsEqual(aPnts(j + 1), Precision::Confusion())) 
243           aNPnts.Append(aPnts(j + 1));
244       }
245
246       for (int j = 1; j <= aNPnts.Size(); j++)
247       { 
248         x.push_back( aNPnts(j).X());
249         y.push_back( aNPnts(j).Y()); 
250       }
251       //x.push_back( aNPnts(1).X());
252       //y.push_back( aNPnts(1).Y()); 
253
254     }
255     
256     aSHPObj = SHPCreateObject( SHPT_POLYGON, -1, NbWires, &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
257     SHPWriteObject( theShpHandle, -1, aSHPObj );
258     SHPDestroyObject( aSHPObj );
259   }
260   else
261     return;
262 }
263
264 void HYDROData_ShapeFile::Parse1(SHPHandle theHandle)
265 {
266   int aShapeType;
267   mySHPObjects.clear();
268   SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL );
269   if (aShapeType == 5) 
270   {
271     for (int i = 0; i < theHandle->nRecords; i++) 
272       mySHPObjects.push_back(SHPReadObject(theHandle, i));
273   }
274 }
275
276 void HYDROData_ShapeFile::ProcessSHP(SHPObject* anObj, int i, TopoDS_Face& F)
277 {
278   TopoDS_Wire W;
279   TopoDS_Edge E; 
280   int nParts = anObj->nParts;
281   gp_Pln pln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
282   BRepBuilderAPI_MakeFace aFBuilder(pln);
283
284   //Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
285   //sfs->FixFaceTool()->FixOrientationMode() = 1;
286   
287   for ( int i = 0 ; i < nParts ; i++ )
288   { 
289     BRepBuilderAPI_MakeWire aBuilder;
290     int StartIndex = anObj->panPartStart[i];
291     int EndIndex;
292     if (i != nParts - 1)
293       EndIndex = anObj->panPartStart[i + 1];
294     else
295       EndIndex = anObj->nVertices;
296
297     for ( int k = StartIndex; k < EndIndex - 1  ; k++ )
298     {
299       gp_Pnt P1 (anObj->padfX[k], anObj->padfY[k], 0);
300       gp_Pnt P2 (anObj->padfX[k+1], anObj->padfY[k+1], 0);
301       if (P1.Distance(P2) < Precision::Confusion())
302         continue;
303       BRepBuilderAPI_MakeEdge aMakeEdge(P1, P2);
304       aBuilder.Add(TopoDS::Edge(aMakeEdge.Shape()));
305     }
306     
307     aBuilder.Build();
308     W = TopoDS::Wire(aBuilder.Shape());
309     W.Reverse();
310     aFBuilder.Add(W);
311   }
312
313   aFBuilder.Build();
314   TopoDS_Face DF = aFBuilder.Face();
315   BRepLib::BuildCurves3d(DF);  
316   bool IsInf = DF.Infinite();
317   if(!DF.IsNull()) 
318   {
319     //sfs->Init ( DF );
320     //sfs->Perform();
321     F = DF; //TopoDS::Face(sfs->Shape());
322   }
323 }
324
325 bool HYDROData_ShapeFile::ImportLandCovers(const QString theFileName, QStringList& thePolygonsList, TopTools_SequenceOfShape& theFaces)
326 {
327   Free();
328   myHSHP = SHPOpen( theFileName.toAscii().data(), "rb" );
329   Parse1(myHSHP);
330   for (int i = 0; i < mySHPObjects.size(); i++)
331     thePolygonsList.append("polygon_" + QString::number(i + 1));
332    
333   TopoDS_Face aF;
334   if (myHSHP->nShapeType == 5)
335   {
336     for (int i = 0; i < mySHPObjects.size(); i++) 
337     {
338        ProcessSHP(mySHPObjects[i], i, aF);
339        theFaces.Append(aF);
340     }
341     return true;
342   }
343   else
344     return false;
345 }
346
347 void HYDROData_ShapeFile::Free()
348 {  
349   for (size_t i = 0; i < mySHPObjects.size(); i++ )
350     free (mySHPObjects[i]);
351
352   mySHPObjects.clear();
353   if (myHSHP != NULL)
354   {
355     SHPClose(myHSHP);
356     myHSHP = NULL; 
357   }
358 }
359
360
361 void HYDROData_ShapeFile::ProcessSHPPolyXY(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName, 
362   int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
363 {
364
365   Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
366   
367   int nParts = anObj->nParts;
368   for ( int i = 0 ; i < nParts ; i++ )
369   {
370     int StartIndex = anObj->panPartStart[i];
371     int EndIndex;
372     if (i != nParts - 1)
373       EndIndex = anObj->panPartStart[i + 1];
374     else
375       EndIndex = anObj->nVertices;
376
377     bool IsClosed = false;
378     HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE; 
379     if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
380         anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] )
381     {
382       IsClosed = true;
383       aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true);
384     }
385     else
386       aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false);
387     
388     if (IsClosed)
389       EndIndex--;
390     for ( int k = StartIndex; k < EndIndex ; k++ )
391     {
392       HYDROData_PolylineXY::Point aSectPoint;
393       aSectPoint.SetX( anObj->padfX[k] );
394       aSectPoint.SetY( anObj->padfY[k] );
395       aPolylineXY->AddPoint( i, aSectPoint );
396     }
397
398   }
399   
400   aPolylineXY->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
401   aPolylineXY->SetName( theFileName + "_PolyXY_" + QString::number(theInd) );
402   
403   aPolylineXY->Update();
404   theEntities.Append(aPolylineXY);
405
406 }
407
408 void HYDROData_ShapeFile::ProcessSHPPoly3D(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName, 
409   int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
410 {
411   Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
412
413   Handle(HYDROData_Polyline3D) aPolylineObj = Handle(HYDROData_Polyline3D)::DownCast( theDocument->CreateObject( KIND_POLYLINE ) );
414
415   Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( theDocument->CreateObject( KIND_BATHYMETRY ) );
416   HYDROData_Bathymetry::AltitudePoints aAPoints;
417
418   int nParts = anObj->nParts;
419   for ( int i = 0 ; i < nParts ; i++ )
420   {
421     //bool aSectClosure = true;
422     int StartIndex = anObj->panPartStart[i];
423     int EndIndex;
424     if (i != nParts - 1)
425       EndIndex = anObj->panPartStart[i + 1];
426     else
427       EndIndex = anObj->nVertices;
428
429     bool IsClosed = false;
430     HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE; 
431     if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
432         anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] &&
433         anObj->padfZ[StartIndex] == anObj->padfZ[EndIndex - 1])
434     {
435       IsClosed = true;
436       aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true );
437     }
438     else
439       aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false  );
440     
441     if (IsClosed)
442       EndIndex--;
443     for ( int k = StartIndex ; k < EndIndex ; k++ )
444     {
445       HYDROData_PolylineXY::Point aSectPoint;
446       aSectPoint.SetX( anObj->padfX[k] );
447       aSectPoint.SetY( anObj->padfY[k] );
448       aPolylineXY->AddPoint( i, aSectPoint );
449       aAPoints.Append(gp_XYZ (anObj->padfX[k], anObj->padfY[k], anObj->padfZ[k]));
450     }
451   }
452
453
454   QString aBathName = theFileName + "_bath_" + QString::number(theInd);
455   QString aPolyXYName = theFileName + "_polyXY_" + QString::number(theInd);
456   QString aPoly3DName = theFileName + "_poly3D_" + QString::number(theInd);
457
458   aPolylineXY->SetName( aPolyXYName );
459   aPolylineXY->SetWireColor(HYDROData_PolylineXY::DefaultWireColor());
460   aPolylineXY->Update();
461   
462   aBath->SetAltitudePoints(aAPoints);
463   aBath->SetName( aBathName );
464
465   aPolylineObj->SetPolylineXY (aPolylineXY, false);
466   aPolylineObj->SetAltitudeObject(aBath);
467
468   aPolylineObj->SetBorderColor( HYDROData_Polyline3D::DefaultBorderColor() );
469   aPolylineObj->SetName( aPoly3DName );
470   
471   aPolylineObj->Update();
472   theEntities.Append(aPolylineXY);
473   theEntities.Append(aPolylineObj);
474
475 }
476
477 void HYDROData_ShapeFile::Parse2(SHPHandle theHandle)
478 {
479   int aShapeType;
480   mySHPObjects.clear();
481   SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL );
482   if (aShapeType == 3 || aShapeType == 13 || aShapeType == 23) 
483   {
484     for (int i = 0; i < theHandle->nRecords; i++) 
485       mySHPObjects.push_back(SHPReadObject(theHandle, i));
486   }
487 }
488
489 bool HYDROData_ShapeFile::ImportPolylines(Handle(HYDROData_Document) theDocument, const QString& theFileName, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
490 {
491   HYDROData_Iterator anIter( theDocument );
492   int anInd = 0;
493   QStringList anExistingNames;
494   std::vector<int> anAllowedIndexes;
495   for( ; anIter.More(); anIter.Next() )
496     anExistingNames.push_back(anIter.Current()->GetName());
497
498   SHPHandle aHSHP;
499   aHSHP = SHPOpen( theFileName.toAscii().data(), "rb" );
500   
501   QFileInfo aFileInfo(theFileName);
502   QString aBaseFileName = aFileInfo.baseName();
503
504   Parse2(aHSHP);
505   bool aStat = false;
506   if (aHSHP->nShapeType == 3 || aHSHP->nShapeType == 23)
507   {
508     anInd = 0;
509     for (;anAllowedIndexes.size() < mySHPObjects.size();)
510     {
511       if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)))
512       {
513         anAllowedIndexes.push_back(anInd);
514         anInd++;
515       }
516       else
517         anInd++;
518     }
519     
520     for (size_t i = 0; i < mySHPObjects.size(); i++ )
521     {
522       ProcessSHPPolyXY(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
523     }
524     aStat = true;
525   }
526   else if (aHSHP->nShapeType == 13)
527   {
528     anInd = 0;
529     for (;anAllowedIndexes.size() < mySHPObjects.size();)
530     {
531       if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)) &&
532           !anExistingNames.contains(aBaseFileName + "_Poly3D_" + QString::number(anInd)) &&
533           !anExistingNames.contains(aBaseFileName + "_Bath_" + QString::number(anInd)))
534       {
535         anAllowedIndexes.push_back(anInd);
536         anInd++;
537       }
538       else
539         anInd++;
540     }
541     for (size_t i = 0; i < mySHPObjects.size(); i++ )
542       ProcessSHPPoly3D(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
543     aStat = true;
544   }
545   else  
546   {
547     aStat = false;
548   }
549   
550   for (size_t i = 0; i < mySHPObjects.size(); i++ )
551     free (mySHPObjects[i]);
552
553   mySHPObjects.clear();
554   SHPClose(aHSHP);
555   return aStat;
556 }