Creating closest distance line between points and lines in QGIS
QGIS
I have multiple lines in one layer (trench) and multiple points in another later.
I've searched the whole Internet for a plug-in which can: connect points to the nearest line, creating a new line in a new layer.
Maybe the plug-in is already there, but I couldn't find it...
What I have:
Desired output:
qgis points-to-line
New contributor
add a comment |
QGIS
I have multiple lines in one layer (trench) and multiple points in another later.
I've searched the whole Internet for a plug-in which can: connect points to the nearest line, creating a new line in a new layer.
Maybe the plug-in is already there, but I couldn't find it...
What I have:
Desired output:
qgis points-to-line
New contributor
1
Which software is that? QGIS or ArcGIS?
– Taras
15 hours ago
Interesting would be the feature count of both of your layers. It looks like your using OpenStreetMap data. According to big data sets you have to think of spatial indexes.
– Stefan
14 hours ago
add a comment |
QGIS
I have multiple lines in one layer (trench) and multiple points in another later.
I've searched the whole Internet for a plug-in which can: connect points to the nearest line, creating a new line in a new layer.
Maybe the plug-in is already there, but I couldn't find it...
What I have:
Desired output:
qgis points-to-line
New contributor
QGIS
I have multiple lines in one layer (trench) and multiple points in another later.
I've searched the whole Internet for a plug-in which can: connect points to the nearest line, creating a new line in a new layer.
Maybe the plug-in is already there, but I couldn't find it...
What I have:
Desired output:
qgis points-to-line
qgis points-to-line
New contributor
New contributor
edited 13 hours ago
Taras
2,1972726
2,1972726
New contributor
asked 16 hours ago
Mathijs AlkemaMathijs Alkema
112
112
New contributor
New contributor
1
Which software is that? QGIS or ArcGIS?
– Taras
15 hours ago
Interesting would be the feature count of both of your layers. It looks like your using OpenStreetMap data. According to big data sets you have to think of spatial indexes.
– Stefan
14 hours ago
add a comment |
1
Which software is that? QGIS or ArcGIS?
– Taras
15 hours ago
Interesting would be the feature count of both of your layers. It looks like your using OpenStreetMap data. According to big data sets you have to think of spatial indexes.
– Stefan
14 hours ago
1
1
Which software is that? QGIS or ArcGIS?
– Taras
15 hours ago
Which software is that? QGIS or ArcGIS?
– Taras
15 hours ago
Interesting would be the feature count of both of your layers. It looks like your using OpenStreetMap data. According to big data sets you have to think of spatial indexes.
– Stefan
14 hours ago
Interesting would be the feature count of both of your layers. It looks like your using OpenStreetMap data. According to big data sets you have to think of spatial indexes.
– Stefan
14 hours ago
add a comment |
2 Answers
2
active
oldest
votes
You can use the QGIS Python console for this. The output results as a memory layer. I'm using QGIS 2.18.28. Probably in QGIS 3 the code does not work, due to software changes.
Just paste this code into the Python console. You have to edit the code according to your layer names. In my case I have a shapefile points and a shapefile lines. You have to know the projection of your shapefiles and they have to have the same projection. Otherwise we have to do some transformation. Choose the right projection when adding the memory layer.
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
from qgis.networkanalysis import *
# define input layer points and lines
p_lyr = QgsMapLayerRegistry.instance().mapLayersByName('points')[0] # set your point layer name here
l_lyr = QgsMapLayerRegistry.instance().mapLayersByName('lines')[0] # set your line layer name here
lines = [feature for feature in l_lyr.getFeatures()]
# set up memory layer for the closest distance line
d_lyr = QgsVectorLayer('LineString', 'normalVector', 'memory')
QgsMapLayerRegistry.instance().addMapLayer(d_lyr)
prov = d_lyr.dataProvider()
# adding three attributes (holding point_id, line_id and the distance)
prov.addAttributes( [ QgsField("point_id", QVariant.Int), QgsField("line_id", QVariant.Int), QgsField("distance",QVariant.Int)])
feat =
for points in p_lyr.getFeatures():
# find closest point to line
minDistPoint = min([l.geometry().closestSegmentWithContext(QgsPoint(points.geometry().asPoint())) for l in lines])[1]
feat = QgsFeature()
# create line from point to minDistPoint
line = QgsGeometry.fromPolyline([QgsPoint(points.geometry().asPoint()), QgsPoint(minDistPoint[0], minDistPoint[1])])
feat.setGeometry(line)
# adding point id, line id and length of the closest distance line as a feature to the memory layer
feat.setAttributes([int(points["id"]), int(l["id"]), line.geometry().length()])
prov.addFeatures([feat])
d_lyr.updateExtents()
d_lyr.triggerRepaint()
d_lyr.updateFields()
This won't work at my Qgis.. Maybe I'm doing something wrong..? Can you pm me, so I can share my shapefiles with you?
– Mathijs Alkema
13 hours ago
Please clarify: QGIS version (2 or 3), any errors (Python console or other). Do you know how to use the Python console? See the docs docs.qgis.org/2.18/en/docs/user_manual/plugins/….
– Stefan
11 hours ago
my shapefiles have an attribute called "id". For the resulting closest distance lines they will be adopted. See the penultimate line in the for loop:feat.setAttributes([int(points["id"]), int(l["id"]), line.geometry().length()])
. You have to replace the "id" regarding the attributes of your shapefiles. I can edit the code when only the distance is needed.
– Stefan
11 hours ago
add a comment |
There is not such a plugin. And if you do not want to use python, you can combine two build-in algorithms in QGIS3
I think that this answer can solve your problem: https://gis.stackexchange.com/a/280787/7849
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "79"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Mathijs Alkema is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f315171%2fcreating-closest-distance-line-between-points-and-lines-in-qgis%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use the QGIS Python console for this. The output results as a memory layer. I'm using QGIS 2.18.28. Probably in QGIS 3 the code does not work, due to software changes.
Just paste this code into the Python console. You have to edit the code according to your layer names. In my case I have a shapefile points and a shapefile lines. You have to know the projection of your shapefiles and they have to have the same projection. Otherwise we have to do some transformation. Choose the right projection when adding the memory layer.
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
from qgis.networkanalysis import *
# define input layer points and lines
p_lyr = QgsMapLayerRegistry.instance().mapLayersByName('points')[0] # set your point layer name here
l_lyr = QgsMapLayerRegistry.instance().mapLayersByName('lines')[0] # set your line layer name here
lines = [feature for feature in l_lyr.getFeatures()]
# set up memory layer for the closest distance line
d_lyr = QgsVectorLayer('LineString', 'normalVector', 'memory')
QgsMapLayerRegistry.instance().addMapLayer(d_lyr)
prov = d_lyr.dataProvider()
# adding three attributes (holding point_id, line_id and the distance)
prov.addAttributes( [ QgsField("point_id", QVariant.Int), QgsField("line_id", QVariant.Int), QgsField("distance",QVariant.Int)])
feat =
for points in p_lyr.getFeatures():
# find closest point to line
minDistPoint = min([l.geometry().closestSegmentWithContext(QgsPoint(points.geometry().asPoint())) for l in lines])[1]
feat = QgsFeature()
# create line from point to minDistPoint
line = QgsGeometry.fromPolyline([QgsPoint(points.geometry().asPoint()), QgsPoint(minDistPoint[0], minDistPoint[1])])
feat.setGeometry(line)
# adding point id, line id and length of the closest distance line as a feature to the memory layer
feat.setAttributes([int(points["id"]), int(l["id"]), line.geometry().length()])
prov.addFeatures([feat])
d_lyr.updateExtents()
d_lyr.triggerRepaint()
d_lyr.updateFields()
This won't work at my Qgis.. Maybe I'm doing something wrong..? Can you pm me, so I can share my shapefiles with you?
– Mathijs Alkema
13 hours ago
Please clarify: QGIS version (2 or 3), any errors (Python console or other). Do you know how to use the Python console? See the docs docs.qgis.org/2.18/en/docs/user_manual/plugins/….
– Stefan
11 hours ago
my shapefiles have an attribute called "id". For the resulting closest distance lines they will be adopted. See the penultimate line in the for loop:feat.setAttributes([int(points["id"]), int(l["id"]), line.geometry().length()])
. You have to replace the "id" regarding the attributes of your shapefiles. I can edit the code when only the distance is needed.
– Stefan
11 hours ago
add a comment |
You can use the QGIS Python console for this. The output results as a memory layer. I'm using QGIS 2.18.28. Probably in QGIS 3 the code does not work, due to software changes.
Just paste this code into the Python console. You have to edit the code according to your layer names. In my case I have a shapefile points and a shapefile lines. You have to know the projection of your shapefiles and they have to have the same projection. Otherwise we have to do some transformation. Choose the right projection when adding the memory layer.
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
from qgis.networkanalysis import *
# define input layer points and lines
p_lyr = QgsMapLayerRegistry.instance().mapLayersByName('points')[0] # set your point layer name here
l_lyr = QgsMapLayerRegistry.instance().mapLayersByName('lines')[0] # set your line layer name here
lines = [feature for feature in l_lyr.getFeatures()]
# set up memory layer for the closest distance line
d_lyr = QgsVectorLayer('LineString', 'normalVector', 'memory')
QgsMapLayerRegistry.instance().addMapLayer(d_lyr)
prov = d_lyr.dataProvider()
# adding three attributes (holding point_id, line_id and the distance)
prov.addAttributes( [ QgsField("point_id", QVariant.Int), QgsField("line_id", QVariant.Int), QgsField("distance",QVariant.Int)])
feat =
for points in p_lyr.getFeatures():
# find closest point to line
minDistPoint = min([l.geometry().closestSegmentWithContext(QgsPoint(points.geometry().asPoint())) for l in lines])[1]
feat = QgsFeature()
# create line from point to minDistPoint
line = QgsGeometry.fromPolyline([QgsPoint(points.geometry().asPoint()), QgsPoint(minDistPoint[0], minDistPoint[1])])
feat.setGeometry(line)
# adding point id, line id and length of the closest distance line as a feature to the memory layer
feat.setAttributes([int(points["id"]), int(l["id"]), line.geometry().length()])
prov.addFeatures([feat])
d_lyr.updateExtents()
d_lyr.triggerRepaint()
d_lyr.updateFields()
This won't work at my Qgis.. Maybe I'm doing something wrong..? Can you pm me, so I can share my shapefiles with you?
– Mathijs Alkema
13 hours ago
Please clarify: QGIS version (2 or 3), any errors (Python console or other). Do you know how to use the Python console? See the docs docs.qgis.org/2.18/en/docs/user_manual/plugins/….
– Stefan
11 hours ago
my shapefiles have an attribute called "id". For the resulting closest distance lines they will be adopted. See the penultimate line in the for loop:feat.setAttributes([int(points["id"]), int(l["id"]), line.geometry().length()])
. You have to replace the "id" regarding the attributes of your shapefiles. I can edit the code when only the distance is needed.
– Stefan
11 hours ago
add a comment |
You can use the QGIS Python console for this. The output results as a memory layer. I'm using QGIS 2.18.28. Probably in QGIS 3 the code does not work, due to software changes.
Just paste this code into the Python console. You have to edit the code according to your layer names. In my case I have a shapefile points and a shapefile lines. You have to know the projection of your shapefiles and they have to have the same projection. Otherwise we have to do some transformation. Choose the right projection when adding the memory layer.
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
from qgis.networkanalysis import *
# define input layer points and lines
p_lyr = QgsMapLayerRegistry.instance().mapLayersByName('points')[0] # set your point layer name here
l_lyr = QgsMapLayerRegistry.instance().mapLayersByName('lines')[0] # set your line layer name here
lines = [feature for feature in l_lyr.getFeatures()]
# set up memory layer for the closest distance line
d_lyr = QgsVectorLayer('LineString', 'normalVector', 'memory')
QgsMapLayerRegistry.instance().addMapLayer(d_lyr)
prov = d_lyr.dataProvider()
# adding three attributes (holding point_id, line_id and the distance)
prov.addAttributes( [ QgsField("point_id", QVariant.Int), QgsField("line_id", QVariant.Int), QgsField("distance",QVariant.Int)])
feat =
for points in p_lyr.getFeatures():
# find closest point to line
minDistPoint = min([l.geometry().closestSegmentWithContext(QgsPoint(points.geometry().asPoint())) for l in lines])[1]
feat = QgsFeature()
# create line from point to minDistPoint
line = QgsGeometry.fromPolyline([QgsPoint(points.geometry().asPoint()), QgsPoint(minDistPoint[0], minDistPoint[1])])
feat.setGeometry(line)
# adding point id, line id and length of the closest distance line as a feature to the memory layer
feat.setAttributes([int(points["id"]), int(l["id"]), line.geometry().length()])
prov.addFeatures([feat])
d_lyr.updateExtents()
d_lyr.triggerRepaint()
d_lyr.updateFields()
You can use the QGIS Python console for this. The output results as a memory layer. I'm using QGIS 2.18.28. Probably in QGIS 3 the code does not work, due to software changes.
Just paste this code into the Python console. You have to edit the code according to your layer names. In my case I have a shapefile points and a shapefile lines. You have to know the projection of your shapefiles and they have to have the same projection. Otherwise we have to do some transformation. Choose the right projection when adding the memory layer.
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
from qgis.networkanalysis import *
# define input layer points and lines
p_lyr = QgsMapLayerRegistry.instance().mapLayersByName('points')[0] # set your point layer name here
l_lyr = QgsMapLayerRegistry.instance().mapLayersByName('lines')[0] # set your line layer name here
lines = [feature for feature in l_lyr.getFeatures()]
# set up memory layer for the closest distance line
d_lyr = QgsVectorLayer('LineString', 'normalVector', 'memory')
QgsMapLayerRegistry.instance().addMapLayer(d_lyr)
prov = d_lyr.dataProvider()
# adding three attributes (holding point_id, line_id and the distance)
prov.addAttributes( [ QgsField("point_id", QVariant.Int), QgsField("line_id", QVariant.Int), QgsField("distance",QVariant.Int)])
feat =
for points in p_lyr.getFeatures():
# find closest point to line
minDistPoint = min([l.geometry().closestSegmentWithContext(QgsPoint(points.geometry().asPoint())) for l in lines])[1]
feat = QgsFeature()
# create line from point to minDistPoint
line = QgsGeometry.fromPolyline([QgsPoint(points.geometry().asPoint()), QgsPoint(minDistPoint[0], minDistPoint[1])])
feat.setGeometry(line)
# adding point id, line id and length of the closest distance line as a feature to the memory layer
feat.setAttributes([int(points["id"]), int(l["id"]), line.geometry().length()])
prov.addFeatures([feat])
d_lyr.updateExtents()
d_lyr.triggerRepaint()
d_lyr.updateFields()
edited 14 hours ago
answered 15 hours ago
StefanStefan
2,67912041
2,67912041
This won't work at my Qgis.. Maybe I'm doing something wrong..? Can you pm me, so I can share my shapefiles with you?
– Mathijs Alkema
13 hours ago
Please clarify: QGIS version (2 or 3), any errors (Python console or other). Do you know how to use the Python console? See the docs docs.qgis.org/2.18/en/docs/user_manual/plugins/….
– Stefan
11 hours ago
my shapefiles have an attribute called "id". For the resulting closest distance lines they will be adopted. See the penultimate line in the for loop:feat.setAttributes([int(points["id"]), int(l["id"]), line.geometry().length()])
. You have to replace the "id" regarding the attributes of your shapefiles. I can edit the code when only the distance is needed.
– Stefan
11 hours ago
add a comment |
This won't work at my Qgis.. Maybe I'm doing something wrong..? Can you pm me, so I can share my shapefiles with you?
– Mathijs Alkema
13 hours ago
Please clarify: QGIS version (2 or 3), any errors (Python console or other). Do you know how to use the Python console? See the docs docs.qgis.org/2.18/en/docs/user_manual/plugins/….
– Stefan
11 hours ago
my shapefiles have an attribute called "id". For the resulting closest distance lines they will be adopted. See the penultimate line in the for loop:feat.setAttributes([int(points["id"]), int(l["id"]), line.geometry().length()])
. You have to replace the "id" regarding the attributes of your shapefiles. I can edit the code when only the distance is needed.
– Stefan
11 hours ago
This won't work at my Qgis.. Maybe I'm doing something wrong..? Can you pm me, so I can share my shapefiles with you?
– Mathijs Alkema
13 hours ago
This won't work at my Qgis.. Maybe I'm doing something wrong..? Can you pm me, so I can share my shapefiles with you?
– Mathijs Alkema
13 hours ago
Please clarify: QGIS version (2 or 3), any errors (Python console or other). Do you know how to use the Python console? See the docs docs.qgis.org/2.18/en/docs/user_manual/plugins/….
– Stefan
11 hours ago
Please clarify: QGIS version (2 or 3), any errors (Python console or other). Do you know how to use the Python console? See the docs docs.qgis.org/2.18/en/docs/user_manual/plugins/….
– Stefan
11 hours ago
my shapefiles have an attribute called "id". For the resulting closest distance lines they will be adopted. See the penultimate line in the for loop:
feat.setAttributes([int(points["id"]), int(l["id"]), line.geometry().length()])
. You have to replace the "id" regarding the attributes of your shapefiles. I can edit the code when only the distance is needed.– Stefan
11 hours ago
my shapefiles have an attribute called "id". For the resulting closest distance lines they will be adopted. See the penultimate line in the for loop:
feat.setAttributes([int(points["id"]), int(l["id"]), line.geometry().length()])
. You have to replace the "id" regarding the attributes of your shapefiles. I can edit the code when only the distance is needed.– Stefan
11 hours ago
add a comment |
There is not such a plugin. And if you do not want to use python, you can combine two build-in algorithms in QGIS3
I think that this answer can solve your problem: https://gis.stackexchange.com/a/280787/7849
add a comment |
There is not such a plugin. And if you do not want to use python, you can combine two build-in algorithms in QGIS3
I think that this answer can solve your problem: https://gis.stackexchange.com/a/280787/7849
add a comment |
There is not such a plugin. And if you do not want to use python, you can combine two build-in algorithms in QGIS3
I think that this answer can solve your problem: https://gis.stackexchange.com/a/280787/7849
There is not such a plugin. And if you do not want to use python, you can combine two build-in algorithms in QGIS3
I think that this answer can solve your problem: https://gis.stackexchange.com/a/280787/7849
edited 14 hours ago
answered 14 hours ago
PieterBPieterB
2,5061126
2,5061126
add a comment |
add a comment |
Mathijs Alkema is a new contributor. Be nice, and check out our Code of Conduct.
Mathijs Alkema is a new contributor. Be nice, and check out our Code of Conduct.
Mathijs Alkema is a new contributor. Be nice, and check out our Code of Conduct.
Mathijs Alkema is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Geographic Information Systems Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f315171%2fcreating-closest-distance-line-between-points-and-lines-in-qgis%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Which software is that? QGIS or ArcGIS?
– Taras
15 hours ago
Interesting would be the feature count of both of your layers. It looks like your using OpenStreetMap data. According to big data sets you have to think of spatial indexes.
– Stefan
14 hours ago