Skip to content
Open
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
19 changes: 14 additions & 5 deletions src/core/vector/qgsvectorlayerprofilegenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1479,7 +1479,7 @@ bool QgsVectorLayerProfileGenerator::generateProfileForPolygons()
if ( triangleIsCollinearInXYPlane( triangle ) )
{
wasCollinear = true;
const QgsLineString *ring = qgsgeometry_cast< const QgsLineString * >( polygon->exteriorRing() );
const QgsLineString *ring = qgsgeometry_cast< const QgsLineString * >( triangle->exteriorRing() );

QString lastError;
if ( const QgsLineString *ls = qgsgeometry_cast< const QgsLineString * >( mProfileCurve.get() ) )
Expand All @@ -1490,24 +1490,33 @@ bool QgsVectorLayerProfileGenerator::generateProfileForPolygons()
const QgsPoint p2 = ls->pointN( curveSegmentIndex + 1 );

QgsPoint intersectionPoint;
bool intersectionFound = false;
double minZ = std::numeric_limits< double >::max();
double maxZ = std::numeric_limits< double >::lowest();

for ( auto vertexPair : std::array<std::pair<int, int>, 3> { { { 0, 1 }, { 1, 2 }, { 2, 0 } } } )
{
bool isIntersection = false;
if ( QgsGeometryUtils::segmentIntersection( ring->pointN( vertexPair.first ), ring->pointN( vertexPair.second ), p1, p2, intersectionPoint, isIntersection ) )
QgsPoint edgeIntersectionPoint;
if ( QgsGeometryUtils::segmentIntersection( ring->pointN( vertexPair.first ), ring->pointN( vertexPair.second ), p1, p2, edgeIntersectionPoint, isIntersection ) )
{
if ( !edgeIntersectionPoint.isEmpty() && !intersectionFound )
{
intersectionPoint = edgeIntersectionPoint;
intersectionFound = true;
}


const double fraction = QgsGeometryUtilsBase::pointFractionAlongLine(
ring->xAt( vertexPair.first ), ring->yAt( vertexPair.first ), ring->xAt( vertexPair.second ), ring->yAt( vertexPair.second ), intersectionPoint.x(), intersectionPoint.y()
ring->xAt( vertexPair.first ), ring->yAt( vertexPair.first ), ring->xAt( vertexPair.second ), ring->yAt( vertexPair.second ), edgeIntersectionPoint.x(), edgeIntersectionPoint.y()
);
const double intersectionZ = ring->zAt( vertexPair.first ) + ( ring->zAt( vertexPair.second ) - ring->zAt( vertexPair.first ) ) * fraction;
double intersectionZ = ring->zAt( vertexPair.first ) + ( ring->zAt( vertexPair.second ) - ring->zAt( vertexPair.first ) ) * fraction;
minZ = std::min( minZ, intersectionZ );
maxZ = std::max( maxZ, intersectionZ );
}
}

if ( !intersectionPoint.isEmpty() )
if ( intersectionFound )
{
// need z?
mResults->mRawPoints.append( intersectionPoint );
Expand Down
30 changes: 30 additions & 0 deletions tests/src/python/test_qgsvectorlayerprofilegenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,36 @@ def testPolygonGenerationAbsolute(self):
self.assertAlmostEqual(results.zRange().lower(), 15.0, 2)
self.assertAlmostEqual(results.zRange().upper(), 22.5000, 2)

def testPolygonGenerationFollowingLinestringZExactly(self):
vl = QgsVectorLayer("PolygonZ?crs=EPSG:3857", "polygon", "memory")
self.assertTrue(vl.isValid())

polygon_wkt = "Polygon Z ((547405.88 6150237.19 0, 547405.88 6150237.19 5, 547413.39 6150203.24 5, 547413.39 6150203.24 0, 547405.88 6150237.19 0))"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those "random" looking coordinates are difficult to reason with. If there was a way of coming with simple looking coordinates, that might help (no strong requirement, just asking in case it is not too difficult)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No strong opinion. I just tried to stay in line with other unit test which use similar "random" coordinates.

feature = QgsFeature()
feature.setGeometry(QgsGeometry.fromWkt(polygon_wkt))
self.assertTrue(vl.dataProvider().addFeature(feature))

vl.elevationProperties().setClamping(Qgis.AltitudeClamping.Absolute)

curve = QgsLineString()
curve.fromWkt(
"LineString (547394.297515 6150220.613784, 547416.10922 6150225.219982)"
)
req = QgsProfileRequest(curve)
req.setCrs(QgsCoordinateReferenceSystem("EPSG:3857"))

generator = vl.createProfileGenerator(req)
self.assertTrue(generator.generateProfile())
results = generator.takeResults()

self.assertEqual(
self.round_dict(results.distanceToHeightMap(), 1, 1),
{14.9: 5.0},
)

self.assertAlmostEqual(results.zRange().lower(), 0.0, 2)
self.assertAlmostEqual(results.zRange().upper(), 5.0, 2)

def testPolygonGenerationTerrain(self):
vl = QgsVectorLayer("PolygonZ?crs=EPSG:27700", "lines", "memory")
self.assertTrue(vl.isValid())
Expand Down
Loading