Salome HOME
5dfc724a7426b80a81cd8822d3372e94520a71b9
[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
30 #include <QFile>
31 #include <QFileInfo>
32 #include <TopoDS.hxx>
33 #include <TopExp_Explorer.hxx>
34 #include <TopoDS_Wire.hxx>
35 #include <TopoDS_Vertex.hxx>
36 #include <TopoDS_Edge.hxx>
37 #include <TopoDS_Face.hxx>
38 #include <BRep_Tool.hxx>
39 #include <Precision.hxx>
40 #include <Handle_Geom_Curve.hxx>
41 #include <Handle_Geom_Line.hxx>
42 #include <Handle_Geom_TrimmedCurve.hxx>
43 #include <Geom_TrimmedCurve.hxx>
44 #include <BRepBuilderAPI_MakeEdge.hxx>
45 #include <BRepBuilderAPI_MakeWire.hxx>
46 #include <BRepBuilderAPI_MakeFace.hxx>
47 #include <gp_Pln.hxx>
48 #include <BRepLib.hxx>
49 #include <ShapeFix_Shape.hxx>
50 #include <TopTools_SequenceOfShape.hxx>
51
52 HYDROData_ShapeFile::HYDROData_ShapeFile() : myHSHP(NULL)
53
54 }
55
56 HYDROData_ShapeFile::~HYDROData_ShapeFile()
57 {
58   Free();
59 }
60
61 void HYDROData_ShapeFile::Export(const QString& aFileName, 
62   NCollection_Sequence<Handle_HYDROData_PolylineXY> aPolyXYSeq,
63   NCollection_Sequence<Handle_HYDROData_Polyline3D> aPoly3DSeq,
64   NCollection_Sequence<Handle_HYDROData_LandCover> aLCSeq,
65   QStringList& aNonExpList)
66 {
67   SHPHandle hSHPHandle;
68   if (!aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty())
69   {
70     hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARC );        
71     for (int i = 1; i <= aPolyXYSeq.Size(); i++)
72       if (WriteObjectPolyXY(hSHPHandle, aPolyXYSeq(i)) != 1)
73         aNonExpList.append(aPolyXYSeq(i)->GetName());
74
75   }
76   else if (aPolyXYSeq.IsEmpty() && !aPoly3DSeq.IsEmpty())
77   {
78     hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARCZ );
79     for (int i = 1; i <= aPoly3DSeq.Size(); i++)
80       if (WriteObjectPoly3D(hSHPHandle, aPoly3DSeq(i)) != 1)
81         aNonExpList.append(aPoly3DSeq(i)->GetName());
82   }
83   else if (aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty() && !aLCSeq.IsEmpty())
84   {
85     hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_POLYGON );
86     for (int i = 1; i <= aLCSeq.Size(); i++)
87       if (WriteObjectLC(hSHPHandle, aLCSeq(i)) != 1)
88         aNonExpList.append(aLCSeq(i)->GetName());
89   }
90   SHPClose( hSHPHandle );
91
92 }
93
94 int HYDROData_ShapeFile::WriteObjectPolyXY(SHPHandle theShpHandle, Handle_HYDROData_PolylineXY thePoly )
95 {
96   SHPObject     *aSHPObj;
97   std::vector<double> x, y;
98   std::vector<int> anPartStart;
99   
100   for (int i = 0; i < thePoly->NbSections(); i++)    
101     if (thePoly->GetSectionType(i) == HYDROData_IPolyline::SECTION_SPLINE)
102       return -1;
103
104   for (int i = 0; i < thePoly->NbSections(); i++)
105   {
106     anPartStart.push_back(x.size());
107     HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPoints(i);
108     for (int j = 1; j <= aPointList.Size(); j++)
109     {
110       x.push_back( aPointList(j).X());
111       y.push_back( aPointList(j).Y()); 
112     }
113     if (thePoly->IsClosedSection(i))
114     {
115       x.push_back( aPointList(1).X());
116       y.push_back( aPointList(1).Y()); 
117     }
118   }
119     
120   aSHPObj = SHPCreateObject( SHPT_ARC, -1, thePoly->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
121   SHPWriteObject( theShpHandle, -1, aSHPObj );
122   SHPDestroyObject( aSHPObj );
123   return 1;
124 }
125
126 int HYDROData_ShapeFile::WriteObjectPoly3D(SHPHandle theShpHandle, Handle_HYDROData_Polyline3D thePoly )
127 {
128   SHPObject     *aSHPObj;
129   std::vector<double> x, y, z;
130   std::vector<int> anPartStart;
131
132   for (int i = 0; i < thePoly->GetPolylineXY()->NbSections(); i++)    
133     if (thePoly->GetPolylineXY()->GetSectionType(i) == HYDROData_IPolyline::SECTION_SPLINE)
134       return -1;
135   
136   for (int i = 0; i < thePoly->GetPolylineXY()->NbSections(); i++)
137   {
138     anPartStart.push_back(x.size());
139     HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPolylineXY()->GetPoints(i);
140     for (int j = 1; j <= aPointList.Size(); j++)
141     {
142       x.push_back( aPointList(j).X());
143       y.push_back( aPointList(j).Y()); 
144       z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(j).X(), aPointList(j).Y())));
145     }
146     if ( thePoly->GetPolylineXY()->IsClosedSection(i))
147     {
148       x.push_back( aPointList(1).X());
149       y.push_back( aPointList(1).Y()); 
150       z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(1).X(), aPointList(1).Y())));
151
152     }
153   }
154   
155   aSHPObj = SHPCreateObject( SHPT_ARCZ, -1, thePoly->GetPolylineXY()->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], &z[0], NULL );
156   SHPWriteObject( theShpHandle, -1, aSHPObj );
157   SHPDestroyObject( aSHPObj );
158   return 1;
159 }
160
161 int HYDROData_ShapeFile::WriteObjectLC(SHPHandle theShpHandle, Handle_HYDROData_LandCover theLC )
162 {
163   TopoDS_Shape aSh = theLC->GetShape();
164   if (aSh.IsNull())
165     return 0;
166   TopExp_Explorer anEdgeEx(aSh, TopAbs_EDGE);
167   for (; anEdgeEx.More(); anEdgeEx.Next()) 
168   {
169     TopoDS_Edge E = TopoDS::Edge(anEdgeEx.Current());
170     double aFP, aLP;
171     Handle_Geom_Curve aCur = BRep_Tool::Curve(E, aFP, aLP);
172     Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCur);
173     if (aLine.IsNull())
174     {
175       Handle(Geom_TrimmedCurve) aTC = Handle(Geom_TrimmedCurve)::DownCast(aCur);
176       if (!aTC.IsNull())
177       {
178         Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aTC->BasisCurve());
179         if (aLine.IsNull())
180           return -1;
181       }
182       else
183         return -1;
184     }
185
186   }
187   if (aSh.ShapeType() == TopAbs_FACE)
188   {
189     ProcessFace(TopoDS::Face(aSh), theShpHandle);
190   }
191   else if (aSh.ShapeType() == TopAbs_COMPOUND)
192   {
193     TopExp_Explorer Ex(aSh, TopAbs_FACE);
194     for (; Ex.More(); Ex.Next()) 
195     {
196       TopoDS_Face aF = TopoDS::Face(Ex.Current());   
197       if (aF.IsNull())
198         continue;
199       ProcessFace(aF, theShpHandle);
200     }
201   }
202   else
203     return 0;
204
205   return 1;
206  
207 }
208
209 void HYDROData_ShapeFile::ProcessFace(TopoDS_Face theFace, SHPHandle theShpHandle)
210 {
211   SHPObject     *aSHPObj;
212   std::vector<double> x, y;
213   std::vector<int> anPartStart;
214   if (theFace.ShapeType() == TopAbs_FACE)
215   {
216     TopExp_Explorer Ex(theFace, TopAbs_WIRE);
217     int NbWires = 0;
218     for (; Ex.More(); Ex.Next()) 
219     {
220       TopoDS_Wire aW = TopoDS::Wire(Ex.Current());   
221       if (aW.IsNull())
222         continue;
223       NbWires++;
224       anPartStart.push_back(x.size());
225       TopExp_Explorer aVEx(aW, TopAbs_VERTEX);
226       NCollection_Sequence<gp_Pnt2d> aPnts;
227       for (; aVEx.More(); aVEx.Next())
228       {
229         TopoDS_Vertex aV = TopoDS::Vertex(aVEx.Current()); 
230         if (aV.IsNull())
231           continue;
232         gp_Pnt P = BRep_Tool::Pnt(aV);
233         aPnts.Append(gp_Pnt2d(P.X(), P.Y()));
234       }
235       NCollection_Sequence<gp_Pnt2d> aNPnts;
236       aNPnts.Append(aPnts.First());
237       for (int j = 1; j <= aPnts.Size() - 1; j++)
238       {
239         if (!aPnts(j).IsEqual(aPnts(j + 1), Precision::Confusion())) 
240           aNPnts.Append(aPnts(j + 1));
241       }
242
243       for (int j = 1; j <= aNPnts.Size(); j++)
244       { 
245         x.push_back( aNPnts(j).X());
246         y.push_back( aNPnts(j).Y()); 
247       }
248       //x.push_back( aNPnts(1).X());
249       //y.push_back( aNPnts(1).Y()); 
250
251     }
252     
253     aSHPObj = SHPCreateObject( SHPT_POLYGON, -1, NbWires, &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
254     SHPWriteObject( theShpHandle, -1, aSHPObj );
255     SHPDestroyObject( aSHPObj );
256   }
257   else
258     return;
259 }
260
261 void HYDROData_ShapeFile::Parse(SHPHandle theHandle)
262 {
263   int aShapeType;
264   mySHPObjects.clear();
265   SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL );
266   if (aShapeType == 5) 
267   {
268     for (int i = 0; i < theHandle->nRecords; i++) 
269       mySHPObjects.push_back(SHPReadObject(theHandle, i));
270   }
271 }
272
273 void HYDROData_ShapeFile::ProcessSHP(SHPObject* anObj, int i, TopoDS_Face& F)
274 {
275   TopoDS_Wire W;
276   TopoDS_Edge E; 
277   int nParts = anObj->nParts;
278   gp_Pln pln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
279   BRepBuilderAPI_MakeFace aFBuilder(pln);
280
281   //Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
282   //sfs->FixFaceTool()->FixOrientationMode() = 1;
283   
284   for ( int i = 0 ; i < nParts ; i++ )
285   { 
286     BRepBuilderAPI_MakeWire aBuilder;
287     int StartIndex = anObj->panPartStart[i];
288     int EndIndex;
289     if (i != nParts - 1)
290       EndIndex = anObj->panPartStart[i + 1];
291     else
292       EndIndex = anObj->nVertices;
293
294     for ( int k = StartIndex; k < EndIndex - 1  ; k++ )
295     {
296       gp_Pnt P1 (anObj->padfX[k], anObj->padfY[k], 0);
297       gp_Pnt P2 (anObj->padfX[k+1], anObj->padfY[k+1], 0);
298       if (P1.Distance(P2) < Precision::Confusion())
299         continue;
300       BRepBuilderAPI_MakeEdge aMakeEdge(P1, P2);
301       aBuilder.Add(TopoDS::Edge(aMakeEdge.Shape()));
302     }
303     
304     aBuilder.Build();
305     W = TopoDS::Wire(aBuilder.Shape());
306     W.Reverse();
307     aFBuilder.Add(W);
308   }
309
310   aFBuilder.Build();
311   TopoDS_Face DF = aFBuilder.Face();
312   BRepLib::BuildCurves3d(DF);  
313   bool IsInf = DF.Infinite();
314   if(!DF.IsNull()) 
315   {
316     //sfs->Init ( DF );
317     //sfs->Perform();
318     F = DF; //TopoDS::Face(sfs->Shape());
319   }
320 }
321
322 bool HYDROData_ShapeFile::ImportLandCovers(const QString theFileName, QStringList& thePolygonsList, TopTools_SequenceOfShape& theFaces)
323 {
324   Free();
325   myHSHP = SHPOpen( theFileName.toAscii().data(), "rb" );
326   Parse(myHSHP);
327   for (int i = 0; i < mySHPObjects.size(); i++)
328     thePolygonsList.append("polygon_" + QString::number(i + 1));
329    
330   TopoDS_Face aF;
331   if (myHSHP->nShapeType == 5)
332   {
333     for (int i = 0; i < mySHPObjects.size(); i++) 
334     {
335        ProcessSHP(mySHPObjects[i], i, aF);
336        theFaces.Append(aF);
337     }
338     return true;
339   }
340   else
341     return false;
342 }
343
344 void HYDROData_ShapeFile::Free()
345 {  
346   for (size_t i = 0; i < mySHPObjects.size(); i++ )
347     free (mySHPObjects[i]);
348
349   mySHPObjects.clear();
350   if (myHSHP != NULL)
351   {
352     SHPClose(myHSHP);
353     myHSHP = NULL; 
354   }
355 }