Skip to content

Commit 6653796

Browse files
committed
Add method to QgsWKBTypes to add z/m dimension to a wkb type
Also add some unit tests for QgsWKBTypes
1 parent 53c507d commit 6653796

File tree

4 files changed

+386
-47
lines changed

4 files changed

+386
-47
lines changed

python/core/geometry/qgswkbtypes.sip

Lines changed: 82 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,53 +8,57 @@ class QgsWKBTypes
88

99
enum Type
1010
{
11-
Unknown = 0,
12-
Point = 1,
13-
LineString = 2,
14-
Polygon = 3,
15-
MultiPoint = 4,
16-
MultiLineString = 5,
17-
MultiPolygon = 6,
18-
CircularString = 8,
19-
CompoundCurve = 9,
20-
CurvePolygon = 10, //13, //should be 10. Seems to be correct in newer postgis versions
21-
MultiCurve = 11,
22-
MultiSurface = 12,
23-
NoGeometry = 100, //attributes only
24-
PointZ = 1001,
25-
LineStringZ = 1002,
26-
PolygonZ = 1003,
27-
MultiPointZ = 1004,
28-
MultiLineStringZ = 1005,
29-
MultiPolygonZ = 1006,
30-
CircularStringZ = 1008,
31-
CompoundCurveZ = 1009,
32-
CurvePolygonZ = 1010,
33-
MultiCurveZ = 1011,
34-
MultiSurfaceZ = 1012,
35-
PointM = 2001,
36-
LineStringM = 2002,
37-
PolygonM = 2003,
38-
MultiPointM = 2004,
39-
MultiLineStringM = 2005,
40-
MultiPolygonM = 2006,
41-
CircularStringM = 2008,
42-
CompoundCurveM = 2009,
43-
CurvePolygonM = 2010,
44-
MultiCurveM = 2011,
45-
MultiSurfaceM = 2012,
46-
PointZM = 3001,
47-
LineStringZM = 3002,
48-
PolygonZM = 3003,
49-
MultiPointZM = 3004,
50-
MultiLineStringZM = 3005,
51-
MultiPolygonZM = 3006,
52-
CircularStringZM = 3008,
53-
CompoundCurveZM = 3009,
54-
CurvePolygonZM = 3010,
55-
MultiCurveZM = 3011,
56-
MultiSurfaceZM = 3012,
57-
Point25D = 0x80000001,
11+
Unknown,
12+
Point,
13+
LineString,
14+
Polygon,
15+
MultiPoint,
16+
MultiLineString,
17+
MultiPolygon,
18+
GeometryCollection,
19+
CircularString,
20+
CompoundCurve,
21+
CurvePolygon,
22+
MultiCurve,
23+
MultiSurface,
24+
NoGeometry,
25+
PointZ,
26+
LineStringZ,
27+
PolygonZ,
28+
MultiPointZ,
29+
MultiLineStringZ,
30+
MultiPolygonZ,
31+
GeometryCollectionZ,
32+
CircularStringZ,
33+
CompoundCurveZ,
34+
CurvePolygonZ,
35+
MultiCurveZ,
36+
MultiSurfaceZ,
37+
PointM,
38+
LineStringM,
39+
PolygonM,
40+
MultiPointM,
41+
MultiLineStringM,
42+
MultiPolygonM,
43+
GeometryCollectionM,
44+
CircularStringM,
45+
CompoundCurveM,
46+
CurvePolygonM,
47+
MultiCurveM,
48+
MultiSurfaceM,
49+
PointZM,
50+
LineStringZM,
51+
PolygonZM,
52+
MultiPointZM,
53+
MultiLineStringZM,
54+
MultiPolygonZM,
55+
GeometryCollectionZM,
56+
CircularStringZM,
57+
CompoundCurveZM,
58+
CurvePolygonZM,
59+
MultiCurveZM,
60+
MultiSurfaceZM,
61+
Point25D,
5862
LineString25D,
5963
Polygon25D,
6064
MultiPoint25D,
@@ -90,4 +94,35 @@ class QgsWKBTypes
9094
static bool isMultiType( Type type );
9195
static int wkbDimensions( Type type );
9296
static GeometryType geometryType( Type type );
97+
static QString displayString( Type type );
98+
99+
/** Tests whether a WKB type contains the z-dimension.
100+
* @returns true if type has z values
101+
* @see addZ()
102+
* @see hasM()
103+
*/
104+
static bool hasZ( Type type );
105+
106+
/** Tests whether a WKB type contains m values.
107+
* @returns true if type has m values
108+
* @see addM()
109+
* @see hasZ()
110+
*/
111+
static bool hasM( Type type );
112+
113+
/** Adds the z dimension to a WKB type and returns the new type
114+
* @param type original type
115+
* @note added in QGIS 2.12
116+
* @see addM()
117+
* @see hasZ()
118+
*/
119+
static Type addZ( Type type );
120+
121+
/** Adds the m dimension to a WKB type and returns the new type
122+
* @param type original type
123+
* @note added in QGIS 2.12
124+
* @see addZ()
125+
* @see hasM()
126+
*/
127+
static Type addM( Type type );
93128
};

