46 #include <QMouseEvent> 56 , mLastMapUnitsPerPixel( -1.0 )
57 , mCoordinatePrecision( 6 )
89 return identify( x, y, mode, QList<QgsMapLayer *>(), layerType );
99 return identify( geometry, mode, QList<QgsMapLayer *>(), layerType );
104 QList<IdentifyResult> results;
106 mLastGeometry = geometry;
121 int x = canvasPt.x(), y = canvasPt.y();
123 QPoint globalPos =
mCanvas->mapToGlobal( QPoint( x + 5, y + 5 ) );
126 else if ( mode ==
ActiveLayer && layerList.isEmpty() )
132 emit
identifyMessage( tr(
"No active layer. To identify features, you must choose an active layer." ) );
136 QApplication::setOverrideCursor( Qt::WaitCursor );
138 identifyLayer( &results, layer, mLastGeometry, mLastExtent, mLastMapUnitsPerPixel, layerType );
142 QApplication::setOverrideCursor( Qt::WaitCursor );
145 if ( layerList.isEmpty() )
148 layerCount = layerList.count();
151 for (
int i = 0; i < layerCount; i++ )
155 if ( layerList.isEmpty() )
158 layer = layerList.value( i );
166 if (
identifyLayer( &results, layer, mLastGeometry, mLastExtent, mLastMapUnitsPerPixel, layerType ) )
177 QApplication::restoreOverrideCursor();
184 mOverrideCanvasSearchRadius = searchRadiusMapUnits;
189 mOverrideCanvasSearchRadius = -1;
211 return identifyRasterLayer( results, qobject_cast<QgsRasterLayer *>( layer ), geometry, viewExtent, mapUnitsPerPixel );
230 QMap< QString, QString > derivedAttributes;
231 derivedAttributes.insert( tr(
"(clicked coordinate X)" ), formatXCoordinate( point ) );
232 derivedAttributes.insert( tr(
"(clicked coordinate Y)" ), formatYCoordinate( point ) );
234 derivedAttributes.insert( tr(
"(clicked coordinate Z)" ), QString::number( point.
z(),
'f' ) );
235 return derivedAttributes;
245 QgsDebugMsg( QStringLiteral(
"Out of scale limits" ) );
249 QApplication::setOverrideCursor( Qt::WaitCursor );
251 QMap< QString, QString > commonDerivedAttributes;
254 bool isPointOrRectangle;
259 isPointOrRectangle =
true;
260 point = selectionGeom.
asPoint();
270 int featureCount = 0;
273 std::unique_ptr<QgsGeometryEngine> selectionGeomPrepared;
283 double sr = mOverrideCanvasSearchRadius < 0 ?
searchRadiusMU(
mCanvas ) : mOverrideCanvasSearchRadius;
290 if ( !isPointOrRectangle )
305 if ( !selectionGeomPrepared || selectionGeomPrepared->intersects( f.
geometry().
constGet() ) )
313 QgsDebugMsg( QStringLiteral(
"Caught CRS exception %1" ).arg( cse.
what() ) );
320 std::unique_ptr< QgsFeatureRenderer > renderer( layer->
renderer() ? layer->
renderer()->
clone() : nullptr );
324 renderer->startRender( context, layer->
fields() );
328 for (
const QgsFeature &feature : qgis::as_const( featureList ) )
330 QMap< QString, QString > derivedAttributes = commonDerivedAttributes;
335 if ( filter && !renderer->willRenderFeature( feature, context ) )
341 derivedAttributes.unite( featureDerivedAttributes( feature, layer,
toLayerCoordinates( layer, point ) ) );
343 derivedAttributes.insert( tr(
"Feature ID" ), fid < 0 ? tr(
"new feature" ) :
FID_TO_STRING( fid ) );
345 results->append(
IdentifyResult( qobject_cast<QgsMapLayer *>( layer ), feature, derivedAttributes ) );
350 renderer->stopRender( context );
353 QgsDebugMsg(
"Feature count on identify: " + QString::number( featureCount ) );
355 QApplication::restoreOverrideCursor();
356 return featureCount > 0;
361 QString str = QLocale().toString( vId.
vertex + 1 );
362 derivedAttributes.insert( tr(
"Closest vertex number" ), str );
367 derivedAttributes.insert( tr(
"Closest vertex X" ), formatXCoordinate( closestPointMapCoords ) );
368 derivedAttributes.insert( tr(
"Closest vertex Y" ), formatYCoordinate( closestPointMapCoords ) );
370 if ( closestPoint.
is3D() )
372 str = QLocale().
toString( closestPoint.
z(),
'g', 10 );
373 derivedAttributes.insert( tr(
"Closest vertex Z" ), str );
377 str = QLocale().toString( closestPoint.
m(),
'g', 10 );
378 derivedAttributes.insert( tr(
"Closest vertex M" ), str );
383 double radius, centerX, centerY;
389 geometry.
vertexAt( vIdAfter ), radius, centerX, centerY );
390 derivedAttributes.insert( QStringLiteral(
"Closest vertex radius" ), QLocale().toString( radius ) );
394 void QgsMapToolIdentify::closestPointAttributes(
const QgsAbstractGeometry &geometry,
const QgsPointXY &layerPoint, QMap<QString, QString> &derivedAttributes )
398 derivedAttributes.insert( tr(
"Closest X" ), formatXCoordinate( closestPoint ) );
399 derivedAttributes.insert( tr(
"Closest Y" ), formatYCoordinate( closestPoint ) );
401 if ( closestPoint.
is3D() )
403 const QString str = QLocale().toString( closestPoint.
z(),
'g', 10 );
404 derivedAttributes.insert( tr(
"Interpolated Z" ), str );
408 const QString str = QLocale().toString( closestPoint.
m(),
'g', 10 );
409 derivedAttributes.insert( tr(
"Interpolated M" ), str );
413 QString QgsMapToolIdentify::formatCoordinate(
const QgsPointXY &canvasPoint )
const 416 mCoordinatePrecision );
419 QString QgsMapToolIdentify::formatXCoordinate(
const QgsPointXY &canvasPoint )
const 421 QString coordinate = formatCoordinate( canvasPoint );
422 return coordinate.split(
',' ).at( 0 );
425 QString QgsMapToolIdentify::formatYCoordinate(
const QgsPointXY &canvasPoint )
const 427 QString coordinate = formatCoordinate( canvasPoint );
428 return coordinate.split(
',' ).at( 1 );
435 QMap< QString, QString > derivedAttributes;
458 QString str = QLocale().toString( static_cast<const QgsGeometryCollection *>( feature.
geometry().
constGet() )->numGeometries() );
459 derivedAttributes.insert( tr(
"Parts" ), str );
460 str = QLocale().toString( vId.
part + 1 );
461 derivedAttributes.insert( tr(
"Part number" ), str );
465 ? displayDistanceUnits() : layer->
crs().
mapUnits();
476 str = formatDistance( dist );
477 derivedAttributes.insert( tr(
"Length (Ellipsoidal, %1)" ).arg( ellipsoid ), str );
481 derivedAttributes.insert( tr(
"Length (Cartesian)" ), str );
487 derivedAttributes.insert( tr(
"Vertices" ), str );
489 closestVertexAttributes( *geom, vId, layer, derivedAttributes );
490 closestPointAttributes( *geom, layerPoint, derivedAttributes );
492 if (
const QgsCurve *curve = qgsgeometry_cast< const QgsCurve * >( geom ) )
496 str = formatXCoordinate( pnt );
497 derivedAttributes.insert( tr(
"firstX",
"attributes get sorted; translation for lastX should be lexically larger than this one" ), str );
498 str = formatYCoordinate( pnt );
499 derivedAttributes.insert( tr(
"firstY" ), str );
501 str = formatXCoordinate( pnt );
502 derivedAttributes.insert( tr(
"lastX",
"attributes get sorted; translation for firstX should be lexically smaller than this one" ), str );
503 str = formatYCoordinate( pnt );
504 derivedAttributes.insert( tr(
"lastY" ), str );
515 str = formatArea( area );
516 derivedAttributes.insert( tr(
"Area (Ellipsoidal, %1)" ).arg( ellipsoid ), str );
520 derivedAttributes.insert( tr(
"Area (Cartesian)" ), str );
526 str = formatDistance( perimeter );
527 derivedAttributes.insert( tr(
"Perimeter (Ellipsoidal, %1)" ).arg( ellipsoid ), str );
531 derivedAttributes.insert( tr(
"Perimeter (Cartesian)" ), str );
534 derivedAttributes.insert( tr(
"Vertices" ), str );
537 closestVertexAttributes( *feature.
geometry().
constGet(), vId, layer, derivedAttributes );
538 closestPointAttributes( *feature.
geometry().
constGet(), layerPoint, derivedAttributes );
546 QString str = formatXCoordinate( pnt );
547 derivedAttributes.insert( tr(
"X" ), str );
548 str = formatYCoordinate( pnt );
549 derivedAttributes.insert( tr(
"Y" ), str );
553 str = QLocale().toString( static_cast<const QgsPoint *>( feature.
geometry().
constGet() )->z(),
'g', 10 );
554 derivedAttributes.insert( tr(
"Z" ), str );
558 str = QLocale().toString( static_cast<const QgsPoint *>( feature.
geometry().
constGet() )->m(),
'g', 10 );
559 derivedAttributes.insert( tr(
"M" ), str );
569 closestVertexAttributes( *geom, vId, layer, derivedAttributes );
574 return derivedAttributes;
605 QgsDebugMsg( QStringLiteral(
"coordinate not reprojectable: %1" ).arg( cse.
what() ) );
608 QgsDebugMsg( QStringLiteral(
"point = %1 %2" ).arg( point.
x() ).arg( point.
y() ) );
613 QMap< QString, QString > attributes, derivedAttributes;
638 r.
setXMinimum( pointInCanvasCrs.
x() - mapUnitsPerPixel / 2. );
639 r.
setXMaximum( pointInCanvasCrs.
x() + mapUnitsPerPixel / 2. );
640 r.
setYMinimum( pointInCanvasCrs.
y() - mapUnitsPerPixel / 2. );
641 r.
setYMaximum( pointInCanvasCrs.
y() + mapUnitsPerPixel / 2. );
645 identifyResult = dprovider->
identify( point, format, r, 1, 1 );
661 int width =
static_cast< int >( std::round( viewExtent.
width() / mapUnitsPerPixel ) );
662 int height =
static_cast< int >( std::round( viewExtent.
height() / mapUnitsPerPixel ) );
664 QgsDebugMsg( QStringLiteral(
"viewExtent.width = %1 viewExtent.height = %2" ).arg( viewExtent.
width() ).arg( viewExtent.
height() ) );
665 QgsDebugMsg( QStringLiteral(
"width = %1 height = %2" ).arg( width ).arg( height ) );
666 QgsDebugMsg( QStringLiteral(
"xRes = %1 yRes = %2 mapUnitsPerPixel = %3" ).arg( viewExtent.
width() / width ).arg( viewExtent.
height() / height ).arg( mapUnitsPerPixel ) );
668 identifyResult = dprovider->
identify( point, format, viewExtent, width, height );
673 if ( identifyResult.
isValid() )
675 QMap<int, QVariant> values = identifyResult.
results();
679 for (
auto it = values.constBegin(); it != values.constEnd(); ++it )
682 if ( it.value().isNull() )
684 valueString = tr(
"no data" );
688 QVariant value( it.value() );
692 if ( static_cast<QMetaType::Type>( value.type() ) == QMetaType::Float )
703 QString label = layer->
name();
704 results->append(
IdentifyResult( qobject_cast<QgsMapLayer *>( layer ), label, attributes, derivedAttributes ) );
708 for (
auto it = values.constBegin(); it != values.constEnd(); ++it )
710 QVariant value = it.value();
711 if ( value.type() == QVariant::Bool && !value.toBool() )
717 if ( value.type() == QVariant::String )
721 QString label = layer->
subLayers().value( it.key() );
723 attributes.insert( tr(
"Error" ), value.toString() );
725 results->append(
IdentifyResult( qobject_cast<QgsMapLayer *>( layer ), label, attributes, derivedAttributes ) );
735 for (
const QgsFeature &feature : storeFeatures )
741 QString sublayer = featureStore.params().value( QStringLiteral(
"sublayer" ) ).toString();
742 QString featureType = featureStore.params().value( QStringLiteral(
"featureType" ) ).toString();
744 featureType.remove( QStringLiteral(
"_feature" ) );
746 if ( sublayer.compare( layer->
name(), Qt::CaseInsensitive ) != 0 )
750 if ( featureType.compare( sublayer, Qt::CaseInsensitive ) != 0 || labels.isEmpty() )
752 labels << featureType;
755 QMap< QString, QString > derAttributes = derivedAttributes;
756 derAttributes.unite( featureDerivedAttributes( feature, layer ) );
758 IdentifyResult identifyResult( qobject_cast<QgsMapLayer *>( layer ), labels.join( QStringLiteral(
" / " ) ), featureStore.fields(), feature, derAttributes );
760 identifyResult.mParams.insert( QStringLiteral(
"getFeatureInfoUrl" ), featureStore.params().value( QStringLiteral(
"getFeatureInfoUrl" ) ) );
761 results->append( identifyResult );
768 QgsDebugMsg( QStringLiteral(
"%1 HTML or text values" ).arg( values.size() ) );
769 for (
auto it = values.constBegin(); it != values.constEnd(); ++it )
771 QString value = it.value().toString();
773 attributes.insert( QString(), value );
775 QString label = layer->
subLayers().value( it.key() );
776 results->append(
IdentifyResult( qobject_cast<QgsMapLayer *>( layer ), label, attributes, derivedAttributes ) );
784 attributes.insert( tr(
"Error" ), value );
785 QString label = tr(
"Identify error" );
786 results->append(
IdentifyResult( qobject_cast<QgsMapLayer *>( layer ), label, attributes, derivedAttributes ) );
802 QString QgsMapToolIdentify::formatDistance(
double distance )
const 804 return formatDistance( distance, displayDistanceUnits() );
807 QString QgsMapToolIdentify::formatArea(
double area )
const 809 return formatArea( area, displayAreaUnits() );
815 bool baseUnit = settings.
value( QStringLiteral(
"qgis/measure/keepbaseunit" ),
true ).toBool();
823 bool baseUnit = settings.
value( QStringLiteral(
"qgis/measure/keepbaseunit" ),
true ).toBool();
830 QList<IdentifyResult> results;
831 if (
identifyRasterLayer( &results, layer, mLastGeometry, mLastExtent, mLastMapUnitsPerPixel ) )
double measureLength(const QgsGeometry &geometry) const
Measures the length of a geometry.
bool isValid() const
Returns true if valid.
Wrapper for iterator of features from vector data provider or vector layer.
A container for features with the same fields and crs.
A rectangle specified with double values.
Base class for all map layer types.
QStringList subLayers() const override
Returns the sublayers of this layer.
QgsMapLayer::LayerType type() const
Returns the type of the layer.
static QString printValue(double value)
Print double value with all necessary significant digits.
double scale() const
Returns the calculated map scale.
double mapUnitsPerPixel() const
Returns the mapUnitsPerPixel (map units per pixel) for the canvas.
int layerCount() const
Returns number of layers on the map.
OperationResult transform(const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection direction=QgsCoordinateTransform::ForwardTransform, bool transformZ=false) SIP_THROW(QgsCsException)
Transforms this geometry as described by the coordinate transform ct.
void setXMaximum(double x)
Set the maximum x value.
Use exact geometry intersection (slower) instead of bounding boxes.
This class is a composition of two QSettings instances:
QgsPointXY layerToMapCoordinates(const QgsMapLayer *layer, QgsPointXY point) const
transform point coordinates from layer's CRS to output CRS
static bool isMultiType(Type type)
Returns true if the WKB type is a multi type.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
Features may be filtered, i.e. some features may not be rendered (categorized, rule based ...
QList< QgsFeatureStore > QgsFeatureStoreList
QList< QgsFeature > QgsFeatureList
A class to represent a 2D point.
QString message(QgsErrorMessage::Format format=QgsErrorMessage::Html) const
Full error messages description.
bool contains(const QgsRectangle &rect) const
Returns true when rectangle contains other rectangle.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
double convertLengthMeasurement(double length, QgsUnitTypes::DistanceUnit toUnits) const
Takes a length measurement calculated by this QgsDistanceArea object and converts it to a different d...
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
static Capability identifyFormatToCapability(QgsRaster::IdentifyFormat format)
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
A geometry is the spatial representation of a feature.
QgsUnitTypes::DistanceUnit mapUnits
bool setEllipsoid(const QString &ellipsoid)
Sets the ellipsoid by its acronym.
QgsPointXY asPoint() const
Returns the contents of the geometry as a 2-dimensional point.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
static QgsPoint closestVertex(const QgsAbstractGeometry &geom, const QgsPoint &pt, QgsVertexId &id)
Returns the closest vertex to a geometry for a specified point.
QString toString(int precision=-1) const
Returns a string representation of the point (x, y) with a preset precision.
bool isSpatial() const FINAL
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
static bool hasZ(Type type)
Tests whether a WKB type contains the z-dimension.
Map canvas is a class for displaying all GIS data types on a canvas.
QMap< int, QVariant > results() const
Returns the identify results.
const QString GEO_NONE
Constant that holds the string representation for "No ellips/No CRS".
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
QgsCoordinateReferenceSystem destinationCrs() const
returns CRS of destination coordinate reference system
Raster identify results container.
static QgsGeometry fromRect(const QgsRectangle &rect)
Creates a new geometry from a QgsRectangle.
QgsRasterDataProvider * dataProvider() override
Returns the layer's data provider.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
Type
The WKB type describes the number of dimensions a geometry has.
bool isMeasure() const
Returns true if the geometry contains m values.
virtual int nCoordinates() const
Returns the number of nodes contained in the geometry.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
bool isInScaleRange(double scale) const
Tests whether the layer should be visible at the specified scale.
Utility class for identifying a unique vertex within a geometry.
virtual QgsRectangle extent() const
Returns the extent of the layer.
static QgsRaster::IdentifyFormat identifyFormatFromName(const QString &formatName)
double convertAreaMeasurement(double area, QgsUnitTypes::AreaUnit toUnits) const
Takes an area measurement calculated by this QgsDistanceArea object and converts it to a different ar...
virtual QgsCoordinateReferenceSystem crs() const =0
Returns the coordinate system for the data source.
void setYMinimum(double y)
Set the minimum y value.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
double measurePerimeter(const QgsGeometry &geometry) const
Measures the perimeter of a polygon geometry.
QgsFeatureRenderer * renderer()
Returns renderer.
Abstract base class for curved geometry type.
QVariant customProperty(const QString &value, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
Abstract base class for all geometries.
Point geometry type, with support for z-dimension and m-values.
virtual int capabilities() const
Returns a bitmask containing the supported capabilities.
static QgsGeometryEngine * createGeometryEngine(const QgsAbstractGeometry *geometry)
Creates and returns a new geometry engine.
QgsMapLayer * currentLayer()
returns current layer (set by legend widget)
virtual double length() const
Returns the length of the geometry.
QgsExpressionContext & expressionContext()
Gets the expression context.
DistanceUnit
Units of distance.
static Q_INVOKABLE DistanceUnitType unitType(QgsUnitTypes::DistanceUnit unit)
Returns the type for a distance unit.
virtual QString generateBandName(int bandNumber) const
helper function to create zero padded band names
QgsCoordinateTransformContext transformContext
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
A general purpose distance and area calculator, capable of performing ellipsoid based calculations...
Contains information about the context of a rendering operation.
#define FID_TO_STRING(fid)
QgsMapLayer::LayerFlags flags() const
Returns the flags for this layer.
virtual double perimeter() const
Returns the perimeter of the geometry.
void setYMaximum(double y)
Set the maximum y value.
static QgsRenderContext fromMapSettings(const QgsMapSettings &mapSettings)
create initialized QgsRenderContext instance from given QgsMapSettings
static QgsProject * instance()
Returns the QgsProject singleton instance.
bool hasGeometry() const
Returns true if the feature has an associated geometry.
virtual QgsRasterIdentifyResult identify(const QgsPointXY &point, QgsRaster::IdentifyFormat format, const QgsRectangle &boundingBox=QgsRectangle(), int width=0, int height=0, int dpi=96)
Identify raster value(s) found on the point position.
static void circleCenterRadius(const QgsPoint &pt1, const QgsPoint &pt2, const QgsPoint &pt3, double &radius, double ¢erX, double ¢erY)
Returns radius and center of the circle through pt1, pt2, pt3.
static QString formatArea(double area, int decimals, QgsUnitTypes::AreaUnit unit, bool keepBaseUnit=false)
Returns an area formatted as a friendly string.
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
T enumValue(const QString &key, const T &defaultValue, const Section section=NoSection)
Returns the setting value for a setting based on an enum.
QgsUnitTypes::DistanceUnit mapUnits() const
Convenience function for returning the current canvas map units.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Query the layer for features specified in request.
static bool hasM(Type type)
Tests whether a WKB type contains m values.
static QCursor getThemeCursor(Cursor cursor)
Helper to get a theme cursor.
void setSourceCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets source spatial reference system crs.
QgsRectangle extent() const
Returns the current zoom extent of the map canvas.
Custom exception class for Coordinate Reference System related exceptions.
static QgsPoint closestPoint(const QgsAbstractGeometry &geometry, const QgsPoint &point)
Returns the nearest point on a segment of a geometry for the specified point.
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
bool nextFeature(QgsFeature &f)
static Q_INVOKABLE double fromUnitToUnitFactor(QgsUnitTypes::DistanceUnit fromUnit, QgsUnitTypes::DistanceUnit toUnit)
Returns the conversion factor between the specified distance units.
double width() const
Returns the width of the rectangle.
virtual QgsPoint vertexAt(QgsVertexId id) const =0
Returns the point corresponding to a specified vertex id.
QgsError error() const
Returns the last error.
Represents a vector layer which manages a vector based data sets.
static Type flatType(Type type)
Returns the flat type for a WKB type.
QgsWkbTypes::GeometryType type() const
Returns type of the geometry as a QgsWkbTypes::GeometryType.
QgsWkbTypes::Type wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
static Q_INVOKABLE QgsUnitTypes::AreaUnit distanceToAreaUnit(QgsUnitTypes::DistanceUnit distanceUnit)
Converts a distance unit to its corresponding area unit, e.g., meters to square meters.
double measureArea(const QgsGeometry &geometry) const
Measures the area of a geometry.
If the layer is identifiable using the identify map tool and as a WMS layer.
QgsMapLayer * layer(int index)
Returns the map layer at position index in the layer stack.
virtual QgsFeatureRenderer * clone() const =0
Create a deep copy of this renderer.
double area() const
Returns the area of the geometry using GEOS.
static QgsGeometry fromPointXY(const QgsPointXY &point)
Creates a new geometry from a QgsPointXY object.
void setXMinimum(double x)
Set the minimum x value.
QgsCoordinateReferenceSystem crs
double height() const
Returns the height of the rectangle.
Base class for raster data providers.
static QString formatDistance(double distance, int decimals, QgsUnitTypes::DistanceUnit unit, bool keepBaseUnit=false)
Returns an distance formatted as a friendly string.