Skip to content

Commit

Permalink
primeiro commit
Browse files Browse the repository at this point in the history
  • Loading branch information
phborba committed Nov 28, 2023
1 parent e16782c commit d6cb222
Show file tree
Hide file tree
Showing 2 changed files with 290 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
# -*- coding: utf-8 -*-
"""
/***************************************************************************
DsgTools
A QGIS plugin
Brazilian Army Cartographic Production Tools
-------------------
begin : 2023-11-27
git sha : $Format:%H$
copyright : (C) 2023 by Philipe Borba - Cartographic Engineer @ Brazilian Army
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""

from collections import defaultdict
from typing import Dict, Set, Tuple
from PyQt5.QtCore import QCoreApplication
from DsgTools.core.GeometricTools import graphHandler
from qgis.PyQt.QtCore import QByteArray
from qgis.core import (
Qgis,
QgsProcessing,
QgsProcessingException,
QgsProcessingMultiStepFeedback,
QgsProcessingParameterEnum,
QgsProcessingParameterVectorLayer,
QgsWkbTypes,
QgsFeedback,
QgsProcessingContext,
QgsGeometry,
QgsFeature,
QgsVectorLayer,
QgsProcessingParameterNumber,
QgsProcessingParameterBoolean,
)

from ...algRunner import AlgRunner
from ..ValidationAlgs.validationAlgorithm import ValidationAlgorithm


class GeneralizeNetworkEdgesWithLengthAlgorithm(ValidationAlgorithm):
NETWORK_LAYER = "NETWORK_LAYER"
MIN_LENGTH = "MIN_LENGTH"
GEOGRAPHIC_BOUNDS_LAYER = "GEOGRAPHIC_BOUNDS_LAYER"
WATER_BODIES_LAYER = "WATER_BODIES_LAYER"
METHOD = "METHOD"

def initAlgorithm(self, config):
"""
Parameter setting.
"""
self.addParameter(
QgsProcessingParameterVectorLayer(
self.NETWORK_LAYER,
self.tr("Network layer"),
[QgsProcessing.TypeVectorLine],
)
)
self.addParameter(
QgsProcessingParameterNumber(
self.MIN_LENGTH,
self.tr("Minimum size"),
minValue=0,
type=QgsProcessingParameterNumber.Double,
defaultValue=0.001,
)
)
self.addParameter(
QgsProcessingParameterVectorLayer(
self.WATER_BODIES_LAYER,
self.tr("Water bodies layer"),
[QgsProcessing.TypeVectorPolygon],
optional=True,
)
)
self.addParameter(
QgsProcessingParameterVectorLayer(
self.GEOGRAPHIC_BOUNDS_LAYER,
self.tr("Reference layer"),
[QgsProcessing.TypeVectorPolygon],
optional=True,
)
)
self.selectionIdDict = {
1: Qgis.SelectBehavior.SetSelection,
2: Qgis.SelectBehavior.AddToSelection,
3: Qgis.SelectBehavior.IntersectSelection,
4: Qgis.SelectBehavior.RemoveFromSelection,
}
self.method = [
self.tr("Remove features from input layer"),
self.tr("Modify current selection by creating new selection"),
self.tr("Modify current selection by adding to current selection"),
self.tr("Modify current selection by selecting within current selection"),
self.tr("Modify current selection by removing from current selection"),
]

self.addParameter(
QgsProcessingParameterEnum(
self.METHOD, self.tr("Method"), options=self.method, defaultValue=0
)
)

def processAlgorithm(self, parameters, context, feedback):
"""
Here is where the processing itself takes place.
"""
try:
import networkx as nx
except ImportError:
raise QgsProcessingException(
self.tr(
"This algorithm requires the Python networkx library. Please install this library and try again."
)
)
# get the network handler
self.algRunner = AlgRunner()
networkLayer = self.parameterAsLayer(parameters, self.NETWORK_LAYER, context)
filterExpression = self.parameterAsExpression(
parameters, self.FILTER_EXPRESSION, context
)
if filterExpression == "":
filterExpression = None
oceanLayer = self.parameterAsLayer(parameters, self.WATER_BODIES_LAYER, context)
waterBodyWithFlowLayer = self.parameterAsLayer(
parameters, self.WATER_BODY_WITH_FLOW_LAYER, context
)
waterSinkLayer = self.parameterAsLayer(parameters, self.SINK_LAYER, context)
geographicBoundsLayer = self.parameterAsLayer(
parameters, self.GEOGRAPHIC_BOUNDS_LAYER, context
)
nSteps = (
15
+ (runFlowCheck is True)
+ (runLoopCheck is True)
+ (waterBodyWithFlowLayer is not None)
)
multiStepFeedback = QgsProcessingMultiStepFeedback(nSteps, feedback)
currentStep = 0
multiStepFeedback.setCurrentStep(currentStep)
multiStepFeedback.setProgressText(self.tr("Building aux structures"))
localCache, nodesLayer = graphHandler.buildAuxLayersPriorGraphBuilding(
networkLayer=networkLayer,
context=context,
geographicBoundsLayer=geographicBoundsLayer,
feedback=multiStepFeedback,
)
currentStep += 1
multiStepFeedback.setCurrentStep(currentStep)
multiStepFeedback.setProgressText(self.tr("Building graph aux structures"))
(
nodeDict,
nodeIdDict,
edgeDict,
hashDict,
networkBidirectionalGraph,
nodeLayerIdDict,
) = graphHandler.buildAuxStructures(
nx,
nodesLayer=nodesLayer,
edgesLayer=localCache,
feedback=multiStepFeedback,
useWkt=False,
computeNodeLayerIdDict=True,
addEdgeLength=True,
)
currentStep += 1
multiStepFeedback.setCurrentStep(currentStep)
return {}

def name(self):
"""
Returns the algorithm name, used for identifying the algorithm. This
string should be fixed for the algorithm, and must not be localised.
The name should be unique within each provider. Names should contain
lowercase alphanumeric characters only and no spaces or other
formatting characters.
"""
return "generalizenetworkedgeswithlengthalgorithm"

def displayName(self):
"""
Returns the translated algorithm name, which should be used for any
user-visible display of the algorithm name.
"""
return self.tr("Generalize Network Edges With Length Algorithm")

def group(self):
"""
Returns the name of the group this algorithm belongs to. This string
should be localised.
"""
return self.tr("Generalization Algorithms")

def groupId(self):
"""
Returns the unique ID of the group this algorithm belongs to. This
string should be fixed for the algorithm, and must not be localised.
The group id should be unique within each provider. Group id should
contain lowercase alphanumeric characters only and no spaces or other
formatting characters.
"""
return "DSGTools - Generalization Algorithms"

def tr(self, string):
return QCoreApplication.translate(
"GeneralizeNetworkEdgesWithLengthAlgorithm", string
)

def createInstance(self):
return GeneralizeNetworkEdgesWithLengthAlgorithm()
70 changes: 70 additions & 0 deletions DsgTools/core/GeometricTools/graphHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@
QgsProcessingMultiStepFeedback,
QgsVectorLayer,
QgsFeedback,
QgsProcessingContext,
)

from DsgTools.core.DSGToolsProcessingAlgs.algRunner import AlgRunner


def fetch_connected_nodes(
G, node: int, max_degree: int, feedback: Optional[QgsFeedback] = None
Expand Down Expand Up @@ -893,3 +896,70 @@ def identify_unmerged_edges_on_graph(
continue
outputIdSet.add(nodeId)
return outputIdSet


def buildAuxLayersPriorGraphBuilding(
networkLayer, context=None, geographicBoundsLayer=None, feedback=None
):
algRunner = AlgRunner()
nSteps = 6 if geographicBoundsLayer is not None else 4
multiStepFeedback = (
QgsProcessingMultiStepFeedback(nSteps, feedback)
if feedback is not None
else None
)
context = QgsProcessingContext() if context is None else context
currentStep = 0
if multiStepFeedback is not None:
multiStepFeedback.setCurrentStep(currentStep)
localCache = algRunner.runCreateFieldWithExpression(
inputLyr=networkLayer,
expression="$id",
fieldName="featid",
fieldType=1,
context=context,
feedback=multiStepFeedback,
)
currentStep += 1
if geographicBoundsLayer is not None:
if multiStepFeedback is not None:
multiStepFeedback.setCurrentStep(currentStep)
algRunner.runCreateSpatialIndex(
inputLyr=localCache, context=context, feedback=multiStepFeedback
)
currentStep += 1
if multiStepFeedback is not None:
multiStepFeedback.setCurrentStep(currentStep)
localCache = algRunner.runExtractByLocation(
inputLyr=localCache,
intersectLyr=geographicBoundsLayer,
context=context,
feedback=multiStepFeedback,
)
currentStep += 1
if multiStepFeedback is not None:
multiStepFeedback.setCurrentStep(currentStep)
algRunner.runCreateSpatialIndex(
inputLyr=localCache, context=context, feedback=multiStepFeedback
)
currentStep += 1
if multiStepFeedback is not None:
multiStepFeedback.setCurrentStep(currentStep)
nodesLayer = algRunner.runExtractSpecificVertices(
inputLyr=localCache,
vertices="0,-1",
context=context,
feedback=multiStepFeedback,
)
currentStep += 1
if multiStepFeedback is not None:
multiStepFeedback.setCurrentStep(currentStep)
nodesLayer = algRunner.runCreateFieldWithExpression(
inputLyr=nodesLayer,
expression="$id",
fieldName="nfeatid",
fieldType=1,
context=context,
feedback=multiStepFeedback,
)
return localCache, nodesLayer

0 comments on commit d6cb222

Please sign in to comment.