2

To repair geometries errors, I'm now trying processing (grass7) using PyQGIS. For one polygon layer (QgsVectorLayer: 'hn_pg'), I wrote the below script and run the grass7:v.clean algorithm. The processing completed without any problems, but the cleaned layer could not appear to the current active layers.

output = processing.runAndLoadResults(
            "grass7:v.clean",
           {'input': hn_pg,
            'type':[0,1,2,3,4,5,6], 
            'tool':[1],   # snap 
            'threshold':0.5,
            '-b':False,
            '-c':False,
            'GRASS_SNAP_TOLERANCE_PARAMETER':1,
            'GRASS_REGION_PARAMETER': "%f, %f, %f, %f"%hn_pg.extent().xMinimum(),hn_pg.extent().xMaximum(), hn_pg.extent().yMinimum(), hn_pg.extent().yMaximum()),
            'GRASS_MIN_AREA_PARAMETER':0.0001,
            'GRASS_OUTPUT_TYPE_PARAMETER':0,
            'output':'memory:',
            'GRASS_VECTOR_DSCO' : '',
            'GRASS_VECTOR_EXPORT_NOCAT' : False,
            'GRASS_VECTOR_LCO' : '',
            'error' : 'memory:'})

QgsProcessingUtils.tempFolder() function provided the path of the completed file(.gpkg) and I also found the outputted file in that file. So, I tried QgsProject.instance().addMapLayer(output['output']) but resulted in no reaction.

How Can I get the cleaned object in Qgis active layers?

3

Try replacing memory: with TEMPORARY_OUTPUT. The following code works for me which was executed on a polygon .gpkg file:

hn_pg = iface.activeLayer()
output = processing.runAndLoadResults(
            "grass7:v.clean",
            {'input': hn_pg,
            'type':[0,1,2,3,4,5,6], 
            'tool':[1],   # snap 
            'threshold':0.5,
            '-b':False,
            '-c':False,
            'GRASS_SNAP_TOLERANCE_PARAMETER':1,
            'GRASS_REGION_PARAMETER': "%f, %f, %f, %f"%(hn_pg.extent().xMinimum(),hn_pg.extent().xMaximum(), hn_pg.extent().yMinimum(), hn_pg.extent().yMaximum()),
            'GRASS_MIN_AREA_PARAMETER':0.0001,
            'GRASS_OUTPUT_TYPE_PARAMETER':0,
            'output':'TEMPORARY_OUTPUT',
            'GRASS_VECTOR_DSCO' : '',
            'GRASS_VECTOR_EXPORT_NOCAT' : False,
            'GRASS_VECTOR_LCO' : '',
            'error':'TEMPORARY_OUTPUT'})

Tested on QGIS 3.8.1 on Win 7 64-bit.

  • 1
    Thank you for quick helpful answer. Your code did work for my environment (QGIS 3.8.1) ! – Lc_decg Sep 7 at 9:41
  • @Lc_decg - Most welcome! Glad it helped :) – Joseph Sep 10 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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