4

I have several buffers distributed along a whole country (therefore, the distances between them is very high). These buffers are represented in the same layer/shp.

I want to create a regular point layer (spaced between 100m) only within each buffer, but so far the only procedure I know is to create a regular point layer in all surface, and then use "clip" tool to get only the points within each buffer.

However, it seems impossible (at least with my computer) to create a regular point layer for a whole country separated by 100m, and then clip it.

Is there any other way to obtain the regular point layer only within each buffer?

2 Answers 2

3

It could be done with PyQGIS. Assuming that vector layer in next image is your buffer (it was relatively small for verification purposes):

enter image description here

I tried out next code for creating the regularly spaced point layer grid (100 m) for each feature.

layer = iface.activeLayer()

feats = [ feat for feat in layer.getFeatures()]

points = []

for feat in feats:
    extent = feat.geometry().boundingBox()
    xmin = extent.xMinimum()
    ymax = extent.yMaximum()
    xmax = extent.xMaximum()
    ymin = extent.yMinimum()

    rows = int(ymax - ymin)/100
    cols = int(xmax - xmin)/100

    x = xmin
    y = ymax

    geom_feat = feat.geometry()

    for i in range(rows+1):
        for j in range(cols+1):
            pt = QgsPoint(x,y)
            tmp_pt = QgsGeometry.fromPoint(pt)
            if tmp_pt.within(geom_feat):
                points.append(tmp_pt.asPoint())
            x +=100
        x = xmin
        y -= 100

epsg = layer.crs().postgisSrid()

#points
uri = "Point?crs=epsg:" + str(epsg) + "&field=id:integer""&index=yes"

mem_layer = QgsVectorLayer(uri,
                           'point',
                           'memory')

prov = mem_layer.dataProvider()

feats = [ QgsFeature() for i in range(len(points)) ]

for i, feat in enumerate(feats):
    feat.setAttributes([i])
    feat.setGeometry(QgsGeometry.fromPoint(points[i]))

prov.addFeatures(feats)

QgsMapLayerRegistry.instance().addMapLayer(mem_layer)

After running the above code at the Python Console of QGIS, I got:

enter image description here

It works adequately for these smalls areas.

1
  • Thank you very much! I have never used the Python Console, but I will try the code you provided!
    – mto23
    Commented Aug 30, 2016 at 13:54
2

Two possible way I can think of is

1) Instead of clipping the points to the buffers, use 'select by location' and save only the selected points out as a new layer. That could be less intensive on the ol' computer.

2) Save the buffers out separately and create the points layers for each individual buffer. Then 'Vector > Data Management Tools > Merge Vector Layers' to create a single layer of points

1
  • 1
    That's an option, but since I also have several buffers, it will take a while. It is better than crashing QGIS though!! Thank you for your suggestions!
    – mto23
    Commented Aug 30, 2016 at 13:54

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.