I am looking for a solution to distribute with a python script equidistant points in QGIS along a polygon. I tried to use the following code example:
http://nathanw.net/2012/08/05/generating-chainage-distance-nodes-in-qgis/
Using it in my example (running over each polygon from a polygon shape file):
# Reading a polygon layer
pp_layer = QgsVectorLayer("poly.shp" , "polylyr", "ogr")
# create layer to store result"
pntl = QgsVectorLayer("Point", "name", "memory")
pr = pntl.dataProvider()
for feature in pp_layer.selectedFeatures():
geom = feature.geometry()
currentdistance = self.distance
feats = []
# geom.asPolyline()
while currentdistance < length:
point = geom.interpolate(currentdistance)
print point
fet = QgsFeature()
fet.setGeometry(point)
feats.append(fet)
currentdistance = currentdistance + self.distance
pr.addFeatures(feats)
However, when running the code, if fails. I got an error message in the python console
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/home2/fun.py", line 92, in run_fun
fet.setGeometry(point)
TypeError: QgsFeature.setGeometry(QgsGeometry): argument 1 has unexpected type 'NoneType'
an I got an errormessage from qgis
ERROR 1: IllegalArgumentException: LinearIterator only supports lineal geometry components
Does anyone has an idea, if i have an error in my code or is it not possible to solve the question with this approach? I am using QGIS 2.0.1.
Best Answer
You cannot use interpolate() with polygons:
If you have a polygon:
The LinearRing of the Polygon is:
Polygons:
Part of a class to create memory layers from the Python console:
Creation of the equidistant points (step = 100m)
Result: