Skip to content

Commit

Permalink
nearest point measure in identify tool
Browse files Browse the repository at this point in the history
  • Loading branch information
blazek committed Jun 22, 2017
1 parent 7cd517c commit 5c6e794
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
6 changes: 6 additions & 0 deletions python/core/geometry/qgsgeometryutils.sip
Expand Up @@ -37,6 +37,12 @@ class QgsGeometryUtils
:rtype: QgsPoint
%End

static double closestPointMeasure( const QgsAbstractGeometry &geom, const QgsPoint &pt );

This comment has been minimized.

Copy link
@3nids

3nids Jun 22, 2017

Member

I guess it would make a lot of sense to create its sibling for Z, no?

This comment has been minimized.

Copy link
@blazek

blazek Jun 22, 2017

Author Member

Yes. In such case, it would be probably better to return QgsPoint with Z / M set, according to input geometry.

This comment has been minimized.

Copy link
@3nids

3nids Jun 22, 2017

Member

Makes sense. Would you do it?

This comment has been minimized.

Copy link
@blazek

blazek Jun 22, 2017

Author Member

No.

This comment has been minimized.

Copy link
@m-kuhn

m-kuhn Jun 23, 2017

Member

Doesn't QgsGeometryUtils::closestVertex do this in a generic way (and this new method is redundant)?

This comment has been minimized.

Copy link
@blazek

blazek Jun 23, 2017

Author Member

QgsGeometryUtils::closestVertex returns nearest vertex, not nearest point on segment.

This comment has been minimized.

Copy link
@m-kuhn

m-kuhn Jun 23, 2017

Member

Ah right, I missed that. Nice!
Can you elaborate on this interpolation behavior in the docs?
Also a unit test and a plan of action concerning the z discussed above would be great.
Thanks a lot.

This comment has been minimized.

Copy link
@blazek

blazek Jun 23, 2017

Author Member

Would be great, but no free resources here.

%Docstring
Returns measure of nearest point on a geometry for a specified point or NaN if geometry does not have measures
:rtype: float
%End

static double distanceToVertex( const QgsAbstractGeometry &geom, QgsVertexId id );
%Docstring
Returns the distance along a geometry from its first vertex to the specified vertex.
Expand Down
29 changes: 29 additions & 0 deletions src/core/geometry/qgsgeometryutils.cpp
Expand Up @@ -93,6 +93,35 @@ QgsPoint QgsGeometryUtils::closestVertex( const QgsAbstractGeometry &geom, const
return minDistPoint;
}

double QgsGeometryUtils::closestPointMeasure( const QgsAbstractGeometry &geom, const QgsPoint &pt )
{
if ( QgsWkbTypes::hasM( geom.wkbType() ) )
{
QgsPoint closestPoint;
QgsVertexId vertexAfter;
bool leftOf;
geom.closestSegment( pt, closestPoint, vertexAfter, &leftOf, DEFAULT_SEGMENT_EPSILON );
if ( vertexAfter.isValid() )
{
QgsPoint pointAfter = geom.vertexAt( vertexAfter );
if ( vertexAfter.vertex > 0 )
{
QgsVertexId vertexBefore = vertexAfter;
vertexBefore.vertex--;
QgsPoint pointBefore = geom.vertexAt( vertexBefore );
double length = pointBefore.distance( pointAfter );
double distance = pointBefore.distance( closestPoint );
return pointBefore.m() + ( pointAfter.m() - pointBefore.m() ) * distance / length;
}
else
{
return pointAfter.m();
}
}
}
return std::numeric_limits<double>::quiet_NaN();
}

double QgsGeometryUtils::distanceToVertex( const QgsAbstractGeometry &geom, QgsVertexId id )
{
double currentDist = 0;
Expand Down
4 changes: 4 additions & 0 deletions src/core/geometry/qgsgeometryutils.h
Expand Up @@ -44,6 +44,10 @@ class CORE_EXPORT QgsGeometryUtils
*/
static QgsPoint closestVertex( const QgsAbstractGeometry &geom, const QgsPoint &pt, QgsVertexId &id SIP_OUT );

/** Returns measure of nearest point on a geometry for a specified point or NaN if geometry does not have measures
*/
static double closestPointMeasure( const QgsAbstractGeometry &geom, const QgsPoint &pt );

/** Returns the distance along a geometry from its first vertex to the specified vertex.
* \param geom geometry
* \param id vertex id to find distance to
Expand Down
13 changes: 13 additions & 0 deletions src/gui/qgsmaptoolidentify.cpp
Expand Up @@ -326,6 +326,18 @@ void QgsMapToolIdentify::closestVertexAttributes( const QgsAbstractGeometry &geo
}
}

void QgsMapToolIdentify::closestPointAttributes( const QgsAbstractGeometry &geometry, QgsMapLayer *layer, const QgsPointXY &layerPoint, QMap< QString, QString > &derivedAttributes )
{
Q_UNUSED( layer );
// measure
if ( QgsWkbTypes::hasM( geometry.wkbType() ) )
{
double measure = QgsGeometryUtils::closestPointMeasure( geometry, QgsPoint( layerPoint.x(), layerPoint.y() ) );
QString str = QLocale::system().toString( measure, 'g', 10 );
derivedAttributes.insert( QStringLiteral( "Closest point M" ), str );
}
}

QString QgsMapToolIdentify::formatCoordinate( const QgsPointXY &canvasPoint ) const
{
return QgsCoordinateUtils::formatCoordinateForProject( canvasPoint, mCanvas->mapSettings().destinationCrs(),
Expand Down Expand Up @@ -392,6 +404,7 @@ QMap< QString, QString > QgsMapToolIdentify::featureDerivedAttributes( QgsFeatur

//add details of closest vertex to identify point
closestVertexAttributes( *curve, vId, layer, derivedAttributes );
closestPointAttributes( *curve, layer, layerPoint, derivedAttributes );

// Add the start and end points in as derived attributes
QgsPointXY pnt = mCanvas->mapSettings().layerToMapCoordinates( layer, QgsPointXY( curve->startPoint().x(), curve->startPoint().y() ) );
Expand Down
4 changes: 4 additions & 0 deletions src/gui/qgsmaptoolidentify.h
Expand Up @@ -183,6 +183,10 @@ class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
*/
void closestVertexAttributes( const QgsAbstractGeometry &geometry, QgsVertexId vId, QgsMapLayer *layer, QMap< QString, QString > &derivedAttributes );

/** Adds details of the closest point to derived attributes
*/
void closestPointAttributes( const QgsAbstractGeometry &geometry, QgsMapLayer *layer, const QgsPointXY &layerPoint, QMap< QString, QString > &derivedAttributes );

QString formatCoordinate( const QgsPointXY &canvasPoint ) const;
QString formatXCoordinate( const QgsPointXY &canvasPoint ) const;
QString formatYCoordinate( const QgsPointXY &canvasPoint ) const;
Expand Down

0 comments on commit 5c6e794

Please sign in to comment.