0

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?

0

I've found the solution. The best way to solve this is working with the shapely.Polygon list, and add your own attributes.

list_polygons = list()
for pol in custom_objects_list:
    P=to_shape(pol.geom)
    P.name = pol.name
    P.other = pol.other
    list_polygons.append(P)

Now you have a list of shapely objects, which you can add the index.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.