When adding a layer that includes MultiPolygons, Theto raises an "Object not iterable" error. This is because the MultiPolygon is no longer an iterable list of polygons. Instead of iterating through shp, we can iterate through list(shp.geoms). A working monkey patch is below, where I've also updated shp.geometryType() to shp.geom_type to handle a future warning.
from theto import Theto, coordinate_utils
from shapely.wkt import loads
# Monkey patch for Multi Polygon support in Shapely v2
def polygon_to_nested_list(pol):
ext_x, ext_y = zip(*list(pol.exterior.coords))
x, y = [list(ext_x)], [list(ext_y)]
for interior in pol.interiors:
int_x, int_y = zip(*interior.coords)
x.append(list(int_x))
y.append(list(int_y))
return x, y
def shape_to_nested_list(shp, buffer=0.000001):
if shp.geom_type in ('LineString', 'LinearRing'):
xs, ys = polygon_to_nested_list(shp.buffer(buffer))
xs, ys = [xs], [ys]
elif shp.geom_type == 'Point':
xs, ys = polygon_to_nested_list(shp.buffer(buffer).envelope)
xs, ys = [xs], [ys]
elif shp.geom_type == 'Polygon':
xs, ys = polygon_to_nested_list(shp)
xs, ys = [xs], [ys]
elif shp.geom_type.startswith('Multi') or (shp.geom_type == 'GeometryCollection'):
xs, ys = list(zip(*[shape_to_nested_list(pol) for pol in list(shp.geoms)]))
xs, ys = [x[0] for x in xs], [y[0] for y in ys]
else:
raise NotImplementedError('Unrecognized shapely shape type.')
return xs, ys
coordinate_utils.shape_to_nested_list = shape_to_nested_list
https://github.com/adtech-labs/theto/blob/debd1bac4cfbac6d90693585661a88ef61d420d3/theto/coordinate_utils.py#L311C5-L311C25
When adding a layer that includes MultiPolygons, Theto raises an "Object not iterable" error. This is because the MultiPolygon is no longer an iterable list of polygons. Instead of iterating through
shp, we can iterate throughlist(shp.geoms). A working monkey patch is below, where I've also updatedshp.geometryType()toshp.geom_typeto handle a future warning.