I want to import a WFS layer from an URL, but spatial filter to import only the objets that intersect or are inside a polygon that i've drawn.
I wrote a code but it doesnt seem to work as intended because the imported layer is empty.
wfs_url="https://wxs.ign.fr/agriculture/geoportail/wfs/?SERVICE=WFS&VERSION=2.0.0&REQUEST=GetFeature"
params = {
'srsname': 'EPSG:4326',
'typeName': "RPG.2013:rpg_2013",
'url': wfs_url,
}
# Get the polygon layer
polygone_layer = QgsProject.instance().mapLayersByName(polygon_name)[0]
# Get the polygon geometry
####### I've tried multiple things, nothing seemed to work
feature = polygone_layer.getFeature(0)
polygon_geometry = feature.geometry()
# Define the filter based on the polygon geometry
cql_filter = "INTERSECTS(the_geom, {})".format(polygon_geometry.asWkt())
# Add the filter to the WFS request parameters
params['cql_filter'] = cql_filter
# Create a vector layer from the modified WFS request parameters
wfs_couche = QgsVectorLayer('{}?{}'.format(wfs_url, '&'.join(['{}={}'.format(k, v) for k, v in params.items()])), nom_couche, 'WFS')
# Add the layer to the map
QgsProject.instance().addMapLayer(wfs_couche)
After tinkering for days, the best result I could achieve was getting the extent rectangle from the polygon and use it as a filter, however, the layer created contained 0 objects....
By the way, the SRS I'm using is the same one required by the WFS API, I've also tried adding and deleting parameters, but it didn't change anything.