[GIS] Creating points projected on line using QGIS
draw-interactionpoints-to-lineqgisvector-layer
How can we create points perpendicularly on a line with QGis ?
Otherwise, how can we draw a line between one point and a line ?
Here is a picture to show what we want :
Best Answer
If you only need to show these lines/points, you can do this with a geometry generator marker style. This is a fairly new feature in QGIS, and it makes it easy to do things that would have needed a database before. I did this in QGIS 2.18
I assume you have a line layer called "ligne", with a unique id field. I called this "unid", and set to 1
then use the following formula on your points layer in the geometry generator. Make sure you set the output type to be "linestring". If it's not, nothing will appear.
Here's a quick PyQGIS script which should do the trick
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['num_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 'd:/test_points.shp' to desired output file name
# ---------------
writer = QgsVectorFileWriter('d:/test_points.shp',None, layer.fields(), QGis.WKBPoint, layer.crs())
for f in layer.getFeatures():
create_points(f,writer)
del writer
Just change the num_points field name and output file name to match your data, select the input layer, and run it in the python console.
Best Answer
If you only need to show these lines/points, you can do this with a geometry generator marker style. This is a fairly new feature in QGIS, and it makes it easy to do things that would have needed a database before. I did this in QGIS 2.18
I assume you have a line layer called "ligne", with a unique id field. I called this "unid", and set to 1
then use the following formula on your points layer in the geometry generator. Make sure you set the output type to be "linestring". If it's not, nothing will appear.
Here's an example. I've using a normal dot symbol and a generator on the points layer
To show the points projected onto the line, add another geometry generator marker, this time make it a POINT
This is not guaranteed to be perpendicular to the line, for example:-
If a point is quite close to the line, the shortest line is likely to be perpendicular.