2

I have a few random points that I generated within a polygon. I randomly selected one of the points and would like to buffer that point. In the case below, buffering will be applied to all points and not the selected point.

Do I need to create a new memory layer of the selected point first? If so, how?

#Generating random points within an "aoi" polygon file
pnts = processing.runAndLoadResults("native:randompointsinpolygons", {
                                    'INPUT': aoi,
                                    'POINTS_NUMBER': 10,
                                    'MIN_DISTANCE': 0,                                    
                                    'MIN_DISTANCE_GLOBAL': 0,                                    
                                    'MAX_TRIES_PER_POINT': 10,
                                    'SEED': None,
                                    'INCLUDE_POLYGON_ATTRIBUTES': True,
                                    'OUTPUT': 'memory:'
                                    }) 

#Randomly Choose one of the Points
processing.run("qgis:randomselection", {
                'INPUT': pnts['OUTPUT'],
                'METHOD': 0,
                'NUMBER':1
                })

#Buffering the point
pnt_buf = processing.runAndLoadResults("native:buffer", {
                           'INPUT':pnts,
                           'DISTANCE':3000,
                           'SEGMENTS':6,
                           'END_CAP_STYLE':0,
                           'JOIN_STYLE':1,
                           'MITER_LIMIT':3,
                           'DISSOLVE':True,
                           'SEPARATE_DISJOINT':False,
                           'OUTPUT':'memory:'
                           })
1

1 Answer 1

2

As per this question and answer Select feature by expression and run "native:buffer" on selection, works for "id" field but no other field in PyQGIS use a QgsProcessingFeatureSourceDefinition as follows (in the native:buffer call)

pnt_buf = processing.runAndLoadResults("native:buffer", {
    'INPUT': QgsProcessingFeatureSourceDefinition(pnts['OUTPUT'], selectedFeaturesOnly=True),
    'DISTANCE': 3000,
    'SEGMENTS': 6,
    'END_CAP_STYLE': 0,
    'JOIN_STYLE': 1,
    'MITER_LIMIT': 3,
    'DISSOLVE': True,
    'SEPARATE_DISJOINT': False,
    'OUTPUT': 'memory:'
    })

This will alter the input to the buffer algorithm on the fly.

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.