How To Buffer a Selected Point in pyQGIS

33 Views Asked by At

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

There are 1 best solutions below

0
lhowarth On

You don't have to pass by an intermediary memory layer (though you could). You don't need to change much, just modify the input value of buffer processing tool like below.

Get a QgsVectorLayer instance (use your original code here):

pnts_0 = iface.activeLayer()

Then setup your 'pnts' input to use just the selected features:

pnts = QgsProcessingFeatureSourceDefinition(pnts_0.id(), selectedFeaturesOnly=True, featureLimit=-1, geometryCheck=QgsFeatureRequest.GeometryAbortOnInvalid)

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

EDIT:

actually i got a little bored and played with it some more, you could do somthing like:

class randomBufferedPoint():

    def randpoints(self, layer, num:int): # Note I added Num so you can modify the number of points when calling the method.
        return processing.runAndLoadResults("native:randompointsinpolygons", {'INPUT':layer,
                                                                'POINTS_NUMBER':num,
                                                                'MIN_DISTANCE':0,
                                                                'MIN_DISTANCE_GLOBAL':0,
                                                                'MAX_TRIES_PER_POINT':10,
                                                                'SEED':None,
                                                                'INCLUDE_POLYGON_ATTRIBUTES':True,
                                                                'OUTPUT':'memory:'})

    def randsel(self, layer, num:int): # Note I added Num so you can modify the number of points to select.
        return processing.run("qgis:randomselection", 
                       {'INPUT':layer,
                        'METHOD':0,'NUMBER':num})


    def bufthis(self, layer, dist:int): # Note I added dist so you can modify the distance for the buffer
        return processing.runAndLoadResults("native:buffer", 
                                  {'INPUT':layer,
                                   'DISTANCE':dist,
                                   'SEGMENTS':6,
                                   'END_CAP_STYLE':0,
                                   'JOIN_STYLE':1,
                                   'MITER_LIMIT':3,
                                   'DISSOLVE':True,
                                   'SEPARATE_DISJOINT':False,
                                   'OUTPUT':'memory:'})
                                   

rbp = randomBufferedPoint()

proj = QgsProject.instance()
lyr = proj.mapLayersByName('aoi')[0]

pts_layer = proj.mapLayer(rbp.randpoints(lyr, 5)['OUTPUT'])
sel_elems = rbp.randsel(pts_layer, 3)['OUTPUT']
pnts = QgsProcessingFeatureSourceDefinition(sel_elems.id(), selectedFeaturesOnly=True, featureLimit=-1, geometryCheck=QgsFeatureRequest.GeometryAbortOnInvalid)
rbp.bufthis(pnts, 5000)

But obviously it depends on how you want to apply/ integrate the code