Commit 2e3d6255 authored by Daniel Scheffler's avatar Daniel Scheffler
Browse files

Only run poly.buffer(0) on invalid polygons.

parent 20640da2
......@@ -147,19 +147,28 @@ def raster2polygon(array: np.ndarray,
# convert JSON string to dict
gjs = json.loads(content_str)
gc = GeometryCollection([shape(f["geometry"]).buffer(0)
for f in gjs['features'] if f['properties']['DN'] == str(DN2extract)])
# convert GeometryCollection into MultiPolygon
def get_valid_polys(val: Union[Polygon, MultiPolygon, GeometryCollection]):
if isinstance(val, Polygon):
val = val if val.is_valid else val.buffer(0)
if isinstance(val, Polygon):
return val
for g in val.geoms:
return get_valid_polys(g)
# extract polygons from GeoJSON dict
polys = []
for i in gc.geoms:
if isinstance(i, Polygon):
polys.append(i)
else:
polys.extend(list(i.geoms))
for f in gjs['features']:
if f['properties']['DN'] == str(DN2extract):
geom = shape(f["geometry"])
geom = get_valid_polys(geom)
if isinstance(geom, list):
polys.extend(geom)
else:
polys.append(geom)
# drop polygons with an area below npx
if min_npx > 1:
if min_npx:
area_1px = gt[1] * abs(gt[5])
area_min = min_npx * area_1px
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment