Skip to content

Commit add14bf

Browse files
committedSep 19, 2023
PG: fix issue GH #54572 st_geographyfromtext
Fix #54572 Error saving edit on PostGIS geometry when table also contains geography
1 parent 37f426e commit add14bf

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed
 

‎src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2616,7 +2616,7 @@ bool QgsPostgresProvider::addFeatures( QgsFeatureList &flist, Flags flags )
26162616
}
26172617
else if ( fieldTypeName == QLatin1String( "geography" ) )
26182618
{
2619-
values += QStringLiteral( "%1st_geographyfromewkt(%2)" )
2619+
values += QStringLiteral( "%1st_geographyfromtext(%2)" )
26202620
.arg( delim,
26212621
quotedValue( v.toString() ) );
26222622
}
@@ -2650,7 +2650,7 @@ bool QgsPostgresProvider::addFeatures( QgsFeatureList &flist, Flags flags )
26502650
}
26512651
else if ( fieldTypeName == QLatin1String( "geography" ) )
26522652
{
2653-
values += QStringLiteral( "%1st_geographyfromewkt($%2)" )
2653+
values += QStringLiteral( "%1st_geographyfromtext($%2)" )
26542654
.arg( delim )
26552655
.arg( defaultValues.size() + offset );
26562656
}
@@ -3223,7 +3223,7 @@ bool QgsPostgresProvider::changeAttributeValues( const QgsChangedAttributesMap &
32233223
}
32243224
else if ( fld.typeName() == QLatin1String( "geography" ) )
32253225
{
3226-
sql += QStringLiteral( "st_geographyfromewkt(%1)" )
3226+
sql += QStringLiteral( "st_geographyfromtext(%1)" )
32273227
.arg( quotedValue( siter->toString() ) );
32283228
}
32293229
else if ( fld.typeName() == QLatin1String( "jsonb" ) )
@@ -3588,7 +3588,7 @@ bool QgsPostgresProvider::changeFeatures( const QgsChangedAttributesMap &attr_ma
35883588
}
35893589
else if ( fld.typeName() == QLatin1String( "geography" ) )
35903590
{
3591-
sql += QStringLiteral( "st_geographyfromewkt(%1)" )
3591+
sql += QStringLiteral( "st_geographyfromtext(%1)" )
35923592
.arg( quotedValue( siter->toString() ) );
35933593
}
35943594
else if ( fld.typeName() == QLatin1String( "jsonb" ) )

‎tests/src/python/test_provider_postgres.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3280,6 +3280,28 @@ def reportError(self, error, fatalError=False):
32803280
extracted_fids = [f['id'] for f in vl_result.getFeatures()]
32813281
self.assertEqual(set(extracted_fids), {1, 2}) # Bug ?
32823282

3283+
def testGeographyAddFeature(self):
3284+
"""Test issue GH #54572 Error saving edit on PostGIS geometry when table also contains geography"""
3285+
3286+
self.execSQLCommand(
3287+
'DROP TABLE IF EXISTS qgis_test."geom_and_geog" CASCADE')
3288+
self.execSQLCommand("""
3289+
CREATE TABLE qgis_test.geom_and_geog (
3290+
pkey SERIAL PRIMARY KEY,
3291+
geom geometry(POLYGON, 3857),
3292+
geog geography(POLYGON, 4326)
3293+
);""")
3294+
3295+
vl = QgsVectorLayer(self.dbconn + ' sslmode=disable key=\'pkey\' srid=3857 table="qgis_test"."geom_and_geog" (geom) sql=', 'geom_and_geog', 'postgres')
3296+
self.assertTrue(vl.isValid())
3297+
self.assertEqual(vl.featureCount(), 0)
3298+
dp = vl.dataProvider()
3299+
f = QgsFeature(vl.fields())
3300+
f.setGeometry(QgsGeometry.fromWkt('POLYGON((28.030080546000004 -26.2055410477482,28.030103891999996 -26.20540054874821,28.030532775999998 -26.205458576748192,28.030553322999996 -26.2056050407482,28.030080546000004 -26.2055410477482))'))
3301+
f.setAttribute('geog', 'POLYGON((28.030080546000004 -26.2055410477482,28.030103891999996 -26.20540054874821,28.030532775999998 -26.205458576748192,28.030553322999996 -26.2056050407482,28.030080546000004 -26.2055410477482))')
3302+
self.assertTrue(dp.addFeature(f))
3303+
self.assertEqual(vl.featureCount(), 1)
3304+
32833305

32843306
class TestPyQgsPostgresProviderCompoundKey(QgisTestCase, ProviderTestCase):
32853307

0 commit comments

Comments
 (0)
Please sign in to comment.