22 #include <spatialindex/SpatialIndex.h>
24 #include <QLinkedListIterator>
26 using namespace SpatialIndex;
35 return Point( plow, 2 );
41 double pLow[2], pHigh[2];
46 return SpatialIndex::Region( pLow, pHigh, 2 );
66 virtual IData*
getNext()
override {
return mIt.next(); }
67 virtual bool hasNext()
override {
return mIt.hasNext(); }
69 virtual uint32_t
size()
override { Q_ASSERT( 0 &&
"not available" );
return 0; }
70 virtual void rewind()
override { Q_ASSERT( 0 &&
"not available" ); }
86 : mLocator( pl ), mBest( m ), mSrcPoint( srcPoint ), mFilter( filter ) {}
88 void visitNode(
const INode& n )
override { Q_UNUSED( n ); }
89 void visitData( std::vector<const IData*>& v )
override { Q_UNUSED( v ); }
95 int vertexIndex, beforeVertex, afterVertex;
101 if ( mFilter && !mFilter->acceptMatch( m ) )
104 if ( !mBest.isValid() || m.
distance() < mBest.distance() )
124 : mLocator( pl ), mBest( m ), mSrcPoint( srcPoint ), mFilter( filter ) {}
126 void visitNode(
const INode& n )
override { Q_UNUSED( n ); }
127 void visitData( std::vector<const IData*>& v )
override { Q_UNUSED( v ); }
140 edgePoints[0] = geom->
vertexAt( afterVertex - 1 );
141 edgePoints[1] = geom->
vertexAt( afterVertex );
144 if ( mFilter && !mFilter->acceptMatch( m ) )
147 if ( !mBest.isValid() || m.
distance() < mBest.distance() )
168 : mLocator( pl ), mList( list ), mGeomPt(
QgsGeometry::fromPoint( origPt ) ) {}
172 void visitNode(
const INode& n )
override { Q_UNUSED( n ); }
173 void visitData( std::vector<const IData*>& v )
override { Q_UNUSED( v ); }
199 static const int INSIDE = 0;
202 static const int BOTTOM = 4;
203 static const int TOP = 8;
210 if ( x < mRect.xMinimum() )
212 else if ( x > mRect.xMaximum() )
214 if ( y < mRect.yMinimum() )
216 else if ( y > mRect.yMaximum() )
224 OutCode outcode0 = computeOutCode( x0, y0 );
225 OutCode outcode1 = computeOutCode( x1, y1 );
230 if ( !( outcode0 | outcode1 ) )
236 else if ( outcode0 & outcode1 )
248 OutCode outcodeOut = outcode0 ? outcode0 : outcode1;
252 if ( outcodeOut & TOP )
254 x = x0 + ( x1 - x0 ) * ( mRect.yMaximum() - y0 ) / ( y1 - y0 );
255 y = mRect.yMaximum();
257 else if ( outcodeOut & BOTTOM )
259 x = x0 + ( x1 - x0 ) * ( mRect.yMinimum() - y0 ) / ( y1 - y0 );
260 y = mRect.yMinimum();
262 else if ( outcodeOut &
RIGHT )
264 y = y0 + ( y1 - y0 ) * ( mRect.xMaximum() - x0 ) / ( x1 - x0 );
265 x = mRect.xMaximum();
267 else if ( outcodeOut &
LEFT )
269 y = y0 + ( y1 - y0 ) * ( mRect.xMinimum() - x0 ) / ( x1 - x0 );
270 x = mRect.xMinimum();
277 if ( outcodeOut == outcode0 )
281 outcode0 = computeOutCode( x0, y0 );
287 outcode1 = computeOutCode( x1, y1 );
302 unsigned char* wkb =
const_cast<unsigned char*
>( geom->
asWkb() );
312 bool hasZValue =
false;
332 double prevx = 0.0, prevy = 0.0;
336 wkbPtr >> thisx >> thisy;
338 wkbPtr +=
sizeof( double );
345 edgePoints[0].
set( prevx, prevy );
346 edgePoints[1].
set( thisx, thisy );
364 for (
int linenr = 0, pointIndex = 0; linenr < nLines; ++linenr )
366 wkbPtr += 1 +
sizeof( int );
370 double prevx = 0.0, prevy = 0.0;
371 for (
int pointnr = 0; pointnr < nPoints; ++pointnr )
374 wkbPtr >> thisx >> thisy;
376 wkbPtr +=
sizeof( double );
383 edgePoints[0].
set( prevx, prevy );
384 edgePoints[1].
set( thisx, thisy );
405 for (
int ringnr = 0, pointIndex = 0; ringnr < nRings; ++ringnr )
410 double prevx = 0.0, prevy = 0.0;
411 for (
int pointnr = 0; pointnr < nPoints; ++pointnr )
414 wkbPtr >> thisx >> thisy;
416 wkbPtr +=
sizeof( double );
423 edgePoints[0].
set( prevx, prevy );
424 edgePoints[1].
set( thisx, thisy );
444 for (
int polynr = 0, pointIndex = 0; polynr < nPolygons; ++polynr )
446 wkbPtr += 1 +
sizeof( int );
449 for (
int ringnr = 0; ringnr < nRings; ++ringnr )
454 double prevx = 0.0, prevy = 0.0;
455 for (
int pointnr = 0; pointnr < nPoints; ++pointnr )
458 wkbPtr >> thisx >> thisy;
460 wkbPtr +=
sizeof( double );
467 edgePoints[0].
set( prevx, prevy );
468 edgePoints[1].
set( thisx, thisy );
496 : mLocator( pl ), mList( lst ), mSrcRect( srcRect ), mFilter( filter ) {}
498 void visitNode(
const INode& n )
override { Q_UNUSED( n ); }
499 void visitData( std::vector<const IData*>& v )
override { Q_UNUSED( v ); }
509 if ( mFilter && !mFilter->acceptMatch( m ) )
536 void getNextEntry(
const IEntry& entry, id_type& nextEntry,
bool& hasNext )
override
538 const INode* n =
dynamic_cast<const INode*
>( &entry );
543 if ( n->getLevel() > 0 )
546 for ( uint32_t cChild = 0; cChild < n->getChildrenCount(); cChild++ )
549 ids.push( n->getChildIdentifier( cChild ) );
555 for ( uint32_t cChild = 0; cChild < n->getChildrenCount(); cChild++ )
563 nextEntry = ids.back(); ids.pop();
577 , mIsEmptyLayer( false )
592 mStorage = StorageManager::createNewMemoryStorageManager();
616 return mRTree != 0 || mIsEmptyLayer;
645 QgsDebugMsg(
QString(
"could not transform bounding box to map, skipping the snap filter (%1)" ).arg( e.
what() ) );
651 int indexedCount = 0;
666 QgsDebugMsg(
QString(
"could not transform geometry to map, skipping the snap for it (%1)" ).arg( e.
what() ) );
672 dataList <<
new RTree::Data( 0, 0, r, f.
id() );
676 if ( maxFeaturesToIndex != -1 && indexedCount > maxFeaturesToIndex )
678 qDeleteAll( dataList );
685 double fillFactor = 0.7;
686 unsigned long indexCapacity = 10;
687 unsigned long leafCapacity = 10;
688 unsigned long dimension = 2;
689 RTree::RTreeVariant variant = RTree::RV_RSTAR;
690 SpatialIndex::id_type indexId;
694 mIsEmptyLayer =
true;
699 mRTree = RTree::createAndBulkLoadNewRTree( RTree::BLM_STR, stream, *mStorage, fillFactor, indexCapacity,
700 leafCapacity, dimension, variant, indexId );
710 mIsEmptyLayer =
false;
712 qDeleteAll( mGeoms );
717 void QgsPointLocator::onFeatureAdded(
QgsFeatureId fid )
741 QgsDebugMsg(
QString(
"could not transform geometry to map, skipping the snap for it (%1)" ).arg( e.
what() ) );
750 mRTree->insertData( 0, 0, r, f.
id() );
756 void QgsPointLocator::onFeatureDeleted(
QgsFeatureId fid )
763 mRTree->deleteData(
rect2region( mGeoms[fid]->boundingBox() ), fid );
771 onFeatureDeleted( fid );
772 onFeatureAdded( fid );
787 QgsRectangle rect( point.
x() - tolerance, point.
y() - tolerance, point.
x() + tolerance, point.
y() + tolerance );
788 mRTree->intersectsWithQuery(
rect2region( rect ), visitor );
805 QgsRectangle rect( point.
x() - tolerance, point.
y() - tolerance, point.
x() + tolerance, point.
y() + tolerance );
806 mRTree->intersectsWithQuery(
rect2region( rect ), visitor );
823 mRTree->intersectsWithQuery(
rect2region( rect ), visitor );
830 QgsRectangle rect( point.
x() - tolerance, point.
y() - tolerance, point.
x() + tolerance, point.
y() + tolerance );
846 mRTree->intersectsWithQuery(
point2point( point ), visitor );
QgsFeatureId id() const
Get the feature ID for this feature.
The class defines interface for querying point location:
QgsPointLocator(QgsVectorLayer *layer, const QgsCoordinateReferenceSystem *destCRS=0, const QgsRectangle *extent=0)
Construct point locator for a layer.
QgsPointLocator_Stream(const QLinkedList< RTree::Data * > &dataList)
Wrapper for iterator of features from vector data provider or vector layer.
A rectangle specified with double values.
static QgsPointLocator::MatchList _geometrySegmentsInRect(QgsGeometry *geom, const QgsRectangle &rect, QgsVectorLayer *vl, QgsFeatureId fid)
void visitData(const IData &d) override
double yMaximum() const
Get the y maximum value (top side of rectangle)
QgsPointLocator_VisitorNearestEdge(QgsPointLocator *pl, QgsPointLocator::Match &m, const QgsPoint &srcPoint, QgsPointLocator::MatchFilter *filter=0)
Match nearestEdge(const QgsPoint &point, double tolerance, MatchFilter *filter=0)
Find nearest edges to the specified point - up to distance specified by tolerance Optional filter may...
QgsPointLocator_VisitorArea(QgsPointLocator *pl, const QgsPoint &origPt, QgsPointLocator::MatchList &list)
constructor
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())
Query the provider for features specified in request.
bool isNull() const
test if the rectangle is null (all coordinates zero or after call to setMinimal()).
~QgsPointLocator_VisitorArea()
QgsRectangle boundingBox() const
Returns the bounding box of this feature.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
Helper class used when traversing the index looking for edges - builds a list of matches.
A geometry is the spatial representation of a feature.
WkbType
Used for symbology operations.
bool rebuildIndex(int maxFeaturesToIndex=-1)
void visitNode(const INode &n) override
static SpatialIndex::Point point2point(const QgsPoint &point)
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.
void visitData(std::vector< const IData * > &v) override
virtual uint32_t size() override
static SpatialIndex::Region rect2region(const QgsRectangle &rect)
virtual bool hasNext() override
double yMinimum() const
Get the y minimum value (bottom side of rectangle)
double xMaximum() const
Get the x maximum value (right side of rectangle)
OutCode computeOutCode(double x, double y)
virtual IData * getNext() override
static const double POINT_LOC_EPSILON
QgsPoint closestVertex(const QgsPoint &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 ...
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)
QList< int > QgsAttributeList
void visitNode(const INode &n) override
QGis::GeometryType geometryType() const
Returns point, line or polygon.
Helper class used when traversing the index with areas - builds a list of matches.
double closestSegmentWithContext(const QgsPoint &point, QgsPoint &minDistPoint, int &afterVertex, double *leftOf=0, double epsilon=DEFAULT_SEGMENT_EPSILON) const
Searches for the closest segment of geometry to the given point.
int remove(const Key &key)
double distance() const
for vertex / edge match units depending on what class returns it (geom.cache: layer units...
void set(double x, double y)
void visitData(const IData &d) override
Helper class used when traversing the index looking for vertices - builds a list of matches...
class QList< Match > MatchList
A class to represent a point.
QgsGeometry * geometry()
Get the geometry object associated with this feature.
~QgsPointLocator_Stream()
Helper class for bulk loading of R-trees.
void visitData(const IData &d) override
void visitNode(const INode &n) override
bool init(int maxFeaturesToIndex=-1)
Prepare the index for queries.
void visitData(std::vector< const IData * > &v) override
Class for storing a coordinate reference system (CRS)
_CohenSutherland(const QgsRectangle &rect)
void getNextEntry(const IEntry &entry, id_type &nextEntry, bool &hasNext) override
const QgsGeometry * constGeometry() const
Gets a const pointer to the geometry object associated with this feature.
void visitData(const IData &d) override
int transform(const QgsCoordinateTransform &ct)
Transform this geometry as described by CoordinateTransform ct.
QgsPointLocator_VisitorNearestVertex(QgsPointLocator *pl, QgsPointLocator::Match &m, const QgsPoint &srcPoint, QgsPointLocator::MatchFilter *filter=0)
const QgsCoordinateReferenceSystem & crs() const
Returns layer's spatial reference system.
Helper class used when traversing the index looking for edges - builds a list of matches.
void visitNode(const INode &n) override
MatchList pointInPolygon(const QgsPoint &point)
find out if the point is in any polygons
bool contains(const Key &key) const
virtual void rewind() override
bool nextFeature(QgsFeature &f)
bool hasIndex() const
Indicate whether the data have been already indexed.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
bool intersects(const QgsRectangle &r) const
Test for intersection with a rectangle (uses GEOS)
Represents a vector layer which manages a vector based data sets.
const unsigned char * asWkb() const
Returns the buffer containing this geometry in WKB format.
double xMinimum() const
Get the x minimum value (left side of rectangle)
Defines a qgis exception class.
QgsPoint vertexAt(int atVertex) const
Returns coordinates of a vertex.
void visitData(std::vector< const IData * > &v) override
QgsPointLocator_VisitorEdgesInRect(QgsPointLocator *pl, QgsPointLocator::MatchList &lst, const QgsRectangle &srcRect, QgsPointLocator::MatchFilter *filter=0)
void visitData(std::vector< const IData * > &v) override
QgsFeatureRequest & setFilterRect(const QgsRectangle &rect)
Set rectangle from which features will be taken.
Match nearestVertex(const QgsPoint &point, double tolerance, MatchFilter *filter=0)
Find nearest vertex to the specified point - up to distance specified by tolerance Optional filter ma...
MatchList edgesInRect(const QgsRectangle &rect, MatchFilter *filter=0)
Find edges within a specified recangle Optional filter may discard unwanted matches.