21 #include <spatialindex/SpatialIndex.h> 23 #include <QLinkedListIterator> 34 return Point( plow, 2 );
40 double pLow[2], pHigh[2];
45 return SpatialIndex::Region( pLow, pHigh, 2 );
65 virtual IData*
getNext()
override {
return mIt.next(); }
66 virtual bool hasNext()
override {
return mIt.hasNext(); }
68 virtual uint32_t
size()
override { Q_ASSERT( 0 &&
"not available" );
return 0; }
69 virtual void rewind()
override { Q_ASSERT( 0 &&
"not available" ); }
72 QLinkedList<RTree::Data*> mDataList;
73 QLinkedListIterator<RTree::Data*> mIt;
85 : mLocator( pl ), mBest( m ), mSrcPoint( srcPoint ), mFilter( filter ) {}
87 void visitNode(
const INode& n )
override { Q_UNUSED( n ); }
88 void visitData( std::vector<const IData*>& v )
override { Q_UNUSED( v ); }
94 int vertexIndex, beforeVertex, afterVertex;
100 if ( mFilter && !mFilter->acceptMatch( m ) )
103 if ( !mBest.isValid() || m.
distance() < mBest.distance() )
123 : mLocator( pl ), mBest( m ), mSrcPoint( srcPoint ), mFilter( filter ) {}
125 void visitNode(
const INode& n )
override { Q_UNUSED( n ); }
126 void visitData( std::vector<const IData*>& v )
override { Q_UNUSED( v ); }
139 edgePoints[0] = geom->
vertexAt( afterVertex - 1 );
140 edgePoints[1] = geom->
vertexAt( afterVertex );
143 if ( mFilter && !mFilter->acceptMatch( m ) )
146 if ( !mBest.isValid() || m.
distance() < mBest.distance() )
167 : mLocator( pl ), mList( list ), mGeomPt(
QgsGeometry::fromPoint( origPt ) ) {}
171 void visitNode(
const INode& n )
override { Q_UNUSED( n ); }
172 void visitData( std::vector<const IData*>& v )
override { Q_UNUSED( v ); }
198 static const int INSIDE = 0;
201 static const int BOTTOM = 4;
202 static const int TOP = 8;
208 OutCode code = INSIDE;
223 OutCode outcode0 = computeOutCode( x0, y0 );
224 OutCode outcode1 = computeOutCode( x1, y1 );
229 if ( !( outcode0 | outcode1 ) )
235 else if ( outcode0 & outcode1 )
247 OutCode outcodeOut = outcode0 ? outcode0 : outcode1;
251 if ( outcodeOut & TOP )
253 x = x0 + ( x1 - x0 ) * ( mRect.
yMaximum() - y0 ) / ( y1 - y0 );
256 else if ( outcodeOut & BOTTOM )
258 x = x0 + ( x1 - x0 ) * ( mRect.
yMinimum() - y0 ) / ( y1 - y0 );
261 else if ( outcodeOut & RIGHT )
263 y = y0 + ( y1 - y0 ) * ( mRect.
xMaximum() - x0 ) / ( x1 - x0 );
266 else if ( outcodeOut & LEFT )
268 y = y0 + ( y1 - y0 ) * ( mRect.
xMinimum() - x0 ) / ( x1 - x0 );
276 if ( outcodeOut == outcode0 )
280 outcode0 = computeOutCode( x0, y0 );
286 outcode1 = computeOutCode( x1, y1 );
301 unsigned char* wkb =
const_cast<unsigned char*
>( geom->
asWkb() );
311 bool hasZValue =
false;
331 double prevx = 0.0, prevy = 0.0;
335 wkbPtr >> thisx >> thisy;
337 wkbPtr +=
sizeof( double );
344 edgePoints[0].
set( prevx, prevy );
345 edgePoints[1].
set( thisx, thisy );
363 for (
int linenr = 0, pointIndex = 0; linenr < nLines; ++linenr )
365 wkbPtr += 1 +
sizeof( int );
369 double prevx = 0.0, prevy = 0.0;
370 for (
int pointnr = 0; pointnr < nPoints; ++pointnr )
373 wkbPtr >> thisx >> thisy;
375 wkbPtr +=
sizeof( double );
382 edgePoints[0].
set( prevx, prevy );
383 edgePoints[1].
set( thisx, thisy );
404 for (
int ringnr = 0, pointIndex = 0; ringnr < nRings; ++ringnr )
409 double prevx = 0.0, prevy = 0.0;
410 for (
int pointnr = 0; pointnr < nPoints; ++pointnr )
413 wkbPtr >> thisx >> thisy;
415 wkbPtr +=
sizeof( double );
422 edgePoints[0].
set( prevx, prevy );
423 edgePoints[1].
set( thisx, thisy );
443 for (
int polynr = 0, pointIndex = 0; polynr < nPolygons; ++polynr )
445 wkbPtr += 1 +
sizeof( int );
448 for (
int ringnr = 0; ringnr < nRings; ++ringnr )
453 double prevx = 0.0, prevy = 0.0;
454 for (
int pointnr = 0; pointnr < nPoints; ++pointnr )
457 wkbPtr >> thisx >> thisy;
459 wkbPtr +=
sizeof( double );
466 edgePoints[0].
set( prevx, prevy );
467 edgePoints[1].
set( thisx, thisy );
495 : mLocator( pl ), mList( lst ), mSrcRect( srcRect ), mFilter( filter ) {}
497 void visitNode(
const INode& n )
override { Q_UNUSED( n ); }
498 void visitData( std::vector<const IData*>& v )
override { Q_UNUSED( v ); }
508 if ( mFilter && !mFilter->acceptMatch( m ) )
535 void getNextEntry(
const IEntry& entry, id_type& nextEntry,
bool& hasNext )
override 537 const INode* n =
dynamic_cast<const INode*
>( &entry );
541 QgsDebugMsg( QString(
"NODE: %1" ).arg( n->getIdentifier() ) );
542 if ( n->getLevel() > 0 )
545 for ( uint32_t cChild = 0; cChild < n->getChildrenCount(); cChild++ )
547 QgsDebugMsg( QString(
"- CH: %1" ).arg( n->getChildIdentifier( cChild ) ) );
548 ids.push( n->getChildIdentifier( cChild ) );
554 for ( uint32_t cChild = 0; cChild < n->getChildrenCount(); cChild++ )
556 QgsDebugMsg( QString(
"- L: %1" ).arg( n->getChildIdentifier( cChild ) ) );
562 nextEntry = ids.back(); ids.pop();
576 , mIsEmptyLayer( false )
591 mStorage = StorageManager::createNewMemoryStorageManager();
615 return mRTree != 0 || mIsEmptyLayer;
624 QLinkedList<RTree::Data*> dataList;
644 QgsDebugMsg( QString(
"could not transform bounding box to map, skipping the snap filter (%1)" ).arg( e.
what() ) );
650 int indexedCount = 0;
665 QgsDebugMsg( QString(
"could not transform geometry to map, skipping the snap for it (%1)" ).arg( e.
what() ) );
671 dataList <<
new RTree::Data( 0, 0, r, f.
id() );
675 if ( maxFeaturesToIndex != -1 && indexedCount > maxFeaturesToIndex )
677 qDeleteAll( dataList );
684 double fillFactor = 0.7;
685 unsigned long indexCapacity = 10;
686 unsigned long leafCapacity = 10;
687 unsigned long dimension = 2;
688 RTree::RTreeVariant variant = RTree::RV_RSTAR;
689 SpatialIndex::id_type indexId;
691 if ( dataList.isEmpty() )
693 mIsEmptyLayer =
true;
698 mRTree = RTree::createAndBulkLoadNewRTree( RTree::BLM_STR, stream, *mStorage, fillFactor, indexCapacity,
699 leafCapacity, dimension, variant, indexId );
709 mIsEmptyLayer =
false;
711 qDeleteAll( mGeoms );
716 void QgsPointLocator::onFeatureAdded(
QgsFeatureId fid )
740 QgsDebugMsg( QString(
"could not transform geometry to map, skipping the snap for it (%1)" ).arg( e.
what() ) );
749 mRTree->insertData( 0, 0, r, f.
id() );
755 void QgsPointLocator::onFeatureDeleted(
QgsFeatureId fid )
760 if ( mGeoms.contains( fid ) )
762 mRTree->deleteData(
rect2region( mGeoms[fid]->boundingBox() ), fid );
763 mGeoms.remove( fid );
770 onFeatureDeleted( fid );
771 onFeatureAdded( fid );
786 QgsRectangle rect( point.
x() - tolerance, point.
y() - tolerance, point.
x() + tolerance, point.
y() + tolerance );
787 mRTree->intersectsWithQuery(
rect2region( rect ), visitor );
804 QgsRectangle rect( point.
x() - tolerance, point.
y() - tolerance, point.
x() + tolerance, point.
y() + tolerance );
805 mRTree->intersectsWithQuery(
rect2region( rect ), visitor );
822 mRTree->intersectsWithQuery(
rect2region( rect ), visitor );
829 QgsRectangle rect( point.
x() - tolerance, point.
y() - tolerance, point.
x() + tolerance, point.
y() + tolerance );
845 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.
QgsGeometry * geometry() const
Get the geometry object associated with this feature.
double closestSegmentWithContext(const QgsPoint &point, QgsPoint &minDistPoint, int &afterVertex, double *leftOf=0, double epsilon=DEFAULT_SEGMENT_EPSILON)
Searches for the closest segment of geometry to the given point.
bool isNull() const
test if the rectangle is null (all coordinates zero or after call to setMinimal()).
~QgsPointLocator_VisitorArea()
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.
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.
QgsPoint closestVertex(const QgsPoint &point, int &atVertex, int &beforeVertex, int &afterVertex, double &sqrDist)
Returns the vertex closest to the given point, the corresponding vertex index, squared distance snap ...
QgsPoint vertexAt(int atVertex)
Returns coordinates of a vertex.
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
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 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.
~QgsPointLocator_Stream()
QgsRectangle boundingBox()
Returns the bounding box of this feature.
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
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
virtual void rewind() override
bool nextFeature(QgsFeature &f)
bool hasIndex() const
Indicate whether the data have been already indexed.
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.
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.