I'm using PyQGIS to run the processing algorithm "qgis:extractbyextent"
. The algorithm runs in a for loop using the current extent to perform the clip. Saving the OUTPUT
to memory works fine and all CRS information is present:
for layer in orderedLayers:
processing.runAndLoadResults("qgis:extractbyextent", {
"INPUT": orderedLayers[layer],
"EXTENT": iface.mapCanvas().extent(),
"CLIP": 'False',
"OUTPUT": 'memory:'})
If I try and save the OUTPUT
to a GeoPackage (using the code below) the CRS information is lost for each saved layer. The context box pops up and I have to specify the CRS before the layer is saved to the gpkg. Once saved checking the layers in the gpkg no CRS information is present and adding a layer to the map from the browser panel requires the CRS to be specified again.
for layer in orderedLayers:
processing.runAndLoadResults("qgis:extractbyextent", {
"INPUT": orderedLayers[layer],
"EXTENT": iface.mapCanvas().extent(),
"CLIP": 'False',
"OUTPUT": 'ogr:dbname=\'C:/my_filepath/my_gpkg.gpkg\' table=\"' + layer + '\" (geom)'})
Is there a way to specify the output CRS?
Update
Using just processing.run
still lost the CRS information. One workaround is to add an extra step first setting OUTPUT
to 'memory: '
and then using "native:savefeatures"
to save to the gpkg.
To include the CRS in the OUTPUT
the a_srs option can be used as shown below:
for layer in reversed(orderedLayers):
processing.runAndLoadResults("qgis:extractbyextent", {
"INPUT": orderedLayers[layer],
"EXTENT": iface.mapCanvas().extent(),
"CLIP": 'False',
"OUTPUT": 'ogr:dbname=\'C:/my_filepath/my_gpkg.gpkg\' table=\"' + layer + '\" a_srs="EPSG:27700" (geom)'
})