Last active
September 29, 2021 08:24
-
-
Save ivn951/51f278bdf48f04277473e14335ebbb0f to your computer and use it in GitHub Desktop.
QGIS3: 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
#Script adapted# | |
#QGIS Python API 3.0# | |
from qgis.core import * | |
def create_points(feat,writer): | |
geometry = feat.geometry() | |
if not geometry: | |
return | |
length = geometry.length() | |
# ----------------- | |
# change 'n_points' to match your field name for the number of points field | |
# ----------------- | |
num_points = feat['n_points'] | |
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 '/path/points.shp' to desired output file name | |
# --------------- | |
writer = QgsVectorFileWriter('/path/points.shp', None, layer.fields(), QgsWkbTypes.Point, layer.crs()) | |
for f in layer.getFeatures(): | |
create_points(f,writer) | |
del writer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment