QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
459  static Type zmType( Type type, bool hasZ, bool hasM )
460  {
461  type = flatType( type );
462  if ( hasZ )
463  type = static_cast<QgsWKBTypes::Type>( static_cast<quint32>( type ) + 1000 );
464  if ( hasM )
465  type = static_cast<QgsWKBTypes::Type>( static_cast<quint32>( type ) + 2000 );
466  return type;
467  }
468 
472  static Type parseType( const QString& wktStr );
473 
478  static bool isSingleType( Type type )
479  {
480  return ( type != Unknown && !isMultiType( type ) );
481  }
482 
487  static bool isMultiType( Type type )
488  {
489  switch ( type )
490  {
491  case Unknown:
492  case Point:
493  case LineString:
494  case Polygon:
495  case CircularString:
496  case CompoundCurve:
497  case CurvePolygon:
498  case NoGeometry:
499  case PointZ:
500  case LineStringZ:
501  case PolygonZ:
502  case CircularStringZ:
503  case CompoundCurveZ:
504  case CurvePolygonZ:
505  case PointM:
506  case LineStringM:
507  case PolygonM:
508  case CircularStringM:
509  case CompoundCurveM:
510  case CurvePolygonM:
511  case PointZM:
512  case LineStringZM:
513  case PolygonZM:
514  case CircularStringZM:
515  case CompoundCurveZM:
516  case CurvePolygonZM:
517  case Point25D:
518  case LineString25D:
519  case Polygon25D:
520  return false;
521 
522  default:
523  return true;
524 
525  }
526  }
527 
531  static bool isCurvedType( Type type )
532  {
533  switch ( flatType( type ) )
534  {
535  case CircularString:
536  case CompoundCurve:
537  case CurvePolygon:
538  case MultiCurve:
539  case MultiSurface:
540  return true;
541 
542  default:
543  return false;
544  }
545  }
546 
553  static int wkbDimensions( Type type )
554  {
555  GeometryType gtype = geometryType( type );
556  switch ( gtype )
557  {
558  case LineGeometry:
559  return 1;
560  case PolygonGeometry:
561  return 2;
562  default: //point, no geometry, unknown geometry
563  return 0;
564  }
565  }
566 
573  static int coordDimensions( Type type )
574  {
575  if ( type == Unknown || type == NoGeometry )
576  return 0;
577 
578  return 2 + hasZ( type ) + hasM( type );
579  }
580 
585  {
586  switch ( type )
587  {
588  case Unknown:
589  case GeometryCollection:
590  case GeometryCollectionZ:
591  case GeometryCollectionM:
592  case GeometryCollectionZM:
593  return UnknownGeometry;
594 
595  case Point:
596  case MultiPoint:
597  case PointZ:
598  case MultiPointZ:
599  case PointM:
600  case MultiPointM:
601  case PointZM:
602  case MultiPointZM:
603  case Point25D:
604  case MultiPoint25D:
605  return PointGeometry;
606 
607  case LineString:
608  case MultiLineString:
609  case LineStringZ:
610  case MultiLineStringZ:
611  case LineStringM:
612  case MultiLineStringM:
613  case LineStringZM:
614  case MultiLineStringZM:
615  case LineString25D:
616  case MultiLineString25D:
617  case CircularString:
618  case CompoundCurve:
619  case MultiCurve:
620  case CircularStringZ:
621  case CompoundCurveZ:
622  case MultiCurveZ:
623  case CircularStringM:
624  case CompoundCurveM:
625  case MultiCurveM:
626  case CircularStringZM:
627  case CompoundCurveZM:
628  case MultiCurveZM:
629  return LineGeometry;
630 
631  case Polygon:
632  case MultiPolygon:
633  case PolygonZ:
634  case MultiPolygonZ:
635  case PolygonM:
636  case MultiPolygonM:
637  case PolygonZM:
638  case MultiPolygonZM:
639  case Polygon25D:
640  case MultiPolygon25D:
641  case CurvePolygon:
642  case MultiSurface:
643  case CurvePolygonZ:
644  case MultiSurfaceZ:
645  case CurvePolygonM:
646  case MultiSurfaceM:
647  case CurvePolygonZM:
648  case MultiSurfaceZM:
649  return PolygonGeometry;
650 
651  case NoGeometry:
652  return NullGeometry;
653  }
654 
655  return UnknownGeometry;
656  }
657 
660  static QString displayString( Type type );
661 
667  static bool hasZ( Type type )
668  {
669  switch ( type )
670  {
671  case PointZ:
672  case LineStringZ:
673  case PolygonZ:
674  case MultiPointZ:
675  case MultiLineStringZ:
676  case MultiPolygonZ:
677  case GeometryCollectionZ:
678  case CircularStringZ:
679  case CompoundCurveZ:
680  case CurvePolygonZ:
681  case MultiCurveZ:
682  case MultiSurfaceZ:
683  case PointZM:
684  case LineStringZM:
685  case PolygonZM:
686  case MultiPointZM:
687  case MultiLineStringZM:
688  case MultiPolygonZM:
689  case GeometryCollectionZM:
690  case CircularStringZM:
691  case CompoundCurveZM:
692  case CurvePolygonZM:
693  case MultiCurveZM:
694  case MultiSurfaceZM:
695  case Point25D:
696  case LineString25D:
697  case Polygon25D:
698  case MultiPoint25D:
699  case MultiLineString25D:
700  case MultiPolygon25D:
701  return true;
702 
703  default:
704  return false;
705 
706  }
707  }
708 
714  static bool hasM( Type type )
715  {
716  switch ( type )
717  {
718  case PointM:
719  case LineStringM:
720  case PolygonM:
721  case MultiPointM:
722  case MultiLineStringM:
723  case MultiPolygonM:
724  case GeometryCollectionM:
725  case CircularStringM:
726  case CompoundCurveM:
727  case CurvePolygonM:
728  case MultiCurveM:
729  case MultiSurfaceM:
730  case PointZM:
731  case LineStringZM:
732  case PolygonZM:
733  case MultiPointZM:
734  case MultiLineStringZM:
735  case MultiPolygonZM:
736  case GeometryCollectionZM:
737  case CircularStringZM:
738  case CompoundCurveZM:
739  case CurvePolygonZM:
740  case MultiCurveZM:
741  case MultiSurfaceZM:
742  return true;
743 
744  default:
745  return false;
746 
747  }
748  }
749 
757  static Type addZ( Type type )
758  {
759  if ( hasZ( type ) )
760  return type;
761  else if ( type == Unknown )
762  return Unknown;
763  else if ( type == NoGeometry )
764  return NoGeometry;
765 
766  //upgrade with z dimension
767  Type flat = flatType( type );
768  if ( hasM( type ) )
769  return static_cast< QgsWKBTypes::Type >( flat + 3000 );
770  else
771  return static_cast< QgsWKBTypes::Type >( flat + 1000 );
772  }
773 
781  static Type addM( Type type )
782  {
783  if ( hasM( type ) )
784  return type;
785  else if ( type == Unknown )
786  return Unknown;
787  else if ( type == NoGeometry )
788  return NoGeometry;
789  else if ( type == Point25D ||
790  type == LineString25D ||
791  type == Polygon25D ||
792  type == MultiPoint25D ||
793  type == MultiLineString25D ||
794  type == MultiPolygon25D )
795  return type; //can't add M dimension to these types
796 
797  //upgrade with m dimension
798  Type flat = flatType( type );
799  if ( hasZ( type ) )
800  return static_cast< QgsWKBTypes::Type >( flat + 3000 );
801  else
802  return static_cast< QgsWKBTypes::Type >( flat + 2000 );
803  }
804 
811  static Type dropZ( Type type )
812  {
813  if ( !hasZ( type ) )
814  return type;
815 
816  QgsWKBTypes::Type returnType = flatType( type );
817  if ( hasM( type ) )
818  returnType = addM( returnType );
819  return returnType;
820  }
821 
828  static Type dropM( Type type )
829  {
830  if ( !hasM( type ) )
831  return type;
832 
833  QgsWKBTypes::Type returnType = flatType( type );
834  if ( hasZ( type ) )
835  returnType = addZ( returnType );
836  return returnType;
837  }
838 
844  static Type to25D( Type type )
845  {
846  QgsWKBTypes::Type flat = flatType( type );
847 
848  if ( flat >= Point && flat <= MultiPolygon )
849  return static_cast< QgsWKBTypes::Type >( flat + 0x80000000 );
850  else if ( type == QgsWKBTypes::NoGeometry )
852  else
853  return Unknown;
854  }
855 
856  private:
857 
858  struct wkbEntry
859  {
860  wkbEntry( const QString& name, bool isMultiType, Type multiType, Type singleType, Type flatType, GeometryType geometryType,
861  bool hasZ, bool hasM )
862  : mName( name )
863  , mIsMultiType( isMultiType )
864  , mMultiType( multiType )
865  , mSingleType( singleType )
866  , mFlatType( flatType )
867  , mGeometryType( geometryType )
868  , mHasZ( hasZ )
869  , mHasM( hasM )
870  {}
871  QString mName;
872  bool mIsMultiType;
873  Type mMultiType;
874  Type mSingleType;
875  Type mFlatType;
876  GeometryType mGeometryType;
877  bool mHasZ;
878  bool mHasM;
879  };
880 
881  static QMap<Type, wkbEntry> registerTypes();
882  static QMap<Type, wkbEntry>* entries();
883 };
884 
885 #endif // QGSWKBTYPES_H
static int coordDimensions(Type type)
Returns the coordinate dimension of the geometry type as an integer.
Definition: qgswkbtypes.h:573
static bool isSingleType(Type type)
Returns true if the WKB type is a single type.
Definition: qgswkbtypes.h:478
static Type addZ(Type type)
Adds the z dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:757
static bool hasM(Type type)
Tests whether a WKB type contains m values.
Definition: qgswkbtypes.h:714
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:487
static bool hasZ(Type type)
Tests whether a WKB type contains the z-dimension.
Definition: qgswkbtypes.h:667
static Type dropZ(Type type)
Drops the z dimension (if present) for a WKB type and returns the new type.
Definition: qgswkbtypes.h:811
static Type addM(Type type)
Adds the m dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:781
static bool isCurvedType(Type type)
Returns true if the WKB type is a curved type or can contain curved geometries.
Definition: qgswkbtypes.h:531
static Type zmType(Type type, bool hasZ, bool hasM)
Returns the modified input geometry type according to hasZ / hasM.
Definition: qgswkbtypes.h:459
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:584
static Type dropM(Type type)
Drops the m dimension (if present) for a WKB type and returns the new type.
Definition: qgswkbtypes.h:828
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:844
static int wkbDimensions(Type type)
Returns the inherent dimension of the geometry type as an integer.
Definition: qgswkbtypes.h:553
static Type singleType(Type type)
Returns the single type for a WKB type.
Definition: qgswkbtypes.h:114