3

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)'
                })
2

2 Answers 2

0

Looks like it's derived from ogr2ogr and the 'a_srs' parameter can be applied to the output.

https://gdal.org/programs/ogr2ogr.html#cmdoption-ogr2ogr-a_srs

0
0

I'm unable to reproduce your issue with QGIS 3.16. Try using processing.run and add yourself the layer instead of using processing.runAndLoadResults like below

for layer in orderedLayers:
    result = processing.run("qgis:extractbyextent", {
                "INPUT": orderedLayers[layer], 
                "EXTENT": iface.mapCanvas().extent(), 
                "CLIP": 'False',
                "OUTPUT": 'ogr:dbname=\'C:/my_filepath/my_gpkg.gpkg\' table=\"' + layer + '\" (geom)'})
    l = QgsVectorLayer(result['OUTPUT'], 'demo')
    QgsProject.instance().addMapLayer(l)

Your issue, that a windows appears each time to ask you to select a projection may also be related to the fact you may not have choose the transformation dialog behavior whereas your projection in destination file could be fine. So, take a look at Deciding whether to Select Transformation in QGIS

1
  • I tried using processing.run but layers still had no CRS assigned. Another workaround I tried was setting the output to 'memory: ' and then saving the layer to the gpkg with "native:savefeatures". This worked but is an extra step and I was looking for a way to set the CRS at the output stage. Jul 27, 2021 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.