QGIS API Documentation  2.14.0-Essen
qgswkbtypes.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgswkbtypes.h
3  -----------------------
4  begin : January 2015
5  copyright : (C) 2015 by Marco Hugentobler
6  email : marco at sourcepole dot ch
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef QGSWKBTYPES_H
19 #define QGSWKBTYPES_H
20 
21 #include <QMap>
22 #include <QString>
23 
24 /***************************************************************************
25  * This class is considered CRITICAL and any change MUST be accompanied with
26  * full unit tests in testqgsstatisticalsummary.cpp.
27  * See details in QEP #17
28  ****************************************************************************/
29 
36 class CORE_EXPORT QgsWKBTypes
37 {
38  public:
39 
40  enum Type
41  {
42  Unknown = 0,
43  Point = 1,
44  LineString = 2,
45  Polygon = 3,
46  MultiPoint = 4,
47  MultiLineString = 5,
48  MultiPolygon = 6,
49  GeometryCollection = 7,
50  CircularString = 8,
51  CompoundCurve = 9,
52  CurvePolygon = 10, //13, //should be 10. Seems to be correct in newer postgis versions
53  MultiCurve = 11,
54  MultiSurface = 12,
55  NoGeometry = 100, //attributes only
56  PointZ = 1001,
57  LineStringZ = 1002,
58  PolygonZ = 1003,
59  MultiPointZ = 1004,
60  MultiLineStringZ = 1005,
61  MultiPolygonZ = 1006,
62  GeometryCollectionZ = 1007,
63  CircularStringZ = 1008,
64  CompoundCurveZ = 1009,
65  CurvePolygonZ = 1010,
66  MultiCurveZ = 1011,
67  MultiSurfaceZ = 1012,
68  PointM = 2001,
69  LineStringM = 2002,
70  PolygonM = 2003,
71  MultiPointM = 2004,
72  MultiLineStringM = 2005,
73  MultiPolygonM = 2006,
74  GeometryCollectionM = 2007,
75  CircularStringM = 2008,
76  CompoundCurveM = 2009,
77  CurvePolygonM = 2010,
78  MultiCurveM = 2011,
79  MultiSurfaceM = 2012,
80  PointZM = 3001,
81  LineStringZM = 3002,
82  PolygonZM = 3003,
83  MultiPointZM = 3004,
84  MultiLineStringZM = 3005,
85  MultiPolygonZM = 3006,
86  GeometryCollectionZM = 3007,
87  CircularStringZM = 3008,
88  CompoundCurveZM = 3009,
89  CurvePolygonZM = 3010,
90  MultiCurveZM = 3011,
91  MultiSurfaceZM = 3012,
92  Point25D = 0x80000001,
97  MultiPolygon25D
98  };
99 
101  {
106  NullGeometry
107  };
108 
114  static Type singleType( Type type )
115  {
116  switch ( type )
117  {
118  case Unknown:
119  case GeometryCollection:
120  case GeometryCollectionZ:
121  case GeometryCollectionM:
122  case GeometryCollectionZM:
123  return Unknown;
124 
125  case Point:
126  case MultiPoint:
127  return Point;
128 
129  case PointZ:
130  case MultiPointZ:
131  return PointZ;
132 
133  case PointM:
134  case MultiPointM:
135  return PointM;
136 
137  case PointZM:
138  case MultiPointZM:
139  return PointZM;
140 
141  case LineString:
142  case MultiLineString:
143  return LineString;
144 
145  case LineStringZ:
146  case MultiLineStringZ:
147  return LineStringZ;
148 
149  case LineStringM:
150  case MultiLineStringM:
151  return LineStringM;
152 
153  case LineStringZM:
154  case MultiLineStringZM:
155  return LineStringZM;
156 
157  case Polygon:
158  case MultiPolygon:
159  return Polygon;
160 
161  case PolygonZ:
162  case MultiPolygonZ:
163  return PolygonZ;
164 
165  case PolygonM:
166  case MultiPolygonM:
167  return PolygonM;
168 
169  case PolygonZM:
170  case MultiPolygonZM:
171  return PolygonZM;
172 
173  case CircularString:
174  return CircularString;
175 
176  case CircularStringZ:
177  return CircularStringZ;
178 
179  case CircularStringM:
180  return CircularStringM;
181 
182  case CircularStringZM:
183  return CircularStringZM;
184 
185  case CompoundCurve:
186  case MultiCurve:
187  return CompoundCurve;
188 
189  case CompoundCurveZ:
190  case MultiCurveZ:
191  return CompoundCurveZ;
192 
193  case CompoundCurveM:
194  case MultiCurveM:
195  return CompoundCurveM;
196 
197  case CompoundCurveZM:
198  case MultiCurveZM:
199  return CompoundCurveZM;
200 
201  case CurvePolygon:
202  case MultiSurface:
203  return CurvePolygon;
204 
205  case CurvePolygonZ:
206  case MultiSurfaceZ:
207  return CurvePolygonZ;
208 
209  case CurvePolygonM:
210  case MultiSurfaceM:
211  return CurvePolygonM;
212 
213  case CurvePolygonZM:
214  case MultiSurfaceZM:
215  return CurvePolygonZM;
216 
217  case NoGeometry:
218  return NoGeometry;
219 
220  case Point25D:
221  case MultiPoint25D:
222  return Point25D;
223 
224  case LineString25D:
225  case MultiLineString25D:
226  return LineString25D;
227 
228  case Polygon25D:
229  case MultiPolygon25D:
230  return Polygon25D;
231  }
232  return Unknown;
233  }
234 
240  static Type multiType( Type type )
241  {
242  switch ( type )
243  {
244  case Unknown:
245  return Unknown;
246 
247  case GeometryCollection:
248  return GeometryCollection;
249 
250  case GeometryCollectionZ:
251  return GeometryCollectionZ;
252 
253  case GeometryCollectionM:
254  return GeometryCollectionM;
255 
256  case GeometryCollectionZM:
257  return GeometryCollectionZM;
258 
259  case Point:
260  case MultiPoint:
261  return MultiPoint;
262 
263  case PointZ:
264  case MultiPointZ:
265  return MultiPointZ;
266 
267  case PointM:
268  case MultiPointM:
269  return MultiPointM;
270 
271  case PointZM:
272  case MultiPointZM:
273  return MultiPointZM;
274 
275  case LineString:
276  case MultiLineString:
277  return MultiLineString;
278 
279  case LineStringZ:
280  case MultiLineStringZ:
281  return MultiLineStringZ;
282 
283  case LineStringM:
284  case MultiLineStringM:
285  return MultiLineStringM;
286 
287  case LineStringZM:
288  case MultiLineStringZM:
289  return MultiLineStringZM;
290 
291  case Polygon:
292  case MultiPolygon:
293  return MultiPolygon;
294 
295  case PolygonZ:
296  case MultiPolygonZ:
297  return MultiPolygonZ;
298 
299  case PolygonM:
300  case MultiPolygonM:
301  return MultiPolygonM;
302 
303  case PolygonZM:
304  case MultiPolygonZM:
305  return MultiPolygonZM;
306 
307  case CompoundCurve:
308  case CircularString:
309  case MultiCurve:
310  return MultiCurve;
311 
312  case CompoundCurveZ:
313  case CircularStringZ:
314  case MultiCurveZ:
315  return MultiCurveZ;
316 
317  case CompoundCurveM:
318  case CircularStringM:
319  case MultiCurveM:
320  return MultiCurveM;
321 
322  case CompoundCurveZM:
323  case CircularStringZM:
324  case MultiCurveZM:
325  return MultiCurveZM;
326 
327  case CurvePolygon:
328  case MultiSurface:
329  return MultiSurface;
330 
331  case CurvePolygonZ:
332  case MultiSurfaceZ:
333  return MultiSurfaceZ;
334 
335  case CurvePolygonM:
336  case MultiSurfaceM:
337  return MultiSurfaceM;
338 
339  case CurvePolygonZM:
340  case MultiSurfaceZM:
341  return MultiSurfaceZM;
342 
343  case NoGeometry:
344  return NoGeometry;
345 
346  case Point25D:
347  case MultiPoint25D:
348  return MultiPoint25D;
349 
350  case LineString25D:
351  case MultiLineString25D:
352  return MultiLineString25D;
353 
354  case Polygon25D:
355  case MultiPolygon25D:
356  return MultiPolygon25D;
357  }
358  return Unknown;
359  }
360 
366  static Type flatType( Type type )
367  {
368  switch ( type )
369  {
370  case Unknown:
371  return Unknown;
372 
373  case Point:
374  case PointZ:
375  case PointM:
376  case PointZM:
377  case Point25D:
378  return Point;
379 
380  case LineString:
381  case LineStringZ:
382  case LineStringM:
383  case LineStringZM:
384  case LineString25D:
385  return LineString;
386 
387  case Polygon:
388  case PolygonZ:
389  case PolygonM:
390  case PolygonZM:
391  case Polygon25D:
392  return Polygon;
393 
394  case MultiPoint:
395  case MultiPointZ:
396  case MultiPointM:
397  case MultiPointZM:
398  case MultiPoint25D:
399  return MultiPoint;
400 
401  case MultiLineString:
402  case MultiLineStringZ:
403  case MultiLineStringM:
404  case MultiLineStringZM:
405  case MultiLineString25D:
406  return MultiLineString;
407 
408  case MultiPolygon:
409  case MultiPolygonZ:
410  case MultiPolygonM:
411  case MultiPolygonZM:
412  case MultiPolygon25D:
413  return MultiPolygon;
414 
415  case GeometryCollection:
416  case GeometryCollectionZ:
417  case GeometryCollectionM:
418  case GeometryCollectionZM:
419  return GeometryCollection;
420 
421  case CircularString:
422  case CircularStringZ:
423  case CircularStringM:
424  case CircularStringZM:
425  return CircularString;
426 
427  case CompoundCurve:
428  case CompoundCurveZ:
429  case CompoundCurveM:
430  case CompoundCurveZM:
431  return CompoundCurve;
432 
433  case MultiCurve:
434  case MultiCurveZ:
435  case MultiCurveM:
436  case MultiCurveZM:
437  return MultiCurve;
438 
439  case CurvePolygon:
440  case CurvePolygonZ:
441  case CurvePolygonM:
442  case CurvePolygonZM:
443  return CurvePolygon;
444 
445  case MultiSurface:
446  case MultiSurfaceZ:
447  case MultiSurfaceM:
448  case MultiSurfaceZM:
449  return MultiSurface;
450 
451  case NoGeometry:
452  return NoGeometry;
453 
454  }
455  return Unknown;
456  }
457 
461  static Type parseType( const QString& wktStr );
462 
467  static bool isSingleType( Type type )
468  {
469  return ( type != Unknown && !isMultiType( type ) );
470  }
471 
476  static bool isMultiType( Type type )
477  {
478  switch ( type )
479  {
480  case Unknown:
481  case Point:
482  case LineString:
483  case Polygon:
484  case CircularString:
485  case CompoundCurve:
486  case CurvePolygon:
487  case NoGeometry:
488  case PointZ:
489  case LineStringZ:
490  case PolygonZ:
491  case CircularStringZ:
492  case CompoundCurveZ:
493  case CurvePolygonZ:
494  case PointM:
495  case LineStringM:
496  case PolygonM:
497  case CircularStringM:
498  case CompoundCurveM:
499  case CurvePolygonM:
500  case PointZM:
501  case LineStringZM:
502  case PolygonZM:
503  case CircularStringZM:
504  case CompoundCurveZM:
505  case CurvePolygonZM:
506  case Point25D:
507  case LineString25D:
508  case Polygon25D:
509  return false;
510 
511  default:
512  return true;
513 
514  }
515  }
516 
520  static bool isCurvedType( Type type )
521  {
522  switch ( flatType( type ) )
523  {
524  case CircularString:
525  case CompoundCurve:
526  case CurvePolygon:
527  case MultiCurve:
528  case MultiSurface:
529  return true;
530 
531  default:
532  return false;
533  }
534  }
535 
542  static int wkbDimensions( Type type )
543  {
544  GeometryType gtype = geometryType( type );
545  switch ( gtype )
546  {
547  case LineGeometry:
548  return 1;
549  case PolygonGeometry:
550  return 2;
551  default: //point, no geometry, unknown geometry
552  return 0;
553  }
554  }
555 
562  static int coordDimensions( Type type )
563  {
564  if ( type == Unknown || type == NoGeometry )
565  return 0;
566 
567  return 2 + hasZ( type ) + hasM( type );
568  }
569 
574  {
575  switch ( type )
576  {
577  case Unknown:
578  case GeometryCollection:
579  case GeometryCollectionZ:
580  case GeometryCollectionM:
581  case GeometryCollectionZM:
582  return UnknownGeometry;
583 
584  case Point:
585  case MultiPoint:
586  case PointZ:
587  case MultiPointZ:
588  case PointM:
589  case MultiPointM:
590  case PointZM:
591  case MultiPointZM:
592  case Point25D:
593  case MultiPoint25D:
594  return PointGeometry;
595 
596  case LineString:
597  case MultiLineString:
598  case LineStringZ:
599  case MultiLineStringZ:
600  case LineStringM:
601  case MultiLineStringM:
602  case LineStringZM:
603  case MultiLineStringZM:
604  case LineString25D:
605  case MultiLineString25D:
606  case CircularString:
607  case CompoundCurve:
608  case MultiCurve:
609  case CircularStringZ:
610  case CompoundCurveZ:
611  case MultiCurveZ:
612  case CircularStringM:
613  case CompoundCurveM:
614  case MultiCurveM:
615  case CircularStringZM:
616  case CompoundCurveZM:
617  case MultiCurveZM:
618  return LineGeometry;
619 
620  case Polygon:
621  case MultiPolygon:
622  case PolygonZ:
623  case MultiPolygonZ:
624  case PolygonM:
625  case MultiPolygonM:
626  case PolygonZM:
627  case MultiPolygonZM:
628  case Polygon25D:
629  case MultiPolygon25D:
630  case CurvePolygon:
631  case MultiSurface:
632  case CurvePolygonZ:
633  case MultiSurfaceZ:
634  case CurvePolygonM:
635  case MultiSurfaceM:
636  case CurvePolygonZM:
637  case MultiSurfaceZM:
638  return PolygonGeometry;
639 
640  case NoGeometry:
641  return NullGeometry;
642  }
643 
644  return UnknownGeometry;
645  }
646 
649  static QString displayString( Type type );
650 
656  static bool hasZ( Type type )
657  {
658  switch ( type )
659  {
660  case PointZ:
661  case LineStringZ:
662  case PolygonZ:
663  case MultiPointZ:
664  case MultiLineStringZ:
665  case MultiPolygonZ:
666  case GeometryCollectionZ:
667  case CircularStringZ:
668  case CompoundCurveZ:
669  case CurvePolygonZ:
670  case MultiCurveZ:
671  case MultiSurfaceZ:
672  case PointZM:
673  case LineStringZM:
674  case PolygonZM:
675  case MultiPointZM:
676  case MultiLineStringZM:
677  case MultiPolygonZM:
678  case GeometryCollectionZM:
679  case CircularStringZM:
680  case CompoundCurveZM:
681  case CurvePolygonZM:
682  case MultiCurveZM:
683  case MultiSurfaceZM:
684  case Point25D:
685  case LineString25D:
686  case Polygon25D:
687  case MultiPoint25D:
688  case MultiLineString25D:
689  case MultiPolygon25D:
690  return true;
691 
692  default:
693  return false;
694 
695  }
696  }
697 
703  static bool hasM( Type type )
704  {
705  switch ( type )
706  {
707  case PointM:
708  case LineStringM:
709  case PolygonM:
710  case MultiPointM:
711  case MultiLineStringM:
712  case MultiPolygonM:
713  case GeometryCollectionM:
714  case CircularStringM:
715  case CompoundCurveM:
716  case CurvePolygonM:
717  case MultiCurveM:
718  case MultiSurfaceM:
719  case PointZM:
720  case LineStringZM:
721  case PolygonZM:
722  case MultiPointZM:
723  case MultiLineStringZM:
724  case MultiPolygonZM:
725  case GeometryCollectionZM:
726  case CircularStringZM:
727  case CompoundCurveZM:
728  case CurvePolygonZM:
729  case MultiCurveZM:
730  case MultiSurfaceZM:
731  return true;
732 
733  default:
734  return false;
735 
736  }
737  }
738 
746  static Type addZ( Type type )
747  {
748  if ( hasZ( type ) )
749  return type;
750  else if ( type == Unknown )
751  return Unknown;
752  else if ( type == NoGeometry )
753  return NoGeometry;
754 
755  //upgrade with z dimension
756  Type flat = flatType( type );
757  if ( hasM( type ) )
758  return static_cast< QgsWKBTypes::Type >( flat + 3000 );
759  else
760  return static_cast< QgsWKBTypes::Type >( flat + 1000 );
761  }
762 
770  static Type addM( Type type )
771  {
772  if ( hasM( type ) )
773  return type;
774  else if ( type == Unknown )
775  return Unknown;
776  else if ( type == NoGeometry )
777  return NoGeometry;
778  else if ( type == Point25D ||
779  type == LineString25D ||
780  type == Polygon25D ||
781  type == MultiPoint25D ||
782  type == MultiLineString25D ||
783  type == MultiPolygon25D )
784  return type; //can't add M dimension to these types
785 
786  //upgrade with m dimension
787  Type flat = flatType( type );
788  if ( hasZ( type ) )
789  return static_cast< QgsWKBTypes::Type >( flat + 3000 );
790  else
791  return static_cast< QgsWKBTypes::Type >( flat + 2000 );
792  }
793 
800  static Type dropZ( Type type )
801  {
802  if ( !hasZ( type ) )
803  return type;
804 
805  QgsWKBTypes::Type returnType = flatType( type );
806  if ( hasM( type ) )
807  returnType = addM( returnType );
808  return returnType;
809  }
810 
817  static Type dropM( Type type )
818  {
819  if ( !hasM( type ) )
820  return type;
821 
822  QgsWKBTypes::Type returnType = flatType( type );
823  if ( hasZ( type ) )
824  returnType = addZ( returnType );
825  return returnType;
826  }
827 
833  static Type to25D( Type type )
834  {
835  QgsWKBTypes::Type flat = flatType( type );
836 
837  if ( flat >= Point && flat <= MultiPolygon )
838  return static_cast< QgsWKBTypes::Type >( flat + 0x80000000 );
839  else if ( type == QgsWKBTypes::NoGeometry )
841  else
842  return Unknown;
843  }
844 
845  private:
846 
847  struct wkbEntry
848  {
849  wkbEntry( const QString& name, bool isMultiType, Type multiType, Type singleType, Type flatType, GeometryType geometryType,
850  bool hasZ, bool hasM ):
851  mName( name ), mIsMultiType( isMultiType ), mMultiType( multiType ), mSingleType( singleType ), mFlatType( flatType ), mGeometryType( geometryType ),
852  mHasZ( hasZ ), mHasM( hasM ) {}
853  QString mName;
854  bool mIsMultiType;
855  Type mMultiType;
856  Type mSingleType;
857  Type mFlatType;
858  GeometryType mGeometryType;
859  bool mHasZ;
860  bool mHasM;
861  };
862 
863  static QMap<Type, wkbEntry> registerTypes();
864  static QMap<Type, wkbEntry>* entries();
865 };
866 
867 #endif // QGSWKBTYPES_H
static int coordDimensions(Type type)
Returns the coordinate dimension of the geometry type as an integer.
Definition: qgswkbtypes.h:562
static bool isSingleType(Type type)
Returns true if the WKB type is a single type.
Definition: qgswkbtypes.h:467
static Type addZ(Type type)
Adds the z dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:746
static bool hasM(Type type)
Tests whether a WKB type contains m values.
Definition: qgswkbtypes.h:703
static Type multiType(Type type)
Returns the multi type for a WKB type.
Definition: qgswkbtypes.h:240
static bool isMultiType(Type type)
Returns true if the WKB type is a multi type.
Definition: qgswkbtypes.h:476
static bool hasZ(Type type)
Tests whether a WKB type contains the z-dimension.
Definition: qgswkbtypes.h:656
static Type dropZ(Type type)
Drops the z dimension (if present) for a WKB type and returns the new type.
Definition: qgswkbtypes.h:800
static Type addM(Type type)
Adds the m dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:770
static bool isCurvedType(Type type)
Returns true if the WKB type is a curved type or can contain curved geometries.
Definition: qgswkbtypes.h:520
static GeometryType geometryType(Type type)
Returns the geometry type for a WKB type, eg both MultiPolygon and CurvePolygon would have a PolygonG...
Definition: qgswkbtypes.h:573
static Type dropM(Type type)
Drops the m dimension (if present) for a WKB type and returns the new type.
Definition: qgswkbtypes.h:817
Handles storage of information regarding WKB types and their properties.
Definition: qgswkbtypes.h:36
static Type flatType(Type type)
Returns the flat type for a WKB type.
Definition: qgswkbtypes.h:366
static Type to25D(Type type)
Will convert the 25D version of the flat type if supported or Unknown if not supported.
Definition: qgswkbtypes.h:833
static int wkbDimensions(Type type)
Returns the inherent dimension of the geometry type as an integer.
Definition: qgswkbtypes.h:542
static Type singleType(Type type)
Returns the single type for a WKB type.
Definition: qgswkbtypes.h:114