src/core/geometry/qgswkbtypes.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,47 @@ bool QgsWKBTypes::hasM( Type type )
136136
return it->mHasM;
137137
}
138138

139+
QgsWKBTypes::Type QgsWKBTypes::addZ( QgsWKBTypes::Type type )
140+
{
141+
if ( hasZ( type ) )
142+
return type;
143+
else if ( type == Unknown )
144+
return Unknown;
145+
else if ( type == NoGeometry )
146+
return NoGeometry;
147+
148+
//upgrade with z dimension
149+
Type flat = flatType( type );
150+
if ( hasM( type ) )
151+
return ( QgsWKBTypes::Type )( flat + 3000 );
152+
else
153+
return ( QgsWKBTypes::Type )( flat + 1000 );
154+
}
155+
156+
QgsWKBTypes::Type QgsWKBTypes::addM( QgsWKBTypes::Type type )
157+
{
158+
if ( hasM( type ) )
159+
return type;
160+
else if ( type == Unknown )
161+
return Unknown;
162+
else if ( type == NoGeometry )
163+
return NoGeometry;
164+
else if ( type == Point25D ||
165+
type == LineString25D ||
166+
type == Polygon25D ||
167+
type == MultiPoint25D ||
168+
type == MultiLineString25D ||
169+
type == MultiPolygon25D )
170+
return type; //can't add M dimension to these types
171+
172+
//upgrade with m dimension
173+
Type flat = flatType( type );
174+
if ( hasZ( type ) )
175+
return ( QgsWKBTypes::Type )( flat + 3000 );
176+
else
177+
return ( QgsWKBTypes::Type )( flat + 2000 );
178+
}
179+
139180
QMap<QgsWKBTypes::Type, QgsWKBTypes::wkbEntry> QgsWKBTypes::registerTypes()
140181
{
141182
QMap<QgsWKBTypes::Type, QgsWKBTypes::wkbEntry> entries;

src/core/geometry/qgswkbtypes.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,37 @@ class CORE_EXPORT QgsWKBTypes
120120
static int wkbDimensions( Type type );
121121
static GeometryType geometryType( Type type );
122122
static QString displayString( Type type );
123+
124+
/** Tests whether a WKB type contains the z-dimension.
125+
* @returns true if type has z values
126+
* @see addZ()
127+
* @see hasM()
128+
*/
123129
static bool hasZ( Type type );
130+
131+
/** Tests whether a WKB type contains m values.
132+
* @returns true if type has m values
133+
* @see addM()
134+
* @see hasZ()
135+
*/
124136
static bool hasM( Type type );
125137

138+
/** Adds the z dimension to a WKB type and returns the new type
139+
* @param type original type
140+
* @note added in QGIS 2.12
141+
* @see addM()
142+
* @see hasZ()
143+
*/
144+
static Type addZ( Type type );
145+
146+
/** Adds the m dimension to a WKB type and returns the new type
147+
* @param type original type
148+
* @note added in QGIS 2.12
149+
* @see addZ()
150+
* @see hasM()
151+
*/
152+
static Type addM( Type type );
153+
126154
private:
127155
static QMap<Type, wkbEntry> registerTypes();
128156
static QMap<Type, wkbEntry>* entries();

0 commit comments

Comments
 (0)