Skip to content

Commit

Permalink
improve tests and remove unused debug log
Browse files Browse the repository at this point in the history
  • Loading branch information
signedav authored and nyalldawson committed Jul 20, 2022
1 parent b601ce4 commit 716f9b8
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 89 deletions.
14 changes: 8 additions & 6 deletions python/core/auto_generated/qgsogcutils.sip.in
Expand Up @@ -174,12 +174,13 @@ Returns an expression from a WFS filter embedded in a document.
%End


static QDomElement expressionToOgcExpression( const QgsExpression &exp, QDomDocument &doc, QString *errorMessage = 0 );
static QDomElement expressionToOgcExpression( const QgsExpression &exp, QDomDocument &doc, QString *errorMessage = 0,
bool requiresFilterElement = false );
%Docstring
Creates an OGC expression XML element.

:return: valid OGC expression QDomElement on success,
otherwise null QDomElement
:return: valid OGC expression QDomElement on success or a valid \verbatim <Filter> QDomElement when:param requiresFilterElement: is set.
otherwise null QDomElement
%End

static QDomElement expressionToOgcExpression( const QgsExpression &exp,
Expand All @@ -190,12 +191,13 @@ Creates an OGC expression XML element.
const QString &srsName,
bool honourAxisOrientation,
bool invertAxisOrientation,
QString *errorMessage = 0 );
QString *errorMessage = 0,
bool requiresFilterElement = false );
%Docstring
Creates an OGC expression XML element.

:return: valid OGC expression QDomElement on success,
otherwise null QDomElement
:return: valid OGC expression QDomElement on success or a valid \verbatim <Filter> QDomElement when:param requiresFilterElement: is set.
otherwise null QDomElement
%End


Expand Down
34 changes: 4 additions & 30 deletions src/core/qgsogcutils.h
Expand Up @@ -211,15 +211,15 @@ class CORE_EXPORT QgsOgcUtils

/**
* Creates an OGC expression XML element.
* \returns valid OGC expression QDomElement on success,
* \returns valid OGC expression QDomElement on success or a valid \verbatim <Filter> QDomElement when \param requiresFilterElement is set.
* otherwise null QDomElement
*/
static QDomElement expressionToOgcExpression( const QgsExpression &exp, QDomDocument &doc, QString *errorMessage = nullptr,
bool requiresFilterElement = false );

/**
* Creates an OGC expression XML element.
* \returns valid OGC expression QDomElement on success,
* \returns valid OGC expression QDomElement on success or a valid \verbatim <Filter> QDomElement when \param requiresFilterElement is set.
* otherwise null QDomElement
*/
static QDomElement expressionToOgcExpression( const QgsExpression &exp,
Expand All @@ -233,28 +233,6 @@ class CORE_EXPORT QgsOgcUtils
QString *errorMessage = nullptr,
bool requiresFilterElement = false );

/**
* Creates an OGC expression XML element.
* \returns valid OGC expression QDomElement on success,
* otherwise null QDomElement
*/
static QDomElement expressionToOgcExpressionFilter( const QgsExpression &exp, QDomDocument &doc, QString *errorMessage = nullptr );

/**
* Creates an OGC expression XML element.
* \returns valid OGC expression QDomElement on success,
* otherwise null QDomElement
*/
static QDomElement expressionToOgcExpressionFilter( const QgsExpression &exp,
QDomDocument &doc,
QgsOgcUtils::GMLVersion gmlVersion,
FilterVersion filterVersion,
const QString &geometryName,
const QString &srsName,
bool honourAxisOrientation,
bool invertAxisOrientation,
QString *errorMessage = nullptr );

#ifndef SIP_RUN

