double QgsLayoutItemScaleBar::mapWidth() const { if ( !mMap ) { return 0.0; } QgsRectangle mapExtent = mMap->extent(); if ( mSettings.units() == QgsUnitTypes::DistanceUnknownUnit ) { return mapExtent.width(); } else { QgsDistanceArea da; da.setSourceCrs( mMap->crs(), mLayout->project()->transformContext() ); da.setEllipsoid( mLayout->project()->ellipsoid() ); QgsUnitTypes::DistanceUnit units = da.lengthUnits(); double measure = da.measureLine( QgsPointXY( mapExtent.xMinimum(), mapExtent.yMinimum() ), QgsPointXY( mapExtent.xMaximum(), mapExtent.yMinimum() ) ); measure /= QgsUnitTypes::fromUnitToUnitFactor( mSettings.units(), units ); return measure; } }
void TestQgsRectangle::include() { QgsRectangle rect1 = QgsRectangle( 10.0, 20.0, 110.0, 220.0 ); // inside rect1.include( QgsPointXY( 15, 50 ) ); QCOMPARE( rect1.xMinimum(), 10.0 ); QCOMPARE( rect1.yMinimum(), 20.0 ); QCOMPARE( rect1.xMaximum(), 110.0 ); QCOMPARE( rect1.yMaximum(), 220.0 ); rect1.include( QgsPointXY( 5, 50 ) ); QCOMPARE( rect1.xMinimum(), 5.0 ); QCOMPARE( rect1.yMinimum(), 20.0 ); QCOMPARE( rect1.xMaximum(), 110.0 ); QCOMPARE( rect1.yMaximum(), 220.0 ); rect1.include( QgsPointXY( 15, 12 ) ); QCOMPARE( rect1.xMinimum(), 5.0 ); QCOMPARE( rect1.yMinimum(), 12.0 ); QCOMPARE( rect1.xMaximum(), 110.0 ); QCOMPARE( rect1.yMaximum(), 220.0 ); rect1.include( QgsPointXY( 115, 12 ) ); QCOMPARE( rect1.xMinimum(), 5.0 ); QCOMPARE( rect1.yMinimum(), 12.0 ); QCOMPARE( rect1.xMaximum(), 115.0 ); QCOMPARE( rect1.yMaximum(), 220.0 ); rect1.include( QgsPointXY( 115, 242 ) ); QCOMPARE( rect1.xMinimum(), 5.0 ); QCOMPARE( rect1.yMinimum(), 12.0 ); QCOMPARE( rect1.xMaximum(), 115.0 ); QCOMPARE( rect1.yMaximum(), 242.0 ); }
void QgsGrassRegionEdit::drawRegion( QgsMapCanvas *canvas, QgsRubberBand *rubberBand, const QgsRectangle &rect, const QgsCoordinateTransform &coordinateTransform, bool isPolygon ) { QVector<QgsPointXY> points; points.append( QgsPointXY( rect.xMinimum(), rect.yMinimum() ) ); points.append( QgsPointXY( rect.xMaximum(), rect.yMinimum() ) ); points.append( QgsPointXY( rect.xMaximum(), rect.yMaximum() ) ); points.append( QgsPointXY( rect.xMinimum(), rect.yMaximum() ) ); if ( !isPolygon ) { points.append( QgsPointXY( rect.xMinimum(), rect.yMinimum() ) ); } if ( coordinateTransform.isValid() ) { transform( canvas, points, coordinateTransform ); } rubberBand->reset( isPolygon ? QgsWkbTypes::PolygonGeometry : QgsWkbTypes::LineGeometry ); for ( int i = 0; i < points.size(); i++ ) { bool update = false; // true to update canvas if ( i == points.size() - 1 ) update = true; rubberBand->addPoint( points[i], update ); } rubberBand->show(); }
QgsCadUtils::AlignMapPointContext baseContext() { QgsCadUtils::AlignMapPointContext context; context.snappingUtils = mSnappingUtils; context.mapUnitsPerPixel = mMapSettings.mapUnitsPerPixel(); context.cadPointList << QgsPointXY() << QgsPointXY( 30, 20 ) << QgsPointXY( 30, 30 ); return context; }
void QgsMapToolLabel::createRubberBands() { delete mLabelRubberBand; delete mFeatureRubberBand; //label rubber band QgsRectangle rect = mCurrentLabel.pos.labelRect; mLabelRubberBand = new QgsRubberBand( mCanvas, QgsWkbTypes::LineGeometry ); mLabelRubberBand->addPoint( QgsPointXY( rect.xMinimum(), rect.yMinimum() ) ); mLabelRubberBand->addPoint( QgsPointXY( rect.xMinimum(), rect.yMaximum() ) ); mLabelRubberBand->addPoint( QgsPointXY( rect.xMaximum(), rect.yMaximum() ) ); mLabelRubberBand->addPoint( QgsPointXY( rect.xMaximum(), rect.yMinimum() ) ); mLabelRubberBand->addPoint( QgsPointXY( rect.xMinimum(), rect.yMinimum() ) ); mLabelRubberBand->setColor( QColor( 0, 255, 0, 65 ) ); mLabelRubberBand->setWidth( 3 ); mLabelRubberBand->show(); //feature rubber band QgsVectorLayer *vlayer = mCurrentLabel.layer; if ( vlayer ) { QgsFeature f; if ( currentFeature( f, true ) ) { QgsGeometry geom = f.geometry(); if ( !geom.isNull() ) { QgsSettings settings; int r = settings.value( QStringLiteral( "qgis/digitizing/line_color_red" ), 255 ).toInt(); int g = settings.value( QStringLiteral( "qgis/digitizing/line_color_green" ), 0 ).toInt(); int b = settings.value( QStringLiteral( "qgis/digitizing/line_color_blue" ), 0 ).toInt(); int a = settings.value( QStringLiteral( "qgis/digitizing/line_color_alpha" ), 200 ).toInt(); mFeatureRubberBand = new QgsRubberBand( mCanvas, geom.type() ); mFeatureRubberBand->setColor( QColor( r, g, b, a ) ); mFeatureRubberBand->setToGeometry( geom, vlayer ); mFeatureRubberBand->show(); } } //fixpoint rubber band QgsPointXY fixPoint; if ( currentLabelRotationPoint( fixPoint, false, false ) ) { if ( mCanvas ) { const QgsMapSettings &s = mCanvas->mapSettings(); fixPoint = s.mapToLayerCoordinates( vlayer, fixPoint ); } QgsGeometry pointGeom = QgsGeometry::fromPointXY( fixPoint ); mFixPointRubberBand = new QgsRubberBand( mCanvas, QgsWkbTypes::LineGeometry ); mFixPointRubberBand->setColor( QColor( 0, 0, 255, 65 ) ); mFixPointRubberBand->setToGeometry( pointGeom, vlayer ); mFixPointRubberBand->show(); } } }
void QgsGrassNewMapset::setCurrentRegion() { QgsRectangle ext = mIface->mapCanvas()->extent(); QgsCoordinateReferenceSystem srs = mIface->mapCanvas()->mapSettings().destinationCrs(); QgsDebugMsg( "srs = " + srs.toWkt() ); std::vector<QgsPointXY> points; // TODO: this is not perfect points.push_back( QgsPointXY( ext.xMinimum(), ext.yMinimum() ) ); points.push_back( QgsPointXY( ext.xMaximum(), ext.yMaximum() ) ); // TODO add a method, this code is copy-paste from setSelectedRegion if ( srs.isValid() && mCrs.isValid() && srs.srsid() != mCrs.srsid() ) { QgsCoordinateTransform trans( srs, mCrs ); bool ok = true; for ( int i = 0; i < 2; i++ ) { try { points[i] = trans.transform( points[i] ); } catch ( QgsCsException &cse ) { Q_UNUSED( cse ); QgsDebugMsg( "Cannot transform point" ); ok = false; break; } } if ( !ok ) { QgsGrass::warning( tr( "Cannot reproject region" ) ); return; } } mNorthLineEdit->setText( QString::number( points[1].y() ) ); mSouthLineEdit->setText( QString::number( points[0].y() ) ); mEastLineEdit->setText( QString::number( points[1].x() ) ); mWestLineEdit->setText( QString::number( points[0].x() ) ); mRegionModified = true; checkRegion(); drawRegion(); QgsDebugMsg( "setCurrentRegion - End" ); }
void QgsGeometryFollowBoundariesCheck::collectErrors( QList<QgsGeometryCheckError *> &errors, QStringList &/*messages*/, QAtomicInt *progressCounter, const QMap<QString, QgsFeatureIds> &ids ) const { if ( !mIndex || !mCheckLayer ) { return; } QMap<QString, QgsFeatureIds> featureIds = ids.isEmpty() ? allLayerFeatureIds() : ids; featureIds.remove( mCheckLayer->id() ); // Don't check layer against itself QgsGeometryCheckerUtils::LayerFeatures layerFeatures( mContext->featurePools, featureIds, mCompatibleGeometryTypes, progressCounter ); for ( const QgsGeometryCheckerUtils::LayerFeature &layerFeature : layerFeatures ) { const QgsAbstractGeometry *geom = layerFeature.geometry(); // The geometry to crs of the check layer QgsCoordinateTransform crst = QgsCoordinateTransformCache::instance()->transform( layerFeature.layer().crs().authid(), mCheckLayer->crs().authid() ); QScopedPointer<QgsAbstractGeometry> geomt( geom->clone() ); geomt->transform( crst ); QSharedPointer<QgsGeometryEngine> geomEngine = QgsGeometryCheckerUtils::createGeomEngine( geomt.data(), mContext->tolerance ); // Get potential reference features QgsRectangle searchBounds = geomt->boundingBox(); searchBounds.grow( mContext->tolerance ); QgsFeatureIds refFeatureIds = mIndex->intersects( searchBounds ).toSet(); QgsFeatureRequest refFeatureRequest = QgsFeatureRequest().setFilterFids( refFeatureIds ).setSubsetOfAttributes( QgsAttributeList() ); QgsFeatureIterator refFeatureIt = mCheckLayer->getFeatures( refFeatureRequest ); if ( refFeatureIds.isEmpty() ) { // If no potential reference features are found, the geometry is definitely not following boundaries of reference layer features errors.append( new QgsGeometryCheckError( this, layerFeature, QgsPointXY( geom->centroid() ) ) ); } else { // All reference features must be either contained or disjoint from tested geometry QgsFeature refFeature; while ( refFeatureIt.nextFeature( refFeature ) ) { QgsAbstractGeometry *refGeom = refFeature.geometry().geometry(); QSharedPointer<QgsGeometryEngine> refgeomEngine = QgsGeometryCheckerUtils::createGeomEngine( refGeom, mContext->tolerance ); QScopedPointer<QgsAbstractGeometry> reducedRefGeom( refgeomEngine->buffer( -mContext->tolerance, 0 ) ); if ( !( geomEngine->contains( reducedRefGeom.data() ) || geomEngine->disjoint( reducedRefGeom.data() ) ) ) { errors.append( new QgsGeometryCheckError( this, layerFeature, QgsPointXY( geom->centroid() ) ) ); break; } } } } }
void testPointInPolygon() { QgsPointLocator loc( mVL ); QgsPointLocator::MatchList mValid = loc.pointInPolygon( QgsPointXY( 0.8, 0.8 ) ); QCOMPARE( mValid.count(), 1 ); QgsPointLocator::Match m = mValid[0]; QVERIFY( m.isValid() ); QVERIFY( m.hasArea() ); QCOMPARE( m.layer(), mVL ); QCOMPARE( m.featureId(), ( QgsFeatureId )1 ); QgsPointLocator::MatchList mInvalid = loc.pointInPolygon( QgsPointXY( 0, 0 ) ); QCOMPARE( mInvalid.count(), 0 ); }
void testEdgesInTolerance() { QgsPointLocator loc( mVL ); QgsPointLocator::MatchList lst = loc.edgesInRect( QgsPointXY( 0, 0 ), 2 ); QCOMPARE( lst.count(), 3 ); QgsPointLocator::MatchList lst2 = loc.edgesInRect( QgsPointXY( 0, 0 ), 0.9 ); QCOMPARE( lst2.count(), 1 ); // test match filtering FilterExcludeEdge myFilter( QgsPointXY( 1, 0 ), QgsPointXY( 0, 1 ) ); QgsPointLocator::MatchList lst3 = loc.edgesInRect( QgsPointXY( 0, 0 ), 2, &myFilter ); QCOMPARE( lst3.count(), 2 ); }
void testCopy() { std::unique_ptr< QgsVectorLayer > vl = qgis::make_unique< QgsVectorLayer >( "Point", QString(), QStringLiteral( "memory" ) ); for ( QgsFeature f : _pointFeatures() ) vl->dataProvider()->addFeature( f ); std::unique_ptr< QgsSpatialIndexKDBush > index( new QgsSpatialIndexKDBush( *vl->dataProvider() ) ); // create copy of the index std::unique_ptr< QgsSpatialIndexKDBush > indexCopy( new QgsSpatialIndexKDBush( *index ) ); QVERIFY( index->d == indexCopy->d ); QVERIFY( index->d->ref == 2 ); // test that copied index works QList<QgsSpatialIndexKDBushData> fids = indexCopy->intersects( QgsRectangle( 0, 0, 10, 10 ) ); QVERIFY( fids.count() == 1 ); QVERIFY( testContains( fids, 1, QgsPointXY( 1, 1 ) ) ); // check that the index is still shared QVERIFY( index->d == indexCopy->d ); QVERIFY( index->d->ref == 2 ); index.reset(); // test that copied index still works fids = indexCopy->intersects( QgsRectangle( 0, 0, 10, 10 ) ); QVERIFY( fids.count() == 1 ); QVERIFY( testContains( fids, 1, QgsPointXY( 1, 1 ) ) ); QVERIFY( indexCopy->d->ref == 1 ); // assignment operator std::unique_ptr< QgsVectorLayer > vl2 = qgis::make_unique< QgsVectorLayer >( "Point", QString(), QStringLiteral( "memory" ) ); QgsSpatialIndexKDBush index3( *vl2->dataProvider() ); QVERIFY( index3.size() == 0 ); fids = index3.intersects( QgsRectangle( 0, 0, 10, 10 ) ); QVERIFY( fids.count() == 0 ); QVERIFY( index3.d->ref == 1 ); index3 = *indexCopy; QVERIFY( index3.d == indexCopy->d ); QVERIFY( index3.d->ref == 2 ); fids = index3.intersects( QgsRectangle( 0, 0, 10, 10 ) ); QVERIFY( fids.count() == 1 ); QVERIFY( testContains( fids, 1, QgsPointXY( 1, 1 ) ) ); indexCopy.reset(); QVERIFY( index3.d->ref == 1 ); }
void testExtent() { QgsRectangle bbox1( 10, 10, 11, 11 ); // out of layer's bounds QgsPointLocator loc1( mVL, QgsCoordinateReferenceSystem(), &bbox1 ); QgsPointLocator::Match m1 = loc1.nearestVertex( QgsPointXY( 2, 2 ), 999 ); QVERIFY( !m1.isValid() ); QgsRectangle bbox2( 0, 0, 1, 1 ); // in layer's bounds QgsPointLocator loc2( mVL, QgsCoordinateReferenceSystem(), &bbox2 ); QgsPointLocator::Match m2 = loc2.nearestVertex( QgsPointXY( 2, 2 ), 999 ); QVERIFY( m2.isValid() ); QCOMPARE( m2.point(), QgsPointXY( 1, 1 ) ); }
QgsPointXY QgsMapToPixel::transform( const QgsPointXY &p ) const { qreal x = p.x(), y = p.y(); transformInPlace( x, y ); // QgsDebugMsg(QString("Point to pixel...X : %1-->%2, Y: %3 -->%4").arg(p.x()).arg(dx).arg(p.y()).arg(dy)); return QgsPointXY( x, y ); }
void QgsMapToolCapture::stopCapturing() { if ( mRubberBand ) { delete mRubberBand; mRubberBand = nullptr; } if ( mTempRubberBand ) { delete mTempRubberBand; mTempRubberBand = nullptr; } while ( !mGeomErrorMarkers.isEmpty() ) { delete mGeomErrorMarkers.takeFirst(); } mGeomErrors.clear(); mTracingStartPoint = QgsPointXY(); mCapturing = false; mCaptureCurve.clear(); mSnappingMatches.clear(); if ( currentVectorLayer() ) currentVectorLayer()->triggerRepaint(); }
void QgsNetworkAnalysisAlgorithmBase::loadPoints( QgsFeatureSource *source, QVector< QgsPointXY > &points, QHash< int, QgsAttributes > &attributes, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) { feedback->pushInfo( QObject::tr( "Loading points…" ) ); QgsFeature feat; int i = 0; int pointId = 1; double step = source->featureCount() > 0 ? 100.0 / source->featureCount() : 0; QgsFeatureIterator features = source->getFeatures( QgsFeatureRequest().setDestinationCrs( mNetwork->sourceCrs(), context.transformContext() ) ); while ( features.nextFeature( feat ) ) { i++; if ( feedback->isCanceled() ) { break; } feedback->setProgress( i * step ); if ( !feat.hasGeometry() ) continue; QgsGeometry geom = feat.geometry(); QgsAbstractGeometry::vertex_iterator it = geom.vertices_begin(); while ( it != geom.vertices_end() ) { points.push_back( QgsPointXY( *it ) ); attributes.insert( pointId, feat.attributes() ); it++; pointId++; } } }
static QgsFeature _pointFeature( QgsFeatureId id, qreal x, qreal y ) { QgsFeature f( id ); QgsGeometry g = QgsGeometry::fromPointXY( QgsPointXY( x, y ) ); f.setGeometry( g ); return f; }
void testNearestEdge() { QgsPointLocator loc( mVL ); QgsPointXY pt( 1.1, 0.5 ); QgsPointLocator::Match m = loc.nearestEdge( pt, 999 ); QVERIFY( m.isValid() ); QVERIFY( m.hasEdge() ); QCOMPARE( m.layer(), mVL ); QCOMPARE( m.featureId(), ( QgsFeatureId )1 ); QCOMPARE( m.point(), QgsPointXY( 1, 0.5 ) ); QCOMPARE( m.distance(), 0.1 ); QCOMPARE( m.vertexIndex(), 1 ); QgsPointXY pt1, pt2; m.edgePoints( pt1, pt2 ); QCOMPARE( pt1, QgsPointXY( 1, 0 ) ); QCOMPARE( pt2, QgsPointXY( 1, 1 ) ); }
int QgsMapToolCapture::addCurve( QgsCurve *c ) { if ( !c ) { return 1; } if ( !mRubberBand ) { mRubberBand = createRubberBand( mCaptureMode == CapturePolygon ? QgsWkbTypes::PolygonGeometry : QgsWkbTypes::LineGeometry ); } QgsLineString *lineString = c->curveToLine(); QgsPointSequence linePoints; lineString->points( linePoints ); delete lineString; QgsPointSequence::const_iterator ptIt = linePoints.constBegin(); for ( ; ptIt != linePoints.constEnd(); ++ptIt ) { mRubberBand->addPoint( QgsPointXY( ptIt->x(), ptIt->y() ) ); } if ( !mTempRubberBand ) { mTempRubberBand = createRubberBand( mCaptureMode == CapturePolygon ? QgsWkbTypes::PolygonGeometry : QgsWkbTypes::LineGeometry, true ); } else { mTempRubberBand->reset(); } QgsPoint endPt = c->endPoint(); mTempRubberBand->addPoint( QgsPointXY( endPt.x(), endPt.y() ) ); //add last point of c //transform back to layer CRS in case map CRS and layer CRS are different QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mCanvas->currentLayer() ); QgsCoordinateTransform ct = mCanvas->mapSettings().layerTransform( vlayer ); if ( ct.isValid() ) { c->transform( ct, QgsCoordinateTransform::ReverseTransform ); } mCaptureCurve.addCurve( c ); for ( int i = 0; i < c->length(); ++i ) mSnappingMatches.append( QgsPointLocator::Match() ); return 0; }
void QgsMapToolPinLabels::highlightLabel( const QgsLabelPosition &labelpos, const QString &id, const QColor &color ) { QgsRectangle rect = labelpos.labelRect; QgsRubberBand *rb = new QgsRubberBand( mCanvas, QgsWkbTypes::PolygonGeometry ); rb->addPoint( QgsPointXY( rect.xMinimum(), rect.yMinimum() ) ); rb->addPoint( QgsPointXY( rect.xMinimum(), rect.yMaximum() ) ); rb->addPoint( QgsPointXY( rect.xMaximum(), rect.yMaximum() ) ); rb->addPoint( QgsPointXY( rect.xMaximum(), rect.yMinimum() ) ); rb->addPoint( QgsPointXY( rect.xMinimum(), rect.yMinimum() ) ); rb->setColor( color ); rb->setWidth( 0 ); rb->show(); mHighlights.insert( id, rb ); }
bool ProjectorData::approximateSrcRowCol( int destRow, int destCol, int *srcRow, int *srcCol ) { int myMatrixRow = matrixRow( destRow ); int myMatrixCol = matrixCol( destCol ); if ( myMatrixRow > mHelperTopRow ) { // TODO: make it more robust (for random, not sequential reading) nextHelper(); } double myDestY = mDestExtent.yMaximum() - ( destRow + 0.5 ) * mDestYRes; // See the schema in javax.media.jai.WarpGrid doc (but up side down) // TODO: use some kind of cache of values which can be reused double myDestXMin, myDestYMin, myDestXMax, myDestYMax; destPointOnCPMatrix( myMatrixRow + 1, myMatrixCol, &myDestXMin, &myDestYMin ); destPointOnCPMatrix( myMatrixRow, myMatrixCol + 1, &myDestXMax, &myDestYMax ); double yfrac = ( myDestY - myDestYMin ) / ( myDestYMax - myDestYMin ); QgsPointXY &myTop = pHelperTop[destCol]; QgsPointXY &myBot = pHelperBottom[destCol]; // Warning: this is very SLOW compared to the following code!: //double mySrcX = myBot.x() + (myTop.x() - myBot.x()) * yfrac; //double mySrcY = myBot.y() + (myTop.y() - myBot.y()) * yfrac; double tx = myTop.x(); double ty = myTop.y(); double bx = myBot.x(); double by = myBot.y(); double mySrcX = bx + ( tx - bx ) * yfrac; double mySrcY = by + ( ty - by ) * yfrac; if ( !mExtent.contains( QgsPointXY( mySrcX, mySrcY ) ) ) { return false; } // TODO: check again cell selection (coor is in the middle) *srcRow = static_cast< int >( std::floor( ( mSrcExtent.yMaximum() - mySrcY ) / mSrcYRes ) ); *srcCol = static_cast< int >( std::floor( ( mySrcX - mSrcExtent.xMinimum() ) / mSrcXRes ) ); // For now silently correct limits to avoid crashes // TODO: review // should not happen if ( *srcRow >= mSrcRows ) return false; if ( *srcRow < 0 ) return false; if ( *srcCol >= mSrcCols ) return false; if ( *srcCol < 0 ) return false; return true; }
bool QgsBox3d::contains( const QgsPoint &p ) const { if ( !mBounds2d.contains( QgsPointXY( p.x(), p.y() ) ) ) return false; if ( p.is3D() ) return mZmin <= p.z() && p.z() <= mZmax; else return true; }
void testNullGeometries() { QgsVectorLayer *vlNullGeom = new QgsVectorLayer( "Polygon", "x", "memory" ); QgsFeature ff( 0 ); ff.setGeometry( QgsGeometry() ); QgsFeatureList flist; flist << ff; vlNullGeom->dataProvider()->addFeatures( flist ); QgsPointLocator loc( vlNullGeom, QgsCoordinateReferenceSystem(), nullptr ); QgsPointLocator::Match m1 = loc.nearestVertex( QgsPointXY( 2, 2 ), std::numeric_limits<double>::max() ); QVERIFY( !m1.isValid() ); QgsPointLocator::Match m2 = loc.nearestEdge( QgsPointXY( 2, 2 ), std::numeric_limits<double>::max() ); QVERIFY( !m2.isValid() ); delete vlNullGeom; }
void QgsMapToolRotateFeature::applyRotation( double rotation ) { mRotation = rotation; mRotationActive = false; QgsVectorLayer *vlayer = currentVectorLayer(); if ( !vlayer ) { deleteRubberband(); notifyNotVectorLayer(); return; } //calculations for affine transformation double angle = -1 * mRotation * ( M_PI / 180 ); QgsPointXY anchorPoint = toLayerCoordinates( vlayer, mStartPointMapCoords ); double a = std::cos( angle ); double b = -1 * std::sin( angle ); double c = anchorPoint.x() - std::cos( angle ) * anchorPoint.x() + std::sin( angle ) * anchorPoint.y(); double d = std::sin( angle ); double ee = std::cos( angle ); double f = anchorPoint.y() - std::sin( angle ) * anchorPoint.x() - std::cos( angle ) * anchorPoint.y(); vlayer->beginEditCommand( tr( "Features Rotated" ) ); int start; if ( vlayer->geometryType() == 2 ) { start = 1; } else { start = 0; } int i = 0; Q_FOREACH ( QgsFeatureId id, mRotatedFeatures ) { QgsFeature feat; vlayer->getFeatures( QgsFeatureRequest().setFilterFid( id ) ).nextFeature( feat ); QgsGeometry geom = feat.geometry(); i = start; QgsPointXY vertex = geom.vertexAt( i ); while ( vertex != QgsPointXY( 0, 0 ) ) { double newX = a * vertex.x() + b * vertex.y() + c; double newY = d * vertex.x() + ee * vertex.y() + f; vlayer->moveVertex( newX, newY, id, i ); i = i + 1; vertex = geom.vertexAt( i ); } }
QgsPointXY QgsMapToolRotateLabel::rotatePointClockwise( const QgsPointXY &input, const QgsPointXY ¢erPoint, double degrees ) const { double rad = -degrees / 180 * M_PI; double v1x = input.x() - centerPoint.x(); double v1y = input.y() - centerPoint.y(); double v2x = std::cos( rad ) * v1x - std::sin( rad ) * v1y; double v2y = std::sin( rad ) * v1x + std::cos( rad ) * v1y; return QgsPointXY( centerPoint.x() + v2x, centerPoint.y() + v2y ); }
QgsPointXY QgsMapToPixel::toMapPoint( double x, double y ) const { bool invertible; QTransform matrix = mMatrix.inverted( &invertible ); assert( invertible ); qreal mx, my; qreal x_qreal = x, y_qreal = y; matrix.map( x_qreal, y_qreal, &mx, &my ); //QgsDebugMsg(QString("XXX toMapPoint x:%1 y:%2 -> x:%3 y:%4").arg(x).arg(y).arg(mx).arg(my)); return QgsPointXY( mx, my ); }
void TestQgsRectangle::fromCenter() { QgsRectangle rect = QgsRectangle::fromCenterAndSize( QgsPointXY( 12, 21 ), 20, 40 ); QVERIFY( ! rect.isEmpty() ); QCOMPARE( rect.xMinimum(), 2.0 ); QCOMPARE( rect.yMinimum(), 1.0 ); QCOMPARE( rect.xMaximum(), 22.0 ); QCOMPARE( rect.yMaximum(), 41.0 ); rect = QgsRectangle::fromCenterAndSize( QgsPointXY( 12, 21 ), 0, 40 ); QCOMPARE( rect.xMinimum(), 12.0 ); QCOMPARE( rect.yMinimum(), 1.0 ); QCOMPARE( rect.xMaximum(), 12.0 ); QCOMPARE( rect.yMaximum(), 41.0 ); rect = QgsRectangle::fromCenterAndSize( QgsPointXY( 12, 21 ), 20, 0 ); QCOMPARE( rect.xMinimum(), 2.0 ); QCOMPARE( rect.yMinimum(), 21.0 ); QCOMPARE( rect.xMaximum(), 22.0 ); QCOMPARE( rect.yMaximum(), 21.0 ); }
bool QgsProjectiveGeorefTransform::updateParametersFromGCPs( const QVector<QgsPointXY> &mapCoords, const QVector<QgsPointXY> &pixelCoords ) { if ( mapCoords.size() < getMinimumGCPCount() ) return false; // HACK: flip y coordinates, because georeferencer and gdal use different conventions QVector<QgsPointXY> flippedPixelCoords; flippedPixelCoords.reserve( pixelCoords.size() ); Q_FOREACH ( const QgsPointXY &coord, pixelCoords ) { flippedPixelCoords << QgsPointXY( coord.x(), -coord.y() ); }
bool QgsMapToolCapture::tracingMouseMove( QgsMapMouseEvent *e ) { if ( !e->isSnapped() ) return false; QgsPointXY pt0 = tracingStartPoint(); if ( pt0 == QgsPointXY() ) return false; QgsMapCanvasTracer *tracer = QgsMapCanvasTracer::tracerForCanvas( mCanvas ); if ( !tracer ) return false; // this should not happen! mTempRubberBand->reset( mCaptureMode == CapturePolygon ? QgsWkbTypes::PolygonGeometry : QgsWkbTypes::LineGeometry ); QgsTracer::PathError err; QVector<QgsPointXY> points = tracer->findShortestPath( pt0, e->mapPoint(), &err ); if ( points.isEmpty() ) { tracer->reportError( err, false ); return false; } if ( mCaptureMode == CapturePolygon ) mTempRubberBand->addPoint( *mRubberBand->getPoint( 0, 0 ), false ); // if there is offset, we need to fix the rubber bands to make sure they are aligned correctly. // There are two cases we need to sort out: // 1. the last point of mRubberBand may need to be moved off the traced curve to respect the offset // 2. extra first point of mTempRubberBand may be needed if there is gap between where mRubberBand ends and trace starts if ( mRubberBand->numberOfVertices() != 0 ) { QgsPointXY lastPoint = *mRubberBand->getPoint( 0, mRubberBand->numberOfVertices() - 1 ); if ( lastPoint == pt0 && points[0] != lastPoint ) { // if rubber band had just one point, for some strange reason it contains the point twice // we only want to move the last point if there are multiple points already if ( mRubberBand->numberOfVertices() > 2 || ( mRubberBand->numberOfVertices() == 2 && *mRubberBand->getPoint( 0, 0 ) != *mRubberBand->getPoint( 0, 1 ) ) ) mRubberBand->movePoint( points[0] ); } else { mTempRubberBand->addPoint( lastPoint, false ); } } // update rubberband for ( int i = 0; i < points.count(); ++i ) mTempRubberBand->addPoint( points.at( i ), i == points.count() - 1 ); tracer->reportError( QgsTracer::ErrNone, false ); // clear messagebar if there was any error return true; }
void QgsGPSInformationWidget::connectGps() { // clear position page fields to give better indication that something happened (or didn't happen) mTxtLatitude->clear(); mTxtLongitude->clear(); mTxtAltitude->clear(); mTxtDateTime->clear(); mTxtSpeed->clear(); mTxtDirection->clear(); mTxtHdop->clear(); mTxtVdop->clear(); mTxtPdop->clear(); mTxtFixMode->clear(); mTxtFixType->clear(); mTxtQuality->clear(); mTxtSatellitesUsed->clear(); mTxtStatus->clear(); mLastGpsPosition = QgsPointXY( 0.0, 0.0 ); QString port; if ( mRadUserPath->isChecked() ) { port = mCboDevices->currentData().toString(); if ( port.isEmpty() ) { QMessageBox::information( this, tr( "/gps" ), tr( "No path to the GPS port " "is specified. Please enter a path then try again." ) ); //toggle the button back off mConnectButton->setChecked( false ); return; } } else if ( mRadGpsd->isChecked() ) { port = QStringLiteral( "%1:%2:%3" ).arg( mGpsdHost->text(), mGpsdPort->text(), mGpsdDevice->text() ); } else if ( mRadInternal->isChecked() ) { port = QStringLiteral( "internalGPS" ); } mGPSPlainTextEdit->appendPlainText( tr( "Connecting..." ) ); showStatusBarMessage( tr( "Connecting to GPS device..." ) ); QgsGPSDetector *detector = new QgsGPSDetector( port ); connect( detector, static_cast < void ( QgsGPSDetector::* )( QgsGPSConnection * ) > ( &QgsGPSDetector::detected ), this, &QgsGPSInformationWidget::connected ); connect( detector, &QgsGPSDetector::detectionFailed, this, &QgsGPSInformationWidget::timedout ); detector->advance(); // start the detection process }
void testNearestVertex() { QgsPointLocator loc( mVL ); QgsPointXY pt( 2, 2 ); QgsPointLocator::Match m = loc.nearestVertex( pt, 999 ); QVERIFY( m.isValid() ); QVERIFY( m.hasVertex() ); QCOMPARE( m.layer(), mVL ); QCOMPARE( m.featureId(), ( QgsFeatureId )1 ); QCOMPARE( m.point(), QgsPointXY( 1, 1 ) ); QCOMPARE( m.distance(), std::sqrt( 2.0 ) ); QCOMPARE( m.vertexIndex(), 2 ); }
QgsPointXY QgsCoordinateTransform::transform( const double theX, const double theY = 0.0, TransformDirection direction ) const { try { return transform( QgsPointXY( theX, theY ), direction ); } catch ( const QgsCsException & ) { // rethrow the exception QgsDebugMsg( QStringLiteral( "rethrowing exception" ) ); throw; } }