QGIS API Documentation
3.16.0-Hannover (43b64b13f3)
|
Go to the documentation of this file.
29 #include <QJsonObject>
31 #include <QPainterPath>
33 #include <nlohmann/json.hpp>
47 auto result = qgis::make_unique< QgsCurvePolygon >();
49 return result.release();
54 return QStringLiteral(
"CurvePolygon" );
99 const QgsCurvePolygon *otherPolygon = qgsgeometry_cast< const QgsCurvePolygon * >( &other );
171 std::unique_ptr< QgsCurve > currentCurve;
172 for (
int i = 0; i < nRings; ++i )
175 wkbPtr -= 1 +
sizeof( int );
193 currentCurve->fromWkb( wkbPtr );
218 QString secondWithoutParentheses =
parts.second;
219 secondWithoutParentheses = secondWithoutParentheses.remove(
'(' ).remove(
')' ).simplified().remove(
' ' );
220 if ( (
parts.second.compare( QLatin1String(
"EMPTY" ), Qt::CaseInsensitive ) == 0 ) ||
221 secondWithoutParentheses.isEmpty() )
224 QString defaultChildWkbType = QStringLiteral(
"LineString%1%2" ).arg(
is3D() ? QStringLiteral(
"Z" ) : QString(),
isMeasure() ? QStringLiteral(
"M" ) : QString() );
227 for (
const QString &childWkt : blocks )
269 hasZ = hasZ || curve->is3D();
270 hasM = hasM || curve->isMeasure();
293 int binarySize =
sizeof( char ) +
sizeof( quint32 ) +
sizeof( quint32 );
300 binarySize += curve->wkbSize( flags );
311 wkbPtr << static_cast<quint32>(
wkbType() );
319 wkbPtr << curve->asWkb( flags );
329 wkt += QLatin1String(
" EMPTY" );
332 wkt += QLatin1String(
" (" );
336 if ( qgsgeometry_cast<QgsLineString *>(
mExteriorRing.get() ) )
339 childWkt = childWkt.mid( childWkt.indexOf(
'(' ) );
341 wkt += childWkt +
',';
345 QString childWkt = curve->asWkt(
precision );
346 if ( qgsgeometry_cast<const QgsLineString *>( curve ) )
349 childWkt = childWkt.mid( childWkt.indexOf(
'(' ) );
351 wkt += childWkt +
',';
353 if ( wkt.endsWith(
',' ) )
365 QDomElement elemPolygon = doc.createElementNS( ns, QStringLiteral(
"Polygon" ) );
370 QDomElement elemOuterBoundaryIs = doc.createElementNS( ns, QStringLiteral(
"outerBoundaryIs" ) );
371 std::unique_ptr< QgsLineString > exteriorLineString(
exteriorRing()->curveToLine() );
372 QDomElement outerRing = exteriorLineString->asGml2( doc,
precision, ns, axisOrder );
373 outerRing.toElement().setTagName( QStringLiteral(
"LinearRing" ) );
374 elemOuterBoundaryIs.appendChild( outerRing );
375 elemPolygon.appendChild( elemOuterBoundaryIs );
376 std::unique_ptr< QgsLineString > interiorLineString;
379 QDomElement elemInnerBoundaryIs = doc.createElementNS( ns, QStringLiteral(
"innerBoundaryIs" ) );
380 interiorLineString.reset(
interiorRing( i )->curveToLine() );
381 QDomElement innerRing = interiorLineString->asGml2( doc,
precision, ns, axisOrder );
382 innerRing.toElement().setTagName( QStringLiteral(
"LinearRing" ) );
383 elemInnerBoundaryIs.appendChild( innerRing );
384 elemPolygon.appendChild( elemInnerBoundaryIs );
391 QDomElement elemCurvePolygon = doc.createElementNS( ns, QStringLiteral(
"Polygon" ) );
394 return elemCurvePolygon;
396 QDomElement elemExterior = doc.createElementNS( ns, QStringLiteral(
"exterior" ) );
398 if ( curveElem.tagName() == QLatin1String(
"LineString" ) )
400 curveElem.setTagName( QStringLiteral(
"LinearRing" ) );
402 elemExterior.appendChild( curveElem );
403 elemCurvePolygon.appendChild( elemExterior );
407 QDomElement elemInterior = doc.createElementNS( ns, QStringLiteral(
"interior" ) );
409 if ( innerRing.tagName() == QLatin1String(
"LineString" ) )
411 innerRing.setTagName( QStringLiteral(
"LinearRing" ) );
413 elemInterior.appendChild( innerRing );
414 elemCurvePolygon.appendChild( elemInterior );
416 return elemCurvePolygon;
421 json coordinates( json::array( ) );
424 std::unique_ptr< QgsLineString > exteriorLineString( lExteriorRing->curveToLine() );
426 exteriorLineString->points( exteriorPts );
429 std::unique_ptr< QgsLineString > interiorLineString;
432 interiorLineString.reset(
interiorRing( i )->curveToLine() );
434 interiorLineString->points( interiorPts );
440 {
"type",
"Polygon" },
441 {
"coordinates", coordinates }
448 kml.append( QLatin1String(
"<Polygon>" ) );
451 kml.append( QLatin1String(
"<outerBoundaryIs>" ) );
453 kml.append( QLatin1String(
"</outerBoundaryIs>" ) );
456 for (
const QgsCurve *ring : interiorRings )
458 kml.append( QLatin1String(
"<innerBoundaryIs>" ) );
460 kml.append( QLatin1String(
"</innerBoundaryIs>" ) );
462 kml.append( QLatin1String(
"</Polygon>" ) );
473 double totalArea = 0.0;
479 totalArea += std::fabs(
area );
485 if ( ring->isRing() )
487 ring->sumUpArea(
area );
488 totalArea -= std::fabs(
area );
510 std::unique_ptr< QgsPolygon > polygon(
new QgsPolygon() );
512 return polygon.release();
514 polygon->setExteriorRing(
exteriorRing()->curveToLine() );
515 QVector<QgsCurve *> interiors;
517 interiors.reserve( n );
518 for (
int i = 0; i < n; ++i )
522 polygon->setInteriorRings( interiors );
523 return polygon.release();
539 multiCurve->
reserve( nInteriorRings + 1 );
541 for (
int i = 0; i < nInteriorRings; ++i )
558 auto exterior = std::unique_ptr<QgsCurve> {
static_cast< QgsCurve *
>(
mExteriorRing->snappedToGrid( hSpacing, vSpacing, dSpacing, mSpacing ) ) };
563 polygon->mExteriorRing = std::move( exterior );
571 QgsCurve *gridifiedInterior =
static_cast< QgsCurve *
>( interior->snappedToGrid( hSpacing, vSpacing, dSpacing, mSpacing ) );
573 if ( !gridifiedInterior )
576 polygon->mInteriorRings.append( gridifiedInterior );
579 return polygon.release();
586 auto cleanRing = [epsilon, useZValues ](
QgsCurve * ring )->
bool
588 if ( ring->numPoints() <= 4 )
591 if ( ring->removeDuplicateNodes( epsilon, useZValues ) )
595 ring->pointAt( 0, startPoint, type );
609 if ( cleanRing( ring ) ) result =
true;
616 std::unique_ptr< QgsPolygon > poly(
new QgsPolygon() );
619 return poly.release();
622 poly->setExteriorRing(
mExteriorRing->curveToLine( tolerance, toleranceType ) );
624 QVector<QgsCurve *> rings;
628 rings.push_back( ring->curveToLine( tolerance, toleranceType ) );
630 poly->setInteriorRings( rings );
631 return poly.release();
689 else if ( !ring->
is3D() )
714 for (
int ringIndex =
mInteriorRings.size() - 1; ringIndex >= 0; --ringIndex )
716 if ( minimumAllowedArea < 0 )
722 if (
area < minimumAllowedArea )
732 QVector<QgsCurve *> validRings;
736 if ( !curve->isRing() )
754 std::unique_ptr< QgsCurve > flipped(
mExteriorRing->reversed() );
758 QVector<QgsCurve *> validRings;
765 validRings << flipped;
788 QPainterPath ringPath = ring->asQPainterPath();
789 ringPath.closeSubpath();
790 p.addPath( ringPath );
812 ring->addToPainterPath( path );
827 curve->transform( ct, d, transformZ );
836 mExteriorRing->transform( t, zTranslate, zScale, mTranslate, mScale );
841 curve->transform( t, zTranslate, zScale, mTranslate, mScale );
860 ring->points( sequence.back().back() );
877 count += ring->nCoordinates();
888 if (
id.ring < 0 || id.ring >=
ringCount() )
903 if (
id.ring == i + 1 )
906 if ( partNumber == -1 )
908 return number + partNumber;
932 QVector<QgsCurve *> segmentList;
984 if ( vertex.
vertex == 0 && n < 3 )
988 else if ( vertex.
vertex == 0 )
996 if ( vertex.
vertex == n - 1 && n < 3 )
1000 else if ( vertex.
vertex == n - 1 )
1019 if ( vertex.
ring == 0 )
1047 else if ( vId.
vertex == n )
1064 bool success = ring->
moveVertex( vId, newPos );
1070 else if ( vId.
vertex == n - 1 )
1089 if ( vId.
ring == 0 )
1113 else if ( vId.
vertex == n - 1 )
1129 if ( ring->hasCurvedSegments() )
1139 return toPolygon( tolerance, toleranceType );
1196 curve->addZValue( zValue );
1213 curve->addMValue( mValue );
1229 curve->dropZValue();
1245 curve->dropMValue();
1274 curve->filterVertices( filter );
Abstract base class for curved geometry type.
bool nextVertex(QgsVertexId &id, QgsPoint &vertex) const override
Returns next vertex id and coordinates.
bool hasCurvedSegments() const override
Returns true if the geometry contains curved segments.
QVector< QgsRingSequence > QgsCoordinateSequence
bool addMValue(double mValue=0) override
Adds a measure to the geometry, initialized to a preset value.
virtual bool dropMValue()=0
Drops any measure values which exist in the geometry.
void clearCache() const override
Clears any cached parameters associated with the geometry, e.g., bounding boxes.
bool addGeometry(QgsAbstractGeometry *g) override
Adds a geometry and takes ownership. Returns true in case of success.
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.
QPainterPath asQPainterPath() const override
Returns the geometry represented as a QPainterPath.
virtual bool dropZValue()=0
Drops any z-dimensions which exist in the geometry.
static Type dropM(Type type) SIP_HOLDGIL
Drops the m dimension (if present) for a WKB type and returns the new type.
static json pointsToJson(const QgsPointSequence &points, int precision)
Returns coordinates as json object.
int nCoordinates() const override
Returns the number of nodes contained in the geometry.
virtual bool addZValue(double zValue=0)=0
Adds a z-dimension to the geometry, initialized to a preset value.
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.
void draw(QPainter &p) const override
Draws the geometry using the specified QPainter.
Point geometry type, with support for z-dimension and m-values.
virtual bool moveVertex(QgsVertexId position, const QgsPoint &newPos)=0
Moves a vertex within the geometry.
virtual QgsCurve * reversed() const =0
Returns a reversed copy of the curve, where the direction of the curve has been flipped.
const QgsCurve * exteriorRing() const SIP_HOLDGIL
Returns the curve polygon's exterior ring.
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.
virtual bool addMValue(double mValue=0)=0
Adds a measure 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.
static Type flatType(Type type) SIP_HOLDGIL
Returns the flat type for a WKB type.
Curve polygon geometry type.
QgsGeometryConstPartIterator parts() const
Returns Java-style iterator for traversal of parts of the geometry.
QgsCurvePolygon * clone() const override
Clones the geometry by performing a deep copy.
static Type addZ(Type type) SIP_HOLDGIL
Adds the z dimension to a WKB type and returns the new type.
QString wktTypeStr() const
Returns the WKT type string of the geometry.
void swapXy() override
Swaps the x and y coordinates from the geometry.
int wkbSize(QgsAbstractGeometry::WkbFlags flags=QgsAbstractGeometry::WkbFlags()) const override
Returns the length of the QByteArray returned by asWkb()
bool deleteVertex(QgsVertexId position) override
Deletes a vertex within the geometry.
void setInteriorRings(const QVector< QgsCurve * > &rings)
Sets all interior rings (takes ownership)
Type
The WKB type describes the number of dimensions a geometry has.
SegmentationToleranceType
Segmentation tolerance as maximum angle or maximum difference between approximation and circle.
QgsWkbTypes::Type mWkbType
Line string geometry type, with support for z-dimension and m-values.
bool isEmpty() const override SIP_HOLDGIL
Returns true if the geometry is empty.
A rectangle specified with double values.
bool dropZValue() override
Drops any z-dimensions which exist in the geometry.
QgsRectangle calculateBoundingBox() const override
Default calculator for the minimal bounding box for the geometry.
QString asKml(int precision=17) const override
Returns a KML representation of the geometry.
void adjacentVertices(QgsVertexId vertex, QgsVertexId &previousVertex, QgsVertexId &nextVertex) const override
Returns the vertices adjacent to a specified vertex within a geometry.
bool isMeasure() const SIP_HOLDGIL
Returns true if the geometry contains m values.
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...
int numInteriorRings() const SIP_HOLDGIL
Returns the number of interior rings contained with the curve polygon.
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 ...
virtual double vertexAngle(QgsVertexId vertex) const =0
Returns approximate angle at a vertex.
static Type addM(Type type) SIP_HOLDGIL
Adds the m dimension to a WKB type and returns the new type.
bool insertVertex(QgsVertexId position, const QgsPoint &vertex) override
Inserts a vertex into the geometry.
bool removeInteriorRing(int ringIndex)
Removes an interior ring from the polygon.
json asJsonObject(int precision=17) const override
Returns a json object representation of the geometry.
static double closestSegmentFromComponents(T &container, ComponentType ctype, const QgsPoint &pt, QgsPoint &segmentPt, QgsVertexId &vertexAfter, int *leftOf, double epsilon)
std::unique_ptr< QgsCurve > mExteriorRing
Circular string geometry type.
QgsWkbTypes::Type wkbType() const SIP_HOLDGIL
Returns the WKB type of the geometry.
bool fromWkb(QgsConstWkbPtr &wkb) override
Sets the geometry from a WKB string.
virtual bool insertVertex(QgsVertexId position, const QgsPoint &vertex)=0
Inserts a vertex into the geometry.
void removeInteriorRings(double minimumAllowedArea=-1)
Removes the interior rings from the polygon.
int partCount() const override SIP_HOLDGIL
Returns count of parts contained in the geometry.
AxisOrder
Axis order for GML generation.
QgsAbstractGeometry * childGeometry(int index) const override
Returns pointer to child geometry (for geometries with child geometries - i.e.
void transform(const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection d=QgsCoordinateTransform::ForwardTransform, bool transformZ=false) override SIP_THROW(QgsCsException)
Transforms the geometry using a coordinate transform.
Multi curve geometry collection.
~QgsCurvePolygon() override
virtual void addInteriorRing(QgsCurve *ring)
Adds an interior ring to the geometry (takes ownership)
int childCount() const override
Returns number of child geometries (for geometries with child geometries) or child points (for geomet...
void removeInvalidRings()
Removes any interior rings which are not valid from the polygon.
bool operator!=(const QgsAbstractGeometry &other) const override
@ Clockwise
Clockwise orientation.
double area() const override SIP_HOLDGIL
Returns the planar, 2-dimensional area of the geometry.
bool nextVertex(QgsVertexId &id, QgsPoint &vertex) const override
Returns next vertex id and coordinates.
virtual bool deleteVertex(QgsVertexId position)=0
Deletes a vertex within 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...
@ CounterClockwise
Counter-clockwise orientation.
static bool hasM(Type type) SIP_HOLDGIL
Tests whether a WKB type contains m values.
bool dropMValue() override
Drops any measure values which exist in the geometry.
bool operator==(const QgsAbstractGeometry &other) const override
static endian_t endian()
Returns whether this machine uses big or little endian.
QString geometryType() const override SIP_HOLDGIL
Returns a unique string representing the geometry type.
static Type dropZ(Type type) SIP_HOLDGIL
Drops the z dimension (if present) for a WKB type and returns the new type.
QgsCurvePolygon * createEmptyWithSameType() const override
Creates a new geometry with the same class and same WKB type as the original and transfers ownership.
int vertexCount(int part=0, int ring=0) const override
Returns the number of vertices of which this geometry is built.
int ringCount(int part=0) const override SIP_HOLDGIL
Returns the number of rings of which this geometry is built.
bool moveVertex(QgsVertexId position, const QgsPoint &newPos) override
Moves a vertex within the geometry.
const QgsCurve * interiorRing(int i) const SIP_HOLDGIL
Retrieves an interior ring from the curve polygon.
double perimeter() const override SIP_HOLDGIL
Returns the planar, 2-dimensional perimeter of the geometry.
QgsPoint vertexAt(QgsVertexId id) const override
Returns the point corresponding to a specified vertex id.
QVector< QgsPointSequence > QgsRingSequence
Abstract base class for all geometries.
QgsCoordinateSequence coordinateSequence() const override
Retrieves the sequence of geometries, rings and nodes.
bool is3D() const SIP_HOLDGIL
Returns true if the geometry is 3D and contains a z-value.
QString asWkt(int precision=17) const override
Returns a WKT representation of the geometry.
static QStringList wktGetChildBlocks(const QString &wkt, const QString &defaultType=QString())
Parses a WKT string and returns of list of blocks contained in the WKT.
QgsCurve * clone() const override=0
Clones the geometry by performing a deep copy.
bool fromWkt(const QString &wkt) override
Sets the geometry from a WKT string.
QgsCurvePolygon * toCurveType() const override
Returns the geometry converted to the more generic curve type.
bool moveVertex(QgsVertexId position, const QgsPoint &newPos) override
Moves a vertex within 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.
int vertexNumberFromVertexId(QgsVertexId id) const override
Returns the vertex number corresponding to a vertex id.
QgsAbstractGeometry * boundary() const override
Returns the closure of the combinatorial boundary of the geometry (ie the topological boundary of the...
QgsAbstractGeometry & operator=(const QgsAbstractGeometry &geom)
QVector< QgsPoint > QgsPointSequence
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.
virtual double segmentLength(QgsVertexId startVertex) const =0
Returns the length of the segment of the geometry which begins at startVertex.
Utility class for identifying a unique vertex within a geometry.
void forceRHR()
Forces the geometry to respect the Right-Hand-Rule, in which the area that is bounded by the polygon ...
QgsPolygon * surfaceToPolygon() const override
Gets a polygon representation of this surface.
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...
int dimension() const override SIP_HOLDGIL
Returns the inherent dimension of the geometry.
VertexType
Type of vertex.
static bool hasZ(Type type) SIP_HOLDGIL
Tests whether a WKB type contains the z-dimension.
QVector< QgsCurve * > mInteriorRings
virtual int numPoints() const =0
Returns the number of points in the curve.
QgsWkbTypes::Type readHeader() const
readHeader
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 (...
void setZMTypeFromSubGeometry(const QgsAbstractGeometry *subggeom, QgsWkbTypes::Type baseGeomType)
Updates the geometry type based on whether sub geometries contain z or m values.
void reserve(int size) SIP_HOLDGIL
Attempts to allocate memory for at least size geometries.
bool addZValue(double zValue=0) override
Adds a z-dimension to the geometry, initialized to a preset value.
QgsAbstractGeometry * segmentize(double tolerance=M_PI_2/90, SegmentationToleranceType toleranceType=MaximumAngle) const override
Returns a geometry without curves.
void ringAdjacentVertices(const QgsCurve *curve, QgsVertexId vertex, QgsVertexId &previousVertex, QgsVertexId &nextVertex)
double vertexAngle(QgsVertexId vertex) const override
Returns approximate rotation angle for a vertex.
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.
Compound curve geometry type.
double segmentLength(QgsVertexId startVertex) const override
Returns the length of the segment of the geometry which begins at startVertex.
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.
QByteArray asWkb(QgsAbstractGeometry::WkbFlags flags=QgsAbstractGeometry::WkbFlags()) const override
QgsCurvePolygon & operator=(const QgsCurvePolygon &p)
QgsPoint vertexAt(QgsVertexId id) const override
Returns the point corresponding to a specified vertex id.