Skip to content

Commit a3d780c

Browse files
committed
[geometry] Add method to drop z/m from wkb types
1 parent a792a47 commit a3d780c

File tree

4 files changed

+171
-0
lines changed

4 files changed

+171
-0
lines changed

python/core/geometry/qgswkbtypes.sip

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,20 @@ class QgsWKBTypes
167167
* @see hasM()
168168
*/
169169
static Type addM( Type type );
170+
171+
/** Drops the z dimension (if present) for a WKB type and returns the new type.
172+
* @param type original type
173+
* @note added in QGIS 2.14
174+
* @see dropM()
175+
* @see addZ()
176+
*/
177+
static Type dropZ( Type type );
178+
179+
/** Drops the m dimension (if present) for a WKB type and returns the new type.
180+
* @param type original type
181+
* @note added in QGIS 2.14
182+
* @see dropZ()
183+
* @see addM()
184+
*/
185+
static Type dropM( Type type );
170186
};

src/core/geometry/qgswkbtypes.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,28 @@ QgsWKBTypes::Type QgsWKBTypes::addM( QgsWKBTypes::Type type )
214214
return ( QgsWKBTypes::Type )( flat + 2000 );
215215
}
216216

217+
QgsWKBTypes::Type QgsWKBTypes::dropZ( QgsWKBTypes::Type type )
218+
{
219+
if ( !hasZ( type ) )
220+
return type;
221+
222+
QgsWKBTypes::Type returnType = flatType( type );
223+
if ( hasM( type ) )
224+
returnType = addM( returnType );
225+
return returnType;
226+
}
227+
228+
QgsWKBTypes::Type QgsWKBTypes::dropM( QgsWKBTypes::Type type )
229+
{
230+
if ( !hasM( type ) )
231+
return type;
232+
233+
QgsWKBTypes::Type returnType = flatType( type );
234+
if ( hasZ( type ) )
235+
returnType = addZ( returnType );
236+
return returnType;
237+
}
238+
217239
/***************************************************************************
218240
* This class is considered CRITICAL and any change MUST be accompanied with
219241
* full unit tests.

src/core/geometry/qgswkbtypes.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ class CORE_EXPORT QgsWKBTypes
187187
* @param type original type
188188
* @note added in QGIS 2.12
189189
* @see addM()
190+
* @see dropZ()
190191
* @see hasZ()
191192
*/
192193
static Type addZ( Type type );
@@ -195,10 +196,27 @@ class CORE_EXPORT QgsWKBTypes
195196
* @param type original type
196197
* @note added in QGIS 2.12
197198
* @see addZ()
199+
* @see dropM()
198200
* @see hasM()
199201
*/
200202
static Type addM( Type type );
201203

204+
/** Drops the z dimension (if present) for a WKB type and returns the new type.
205+
* @param type original type
206+
* @note added in QGIS 2.14
207+
* @see dropM()
208+
* @see addZ()
209+
*/
210+
static Type dropZ( Type type );
211+
212+
/** Drops the m dimension (if present) for a WKB type and returns the new type.
213+
* @param type original type
214+
* @note added in QGIS 2.14
215+
* @see dropZ()
216+
* @see addM()
217+
*/
218+
static Type dropM( Type type );
219+
202220
private:
203221

204222
struct wkbEntry

tests/src/python/test_qgsgeometry.py

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2480,6 +2480,121 @@ def testWkbTypes(self):
24802480
assert QgsWKBTypes.addM(QgsWKBTypes.MultiLineString25D) == QgsWKBTypes.MultiLineString25D
24812481
assert QgsWKBTypes.addM(QgsWKBTypes.MultiPolygon25D) == QgsWKBTypes.MultiPolygon25D
24822482

