QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 <QObject>
22 #include <QMap>
23 #include <QString>
24 
25 #include "qgis_core.h"
26 
27 /***************************************************************************
28  * This class is considered CRITICAL and any change MUST be accompanied with
29  * full unit tests in testqgsstatisticalsummary.cpp.
30  * See details in QEP #17
31  ****************************************************************************/
32 
40 class CORE_EXPORT QgsWkbTypes
41 {
42  Q_GADGET
43  public:
44 
68  enum Type
69  {
70  Unknown = 0,
71  Point = 1,
72  LineString = 2,
73  Polygon = 3,
74  Triangle = 17,
75  MultiPoint = 4,
76  MultiLineString = 5,
77  MultiPolygon = 6,
78  GeometryCollection = 7,
79  CircularString = 8,
80  CompoundCurve = 9,
81  CurvePolygon = 10, //13, //should be 10. Seems to be correct in newer PostGIS versions
82  MultiCurve = 11,
83  MultiSurface = 12,
84  NoGeometry = 100, //attributes only
85  PointZ = 1001,
86  LineStringZ = 1002,
87  PolygonZ = 1003,
88  TriangleZ = 1017,
89  MultiPointZ = 1004,
90  MultiLineStringZ = 1005,
91  MultiPolygonZ = 1006,
92  GeometryCollectionZ = 1007,
93  CircularStringZ = 1008,
94  CompoundCurveZ = 1009,
95  CurvePolygonZ = 1010,
96  MultiCurveZ = 1011,
97  MultiSurfaceZ = 1012,
98  PointM = 2001,
99  LineStringM = 2002,
100  PolygonM = 2003,
101  TriangleM = 2017,
102  MultiPointM = 2004,
103  MultiLineStringM = 2005,
104  MultiPolygonM = 2006,
105  GeometryCollectionM = 2007,
106  CircularStringM = 2008,
107  CompoundCurveM = 2009,
108  CurvePolygonM = 2010,
109  MultiCurveM = 2011,
110  MultiSurfaceM = 2012,
111  PointZM = 3001,
112  LineStringZM = 3002,
113  PolygonZM = 3003,
114  MultiPointZM = 3004,
115  MultiLineStringZM = 3005,
116  MultiPolygonZM = 3006,
117  GeometryCollectionZM = 3007,
118  CircularStringZM = 3008,
119  CompoundCurveZM = 3009,
120  CurvePolygonZM = 3010,
121  MultiCurveZM = 3011,
122  MultiSurfaceZM = 3012,
123  TriangleZM = 3017,
124  Point25D = 0x80000001,
129  MultiPolygon25D
130  };
131  Q_ENUM( Type )
132 
133 
140  {
145  NullGeometry
146  };
147  Q_ENUM( GeometryType )
148 
149 
155  static Type singleType( Type type )
156  {
157  switch ( type )
158  {
159  case Unknown:
160  case GeometryCollection:
161  case GeometryCollectionZ:
162  case GeometryCollectionM:
163  case GeometryCollectionZM:
164  return Unknown;
165 
166  case Point:
167  case MultiPoint:
168  return Point;
169 
170  case PointZ:
171  case MultiPointZ:
172  return PointZ;
173 
174  case PointM:
175  case MultiPointM:
176  return PointM;
177 
178  case PointZM:
179  case MultiPointZM:
180  return PointZM;
181 
182  case LineString:
183  case MultiLineString:
184  return LineString;
185 
186  case LineStringZ:
187  case MultiLineStringZ:
188  return LineStringZ;
189 
190  case LineStringM:
191  case MultiLineStringM:
192  return LineStringM;
193 
194  case LineStringZM:
195  case MultiLineStringZM:
196  return LineStringZM;
197 
198  case Polygon:
199  case MultiPolygon:
200  return Polygon;
201 
202  case PolygonZ:
203  case MultiPolygonZ:
204  return PolygonZ;
205 
206  case PolygonM:
207  case MultiPolygonM:
208  return PolygonM;
209 
210  case PolygonZM:
211  case MultiPolygonZM:
212  return PolygonZM;
213 
214  case Triangle:
215  // case MultiTriangle:
216  return Triangle;
217 
218  case TriangleZ:
219  // case MultiTriangleZ:
220  return TriangleZ;
221 
222  case TriangleM:
223  // case MultiTriangleM:
224  return TriangleM;
225 
226  case TriangleZM:
227  // case MultiTriangleZM:
228  return TriangleZM;
229 
230  case CircularString:
231  return CircularString;
232 
233  case CircularStringZ:
234  return CircularStringZ;
235 
236  case CircularStringM:
237  return CircularStringM;
238 
239  case CircularStringZM:
240  return CircularStringZM;
241 
242  case CompoundCurve:
243  case MultiCurve:
244  return CompoundCurve;
245 
246  case CompoundCurveZ:
247  case MultiCurveZ:
248  return CompoundCurveZ;
249 
250  case CompoundCurveM:
251  case MultiCurveM:
252  return CompoundCurveM;
253 
254  case CompoundCurveZM:
255  case MultiCurveZM:
256  return CompoundCurveZM;
257 
258  case CurvePolygon:
259  case MultiSurface:
260  return CurvePolygon;
261 
262  case CurvePolygonZ:
263  case MultiSurfaceZ:
264  return CurvePolygonZ;
265 
266  case CurvePolygonM:
267  case MultiSurfaceM:
268  return CurvePolygonM;
269 
270  case CurvePolygonZM:
271  case MultiSurfaceZM:
272  return CurvePolygonZM;
273 
274  case NoGeometry:
275  return NoGeometry;
276 
277  case Point25D:
278  case MultiPoint25D:
279  return Point25D;
280 
281  case LineString25D:
282  case MultiLineString25D:
283  return LineString25D;
284 
285  case Polygon25D:
286  case MultiPolygon25D:
287  return Polygon25D;
288 
289  }
290  return Unknown;
291  }
292 
299  static Type multiType( Type type )
300  {
301  switch ( type )
302  {
303  case Unknown:
304  case Triangle:
305  case TriangleZ:
306  case TriangleM:
307  case TriangleZM:
308  return Unknown;
309 
310  case GeometryCollection:
311  return GeometryCollection;
312 
313  case GeometryCollectionZ:
314  return GeometryCollectionZ;
315 
316  case GeometryCollectionM:
317  return GeometryCollectionM;
318 
319  case GeometryCollectionZM:
320  return GeometryCollectionZM;
321 
322  case Point:
323  case MultiPoint:
324  return MultiPoint;
325 
326  case PointZ:
327  case MultiPointZ:
328  return MultiPointZ;
329 
330  case PointM:
331  case MultiPointM:
332  return MultiPointM;
333 
334  case PointZM:
335  case MultiPointZM:
336  return MultiPointZM;
337 
338  case LineString:
339  case MultiLineString:
340  return MultiLineString;
341 
342  case LineStringZ:
343  case MultiLineStringZ:
344  return MultiLineStringZ;
345 
346  case LineStringM:
347  case MultiLineStringM:
348  return MultiLineStringM;
349 
350  case LineStringZM:
351  case MultiLineStringZM:
352  return MultiLineStringZM;
353 
354  case Polygon:
355  case MultiPolygon:
356  return MultiPolygon;
357 
358  case PolygonZ:
359  case MultiPolygonZ:
360  return MultiPolygonZ;
361 
362  case PolygonM:
363  case MultiPolygonM:
364  return MultiPolygonM;
365 
366  case PolygonZM:
367  case MultiPolygonZM:
368  return MultiPolygonZM;
369 
370  case CompoundCurve:
371  case CircularString:
372  case MultiCurve:
373  return MultiCurve;
374 
375  case CompoundCurveZ:
376  case CircularStringZ:
377  case MultiCurveZ:
378  return MultiCurveZ;
379 
380  case CompoundCurveM:
381  case CircularStringM:
382  case MultiCurveM:
383  return MultiCurveM;
384 
385  case CompoundCurveZM:
386  case CircularStringZM:
387  case MultiCurveZM:
388  return MultiCurveZM;
389 
390  case CurvePolygon:
391  case MultiSurface:
392  return MultiSurface;
393 
394  case CurvePolygonZ:
395  case MultiSurfaceZ:
396  return MultiSurfaceZ;
397 
398  case CurvePolygonM:
399  case MultiSurfaceM:
400  return MultiSurfaceM;
401 
402  case CurvePolygonZM:
403  case MultiSurfaceZM:
404  return MultiSurfaceZM;
405 
406  case NoGeometry:
407  return NoGeometry;
408 
409  case Point25D:
410  case MultiPoint25D:
411  return MultiPoint25D;
412 
413  case LineString25D:
414  case MultiLineString25D:
415  return MultiLineString25D;
416 
417  case Polygon25D:
418  case MultiPolygon25D:
419  return MultiPolygon25D;
420  }
421  return Unknown;
422  }
423 
430  static Type flatType( Type type )
431  {
432  switch ( type )
433  {
434  case Unknown:
435  return Unknown;
436 
437  case Point:
438  case PointZ:
439  case PointM:
440  case PointZM:
441  case Point25D:
442  return Point;
443 
444  case LineString:
445  case LineStringZ:
446  case LineStringM:
447  case LineStringZM:
448  case LineString25D:
449  return LineString;
450 
451  case Polygon:
452  case PolygonZ:
453  case PolygonM:
454  case PolygonZM:
455  case Polygon25D:
456  return Polygon;
457 
458  case Triangle:
459  case TriangleZ:
460  case TriangleM:
461  case TriangleZM:
462  return Triangle;
463 
464  case MultiPoint:
465  case MultiPointZ:
466  case MultiPointM:
467  case MultiPointZM:
468  case MultiPoint25D:
469  return MultiPoint;
470 
471  case MultiLineString:
472  case MultiLineStringZ:
473  case MultiLineStringM:
474  case MultiLineStringZM:
475  case MultiLineString25D:
476  return MultiLineString;
477 
478  case MultiPolygon:
479  case MultiPolygonZ:
480  case MultiPolygonM:
481  case MultiPolygonZM:
482  case MultiPolygon25D:
483  return MultiPolygon;
484 
485  case GeometryCollection:
486  case GeometryCollectionZ:
487  case GeometryCollectionM:
488  case GeometryCollectionZM:
489  return GeometryCollection;
490 
491  case CircularString:
492  case CircularStringZ:
493  case CircularStringM:
494  case CircularStringZM:
495  return CircularString;
496 
497  case CompoundCurve:
498  case CompoundCurveZ:
499  case CompoundCurveM:
500  case CompoundCurveZM:
501  return CompoundCurve;
502 
503  case MultiCurve:
504  case MultiCurveZ:
505  case MultiCurveM:
506  case MultiCurveZM:
507  return MultiCurve;
508 
509  case CurvePolygon:
510  case CurvePolygonZ:
511  case CurvePolygonM:
512  case CurvePolygonZM:
513  return CurvePolygon;
514 
515  case MultiSurface:
516  case MultiSurfaceZ:
517  case MultiSurfaceM:
518  case MultiSurfaceZM:
519  return MultiSurface;
520 
521  case NoGeometry:
522  return NoGeometry;
523 
524  }
525  return Unknown;
526  }
527 
529  static Type zmType( Type type, bool hasZ, bool hasM )
530  {
531  type = flatType( type );
532  if ( hasZ )
533  type = static_cast<QgsWkbTypes::Type>( static_cast<quint32>( type ) + 1000 );
534  if ( hasM )
535  type = static_cast<QgsWkbTypes::Type>( static_cast<quint32>( type ) + 2000 );
536  return type;
537  }
538 
543  static Type parseType( const QString &wktStr );
544 
550  static bool isSingleType( Type type )
551  {
552  return ( type != Unknown && !isMultiType( type ) );
553  }
554 
560  static bool isMultiType( Type type )
561  {
562  switch ( type )
563  {
564  case Unknown:
565  case Point:
566  case LineString:
567  case Polygon:
568  case Triangle:
569  case CircularString:
570  case CompoundCurve:
571  case CurvePolygon:
572  case NoGeometry:
573  case PointZ:
574  case LineStringZ:
575  case PolygonZ:
576  case TriangleZ:
577  case CircularStringZ:
578  case CompoundCurveZ:
579  case CurvePolygonZ:
580  case PointM:
581  case LineStringM:
582  case PolygonM:
583  case TriangleM:
584  case CircularStringM:
585  case CompoundCurveM:
586  case CurvePolygonM:
587  case PointZM:
588  case LineStringZM:
589  case PolygonZM:
590  case TriangleZM:
591  case CircularStringZM:
592  case CompoundCurveZM:
593  case CurvePolygonZM:
594  case Point25D:
595  case LineString25D:
596  case Polygon25D:
597  return false;
598 
599  default:
600  return true;
601 
602  }
603  }
604 
609  static bool isCurvedType( Type type )
610  {
611  switch ( flatType( type ) )
612  {
613  case CircularString:
614  case CompoundCurve:
615  case CurvePolygon:
616  case MultiCurve:
617  case MultiSurface:
618  return true;
619 
620  default:
621  return false;
622  }
623  }
624 
632  static int wkbDimensions( Type type )
633  {
634  GeometryType gtype = geometryType( type );
635  switch ( gtype )
636  {
637  case LineGeometry:
638  return 1;
639  case PolygonGeometry:
640  return 2;
641  default: //point, no geometry, unknown geometry
642  return 0;
643  }
644  }
645 
653  static int coordDimensions( Type type )
654  {
655  if ( type == Unknown || type == NoGeometry )
656  return 0;
657 
658  return 2 + hasZ( type ) + hasM( type );
659  }
660 
667  {
668  switch ( type )
669  {
670  case Unknown:
671  case GeometryCollection:
672  case GeometryCollectionZ:
673  case GeometryCollectionM:
674  case GeometryCollectionZM:
675  return UnknownGeometry;
676 
677  case Point:
678  case MultiPoint:
679  case PointZ:
680  case MultiPointZ:
681  case PointM:
682  case MultiPointM:
683  case PointZM:
684  case MultiPointZM:
685  case Point25D:
686  case MultiPoint25D:
687  return PointGeometry;
688 
689  case LineString:
690  case MultiLineString:
691  case LineStringZ:
692  case MultiLineStringZ:
693  case LineStringM:
694  case MultiLineStringM:
695  case LineStringZM:
696  case MultiLineStringZM:
697  case LineString25D:
698  case MultiLineString25D:
699  case CircularString:
700  case CompoundCurve:
701  case MultiCurve:
702  case CircularStringZ:
703  case CompoundCurveZ:
704  case MultiCurveZ:
705  case CircularStringM:
706  case CompoundCurveM:
707  case MultiCurveM:
708  case CircularStringZM:
709  case CompoundCurveZM:
710  case MultiCurveZM:
711  return LineGeometry;
712 
713  case Polygon:
714  case MultiPolygon:
715  case Triangle:
716  case PolygonZ:
717  case TriangleZ:
718  case MultiPolygonZ:
719  case PolygonM:
720  case TriangleM:
721  case MultiPolygonM:
722  case PolygonZM:
723  case MultiPolygonZM:
724  case TriangleZM:
725  case Polygon25D:
726  case MultiPolygon25D:
727  case CurvePolygon:
728  case MultiSurface:
729  case CurvePolygonZ:
730  case MultiSurfaceZ:
731  case CurvePolygonM:
732  case MultiSurfaceM:
733  case CurvePolygonZM:
734  case MultiSurfaceZM:
735  return PolygonGeometry;
736 
737  case NoGeometry:
738  return NullGeometry;
739  }
740 
741  return UnknownGeometry;
742  }
743 
747  static QString displayString( Type type );
748 
763  static QString geometryDisplayString( GeometryType type );
764 
771  static bool hasZ( Type type )
772  {
773  switch ( type )
774  {
775  case PointZ:
776  case LineStringZ:
777  case PolygonZ:
778  case TriangleZ:
779  case MultiPointZ:
780  case MultiLineStringZ:
781  case MultiPolygonZ:
782  case GeometryCollectionZ:
783  case CircularStringZ:
784  case CompoundCurveZ:
785  case CurvePolygonZ:
786  case MultiCurveZ:
787  case MultiSurfaceZ:
788  case PointZM:
789  case LineStringZM:
790  case PolygonZM:
791  case TriangleZM:
792  case MultiPointZM:
793  case MultiLineStringZM:
794  case MultiPolygonZM:
795  case GeometryCollectionZM:
796  case CircularStringZM:
797  case CompoundCurveZM:
798  case CurvePolygonZM:
799  case MultiCurveZM:
800  case MultiSurfaceZM:
801  case Point25D:
802  case LineString25D:
803  case Polygon25D:
804  case MultiPoint25D:
805  case MultiLineString25D:
806  case MultiPolygon25D:
807  return true;
808 
809  default:
810  return false;
811 
812  }
813  }
814 
821  static bool hasM( Type type )
822  {
823  switch ( type )
824  {
825  case PointM:
826  case LineStringM:
827  case PolygonM:
828  case TriangleM:
829  case MultiPointM:
830  case MultiLineStringM:
831  case MultiPolygonM:
832  case GeometryCollectionM:
833  case CircularStringM:
834  case CompoundCurveM:
835  case CurvePolygonM:
836  case MultiCurveM:
837  case MultiSurfaceM:
838  case PointZM:
839  case LineStringZM:
840  case PolygonZM:
841  case TriangleZM:
842  case MultiPointZM:
843  case MultiLineStringZM:
844  case MultiPolygonZM:
845  case GeometryCollectionZM:
846  case CircularStringZM:
847  case CompoundCurveZM:
848  case CurvePolygonZM:
849  case MultiCurveZM:
850  case MultiSurfaceZM:
851  return true;
852 
853  default:
854  return false;
855 
856  }
857  }
858 
867  static Type addZ( Type type )
868  {
869  if ( hasZ( type ) )
870  return type;
871  else if ( type == Unknown )
872  return Unknown;
873  else if ( type == NoGeometry )
874  return NoGeometry;
875 
876  //upgrade with z dimension
877  Type flat = flatType( type );
878  if ( hasM( type ) )
879  return static_cast< QgsWkbTypes::Type >( flat + 3000 );
880  else
881  return static_cast< QgsWkbTypes::Type >( flat + 1000 );
882  }
883 
892  static Type addM( Type type )
893  {
894  if ( hasM( type ) )
895  return type;
896  else if ( type == Unknown )
897  return Unknown;
898  else if ( type == NoGeometry )
899  return NoGeometry;
900  else if ( type == Point25D ||
901  type == LineString25D ||
902  type == Polygon25D ||
903  type == MultiPoint25D ||
904  type == MultiLineString25D ||
905  type == MultiPolygon25D )
906  return type; //can't add M dimension to these types
907 
908  //upgrade with m dimension
909  Type flat = flatType( type );
910  if ( hasZ( type ) )
911  return static_cast< QgsWkbTypes::Type >( flat + 3000 );
912  else
913  return static_cast< QgsWkbTypes::Type >( flat + 2000 );
914  }
915 
923  static Type dropZ( Type type )
924  {
925  if ( !hasZ( type ) )
926  return type;
927 
928  QgsWkbTypes::Type returnType = flatType( type );
929  if ( hasM( type ) )
930  returnType = addM( returnType );
931  return returnType;
932  }
933 
941  static Type dropM( Type type )
942  {
943  if ( !hasM( type ) )
944  return type;
945 
946  QgsWkbTypes::Type returnType = flatType( type );
947  if ( hasZ( type ) )
948  returnType = addZ( returnType );
949  return returnType;
950  }
951 
957  static Type to25D( Type type )
958  {
959  QgsWkbTypes::Type flat = flatType( type );
960 
961  if ( flat >= Point && flat <= MultiPolygon )
962  return static_cast< QgsWkbTypes::Type >( flat + 0x80000000 );
963  else if ( type == QgsWkbTypes::NoGeometry )
965  else
966  return Unknown;
967  }
968 
969  private:
970 
971  struct wkbEntry
972  {
973  wkbEntry( const QString &name, bool isMultiType, Type multiType, Type singleType, Type flatType, GeometryType geometryType,
974  bool hasZ, bool hasM )
975  : mName( name )
976  , mIsMultiType( isMultiType )
977  , mMultiType( multiType )
978  , mSingleType( singleType )
979  , mFlatType( flatType )
980  , mGeometryType( geometryType )
981  , mHasZ( hasZ )
982  , mHasM( hasM )
983  {}
984  QString mName;
985  bool mIsMultiType;
986  Type mMultiType;
987  Type mSingleType;
988  Type mFlatType;
989  GeometryType mGeometryType;
990  bool mHasZ;
991  bool mHasM;
992  };
993 
994  static const QMap<Type, wkbEntry> ENTRIES;
995 };
996 
997 #endif // QGSWKBTYPES_H
static Type to25D(Type type)
Will convert the 25D version of the flat type if supported or Unknown if not supported.
Definition: qgswkbtypes.h:957
static Type multiType(Type type)
Returns the multi type for a WKB type.
Definition: qgswkbtypes.h:299
static int wkbDimensions(Type type)
Returns the inherent dimension of the geometry type as an integer.
Definition: qgswkbtypes.h:632
static bool isMultiType(Type type)
Returns true if the WKB type is a multi type.
Definition: qgswkbtypes.h:560
Handles storage of information regarding WKB types and their properties.
Definition: qgswkbtypes.h:40
static bool hasZ(Type type)
Tests whether a WKB type contains the z-dimension.
Definition: qgswkbtypes.h:771
static Type dropM(Type type)
Drops the m dimension (if present) for a WKB type and returns the new type.
Definition: qgswkbtypes.h:941
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
static Type addM(Type type)
Adds the m dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:892
static GeometryType geometryType(Type type)
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
Definition: qgswkbtypes.h:666
static Type addZ(Type type)
Adds the z dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:867
static bool isSingleType(Type type)
Returns true if the WKB type is a single type.
Definition: qgswkbtypes.h:550
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:139
static Type dropZ(Type type)
Drops the z dimension (if present) for a WKB type and returns the new type.
Definition: qgswkbtypes.h:923
static bool isCurvedType(Type type)
Returns true if the WKB type is a curved type or can contain curved geometries.
Definition: qgswkbtypes.h:609
static Type zmType(Type type, bool hasZ, bool hasM)
Returns the modified input geometry type according to hasZ / hasM.
Definition: qgswkbtypes.h:529
static bool hasM(Type type)
Tests whether a WKB type contains m values.
Definition: qgswkbtypes.h:821
static Type flatType(Type type)
Returns the flat type for a WKB type.
Definition: qgswkbtypes.h:430
static int coordDimensions(Type type)
Returns the coordinate dimension of the geometry type as an integer.
Definition: qgswkbtypes.h:653