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?

Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Sep 10, 2020 at 11:02

1 Answer 1

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.

answered Sep 18, 2020 at 14:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.