Umasoft
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
Features.h
1 //
2 // Features.h
3 //
4 // Copyright (c) 2002-2009 Virtual Terrain Project
5 // Free for all uses, see license.txt for details.
6 //
7 
8 #ifndef VTDATA_FEATURES
9 #define VTDATA_FEATURES
10 
11 #include "shapelib/shapefil.h"
12 #include "ogrsf_frmts.h"
13 
14 #include "MathTypes.h"
15 #include "vtString.h"
16 #include "Projections.h"
17 #include "Content.h"
18 
19 enum SelectionType
20 {
21  ST_NORMAL,
22  ST_ADD,
23  ST_SUBTRACT,
24  ST_TOGGLE
25 };
26 
27 enum FieldType
28 {
29  FT_Boolean,
30  FT_Short,
31  FT_Integer,
32  FT_Float,
33  FT_Double,
34  FT_String,
35  FT_Unknown
36 };
37 
44 class Field
45 {
46 public:
47  Field(const char *name, FieldType ftype);
48  ~Field();
49 
50  int AddRecord();
51  void SetNumRecords(int iNum);
52 
53  void SetValue(uint iRecord, const char *string);
54  void SetValue(uint iRecord, int value);
55  void SetValue(uint iRecord, double value);
56  void SetValue(uint iRecord, bool value);
57 
58  void GetValue(uint iRecord, vtString &string);
59  void GetValue(uint iRecord, short &value);
60  void GetValue(uint iRecord, int &value);
61  void GetValue(uint iRecord, float &value);
62  void GetValue(uint iRecord, double &value);
63  void GetValue(uint iRecord, bool &value);
64 
65  void CopyValue(uint FromRecord, int ToRecord);
66  void GetValueAsString(uint iRecord, vtString &str);
67  void SetValueFromString(uint iRecord, const vtString &str);
68  void SetValueFromString(uint iRecord, const char *str);
69 
70  FieldType m_type;
71  int m_width, m_decimals; // these are for remembering SHP limitations
72  vtString m_name;
73 
74  vtArray<bool> m_bool;
75  vtArray<short> m_short;
76  vtArray<int> m_int;
77  vtArray<float> m_float;
78  vtArray<double> m_double;
79  vtStringArray m_string;
80 };
81 
82 // Helpers
83 const char *DescribeFieldType(FieldType type);
84 const char *DescribeFieldType(DBFFieldType type);
85 DBFFieldType ConvertFieldType(FieldType type);
86 FieldType ConvertFieldType(DBFFieldType type);
87 
88 // feature flags (bit flags)
89 #define FF_SELECTED 1
90 #define FF_PICKED 2
91 #define FF_DELETE 4
92 
93 struct vtFeature {
94  uchar flags;
95 };
96 
106 {
107 public:
108  vtFeatureSet();
109  virtual ~vtFeatureSet();
110 
111  // File IO
112  bool SaveToSHP(const char *filename, bool progress_callback(int)=0) const;
113  bool LoadFromOGR(OGRLayer *pLayer, bool progress_callback(int)=0);
114  virtual void LoadGeomFromSHP(SHPHandle hSHP, bool progress_callback(int)=0) = 0;
115  bool LoadFromSHP(const char *fname, bool progress_callback(int)=0);
116  bool LoadDataFromDBF(const char *filename, bool progress_callback(int)=0);
117  bool LoadFieldInfoFromDBF(const char *filename);
118  bool LoadDataFromCSV(const char *filename, bool progress_callback(int)=0);
119  bool SaveToKML(const char *filename, bool progress_callback(int)=0) const;
120 
121  void SetFilename(const vtString &str) { m_strFilename = str; }
122  vtString GetFilename() const { return m_strFilename; }
123 
124  // feature (entity) operations
125  virtual uint GetNumEntities() const = 0;
126  void SetNumEntities(int iNum);
127  void AllocateFeatures();
128  OGRwkbGeometryType GetGeomType() const;
129  void SetGeomType(OGRwkbGeometryType eGeomType);
130  bool AppendDataFrom(vtFeatureSet *pFromSet);
135  virtual void Reserve(int iNum) = 0;
136  virtual bool ComputeExtent(DRECT &rect) const = 0;
137  virtual void Offset(const DPoint2 &p, bool bSelectedOnly = false) = 0;
138  virtual bool TransformCoords(OCT *pTransform, bool progress_callback(int)=0) = 0;
139  virtual bool AppendGeometryFrom(vtFeatureSet *pFromSet) = 0;
140  virtual int NumTotalVertices() const { return 0; }
141 
142  // deletion
143  void SetToDelete(int iFeature);
144  void ApplyDeletion();
145 
146  // selection
147  void Select(uint iEnt, bool set = true)
148  {
149  if (set)
150  m_Features[iEnt]->flags |= FF_SELECTED;
151  else
152  m_Features[iEnt]->flags &= ~FF_SELECTED;
153  }
154  bool IsSelected(uint iEnt)
155  {
156  return ((m_Features[iEnt]->flags & FF_SELECTED) != 0);
157  }
158  uint NumSelected() const;
159  void DeselectAll();
160  void InvertSelection();
161  int SelectByCondition(int iField, int iCondition, const char *szValue);
162  void DeleteSelected();
163  bool IsDeleted(uint iEnt)
164  {
165  return ((m_Features[iEnt]->flags & FF_DELETE) != 0);
166  }
167  int DoBoxSelect(const DRECT &rect, SelectionType st);
168 
169  // picking (alternate form of selection)
170  void Pick(uint iEnt, bool set = true)
171  {
172  if (set)
173  m_Features[iEnt]->flags |= FF_PICKED;
174  else
175  m_Features[iEnt]->flags &= ~FF_PICKED;
176  }
177  bool IsPicked(uint iEnt)
178  {
179  return ((m_Features[iEnt]->flags & FF_PICKED) != 0);
180  }
181  void DePickAll();
182 
183  // attribute (field) operations
184  uint GetNumFields() const { return m_fields.GetSize(); }
185  Field *GetField(int i) { return m_fields.GetAt(i); }
186  const Field *GetField(int i) const { return m_fields.GetAt(i); }
187  Field *GetField(const char *name);
188  int GetFieldIndex(const char *name) const;
189  int AddField(const char *name, FieldType ftype, int string_length = 40);
190  int AddRecord();
191  void DeleteFields();
192 
193  void SetValue(uint record, uint field, const char *string);
194  void SetValue(uint record, uint field, int value);
195  void SetValue(uint record, uint field, double value);
196  void SetValue(uint record, uint field, bool value);
197 
198  void GetValueAsString(uint record, uint field, vtString &str) const;
199  void SetValueFromString(uint iRecord, uint iField, const vtString &str);
200  void SetValueFromString(uint iRecord, uint iField, const char *str);
201 
202  int GetIntegerValue(uint iRecord, uint iField) const;
203  short GetShortValue(uint iRecord, uint iField) const;
204  float GetFloatValue(uint iRecord, uint iField) const;
205  double GetDoubleValue(uint iRecord, uint iField) const;
206  bool GetBoolValue(uint iRecord, uint iField) const;
207 
208  void SetProjection(const vtProjection &proj) { m_proj = proj; }
209  vtProjection &GetAtProjection() { return m_proj; }
210 
211  vtFeature *GetFeature(uint iIndex) const { return m_Features[iIndex]; }
212 
213 protected:
214  // these must be implemented for each type of geometry
215  virtual bool IsInsideRect(int iElem, const DRECT &rect) = 0;
216  virtual void CopyGeometry(uint from, uint to) = 0;
217  virtual void SaveGeomToSHP(SHPHandle hSHP, bool progress_callback(int)=0) const = 0;
218  virtual void SetNumGeometries(int iNum) = 0;
219 
220  void CopyEntity(uint from, uint to);
221  void ParseDBFFields(DBFHandle db);
222  void ParseDBFRecords(DBFHandle db, bool progress_callback(int)=0);
223 
224  OGRwkbGeometryType m_eGeomType;
225 
226  // The size of the features array will match the number of elements
227  std::vector<vtFeature*> m_Features;
228 
229  vtArray<Field*> m_fields;
230  vtProjection m_proj;
231 
232  // remember the filename these feature were loaded from or saved to
233  vtString m_strFilename;
234 };
235 
240 {
241 public:
243 
244  uint GetNumEntities() const;
245  void SetNumGeometries(int iNum);
246  void Reserve(int iNum);
247  bool ComputeExtent(DRECT &rect) const;
248  void Offset(const DPoint2 &p, bool bSelectedOnly = false);
249  bool TransformCoords(OCT *pTransform, bool progress_callback(int)=0);
250  bool AppendGeometryFrom(vtFeatureSet *pFromSet);
251 
252  int AddPoint(const DPoint2 &p);
253  void SetPoint(uint num, const DPoint2 &p);
254  DPoint2 &GetPoint(uint num) { return m_Point2[num]; }
255  const DPoint2 &GetPoint(uint num) const { return m_Point2[num]; }
256 
257  int FindClosestPoint(const DPoint2 &p, double epsilon);
258  void FindAllPointsAtLocation(const DPoint2 &p, vtArray<int> &found);
259  void GetPoint(uint num, DPoint2 &p) const;
260 
261  // implement necessary virtual methods
262  virtual bool IsInsideRect(int iElem, const DRECT &rect);
263  virtual void CopyGeometry(uint from, uint to);
264  virtual void SaveGeomToSHP(SHPHandle hSHP, bool progress_callback(int)=0) const;
265  virtual void LoadGeomFromSHP(SHPHandle hSHP, bool progress_callback(int)=0);
266 
267 protected:
268  DLine2 m_Point2; // wkbPoint
269 };
270 
275 {
276 public:
278 
279  uint GetNumEntities() const;
280  void SetNumGeometries(int iNum);
281  void Reserve(int iNum);
282  bool ComputeExtent(DRECT &rect) const;
283  void Offset(const DPoint2 &p, bool bSelectedOnly = false);
284  bool TransformCoords(OCT *pTransform, bool progress_callback(int)=0);
285  bool AppendGeometryFrom(vtFeatureSet *pFromSet);
286 
287  int AddPoint(const DPoint3 &p);
288  void SetPoint(uint num, const DPoint3 &p);
289  void GetPoint(uint num, DPoint3 &p) const;
290  DPoint3 &GetPoint(uint num) { return m_Point3[num]; }
291  const DPoint3 &GetPoint(uint num) const { return m_Point3[num]; }
292  const DLine3 &GetAllPoints() const { return m_Point3; }
293  bool ComputeHeightRange(float &fmin, float &fmax);
294 
295  // implement necessary virtual methods
296  virtual bool IsInsideRect(int iElem, const DRECT &rect);
297  virtual void CopyGeometry(uint from, uint to);
298  virtual void SaveGeomToSHP(SHPHandle hSHP, bool progress_callback(int)=0) const;
299  virtual void LoadGeomFromSHP(SHPHandle hSHP, bool progress_callback(int)=0);
300 
301 protected:
302  DLine3 m_Point3; // wkbPoint25D
303 };
304 
310 {
311 public:
313 
314  uint GetNumEntities() const;
315  void SetNumGeometries(int iNum);
316  void Reserve(int iNum);
317  bool ComputeExtent(DRECT &rect) const;
318  void Offset(const DPoint2 &p, bool bSelectedOnly = false);
319  bool TransformCoords(OCT *pTransform, bool progress_callback(int)=0);
320  bool AppendGeometryFrom(vtFeatureSet *pFromSet);
321 
322  int AddPolyLine(const DLine2 &pl);
323  const DLine2 &GetPolyLine(uint num) const { return m_Line[num]; }
324  DLine2 &GetPolyLine(uint num) { return m_Line[num]; }
325  int NumTotalVertices() const;
326 
327  // implement necessary virtual methods
328  virtual bool IsInsideRect(int iElem, const DRECT &rect);
329  virtual void CopyGeometry(uint from, uint to);
330  virtual void SaveGeomToSHP(SHPHandle hSHP, bool progress_callback(int)=0) const;
331  virtual void LoadGeomFromSHP(SHPHandle hSHP, bool progress_callback(int)=0);
332 
333 protected:
334  DLine2Array m_Line; // wkbLineString
335 };
336 
342 {
343 public:
345 
346  uint GetNumEntities() const;
347  void SetNumGeometries(int iNum);
348  void Reserve(int iNum);
349  bool ComputeExtent(DRECT &rect) const;
350  void Offset(const DPoint2 &p, bool bSelectedOnly = false);
351  bool TransformCoords(OCT *pTransform, bool progress_callback(int)=0);
352  bool AppendGeometryFrom(vtFeatureSet *pFromSet);
353 
354  int AddPolyLine(const DLine3 &pl);
355  const DLine3 &GetPolyLine(uint num) const { return m_Line[num]; }
356  DLine3 &GetPolyLine(uint num) { return m_Line[num]; }
357  bool ComputeHeightRange(float &fmin, float &fmax);
358  int NumTotalVertices() const;
359  bool FindClosest(const DPoint2 &p, int &close_feature, DPoint3 &close_point);
360 
361  // implement necessary virtual methods
362  virtual bool IsInsideRect(int iElem, const DRECT &rect);
363  virtual void CopyGeometry(uint from, uint to);
364  virtual void SaveGeomToSHP(SHPHandle hSHP, bool progress_callback(int)=0) const;
365  virtual void LoadGeomFromSHP(SHPHandle hSHP, bool progress_callback(int)=0);
366 
367 protected:
368  std::vector<DLine3> m_Line; // wkbLineString25D
369 };
370 
371 
372 typedef std::vector<int> IntVector;
373 typedef IntVector *IntVectorPtr;
374 
375 //
376 // A utility class to speed up polygon testing operations
378 {
379 public:
380  SpatialIndex(int iSize);
381  ~SpatialIndex();
382 
383  void GenerateIndices(const class vtFeatureSetPolygon *feat);
384  const IntVector *GetIndexForPoint(const DPoint2 &p) const;
385  int m_iLastFound;
386 
387 protected:
388  int m_iSize;
389  IntVectorPtr *m_pArray;
390  DPoint2 m_base, m_step;
391  DRECT m_Extent;
392 };
393 
400 {
401 public:
403 
404  uint GetNumEntities() const;
405  void SetNumGeometries(int iNum);
406  void Reserve(int iNum);
407  bool ComputeExtent(DRECT &rect) const;
408  void Offset(const DPoint2 &p, bool bSelectedOnly = false);
409  bool TransformCoords(OCT *pTransform, bool progress_callback(int)=0);
410  bool AppendGeometryFrom(vtFeatureSet *pFromSet);
411 
412  int AddPolygon(const DPolygon2 &poly);
413  void SetPolygon(uint num, const DPolygon2 &poly) { m_Poly[num] = poly; }
414  const DPolygon2 &GetPolygon(uint num) const { return m_Poly[num]; }
415  DPolygon2 &GetPolygon(uint num) { return m_Poly[num]; }
416  int FindSimplePolygon(const DPoint2 &p) const;
417  int FindPolygon(const DPoint2 &p) const;
418 
419  // Try to address some kinds of degenerate geometry that can occur in polygons
420  int FixGeometry(double dEpsilon);
421  int SelectBadFeatures(double dEpsilon);
422 
423  // speed optimization
424  void CreateIndex(int iSize);
425  void FreeIndex();
426 
427  // implement necessary virtual methods
428  virtual bool IsInsideRect(int iElem, const DRECT &rect);
429  virtual void CopyGeometry(uint from, uint to);
430  virtual void SaveGeomToSHP(SHPHandle hSHP, bool progress_callback(int)=0) const;
431  virtual void LoadGeomFromSHP(SHPHandle hSHP, bool progress_callback(int)=0);
432 
433 protected:
434  DPolyArray m_Poly; // wkbPolygon
435 
436  // speed optimization
437  SpatialIndex *m_pIndex;
438 };
439 
441 {
442 public:
443  virtual vtFeatureSet *LoadFrom(const char *filename);
444  vtFeatureSet *LoadFromSHP(const char *filename, bool progress_callback(int) = NULL);
445  vtFeatureSet *LoadHeaderFromSHP(const char *filename);
446  vtFeatureSet *LoadWithOGR(const char *filename, bool progress_callback(int) = NULL);
447  vtFeatureSet *LoadWithOGR(OGRLayer *pLayer, bool progress_callback(int) = NULL);
448  vtFeatureSet *LoadFromIGC(const char *filename);
449  vtFeatureSet *LoadFromDXF(const char *filename, bool progress_callback(int) = NULL);
450 
451  vtFeatureSet *ReadFeaturesFromWFS(const char *szServerURL, const char *layername);
452 
453  vtString m_strErrorMsg;
454 };
455 
456 // Helpers
457 OGRwkbGeometryType GetFeatureGeomType(const char *filename);
458 OGRwkbGeometryType ShapelibToOGR(int nSHPType);
459 int OGRToShapelib(OGRwkbGeometryType eGeomType);
460 vtString MakeDBFName(const char *filename);
461 int GetIntFromString(const char *buf, int len);
462 bool GeometryTypeIs3D(OGRwkbGeometryType type);
463 vtString GetShapeTypeName(int nShapeType);
464 
465 bool SHPToDPolygon2(SHPObject *pObj, DPolygon2 &dpoly);
466 void DPolygon2ToOGR(const DPolygon2 &dp, OGRPolygon &op);
467 void OGRToDPolygon2(const OGRPolygon &op, DPolygon2 &dp);
468 
469 #endif // VTDATA_FEATURES
470