5

Using the code outlined in the question: How to create square buffers around points in QGIS with Python? I created a layer of square buffers from coordinate points, however I was unable to carry over all of the point attributes to the new layer.

We have the following columns in our point attributes:

BigCellID (integer),
BigCellTyp (integer),
x,
y

The following code carried over the x and y columns perfectly:

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)

However to get it to also carry over the other two columns I made the following modification to one line:

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

instead of:

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

This modification did not affect the x and y columns being carried over, and did create new columns; but the column BigCellID was filled with sequentially increasing numbers from 0, while BigCellTyp was filled with 'Null'. These are not the original values.

I would like to know how I can modify this code to ensure the contents of my two columns BigCellID and BigCellTyp are carried over to the new buffer layer?

asked Nov 15, 2016 at 3:33
1
  • You need to modify the line where is new_feat.setAttributes command (see my answer). Commented Nov 15, 2016 at 5:23

1 Answer 1

1

If you did this modification:

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

you need this one:

.
.
.
for i, feat in enumerate(feats):
 .
 .
 .
 new_feat.setAttributes([i, point[0], point[1], BigCellID[i], BigCellTyp[i]])
 .
 .
 .

to fill all fields with values that you are expected. Of course, before the loop, you need to have BigCellID and BigCellTyp defined as a list of values with the same dimension of features number.

Editing Note:

As you said: "We have the following columns in our point attributes:

BigCellID (integer), BigCellTyp (integer)..."

so, next code should work well:

layer = iface.activeLayer()
feats = [ feat for feat in layer.getFeatures() ]
n = len(feats)
BigCellID = [ feats[i].attribute('BigCellID') for i in range(n) ]
BigCellTyp = [ feats[i].attribute('BigCellTyp') for i in range(n) ]
epsg = layer.crs().postgisSrid()
uri = "Polygon?crs=epsg:" + str(epsg) + "&field=id:integer&field=x:real&field=y:real&field=BigCelID:integer&field=BigCellTyp: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], BigCellID[i], BigCellTyp[i]])
 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)

I tried it out with my own point layer (where I defined 'BigCellID' and 'BigCellTyp' fields with arbitrary values) and it works fine.

enter image description here

answered Nov 15, 2016 at 5:19
2
  • OK, thank you! I can see where I went wrong, however being unfamiliar with python I am not sure how to define those those lists, or when to run that line. Commented Nov 16, 2016 at 2:06
  • Done. Please, see my "Editing Note". Commented Nov 16, 2016 at 9:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.