Skip to content

Commit

Permalink
qgsgeometry: Add polygon to polyhedralsurface conversion in coerceToType
Browse files Browse the repository at this point in the history
This allows to edit a vector layer of polyhedral surface to add new
polygon features.
  • Loading branch information
ptitjano authored and lbartoletti committed Sep 9, 2024
1 parent 9f18b92 commit 6012b65
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/core/geometry/qgsgeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ email : morb at ozemail dot com dot au
#include "qgslinestring.h"
#include "qgscircle.h"
#include "qgscurve.h"
#include "qgspolyhedralsurface.h"

struct QgsGeometryPrivate
{
Expand Down Expand Up @@ -1772,6 +1773,22 @@ QVector<QgsGeometry> QgsGeometry::coerceToType( const Qgis::WkbType type, double
newGeom = QgsGeometry( std::move( mp ) );
}

//(Multi)Polygon to PolyhedralSurface
if ( QgsWkbTypes::flatType( type ) == Qgis::WkbType::PolyhedralSurface &&
QgsWkbTypes::flatType( QgsWkbTypes::singleType( newGeom.wkbType() ) ) == Qgis::WkbType::Polygon )
{
std::unique_ptr< QgsPolyhedralSurface > polySurface = std::make_unique< QgsPolyhedralSurface >();
const QgsGeometry source = newGeom;
for ( auto part = source.const_parts_begin(); part != source.const_parts_end(); ++part )
{
if ( const QgsPolygon *polygon = qgsgeometry_cast< const QgsPolygon * >( *part ) )
{
polySurface->addPatch( polygon->clone() );
}
}
newGeom = QgsGeometry( std::move( polySurface ) );
}

// Single -> multi
if ( QgsWkbTypes::isMultiType( type ) && ! newGeom.isMultipart( ) )
{
Expand Down
8 changes: 8 additions & 0 deletions tests/src/python/test_qgsgeometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -6903,6 +6903,14 @@ def coerce_to_wkt(wkt, type, defaultZ=None, defaultM=None):
QgsWkbTypes.Type.MultiCurve),
['MultiCurve (LineString (0 0, 1 1))'])

# Polygon to PolyhedralSurface
self.assertEqual(coerce_to_wkt('Polygon ((1 1, 1 2, 2 2, 5 5, 1 1))',
QgsWkbTypes.Type.PolyhedralSurface),
['PolyhedralSurface (((1 1, 1 2, 2 2, 5 5, 1 1)))'])
self.assertEqual(coerce_to_wkt('Polygon Z((1 1 2, 1 2 2, 2 2 3, 5 5 3, 1 1 2))',
QgsWkbTypes.Type.PolyhedralSurfaceZ),
['PolyhedralSurfaceZ (((1 1 2, 1 2 2, 2 2 3, 5 5 3, 1 1 2)))'])

def testTriangularWaves(self):
"""Test triangular waves"""
self.assertEqual(QgsGeometry.fromWkt('Point (1 1)').triangularWaves(1, 2).asWkt(3), 'Point (1 1)')
Expand Down

0 comments on commit 6012b65

Please sign in to comment.