If setUsingMemoryCache(true) is still slow, you're likely facing a fundamental WFS performance bottleneck. WFS joins are notoriously slow because they operate on remote data. Here are more effective alternatives:
1. Skip QGIS Joins Completely
Process the data in Python first, then load the result:
import geopandas as gpd
from shapely.geometry import shape
import json
# Function to convert WFS to GeoDataFrame
def wfs_to_gdf(layer):
features = []
for feat in layer.getFeatures():
geom = feat.geometry()
wkt = geom.asWkt()
attrs = {field.name(): feat[field.name()] for field in layer.fields()}
features.append({"geometry": shape(json.loads(geom.asJson())), "properties": attrs})
return gpd.GeoDataFrame(
[f["properties"] for f in features],
geometry=[f["geometry"] for f in features],
crs=layer.crs().toWkt()
)
# Convert layers to GeoDataFrames
gdf1 = wfs_to_gdf(layer1)
gdf2 = wfs_to_gdf(layer2)
# Join in memory using pandas (much faster)
joined = gdf1.merge(gdf2.drop('geometry', axis=1),
left_on='target_field',
right_on='join_field',
how='left')
# Convert back to QGIS layer
joined.to_file("joined_result.gpkg", driver="GPKG")
joined_layer = QgsVectorLayer("joined_result.gpkg", "Joined Layer", "ogr")
QgsProject.instance().addMapLayer(joined_layer)
2. Use Virtual Layers with SQLite
This performs the join at the database level:
query = """
SELECT a.*, b.field1, b.field2
FROM layer1 AS a
LEFT JOIN layer2 AS b ON a.target_field = b.join_field
"""
vlayer = QgsVectorLayer(f"?query={query}", "joined_virtual", "virtual")
QgsProject.instance().addMapLayer(vlayer)
3. Cache WFS First, Then Join
Download complete layers before attempting joins:
# Create a temporary GeoPackage
import tempfile
temp_dir = tempfile.gettempdir()
# Save both layers locally first
layer1_path = f"{temp_dir}/layer1_cache.gpkg"
layer2_path = f"{temp_dir}/layer2_cache.gpkg"
# Process in chunks if necessary (for large WFS)
processing.run("native:package", {'INPUT': layer1, 'OUTPUT': layer1_path})
processing.run("native:package", {'INPUT': layer2, 'OUTPUT': layer2_path})
# Load cached layers
cached_layer1 = QgsVectorLayer(layer1_path, "cached_layer1", "ogr")
cached_layer2 = QgsVectorLayer(layer2_path, "cached_layer2", "ogr")
# Now create the join on cached layers
join_info = QgsVectorLayerJoinInfo()
join_info.setJoinFieldName("join_field")
join_info.setTargetFieldName("target_field")
join_info.setJoinLayer(cached_layer2)
join_info.setUsingMemoryCache(True)
join_info.setPrefix("joined_")
cached_layer1.addJoin(join_info)
4. Use Processing Algorithm
The processing framework may handle this better than direct joins:
result = processing.run("native:joinattributestable", {
'INPUT': layer1,
'FIELD': 'target_field',
'INPUT_2': layer2,
'FIELD_2': 'join_field',
'METHOD': 1, # One-to-many relationship
'DISCARD_NONMATCHING': False,
'PREFIX': 'joined_',
'OUTPUT': 'memory:'
})
joined_layer = result['OUTPUT']
QgsProject.instance().addMapLayer(joined_layer)
The GeoPandas approach is likely to be the fastest for WFS data since it bypasses QGIS's join system entirely and uses pandas' highly optimized merging functions.
Would you like me to elaborate on any of these approaches?