QGIS API Documentation
3.14.0-Pi (9f7028fd23)
|
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 if (
parts.second.compare( QLatin1String(
"EMPTY" ), Qt::CaseInsensitive ) == 0 )
221 QString defaultChildWkbType = QStringLiteral(
"LineString%1%2" ).arg(
is3D() ? QStringLiteral(
"Z" ) : QString(),
isMeasure() ? QStringLiteral(
"M" ) : QString() );
224 for (
const QString &childWkt : blocks )
266 hasZ = hasZ || curve->is3D();
267 hasM = hasM || curve->isMeasure();
290 int binarySize =
sizeof( char ) +
sizeof( quint32 ) +
sizeof( quint32 );
291 QVector<QByteArray> wkbForRings;
296 binarySize += wkb.length();
301 QByteArray wkb( curve->asWkb( flags ) );
302 binarySize += wkb.length();
307 wkbArray.resize( binarySize );
310 wkbPtr << static_cast<quint32>(
wkbType() );
311 wkbPtr << static_cast<quint32>( wkbForRings.count() );
312 for (
const QByteArray &wkb : qgis::as_const( wkbForRings ) )
324 wkt += QStringLiteral(
" EMPTY" );
327 wkt += QLatin1String(
" (" );
331 if ( qgsgeometry_cast<QgsLineString *>(
mExteriorRing.get() ) )
334 childWkt = childWkt.mid( childWkt.indexOf(
'(' ) );
336 wkt += childWkt +
',';
340 QString childWkt = curve->asWkt(
precision );
341 if ( qgsgeometry_cast<const QgsLineString *>( curve ) )
344 childWkt = childWkt.mid( childWkt.indexOf(
'(' ) );
346 wkt += childWkt +
',';
348 if ( wkt.endsWith(
',' ) )
360 QDomElement elemPolygon = doc.createElementNS( ns, QStringLiteral(
"Polygon" ) );
365 QDomElement elemOuterBoundaryIs = doc.createElementNS( ns, QStringLiteral(
"outerBoundaryIs" ) );
366 std::unique_ptr< QgsLineString > exteriorLineString(
exteriorRing()->curveToLine() );
367 QDomElement outerRing = exteriorLineString->asGml2( doc,
precision, ns, axisOrder );
368 outerRing.toElement().setTagName( QStringLiteral(
"LinearRing" ) );
369 elemOuterBoundaryIs.appendChild( outerRing );
370 elemPolygon.appendChild( elemOuterBoundaryIs );
371 std::unique_ptr< QgsLineString > interiorLineString;
374 QDomElement elemInnerBoundaryIs = doc.createElementNS( ns, QStringLiteral(
"innerBoundaryIs" ) );
375 interiorLineString.reset(
interiorRing( i )->curveToLine() );
376 QDomElement innerRing = interiorLineString->asGml2( doc,
precision, ns, axisOrder );
377 innerRing.toElement().setTagName( QStringLiteral(
"LinearRing" ) );
378 elemInnerBoundaryIs.appendChild( innerRing );
379 elemPolygon.appendChild( elemInnerBoundaryIs );
386 QDomElement elemCurvePolygon = doc.createElementNS( ns, QStringLiteral(
"Polygon" ) );
389 return elemCurvePolygon;
391 QDomElement elemExterior = doc.createElementNS( ns, QStringLiteral(
"exterior" ) );
393 if ( curveElem.tagName() == QLatin1String(
"LineString" ) )
395 curveElem.setTagName( QStringLiteral(
"LinearRing" ) );
397 elemExterior.appendChild( curveElem );
398 elemCurvePolygon.appendChild( elemExterior );
402 QDomElement elemInterior = doc.createElementNS( ns, QStringLiteral(
"interior" ) );
404 if ( innerRing.tagName() == QLatin1String(
"LineString" ) )
406 innerRing.setTagName( QStringLiteral(
"LinearRing" ) );
408 elemInterior.appendChild( innerRing );
409 elemCurvePolygon.appendChild( elemInterior );
411 return elemCurvePolygon;
416 json coordinates( json::array( ) );
419 std::unique_ptr< QgsLineString > exteriorLineString(
exteriorRing()->curveToLine() );
421 exteriorLineString->points( exteriorPts );
424 std::unique_ptr< QgsLineString > interiorLineString;
427 interiorLineString.reset(
interiorRing( i )->curveToLine() );
429 interiorLineString->points( interiorPts );
435 {
"type",
"Polygon" },
436 {
"coordinates", coordinates }
443 kml.append( QLatin1String(
"<Polygon>" ) );
446 kml.append( QLatin1String(
"<outerBoundaryIs>" ) );
448 kml.append( QLatin1String(
"</outerBoundaryIs>" ) );
451 for (
const QgsCurve *ring : interiorRings )
453 kml.append( QLatin1String(
"<innerBoundaryIs>" ) );
455 kml.append( QLatin1String(
"</innerBoundaryIs>" ) );
457 kml.append( QLatin1String(
"</Polygon>" ) );
468 double totalArea = 0.0;
474 totalArea += std::fabs(
area );
480 if ( ring->isRing() )
482 ring->sumUpArea(
area );
483 totalArea -= std::fabs(
area );
505 std::unique_ptr< QgsPolygon > polygon(
new QgsPolygon() );
507 return polygon.release();
509 polygon->setExteriorRing(
exteriorRing()->curveToLine() );
510 QVector<QgsCurve *> interiors;
512 interiors.reserve( n );
513 for (
int i = 0; i < n; ++i )
517 polygon->setInteriorRings( interiors );
518 return polygon.release();
534 multiCurve->
reserve( nInteriorRings + 1 );
536 for (
int i = 0; i < nInteriorRings; ++i )
553 auto exterior = std::unique_ptr<QgsCurve> {
static_cast< QgsCurve *
>(
mExteriorRing->snappedToGrid( hSpacing, vSpacing, dSpacing, mSpacing ) ) };
558 polygon->mExteriorRing = std::move( exterior );
566 QgsCurve *gridifiedInterior =
static_cast< QgsCurve *
>( interior->snappedToGrid( hSpacing, vSpacing, dSpacing, mSpacing ) );
568 if ( !gridifiedInterior )
571 polygon->mInteriorRings.append( gridifiedInterior );
574 return polygon.release();
581 auto cleanRing = [epsilon, useZValues ](
QgsCurve * ring )->
bool
583 if ( ring->numPoints() <= 4 )
586 if ( ring->removeDuplicateNodes( epsilon, useZValues ) )
590 ring->pointAt( 0, startPoint, type );
604 if ( cleanRing( ring ) ) result =
true;
611 std::unique_ptr< QgsPolygon > poly(
new QgsPolygon() );
614 return poly.release();
617 poly->setExteriorRing(
mExteriorRing->curveToLine( tolerance, toleranceType ) );
619 QVector<QgsCurve *> rings;
623 rings.push_back( ring->curveToLine( tolerance, toleranceType ) );
625 poly->setInteriorRings( rings );
626 return poly.release();
684 else if ( !ring->
is3D() )
709 for (
int ringIndex =
mInteriorRings.size() - 1; ringIndex >= 0; --ringIndex )
711 if ( minimumAllowedArea < 0 )
717 if (
area < minimumAllowedArea )
727 QVector<QgsCurve *> validRings;
731 if ( !curve->isRing() )
749 std::unique_ptr< QgsCurve > flipped(
mExteriorRing->reversed() );
753 QVector<QgsCurve *> validRings;
760 validRings << flipped;
787 ring->addToPainterPath( path );
802 curve->transform( ct, d, transformZ );
811 mExteriorRing->transform( t, zTranslate, zScale, mTranslate, mScale );
816 curve->transform( t, zTranslate, zScale, mTranslate, mScale );
835 ring->points( sequence.back().back() );
852 count += ring->nCoordinates();
863 if (
id.ring < 0 || id.ring >=
ringCount() )
878 if (
id.ring == i + 1 )
881 if ( partNumber == -1 )
883 return number + partNumber;
907 QVector<QgsCurve *> segmentList;
959 if ( vertex.
vertex == 0 && n < 3 )
963 else if ( vertex.
vertex == 0 )
971 if ( vertex.
vertex == n - 1 && n < 3 )
975 else if ( vertex.
vertex == n - 1 )
994 if ( vertex.
ring == 0 )
1022 else if ( vId.
vertex == n )
1039 bool success = ring->
moveVertex( vId, newPos );
1045 else if ( vId.
vertex == n - 1 )
1064 if ( vId.
ring == 0 )
1088 else if ( vId.
vertex == n - 1 )
1104 if ( ring->hasCurvedSegments() )
1114 return toPolygon( tolerance, toleranceType );
1171 curve->addZValue( zValue );
1188 curve->addMValue( mValue );
1204 curve->dropZValue();
1220 curve->dropMValue();
1249 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.
QgsWkbTypes::Type wkbType() const
Returns the WKB type of the geometry.
virtual bool dropZValue()=0
Drops any z-dimensions which exist in the geometry.
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.
void reserve(int size)
Attempts to allocate memory for at least size geometries.
virtual QgsCurve * reversed() const =0
Returns a reversed copy of the curve, where the direction of the curve has been flipped.
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.
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.
const QgsCurve * exteriorRing() const
Returns the curve polygon's exterior ring.
QString wktTypeStr() const
Returns the WKT type string of the geometry.
void swapXy() override
Swaps the x and y coordinates from the geometry.
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.
static bool hasZ(Type type)
Tests whether a WKB type contains the z-dimension.
SegmentationToleranceType
Segmentation tolerance as maximum angle or maximum difference between approximation and circle.
QgsWkbTypes::Type mWkbType
static Type dropM(Type type)
Drops the m dimension (if present) for a WKB type and returns the new type.
Line string geometry type, with support for z-dimension and m-values.
static Type addM(Type type)
Adds the m dimension to a WKB type and returns the new type.
const QgsCurve * interiorRing(int i) const
Retrieves an interior ring from the curve polygon.
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 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...
double area() const override
Returns the planar, 2-dimensional area of the geometry.
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.
int dimension() const override
Returns the inherent dimension of the geometry.
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.
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.
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.
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.
bool dropMValue() override
Drops any measure values which exist in the geometry.
static GeometryType geometryType(Type type)
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
bool operator==(const QgsAbstractGeometry &other) const override
static endian_t endian()
Returns whether this machine uses big or little endian.
int partCount() const override
Returns count of parts contained in the geometry.
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
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.
bool moveVertex(QgsVertexId position, const QgsPoint &newPos) override
Moves a vertex within the geometry.
QgsPoint vertexAt(QgsVertexId id) const override
Returns the point corresponding to a specified vertex id.
QVector< QgsPointSequence > QgsRingSequence
bool isEmpty() const override
Returns true if the geometry is empty.
Abstract base class for all geometries.
QgsCoordinateSequence coordinateSequence() const override
Retrieves the sequence of geometries, rings and nodes.
double perimeter() const override
Returns the planar, 2-dimensional perimeter of the geometry.
QString asWkt(int precision=17) const override
Returns a WKT representation of the geometry.
static Type addZ(Type type)
Adds the z dimension to a WKB type and returns the new type.
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.
static bool hasM(Type type)
Tests whether a WKB type contains m values.
int numInteriorRings() const
Returns the number of interior rings contained with the curve polygon.
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.
int ringCount(int part=0) const override
Returns the number of rings of which this geometry is built.
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 ...
QString geometryType() const override
Returns a unique string representing the geometry type.
QgsPolygon * surfaceToPolygon() const override
Gets a polygon representation of this surface.
bool isMeasure() const
Returns true if the geometry contains m values.
QVector< QgsCurve * > mInteriorRings
static Type dropZ(Type type)
Drops the z dimension (if present) for a WKB type and returns the new type.
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.
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.
static Type flatType(Type type)
Returns the flat type for a WKB type.
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.