[Qgis-developer] Use of Spatial Index

maaza mekuria sailmcm at yahoo.com
Thu Jul 28 14:36:17 EDT 2011


Thank you, Martin for your explanation. I ahve one more followup question.

Can the spatial index be composed of features other than points? I do not see any restriction in the documentation. Can I use features such as lines and polylines in an in-memory spatial index and use these as one uses points? 

Thank you,

Maaza

--- On Thu, 7/28/11, Martin Dobias <wonder.sk at gmail.com> wrote:

> From: Martin Dobias <wonder.sk at gmail.com>
> Subject: Re: [Qgis-developer] Use of Spatial Index
> To: "maaza mekuria" <sailmcm at yahoo.com>
> Cc: qgis-developer at lists.osgeo.org
> Date: Thursday, July 28, 2011, 7:17 AM
> 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