Skip to content

Commit 6c6c145

Browse files
szekerestnyalldawson
authored andcommitted
Fix writing geometries with M values, adding tests for Z/M/ZM geometries
1 parent 01112bd commit 6c6c145

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

src/providers/mssql/qgsmssqlprovider.cpp

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,15 +274,21 @@ void QgsMssqlProvider::loadMetadata()
274274

275275
QSqlQuery query = createQuery();
276276
query.setForwardOnly( true );
277-
if ( !query.exec( QStringLiteral( "select f_geometry_column, srid, geometry_type from geometry_columns where f_table_schema = '%1' and f_table_name = '%2'" ).arg( mSchemaName, mTableName ) ) )
277+
if ( !query.exec( QStringLiteral( "select f_geometry_column, srid, geometry_type, coord_dimension from geometry_columns where f_table_schema = '%1' and f_table_name = '%2'" ).arg( mSchemaName, mTableName ) ) )
278278
{
279279
QgsDebugMsg( query.lastError().text() );
280280
}
281281
if ( query.isActive() && query.next() )
282282
{
283283
mGeometryColName = query.value( 0 ).toString();
284284
mSRId = query.value( 1 ).toInt();
285-
mWkbType = getWkbType( query.value( 2 ).toString() );
285+
QString detectedType = query.value( 2 ).toString();
286+
QString dim = query.value( 3 ).toString();
287+
if ( dim == QLatin1String( "3" ) && !detectedType.endsWith( 'M' ) )
288+
detectedType += QLatin1String( "Z" );
289+
else if ( dim == QLatin1String( "4" ) )
290+
detectedType += QLatin1String( "ZM" );
291+
mWkbType = getWkbType( detectedType );
286292
}
287293
}
288294

@@ -1064,6 +1070,11 @@ bool QgsMssqlProvider::addFeatures( QgsFeatureList &flist, Flags flags )
10641070
// SQL Server so we have to remove it first.
10651071
wkt = geom.asWkt();
10661072
wkt.replace( QRegExp( "[mzMZ]+\\s*\\(" ), QStringLiteral( "(" ) );
1073+
// if we have M value only, we need to insert null-s for the Z value
1074+
if ( QgsWkbTypes::hasM( geom.wkbType() ) && !QgsWkbTypes::hasZ( geom.wkbType() ) )
1075+
{
1076+
wkt.replace( QRegExp( "(?=\\s[0-9+-.]+[,)])" ), QStringLiteral( " NULL" ) );
1077+
}
10671078
}
10681079
query.addBindValue( wkt );
10691080
}
@@ -1664,9 +1675,6 @@ bool QgsMssqlProvider::convertField( QgsField &field )
16641675

