Skip to content

Commit

Permalink
Merge pull request #2 from opengisch/merge-by-matrix
Browse files Browse the repository at this point in the history
Dissolve in calculation - Merge right after difference
  • Loading branch information
domi4484 authored Aug 11, 2023
2 parents 1cecd3c + ae16854 commit b38976c
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 137 deletions.
68 changes: 56 additions & 12 deletions pzp_utils/processing/danger_zones.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
)
from qgis.PyQt.QtCore import QVariant

from pzp_utils.processing.merge_by_area import MergeByArea


class DangerZones(QgsProcessingAlgorithm):
INPUT = "INPUT"
Expand Down Expand Up @@ -180,18 +182,6 @@ def prepare_process_source(
feedback=feedback,
is_child_algorithm=True,
)
result = processing.run(
"native:dissolve",
{
"INPUT": result["OUTPUT"],
"FIELD": f"{matrix_field}",
"SEPARATE_DISJOINT": True,
"OUTPUT": "memory:",
},
context=context,
feedback=feedback,
is_child_algorithm=True,
)

if final_layer:
result = processing.run(
Expand All @@ -217,6 +207,60 @@ def prepare_process_source(
is_child_algorithm=True,
)

result = processing.run(
"native:multiparttosingleparts",
{
"INPUT": result["OUTPUT"],
"OUTPUT": "memory:",
},
context=context,
feedback=feedback,
is_child_algorithm=True,
)

result = processing.run(
"native:fixgeometries",
{"INPUT": result["OUTPUT"], "OUTPUT": "memory:"},
context=context,
feedback=feedback,
is_child_algorithm=True,
)

result = processing.run(
"pzp:merge_by_area",
{
"INPUT": result["OUTPUT"],
"MODE": MergeByArea.MODE_BOUNDARY,
"OUTPUT": "memory:",
},
context=context,
feedback=feedback,
is_child_algorithm=True,
)

result = processing.run(
"native:multiparttosingleparts",
{
"INPUT": result["OUTPUT"],
"OUTPUT": "memory:",
},
context=context,
feedback=feedback,
is_child_algorithm=True,
)

# Workaround to re-remove small invalid parts that comes back after multi to single
result = processing.run(
"pzp:remove_by_area",
{
"INPUT": result["OUTPUT"],
"OUTPUT": "memory:",
},
context=context,
feedback=feedback,
is_child_algorithm=True,
)

final_layer = result["OUTPUT"]

result = processing.run(
Expand Down
104 changes: 42 additions & 62 deletions pzp_utils/processing/fix_geometries.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
from qgis import processing
from qgis.core import (
QgsFeature,
QgsFeatureSink,
QgsField,
QgsFields,
QgsProcessing,
QgsProcessingAlgorithm,
QgsProcessingException,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterField,
QgsProcessingParameterMatrix,
)
from qgis.PyQt.QtCore import QVariant

from qgis import processing


class FixGeometries(QgsProcessingAlgorithm):

INPUT = "INPUT"
OUTPUT = "OUTPUT"

Expand Down Expand Up @@ -48,20 +38,13 @@ def initAlgorithm(self, config=None):
)
)

self.addParameter(QgsProcessingParameterFeatureSink(
self.OUTPUT, "Output layer"))
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, "Output layer"))

def processAlgorithm(self, parameters, context, feedback):

min_area_to_keep = 1
delete_holes_area = 100

