Geodjango and spatial indexes

539 views
Skip to first unread message

Max Demars

unread,
May 30, 2013, 9:51:11 AM5/30/13
to geod...@googlegroups.com
Hi,

I have three questions about spatial indexes management in GeoDjango, and I cant find the answers in the doc.

First, I know GeoDjango creates a spatial_index for a geometry field by default when a table is loaded. Is the spatial_index is automatically re-created when the table is updated. For instance, when new features are added to the table through the web application.

Second, If a table has more than one geometry fields, is GeoDjango creating spatial index for each of them or just for the first it encounters?

Third, I would like to create many spatial indexes on the same geometry table based on an id. Is it possible to do it with GeoDjango? For instance, many countries limits are stored in a single table and I would like to create a different spatial index for each country.

Thank you very much, any advices and information would be appreciated.

Max Demars

Trevor Wiens

unread,
May 30, 2013, 10:25:44 AM5/30/13
to geod...@googlegroups.com, geod...@googlegroups.com
Please see here. 


You can specify if you want a spatial index created for each geometry field. 

The updating of an index is managed by PostgreSQL. 

Indexing by ID isn't a spatial index but you can get django to create indexes on other fields. 

If in your example you have features within a country there is to my knowledge no benefit to creating a spatial index on that subset of records nor am I aware of how you would actually do that. 

TSW
------------------
Trevor Wiens
Apropos Information Systems Inc
--
You received this message because you are subscribed to the Google Groups "geodjango" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geodjango+...@googlegroups.com.
To post to this group, send email to geod...@googlegroups.com.
Visit this group at http://groups.google.com/group/geodjango?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Max Demars

unread,
May 30, 2013, 12:02:55 PM5/30/13
to geod...@googlegroups.com

On Thursday, May 30, 2013 10:25:44 AM UTC-4, Trevor wrote:
Please see here. 


You can specify if you want a spatial index created for each geometry field. 

The updating of an index is managed by PostgreSQL. 

Indexing by ID isn't a spatial index but you can get django to create indexes on other fields. 

If in your example you have features within a country there is to my knowledge no benefit to creating a spatial index on that subset of records nor am I aware of how you would actually do that. 

Ok im not sure about that. Imagine a table where you store many different features that dont have the same extent at all. In the same table I could store all the world countries in polygon and also points of interest inside a city. I would have a single id for countries and another id for the points. I do this in order to store a user uploaded shapefiles inside a single generic postgis table. Its the only way I know to store uploaded shapefiles with GeoDjango because the model definition has to be declared before the table creation. I wonder if I could have different spatial indexes on part of postgis table that would be filtered by an id.

Trevor Wiens

unread,
May 30, 2013, 12:25:33 PM5/30/13
to geod...@googlegroups.com
An ID field is not spatial, therefore it is not a spatial index, it is a regular database index. You could use an index on all rows of the ID field which should speed the filtering of records and then use that subset of records' spatial indices for an intersection operation for example. Is that what you mean? The rows may contain spatial information but the spatial index is just on the spatial aspects of the geometry field which is used for bounding box comparisons to speed spatial operations, not record filtering.

Hope that helps

TSW
--
Trevor Wiens
Apropos Information Systems

Max Demars

unread,
May 30, 2013, 1:58:06 PM5/30/13
to geod...@googlegroups.com
Thank you Trevor for your quick answer. Hmmm I think I was not clear enough with my previous post.  If my application display on map only subsets of a table (never the entire table) and those subsets have extents at different scales. Would it be possible to create spatial indexes for those subset, because the whole table spatial indexe would probably not be optimized for the subsets extent and bbox?




--
You received this message because you are subscribed to a topic in the Google Groups "geodjango" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/geodjango/GPDFmOLc5Xc/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to geodjango+...@googlegroups.com.

Trevor Wiens

unread,
May 30, 2013, 2:27:38 PM5/30/13
to geod...@googlegroups.com, geod...@googlegroups.com
Spatial indexing doesn't affect display time. You filter your records by I'd just like regular django or straight SQL. The spatial index is only used for spatial interaction operations like intersections and unions. If you want to query by an area that is what the spatial index is for but if you want to limit which records are checked an ID field will work fine. Just when adding records you need to assign that ID correctly. 

If your concern is speed of display the more important question is are you going to build tiles and send those or are you going to send geojson files. For editing the later is needed but the former is faster for complex shapes or large numbers of features. 

The way a spatial index works is roughly as follows. 

You have a spatial feature that is shaped like and X and you have a second set of features that is a cloud if points. You want to figure out which points intersect with the X. The points are first compared to the bounding box of the X (via the use of the spatial index) and if they overlap then the actual intersection algorithm I run to see if they really intersect. 

In this case you can see that all the points within the bounding box of the X will have to go to the next step to see if they really intersect. 

Now imagine that we use a 10x10 grid to cut the X into pieces. This time when we test to see which points intersect many operations can be avoided by the fact that the X is now comprised of many smaller pieces and thus most of the points won't intersect with its bounding boxes. 

If you have a situations where you need to intersect 100,000's of features with a large feature with a shape dynamic like the big X on way to optimize spatial performance I to store a broken up copy of that feature and do you spatial operations against that. 

I know this is not what you asked but I'm hoping by understanding how spatial indexes work this will help you to understand that it isn't the answer you seek.

TSW
------------------
Trevor Wiens
Apropos Information Systems Inc
Reply all
Reply to author
Forward
0 new messages