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