/**
Expand Down Expand Up @@ -326,12 +304,8 @@ class CORE_EXPORT QgsOgcUtils
static QgsGeometry geometryFromGMLMultiPolygon( const QDomElement &geometryElement );

/**
* @brief filterElement
* @param doc
* @param gmlVersion
* @param filterVersion
* @param GMLUsed
* @return
* Creates an empty \verbatim <Filter> \endverbatim QDomElement
* \returns valid \verbatim <Filter> \endverbatim QDomElement
*/
static QDomElement filterElement(
QDomDocument &doc,
Expand Down
2 changes: 0 additions & 2 deletions src/providers/wfs/qgsbackgroundcachedfeatureiterator.cpp
Expand Up @@ -277,9 +277,7 @@ QgsBackgroundCachedFeatureIterator::QgsBackgroundCachedFeatureIterator(
QString serverExpression;
if ( mRequest.filterType() == QgsFeatureRequest::FilterExpression && mRequest.filterExpression() && mRequest.filterExpression()->isValid() )
{
qDebug() << "WFS DEB expression: " << mRequest.filterExpression()->expression();
serverExpression = mShared->computedExpression( *mRequest.filterExpression() );
qDebug() << "WFS DEB EX" << serverExpression;
}

if ( !shared->clientSideFilterExpression().isEmpty() )
Expand Down
4 changes: 1 addition & 3 deletions src/providers/wfs/qgswfsfeatureiterator.cpp
Expand Up @@ -268,8 +268,6 @@ QUrl QgsWFSFeatureDownloaderImpl::buildURL( qint64 startIndex, long long maxFeat

envelopeFilterDoc.firstChildElement().appendChild( andElem );

qDebug() << "WFS DEB filter " << mShared->mWFSFilter;
qDebug() << "WFS DEB expres " << mShared->mServerExpression;
QSet<QString> setNamespaceURI;
for ( const QgsOgcUtils::LayerProperties &props : std::as_const( mShared->mLayerPropertiesList ) )
{
Expand All @@ -283,7 +281,6 @@ QUrl QgsWFSFeatureDownloaderImpl::buildURL( qint64 startIndex, long long maxFeat
}
}

qDebug() << "WFS DEB having the doc:\n" << envelopeFilterDoc.toString();
query.addQueryItem( QStringLiteral( "FILTER" ), sanitizeFilter( envelopeFilterDoc.toString() ) );
}
else if ( !rect.isNull() )
Expand Down Expand Up @@ -372,6 +369,7 @@ QUrl QgsWFSFeatureDownloaderImpl::buildURL( qint64 startIndex, long long maxFeat

getFeatureUrl.setQuery( query );
QgsDebugMsgLevel( QStringLiteral( "WFS GetFeature URL: %1" ).arg( getFeatureUrl.toDisplayString( ) ), 2 );
qDebug() << "\n\n" << "WFS GetFeature URL: " << getFeatureUrl.toDisplayString() << "\n\n";
return getFeatureUrl;
}

Expand Down
2 changes: 1 addition & 1 deletion src/providers/wfs/qgswfsshareddata.h
Expand Up @@ -34,7 +34,7 @@ class QgsWFSSharedData : public QObject, public QgsBackgroundCachedSharedData
//! Compute WFS filter from the sql or filter in the URI
bool computeFilter( QString &errorMsg );

//! Compute WFS filter from the sql or filter in the URI
//! Returns computed WFS server expression
QString computedExpression( const QgsExpression &expression ) const override;

//! Returns srsName
Expand Down
149 changes: 102 additions & 47 deletions tests/src/python/test_provider_wfs.py
Expand Up @@ -3460,16 +3460,16 @@ def testGetFeatureWithServerExpression(self):

with open(sanitize(endpoint, '?SERVICE=WFS?REQUEST=GetCapabilities?VERSION=2.0.0'), 'wb') as f:
f.write("""
<wfs:WFS_Capabilities version="2.0.0" xmlns:wfs="http://www.opengis.net/wfs/2.0" xmlns:ows="http://www.opengis.net/ows/1.1">
<wfs:WFS_Capabilities version="2.0.0" xmlns:wfs="http://www.opengis.net/wfs/2.0" xmlns:ows="http://www.opengis.net/ows/1.1" xmlns:gml="http://schemas.opengis.net/gml/3.2" xmlns:fes="http://www.opengis.net/fes/2.0">
<wfs:FeatureTypeList>
<wfs:FeatureType xmlns:my="http://my">
<wfs:Name>my:typename</wfs:Name>
<wfs:Title>Title</wfs:Title>
<wfs:Abstract>Abstract</wfs:Abstract>
<wfs:SRS>EPSG:32631</wfs:SRS>
<wfs:SRS>EPSG:4326</wfs:SRS>
<WGS84BoundingBox>
<LowerCorner>0 40</LowerCorner>
<UpperCorner>15 50</UpperCorner>
<LowerCorner>50 -100</LowerCorner>
<UpperCorner>100 -50</UpperCorner>
</WGS84BoundingBox>
</wfs:FeatureType>
</wfs:FeatureTypeList>
Expand Down Expand Up @@ -3500,8 +3500,9 @@ def testGetFeatureWithServerExpression(self):
self.assertEqual(len(vl.fields()), 1)
self.assertEqual(vl.wkbType(), QgsWkbTypes.Point)

# Simple test
with open(sanitize(endpoint,
'?SERVICE=WFS&REQUEST=GetFeature&VERSION=2.0.0&TYPENAMES=my:typename&SRSNAME=urn:ogc:def:crs:EPSG::32631&NAMESPACES=xmlns(my,http://my)&NAMESPACE=xmlns(my,http://my)'),
'?SERVICE=WFS&REQUEST=GetFeature&VERSION=2.0.0&TYPENAMES=my:typename&SRSNAME=urn:ogc:def:crs:EPSG::4326&NAMESPACES=xmlns(my,http://my)&NAMESPACE=xmlns(my,http://my)'),
'wb') as f:
f.write("""
<wfs:FeatureCollection
Expand All @@ -3518,42 +3519,93 @@ def testGetFeatureWithServerExpression(self):
values = [f['intfield'] for f in vl.getFeatures()]
self.assertEqual(values, [1])

# Get feature according to expression
vl = QgsVectorLayer("url='http://" + endpoint + "' typename='my:typename' version='2.0.0'", 'test', 'WFS')
self.assertTrue(vl.isValid())
self.assertEqual(len(vl.fields()), 1)
self.assertEqual(vl.wkbType(), QgsWkbTypes.Point)

polygonLayer = QgsVectorLayer('Polygon', 'test_polygon', 'memory')
poly = QgsFeature(polygonLayer.fields())
parent_feature = QgsFeature()
parent_feature.setGeometry(QgsGeometry.fromWkt('Polygon ((80 -80, 80 -60, 60 -60, 60 -80, 80 -80))'))
context = QgsExpressionContext()
context.appendScope(QgsExpressionContextUtils.globalScope())
scope = QgsExpressionContextScope()
scope.setVariable('parent', parent_feature, True)
context.appendScope(scope)
request = QgsFeatureRequest()
request.setExpressionContext(context)
request.setFilterExpression("intersects( $geometry, geometry(var('parent')))")

with open(sanitize(endpoint,
"""?SERVICE=WFS&REQUEST=GetFeature&VERSION=2.0.0&TYPENAMES=my:typename&SRSNAME=urn:ogc:def:crs:EPSG::4326&FILTER=<fes:Filter xmlns:fes="http://www.opengis.net/fes/2.0" xmlns:gml="http://www.opengis.net/gml/3.2">
<fes:Intersects>
<fes:ValueReference>geometryProperty</fes:ValueReference>
<gml:Polygon gml:id="qgis_id_geom_1" srsName="urn:ogc:def:crs:EPSG::4326">
<gml:exterior>
<gml:LinearRing>
<gml:posList srsDimension="2">80 -80 80 -60 60 -60 60 -80 80 -80</gml:posList>
</gml:LinearRing>
</gml:exterior>
</gml:Polygon>
</fes:Intersects>
</fes:Filter>
&NAMESPACES=xmlns(my,http://my)&NAMESPACE=xmlns(my,http://my)
"""),
'wb') as f:
f.write("""
<wfs:FeatureCollection
xmlns:wfs="http://www.opengis.net/wfs/2.0"
xmlns:gml="http://www.opengis.net/gml/3.2"
xmlns:my="http://my">
<gml:featureMember>
<my:typename fid="typename.0">
<my:intfield>1</my:intfield>
<my:geometryProperty><gml:Point srsName="urn:ogc:def:crs:EPSG::4326"><gml:pos>70 -65</gml:pos></gml:Point></my:geometryProperty>
</my:typename>
</gml:featureMember>
</wfs:FeatureCollection>""".encode('UTF-8'))
values = [f['intfield'] for f in vl.getFeatures(request)]
self.assertEqual(values, [1])

# Get feature according to expression and filter
vl = QgsVectorLayer("url='http://" + endpoint + "' typename='my:typename' version='2.0.0' sql=SELECT * FROM \"my:typename\" WHERE intfield = 1", 'test', 'WFS')
self.assertTrue(vl.isValid())
self.assertEqual(len(vl.fields()), 1)
self.assertEqual(vl.wkbType(), QgsWkbTypes.Point)

request = QgsFeatureRequest()
feature = QgsFeature()
feature.setGeometry(QgsGeometry.fromWkt('Polygon ((80 -80, 80 -60, 60 -60, 60 -80, 80 -80))'))
context = QgsExpressionContext()
parent_feature = QgsFeature().setGeometry(QgsGeometry.fromWkt('Polygon ((80 -80, 80 -60, 60 -60, 60 -80, 80 -80))'))
scope = QgsExpressionContextScope()
scope.setVariable('parent', feature)
scope.setVariable('parent', parent_feature, True)
context = QgsExpressionContext()
context.appendScope(QgsExpressionContextUtils.globalScope())
context.appendScope(scope)
request = QgsFeatureRequest()
request.setExpressionContext(context)
request.setFilterExpression("intersects( $geometry, geometry(@parent))")
request.setFilterExpression("intersects( $geometry, geometry(var('parent')))")

with open(sanitize(endpoint,
"""?SERVICE=WFS&REQUEST=GetFeature&VERSION=2.0.0&TYPENAMES=my:typename&SRSNAME=urn:ogc:def:crs:EPSG::32631&FILTER=<fes:Filter xmlns:fes="http://www.opengis.net/fes/2.0" xmlns:my="http://my">
<fes:And>
<fes:Intersects xmlns:fes=\"http://www.opengis.net/fes/2.0\">
<fes:ValueReference xmlns:fes=\"http://www.opengis.net/fes/2.0\">wkb_geometry</fes:ValueReference>
<gml:Polygon xmlns:gml=\"http://www.opengis.net/gml/3.2\" gml:id=\"qgis_id_geom_1\" srsName=\"urn:ogc:def:crs:EPSG::4326\">
<gml:exterior xmlns:gml=\"http://www.opengis.net/gml/3.2\">
<gml:LinearRing xmlns:gml=\"http://www.opengis.net/gml/3.2\">
<gml:posList xmlns:gml=\"http://www.opengis.net/gml/3.2\" srsDimension=\"2\">80 -80 80 -60 60 -60 60 -80 80 -80</gml:posList>
</gml:LinearRing>
</gml:exterior>
</gml:Polygon>
</fes:Intersects>
<fes:PropertyIsEqualTo>
<fes:ValueReference>my:intfield</fes:ValueReference>
<fes:Literal>1</fes:Literal>
</fes:PropertyIsEqualTo>
</fes:And>
</fes:Filter>
&NAMESPACES=xmlns(my,http://my)&NAMESPACE=xmlns(my,http://my)"""),
"""?SERVICE=WFS&REQUEST=GetFeature&VERSION=2.0.0&TYPENAMES=my:typename&SRSNAME=urn:ogc:def:crs:EPSG::4326&FILTER=<fes:Filter xmlns:fes="http://www.opengis.net/fes/2.0" xmlns:gml="http://www.opengis.net/gml/3.2">
<fes:And>
<fes:Intersects>
<fes:ValueReference>geometryProperty</fes:ValueReference>
<gml:Polygon gml:id="qgis_id_geom_1" srsName="urn:ogc:def:crs:EPSG::4326">
<gml:exterior>
<gml:LinearRing>
<gml:posList srsDimension="2">80 -80 80 -60 60 -60 60 -80 80 -80</gml:posList>
</gml:LinearRing>
</gml:exterior>
</gml:Polygon>
</fes:Intersects>
<fes:PropertyIsEqualTo>
<fes:ValueReference>my:intfield</fes:ValueReference>
<fes:Literal>1</fes:Literal>
</fes:PropertyIsEqualTo>
</fes:And>
</fes:Filter>
&NAMESPACES=xmlns(my,http://my)&NAMESPACE=xmlns(my,http://my)
"""),
'wb') as f:
f.write("""
<wfs:FeatureCollection
Expand All @@ -3572,42 +3624,45 @@ def testGetFeatureWithServerExpression(self):

# Get feature according to expression and filter and bounding box
vl = QgsVectorLayer(
"url='http://" + endpoint + "' typename='my:typename' version='2.0.0' restrictToRequestBBOX=1 sql=SELECT * FROM \"my:typename\" WHERE intfield > 0", 'test',
'WFS')
"url='http://" + endpoint + "' typename='my:typename' version='2.0.0' restrictToRequestBBOX=1 sql=SELECT * FROM \"my:typename\" WHERE intfield = 1", 'test', 'WFS')
self.assertTrue(vl.isValid())
self.assertEqual(len(vl.fields()), 1)
self.assertEqual(vl.wkbType(), QgsWkbTypes.Point)

extent = QgsRectangle(400000.0, 5400000.0, 450000.0, 5500000.0)
extent = QgsRectangle(-80, 60, -60, 89)

request = QgsFeatureRequest().setFilterRect(extent)

feature = QgsFeature()
feature.setGeometry(QgsGeometry.fromWkt('Polygon ((80 -80, 80 -60, 60 -60, 60 -80, 80 -80))'))
context = QgsExpressionContext()
parent_feature = QgsFeature().setGeometry(QgsGeometry.fromWkt('Polygon ((80 -80, 80 -60, 60 -60, 60 -80, 80 -80))'))
scope = QgsExpressionContextScope()
scope.setVariable('parent', feature)
scope.setVariable('parent', parent_feature, True)
context = QgsExpressionContext()
context.appendScope(QgsExpressionContextUtils.globalScope())
context.appendScope(scope)
request.setExpressionContext(context)
request.setFilterExpression("intersects( $geometry, geometry(@parent))")
request.setFilterExpression("intersects( $geometry, geometry(var('parent')))")

with open(sanitize(endpoint,
"""?SERVICE=WFS&REQUEST=GetFeature&VERSION=2.0.0&TYPENAMES=my:typename&SRSNAME=urn:ogc:def:crs:EPSG::32631&FILTER=<fes:Filter xmlns:fes="http://www.opengis.net/fes/2.0" xmlns:gml="http://www.opengis.net/gml/3.2" xmlns:my="http://my">
"""?SERVICE=WFS&REQUEST=GetFeature&VERSION=2.0.0&TYPENAMES=my:typename&SRSNAME=urn:ogc:def:crs:EPSG::4326&FILTER=<fes:Filter xmlns:fes="http://www.opengis.net/fes/2.0" xmlns:gml="http://www.opengis.net/gml/3.2" xmlns:gml="http://www.opengis.net/gml/3.2">
<fes:And>
<fes:BBOX>
<fes:ValueReference>my:geometryProperty</fes:ValueReference>
<gml:Envelope srsName="urn:ogc:def:crs:EPSG::32631">
<gml:lowerCorner>400000 5400000</gml:lowerCorner>
<gml:upperCorner>450000 5500000</gml:upperCorner>
<LowerCorner>-90 70</LowerCorner>
<UpperCorner>-70 90</UpperCorner>
</gml:Envelope>
</fes:BBOX>
<fes:Intersects xmlns:fes=\"http://www.opengis.net/fes/2.0\">
<fes:ValueReference xmlns:fes=\"http://www.opengis.net/fes/2.0\">wkb_geometry</fes:ValueReference>
<gml:Polygon xmlns:gml=\"http://www.opengis.net/gml/3.2\" gml:id=\"qgis_id_geom_1\" srsName=\"urn:ogc:def:crs:EPSG::4326\">
<gml:exterior xmlns:gml=\"http://www.opengis.net/gml/3.2\">
<gml:LinearRing xmlns:gml=\"http://www.opengis.net/gml/3.2\">
<gml:posList xmlns:gml=\"http://www.opengis.net/gml/3.2\" srsDimension=\"2\">80 -80 80 -60 60 -60 60 -80 80 -80</gml:posList>
<fes:Intersects>
<fes:ValueReference>geometryProperty</fes:ValueReference>
<gml:Polygon gml:id="qgis_id_geom_1" srsName="urn:ogc:def:crs:EPSG::4326">
<gml:exterior>
<gml:LinearRing>
<gml:posList srsDimension="2">80 -80 80 -60 60 -60 60 -80 80 -80</gml:posList>
</gml:LinearRing>
</gml:exterior>
</gml:exterior>
</gml:Polygon>
</fes:Intersects>
<fes:PropertyIsGreaterThan xmlns:fes="http://www.opengis.net/fes/2.0">
Expand All @@ -3625,7 +3680,7 @@ def testGetFeatureWithServerExpression(self):
xmlns:my="http://my">
<gml:featureMember>
<my:typename fid="typename.0">
<my:geometryProperty><gml:Point srsName="urn:ogc:def:crs:EPSG::32631" gml:id="typename.geom.0"><gml:pos>426858 5427937</gml:pos></gml:Point></my:geometryProperty>
<my:geometryProperty><gml:Point srsName="urn:ogc:def:crs:EPSG::4326" gml:id="typename.geom.0"><gml:pos>426858 5427937</gml:pos></gml:Point></my:geometryProperty>
<my:intfield>1</my:intfield>
</my:typename>
</gml:featureMember>
Expand Down

0 comments on commit 716f9b8

Please sign in to comment.