26 #include <SpatialIndex.h> 28 #include <QLinkedListIterator> 34 static SpatialIndex::Point point2point(
const QgsPointXY &point )
36 double plow[2] = { point.
x(), point.
y() };
37 return Point( plow, 2 );
41 static SpatialIndex::Region rect2region(
const QgsRectangle &rect )
45 return SpatialIndex::Region( pLow, pHigh, 2 );
53 static const double POINT_LOC_EPSILON = 1e-12;
67 : mDataList( dataList )
71 IData *
getNext()
override {
return mIt.next(); }
72 bool hasNext()
override {
return mIt.hasNext(); }
74 uint32_t
size()
override { Q_ASSERT(
false &&
"not available" );
return 0; }
75 void rewind()
override { Q_ASSERT(
false &&
"not available" ); }
78 QLinkedList<RTree::Data *> mDataList;
79 QLinkedListIterator<RTree::Data *> mIt;
97 , mSrcPoint( srcPoint )
101 void visitNode(
const INode &n )
override { Q_UNUSED( n ); }
102 void visitData( std::vector<const IData *> &v )
override { Q_UNUSED( v ); }
108 int vertexIndex, beforeVertex, afterVertex;
117 if ( mFilter && !mFilter->acceptMatch( m ) )
120 if ( !mBest.isValid() || m.
distance() < mBest.distance() )
146 , mSrcPoint( srcPoint )
150 void visitNode(
const INode &n )
override { Q_UNUSED( n ); }
151 void visitData( std::vector<const IData *> &v )
override { Q_UNUSED( v ); }
164 edgePoints[0] = geom->
vertexAt( afterVertex - 1 );
165 edgePoints[1] = geom->
vertexAt( afterVertex );
168 if ( mFilter && !mFilter->acceptMatch( m ) )
171 if ( !mBest.isValid() || m.
distance() < mBest.distance() )
200 void visitNode(
const INode &n )
override { Q_UNUSED( n ); }
201 void visitData( std::vector<const IData *> &v )
override { Q_UNUSED( v ); }
227 static const int INSIDE = 0;
230 static const int BOTTOM = 4;
231 static const int TOP = 8;
237 OutCode code = INSIDE;
252 OutCode outcode0 = computeOutCode( x0, y0 );
253 OutCode outcode1 = computeOutCode( x1, y1 );
258 if ( !( outcode0 | outcode1 ) )
264 else if ( outcode0 & outcode1 )
276 OutCode outcodeOut = outcode0 ? outcode0 : outcode1;
280 if ( outcodeOut & TOP )
283 x = x0 + ( x1 - x0 ) * ( mRect.
yMaximum() - y0 ) / ( y1 - y0 );
286 else if ( outcodeOut & BOTTOM )
289 x = x0 + ( x1 - x0 ) * ( mRect.
yMinimum() - y0 ) / ( y1 - y0 );
292 else if ( outcodeOut & RIGHT )
295 y = y0 + ( y1 - y0 ) * ( mRect.
xMaximum() - x0 ) / ( x1 - x0 );
298 else if ( outcodeOut & LEFT )
301 y = y0 + ( y1 - y0 ) * ( mRect.
xMinimum() - x0 ) / ( x1 - x0 );
309 if ( outcodeOut == outcode0 )
313 outcode0 = computeOutCode( x0, y0 );
319 outcode1 = computeOutCode( x1, y1 );
334 QByteArray wkb( geom->
asWkb() );
345 bool hasZValue =
false;
366 double prevx = 0.0, prevy = 0.0;
367 for (
int index = 0; index < nPoints; ++index )
369 double thisx = 0.0, thisy = 0.0;
370 wkbPtr >> thisx >> thisy;
372 wkbPtr +=
sizeof( double );
379 edgePoints[0].
set( prevx, prevy );
380 edgePoints[1].
set( thisx, thisy );
399 for (
int linenr = 0, pointIndex = 0; linenr < nLines; ++linenr )
405 double prevx = 0.0, prevy = 0.0;
406 for (
int pointnr = 0; pointnr < nPoints; ++pointnr )
408 double thisx = 0.0, thisy = 0.0;
409 wkbPtr >> thisx >> thisy;
411 wkbPtr +=
sizeof( double );
418 edgePoints[0].
set( prevx, prevy );
419 edgePoints[1].
set( thisx, thisy );
441 for (
int ringnr = 0, pointIndex = 0; ringnr < nRings; ++ringnr )
446 double prevx = 0.0, prevy = 0.0;
447 for (
int pointnr = 0; pointnr < nPoints; ++pointnr )
449 double thisx = 0.0, thisy = 0.0;
450 wkbPtr >> thisx >> thisy;
452 wkbPtr +=
sizeof( double );
459 edgePoints[0].
set( prevx, prevy );
460 edgePoints[1].
set( thisx, thisy );
481 for (
int polynr = 0, pointIndex = 0; polynr < nPolygons; ++polynr )
486 for (
int ringnr = 0; ringnr < nRings; ++ringnr )
491 double prevx = 0.0, prevy = 0.0;
492 for (
int pointnr = 0; pointnr < nPoints; ++pointnr )
494 double thisx = 0.0, thisy = 0.0;
495 wkbPtr >> thisx >> thisy;
497 wkbPtr +=
sizeof( double );
504 edgePoints[0].
set( prevx, prevy );
505 edgePoints[1].
set( thisx, thisy );
538 , mSrcRect( srcRect )
542 void visitNode(
const INode &n )
override { Q_UNUSED( n ); }
543 void visitData( std::vector<const IData *> &v )
override { Q_UNUSED( v ); }
550 Q_FOREACH (
const QgsPointLocator::Match &m, _geometrySegmentsInRect( geom, mSrcRect, mLocator->mLayer,
id ) )
553 if ( mFilter && !mFilter->acceptMatch( m ) )
584 void getNextEntry(
const IEntry &entry, id_type &nextEntry,
bool &hasNext )
override 586 const INode *n =
dynamic_cast<const INode *
>( &entry );
590 QgsDebugMsg( QString(
"NODE: %1" ).arg( n->getIdentifier() ) );
591 if ( n->getLevel() > 0 )
594 for ( uint32_t cChild = 0; cChild < n->getChildrenCount(); cChild++ )
596 QgsDebugMsg( QString(
"- CH: %1" ).arg( n->getChildIdentifier( cChild ) ) );
597 ids.push( n->getChildIdentifier( cChild ) );
603 for ( uint32_t cChild = 0; cChild < n->getChildrenCount(); cChild++ )
605 QgsDebugMsg( QString(
"- L: %1" ).arg( n->getChildIdentifier( cChild ) ) );
611 nextEntry = ids.back();
624 : mIsEmptyLayer( false )
634 mStorage = StorageManager::createNewMemoryStorageManager();
671 mContext.reset(
nullptr );
675 mContext = std::unique_ptr<QgsRenderContext>(
new QgsRenderContext( *context ) );
689 return mRTree || mIsEmptyLayer;
697 QLinkedList<RTree::Data *> dataList;
719 QgsDebugMsg( QString(
"could not transform bounding box to map, skipping the snap filter (%1)" ).arg( e.
what() ) );
726 std::unique_ptr< QgsFeatureRenderer > renderer( mLayer->
renderer() ? mLayer->
renderer()->
clone() : nullptr );
731 ctx = mContext.get();
735 renderer->startRender( *ctx, mLayer->
fields() );
742 int indexedCount = 0;
749 if ( filter && ctx && renderer )
751 ctx->expressionContext().setFeature( f );
752 if ( !renderer->willRenderFeature( f, *ctx ) )
763 transformedGeometry.
transform( mTransform );
770 QgsDebugMsg( QString(
"could not transform geometry to map, skipping the snap for it (%1)" ).arg( e.
what() ) );
776 dataList <<
new RTree::Data( 0,
nullptr, r, f.
id() );
778 if ( mGeoms.contains( f.
id() ) )
779 delete mGeoms.take( f.
id() );
783 if ( maxFeaturesToIndex != -1 && indexedCount > maxFeaturesToIndex )
785 qDeleteAll( dataList );
792 double fillFactor = 0.7;
793 unsigned long indexCapacity = 10;
794 unsigned long leafCapacity = 10;
795 unsigned long dimension = 2;
796 RTree::RTreeVariant variant = RTree::RV_RSTAR;
797 SpatialIndex::id_type indexId;
799 if ( dataList.isEmpty() )
801 mIsEmptyLayer =
true;
806 mRTree = RTree::createAndBulkLoadNewRTree( RTree::BLM_STR, stream, *mStorage, fillFactor, indexCapacity,
807 leafCapacity, dimension, variant, indexId );
809 if ( ctx && renderer )
811 renderer->stopRender( *ctx );
823 mIsEmptyLayer =
false;
825 qDeleteAll( mGeoms );
830 void QgsPointLocator::onFeatureAdded(
QgsFeatureId fid )
847 std::unique_ptr< QgsFeatureRenderer > renderer( mLayer->
renderer() ? mLayer->
renderer()->
clone() : nullptr );
851 ctx = mContext.get();
852 if ( renderer && ctx )
855 renderer->startRender( *ctx, mLayer->
fields() );
857 ctx->expressionContext().setFeature( f );
858 if ( !renderer->willRenderFeature( f, *ctx ) )
863 renderer->stopRender( *ctx );
882 QgsDebugMsg( QString(
"could not transform geometry to map, skipping the snap for it (%1)" ).arg( e.
what() ) );
890 SpatialIndex::Region r( rect2region( bbox ) );
891 mRTree->insertData( 0,
nullptr, r, f.
id() );
893 if ( mGeoms.contains( f.
id() ) )
894 delete mGeoms.take( f.
id() );
900 void QgsPointLocator::onFeatureDeleted(
QgsFeatureId fid )
905 if ( mGeoms.contains( fid ) )
907 mRTree->deleteData( rect2region( mGeoms[fid]->boundingBox() ), fid );
908 delete mGeoms.take( fid );
916 onFeatureDeleted( fid );
917 onFeatureAdded( fid );
920 void QgsPointLocator::onAttributeValueChanged(
QgsFeatureId fid,
int idx,
const QVariant &value )
926 onFeatureDeleted( fid );
927 onFeatureAdded( fid );
943 QgsRectangle rect( point.
x() - tolerance, point.
y() - tolerance, point.
x() + tolerance, point.
y() + tolerance );
944 mRTree->intersectsWithQuery( rect2region( rect ), visitor );
965 QgsRectangle rect( point.
x() - tolerance, point.
y() - tolerance, point.
x() + tolerance, point.
y() + tolerance );
966 mRTree->intersectsWithQuery( rect2region( rect ), visitor );
982 if ( mlist.count() && mlist.at( 0 ).isValid() )
984 return mlist.at( 0 );
987 if ( tolerance == 0 )
1021 mRTree->intersectsWithQuery( rect2region( rect ), visitor );
1028 QgsRectangle rect( point.
x() - tolerance, point.
y() - tolerance, point.
x() + tolerance, point.
y() + tolerance );
1048 mRTree->intersectsWithQuery( point2point( point ), visitor );
The class defines interface for querying point location:
Wrapper for iterator of features from vector data provider or vector layer.
double closestSegmentWithContext(const QgsPointXY &point, QgsPointXY &minDistPoint, int &afterVertex, int *leftOf=nullptr, double epsilon=DEFAULT_SEGMENT_EPSILON) const
Searches for the closest segment of geometry to the given point.
void visitData(std::vector< const IData *> &v) override
void set(double x, double y)
Sets the x and y value of the point.
A rectangle specified with double values.
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.
QgsVectorLayer * layer() const
The vector layer where the snap occurred.
void visitData(const IData &d) override
bool hasIndex() const
Indicate whether the data have been already indexed.
Features may be filtered, i.e. some features may not be rendered (categorized, rule based ...
QgsWkbTypes::Type wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
QgsPointLocator_VisitorArea(QgsPointLocator *pl, const QgsPointXY &origPt, QgsPointLocator::MatchList &list)
constructor
A class to represent a 2D point.
Match nearestEdge(const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter=nullptr)
Find nearest edge to the specified point - up to distance specified by tolerance Optional filter may ...
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
QgsCoordinateReferenceSystem destinationCrs() const
Gets destination CRS - may be an invalid QgsCoordinateReferenceSystem if not doing OTF reprojection...
QgsWkbTypes::GeometryType geometryType() const
Returns point, line or polygon.
class QList< QgsPointLocator::Match > MatchList
Helper class used when traversing the index looking for edges - builds a list of matches.
A geometry is the spatial representation of a feature.
Match nearestArea(const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter=nullptr)
Find nearest area to the specified point - up to distance specified by tolerance Optional filter may ...
bool rebuildIndex(int maxFeaturesToIndex=-1)
void visitNode(const INode &n) override
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Interface that allows rejection of some matches in intersection queries (e.g.
bool hasGeometry() const
Returns true if the feature has an associated geometry.
void featureDeleted(QgsFeatureId fid)
Emitted when a feature has been deleted.
void styleChanged()
Signal emitted whenever a change affects the layer's style.
QgsPointLocator_Stream(const QLinkedList< RTree::Data *> &dataList)
QgsPointXY closestVertex(const QgsPointXY &point, int &atVertex, int &beforeVertex, int &afterVertex, double &sqrDist) const
Returns the vertex closest to the given point, the corresponding vertex index, squared distance snap ...
bool intersects(const QgsRectangle &rectangle) const
Returns true if this geometry exactly intersects with a rectangle.
~QgsPointLocator() override
Type
The WKB type describes the number of dimensions a geometry has.
QgsFields fields() const override
Returns the list of fields of this layer.
QgsPointLocator_VisitorNearestVertex(QgsPointLocator *pl, QgsPointLocator::Match &m, const QgsPointXY &srcPoint, QgsPointLocator::MatchFilter *filter=nullptr)
OutCode computeOutCode(double x, double y)
void setRenderContext(const QgsRenderContext *context)
Configure render context - if not null, it will use to index only visible feature.
Snapped to a vertex. Can be a vertex of the geometry or an intersection.
Helper class to dump the R-index nodes and their content.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
bool isSegmentInRect(double x0, double y0, double x1, double y1)
QgsFeatureRequest & setFilterRect(const QgsRectangle &rectangle)
Sets the rectangle from which features will be taken.
QgsCoordinateReferenceSystem crs() const
Returns the layer's spatial reference system.
void geometryChanged(QgsFeatureId fid, const QgsGeometry &geometry)
Is emitted whenever a geometry change is done in the edit buffer.
void visitNode(const INode &n) override
void setExtent(const QgsRectangle *extent)
Configure extent - if not null, it will index only that area.
Helper class used when traversing the index with areas - builds a list of matches.
QgsFeatureRenderer * renderer()
Returns renderer.
void attributeValueChanged(QgsFeatureId fid, int idx, const QVariant &value)
Is emitted whenever an attribute value change is done in the edit buffer.
void featureAdded(QgsFeatureId fid)
Emitted when a new feature has been added to the layer.
Contains information about the context in which a coordinate transform is executed.
QgsGeometry geometry() const
Returns the geometry associated with this feature.
void visitData(const IData &d) override
Helper class used when traversing the index looking for vertices - builds a list of matches...
const QgsRectangle * extent() const
Gets extent of the area point locator covers - if null then it caches the whole layer.
void visitData(std::vector< const IData *> &v) override
QByteArray asWkb() const
Export the geometry to WKB.
IData * getNext() override
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const override
Query the layer for features specified in request.
void visitData(std::vector< const IData *> &v) override
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
double xMaximum() const
Returns the x maximum value (right side of rectangle).
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
QgsPointLocator(QgsVectorLayer *layer, const QgsCoordinateReferenceSystem &destinationCrs=QgsCoordinateReferenceSystem(), const QgsCoordinateTransformContext &transformContext=QgsCoordinateTransformContext(), const QgsRectangle *extent=nullptr)
Construct point locator for a layer.
QgsPointXY point() const
for vertex / edge match coords depending on what class returns it (geom.cache: layer coords...
Contains information about the context of a rendering operation.
QgsPoint vertexAt(int atVertex) const
Returns coordinates of a vertex.
void visitData(std::vector< const IData *> &v) override
QgsPointLocator_VisitorNearestEdge(QgsPointLocator *pl, QgsPointLocator::Match &m, const QgsPointXY &srcPoint, QgsPointLocator::MatchFilter *filter=nullptr)
Helper class for bulk loading of R-trees.
void visitData(const IData &d) override
MatchList pointInPolygon(const QgsPointXY &point)
find out if the point is in any polygons
void visitNode(const INode &n) override
bool init(int maxFeaturesToIndex=-1)
Prepare the index for queries.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
This class represents a coordinate reference system (CRS).
_CohenSutherland(const QgsRectangle &rect)
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
bool isNull() const
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
void getNextEntry(const IEntry &entry, id_type &nextEntry, bool &hasNext) override
Match nearestVertex(const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter=nullptr)
Find nearest vertex to the specified point - up to distance specified by tolerance Optional filter ma...
void visitData(const IData &d) override
double xMinimum() const
Returns the x minimum value (left side of rectangle).
void dataChanged()
Data of layer changed.
double distance() const
for vertex / edge match units depending on what class returns it (geom.cache: layer units...
double yMaximum() const
Returns the y maximum value (top side of rectangle).
Helper class used when traversing the index looking for edges - builds a list of matches.
void visitNode(const INode &n) override
MatchList edgesInRect(const QgsRectangle &rect, QgsPointLocator::MatchFilter *filter=nullptr)
Find edges within a specified recangle Optional filter may discard unwanted matches.
QList< int > QgsAttributeList
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
QgsFeatureId featureId() const
The id of the feature to which the snapped geometry belongs.
bool nextFeature(QgsFeature &f)
Represents a vector layer which manages a vector based data sets.
QgsWkbTypes::Type readHeader() const
readHeader
Defines a QGIS exception class.
virtual QgsFeatureRenderer * clone() const =0
Create a deep copy of this renderer.
QgsPointLocator_VisitorEdgesInRect(QgsPointLocator *pl, QgsPointLocator::MatchList &lst, const QgsRectangle &srcRect, QgsPointLocator::MatchFilter *filter=nullptr)
bool isValid() const
Returns whether this CRS is correctly initialized and usable.