Salome HOME
Merge remote-tracking branch 'origin/BR_DEMO' into BR_2017
[modules/hydro.git] / src / HYDROData / HYDROData_ShapeFile.h
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #ifndef HYDRODATA_SHAPEFILE_H
20 #define HYDRODATA_SHAPEFILE_H
21
22 #include "HYDROData.h"
23
24 #include <NCollection_Sequence.hxx>
25
26 #include <TopTools_SequenceOfShape.hxx>
27
28 #include <QStringList>
29
30 #include <vector>
31
32
33 //extern "C" {
34 #include <shapefil.h> 
35 //};
36
37 class gp_XYZ;
38 class HYDROData_PolylineXY;
39 class HYDROData_Polyline3D;
40 class HYDROData_Document;
41 class TopoDS_Face;
42 class TopoDS_Shape;
43 class HYDROData_Entity;
44 class HYDROData_LandCoverMap;
45
46 class HYDROData_ShapeFile 
47 {
48
49 public:
50
51 enum ShapeType
52 {
53   ShapeType_Polyline,
54   ShapeType_Polygon
55 };
56
57 enum DBF_FieldType
58 {
59   DBF_FieldType_None,
60   DBF_FieldType_String,
61   DBF_FieldType_Integer, 
62   DBF_FieldType_Double,
63   DBF_FieldType_Invalid
64 };
65
66
67 struct DBF_AttrValue
68 {
69   DBF_AttrValue() 
70   {
71     myIntVal = -1;
72     myDoubleVal = -1.0;
73     myIsNull = true;
74     myFieldType = DBF_FieldType_None;
75   }
76   QString myStrVal;
77   int myIntVal;
78   double myDoubleVal;
79   std::string myRawValue;
80   DBF_FieldType myFieldType;
81   bool myIsNull;
82 };
83
84 public:
85   HYDRODATA_EXPORT HYDROData_ShapeFile(  );
86   virtual HYDRODATA_EXPORT ~HYDROData_ShapeFile();
87
88   //Export operation
89   HYDRODATA_EXPORT void Export(Handle(HYDROData_Document) theDocument,
90                                const QString& aFileName,
91                                NCollection_Sequence<Handle(HYDROData_PolylineXY)> aPolyXYSeq,
92                                NCollection_Sequence<Handle(HYDROData_Polyline3D)> aPoly3DSeq,
93                                QStringList& aNonExpList);
94
95   HYDRODATA_EXPORT void Export(Handle(HYDROData_Document) theDocument,
96                                const QString& aFileName,
97                                const Handle(HYDROData_LandCoverMap)& aLCM,
98                                QStringList& aNonExpList,
99                                bool bCheckLinear = true,
100                                bool bUseDiscr = false, 
101                                double theDefl = 0.1);
102
103   int WriteObjectPolyXY(Handle(HYDROData_Document) theDocument, SHPHandle theShpHandle,
104                         Handle(HYDROData_PolylineXY) thePoly );
105
106   int WriteObjectPoly3D(Handle(HYDROData_Document) theDocument, SHPHandle theShpHandle,
107                         Handle(HYDROData_Polyline3D) thePoly );
108
109   int WriteObjectPolygon(Handle(HYDROData_Document) theDocument, SHPHandle theShpHandle,
110                          const TopoDS_Shape& theInputShape, bool bUseDiscr, double theDefl );
111   //Import
112   bool Parse(SHPHandle theHandle, ShapeType theType, int& theShapeTypeOfFile);
113   //Import Landcover
114   void ReadSHPPolygon(Handle(HYDROData_Document) theDocument, SHPObject* anObj, int i, TopoDS_Face& F);
115
116   HYDRODATA_EXPORT int ImportPolygons(Handle(HYDROData_Document) theDocument,
117                                       const QString theFileName,
118                                       QStringList& thePolygonsList, 
119                                       TopTools_SequenceOfShape& theFaces, 
120                                       int& theShapeTypeOfFile);
121
122   HYDRODATA_EXPORT void Free();
123
124   //Import Polyline
125   void ReadSHPPolyXY(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName, 
126                      int theInd, NCollection_Sequence<Handle(HYDROData_Entity)>& theEntities,
127                      bool bReadAsPolylin = false);
128
129   void ReadSHPPoly3D(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName, 
130                      int theInd, NCollection_Sequence<Handle(HYDROData_Entity)>& theEntities);
131
132   HYDRODATA_EXPORT int ImportPolylines(Handle(HYDROData_Document) theDocument, const QString& theFileName, 
133                                        NCollection_Sequence<Handle(HYDROData_Entity)>& theEntities, int& theShapeTypeOfFile);
134
135   HYDRODATA_EXPORT QString GetShapeTypeName(int theType);
136
137   //DBF I/O Methods
138   HYDRODATA_EXPORT bool CheckDBFFileExisting(const QString& theSHPFilePath, QString& thePathToDBFFile);
139   HYDRODATA_EXPORT bool DBF_OpenDBF(const QString& thePathToDBFFile);
140   HYDRODATA_EXPORT int DBF_GetNbFields();
141   HYDRODATA_EXPORT QStringList DBF_GetFieldList();
142   HYDRODATA_EXPORT void DBF_GetFieldTypeList(std::vector<DBF_FieldType>& FTVect);
143   HYDRODATA_EXPORT int DBF_GetNbRecords();
144   HYDRODATA_EXPORT void DBF_CloseDBF();
145   HYDRODATA_EXPORT void DBF_GetAttributeList(int theIndexOfField, std::vector<DBF_AttrValue>& theAttrV);
146   HYDRODATA_EXPORT bool DBF_WriteFieldAndValues(const QString& theFileName, const QString& theFieldName, DBF_FieldType theType, const std::vector<DBF_AttrValue>& theAttrV, bool bUseStrValue);
147
148 private:
149   
150   void ProcessFace(Handle(HYDROData_Document) theDocument,
151                    const TopoDS_Face& theFace, SHPHandle theShpHandle,
152                    bool bUseDiscr, double theDefl);
153
154   bool CheckLinear(const TopoDS_Shape& theInpShape);
155
156   int TryOpenShapeFile(QString theFileName);
157
158   void GetFreeIndices(std::vector<int>& theAllowedIndexes, QString strName, size_t theObjsSize,
159                       QStringList theExistingNames, QString theBaseFileName);
160
161 private:
162   std::vector<SHPObject*> mySHPObjects;
163   SHPHandle myHSHP;
164   DBFHandle     myHDBF;
165
166   friend class test_HYDROData_ShapeFile;
167 };
168
169 #endif