Skip to content

Instantly share code, notes, and snippets.

@ivn951
Last active August 21, 2018 08:48
Show Gist options
  • Save ivn951/dce165985694983b7808060d7b812842 to your computer and use it in GitHub Desktop.
Save ivn951/dce165985694983b7808060d7b812842 to your computer and use it in GitHub Desktop.
QGIS: Create new equidistant points along a line based on attribute data
# -------------------------------
# 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
@ivn951
Copy link
Author

ivn951 commented Aug 20, 2018

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

@ivn951
Copy link
Author

ivn951 commented Aug 21, 2018

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