Last active
August 21, 2018 08:48
-
-
Save ivn951/dce165985694983b7808060d7b812842 to your computer and use it in GitHub Desktop.
QGIS: Create new equidistant points along a line based on attribute data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ------------------------------- | |
# Original Author : ndawson | |
# ------------------------------- | |
from qgis.core import QgsFeature, QgsVectorFileWriter, QgsGeometry | |
def create_points(feat,writer): | |
geometry = feat.constGeometry() | |
if not geometry: | |
return | |
length = geometry.length() | |
# ----------------- | |
# change 'num_points' to match your field name for the number of points field | |
# ----------------- | |
num_points = feat['utenze'] | |
delta = length / ( num_points + 1.0 ) | |
distance = 0.0 | |
for i in range(num_points): | |
distance += delta | |
output_feature = QgsFeature(feat) | |
output_feature.setGeometry( geometry.interpolate(distance) ) | |
writer.addFeature(output_feature) | |
layer = iface.activeLayer() | |
# --------------- | |
# change 'C:\mydata\points.shp' to desired output file name | |
# --------------- | |
writer = QgsVectorFileWriter('C:\mydata\points.shp',None, layer.fields(), QGis.WKBPoint, layer.crs()) | |
for f in layer.getFeatures(): | |
create_points(f,writer) | |
del writer |
Tested only on QGIS Version 2.18
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Select the input layer, and run it in the python console paying attention to change the 'num_points' field name and output file name to match your data