30 #include <QJsonObject>
32 #include <QPainterPath>
34 #include <nlohmann/json.hpp>
48 auto result = std::make_unique< QgsCurvePolygon >();
50 return result.release();
55 return QStringLiteral(
"CurvePolygon" );
100 const QgsCurvePolygon *otherPolygon = qgsgeometry_cast< const QgsCurvePolygon * >( &other );
172 std::unique_ptr< QgsCurve > currentCurve;
173 for (
int i = 0; i < nRings; ++i )
176 wkbPtr -= 1 +
sizeof( int );
194 currentCurve->fromWkb( wkbPtr );
219 QString secondWithoutParentheses =
parts.second;
220 secondWithoutParentheses = secondWithoutParentheses.remove(
'(' ).remove(
')' ).simplified().remove(
' ' );
221 if ( (
parts.second.compare( QLatin1String(
"EMPTY" ), Qt::CaseInsensitive ) == 0 ) ||
222 secondWithoutParentheses.isEmpty() )
225 QString defaultChildWkbType = QStringLiteral(
"LineString%1%2" ).arg(
is3D() ? QStringLiteral(
"Z" ) : QString(),
isMeasure() ? QStringLiteral(
"M" ) : QString() );
228 for (
const QString &childWkt : blocks )
270 hasZ = hasZ || curve->is3D();
271 hasM = hasM || curve->isMeasure();
294 int binarySize =
sizeof( char ) +
sizeof( quint32 ) +
sizeof( quint32 );
301 binarySize += curve->wkbSize( flags );
312 wkbPtr << static_cast<quint32>(
wkbType() );
320 wkbPtr << curve->asWkb( flags );
330 wkt += QLatin1String(
" EMPTY" );
333 wkt += QLatin1String(
" (" );
337 if ( qgsgeometry_cast<QgsLineString *>(
mExteriorRing.get() ) )
340 childWkt = childWkt.mid( childWkt.indexOf(
'(' ) );
342 wkt += childWkt +
',';
346 QString childWkt = curve->asWkt(
precision );
347 if ( qgsgeometry_cast<const QgsLineString *>( curve ) )
350 childWkt = childWkt.mid( childWkt.indexOf(
'(' ) );
352 wkt += childWkt +
',';
354 if ( wkt.endsWith(
',' ) )
366 QDomElement elemPolygon = doc.createElementNS( ns, QStringLiteral(
"Polygon" ) );
371 QDomElement elemOuterBoundaryIs = doc.createElementNS( ns, QStringLiteral(
"outerBoundaryIs" ) );
372 std::unique_ptr< QgsLineString > exteriorLineString(
exteriorRing()->curveToLine() );
373 QDomElement outerRing = exteriorLineString->asGml2( doc,
precision, ns, axisOrder );
374 outerRing.toElement().setTagName( QStringLiteral(
"LinearRing" ) );
375 elemOuterBoundaryIs.appendChild( outerRing );
376 elemPolygon.appendChild( elemOuterBoundaryIs );
377 std::unique_ptr< QgsLineString > interiorLineString;
380 QDomElement elemInnerBoundaryIs = doc.createElementNS( ns, QStringLiteral(
"innerBoundaryIs" ) );
381 interiorLineString.reset(
interiorRing( i )->curveToLine() );
382 QDomElement innerRing = interiorLineString->asGml2( doc,
precision, ns, axisOrder );
383 innerRing.toElement().setTagName( QStringLiteral(
"LinearRing" ) );
384 elemInnerBoundaryIs.appendChild( innerRing );
385 elemPolygon.appendChild( elemInnerBoundaryIs );
392 QDomElement elemCurvePolygon = doc.createElementNS( ns, QStringLiteral(
"Polygon" ) );
395 return elemCurvePolygon;
397 QDomElement elemExterior = doc.createElementNS( ns, QStringLiteral(
"exterior" ) );
399 if ( curveElem.tagName() == QLatin1String(
"LineString" ) )
401 curveElem.setTagName( QStringLiteral(
"LinearRing" ) );
403 elemExterior.appendChild( curveElem );
404 elemCurvePolygon.appendChild( elemExterior );
408 QDomElement elemInterior = doc.createElementNS( ns, QStringLiteral(
"interior" ) );
410 if ( innerRing.tagName() == QLatin1String(
"LineString" ) )
412 innerRing.setTagName( QStringLiteral(
"LinearRing" ) );
414 elemInterior.appendChild( innerRing );
415 elemCurvePolygon.appendChild( elemInterior );
417 return elemCurvePolygon;
422 json coordinates( json::array( ) );
425 std::unique_ptr< QgsLineString > exteriorLineString( lExteriorRing->curveToLine() );
427 exteriorLineString->points( exteriorPts );
430 std::unique_ptr< QgsLineString > interiorLineString;
433 interiorLineString.reset(
interiorRing( i )->curveToLine() );
435 interiorLineString->points( interiorPts );
441 {
"type",
"Polygon" },
442 {
"coordinates", coordinates }
449 kml.append( QLatin1String(
"<Polygon>" ) );
452 kml.append( QLatin1String(
"<outerBoundaryIs>" ) );
454 kml.append( QLatin1String(
"</outerBoundaryIs>" ) );
457 for (
const QgsCurve *ring : interiorRings )
459 kml.append( QLatin1String(
"<innerBoundaryIs>" ) );
461 kml.append( QLatin1String(
"</innerBoundaryIs>" ) );
463 kml.append( QLatin1String(
"</Polygon>" ) );
481 return a->compareTo( b ) > 0;
495 double totalArea = 0.0;
501 totalArea += std::fabs(
area );
507 if ( ring->isRing() )
509 ring->sumUpArea(
area );
510 totalArea -= std::fabs(
area );
532 std::unique_ptr< QgsPolygon > polygon(
new QgsPolygon() );
534 return polygon.release();
536 polygon->setExteriorRing(
exteriorRing()->curveToLine() );
537 QVector<QgsCurve *> interiors;
539 interiors.reserve( n );
540 for (
int i = 0; i < n; ++i )
544 polygon->setInteriorRings( interiors );
545 return polygon.release();
561 multiCurve->
reserve( nInteriorRings + 1 );
563 for (
int i = 0; i < nInteriorRings; ++i )
580 auto exterior = std::unique_ptr<QgsCurve> {
static_cast< QgsCurve *
>(
mExteriorRing->snappedToGrid( hSpacing, vSpacing, dSpacing, mSpacing ) ) };
585 polygon->mExteriorRing = std::move( exterior );
593 QgsCurve *gridifiedInterior =
static_cast< QgsCurve *
>( interior->snappedToGrid( hSpacing, vSpacing, dSpacing, mSpacing ) );
595 if ( !gridifiedInterior )
598 polygon->mInteriorRings.append( gridifiedInterior );
601 return polygon.release();
608 auto cleanRing = [epsilon, useZValues ](
QgsCurve * ring )->
bool
610 if ( ring->numPoints() <= 4 )
613 if ( ring->removeDuplicateNodes( epsilon, useZValues ) )
617 ring->pointAt( 0, startPoint, type );
631 if ( cleanRing( ring ) ) result =
true;
657 if ( ring->boundingBoxIntersects( rectangle ) )
671 std::unique_ptr< QgsPolygon > poly(
new QgsPolygon() );
674 return poly.release();
677 poly->setExteriorRing(
mExteriorRing->curveToLine( tolerance, toleranceType ) );
679 QVector<QgsCurve *> rings;
683 rings.push_back( ring->curveToLine( tolerance, toleranceType ) );
685 poly->setInteriorRings( rings );
686 return poly.release();
744 else if ( !ring->
is3D() )
769 for (
int ringIndex =
mInteriorRings.size() - 1; ringIndex >= 0; --ringIndex )
771 if ( minimumAllowedArea < 0 )
777 if (
area < minimumAllowedArea )
787 QVector<QgsCurve *> validRings;
791 if ( !curve->isRing() )
809 std::unique_ptr< QgsCurve > flipped(
mExteriorRing->reversed() );
813 QVector<QgsCurve *> validRings;
820 validRings << flipped;
843 QPainterPath ringPath = ring->asQPainterPath();
844 ringPath.closeSubpath();
845 p.addPath( ringPath );
867 ring->addToPainterPath( path );
882 curve->transform( ct, d, transformZ );
891 mExteriorRing->transform( t, zTranslate, zScale, mTranslate, mScale );
896 curve->transform( t, zTranslate, zScale, mTranslate, mScale );
915 ring->points( sequence.back().back() );
932 count += ring->nCoordinates();
943 if (
id.ring < 0 || id.ring >=
ringCount() )
958 if (
id.ring == i + 1 )
961 if ( partNumber == -1 )
963 return number + partNumber;
987 QVector<QgsCurve *> segmentList;
1039 if ( vertex.
vertex == 0 && n < 3 )
1043 else if ( vertex.
vertex == 0 )
1051 if ( vertex.
vertex == n - 1 && n < 3 )
1055 else if ( vertex.
vertex == n - 1 )
1074 if ( vertex.
ring == 0 )
1102 else if ( vId.
vertex == n )
1119 bool success = ring->
moveVertex( vId, newPos );
1125 else if ( vId.
vertex == n - 1 )
1144 if ( vId.
ring == 0 )
1168 else if ( vId.
vertex == n - 1 )
1184 if ( ring->hasCurvedSegments() )
1194 return toPolygon( tolerance, toleranceType );
1251 curve->addZValue( zValue );
1268 curve->addMValue( mValue );
1284 curve->dropZValue();
1300 curve->dropMValue();
1331 if ( !res || ( feedback && feedback->
isCanceled() ) )
1339 res = curve->transform( transformer );
1358 curve->filterVertices( filter );
1390 const QgsCurvePolygon *otherPolygon = qgsgeometry_cast<const QgsCurvePolygon *>( other );
1391 if ( !otherPolygon )
1401 if ( shellComp != 0 )
1409 if ( nHole1 < nHole2 )
1413 if ( nHole1 > nHole2 )
1418 for (
int i = 0; i < nHole1; i++ )
1421 if ( holeComp != 0 )
Abstract base class for all geometries.
virtual bool addZValue(double zValue=0)=0
Adds a z-dimension to the geometry, initialized to a preset value.
virtual bool moveVertex(QgsVertexId position, const QgsPoint &newPos)=0
Moves a vertex within the geometry.
SegmentationToleranceType
Segmentation tolerance as maximum angle or maximum difference between approximation and circle.
virtual double vertexAngle(QgsVertexId vertex) const =0
Returns approximate angle at a vertex.
virtual bool dropMValue()=0
Drops any measure values which exist in the geometry.
bool is3D() const SIP_HOLDGIL
Returns true if the geometry is 3D and contains a z-value.
virtual bool boundingBoxIntersects(const QgsRectangle &rectangle) const SIP_HOLDGIL
Returns true if the bounding box of this geometry intersects with a rectangle.
AxisOrder
Axis order for GML generation.
QString wktTypeStr() const
Returns the WKT type string of the geometry.
QgsAbstractGeometry & operator=(const QgsAbstractGeometry &geom)
virtual bool addMValue(double mValue=0)=0
Adds a measure to the geometry, initialized to a preset value.
virtual bool insertVertex(QgsVertexId position, const QgsPoint &vertex)=0
Inserts a vertex into the geometry.
virtual QDomElement asGml3(QDomDocument &doc, int precision=17, const QString &ns="gml", AxisOrder axisOrder=QgsAbstractGeometry::AxisOrder::XY) const =0
Returns a GML3 representation of the geometry.
virtual bool deleteVertex(QgsVertexId position)=0
Deletes a vertex within the geometry.
QgsWkbTypes::Type wkbType() const SIP_HOLDGIL
Returns the WKB type of the geometry.
virtual bool dropZValue()=0
Drops any z-dimensions which exist in the geometry.
virtual double segmentLength(QgsVertexId startVertex) const =0
Returns the length of the segment of the geometry which begins at startVertex.
QgsWkbTypes::Type mWkbType
QgsGeometryConstPartIterator parts() const
Returns Java-style iterator for traversal of parts of the geometry.
bool isMeasure() const SIP_HOLDGIL
Returns true if the geometry contains m values.
void setZMTypeFromSubGeometry(const QgsAbstractGeometry *subggeom, QgsWkbTypes::Type baseGeomType)
Updates the geometry type based on whether sub geometries contain z or m values.
static endian_t endian()
Returns whether this machine uses big or little endian.
Circular string geometry type.
Compound curve geometry type.
QgsWkbTypes::Type readHeader() const
readHeader
Curve polygon geometry type.
QByteArray asWkb(QgsAbstractGeometry::WkbFlags flags=QgsAbstractGeometry::WkbFlags()) const override
QgsCoordinateSequence coordinateSequence() const override
Retrieves the sequence of geometries, rings and nodes.
int ringCount(int part=0) const override SIP_HOLDGIL
Returns the number of rings of which this geometry is built.
int wkbSize(QgsAbstractGeometry::WkbFlags flags=QgsAbstractGeometry::WkbFlags()) const override
Returns the length of the QByteArray returned by asWkb()
bool moveVertex(QgsVertexId position, const QgsPoint &newPos) override
Moves a vertex within the geometry.
bool isEmpty() const override SIP_HOLDGIL
Returns true if the geometry is empty.
QString asWkt(int precision=17) const override
Returns a WKT representation of the geometry.
bool addZValue(double zValue=0) override
Adds a z-dimension to the geometry, initialized to a preset value.
QDomElement asGml2(QDomDocument &doc, int precision=17, const QString &ns="gml", QgsAbstractGeometry::AxisOrder axisOrder=QgsAbstractGeometry::AxisOrder::XY) const override
Returns a GML2 representation of the geometry.
double vertexAngle(QgsVertexId vertex) const override
Returns approximate rotation angle for a vertex.
bool hasCurvedSegments() const override
Returns true if the geometry contains curved segments.
QgsAbstractGeometry * boundary() const override
Returns the closure of the combinatorial boundary of the geometry (ie the topological boundary of the...
QgsCurvePolygon * createEmptyWithSameType() const override
Creates a new geometry with the same class and same WKB type as the original and transfers ownership.
QPainterPath asQPainterPath() const override
Returns the geometry represented as a QPainterPath.
void swapXy() override
Swaps the x and y coordinates from the geometry.
int vertexCount(int part=0, int ring=0) const override
Returns the number of vertices of which this geometry is built.
virtual QgsPolygon * toPolygon(double tolerance=M_PI_2/90, SegmentationToleranceType toleranceType=MaximumAngle) const
Returns a new polygon geometry corresponding to a segmentized approximation of the curve.
QVector< QgsCurve * > mInteriorRings
bool fromWkb(QgsConstWkbPtr &wkb) override
Sets the geometry from a WKB string.
QgsCurvePolygon * snappedToGrid(double hSpacing, double vSpacing, double dSpacing=0, double mSpacing=0) const override
Makes a new geometry with all the points or vertices snapped to the closest point of the grid.
QgsRectangle calculateBoundingBox() const override
Default calculator for the minimal bounding box for the geometry.
void normalize() final SIP_HOLDGIL
Reorganizes the geometry into a normalized form (or "canonical" form).
bool operator!=(const QgsAbstractGeometry &other) const override
void removeInteriorRings(double minimumAllowedArea=-1)
Removes the interior rings from the polygon.
QgsCurvePolygon * clone() const override
Clones the geometry by performing a deep copy.
QgsCurvePolygon * toCurveType() const override
Returns the geometry converted to the more generic curve type.
void forceRHR()
Forces the geometry to respect the Right-Hand-Rule, in which the area that is bounded by the polygon ...
QString asKml(int precision=17) const override
Returns a KML representation of the geometry.
void clear() override
Clears the geometry, ie reset it to a null geometry.
virtual void setExteriorRing(QgsCurve *ring)
Sets the exterior ring of the polygon.
double closestSegment(const QgsPoint &pt, QgsPoint &segmentPt, QgsVertexId &vertexAfter, int *leftOf=nullptr, double epsilon=4 *std::numeric_limits< double >::epsilon()) const override
Searches for the closest segment of the geometry to a given point.
int nCoordinates() const override
Returns the number of nodes contained in the geometry.
int partCount() const override SIP_HOLDGIL
Returns count of parts contained in the geometry.
void filterVertices(const std::function< bool(const QgsPoint &) > &filter) override
Filters the vertices from the geometry in place, removing any which do not return true for the filter...
void adjacentVertices(QgsVertexId vertex, QgsVertexId &previousVertex, QgsVertexId &nextVertex) const override
Returns the vertices adjacent to a specified vertex within a geometry.
void draw(QPainter &p) const override
Draws the geometry using the specified QPainter.
bool addMValue(double mValue=0) override
Adds a measure to the geometry, initialized to a preset value.
QgsCurvePolygon & operator=(const QgsCurvePolygon &p)
bool dropZValue() override
Drops any z-dimensions which exist in the geometry.
virtual void addInteriorRing(QgsCurve *ring)
Adds an interior ring to the geometry (takes ownership)
~QgsCurvePolygon() override
void transform(const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection d=QgsCoordinateTransform::ForwardTransform, bool transformZ=false) override SIP_THROW(QgsCsException)
Transforms the geometry using a coordinate transform.
bool nextVertex(QgsVertexId &id, QgsPoint &vertex) const override
Returns next vertex id and coordinates.
QgsPoint vertexAt(QgsVertexId id) const override
Returns the point corresponding to a specified vertex id.
bool insertVertex(QgsVertexId position, const QgsPoint &vertex) override
Inserts a vertex into the geometry.
const QgsCurve * interiorRing(int i) const SIP_HOLDGIL
Retrieves an interior ring from the curve polygon.
int dimension() const override SIP_HOLDGIL
Returns the inherent dimension of the geometry.
bool fromWkt(const QString &wkt) override
Sets the geometry from a WKT string.
void removeInvalidRings()
Removes any interior rings which are not valid from the polygon.
QgsAbstractGeometry * segmentize(double tolerance=M_PI_2/90, SegmentationToleranceType toleranceType=MaximumAngle) const override
Returns a geometry without curves.
int vertexNumberFromVertexId(QgsVertexId id) const override
Returns the vertex number corresponding to a vertex id.
double area() const override SIP_HOLDGIL
Returns the planar, 2-dimensional area of the geometry.
void transformVertices(const std::function< QgsPoint(const QgsPoint &) > &transform) override
Transforms the vertices from the geometry in place, applying the transform function to every vertex.
int childCount() const override
Returns number of child geometries (for geometries with child geometries) or child points (for geomet...
bool deleteVertex(QgsVertexId position) override
Deletes a vertex within the geometry.
QString geometryType() const override SIP_HOLDGIL
Returns a unique string representing the geometry type.
QDomElement asGml3(QDomDocument &doc, int precision=17, const QString &ns="gml", QgsAbstractGeometry::AxisOrder axisOrder=QgsAbstractGeometry::AxisOrder::XY) const override
Returns a GML3 representation of the geometry.
bool removeDuplicateNodes(double epsilon=4 *std::numeric_limits< double >::epsilon(), bool useZValues=false) override
Removes duplicate nodes from the geometry, wherever removing the nodes does not result in a degenerat...
const QgsCurve * exteriorRing() const SIP_HOLDGIL
Returns the curve polygon's exterior ring.
bool dropMValue() override
Drops any measure values which exist in the geometry.
int numInteriorRings() const SIP_HOLDGIL
Returns the number of interior rings contained with the curve polygon.
bool boundingBoxIntersects(const QgsRectangle &rectangle) const override SIP_HOLDGIL
Returns true if the bounding box of this geometry intersects with a rectangle.
QgsAbstractGeometry * childGeometry(int index) const override
Returns pointer to child geometry (for geometries with child geometries - i.e.
json asJsonObject(int precision=17) const override
Returns a json object representation of the geometry.
void setInteriorRings(const QVector< QgsCurve * > &rings)
Sets all interior rings (takes ownership)
double perimeter() const override SIP_HOLDGIL
Returns the planar, 2-dimensional perimeter of the geometry.
bool operator==(const QgsAbstractGeometry &other) const override
QgsPolygon * surfaceToPolygon() const override
Gets a polygon representation of this surface.
bool removeInteriorRing(int ringIndex)
Removes an interior ring from the polygon.
std::unique_ptr< QgsCurve > mExteriorRing
int compareToSameClass(const QgsAbstractGeometry *other) const final
Compares to an other geometry of the same class, and returns a integer for sorting of the two geometr...
double segmentLength(QgsVertexId startVertex) const override
Returns the length of the segment of the geometry which begins at startVertex.
Abstract base class for curved geometry type.
virtual int numPoints() const =0
Returns the number of points in the curve.
QgsPoint vertexAt(QgsVertexId id) const override
Returns the point corresponding to a specified vertex id.
QgsCurve * clone() const override=0
Clones the geometry by performing a deep copy.
@ Clockwise
Clockwise orientation.
@ CounterClockwise
Counter-clockwise orientation.
bool nextVertex(QgsVertexId &id, QgsPoint &vertex) const override
Returns next vertex id and coordinates.
virtual QgsCurve * reversed() const =0
Returns a reversed copy of the curve, where the direction of the curve has been flipped.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
bool isCanceled() const SIP_HOLDGIL
Tells whether the operation has been canceled already.
void reserve(int size) SIP_HOLDGIL
Attempts to allocate memory for at least size geometries.
static QPair< QgsWkbTypes::Type, QString > wktReadBlock(const QString &wkt)
Parses a WKT block of the format "TYPE( contents )" and returns a pair of geometry type to contents (...
static json pointsToJson(const QgsPointSequence &points, int precision)
Returns coordinates as json object.
static QStringList wktGetChildBlocks(const QString &wkt, const QString &defaultType=QString())
Parses a WKT string and returns of list of blocks contained in the WKT.
static double closestSegmentFromComponents(T &container, ComponentType ctype, const QgsPoint &pt, QgsPoint &segmentPt, QgsVertexId &vertexAfter, int *leftOf, double epsilon)
Line string geometry type, with support for z-dimension and m-values.
Multi curve geometry collection.
bool addGeometry(QgsAbstractGeometry *g) override
Adds a geometry and takes ownership. Returns true in case of success.
Point geometry type, with support for z-dimension and m-values.
bool moveVertex(QgsVertexId position, const QgsPoint &newPos) override
Moves a vertex within the geometry.
A rectangle specified with double values.
bool intersects(const QgsRectangle &rect) const SIP_HOLDGIL
Returns true when rectangle intersects with other rectangle.
bool isNull() const
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
QgsRectangle mBoundingBox
void clearCache() const override
Clears any cached parameters associated with the geometry, e.g., bounding boxes.
static GeometryType geometryType(Type type) SIP_HOLDGIL
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
static bool hasM(Type type) SIP_HOLDGIL
Tests whether a WKB type contains m values.
Type
The WKB type describes the number of dimensions a geometry has.
static Type dropZ(Type type) SIP_HOLDGIL
Drops the z dimension (if present) for a WKB type and returns the new type.
static Type flatType(Type type) SIP_HOLDGIL
Returns the flat type for a WKB type.
static Type addZ(Type type) SIP_HOLDGIL
Adds the z dimension to a WKB type and returns the new type.
static bool hasZ(Type type) SIP_HOLDGIL
Tests whether a WKB type contains the z-dimension.
static Type addM(Type type) SIP_HOLDGIL
Adds the m dimension to a WKB type and returns the new type.
static Type dropM(Type type) SIP_HOLDGIL
Drops the m dimension (if present) for a WKB type and returns the new type.
double ANALYSIS_EXPORT leftOf(const QgsPoint &thepoint, const QgsPoint *p1, const QgsPoint *p2)
Returns whether 'thepoint' is left or right of the line from 'p1' to 'p2'. Negative values mean left ...
QVector< QgsRingSequence > QgsCoordinateSequence
QVector< QgsPointSequence > QgsRingSequence
QVector< QgsPoint > QgsPointSequence
void ringAdjacentVertices(const QgsCurve *curve, QgsVertexId vertex, QgsVertexId &previousVertex, QgsVertexId &nextVertex)
Utility class for identifying a unique vertex within a geometry.
VertexType
Type of vertex.