Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

corrige dangle #809

Merged
merged 3 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,6 @@ def computePoints(
geographicBoundsLyr,
areaWithoutInformationLyr,
waterBodiesLyr,
contourBufferLyr,
context,
algRunner,
feedback=multiStepFeedback,
Expand All @@ -608,14 +607,26 @@ def computePoints(
multiStepFeedback.pushInfo(
self.tr("Getting max min feats and building exclusion polygons...")
)
minMaxFeats = self.getMinMaxFeatures(
maxFeats = self.getMaxFeatures(
fields,
npRaster,
transform,
distance=localBufferDistance,
maskLyr=maskLyr,
feedback=multiStepFeedback,
)
minMaxFeats = self.getMinFeatures(
maxFeats,
fields,
npRaster,
transform,
distance=localBufferDistance,
maskLyr=maskLyr,
feedback=multiStepFeedback,
)
maskLyr = AlgRunner().runMergeVectorLayers(
[maskLyr, contourBufferLyr], context
)
elevationPointsLayer = layerHandler.createMemoryLayerWithFeatures(
featList=minMaxFeats,
fields=fields,
Expand Down Expand Up @@ -1070,7 +1081,6 @@ def readAndMaskRaster(
geographicBoundsLyr,
areaWithoutInformationLyr,
waterBodiesLyr,
contourBufferLyr,
context,
algRunner,
feedback,
Expand All @@ -1095,11 +1105,11 @@ def readAndMaskRaster(
layerList = list(
filter(
lambda x: x is not None,
[areaWithoutInformationLyr, waterBodiesLyr, contourBufferLyr],
[areaWithoutInformationLyr, waterBodiesLyr],
)
)
if layerList == []:
return npRaster, npRaster, transform, contourBufferLyr
return npRaster, npRaster, transform, None
maskLyr = (
AlgRunner().runMergeVectorLayers(
layerList, context, feedback=multiStepFeedback
Expand All @@ -1115,7 +1125,7 @@ def readAndMaskRaster(
)

if maskLyr.featureCount() == 0:
return npRaster
return npRaster, npRaster, transform, None
if multiStepFeedback is not None:
currentStep += 1
multiStepFeedback.setCurrentStep(currentStep)
Expand Down Expand Up @@ -1591,21 +1601,15 @@ def getElevationPointsFromLineIntersections(
context=context,
feedback=multiStepFeedback,
)

def getMinMaxFeatures(
def getMaxFeatures(
self, fields, npRaster, transform, distance, maskLyr, feedback=None
):
featSet = set()
maxCoordinatesArray = rasterHandler.getMaxCoordinatesFromNpArray(npRaster)
multiStepFeedback = (
QgsProcessingMultiStepFeedback(3, feedback)
if feedback is not None
else None
)
npRasterCopy = np.array(npRaster)
if multiStepFeedback is not None:
multiStepFeedback.setCurrentStep(1)
multiStepFeedback.pushInfo(
if feedback is not None:
feedback.pushInfo(
self.tr("Creating max feature list from pixel coordinates array...")
)
maxFeatList = (
Expand All @@ -1630,16 +1634,17 @@ def getMinMaxFeatures(
filteredFeatureList = self.filterFeaturesByDistanceAndExclusionLayer(
candidatesPointLyr=maxFeatLyr,
exclusionLyr=maskLyr,
distance=distance,
distance=distance/10,
context=QgsProcessingContext(),
feedback=None,
)
if filteredFeatureList != []:
featSet |= self.filterFeaturesByBuffer(
filteredFeatureList, distance, cotaMaisAlta=True
filteredFeatureList, distance/10, cotaMaisAlta=True
)
break
npRasterCopy[npRasterCopy == cotaMax] = np.nan
if len(npRasterCopy[npRasterCopy == cotaMax]) > 0:
npRasterCopy[npRasterCopy == cotaMax] = np.nan
maxCoordinatesArray = rasterHandler.getMaxCoordinatesFromNpArray(
npRasterCopy
)
Expand All @@ -1656,19 +1661,20 @@ def getMinMaxFeatures(
)
)
cotaMax -= 1
if multiStepFeedback is not None and multiStepFeedback.isCanceled():
if feedback is not None and feedback.isCanceled():
break
if multiStepFeedback is not None:
multiStepFeedback.setCurrentStep(2)
multiStepFeedback.pushInfo(
self.tr("Getting min coordinates from numpy array...")
)
return list(featSet)

def getMinFeatures(
self, maxFeats, fields, npRaster, transform, distance, maskLyr, feedback=None
):
featSet = set(maxFeats)
npRasterCopy = np.array(npRaster)
minCoordinatesArray = rasterHandler.getMinCoordinatesFromNpArray(npRasterCopy)
if minCoordinatesArray == []:
if len(minCoordinatesArray) == 0:
return list(featSet)
if multiStepFeedback is not None:
multiStepFeedback.setCurrentStep(3)
multiStepFeedback.pushInfo(
if feedback is not None:
feedback.pushInfo(
self.tr("Creating min feature list from pixel coordinates array...")
)
minFeatList = (
Expand All @@ -1693,7 +1699,7 @@ def getMinMaxFeatures(
filteredFeatureList = self.filterFeaturesByDistanceAndExclusionLayer(
candidatesPointLyr=minFeatLyr,
exclusionLyr=maskLyr,
distance=distance,
distance=distance / 10,
context=QgsProcessingContext(),
feedback=None,
)
Expand All @@ -1704,7 +1710,7 @@ def getMinMaxFeatures(
minCoordinatesArray = rasterHandler.getMinCoordinatesFromNpArray(
npRasterCopy
)
if minCoordinatesArray == []:
if len(minCoordinatesArray) == 0:
return list(featSet)
minFeatList = (
rasterHandler.createFeatureListWithPixelValuesFromPixelCoordinatesArray(
Expand All @@ -1717,7 +1723,7 @@ def getMinMaxFeatures(
)
)
cota += 1
if multiStepFeedback is not None and multiStepFeedback.isCanceled():
if feedback is not None and feedback.isCanceled():
break
return list(featSet)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def populateAuxStructure(self, lines, feedback):
break
self.id_to_feature[currentFeature["AUTO"]] = currentFeature
self.ids_in_stack.add(currentFeature["AUTO"])
feedback.setCurrentStep(current * stepSize)
feedback.setProgress(current * stepSize)

def aggregate(self, featureId, feedback):
stack = [featureId]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,10 @@ def processAlgorithm(self, parameters, context, feedback):
for current, point in enumerate(dangleSet):
if multiStepFeedback.isCanceled():
break
geom = QgsGeometry()
geom.fromWkb(point)
self.flagFeature(
QgsGeometry.fromPointXY(point),
geom,
self.tr("Dangle on {0}").format(inputLyr.name()),
)
multiStepFeedback.setProgress(current * currentTotal)
Expand Down Expand Up @@ -315,7 +317,8 @@ def buildInitialAndEndPointDict(
id = feat["AUTO"]
pointList = geom.asMultiPoint() if geom.isMultipart() else [geom.asPoint()]
for point in pointList:
pointDict[point].add(id)
pointGeom = QgsGeometry.fromPointXY(point)
pointDict[pointGeom.asWkb()].add(id)
multiStepFeedback.setProgress(current * step)
return pointDict

Expand Down Expand Up @@ -386,7 +389,9 @@ def getDanglesOnInputLayerFeatures(
futures = set()

def evaluate(point) -> Union[QgsPointXY, None]:
qgisPoint = QgsGeometry.fromPointXY(point)
# qgisPoint = QgsGeometry.fromPointXY(point)
qgisPoint = QgsGeometry()
qgisPoint.fromWkb(point)
geomEngine = QgsGeometry.createGeometryEngine(qgisPoint.constGet())
geomEngine.prepareGeometry()
buffer = qgisPoint.buffer(searchRadius, -1)
Expand All @@ -396,7 +401,7 @@ def evaluate(point) -> Union[QgsPointXY, None]:
bufferCount, intersectCount = 0, 0
point_relationship_lambda = (
lambda x: geomEngine.intersects(x.constGet())
or qgisPoint.distance(x) < 1e-8
or qgisPoint.distance(x) < 1e-16
if ignoreDanglesOnUnsegmentedLines
else geomEngine.touches(x.constGet())
)
Expand Down Expand Up @@ -476,7 +481,9 @@ def getDanglesWithFilterLayers(

def evaluate(point: QgsPointXY) -> dict:
candidateCount, bufferCount = 0, 0
qgisPoint = QgsGeometry.fromPointXY(point)
qgisPoint = QgsGeometry()
qgisPoint.fromWkb(point)
# qgisPoint = QgsGeometry.fromPointXY(point)
# search radius to narrow down candidates
buffer = qgisPoint.buffer(searchRadius, -1)
bufferBB = buffer.boundingBox()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def compute(localGeographicBoundsLyr):
spotElevationLyr=localElevationPointsLyr,
spotElevationFieldName=elevationPointHeightFieldName,
)
return terrainModel.validate()
return terrainModel.validate(context=localContext)

multiStepFeedback.setCurrentStep(currentStep)
nRegions = len(geographicBoundaryLayerList)
Expand Down
9 changes: 7 additions & 2 deletions DsgTools/core/GeometricTools/terrainHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@
QgsSpatialIndex,
QgsGeometry,
QgsProject,
QgsProcessingUtils,
)
from typing import Dict, List, Optional, Set, Tuple
from typing import Dict, List, Optional, Set, Tuple, Union
from . import graphHandler
from DsgTools.core.DSGToolsProcessingAlgs.algRunner import AlgRunner

Expand All @@ -50,7 +51,7 @@ class TerrainSlice:
outershellFeat: QgsFeature
holesFeatSet: Set[QgsFeature]
contoursOnSlice: Set[QgsFeature]
contourLineLayer: QgsVectorLayer
contourLineLayer: Union[QgsVectorLayer, str]
contourIdField: str

def __post_init__(self):
Expand All @@ -59,6 +60,9 @@ def __post_init__(self):
for feat in self.contoursOnSlice:
self.contourDict[feat.id()] = feat
self.spatialIndex.addFeature(feat)
if isinstance(self.contourLineLayer, str):
self.context = QgsProcessingContext()
self.contourLineLayer: QgsVectorLayer = QgsProcessingUtils.mapLayerFromString(self.contourLineLayer, self.context)
self.contourLineDict = {
feat[self.contourIdField]: feat
for feat in self.contourLineLayer.getFeatures()
Expand Down Expand Up @@ -574,6 +578,7 @@ def buildTerrainBand(
",)", ")"
),
context=context,
is_child_algorithm=True,
)
futures.add(
pool.submit(
Expand Down
Loading