2483+
# test dropping z dimension from types
2484+
assert QgsWKBTypes.dropZ(QgsWKBTypes.Unknown) == QgsWKBTypes.Unknown
2485+
assert QgsWKBTypes.dropZ(QgsWKBTypes.Point) == QgsWKBTypes.Point
2486+
assert QgsWKBTypes.dropZ(QgsWKBTypes.PointZ) == QgsWKBTypes.Point
2487+
assert QgsWKBTypes.dropZ(QgsWKBTypes.PointM) == QgsWKBTypes.PointM
2488+
assert QgsWKBTypes.dropZ(QgsWKBTypes.PointZM) == QgsWKBTypes.PointM
2489+
assert QgsWKBTypes.dropZ(QgsWKBTypes.MultiPoint) == QgsWKBTypes.MultiPoint
2490+
assert QgsWKBTypes.dropZ(QgsWKBTypes.MultiPointZ) == QgsWKBTypes.MultiPoint
2491+
assert QgsWKBTypes.dropZ(QgsWKBTypes.MultiPointM) == QgsWKBTypes.MultiPointM
2492+
assert QgsWKBTypes.dropZ(QgsWKBTypes.MultiPointZM) == QgsWKBTypes.MultiPointM
2493+
assert QgsWKBTypes.dropZ(QgsWKBTypes.LineString) == QgsWKBTypes.LineString
2494+
assert QgsWKBTypes.dropZ(QgsWKBTypes.LineStringZ) == QgsWKBTypes.LineString
2495+
assert QgsWKBTypes.dropZ(QgsWKBTypes.LineStringM) == QgsWKBTypes.LineStringM
2496+
assert QgsWKBTypes.dropZ(QgsWKBTypes.LineStringZM) == QgsWKBTypes.LineStringM
2497+
assert QgsWKBTypes.dropZ(QgsWKBTypes.MultiLineString) == QgsWKBTypes.MultiLineString
2498+
assert QgsWKBTypes.dropZ(QgsWKBTypes.MultiLineStringZ) == QgsWKBTypes.MultiLineString
2499+
assert QgsWKBTypes.dropZ(QgsWKBTypes.MultiLineStringM) == QgsWKBTypes.MultiLineStringM
2500+
assert QgsWKBTypes.dropZ(QgsWKBTypes.MultiLineStringZM) == QgsWKBTypes.MultiLineStringM
2501+
assert QgsWKBTypes.dropZ(QgsWKBTypes.Polygon) == QgsWKBTypes.Polygon
2502+
assert QgsWKBTypes.dropZ(QgsWKBTypes.PolygonZ) == QgsWKBTypes.Polygon
2503+
assert QgsWKBTypes.dropZ(QgsWKBTypes.PolygonM) == QgsWKBTypes.PolygonM
2504+
assert QgsWKBTypes.dropZ(QgsWKBTypes.PolygonZM) == QgsWKBTypes.PolygonM
2505+
assert QgsWKBTypes.dropZ(QgsWKBTypes.MultiPolygon) == QgsWKBTypes.MultiPolygon
2506+
assert QgsWKBTypes.dropZ(QgsWKBTypes.MultiPolygonZ) == QgsWKBTypes.MultiPolygon
2507+
assert QgsWKBTypes.dropZ(QgsWKBTypes.MultiPolygonM) == QgsWKBTypes.MultiPolygonM
2508+
assert QgsWKBTypes.dropZ(QgsWKBTypes.MultiPolygonZM) == QgsWKBTypes.MultiPolygonM
2509+
assert QgsWKBTypes.dropZ(QgsWKBTypes.GeometryCollection) == QgsWKBTypes.GeometryCollection
2510+
assert QgsWKBTypes.dropZ(QgsWKBTypes.GeometryCollectionZ) == QgsWKBTypes.GeometryCollection
2511+
assert QgsWKBTypes.dropZ(QgsWKBTypes.GeometryCollectionM) == QgsWKBTypes.GeometryCollectionM
2512+
assert QgsWKBTypes.dropZ(QgsWKBTypes.GeometryCollectionZM) == QgsWKBTypes.GeometryCollectionM
2513+
assert QgsWKBTypes.dropZ(QgsWKBTypes.CircularString) == QgsWKBTypes.CircularString
2514+
assert QgsWKBTypes.dropZ(QgsWKBTypes.CircularStringZ) == QgsWKBTypes.CircularString
2515+
assert QgsWKBTypes.dropZ(QgsWKBTypes.CircularStringM) == QgsWKBTypes.CircularStringM
2516+
assert QgsWKBTypes.dropZ(QgsWKBTypes.CircularStringZM) == QgsWKBTypes.CircularStringM
2517+
assert QgsWKBTypes.dropZ(QgsWKBTypes.CompoundCurve) == QgsWKBTypes.CompoundCurve
2518+
assert QgsWKBTypes.dropZ(QgsWKBTypes.CompoundCurveZ) == QgsWKBTypes.CompoundCurve
2519+
assert QgsWKBTypes.dropZ(QgsWKBTypes.CompoundCurveM) == QgsWKBTypes.CompoundCurveM
2520+
assert QgsWKBTypes.dropZ(QgsWKBTypes.CompoundCurveZM) == QgsWKBTypes.CompoundCurveM
2521+
assert QgsWKBTypes.dropZ(QgsWKBTypes.CurvePolygon) == QgsWKBTypes.CurvePolygon
2522+
assert QgsWKBTypes.dropZ(QgsWKBTypes.CurvePolygonZ) == QgsWKBTypes.CurvePolygon
2523+
assert QgsWKBTypes.dropZ(QgsWKBTypes.CurvePolygonM) == QgsWKBTypes.CurvePolygonM
2524+
assert QgsWKBTypes.dropZ(QgsWKBTypes.CurvePolygonZM) == QgsWKBTypes.CurvePolygonM
2525+
assert QgsWKBTypes.dropZ(QgsWKBTypes.MultiCurve) == QgsWKBTypes.MultiCurve
2526+
assert QgsWKBTypes.dropZ(QgsWKBTypes.MultiCurveZ) == QgsWKBTypes.MultiCurve
2527+
assert QgsWKBTypes.dropZ(QgsWKBTypes.MultiCurveM) == QgsWKBTypes.MultiCurveM
2528+
assert QgsWKBTypes.dropZ(QgsWKBTypes.MultiCurveZM) == QgsWKBTypes.MultiCurveM
2529+
assert QgsWKBTypes.dropZ(QgsWKBTypes.MultiSurface) == QgsWKBTypes.MultiSurface
2530+
assert QgsWKBTypes.dropZ(QgsWKBTypes.MultiSurfaceZ) == QgsWKBTypes.MultiSurface
2531+
assert QgsWKBTypes.dropZ(QgsWKBTypes.MultiSurfaceM) == QgsWKBTypes.MultiSurfaceM
2532+
assert QgsWKBTypes.dropZ(QgsWKBTypes.MultiSurfaceZM) == QgsWKBTypes.MultiSurfaceM
2533+
assert QgsWKBTypes.dropZ(QgsWKBTypes.NoGeometry) == QgsWKBTypes.NoGeometry
2534+
assert QgsWKBTypes.dropZ(QgsWKBTypes.Point25D) == QgsWKBTypes.Point
2535+
assert QgsWKBTypes.dropZ(QgsWKBTypes.LineString25D) == QgsWKBTypes.LineString
2536+
assert QgsWKBTypes.dropZ(QgsWKBTypes.Polygon25D) == QgsWKBTypes.Polygon
2537+
assert QgsWKBTypes.dropZ(QgsWKBTypes.MultiPoint25D) == QgsWKBTypes.MultiPoint
2538+
assert QgsWKBTypes.dropZ(QgsWKBTypes.MultiLineString25D) == QgsWKBTypes.MultiLineString
2539+
assert QgsWKBTypes.dropZ(QgsWKBTypes.MultiPolygon25D) == QgsWKBTypes.MultiPolygon
2540+
2541+
# test dropping m dimension from types
2542+
assert QgsWKBTypes.dropM(QgsWKBTypes.Unknown) == QgsWKBTypes.Unknown
2543+
assert QgsWKBTypes.dropM(QgsWKBTypes.Point) == QgsWKBTypes.Point
2544+
assert QgsWKBTypes.dropM(QgsWKBTypes.PointZ) == QgsWKBTypes.PointZ
2545+
assert QgsWKBTypes.dropM(QgsWKBTypes.PointM) == QgsWKBTypes.Point
2546+
assert QgsWKBTypes.dropM(QgsWKBTypes.PointZM) == QgsWKBTypes.PointZ
2547+
assert QgsWKBTypes.dropM(QgsWKBTypes.MultiPoint) == QgsWKBTypes.MultiPoint
2548+
assert QgsWKBTypes.dropM(QgsWKBTypes.MultiPointZ) == QgsWKBTypes.MultiPointZ
2549+
assert QgsWKBTypes.dropM(QgsWKBTypes.MultiPointM) == QgsWKBTypes.MultiPoint
2550+
assert QgsWKBTypes.dropM(QgsWKBTypes.MultiPointZM) == QgsWKBTypes.MultiPointZ
2551+
assert QgsWKBTypes.dropM(QgsWKBTypes.LineString) == QgsWKBTypes.LineString
2552+
assert QgsWKBTypes.dropM(QgsWKBTypes.LineStringZ) == QgsWKBTypes.LineStringZ
2553+
assert QgsWKBTypes.dropM(QgsWKBTypes.LineStringM) == QgsWKBTypes.LineString
2554+
assert QgsWKBTypes.dropM(QgsWKBTypes.LineStringZM) == QgsWKBTypes.LineStringZ
2555+
assert QgsWKBTypes.dropM(QgsWKBTypes.MultiLineString) == QgsWKBTypes.MultiLineString
2556+
assert QgsWKBTypes.dropM(QgsWKBTypes.MultiLineStringZ) == QgsWKBTypes.MultiLineStringZ
2557+
assert QgsWKBTypes.dropM(QgsWKBTypes.MultiLineStringM) == QgsWKBTypes.MultiLineString
2558+
assert QgsWKBTypes.dropM(QgsWKBTypes.MultiLineStringZM) == QgsWKBTypes.MultiLineStringZ
2559+
assert QgsWKBTypes.dropM(QgsWKBTypes.Polygon) == QgsWKBTypes.Polygon
2560+
assert QgsWKBTypes.dropM(QgsWKBTypes.PolygonZ) == QgsWKBTypes.PolygonZ
2561+
assert QgsWKBTypes.dropM(QgsWKBTypes.PolygonM) == QgsWKBTypes.Polygon
2562+
assert QgsWKBTypes.dropM(QgsWKBTypes.PolygonZM) == QgsWKBTypes.PolygonZ
2563+
assert QgsWKBTypes.dropM(QgsWKBTypes.MultiPolygon) == QgsWKBTypes.MultiPolygon
2564+
assert QgsWKBTypes.dropM(QgsWKBTypes.MultiPolygonZ) == QgsWKBTypes.MultiPolygonZ
2565+
assert QgsWKBTypes.dropM(QgsWKBTypes.MultiPolygonM) == QgsWKBTypes.MultiPolygon
2566+
assert QgsWKBTypes.dropM(QgsWKBTypes.MultiPolygonZM) == QgsWKBTypes.MultiPolygonZ
2567+
assert QgsWKBTypes.dropM(QgsWKBTypes.GeometryCollection) == QgsWKBTypes.GeometryCollection
2568+
assert QgsWKBTypes.dropM(QgsWKBTypes.GeometryCollectionZ) == QgsWKBTypes.GeometryCollectionZ
2569+
assert QgsWKBTypes.dropM(QgsWKBTypes.GeometryCollectionM) == QgsWKBTypes.GeometryCollection
2570+
assert QgsWKBTypes.dropM(QgsWKBTypes.GeometryCollectionZM) == QgsWKBTypes.GeometryCollectionZ
2571+
assert QgsWKBTypes.dropM(QgsWKBTypes.CircularString) == QgsWKBTypes.CircularString
2572+
assert QgsWKBTypes.dropM(QgsWKBTypes.CircularStringZ) == QgsWKBTypes.CircularStringZ
2573+
assert QgsWKBTypes.dropM(QgsWKBTypes.CircularStringM) == QgsWKBTypes.CircularString
2574+
assert QgsWKBTypes.dropM(QgsWKBTypes.CircularStringZM) == QgsWKBTypes.CircularStringZ
2575+
assert QgsWKBTypes.dropM(QgsWKBTypes.CompoundCurve) == QgsWKBTypes.CompoundCurve
2576+
assert QgsWKBTypes.dropM(QgsWKBTypes.CompoundCurveZ) == QgsWKBTypes.CompoundCurveZ
2577+
assert QgsWKBTypes.dropM(QgsWKBTypes.CompoundCurveM) == QgsWKBTypes.CompoundCurve
2578+
assert QgsWKBTypes.dropM(QgsWKBTypes.CompoundCurveZM) == QgsWKBTypes.CompoundCurveZ
2579+
assert QgsWKBTypes.dropM(QgsWKBTypes.CurvePolygon) == QgsWKBTypes.CurvePolygon
2580+
assert QgsWKBTypes.dropM(QgsWKBTypes.CurvePolygonZ) == QgsWKBTypes.CurvePolygonZ
2581+
assert QgsWKBTypes.dropM(QgsWKBTypes.CurvePolygonM) == QgsWKBTypes.CurvePolygon
2582+
assert QgsWKBTypes.dropM(QgsWKBTypes.CurvePolygonZM) == QgsWKBTypes.CurvePolygonZ
2583+
assert QgsWKBTypes.dropM(QgsWKBTypes.MultiCurve) == QgsWKBTypes.MultiCurve
2584+
assert QgsWKBTypes.dropM(QgsWKBTypes.MultiCurveZ) == QgsWKBTypes.MultiCurveZ
2585+
assert QgsWKBTypes.dropM(QgsWKBTypes.MultiCurveM) == QgsWKBTypes.MultiCurve
2586+
assert QgsWKBTypes.dropM(QgsWKBTypes.MultiCurveZM) == QgsWKBTypes.MultiCurveZ
2587+
assert QgsWKBTypes.dropM(QgsWKBTypes.MultiSurface) == QgsWKBTypes.MultiSurface
2588+
assert QgsWKBTypes.dropM(QgsWKBTypes.MultiSurfaceZ) == QgsWKBTypes.MultiSurfaceZ
2589+
assert QgsWKBTypes.dropM(QgsWKBTypes.MultiSurfaceM) == QgsWKBTypes.MultiSurface
2590+
assert QgsWKBTypes.dropM(QgsWKBTypes.MultiSurfaceZM) == QgsWKBTypes.MultiSurfaceZ
2591+
assert QgsWKBTypes.dropM(QgsWKBTypes.NoGeometry) == QgsWKBTypes.NoGeometry
2592+
assert QgsWKBTypes.dropM(QgsWKBTypes.Point25D) == QgsWKBTypes.Point25D
2593+
assert QgsWKBTypes.dropM(QgsWKBTypes.LineString25D) == QgsWKBTypes.LineString25D
2594+
assert QgsWKBTypes.dropM(QgsWKBTypes.Polygon25D) == QgsWKBTypes.Polygon25D
2595+
assert QgsWKBTypes.dropM(QgsWKBTypes.MultiPoint25D) == QgsWKBTypes.MultiPoint25D
2596+
assert QgsWKBTypes.dropM(QgsWKBTypes.MultiLineString25D) == QgsWKBTypes.MultiLineString25D
2597+
assert QgsWKBTypes.dropM(QgsWKBTypes.MultiPolygon25D) == QgsWKBTypes.MultiPolygon25D
24832598

24842599
if __name__ == '__main__':
24852600
unittest.main()

0 commit comments

Comments
 (0)