Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #54664 from elpaso/bugfix-gh54572-pg-geom-and-geog
PG: fix issue GH #54572 st_geographyfromtext
  • Loading branch information
elpaso committed Sep 22, 2023
2 parents 6daf48e + add14bf commit b443c19
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -2616,7 +2616,7 @@ bool QgsPostgresProvider::addFeatures( QgsFeatureList &flist, Flags flags )
}
else if ( fieldTypeName == QLatin1String( "geography" ) )
{
values += QStringLiteral( "%1st_geographyfromewkt(%2)" )
values += QStringLiteral( "%1st_geographyfromtext(%2)" )
.arg( delim,
quotedValue( v.toString() ) );
}
Expand Down Expand Up @@ -2650,7 +2650,7 @@ bool QgsPostgresProvider::addFeatures( QgsFeatureList &flist, Flags flags )
}
else if ( fieldTypeName == QLatin1String( "geography" ) )
{
values += QStringLiteral( "%1st_geographyfromewkt($%2)" )
values += QStringLiteral( "%1st_geographyfromtext($%2)" )
.arg( delim )
.arg( defaultValues.size() + offset );
}
Expand Down Expand Up @@ -3223,7 +3223,7 @@ bool QgsPostgresProvider::changeAttributeValues( const QgsChangedAttributesMap &
}
else if ( fld.typeName() == QLatin1String( "geography" ) )
{
sql += QStringLiteral( "st_geographyfromewkt(%1)" )
sql += QStringLiteral( "st_geographyfromtext(%1)" )
.arg( quotedValue( siter->toString() ) );
}
else if ( fld.typeName() == QLatin1String( "jsonb" ) )
Expand Down Expand Up @@ -3588,7 +3588,7 @@ bool QgsPostgresProvider::changeFeatures( const QgsChangedAttributesMap &attr_ma
}
else if ( fld.typeName() == QLatin1String( "geography" ) )
{
sql += QStringLiteral( "st_geographyfromewkt(%1)" )
sql += QStringLiteral( "st_geographyfromtext(%1)" )
.arg( quotedValue( siter->toString() ) );
}
else if ( fld.typeName() == QLatin1String( "jsonb" ) )
Expand Down
22 changes: 22 additions & 0 deletions tests/src/python/test_provider_postgres.py
Expand Up @@ -3280,6 +3280,28 @@ def reportError(self, error, fatalError=False):
extracted_fids = [f['id'] for f in vl_result.getFeatures()]
self.assertEqual(set(extracted_fids), {1, 2}) # Bug ?

def testGeographyAddFeature(self):
"""Test issue GH #54572 Error saving edit on PostGIS geometry when table also contains geography"""

self.execSQLCommand(
'DROP TABLE IF EXISTS qgis_test."geom_and_geog" CASCADE')
self.execSQLCommand("""
CREATE TABLE qgis_test.geom_and_geog (
pkey SERIAL PRIMARY KEY,
geom geometry(POLYGON, 3857),
geog geography(POLYGON, 4326)
);""")

vl = QgsVectorLayer(self.dbconn + ' sslmode=disable key=\'pkey\' srid=3857 table="qgis_test"."geom_and_geog" (geom) sql=', 'geom_and_geog', 'postgres')
self.assertTrue(vl.isValid())
self.assertEqual(vl.featureCount(), 0)
dp = vl.dataProvider()
f = QgsFeature(vl.fields())
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))'))
f.setAttribute('geog', 'POLYGON((28.030080546000004 -26.2055410477482,28.030103891999996 -26.20540054874821,28.030532775999998 -26.205458576748192,28.030553322999996 -26.2056050407482,28.030080546000004 -26.2055410477482))')
self.assertTrue(dp.addFeature(f))
self.assertEqual(vl.featureCount(), 1)


class TestPyQgsPostgresProviderCompoundKey(QgisTestCase, ProviderTestCase):

Expand Down

0 comments on commit b443c19

Please sign in to comment.