result = processing.run(
"native:fixgeometries",
{
'INPUT': parameters[self.INPUT],
'OUTPUT': "memory:"
},
{"INPUT": parameters[self.INPUT], "OUTPUT": "memory:"},
context=context,
feedback=feedback,
is_child_algorithm=True,
Expand All @@ -70,9 +53,9 @@ def processAlgorithm(self, parameters, context, feedback):
result = processing.run(
"pzp:merge_by_area",
{
'INPUT': result['OUTPUT'],
'MODE': 2,
'OUTPUT': parameters[self.OUTPUT],
"INPUT": result["OUTPUT"],
"MODE": 2,
"OUTPUT": parameters[self.OUTPUT],
},
context=context,
feedback=feedback,
Expand All @@ -82,12 +65,12 @@ def processAlgorithm(self, parameters, context, feedback):
result = processing.run(
"native:snappointstogrid",
{
'INPUT': result['OUTPUT'],
'HSPACING': 0.001,
'VSPACING': 0.001,
'ZSPACING': 0,
'MSPACING': 0,
'OUTPUT': 'memory:',
"INPUT": result["OUTPUT"],
"HSPACING": 0.001,
"VSPACING": 0.001,
"ZSPACING": 0,
"MSPACING": 0,
"OUTPUT": "memory:",
},
context=context,
feedback=feedback,
Expand All @@ -97,14 +80,14 @@ def processAlgorithm(self, parameters, context, feedback):
result = processing.run(
"native:buffer",
{
'INPUT': result['OUTPUT'],
'DISTANCE': -1e-06,
'SEGMENTS': 5,
'END_CAP_STYLE': 0,
'JOIN_STYLE': 0,
'MITER_LIMIT': 2,
'DISSOLVE': False,
'OUTPUT': 'memory:'
"INPUT": result["OUTPUT"],
"DISTANCE": -1e-06,
"SEGMENTS": 5,
"END_CAP_STYLE": 0,
"JOIN_STYLE": 0,
"MITER_LIMIT": 2,
"DISSOLVE": False,
"OUTPUT": "memory:",
},
context=context,
feedback=feedback,
Expand All @@ -114,12 +97,12 @@ def processAlgorithm(self, parameters, context, feedback):
result = processing.run(
"native:snappointstogrid",
{
'INPUT': result['OUTPUT'],
'HSPACING': 0.001,
'VSPACING': 0.001,
'ZSPACING': 0,
'MSPACING': 0,
'OUTPUT': 'memory:',
"INPUT": result["OUTPUT"],
"HSPACING": 0.001,
"VSPACING": 0.001,
"ZSPACING": 0,
"MSPACING": 0,
"OUTPUT": "memory:",
},
context=context,
feedback=feedback,
Expand All @@ -129,14 +112,14 @@ def processAlgorithm(self, parameters, context, feedback):
result = processing.run(
"native:buffer",
{
'INPUT': result['OUTPUT'],
'DISTANCE': 1e-06,
'SEGMENTS': 5,
'END_CAP_STYLE': 0,
'JOIN_STYLE': 0,
'MITER_LIMIT': 2,
'DISSOLVE': False,
'OUTPUT': 'memory:'
"INPUT": result["OUTPUT"],
"DISTANCE": 1e-06,
"SEGMENTS": 5,
"END_CAP_STYLE": 0,
"JOIN_STYLE": 0,
"MITER_LIMIT": 2,
"DISSOLVE": False,
"OUTPUT": "memory:",
},
context=context,
feedback=feedback,
Expand All @@ -146,12 +129,12 @@ def processAlgorithm(self, parameters, context, feedback):
result = processing.run(
"native:snappointstogrid",
{
'INPUT': result['OUTPUT'],
'HSPACING': 0.001,
'VSPACING': 0.001,
'ZSPACING': 0,
'MSPACING': 0,
'OUTPUT': 'memory:',
"INPUT": result["OUTPUT"],
"HSPACING": 0.001,
"VSPACING": 0.001,
"ZSPACING": 0,
"MSPACING": 0,
"OUTPUT": "memory:",
},
context=context,
feedback=feedback,
Expand All @@ -160,13 +143,10 @@ def processAlgorithm(self, parameters, context, feedback):

result = processing.run(
"native:fixgeometries",
{
'INPUT': result['OUTPUT'],
'OUTPUT': "memory:"
},
{"INPUT": result["OUTPUT"], "OUTPUT": "memory:"},
context=context,
feedback=feedback,
is_child_algorithm=True,
)

return {self.OUTPUT: result['OUTPUT']}
return {self.OUTPUT: result["OUTPUT"]}
Loading

0 comments on commit b38976c

Please sign in to comment.