I have two layers, one point and one line. I need the distance to the closest line from the point layer. Not NNJoin, MMQGIS nor the distance hub in QGIS provided accurate measures. Any ideas?
If you have to measure the distances once again, the following Python code may save you some time.
Have your points and lines loaded as map layers. Change the names 'point' and 'line' in line 4 and 5 resp. to the names of your layers. Copy the code into the Python console.
The function iterates over all points and finds the closest segment of all lines, and writes the closest position along the segment to a list. Then takes the position with the minimum distance to a points and builds a connecting line. The length of the line goes to the field distance.
d_lyr = QgsVectorLayer('LineString?field=distance:float', 'dist', 'memory') QgsMapLayerRegistry.instance().addMapLayer(d_lyr) p_lyr = QgsMapLayerRegistry.instance().mapLayersByName('point') l_lyr = QgsMapLayerRegistry.instance().mapLayersByName('line') d_lyr = QgsMapLayerRegistry.instance().mapLayersByName('dist') prov = d_lyr.dataProvider() feats =  for p in p_lyr.getFeatures(): minDistPoint = min([l.geometry().closestSegmentWithContext(QgsPoint(p.geometry().asPoint())) for l in l_lyr.getFeatures()]) feat = QgsFeature() feat.setGeometry(QgsGeometry.fromPolyline([QgsPoint(p.geometry().asPoint()), QgsPoint(minDistPoint, minDistPoint)])) feat.setAttributes([feat.geometry().length()]) feats.append(feat) prov.addFeatures(feats) d_lyr.updateExtents() d_lyr.triggerRepaint()
To test the code initiate 2 memory layers, a point layer named 'point' and a line layer named 'line', digitize some features, and run the code.
The NNJoin plugin uses QgsGeometry.distance, which in turn uses GEOS. It should return the minimum distance:
Returns the minimum distanace between this geometry and another geometry, using GEOS.
So I am a bit surprised that NNJoin does not work as expected (as long as you do not choose to use approximate geometries).
Are you using a non-projected coordinate system? Geos does not support that:
GEOS distance calculations are linear – in other words, GEOS does not perform a spherical calculation even if the SRID specifies a geographic coordinate system.
If you are not using a non-projected coordinate system, it would be interesting to see example data sets. You could also open an issue on the NNJoin issue tracker.