Skip to content

Commit

Permalink
Merge pull request #36 from OnroerendErfgoed/development_for_brdr_0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dieuska authored Sep 9, 2024
2 parents fd9dbf8 + 071e7d7 commit 2d01caa
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions autocorrectborders.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ def find_python():
Polygon,
from_wkt,
to_wkt,
unary_union
unary_union,
make_valid
)
from shapely.geometry import shape
except (ModuleNotFoundError):
Expand All @@ -114,7 +115,8 @@ def find_python():
Polygon,
from_wkt,
to_wkt,
unary_union
unary_union,
make_valid
)
from shapely.geometry import shape

Expand Down Expand Up @@ -212,7 +214,7 @@ class AutocorrectBordersProcessingAlgorithm(QgsProcessingAlgorithm):
BUFFER_MULTIPLICATION_FACTOR = 1.01
DOWNLOAD_LIMIT = 10000
MAX_REFERENCE_BUFFER = 10
MAX_AREA_FOR_DOWNLOADING_REFERENCE = 1000000
MAX_AREA_FOR_DOWNLOADING_REFERENCE = 2500000
PREDICTIONS = False
UPDATE_TO_ACTUAL = False
SHOW_LOG_INFO = False
Expand Down Expand Up @@ -287,7 +289,7 @@ def geom_shapely_to_qgis(self, geom_shapely):
"""
Method to convert a Shapely-geometry to a QGIS geometry
"""
wkt = to_wkt(geom_shapely, rounding_precision=-1, output_dimension=2)
wkt = to_wkt(make_valid(geom_shapely), rounding_precision=-1, output_dimension=2)
geom_qgis = QgsGeometry.fromWkt(wkt)
return geom_qgis

Expand All @@ -297,7 +299,7 @@ def geom_qgis_to_shapely(self, geom_qgis):
"""
wkt = geom_qgis.asWkt()
geom_shapely = from_wkt(wkt)
return geom_shapely
return make_valid(geom_shapely)

def get_layer_by_name(self, layer_name):
"""
Expand Down Expand Up @@ -619,20 +621,24 @@ def processAlgorithm(self, parameters, context, feedback):
# Load thematic into a shapely_dict:
dict_thematic = {}
features = thematic.getFeatures()
area = 0
for current, feature in enumerate(features):
feature_geom = feature.geometry()
area = area + feature_geom.area()
if feedback.isCanceled():
return {}
id_theme = feature.attribute(self.ID_THEME)
# feedback.pushInfo(str(self.ID_THEME))
# feedback.pushInfo(str(id_theme))

dict_thematic[id_theme] = self.geom_qgis_to_shapely(feature_geom)

area = make_valid(unary_union(list(dict_thematic.values()))).area
feedback.pushInfo("Area of thematic zone: " + str(area))
if self.SELECTED_REFERENCE != 0 and area > self.MAX_AREA_FOR_DOWNLOADING_REFERENCE:
raise QgsProcessingException(
"Please make use of a local REFERENCELAYER from the table of contents, instead of a on-the-fly download (for performance reasons)"
"Unioned area of thematic geometries bigger than threshold (" + str(
self.MAX_AREA_FOR_DOWNLOADING_REFERENCE) + " m²) to use the on-the-fly downloads: " + str(
area) + "(m²) " +
"Please make use of a local REFERENCELAYER (for performance reasons)"
)
feedback.pushInfo("1) BEREKENING - Thematic layer fixed")
feedback.setCurrentStep(1)
Expand Down

0 comments on commit 2d01caa

Please sign in to comment.