2

I have a polygon layer upon which i want to create a point layer (regular points) depending on each feature's extent of the polygon layer. So far i have to create a field containing the adequate string: concat(x_min($geometry),',', x_max($geometry),',', y_min($geometry),',', y_max($geometry)

And then parse that field in a loop:

for feature in layer.getFeatures():
 attrs = feature.attributes()
processing.runalg("qgis:regularpoints",attrs[14],1000,0,False,True,outfile)

Since my approach appears to be rather bulky and inflexible i was wondering whether there are more direct ways to get the extends - something like layer.getFeaturesExt() or layer.getFeaturesBbox()?

mgri
16.4k6 gold badges48 silver badges80 bronze badges
asked May 6, 2017 at 11:57

1 Answer 1

6

Assuming that layer is your polygon layer, you may use this code:

for feature in layer.getFeatures():
 bbox = feature.geometry().boundingBox()
 bbox_extent = '%f,%f,%f,%f' % (bbox.xMinimum(), bbox.xMaximum(), bbox.yMinimum(), bbox.yMaximum())
 processing.runalg("qgis:regularpoints",bbox_extent,1000,0,False,True,outfile)

and you will create a grid of regular points for each feature of your polygon layer (without the needing of preliminarily creating a field that stores the coordinates).

answered May 6, 2017 at 12:09
2
  • 1
    Thanks a lot for your quick reply, it works! but still i am wondering why there isn't a less verbose way since a lot of these vector creation tools require the extend in that specific string formatting... Commented May 9, 2017 at 2:49
  • @maxwhere I'm glad it worked! However, you have done the job with four lines of code, I don't think it is too much verbose :) Commented May 9, 2017 at 6:48

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.