void QgsMeasureDialog::mouseMove( QgsPoint &point ) { QSettings settings; int decimalPlaces = settings.value( "/qgis/measure/decimalplaces", "3" ).toInt(); // Create QgsDistance Area for customization ProjectionEnabled setting QgsDistanceArea myDa; configureDistanceArea( myDa ); // show current distance/area while moving the point // by creating a temporary copy of point array // and adding moving point at the end if ( mMeasureArea && mTool->points().size() > 1 ) { QList<QgsPoint> tmpPoints = mTool->points(); tmpPoints.append( point ); double area = myDa.measurePolygon( tmpPoints ); editTotal->setText( formatArea( area, decimalPlaces ) ); } else if ( !mMeasureArea && mTool->points().size() > 0 ) { QgsPoint p1( mTool->points().last() ), p2( point ); double d = myDa.measureLine( p1, p2 ); editTotal->setText( formatDistance( mTotal + d, decimalPlaces ) ); QGis::UnitType myDisplayUnits; // Ignore units convertMeasurement( d, myDisplayUnits, false ); QTreeWidgetItem *item = mTable->topLevelItem( mTable->topLevelItemCount() - 1 ); item->setText( 0, QLocale::system().toString( d, 'f', decimalPlaces ) ); } }
void QgsMeasureDialog::mouseMove( QgsPoint &point ) { // show current distance/area while moving the point // by creating a temporary copy of point array // and adding moving point at the end if ( mMeasureArea && mTool->points().size() >= 2 ) { QList<QgsPoint> tmpPoints = mTool->points(); tmpPoints.append( point ); double area = mDa.measurePolygon( tmpPoints ); editTotal->setText( formatArea( area ) ); } else if ( !mMeasureArea && mTool->points().size() >= 1 ) { QgsPoint p1( mTool->points().last() ), p2( point ); double d = mDa.measureLine( p1, p2 ); editTotal->setText( formatDistance( mTotal + d ) ); QGis::UnitType displayUnits; // Meters or feet? convertMeasurement( d, displayUnits, false ); // Set moving QTreeWidgetItem *item = mTable->topLevelItem( mTable->topLevelItemCount() - 1 ); item->setText( 0, QLocale::system().toString( d, 'f', mDecimalPlaces ) ); QgsDebugMsg( QString( "Final result is %1" ).arg( item->text( 0 ) ) ); } }
QString QgsMeasureDialog::formatValue( double value, bool measureArea ) { bool baseUnit = QSettings().value( "/Qgis/measure/keepbaseunit", false ).toBool(); QGis::UnitType newDisplayUnits; convertMeasurement( value, newDisplayUnits, measureArea ); return QgsDistanceArea::textUnit( value, mDecimalPlaces, newDisplayUnits, measureArea, baseUnit ); }
QString QgsMeasureDialog::formatArea( double area ) { QSettings settings; bool baseUnit = settings.value( "/qgis/measure/keepbaseunit", false ).toBool(); QGis::UnitType newDisplayUnits; convertMeasurement( area, newDisplayUnits, true ); return QgsDistanceArea::textUnit( area, mDecimalPlaces, newDisplayUnits, true, baseUnit ); }
QString QgsMeasureDialog::formatDistance( double distance, int decimalPlaces ) { QSettings settings; bool baseUnit = settings.value( "/qgis/measure/keepbaseunit", false ).toBool(); QGis::UnitType myDisplayUnits; convertMeasurement( distance, myDisplayUnits, false ); return QgsDistanceArea::textUnit( distance, decimalPlaces, myDisplayUnits, false, baseUnit ); }
QString CMeasureDialog::formatDistance( double distance ) { QSettings settings; bool baseUnit = settings.value( "/qgis/measure/keepbaseunit", false ).toBool(); QGis::UnitType newDisplayUnits; convertMeasurement( distance, newDisplayUnits, false ); return FormatDistance( distance, mDecimalPlaces, newDisplayUnits, baseUnit ); }
void QgsMeasureDialog::updateUi() { // Set tooltip to indicate how we calculate measurments QString toolTip = tr( "The calculations are based on:" ); if ( ! mTool->canvas()->hasCrsTransformEnabled() ) { toolTip += "<br> * " + tr( "Project CRS transformation is turned off." ) + " "; toolTip += tr( "Canvas units setting is taken from project properties setting (%1)." ).arg( QGis::tr( mCanvasUnits ) ); toolTip += "<br> * " + tr( "Ellipsoidal calculation is not possible, as project CRS is undefined." ); setWindowTitle( tr( "Measure (OTF off)" ) ); } else { if ( mDa.ellipsoidalEnabled() ) { toolTip += "<br> * " + tr( "Project CRS transformation is turned on and ellipsoidal calculation is selected." ) + " "; toolTip += "<br> * " + tr( "The coordinates are transformed to the chosen ellipsoid (%1), and the result is in meters" ).arg( mDa.ellipsoid() ); } else { toolTip += "<br> * " + tr( "Project CRS transformation is turned on but ellipsoidal calculation is not selected." ); toolTip += "<br> * " + tr( "The canvas units setting is taken from the project CRS (%1)." ).arg( QGis::tr( mCanvasUnits ) ); } setWindowTitle( tr( "Measure (OTF on)" ) ); } if (( mCanvasUnits == QGis::Meters && mDisplayUnits == QGis::Feet ) || ( mCanvasUnits == QGis::Feet && mDisplayUnits == QGis::Meters ) ) { toolTip += "<br> * " + tr( "Finally, the value is converted from %1 to %2." ).arg( QGis::tr( mCanvasUnits ) ).arg( QGis::tr( mDisplayUnits ) ); } editTotal->setToolTip( toolTip ); mTable->setToolTip( toolTip ); mNotesLabel->setText( toolTip ); QGis::UnitType newDisplayUnits; double dummy = 1.0; convertMeasurement( dummy, newDisplayUnits, true ); mTable->setHeaderLabels( QStringList() << tr( "Parts" ) ); mTable->clear(); const QList< QList< QgsPoint > >& points = mTool->getPoints(); double total = 0.; for ( int i = 0, n = points.size(); i < n; ++i ) { double value = measureGeometry( points[i], mMeasureArea ); QTreeWidgetItem* item = new QTreeWidgetItem( QStringList() << formatValue( value, mMeasureArea ) ); item->setTextAlignment( 0, Qt::AlignRight ); mTable->addTopLevelItem( item ); total += value; } editTotal->setText( formatValue( total, mMeasureArea ) ); mTool->updateLabels(); }
QString CMeasureDialog::formatArea( double area ) { QSettings settings; bool baseUnit = settings.value( "/qgis/measure/keepbaseunit", false ).toBool(); #if (VERSION_INT < 21601) QGis::UnitType newDisplayUnits; convertMeasurement( area, newDisplayUnits, true ); #else //TODO: review QgsUnitTypes::AreaUnit newDisplayUnits = QgsProject::instance()->areaUnits(); area = mDa.convertAreaMeasurement( area, newDisplayUnits ); //QGIS 2.16.1 double QgsMeasureDialog::convertArea( double area, QgsUnitTypes::AreaUnit toUnit ) const #endif return FormatArea( area, mDecimalPlaces, newDisplayUnits, baseUnit ); }
void QgsMeasureDialog::updateUi() { QSettings settings; int decimalPlaces = settings.value( "/qgis/measure/decimalplaces", "3" ).toInt(); double dummy = 1.0; QGis::UnitType myDisplayUnits; // The dummy distance is ignored convertMeasurement( dummy, myDisplayUnits, false ); switch ( myDisplayUnits ) { case QGis::Meters: mTable->setHeaderLabels( QStringList( tr( "Segments (in meters)" ) ) ); break; case QGis::Feet: mTable->setHeaderLabels( QStringList( tr( "Segments (in feet)" ) ) ); break; case QGis::DegreesMinutesSeconds: case QGis::DegreesDecimalMinutes: case QGis::Degrees: mTable->setHeaderLabels( QStringList( tr( "Segments (in degrees)" ) ) ); break; case QGis::UnknownUnit: mTable->setHeaderLabels( QStringList( tr( "Segments" ) ) ); }; if ( mMeasureArea ) { mTable->hide(); editTotal->setText( formatArea( 0, decimalPlaces ) ); } else { mTable->show(); editTotal->setText( formatDistance( 0, decimalPlaces ) ); } }
void QgsMeasureDialog::addPoint( QgsPoint &point ) { QSettings settings; int decimalPlaces = settings.value( "/qgis/measure/decimalplaces", "3" ).toInt(); // Create QgsDistance Area for customization ProjectionEnabled setting QgsDistanceArea myDa; configureDistanceArea( myDa ); int numPoints = mTool->points().size(); if ( mMeasureArea && numPoints > 2 ) { double area = myDa.measurePolygon( mTool->points() ); editTotal->setText( formatArea( area, decimalPlaces ) ); } else if ( !mMeasureArea && numPoints > 1 ) { int last = numPoints - 2; QgsPoint p1 = mTool->points()[last], p2 = mTool->points()[last+1]; double d = myDa.measureLine( p1, p2 ); mTotal += d; editTotal->setText( formatDistance( mTotal, decimalPlaces ) ); QGis::UnitType myDisplayUnits; // Ignore units convertMeasurement( d, myDisplayUnits, false ); QTreeWidgetItem *item = mTable->topLevelItem( mTable->topLevelItemCount() - 1 ); item->setText( 0, QLocale::system().toString( d, 'f', decimalPlaces ) ); item = new QTreeWidgetItem( QStringList( QLocale::system().toString( 0.0, 'f', decimalPlaces ) ) ); item->setTextAlignment( 0, Qt::AlignRight ); mTable->addTopLevelItem( item ); mTable->scrollToItem( item ); } }
void QgsMeasureDialog::removeLastPoint() { int numPoints = mTool->points().size(); if ( mMeasureArea ) { if ( numPoints > 1 ) { QList<QgsPoint> tmpPoints = mTool->points(); tmpPoints.append( mLastMousePoint ); double area = mDa.measurePolygon( tmpPoints ); editTotal->setText( formatArea( area ) ); } else { editTotal->setText( formatArea( 0 ) ); } } else if ( !mMeasureArea && numPoints >= 1 ) { //remove final row delete mTable->takeTopLevelItem( mTable->topLevelItemCount() - 1 ); QgsPoint p1( mTool->points().last() ); double d = mDa.measureLine( p1, mLastMousePoint ); mTotal = mDa.measureLine( mTool->points() ); editTotal->setText( formatDistance( mTotal + d ) ); QGis::UnitType displayUnits; // Meters or feet? convertMeasurement( d, displayUnits, false ); QTreeWidgetItem *item = mTable->topLevelItem( mTable->topLevelItemCount() - 1 ); item->setText( 0, QLocale::system().toString( d, 'f', mDecimalPlaces ) ); } }
QMap< QString, QString > QgsMapToolIdentify::featureDerivedAttributes( QgsFeature *feature, QgsMapLayer *layer, const QgsPoint& layerPoint ) { // Calculate derived attributes and insert: // measure distance or area depending on geometry type QMap< QString, QString > derivedAttributes; // init distance/area calculator QString ellipsoid = QgsProject::instance()->readEntry( "Measure", "/Ellipsoid", GEO_NONE ); QgsDistanceArea calc; calc.setEllipsoidalMode( mCanvas->hasCrsTransformEnabled() ); calc.setEllipsoid( ellipsoid ); calc.setSourceCrs( layer->crs().srsid() ); QgsWKBTypes::Type wkbType = QgsWKBTypes::NoGeometry; QGis::GeometryType geometryType = QGis::NoGeometry; QgsVertexId vId; QgsPointV2 closestPoint; if ( feature->constGeometry() ) { geometryType = feature->constGeometry()->type(); wkbType = feature->constGeometry()->geometry()->wkbType(); //find closest vertex to clicked point closestPoint = QgsGeometryUtils::closestVertex( *feature->constGeometry()->geometry(), QgsPointV2( layerPoint.x(), layerPoint.y() ), vId ); } if ( QgsWKBTypes::isMultiType( wkbType ) ) { QString str = QLocale::system().toString( static_cast<QgsGeometryCollectionV2*>( feature->constGeometry()->geometry() )->numGeometries() ); derivedAttributes.insert( tr( "Parts" ), str ); str = QLocale::system().toString( vId.part + 1 ); derivedAttributes.insert( tr( "Part number" ), str ); } if ( geometryType == QGis::Line ) { const QgsPolyline &pline = feature->constGeometry()->asPolyline(); double dist = calc.measureLength( feature->constGeometry() ); QGis::UnitType myDisplayUnits; convertMeasurement( calc, dist, myDisplayUnits, false ); QString str = calc.textUnit( dist, 3, myDisplayUnits, false ); // dist and myDisplayUnits are out params derivedAttributes.insert( tr( "Length" ), str ); str = QLocale::system().toString( pline.size() ); derivedAttributes.insert( tr( "Vertices" ), str ); //add details of closest vertex to identify point closestVertexAttributes( *feature->constGeometry()->geometry(), vId, layer, derivedAttributes ); if ( QgsWKBTypes::flatType( wkbType ) == QgsWKBTypes::LineString ) { // Add the start and end points in as derived attributes QgsPoint pnt = mCanvas->mapSettings().layerToMapCoordinates( layer, pline.first() ); str = QLocale::system().toString( pnt.x(), 'g', 10 ); derivedAttributes.insert( tr( "firstX", "attributes get sorted; translation for lastX should be lexically larger than this one" ), str ); str = QLocale::system().toString( pnt.y(), 'g', 10 ); derivedAttributes.insert( tr( "firstY" ), str ); pnt = mCanvas->mapSettings().layerToMapCoordinates( layer, pline.last() ); str = QLocale::system().toString( pnt.x(), 'g', 10 ); derivedAttributes.insert( tr( "lastX", "attributes get sorted; translation for firstX should be lexically smaller than this one" ), str ); str = QLocale::system().toString( pnt.y(), 'g', 10 ); derivedAttributes.insert( tr( "lastY" ), str ); } } else if ( geometryType == QGis::Polygon ) { double area = calc.measureArea( feature->constGeometry() ); double perimeter = calc.measurePerimeter( feature->constGeometry() ); QGis::UnitType myDisplayUnits; convertMeasurement( calc, area, myDisplayUnits, true ); // area and myDisplayUnits are out params QString str = calc.textUnit( area, 3, myDisplayUnits, true ); derivedAttributes.insert( tr( "Area" ), str ); convertMeasurement( calc, perimeter, myDisplayUnits, false ); // perimeter and myDisplayUnits are out params str = calc.textUnit( perimeter, 3, myDisplayUnits, false ); derivedAttributes.insert( tr( "Perimeter" ), str ); str = QLocale::system().toString( feature->constGeometry()->geometry()->nCoordinates() ); derivedAttributes.insert( tr( "Vertices" ), str ); //add details of closest vertex to identify point closestVertexAttributes( *feature->constGeometry()->geometry(), vId, layer, derivedAttributes ); } else if ( geometryType == QGis::Point && QgsWKBTypes::flatType( wkbType ) == QgsWKBTypes::Point ) { // Include the x and y coordinates of the point as a derived attribute QgsPoint pnt = mCanvas->mapSettings().layerToMapCoordinates( layer, feature->constGeometry()->asPoint() ); QString str = QLocale::system().toString( pnt.x(), 'g', 10 ); derivedAttributes.insert( "X", str ); str = QLocale::system().toString( pnt.y(), 'g', 10 ); derivedAttributes.insert( "Y", str ); if ( QgsWKBTypes::hasZ( wkbType ) ) { str = QLocale::system().toString( static_cast<QgsPointV2*>( feature->constGeometry()->geometry() )->z(), 'g', 10 ); derivedAttributes.insert( "Z", str ); } if ( QgsWKBTypes::hasM( wkbType ) ) { str = QLocale::system().toString( static_cast<QgsPointV2*>( feature->constGeometry()->geometry() )->m(), 'g', 10 ); derivedAttributes.insert( "M", str ); } } return derivedAttributes; }
QMap< QString, QString > QgsMapToolIdentify::featureDerivedAttributes( QgsFeature *feature, QgsMapLayer *layer ) { // Calculate derived attributes and insert: // measure distance or area depending on geometry type QMap< QString, QString > derivedAttributes; // init distance/area calculator QString ellipsoid = QgsProject::instance()->readEntry( "Measure", "/Ellipsoid", GEO_NONE ); QgsDistanceArea calc; calc.setEllipsoidalMode( mCanvas->hasCrsTransformEnabled() ); calc.setEllipsoid( ellipsoid ); calc.setSourceCrs( layer->crs().srsid() ); QGis::WkbType wkbType = QGis::WKBNoGeometry; QGis::GeometryType geometryType = QGis::NoGeometry; if ( feature->geometry() ) { geometryType = feature->geometry()->type(); wkbType = feature->geometry()->wkbType(); } if ( geometryType == QGis::Line ) { double dist = calc.measure( feature->geometry() ); QGis::UnitType myDisplayUnits; convertMeasurement( calc, dist, myDisplayUnits, false ); QString str = calc.textUnit( dist, 3, myDisplayUnits, false ); // dist and myDisplayUnits are out params derivedAttributes.insert( tr( "Length" ), str ); if ( wkbType == QGis::WKBLineString || wkbType == QGis::WKBLineString25D ) { // Add the start and end points in as derived attributes QgsPoint pnt = mCanvas->mapSettings().layerToMapCoordinates( layer, feature->geometry()->asPolyline().first() ); str = QLocale::system().toString( pnt.x(), 'g', 10 ); derivedAttributes.insert( tr( "firstX", "attributes get sorted; translation for lastX should be lexically larger than this one" ), str ); str = QLocale::system().toString( pnt.y(), 'g', 10 ); derivedAttributes.insert( tr( "firstY" ), str ); pnt = mCanvas->mapSettings().layerToMapCoordinates( layer, feature->geometry()->asPolyline().last() ); str = QLocale::system().toString( pnt.x(), 'g', 10 ); derivedAttributes.insert( tr( "lastX", "attributes get sorted; translation for firstX should be lexically smaller than this one" ), str ); str = QLocale::system().toString( pnt.y(), 'g', 10 ); derivedAttributes.insert( tr( "lastY" ), str ); } } else if ( geometryType == QGis::Polygon ) { double area = calc.measure( feature->geometry() ); double perimeter = calc.measurePerimeter( feature->geometry() ); QGis::UnitType myDisplayUnits; convertMeasurement( calc, area, myDisplayUnits, true ); // area and myDisplayUnits are out params QString str = calc.textUnit( area, 3, myDisplayUnits, true ); derivedAttributes.insert( tr( "Area" ), str ); convertMeasurement( calc, perimeter, myDisplayUnits, false ); // perimeter and myDisplayUnits are out params str = calc.textUnit( perimeter, 3, myDisplayUnits, false ); derivedAttributes.insert( tr( "Perimeter" ), str ); } else if ( geometryType == QGis::Point && ( wkbType == QGis::WKBPoint || wkbType == QGis::WKBPoint25D ) ) { // Include the x and y coordinates of the point as a derived attribute QgsPoint pnt = mCanvas->mapSettings().layerToMapCoordinates( layer, feature->geometry()->asPoint() ); QString str = QLocale::system().toString( pnt.x(), 'g', 10 ); derivedAttributes.insert( "X", str ); str = QLocale::system().toString( pnt.y(), 'g', 10 ); derivedAttributes.insert( "Y", str ); } return derivedAttributes; }
void QgsMeasureDialog::updateUi() { // Set tooltip to indicate how we calculate measurments QString toolTip = tr( "The calculations are based on:" ); if ( ! mTool->canvas()->hasCrsTransformEnabled() ) { toolTip += "<br> * " + tr( "Project CRS transformation is turned off." ) + " "; toolTip += tr( "Canvas units setting is taken from project properties setting (%1)." ).arg( QGis::tr( mCanvasUnits ) ); toolTip += "<br> * " + tr( "Ellipsoidal calculation is not possible, as project CRS is undefined." ); setWindowTitle( tr( "Measure (OTF off)" ) ); } else { if ( mDa.ellipsoidalEnabled() ) { toolTip += "<br> * " + tr( "Project CRS transformation is turned on and ellipsoidal calculation is selected." ) + " "; toolTip += "<br> * " + tr( "The coordinates are transformed to the chosen ellipsoid (%1), and the result is in meters" ).arg( mDa.ellipsoid() ); } else { toolTip += "<br> * " + tr( "Project CRS transformation is turned on but ellipsoidal calculation is not selected." ); toolTip += "<br> * " + tr( "The canvas units setting is taken from the project CRS (%1)." ).arg( QGis::tr( mCanvasUnits ) ); } setWindowTitle( tr( "Measure (OTF on)" ) ); } if (( mCanvasUnits == QGis::Meters && mDisplayUnits == QGis::Feet ) || ( mCanvasUnits == QGis::Feet && mDisplayUnits == QGis::Meters ) ) { toolTip += "<br> * " + tr( "Finally, the value is converted from %1 to %2." ).arg( QGis::tr( mCanvasUnits ) ).arg( QGis::tr( mDisplayUnits ) ); } editTotal->setToolTip( toolTip ); mTable->setToolTip( toolTip ); QGis::UnitType newDisplayUnits; double dummy = 1.0; convertMeasurement( dummy, newDisplayUnits, true ); mTable->setHeaderLabels( QStringList( tr( "Segments [%1]" ).arg( QGis::tr( newDisplayUnits ) ) ) ); if ( mMeasureArea ) { double area = 0.0; if ( mTool->points().size() > 1 ) { area = mDa.measurePolygon( mTool->points() ); } mTable->hide(); // Hide the table, only show summary. editTotal->setText( formatArea( area ) ); } else { QList<QgsPoint>::const_iterator it; bool b = true; // first point QgsPoint p1, p2; for ( it = mTool->points().constBegin(); it != mTool->points().constEnd(); ++it ) { p2 = *it; if ( !b ) { double d = mDa.measureLine( p1, p2 ); QGis::UnitType dummyUnits; convertMeasurement( d, dummyUnits, false ); QTreeWidgetItem *item = new QTreeWidgetItem( QStringList( QLocale::system().toString( d , 'f', mDecimalPlaces ) ) ); item->setTextAlignment( 0, Qt::AlignRight ); mTable->addTopLevelItem( item ); mTable->scrollToItem( item ); } p1 = p2; b = false; } mTotal = mDa.measureLine( mTool->points() ); mTable->show(); // Show the table with items editTotal->setText( formatDistance( mTotal ) ); } }
void QgsMeasureDialog::changeProjectionEnabledState() { // store value QSettings settings; if ( mcbProjectionEnabled->isChecked() ) settings.setValue( "/qgis/measure/projectionEnabled", 2 ); else settings.setValue( "/qgis/measure/projectionEnabled", 0 ); // clear interface mTable->clear(); QTreeWidgetItem* item = new QTreeWidgetItem( QStringList( QString::number( 0, 'f', 1 ) ) ); item->setTextAlignment( 0, Qt::AlignRight ); mTable->addTopLevelItem( item ); mTotal = 0; updateUi(); int decimalPlaces = settings.value( "/qgis/measure/decimalplaces", "3" ).toInt(); // create DistanceArea QgsDistanceArea myDa; configureDistanceArea( myDa ); if ( mMeasureArea ) { double area = 0.0; if ( mTool->points().size() > 1 ) { area = myDa.measurePolygon( mTool->points() ); } editTotal->setText( formatArea( area, decimalPlaces ) ); } else { QList<QgsPoint>::const_iterator it; bool b = true; // first point QgsPoint p1, p2; for ( it = mTool->points().constBegin(); it != mTool->points().constEnd(); ++it ) { p2 = *it; if ( !b ) { double d = myDa.measureLine( p1, p2 ); mTotal += d; editTotal->setText( formatDistance( mTotal, decimalPlaces ) ); QGis::UnitType myDisplayUnits; convertMeasurement( d, myDisplayUnits, false ); QTreeWidgetItem *item = mTable->topLevelItem( mTable->topLevelItemCount() - 1 ); item->setText( 0, QLocale::system().toString( d, 'f', decimalPlaces ) ); item = new QTreeWidgetItem( QStringList( QLocale::system().toString( 0.0, 'f', decimalPlaces ) ) ); item->setTextAlignment( 0, Qt::AlignRight ); mTable->addTopLevelItem( item ); mTable->scrollToItem( item ); } p1 = p2; b = false; } } }
bool QgsMapToolIdentify::identifyVectorLayer( QgsVectorLayer *layer, int x, int y ) { if ( !layer ) return false; QMap< QString, QString > attributes, derivedAttributes; QgsPoint point = mCanvas->getCoordinateTransform()->toMapCoordinates( x, y ); derivedAttributes.insert( tr( "(clicked coordinate)" ), point.toString() ); // load identify radius from settings QSettings settings; double identifyValue = settings.value( "/Map/identifyRadius", QGis::DEFAULT_IDENTIFY_RADIUS ).toDouble(); QString ellipsoid = settings.value( "/qgis/measure/ellipsoid", "WGS84" ).toString(); if ( identifyValue <= 0.0 ) identifyValue = QGis::DEFAULT_IDENTIFY_RADIUS; int featureCount = 0; QgsFeatureList featureList; // toLayerCoordinates will throw an exception for an 'invalid' point. // For example, if you project a world map onto a globe using EPSG 2163 // and then click somewhere off the globe, an exception will be thrown. try { // create the search rectangle double searchRadius = mCanvas->extent().width() * ( identifyValue / 100.0 ); QgsRectangle r; r.setXMinimum( point.x() - searchRadius ); r.setXMaximum( point.x() + searchRadius ); r.setYMinimum( point.y() - searchRadius ); r.setYMaximum( point.y() + searchRadius ); r = toLayerCoordinates( layer, r ); layer->select( layer->pendingAllAttributesList(), r, true, true ); QgsFeature f; while ( layer->nextFeature( f ) ) featureList << QgsFeature( f ); } catch ( QgsCsException & cse ) { Q_UNUSED( cse ); // catch exception for 'invalid' point and proceed with no features found QgsDebugMsg( QString( "Caught CRS exception %1" ).arg( cse.what() ) ); } // init distance/area calculator QgsDistanceArea calc; if ( !featureList.count() == 0 ) { calc.setProjectionsEnabled( mCanvas->hasCrsTransformEnabled() ); // project? calc.setEllipsoid( ellipsoid ); calc.setSourceCrs( layer->crs().srsid() ); } QgsFeatureList::iterator f_it = featureList.begin(); for ( ; f_it != featureList.end(); ++f_it ) { featureCount++; QgsFeatureId fid = f_it->id(); QMap<QString, QString> derivedAttributes; // Calculate derived attributes and insert: // measure distance or area depending on geometry type if ( layer->geometryType() == QGis::Line ) { double dist = calc.measure( f_it->geometry() ); QGis::UnitType myDisplayUnits; convertMeasurement( calc, dist, myDisplayUnits, false ); QString str = calc.textUnit( dist, 3, myDisplayUnits, false ); // dist and myDisplayUnits are out params derivedAttributes.insert( tr( "Length" ), str ); if ( f_it->geometry()->wkbType() == QGis::WKBLineString || f_it->geometry()->wkbType() == QGis::WKBLineString25D ) { // Add the start and end points in as derived attributes str = QLocale::system().toString( f_it->geometry()->asPolyline().first().x(), 'g', 10 ); derivedAttributes.insert( tr( "firstX", "attributes get sorted; translation for lastX should be lexically larger than this one" ), str ); str = QLocale::system().toString( f_it->geometry()->asPolyline().first().y(), 'g', 10 ); derivedAttributes.insert( tr( "firstY" ), str ); str = QLocale::system().toString( f_it->geometry()->asPolyline().last().x(), 'g', 10 ); derivedAttributes.insert( tr( "lastX", "attributes get sorted; translation for firstX should be lexically smaller than this one" ), str ); str = QLocale::system().toString( f_it->geometry()->asPolyline().last().y(), 'g', 10 ); derivedAttributes.insert( tr( "lastY" ), str ); } } else if ( layer->geometryType() == QGis::Polygon ) { double area = calc.measure( f_it->geometry() ); QGis::UnitType myDisplayUnits; convertMeasurement( calc, area, myDisplayUnits, true ); // area and myDisplayUnits are out params QString str = calc.textUnit( area, 3, myDisplayUnits, true ); derivedAttributes.insert( tr( "Area" ), str ); } else if ( layer->geometryType() == QGis::Point && ( f_it->geometry()->wkbType() == QGis::WKBPoint || f_it->geometry()->wkbType() == QGis::WKBPoint25D ) ) { // Include the x and y coordinates of the point as a derived attribute QString str; str = QLocale::system().toString( f_it->geometry()->asPoint().x(), 'g', 10 ); derivedAttributes.insert( "X", str ); str = QLocale::system().toString( f_it->geometry()->asPoint().y(), 'g', 10 ); derivedAttributes.insert( "Y", str ); } derivedAttributes.insert( tr( "feature id" ), fid < 0 ? tr( "new feature" ) : FID_TO_STRING( fid ) ); results()->addFeature( layer, *f_it, derivedAttributes ); } QgsDebugMsg( "Feature count on identify: " + QString::number( featureCount ) ); return featureCount > 0; }