25 #include <SpatialIndex.h> 27 #include <QLinkedListIterator> 33 static SpatialIndex::Point point2point(
const QgsPointXY &point )
35 double plow[2] = { point.
x(), point.
y() };
36 return Point( plow, 2 );
40 static SpatialIndex::Region rect2region(
const QgsRectangle &rect )
44 return SpatialIndex::Region( pLow, pHigh, 2 );
52 static const double POINT_LOC_EPSILON = 1e-12;
66 : mDataList( dataList )
70 IData *
getNext()
override {
return mIt.next(); }
71 bool hasNext()
override {
return mIt.hasNext(); }
73 uint32_t
size()
override { Q_ASSERT(
false &&
"not available" );
return 0; }
74 void rewind()
override { Q_ASSERT(
false &&
"not available" ); }
77 QLinkedList<RTree::Data *> mDataList;
78 QLinkedListIterator<RTree::Data *> mIt;
96 , mSrcPoint( srcPoint )
100 void visitNode(
const INode &n )
override { Q_UNUSED( n ); }
101 void visitData( std::vector<const IData *> &v )
override { Q_UNUSED( v ); }
107 int vertexIndex, beforeVertex, afterVertex;
116 if ( mFilter && !mFilter->acceptMatch( m ) )
119 if ( !mBest.isValid() || m.
distance() < mBest.distance() )
145 , mSrcPoint( srcPoint )
149 void visitNode(
const INode &n )
override { Q_UNUSED( n ); }
150 void visitData( std::vector<const IData *> &v )
override { Q_UNUSED( v ); }
163 edgePoints[0] = geom->
vertexAt( afterVertex - 1 );
164 edgePoints[1] = geom->
vertexAt( afterVertex );
167 if ( mFilter && !mFilter->acceptMatch( m ) )
170 if ( !mBest.isValid() || m.
distance() < mBest.distance() )
199 void visitNode(
const INode &n )
override { Q_UNUSED( n ); }
200 void visitData( std::vector<const IData *> &v )
override { Q_UNUSED( v ); }
226 static const int INSIDE = 0;
229 static const int BOTTOM = 4;
230 static const int TOP = 8;
236 OutCode code = INSIDE;
251 OutCode outcode0 = computeOutCode( x0, y0 );
252 OutCode outcode1 = computeOutCode( x1, y1 );
257 if ( !( outcode0 | outcode1 ) )
263 else if ( outcode0 & outcode1 )
275 OutCode outcodeOut = outcode0 ? outcode0 : outcode1;
279 if ( outcodeOut & TOP )
282 x = x0 + ( x1 - x0 ) * ( mRect.
yMaximum() - y0 ) / ( y1 - y0 );
285 else if ( outcodeOut & BOTTOM )
288 x = x0 + ( x1 - x0 ) * ( mRect.
yMinimum() - y0 ) / ( y1 - y0 );
291 else if ( outcodeOut & RIGHT )
294 y = y0 + ( y1 - y0 ) * ( mRect.
xMaximum() - x0 ) / ( x1 - x0 );
297 else if ( outcodeOut & LEFT )
300 y = y0 + ( y1 - y0 ) * ( mRect.
xMinimum() - x0 ) / ( x1 - x0 );
308 if ( outcodeOut == outcode0 )
312 outcode0 = computeOutCode( x0, y0 );
318 outcode1 = computeOutCode( x1, y1 );
333 QByteArray wkb( geom->
asWkb() );
344 bool hasZValue =
false;
365 double prevx = 0.0, prevy = 0.0;
366 for (
int index = 0; index < nPoints; ++index )
368 double thisx = 0.0, thisy = 0.0;
369 wkbPtr >> thisx >> thisy;
371 wkbPtr +=
sizeof( double );
378 edgePoints[0].
set( prevx, prevy );
379 edgePoints[1].
set( thisx, thisy );
398 for (
int linenr = 0, pointIndex = 0; linenr < nLines; ++linenr )
404 double prevx = 0.0, prevy = 0.0;
405 for (
int pointnr = 0; pointnr < nPoints; ++pointnr )
407 double thisx = 0.0, thisy = 0.0;
408 wkbPtr >> thisx >> thisy;
410 wkbPtr +=
sizeof( double );
417 edgePoints[0].
set( prevx, prevy );
418 edgePoints[1].
set( thisx, thisy );
440 for (
int ringnr = 0, pointIndex = 0; ringnr < nRings; ++ringnr )
445 double prevx = 0.0, prevy = 0.0;
446 for (
int pointnr = 0; pointnr < nPoints; ++pointnr )
448 double thisx = 0.0, thisy = 0.0;
449 wkbPtr >> thisx >> thisy;
451 wkbPtr +=
sizeof( double );
458 edgePoints[0].
set( prevx, prevy );
459 edgePoints[1].
set( thisx, thisy );
480 for (
int polynr = 0, pointIndex = 0; polynr < nPolygons; ++polynr )
485 for (
int ringnr = 0; ringnr < nRings; ++ringnr )
490 double prevx = 0.0, prevy = 0.0;
491 for (
int pointnr = 0; pointnr < nPoints; ++pointnr )
493 double thisx = 0.0, thisy = 0.0;
494 wkbPtr >> thisx >> thisy;
496 wkbPtr +=
sizeof( double );
503 edgePoints[0].
set( prevx, prevy );
504 edgePoints[1].
set( thisx, thisy );
537 , mSrcRect( srcRect )
541 void visitNode(
const INode &n )
override { Q_UNUSED( n ); }
542 void visitData( std::vector<const IData *> &v )
override { Q_UNUSED( v ); }
549 Q_FOREACH (
const QgsPointLocator::Match &m, _geometrySegmentsInRect( geom, mSrcRect, mLocator->mLayer,
id ) )
552 if ( mFilter && !mFilter->acceptMatch( m ) )
583 void getNextEntry(
const IEntry &entry, id_type &nextEntry,
bool &hasNext )
override 585 const INode *n =
dynamic_cast<const INode *
>( &entry );
589 QgsDebugMsg( QString(
"NODE: %1" ).arg( n->getIdentifier() ) );
590 if ( n->getLevel() > 0 )
593 for ( uint32_t cChild = 0; cChild < n->getChildrenCount(); cChild++ )
595 QgsDebugMsg( QString(
"- CH: %1" ).arg( n->getChildIdentifier( cChild ) ) );
596 ids.push( n->getChildIdentifier( cChild ) );
602 for ( uint32_t cChild = 0; cChild < n->getChildrenCount(); cChild++ )
604 QgsDebugMsg( QString(
"- L: %1" ).arg( n->getChildIdentifier( cChild ) ) );
610 nextEntry = ids.back();
623 : mIsEmptyLayer( false )
633 mStorage = StorageManager::createNewMemoryStorageManager();
673 return mRTree || mIsEmptyLayer;
681 QLinkedList<RTree::Data *> dataList;
702 QgsDebugMsg( QString(
"could not transform bounding box to map, skipping the snap filter (%1)" ).arg( e.
what() ) );
708 int indexedCount = 0;
719 transformedGeometry.
transform( mTransform );
726 QgsDebugMsg( QString(
"could not transform geometry to map, skipping the snap for it (%1)" ).arg( e.
what() ) );
732 dataList <<
new RTree::Data( 0,
nullptr, r, f.
id() );
734 if ( mGeoms.contains( f.
id() ) )
735 delete mGeoms.take( f.
id() );
739 if ( maxFeaturesToIndex != -1 && indexedCount > maxFeaturesToIndex )
741 qDeleteAll( dataList );
748 double fillFactor = 0.7;
749 unsigned long indexCapacity = 10;
750 unsigned long leafCapacity = 10;
751 unsigned long dimension = 2;
752 RTree::RTreeVariant variant = RTree::RV_RSTAR;
753 SpatialIndex::id_type indexId;
755 if ( dataList.isEmpty() )
757 mIsEmptyLayer =
true;
762 mRTree = RTree::createAndBulkLoadNewRTree( RTree::BLM_STR, stream, *mStorage, fillFactor, indexCapacity,
763 leafCapacity, dimension, variant, indexId );
773 mIsEmptyLayer =
false;
775 qDeleteAll( mGeoms );
780 void QgsPointLocator::onFeatureAdded(
QgsFeatureId fid )
807 QgsDebugMsg( QString(
"could not transform geometry to map, skipping the snap for it (%1)" ).arg( e.
what() ) );
815 SpatialIndex::Region r( rect2region( bbox ) );
816 mRTree->insertData( 0,
nullptr, r, f.
id() );
818 if ( mGeoms.contains( f.
id() ) )
819 delete mGeoms.take( f.
id() );
825 void QgsPointLocator::onFeatureDeleted(
QgsFeatureId fid )
830 if ( mGeoms.contains( fid ) )
832 mRTree->deleteData( rect2region( mGeoms[fid]->boundingBox() ), fid );
833 delete mGeoms.take( fid );
840 onFeatureDeleted( fid );
841 onFeatureAdded( fid );
856 QgsRectangle rect( point.
x() - tolerance, point.
y() - tolerance, point.
x() + tolerance, point.
y() + tolerance );
857 mRTree->intersectsWithQuery( rect2region( rect ), visitor );
878 QgsRectangle rect( point.
x() - tolerance, point.
y() - tolerance, point.
x() + tolerance, point.
y() + tolerance );
879 mRTree->intersectsWithQuery( rect2region( rect ), visitor );
895 if ( mlist.count() && mlist.at( 0 ).isValid() )
897 return mlist.at( 0 );
900 if ( tolerance == 0 )
934 mRTree->intersectsWithQuery( rect2region( rect ), visitor );
941 QgsRectangle rect( point.
x() - tolerance, point.
y() - tolerance, point.
x() + tolerance, point.
y() + tolerance );
961 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.
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.
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
Get 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.
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.
QgsPointLocator_VisitorNearestVertex(QgsPointLocator *pl, QgsPointLocator::Match &m, const QgsPointXY &srcPoint, QgsPointLocator::MatchFilter *filter=nullptr)
OutCode computeOutCode(double x, double y)
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.
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
Get 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.
OperationResult transform(const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection direction=QgsCoordinateTransform::ForwardTransform, bool transformZ=false)
Transforms this geometry as described by the coordinate transform ct.
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...
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
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.
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.