QGIS API Documentation  3.6.0-Noosa (5873452)
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 
139  {
144  NullGeometry
145  };
146  Q_ENUM( GeometryType )
147 
148 
154  static Type singleType( Type type )
155  {
156  switch ( type )
157  {
158  case Unknown:
159  case GeometryCollection:
160  case GeometryCollectionZ:
161  case GeometryCollectionM:
162  case GeometryCollectionZM:
163  return Unknown;
164 
165  case Point:
166  case MultiPoint:
167  return Point;
168 
169  case PointZ:
170  case MultiPointZ:
171  return PointZ;
172 
173  case PointM:
174  case MultiPointM:
175  return PointM;
176 
177  case PointZM:
178  case MultiPointZM:
179  return PointZM;
180 
181  case LineString:
182  case MultiLineString:
183  return LineString;
184 
185  case LineStringZ:
186  case MultiLineStringZ:
187  return LineStringZ;
188 
189  case LineStringM:
190  case MultiLineStringM:
191  return LineStringM;
192 
193  case LineStringZM:
194  case MultiLineStringZM:
195  return LineStringZM;
196 
197  case Polygon:
198  case MultiPolygon:
199  return Polygon;
200 
201  case PolygonZ:
202  case MultiPolygonZ:
203  return PolygonZ;
204 
205  case PolygonM:
206  case MultiPolygonM:
207  return PolygonM;
208 
209  case PolygonZM:
210  case MultiPolygonZM:
211  return PolygonZM;
212 
213  case Triangle:
214  // case MultiTriangle:
215  return Triangle;
216 
217  case TriangleZ:
218  // case MultiTriangleZ:
219  return TriangleZ;
220 
221  case TriangleM:
222  // case MultiTriangleM:
223  return TriangleM;
224 
225  case TriangleZM:
226  // case MultiTriangleZM:
227  return TriangleZM;
228 
229  case CircularString:
230  return CircularString;
231 
232  case CircularStringZ:
233  return CircularStringZ;
234 
235  case CircularStringM:
236  return CircularStringM;
237 
238  case CircularStringZM:
239  return CircularStringZM;
240 
241  case CompoundCurve:
242  case MultiCurve:
243  return CompoundCurve;
244 
245  case CompoundCurveZ:
246  case MultiCurveZ:
247  return CompoundCurveZ;
248 
249  case CompoundCurveM:
250  case MultiCurveM:
251  return CompoundCurveM;
252 
253  case CompoundCurveZM:
254  case MultiCurveZM:
255  return CompoundCurveZM;
256 
257  case CurvePolygon:
258  case MultiSurface:
259  return CurvePolygon;
260 
261  case CurvePolygonZ:
262  case MultiSurfaceZ:
263  return CurvePolygonZ;
264 
265  case CurvePolygonM:
266  case MultiSurfaceM:
267  return CurvePolygonM;
268 
269  case CurvePolygonZM:
270  case MultiSurfaceZM:
271  return CurvePolygonZM;
272 
273  case NoGeometry:
274  return NoGeometry;
275 
276  case Point25D:
277  case MultiPoint25D:
278  return Point25D;
279 
280  case LineString25D:
281  case MultiLineString25D:
282  return LineString25D;
283 
284  case Polygon25D:
285  case MultiPolygon25D:
286  return Polygon25D;
287 
288  }
289  return Unknown;
290  }
291 
298  static Type multiType( Type type )
299  {
300  switch ( type )
301  {
302  case Unknown:
303  case Triangle:
304  case TriangleZ:
305  case TriangleM:
306  case TriangleZM:
307  return Unknown;
308 
309  case GeometryCollection:
310  return GeometryCollection;
311 
312  case GeometryCollectionZ:
313  return GeometryCollectionZ;
314 
315  case GeometryCollectionM:
316  return GeometryCollectionM;
317 
318  case GeometryCollectionZM:
319  return GeometryCollectionZM;
320 
321  case Point:
322  case MultiPoint:
323  return MultiPoint;
324 
325  case PointZ:
326  case MultiPointZ:
327  return MultiPointZ;
328 
329  case PointM:
330  case MultiPointM:
331  return MultiPointM;
332 
333  case PointZM:
334  case MultiPointZM:
335  return MultiPointZM;
336 
337  case LineString:
338  case MultiLineString:
339  return MultiLineString;
340 
341  case LineStringZ:
342  case MultiLineStringZ:
343  return MultiLineStringZ;
344 
345  case LineStringM:
346  case MultiLineStringM:
347  return MultiLineStringM;
348 
349  case LineStringZM:
350  case MultiLineStringZM:
351  return MultiLineStringZM;
352 
353  case Polygon:
354  case MultiPolygon:
355  return MultiPolygon;
356 
357  case PolygonZ:
358  case MultiPolygonZ:
359  return MultiPolygonZ;
360 
361  case PolygonM:
362  case MultiPolygonM:
363  return MultiPolygonM;
364 
365  case PolygonZM:
366  case MultiPolygonZM:
367  return MultiPolygonZM;
368 
369  case CompoundCurve:
370  case CircularString:
371  case MultiCurve:
372  return MultiCurve;
373 
374  case CompoundCurveZ:
375  case CircularStringZ:
376  case MultiCurveZ:
377  return MultiCurveZ;
378 
379  case CompoundCurveM:
380  case CircularStringM:
381  case MultiCurveM:
382  return MultiCurveM;
383 
384  case CompoundCurveZM:
385  case CircularStringZM:
386  case MultiCurveZM:
387  return MultiCurveZM;
388 
389  case CurvePolygon:
390  case MultiSurface:
391  return MultiSurface;
392 
393  case CurvePolygonZ:
394  case MultiSurfaceZ:
395  return MultiSurfaceZ;
396 
397  case CurvePolygonM:
398  case MultiSurfaceM:
399  return MultiSurfaceM;
400 
401  case CurvePolygonZM:
402  case MultiSurfaceZM:
403  return MultiSurfaceZM;
404 
405  case NoGeometry:
406  return NoGeometry;
407 
408  case Point25D:
409  case MultiPoint25D:
410  return MultiPoint25D;
411 
412  case LineString25D:
413  case MultiLineString25D:
414  return MultiLineString25D;
415 
416  case Polygon25D:
417  case MultiPolygon25D:
418  return MultiPolygon25D;
419  }
420  return Unknown;
421  }
422 
429  static Type flatType( Type type )
430  {
431  switch ( type )
432  {
433  case Unknown:
434  return Unknown;
435 
436  case Point:
437  case PointZ:
438  case PointM:
439  case PointZM:
440  case Point25D:
441  return Point;
442 
443  case LineString:
444  case LineStringZ:
445  case LineStringM:
446  case LineStringZM:
447  case LineString25D:
448  return LineString;
449 
450  case Polygon:
451  case PolygonZ:
452  case PolygonM:
453  case PolygonZM:
454  case Polygon25D:
455  return Polygon;
456 
457  case Triangle:
458  case TriangleZ:
459  case TriangleM:
460  case TriangleZM:
461  return Triangle;
462 
463  case MultiPoint:
464  case MultiPointZ:
465  case MultiPointM:
466  case MultiPointZM:
467  case MultiPoint25D:
468  return MultiPoint;
469 
470  case MultiLineString:
471  case MultiLineStringZ:
472  case MultiLineStringM:
473  case MultiLineStringZM:
474  case MultiLineString25D:
475  return MultiLineString;
476 
477  case MultiPolygon:
478  case MultiPolygonZ:
479  case MultiPolygonM:
480  case MultiPolygonZM:
481  case MultiPolygon25D:
482  return MultiPolygon;
483 
484  case GeometryCollection:
485  case GeometryCollectionZ:
486  case GeometryCollectionM:
487  case GeometryCollectionZM:
488  return GeometryCollection;
489 
490  case CircularString:
491  case CircularStringZ:
492  case CircularStringM:
493  case CircularStringZM:
494  return CircularString;
495 
496  case CompoundCurve:
497  case CompoundCurveZ:
498  case CompoundCurveM:
499  case CompoundCurveZM:
500  return CompoundCurve;
501 
502  case MultiCurve:
503  case MultiCurveZ:
504  case MultiCurveM:
505  case MultiCurveZM:
506  return MultiCurve;
507 
508  case CurvePolygon:
509  case CurvePolygonZ:
510  case CurvePolygonM:
511  case CurvePolygonZM:
512  return CurvePolygon;
513 
514  case MultiSurface:
515  case MultiSurfaceZ:
516  case MultiSurfaceM:
517  case MultiSurfaceZM:
518  return MultiSurface;
519 
520  case NoGeometry:
521  return NoGeometry;
522 
523  }
524  return Unknown;
525  }
526 
528  static Type zmType( Type type, bool hasZ, bool hasM )
529  {
530  type = flatType( type );
531  if ( hasZ )
532  type = static_cast<QgsWkbTypes::Type>( static_cast<quint32>( type ) + 1000 );
533  if ( hasM )
534  type = static_cast<QgsWkbTypes::Type>( static_cast<quint32>( type ) + 2000 );
535  return type;
536  }
537 
542  static Type parseType( const QString &wktStr );
543 
549  static bool isSingleType( Type type )
550  {
551  return ( type != Unknown && !isMultiType( type ) );
552  }
553 
559  static bool isMultiType( Type type )
560  {
561  switch ( type )
562  {
563  case Unknown:
564  case Point:
565  case LineString:
566  case Polygon:
567  case Triangle:
568  case CircularString:
569  case CompoundCurve:
570  case CurvePolygon:
571  case NoGeometry:
572  case PointZ:
573  case LineStringZ:
574  case PolygonZ:
575  case TriangleZ:
576  case CircularStringZ:
577  case CompoundCurveZ:
578  case CurvePolygonZ:
579  case PointM:
580  case LineStringM:
581  case PolygonM:
582  case TriangleM:
583  case CircularStringM:
584  case CompoundCurveM:
585  case CurvePolygonM:
586  case PointZM:
587  case LineStringZM:
588  case PolygonZM:
589  case TriangleZM:
590  case CircularStringZM:
591  case CompoundCurveZM:
592  case CurvePolygonZM:
593  case Point25D:
594  case LineString25D:
595  case Polygon25D:
596  return false;
597 
598  default:
599  return true;
600 
601  }
602  }
603 
608  static bool isCurvedType( Type type )
609  {
610  switch ( flatType( type ) )
611  {
612  case CircularString:
613  case CompoundCurve:
614  case CurvePolygon:
615  case MultiCurve:
616  case MultiSurface:
617  return true;
618 
619  default:
620  return false;
621  }
622  }
623 
631  static int wkbDimensions( Type type )
632  {
633  GeometryType gtype = geometryType( type );
634  switch ( gtype )
635  {
636  case LineGeometry:
637  return 1;
638  case PolygonGeometry:
639  return 2;
640  default: //point, no geometry, unknown geometry
641  return 0;
642  }
643  }
644 
652  static int coordDimensions( Type type )
653  {
654  if ( type == Unknown || type == NoGeometry )
655  return 0;
656 
657  return 2 + hasZ( type ) + hasM( type );
658  }
659 
666  {
667  switch ( type )
668  {
669  case Unknown:
670  case GeometryCollection:
671  case GeometryCollectionZ:
672  case GeometryCollectionM:
673  case GeometryCollectionZM:
674  return UnknownGeometry;
675 
676  case Point:
677  case MultiPoint:
678  case PointZ:
679  case MultiPointZ:
680  case PointM:
681  case MultiPointM:
682  case PointZM:
683  case MultiPointZM:
684  case Point25D:
685  case MultiPoint25D:
686  return PointGeometry;
687 
688  case LineString:
689  case MultiLineString:
690  case LineStringZ:
691  case MultiLineStringZ:
692  case LineStringM:
693  case MultiLineStringM:
694  case LineStringZM:
695  case MultiLineStringZM:
696  case LineString25D:
697  case MultiLineString25D:
698  case CircularString:
699  case CompoundCurve:
700  case MultiCurve:
701  case CircularStringZ:
702  case CompoundCurveZ:
703  case MultiCurveZ:
704  case CircularStringM:
705  case CompoundCurveM:
706  case MultiCurveM:
707  case CircularStringZM:
708  case CompoundCurveZM:
709  case MultiCurveZM:
710  return LineGeometry;
711 
712  case Polygon:
713  case MultiPolygon:
714  case Triangle:
715  case PolygonZ:
716  case TriangleZ:
717  case MultiPolygonZ:
718  case PolygonM:
719  case TriangleM:
720  case MultiPolygonM:
721  case PolygonZM:
722  case MultiPolygonZM:
723  case TriangleZM:
724  case Polygon25D:
725  case MultiPolygon25D:
726  case CurvePolygon:
727  case MultiSurface:
728  case CurvePolygonZ:
729  case MultiSurfaceZ:
730  case CurvePolygonM:
731  case MultiSurfaceM:
732  case CurvePolygonZM:
733  case MultiSurfaceZM:
734  return PolygonGeometry;
735 
736  case NoGeometry:
737  return NullGeometry;
738  }
739 
740  return UnknownGeometry;
741  }
742 
746  static QString displayString( Type type );
747 
762  static QString geometryDisplayString( GeometryType type );
763 
770  static bool hasZ( Type type )
771  {
772  switch ( type )
773  {
774  case PointZ:
775  case LineStringZ:
776  case PolygonZ:
777  case TriangleZ:
778  case MultiPointZ:
779  case MultiLineStringZ:
780  case MultiPolygonZ:
781  case GeometryCollectionZ:
782  case CircularStringZ:
783  case CompoundCurveZ:
784  case CurvePolygonZ:
785  case MultiCurveZ:
786  case MultiSurfaceZ:
787  case PointZM:
788  case LineStringZM:
789  case PolygonZM:
790  case TriangleZM:
791  case MultiPointZM:
792  case MultiLineStringZM:
793  case MultiPolygonZM:
794  case GeometryCollectionZM:
795  case CircularStringZM:
796  case CompoundCurveZM:
797  case CurvePolygonZM:
798  case MultiCurveZM:
799  case MultiSurfaceZM:
800  case Point25D:
801  case LineString25D:
802  case Polygon25D:
803  case MultiPoint25D:
804  case MultiLineString25D:
805  case MultiPolygon25D:
806  return true;
807 
808  default:
809  return false;
810 
811  }
812  }
813 
820  static bool hasM( Type type )
821  {
822  switch ( type )
823  {
824  case PointM:
825  case LineStringM:
826  case PolygonM:
827  case TriangleM:
828  case MultiPointM:
829  case MultiLineStringM:
830  case MultiPolygonM:
831  case GeometryCollectionM:
832  case CircularStringM:
833  case CompoundCurveM:
834  case CurvePolygonM:
835  case MultiCurveM:
836  case MultiSurfaceM:
837  case PointZM:
838  case LineStringZM:
839  case PolygonZM:
840  case TriangleZM:
841  case MultiPointZM:
842  case MultiLineStringZM:
843  case MultiPolygonZM:
844  case GeometryCollectionZM:
845  case CircularStringZM:
846  case CompoundCurveZM:
847  case CurvePolygonZM:
848  case MultiCurveZM:
849  case MultiSurfaceZM:
850  return true;
851 
852  default:
853  return false;
854 
855  }
856  }
857 
866  static Type addZ( Type type )
867  {
868  if ( hasZ( type ) )
869  return type;
870  else if ( type == Unknown )
871  return Unknown;
872  else if ( type == NoGeometry )
873  return NoGeometry;
874 
875  //upgrade with z dimension
876  Type flat = flatType( type );
877  if ( hasM( type ) )
878  return static_cast< QgsWkbTypes::Type >( flat + 3000 );
879  else
880  return static_cast< QgsWkbTypes::Type >( flat + 1000 );
881  }
882 
891  static Type addM( Type type )
892  {
893  if ( hasM( type ) )
894  return type;
895  else if ( type == Unknown )
896  return Unknown;
897  else if ( type == NoGeometry )
898  return NoGeometry;
899  else if ( type == Point25D ||
900  type == LineString25D ||
901  type == Polygon25D ||
902  type == MultiPoint25D ||
903  type == MultiLineString25D ||
904  type == MultiPolygon25D )
905  return type; //can't add M dimension to these types
906 
907  //upgrade with m dimension
908  Type flat = flatType( type );
909  if ( hasZ( type ) )
910  return static_cast< QgsWkbTypes::Type >( flat + 3000 );
911  else
912  return static_cast< QgsWkbTypes::Type >( flat + 2000 );
913  }
914 
922  static Type dropZ( Type type )
923  {
924  if ( !hasZ( type ) )
925  return type;
926 
927  QgsWkbTypes::Type returnType = flatType( type );
928  if ( hasM( type ) )
929  returnType = addM( returnType );
930  return returnType;
931  }
932 
940  static Type dropM( Type type )
941  {
942  if ( !hasM( type ) )
943  return type;
944 
945  QgsWkbTypes::Type returnType = flatType( type );
946  if ( hasZ( type ) )
947  returnType = addZ( returnType );
948  return returnType;
949  }
950 
956  static Type to25D( Type type )
957  {
958  QgsWkbTypes::Type flat = flatType( type );
959 
960  if ( flat >= Point && flat <= MultiPolygon )
961  return static_cast< QgsWkbTypes::Type >( flat + 0x80000000 );
962  else if ( type == QgsWkbTypes::NoGeometry )
964  else
965  return Unknown;
966  }
967 
968  private:
969 
970  struct wkbEntry
971  {
972  wkbEntry( const QString &name, bool isMultiType, Type multiType, Type singleType, Type flatType, GeometryType geometryType,
973  bool hasZ, bool hasM )
974  : mName( name )
975  , mIsMultiType( isMultiType )
976  , mMultiType( multiType )
977  , mSingleType( singleType )
978  , mFlatType( flatType )
979  , mGeometryType( geometryType )
980  , mHasZ( hasZ )
981  , mHasM( hasM )
982  {}
983  QString mName;
984  bool mIsMultiType;
985  Type mMultiType;
986  Type mSingleType;
987  Type mFlatType;
988  GeometryType mGeometryType;
989  bool mHasZ;
990  bool mHasM;
991  };
992 
993  static const QMap<Type, wkbEntry> ENTRIES;
994 };
995 
996 #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:956
static Type multiType(Type type)
Returns the multi type for a WKB type.
Definition: qgswkbtypes.h:298
static int wkbDimensions(Type type)
Returns the inherent dimension of the geometry type as an integer.
Definition: qgswkbtypes.h:631
static bool isMultiType(Type type)
Returns true if the WKB type is a multi type.
Definition: qgswkbtypes.h:559
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:770
static Type dropM(Type type)
Drops the m dimension (if present) for a WKB type and returns the new type.
Definition: qgswkbtypes.h:940
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:891
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:665
static Type addZ(Type type)
Adds the z dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:866
static bool isSingleType(Type type)
Returns true if the WKB type is a single type.
Definition: qgswkbtypes.h:549
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:138
static Type dropZ(Type type)
Drops the z dimension (if present) for a WKB type and returns the new type.
Definition: qgswkbtypes.h:922
static bool isCurvedType(Type type)
Returns true if the WKB type is a curved type or can contain curved geometries.
Definition: qgswkbtypes.h:608
static Type zmType(Type type, bool hasZ, bool hasM)
Returns the modified input geometry type according to hasZ / hasM.
Definition: qgswkbtypes.h:528
static bool hasM(Type type)
Tests whether a WKB type contains m values.
Definition: qgswkbtypes.h:820
static Type flatType(Type type)
Returns the flat type for a WKB type.
Definition: qgswkbtypes.h:429
static int coordDimensions(Type type)
Returns the coordinate dimension of the geometry type as an integer.
Definition: qgswkbtypes.h:652