[Qgis-developer] Use of Spatial Index
Martin Dobias
wonder.sk at gmail.com
Thu Jul 28 07:17:43 EDT 2011
Hi
On Wed, Jul 27, 2011 at 8:56 PM, maaza mekuria <sailmcm at yahoo.com> wrote:
> I am just wondering if one could use the following in a spatial index created from the layer provider
>
> QgsVectorLayer* cvertexLayer = cVertexLayer(txtStream);
> QgsVectorDataProvider* cvectorProvider = cvertexLayer->dataProvider();
> bool blnSpatialIdx = cvectorProvider->createSpatialIndex();
createSpatialIndex() method is supported only by memory provider and
by OGR provider for shapefiles. With shapefiles it means that a .qix
file is created with a quadtree structure. Memory provider builds
R-tree (using QgsSpatialIndex class).
> After creating the spatial index file how can I access it in code?
> can I use it in anyway to perform spatial filters for selection purposes?
>
> QgsSpatialIndex mSpatialIndex = <dynamic_Cast> cvectorProvider; // may be cvectorLayer;
>
> or may be even directly perform this
>
> QgsSpatialIndex mSpatialIndex = cvectorProvider->createSpatialIndex();
>
> // and then do something like this
> mSpatialIndex.intersects( rect );
No. You cannot access the provider's spatial index. Some providers do
not support spatial indexing, others (optionally) use it, but it is
invisible for the client. When you call provider.select( ... ) and
specify a rectangle, you will get only features that are inside this
rectangle - and if a spatial index is available then it will be used
for faster access.
> DO I have to create my own in memory spatial index to perform the above
>
> what do you think?
If you build an application where you need to do lots of spatial
queries it makes sense to load all the features into a spatial index
(QgsSpatialIndex) and do the intersect / nearest neighbor queries
using it.
Martin
More information about the Qgis-developer
mailing list