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()
?
1 Answer 1
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).
-
1Thanks 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...maxwhere– maxwhere2017年05月09日 02:49:09 +00:00Commented 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 :)mgri– mgri2017年05月09日 06:48:04 +00:00Commented May 9, 2017 at 6:48