2

I asked a question some days ago (see Extent of a group of lines in a polyline shapefile) and got a nice answer that works: use aggregate procedure in the processing toolbox. Unfortunately, this process generates a 'temporary layer", that is lost when you close the application. So, I tried to make this process reproducible. I saw that, when preparing the request in the aggregate screen, you can, in the advanced button, "Copy as Python command", and then I could save it. I want to replay this procedure, but when I run it in the Python console, it seems to run but generate nothing.

What can I do with this procedure, where can I run it again?

The procedure generated is as follows:

processing.run("native:aggregate", {'INPUT':'D:/Data_CV/CapDemain5140/Sentiers5140/Projets/2022 PointsNoeudsPietonsProvince/CartographiePNP/JonctionsPN.shp','GROUP_BY':'NULL','AGGREGATES':[{'aggregate': 'first_value','delimiter': ',','input': '"RespCtrl"','length': 5,'name': 'RespCtrl','precision': 0,'sub_type': 0,'type': 10,'type_name': 'text'},{'aggregate': 'concatenate','delimiter': ',','input': '"Nom"','length': -1,'name': 'Nom','precision': 0,'sub_type': 0,'type': 10,'type_name': 'text'},{'aggregate': 'sum','delimiter': ',','input': '"Longueur"','length': -1,'name': 'Longueur','precision': 0,'sub_type': 0,'type': 6,'type_name': 'double precision'},{'aggregate': 'concatenate','delimiter': ',','input': '"PNExtremit"','length': -1,'name': 'PNExtremit','precision': 0,'sub_type': 0,'type': 10,'type_name': 'text'},{'aggregate': 'sum','delimiter': ',','input': '"NbPick"','length': 10,'name': 'NbPick','precision': 0,'sub_type': 0,'type': 2,'type_name': 'integer'},{'aggregate': 'concatenate','delimiter': ',','input': '"PickNumber"','length': -1,'name': 'PickNumber','precision': 0,'sub_type': 0,'type': 10,'type_name': 'text'}],'OUTPUT':'TEMPORARY_OUTPUT'})

2 Answers 2

2

For a simple script like this you can also save it in the script editor alongside the Python console:

python script editor

This makes it much easier to edit and reuse. Then, as already mentioned, you can direct the output to a file (line 12 in my example)

For a more serious approach you can try making a graphical processing model, which can also preserve an aggregate configuration. With a processing model, you can specify the output destination easily and graphically with each run.

aggregate processing model

3
  • Thanks for your comment. When I create and run it in the preparation screen of "aggregate" function from the Processing toolbox, I have a layer appearing in the layer list. If I close and reopen QGis, this layer desapears as it is an "in memory" layer. So in the Python console, I run the script, and I was expecting the "in memory" layer to be recreated, but nothing appeared and no error highlighted in the console.
    – ClaudeVer
    Commented Jan 12 at 18:27
  • In the Aggregate preparation screen of the Processing toolbox, you can also set the output destination to a file (the last parameter). Have you tried that? And in the Python console, if you specify a .shp as output, the Shapefile will be created but not loaded (and no errors will be raised). To also load the created layer, replace processing.run() with processing.runAndLoadResults()
    – Andre Geo
    Commented Jan 13 at 20:25
  • Yes it works with shp file and runandload. Thanks
    – ClaudeVer
    Commented Jan 15 at 9:29
2

It is generating an output in a temporary directory. See the 'OUTPUT':'TEMPORARY_OUTPUT' parameter at the end.

You have various options how to proceed including:

  1. Replacing 'TEMPORARY_OUTPUT' with a file path (shapefile or geopackage compliant path)

  2. Assign the procedure to a variable such as

result = processing.run("native:aggregate", {'INPUT':'D:/Data_CV/CapDemain5140/Sentiers5140/Projets/2022 PointsNoeudsPietonsProvince/CartographiePNP/JonctionsPN.shp','GROUP_BY':'NULL','AGGREGATES':[{'aggregate': 'first_value','delimiter': ',','input': '"RespCtrl"','length': 5,'name': 'RespCtrl','precision': 0,'sub_type': 0,'type': 10,'type_name': 'text'},{'aggregate': 'concatenate','delimiter': ',','input': '"Nom"','length': -1,'name': 'Nom','precision': 0,'sub_type': 0,'type': 10,'type_name': 'text'},{'aggregate': 'sum','delimiter': ',','input': '"Longueur"','length': -1,'name': 'Longueur','precision': 0,'sub_type': 0,'type': 6,'type_name': 'double precision'},{'aggregate': 'concatenate','delimiter': ',','input': '"PNExtremit"','length': -1,'name': 'PNExtremit','precision': 0,'sub_type': 0,'type': 10,'type_name': 'text'},{'aggregate': 'sum','delimiter': ',','input': '"NbPick"','length': 10,'name': 'NbPick','precision': 0,'sub_type': 0,'type': 2,'type_name': 'integer'},{'aggregate': 'concatenate','delimiter': ',','input': '"PickNumber"','length': -1,'name': 'PickNumber','precision': 0,'sub_type': 0,'type': 10,'type_name': 'text'}],'OUTPUT':'TEMPORARY_OUTPUT'})

This will return a dictionary. To capture the processed QgsVectorLayer you can use

result['OUTPUT']

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.