6

Using the following code from a similar question (How to create square buffers around points in QGIS with Python?) I am able to create square buffers (5x5 degree cells) around my points.

However, I also need to make 5x10 degree cells and 10x20, is there a way I can modify the following code to get this result?

Code:

layer = iface.activeLayer()

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

epsg = layer.crs().postgisSrid()

uri = "Polygon?crs=epsg:" + str(epsg) + "&field=id:integer&field=x:real&field=y:real&field=point_id:integer""&index=yes"

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

prov = mem_layer.dataProvider()

for i, feat in enumerate(feats):
    point = feat.geometry().asPoint()
    new_feat = QgsFeature()
    new_feat.setAttributes([i, point[0], point[1], feat.id()])
    tmp_feat = feat.geometry().buffer(1000, -1).boundingBox().asWktPolygon()
    new_feat.setGeometry(QgsGeometry.fromWkt(tmp_feat))
    prov.addFeatures([new_feat])

QgsMapLayerRegistry.instance().addMapLayer(mem_layer)
3
  • Perhaps these answers can help gis.stackexchange.com/questions/216394/….
    – user30184
    Commented Nov 9, 2016 at 21:18
  • Cheers, I can add length and width columns to my points table, but I was wondering if there is a way to build that into the tidy code above. I can get squares with this line: tmp_feat = feat.geometry().buffer(1000, -1).boundingBox().asWktPolygon() Yet I feel that there should be a way to add into the buffer function some kind of length and width specifications. However I am finding it hard to locate this.
    – Amroco
    Commented Nov 9, 2016 at 23:23
  • Yes, it is possible to modify the code for getting "rectangular" buffers (please, see my answer).
    – xunilk
    Commented Nov 9, 2016 at 23:49

1 Answer 1

8

If the code is modified as follow, it can be obtained rectangular buffers as you expected.

layer = iface.activeLayer()

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

epsg = layer.crs().postgisSrid()

uri = "Polygon?crs=epsg:" + str(epsg) + "&field=id:integer&field=x:real&field=y:real&field=point_id:integer""&index=yes"

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

prov = mem_layer.dataProvider()

for i, feat in enumerate(feats):
    point = feat.geometry().asPoint()
    new_feat = QgsFeature()
    new_feat.setAttributes([i, point[0], point[1], feat.id()])
    bbox = feat.geometry().buffer(1000, -1).boundingBox()
    tmp_feat = bbox.asWktPolygon()
    xmin1,ymin1,xmax1,ymax1 = bbox.toRectF().getCoords()
    xmin2,ymin2,xmax2,ymax2 = feat.geometry().buffer(2000, -1).boundingBox().toRectF().getCoords()
    p1 = QgsPoint(xmin1, ymax2)
    p2 = QgsPoint(xmax1, ymin2)
    new_ext = QgsRectangle(p1,p2)
    new_tmp_feat = new_ext.asWktPolygon()
    new_feat.setGeometry(QgsGeometry.fromWkt(new_tmp_feat))
    prov.addFeatures([new_feat])

QgsMapLayerRegistry.instance().addMapLayer(mem_layer)

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

enter image description here

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.