I'm trying to improve a spatial calculation, and I think the most efficient way is to add a spatial index, but I'm not sure how to do it.
- In one hand I have a list of custom objects, with a
geom
attribute load in memory from a PostGIS table. - In the other hand, a
shapely.LineString
.
What I would need to do is get the segments of the line that are inside each polygon.
What I'm doing now is iterate through the Polygons list, and check if the linestring intersects with each polygon, if does, add it to a list of segments with some polygon
for buff in polygons:
intersection = multi_line.intersection(to_shape(buff.geom))
if intersection:
dict_segment = dict(segment_length = intersection.length, pol_name=buff.name)
segments.append(dict_segment)
The main problem that I've seen is that I"m iterating through all polygons list instead of iterate only in a portion to save time.
For this I'd need a spatial index, but as far as I know, I only can set a spatial index to a list of shapes (polygons in this case) but not a list of custom objects, although it has the geom property.
Does anybody know how I could set the index to this object list?