16651676
void QgsMssqlProvider::mssqlWkbTypeAndDimension( QgsWkbTypes::Type wkbType, QString &geometryType, int &dim )
16661677
{
1667-
if ( QgsWkbTypes::hasZ( wkbType ) )
1668-
dim = 3;
1669-
16701678
QgsWkbTypes::Type flatType = QgsWkbTypes::flatType( wkbType );
16711679

16721680
if ( flatType == QgsWkbTypes::Point )
@@ -1692,7 +1700,28 @@ void QgsMssqlProvider::mssqlWkbTypeAndDimension( QgsWkbTypes::Type wkbType, QStr
16921700
else if ( flatType == QgsWkbTypes::Unknown )
16931701
geometryType = QStringLiteral( "GEOMETRY" );
16941702
else
1703+
{
16951704
dim = 0;
1705+
return;
1706+
}
1707+
1708+
if ( QgsWkbTypes::hasZ( wkbType ) && QgsWkbTypes::hasM( wkbType ) )
1709+
{
1710+
dim = 4;
1711+
}
1712+
else if ( QgsWkbTypes::hasZ( wkbType ) )
1713+
{
1714+
dim = 3;
1715+
}
1716+
else if ( QgsWkbTypes::hasM( wkbType ) )
1717+
{
1718+
geometryType += QLatin1String( "M" );
1719+
dim = 3;
1720+
}
1721+
else if ( wkbType >= QgsWkbTypes::Point25D && wkbType <= QgsWkbTypes::MultiPolygon25D )
1722+
{
1723+
dim = 3;
1724+
}
16961725
}
16971726

16981727
QgsWkbTypes::Type QgsMssqlProvider::getWkbType( const QString &geometryType )

tests/src/python/test_provider_mssql.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,8 @@ def testCreateLayerMultiPoint(self):
359359

360360
@unittest.skipIf(os.environ.get('TRAVIS', '') == 'true', 'Failing on Travis')
361361
def testCurveGeometries(self):
362-
geomtypes = ['CompoundCurve', 'CurvePolygon', 'CircularString']
363-
geoms = ['CompoundCurve ((0 -23.43778, 0 23.43778),CircularString (0 23.43778, -45 33.43778, -90 23.43778),(-90 23.43778, -90 -23.43778),CircularString (-90 -23.43778, -45 -23.43778, 0 -23.43778))', 'CurvePolygon (CompoundCurve ((0 -23.43778, 0 -15.43778, 0 23.43778),CircularString (0 23.43778, -45 100, -90 23.43778),(-90 23.43778, -90 -23.43778),CircularString (-90 -23.43778, -45 -16.43778, 0 -23.43778)),CompoundCurve (CircularString (-30 0, -48 -12, -60 0, -48 -6, -30 0)))', 'CircularString (0 0, 0.14644660940672 0.35355339059327, 0.5 0.5, 0.85355339059327 0.35355339059327, 1 0, 0.85355339059327 -0.35355339059327, 0.5 -0.5, 0.14644660940672 -0.35355339059327, 0 0)']
362+
geomtypes = ['CompoundCurveM', 'CurvePolygonM', 'CircularStringM', 'CompoundCurveZM', 'CurvePolygonZM', 'CircularStringZM', 'CompoundCurveZ', 'CurvePolygonZ', 'CircularStringZ', 'CompoundCurve', 'CurvePolygon', 'CircularString']
363+
geoms = ['CompoundCurveM ((0 -23.43778 10, 0 23.43778 10),CircularStringM (0 23.43778 10, -45 33.43778 10, -90 23.43778 10),(-90 23.43778 10, -90 -23.43778 10),CircularStringM (-90 -23.43778 10, -45 -23.43778 10, 0 -23.43778 10))', 'CurvePolygonM (CompoundCurveM ((0 -23.43778 10, 0 -15.43778 10, 0 23.43778 10),CircularStringM (0 23.43778 10, -45 100 10, -90 23.43778 10),(-90 23.43778 10, -90 -23.43778 10),CircularStringM (-90 -23.43778 10, -45 -16.43778 10, 0 -23.43778 10)),CompoundCurveM (CircularStringM (-30 0 10, -48 -12 10, -60 0 10, -48 -6 10, -30 0 10)))', 'CircularStringM (0 0 10, 0.14644660940672 0.35355339059327 10, 0.5 0.5 10, 0.85355339059327 0.35355339059327 10, 1 0 10, 0.85355339059327 -0.35355339059327 10, 0.5 -0.5 10, 0.14644660940672 -0.35355339059327 10, 0 0 10)', 'CompoundCurveZM ((0 -23.43778 2 10, 0 23.43778 2 10),CircularStringZM (0 23.43778 2 10, -45 33.43778 2 10, -90 23.43778 2 10),(-90 23.43778 2 10, -90 -23.43778 2 10),CircularStringZM (-90 -23.43778 2 10, -45 -23.43778 2 10, 0 -23.43778 2 10))', 'CurvePolygonZM (CompoundCurveZM ((0 -23.43778 5 10, 0 -15.43778 8 10, 0 23.43778 6 10),CircularStringZM (0 23.43778 6 10, -45 100 6 10, -90 23.43778 6 10),(-90 23.43778 6 10, -90 -23.43778 5 10),CircularStringZM (-90 -23.43778 5 10, -45 -16.43778 5 10, 0 -23.43778 5 10)),CompoundCurveZM (CircularStringZM (-30 0 10 10, -48 -12 10 10, -60 0 10 10, -48 -6 10 10, -30 0 10 10)))', 'CircularStringZM (0 0 1 10, 0.14644660940672 0.35355339059327 1 10, 0.5 0.5 1 10, 0.85355339059327 0.35355339059327 1 10, 1 0 1 10, 0.85355339059327 -0.35355339059327 1 10, 0.5 -0.5 1 10, 0.14644660940672 -0.35355339059327 1 10, 0 0 1 10)', 'CompoundCurveZ ((0 -23.43778 2, 0 23.43778 2),CircularStringZ (0 23.43778 2, -45 33.43778 2, -90 23.43778 2),(-90 23.43778 2, -90 -23.43778 2),CircularStringZ (-90 -23.43778 2, -45 -23.43778 2, 0 -23.43778 2))', 'CurvePolygonZ (CompoundCurveZ ((0 -23.43778 5, 0 -15.43778 8, 0 23.43778 6),CircularStringZ (0 23.43778 6, -45 100 6, -90 23.43778 6),(-90 23.43778 6, -90 -23.43778 5),CircularStringZ (-90 -23.43778 5, -45 -16.43778 5, 0 -23.43778 5)),CompoundCurveZ (CircularStringZ (-30 0 10, -48 -12 10, -60 0 10, -48 -6 10, -30 0 10)))', 'CircularStringZ (0 0 1, 0.14644660940672 0.35355339059327 1, 0.5 0.5 1, 0.85355339059327 0.35355339059327 1, 1 0 1, 0.85355339059327 -0.35355339059327 1, 0.5 -0.5 1, 0.14644660940672 -0.35355339059327 1, 0 0 1)', 'CompoundCurve ((0 -23.43778, 0 23.43778),CircularString (0 23.43778, -45 33.43778, -90 23.43778),(-90 23.43778, -90 -23.43778),CircularString (-90 -23.43778, -45 -23.43778, 0 -23.43778))', 'CurvePolygon (CompoundCurve ((0 -23.43778, 0 -15.43778, 0 23.43778),CircularString (0 23.43778, -45 100, -90 23.43778),(-90 23.43778, -90 -23.43778),CircularString (-90 -23.43778, -45 -16.43778, 0 -23.43778)),CompoundCurve (CircularString (-30 0, -48 -12, -60 0, -48 -6, -30 0)))', 'CircularString (0 0, 0.14644660940672 0.35355339059327, 0.5 0.5, 0.85355339059327 0.35355339059327, 1 0, 0.85355339059327 -0.35355339059327, 0.5 -0.5, 0.14644660940672 -0.35355339059327, 0 0)']
364364
for idx, t in enumerate(geoms):
365365
f = QgsFeature()
366366
g = QgsGeometry.fromWkt(t)

0 commit comments

Comments
 (0)