# Calculate distance from a point to a MultiLineString geometry - PYQGIS

I'm trying to calculate the distance from a point to the closest river in my QGIS plugin. But I receive this message: `MultiLineString geometry cannot be converted to a point. Only Point types are permitted.` I can understand the cause of this message, but I don't know how to calculate the distance from a point and a MultiLineString geometry.

This is my code:

``````def getPointFromFeature(self, featureID, layer):
expr = QgsExpression("Id = '"+featureID+"'")
selection = self.selectionByExpr(layer,expr)
geom = selection[0].geometry().asPoint()
return geom

def getDistances(self, geom1, geom2):
d = QgsDistanceArea()
d.setEllipsoid('WGS84')
measure=d.measureLine(geom1, geom2)/1000
measure=round(measure,1)
return str(measure)

def getRiversDistances(self, featureID):
layerFiumi = self.getLayerFiumi()
layerBeni = self.getLayerBeni()
geomBene = self.getPointFromFeature(featureID, layerBeni)
expr = QgsExpression("type = 'river'")
selection = self.selectionByExpr(layerFiumi, expr)
d = {}
for river in selection:
d[river[1]] = self.getDistances(geomBene, river.geometry().asPoint())
return str(d)
``````
• Just get distance to multilinestring geometry without trying to cast it into point. The function should find the closest point of the river automatically. Commented Jan 16, 2020 at 18:43
• Hi thanks for your answer. I did like you said, but when I call `d.measureLine(geom1, geom2)` it says: `argument 2 has unexpected type 'QgsGeometry'` Commented Jan 16, 2020 at 20:15
• `measureLine()` requires two points. So, I need to find the closest point of the river to my point (I don't know how) or I need to find something different from `measureLine()` Commented Jan 16, 2020 at 20:25
• `layerBeni` is a point layer and `layerFiumi` is a multiline layer, right? Commented Jan 16, 2020 at 21:12
• yes it's right Kadir! Commented Jan 16, 2020 at 21:13

I've changed names of variables to be more eligible/legible for other readers.

``````def getPointFromFeature(self, featureID, layer):
expr = QgsExpression("Id = '"+featureID+"'")
selection = self.selectionByExpr(layer, expr)
geom = selection[0].geometry() # removed asPoint()
return geom

def getDistances(self, point_geom, river_geom):
d = QgsDistanceArea()
d.setEllipsoid('WGS84')

# Added/changed next two code lines.
# Line A
nearest_point_on_river = river_geom.nearestPoint(point_geom)
# Line B
measure = d.measureLine(point_geom.asPoint(), nearest_point_on_river.asPoint()) / 1000

measure = round(measure, 1)
return str(measure)

def getRiversDistances(self, featureID):
river_layer = self.getLayerFiumi()
point_layer = self.getLayerBeni()
point_geom  = self.getPointFromFeature(featureID, point_layer)
expr = QgsExpression("type = 'river'")
selection = self.selectionByExpr(river_layer, expr)
d = {}
for river in selection:
d[river[1]] = self.getDistances(point_geom, river.geometry())  # removed asPoint()
return str(d)
``````

I think this script will solve two errors problem: `MultiLineString geometry cannot be converted to a point. Only Point types are permitted.` and `argument 2 has unexpected type 'QgsGeometry'`.

Note that `Line A` calculates the nearest point using plain geometry and `Line B` calculates the distance on ellipsoid. This is not a perfect solution, but I guess it is sufficient for your goal.

• Thanks again, this solution is very good for my aim. Commented Jan 17, 2020 at 15:55