pypsa-eur/scripts/helper.py
Tom Brown 8538233bba Fix bug due to invalid cluster bus region geometries
These geometries are apparently invalid due to self-crossing or
self-touching polygons.

The geometries are created by pypsa-eur/scripts/cluster_network.py but
appear to be valid before being written to file.

They are only valid after being read back in from file.

This seems to indicate some numerical issue relating to file reading
and writing.

Now the geometries are cleaned after being read in.
2020-05-07 14:45:14 +02:00

16 lines
790 B
Python

import logging
logger = logging.getLogger(__name__)
#https://stackoverflow.com/questions/20833344/fix-invalid-polygon-in-shapely
#https://stackoverflow.com/questions/13062334/polygon-intersection-error-in-shapely-shapely-geos-topologicalerror-the-opera
#https://shapely.readthedocs.io/en/latest/manual.html#object.buffer
def clean_invalid_geometries(geometries):
"""Fix self-touching or self-crossing polygons; these seem to appear
due to numerical problems from writing and reading, since the geometries
are valid before being written in pypsa-eur/scripts/cluster_network.py"""
for i,p in geometries.items():
if not p.is_valid:
logger.warning(f'Clustered region {i} had an invalid geometry, fixing using zero buffer.')
geometries[i] = p.buffer(0)