29#include <QPainterPath>
30#include <QRegularExpression>
33#include <nlohmann/json.hpp>
52 else if ( std::isnan(
z ) )
54 if ( std::isnan(
m ) )
59 else if ( std::isnan(
m ) )
68 , mZ( std::numeric_limits<double>::quiet_NaN() )
69 , mM( std::numeric_limits<double>::quiet_NaN() )
74 mX = std::numeric_limits<double>::quiet_NaN();
75 mY = std::numeric_limits<double>::quiet_NaN();
82 , mZ( std::numeric_limits<double>::quiet_NaN() )
83 , mM( std::numeric_limits<double>::quiet_NaN() )
91 , mZ(
QgsWkbTypes::hasZ( wkbType ) ? z : std::numeric_limits<double>::quiet_NaN() )
92 , mM(
QgsWkbTypes::hasM( wkbType ) ? m : std::numeric_limits<double>::quiet_NaN() )
112 auto gridifyValue = [](
double value,
double spacing,
bool extraCondition = true ) ->
double
114 if ( spacing > 0 && extraCondition )
115 return std::round( value / spacing ) * spacing;
121 const auto x = gridifyValue( mX, hSpacing );
122 const auto y = gridifyValue( mY, vSpacing );
173 QString secondWithoutParentheses =
parts.second;
174 secondWithoutParentheses = secondWithoutParentheses.remove(
'(' ).remove(
')' ).simplified().remove(
' ' );
175 parts.second =
parts.second.remove(
'(' ).remove(
')' );
176 if ( (
parts.second.compare( QLatin1String(
"EMPTY" ), Qt::CaseInsensitive ) == 0 ) ||
177 secondWithoutParentheses.isEmpty() )
180 const thread_local QRegularExpression
rx( QStringLiteral(
"\\s" ) );
181#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
182 QStringList coordinates =
parts.second.split(
rx, QString::SkipEmptyParts );
184 QStringList coordinates =
parts.second.split(
rx, Qt::SkipEmptyParts );
198 const thread_local QRegularExpression rxIsNumber( QStringLiteral(
"^[+-]?(\\d\\.?\\d*[Ee][+\\-]?\\d+|(\\d+\\.\\d*|\\d*\\.\\d+)|\\d+)$" ) );
199 if ( coordinates.filter( rxIsNumber ).size() != coordinates.size() )
202 if ( coordinates.size() < 2 )
207 else if ( coordinates.size() == 3 && !
is3D() && !
isMeasure() )
213 else if ( coordinates.size() >= 4 && ( !
is3D() || !
isMeasure() ) )
222 mX = coordinates[idx++].toDouble();
223 mY = coordinates[idx++].toDouble();
224 if (
is3D() && coordinates.length() > 2 )
225 mZ = coordinates[idx++].toDouble();
227 mM = coordinates[idx++].toDouble();
240 int binarySize =
sizeof( char ) +
sizeof( quint32 );
241 binarySize += ( 2 +
is3D() +
isMeasure() ) *
sizeof(
double );
251 wkb << static_cast<quint32>(
wkbType() );
269 wkt += QLatin1String(
" EMPTY" );
272 wkt += QLatin1String(
" (" );
285 QDomElement elemPoint = doc.createElementNS( ns, QStringLiteral(
"Point" ) );
286 QDomElement elemCoordinates = doc.createElementNS( ns, QStringLiteral(
"coordinates" ) );
289 const QString cs = QStringLiteral(
"," );
291 const QString ts = QStringLiteral(
" " );
293 elemCoordinates.setAttribute( QStringLiteral(
"cs" ), cs );
294 elemCoordinates.setAttribute( QStringLiteral(
"ts" ), ts );
296 QString strCoordinates;
297 if ( axisOrder == QgsAbstractGeometry::AxisOrder::XY )
301 elemCoordinates.appendChild( doc.createTextNode( strCoordinates ) );
302 elemPoint.appendChild( elemCoordinates );
308 QDomElement elemPoint = doc.createElementNS( ns, QStringLiteral(
"Point" ) );
309 QDomElement elemPosList = doc.createElementNS( ns, QStringLiteral(
"pos" ) );
310 elemPosList.setAttribute( QStringLiteral(
"srsDimension" ),
is3D() ? 3 : 2 );
311 QString strCoordinates;
312 if ( axisOrder == QgsAbstractGeometry::AxisOrder::XY )
319 elemPosList.appendChild( doc.createTextNode( strCoordinates ) );
320 elemPoint.appendChild( elemPosList );
330 {
"coordinates", json::array() },
351 p.drawRect( QRectF( mX - 2, mY - 2, 4, 4 ) );
356 return QPainterPath();
361 mX = mY = std::numeric_limits<double>::quiet_NaN();
365 mZ = std::numeric_limits<double>::quiet_NaN();
370 mM = std::numeric_limits<double>::quiet_NaN();
413 if (
id.vertex != 0 )
468 Q_UNUSED( segmentPt )
469 Q_UNUSED( vertexAfter )
542 return rectangle.
contains( mX, mY );
573void QgsPoint::transform(
const QTransform &t,
double zTranslate,
double zScale,
double mTranslate,
double mScale )
577 t.map( mX, mY, &
x, &
y );
583 mZ = mZ * zScale + zTranslate;
587 mM = mM * mScale + mTranslate;
598 mZ = std::numeric_limits<double>::quiet_NaN();
609 mM = std::numeric_limits<double>::quiet_NaN();
630 mZ = std::numeric_limits<double>::quiet_NaN();
631 mM = std::numeric_limits<double>::quiet_NaN();
636 mM = std::numeric_limits<double>::quiet_NaN();
640 mZ = std::numeric_limits<double>::quiet_NaN();
682 double zDistSquared = 0.0;
683 if (
is3D() || !std::isnan(
z ) )
684 zDistSquared = ( mZ -
z ) * ( mZ -
z );
686 return std::sqrt( ( mX -
x ) * ( mX -
x ) + ( mY -
y ) * ( mY -
y ) + zDistSquared );
691 double zDistSquared = 0.0;
693 zDistSquared = ( mZ - other.
z() ) * ( mZ - other.
z() );
695 return std::sqrt( ( mX - other.
x() ) * ( mX - other.
x() ) + ( mY - other.
y() ) * ( mY - other.
y() ) + zDistSquared );
700 double zDistSquared = 0.0;
701 if (
is3D() || !std::isnan(
z ) )
702 zDistSquared = ( mZ -
z ) * ( mZ -
z );
704 return ( mX -
x ) * ( mX -
x ) + ( mY -
y ) * ( mY -
y ) + zDistSquared;
709 double zDistSquared = 0.0;
711 zDistSquared = ( mZ - other.
z() ) * ( mZ - other.
z() );
713 return ( mX - other.
x() ) * ( mX - other.
x() ) + ( mY - other.
y() ) * ( mY - other.
y() ) + zDistSquared;
718 const double dx = other.
x() - mX;
719 const double dy = other.
y() - mY;
720 return ( std::atan2( dx, dy ) * 180.0 / M_PI );
730 const double dz = other.
z() - mZ;
732 return ( std::acos( dz /
distance ) * 180.0 / M_PI );
738 const double radsXy =
azimuth * M_PI / 180.0;
739 double dx = 0.0, dy = 0.0, dz = 0.0;
754 dx =
distance * std::sin( radsZ ) * std::sin( radsXy );
755 dy =
distance * std::sin( radsZ ) * std::cos( radsXy );
759 return QgsPoint( mX + dx, mY + dy, mZ + dz, mM, pType );
769 return std::isnan( mX ) || std::isnan( mY );
779 return QStringLiteral(
"Point" );
794 Q_ASSERT( index == 0 );
800 const double nan = std::numeric_limits<double>::quiet_NaN();
806 const QgsPoint *otherPoint = qgsgeometry_cast< const QgsPoint * >( other );
810 if ( mX < otherPoint->mX )
814 else if ( mX > otherPoint->mX )
819 if ( mY < otherPoint->mY )
823 else if ( mY > otherPoint->mY )
828 if (
is3D() && !otherPoint->
is3D() )
830 else if ( !
is3D() && otherPoint->
is3D() )
832 else if (
is3D() && otherPoint->
is3D() )
834 if ( mZ < otherPoint->mZ )
838 else if ( mZ > otherPoint->mZ )
850 if ( mM < otherPoint->mM )
854 else if ( mM > otherPoint->mM )
WkbType
The WKB type describes the number of dimensions a geometry has.
TransformDirection
Flags for raster layer temporal capabilities.
Abstract base class for all geometries.
bool is3D() const SIP_HOLDGIL
Returns true if the geometry is 3D and contains a z-value.
AxisOrder
Axis order for GML generation.
QString wktTypeStr() const
Returns the WKT type string of the geometry.
virtual void clearCache() const
Clears any cached parameters associated with the geometry, e.g., bounding boxes.
Qgis::WkbType wkbType() const SIP_HOLDGIL
Returns the WKB type of the geometry.
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.
static endian_t endian()
Returns whether this machine uses big or little endian.
Qgis::WkbType readHeader() const
readHeader
Base class for feedback objects to be used for cancellation of something running in a worker thread.
static QPair< Qgis::WkbType, QString > wktReadBlock(const QString &wkt)
Parses a WKT block of the format "TYPE( contents )" and returns a pair of geometry type to contents (...
A class to represent a 2D point.
bool isEmpty() const SIP_HOLDGIL
Returns true if the geometry is empty.
Point geometry type, with support for z-dimension and m-values.
int wkbSize(QgsAbstractGeometry::WkbFlags flags=QgsAbstractGeometry::WkbFlags()) const override
Returns the length of the QByteArray returned by asWkb()
double distance(double x, double y) const SIP_HOLDGIL
Returns the Cartesian 2D distance between this point and a specified x, y coordinate.
bool fromWkb(QgsConstWkbPtr &wkb) override
Sets the geometry from a WKB string.
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...
QgsCoordinateSequence coordinateSequence() const override
Retrieves the sequence of geometries, rings and nodes.
double vertexAngle(QgsVertexId vertex) const override
Angle undefined.
void transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection d=Qgis::TransformDirection::Forward, bool transformZ=false) override SIP_THROW(QgsCsException)
Transforms the geometry using a coordinate transform.
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 addMValue(double mValue=0) override
Adds a measure to the geometry, initialized to a preset value.
QPainterPath asQPainterPath() const override
Returns the geometry represented as a QPainterPath.
QByteArray asWkb(QgsAbstractGeometry::WkbFlags=QgsAbstractGeometry::WkbFlags()) const override
double & rx()
Returns a reference to the x-coordinate of this point.
void clear() override
Clears the geometry, ie reset it to a null geometry.
bool fromWkt(const QString &wkt) override
Sets the geometry from a WKT string.
bool dropMValue() override
Drops any measure values which exist in the geometry.
bool insertVertex(QgsVertexId position, const QgsPoint &vertex) override
Inserts a vertex into the geometry.
bool isEmpty() const override SIP_HOLDGIL
Returns true if the geometry is empty.
void adjacentVertices(QgsVertexId vertex, QgsVertexId &previousVertex, QgsVertexId &nextVertex) const override
Returns the vertices adjacent to a specified vertex within a geometry.
bool isValid(QString &error, Qgis::GeometryValidityFlags flags=Qgis::GeometryValidityFlags()) const override SIP_HOLDGIL
Checks validity of the geometry, and returns true if the geometry is valid.
int dimension() const override SIP_HOLDGIL
Returns the inherent dimension of the geometry.
bool addZValue(double zValue=0) override
Adds a z-dimension to the geometry, initialized to a preset value.
QgsAbstractGeometry * boundary() const override
Returns the closure of the combinatorial boundary of the geometry (ie the topological boundary of the...
void draw(QPainter &p) const override
Draws the geometry using the specified QPainter.
QgsPoint * toCurveType() const override
Returns the geometry converted to the more generic curve type.
QgsPoint project(double distance, double azimuth, double inclination=90.0) const SIP_HOLDGIL
Returns a new point which corresponds to this point projected by a specified distance with specified ...
double distance3D(double x, double y, double z) const SIP_HOLDGIL
Returns the Cartesian 3D distance between this point and a specified x, y, z coordinate.
QString asWkt(int precision=17) const override
Returns a WKT representation of the geometry.
QgsPoint childPoint(int index) const override
Returns point at index (for geometries without child geometries - i.e.
bool boundingBoxIntersects(const QgsRectangle &rectangle) const override SIP_HOLDGIL
Returns true if the bounding box of this geometry intersects with a rectangle.
QgsPoint * clone() const override
Clones the geometry by performing a deep copy.
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 ringCount(int=0) const override
Returns the number of rings of which this geometry is built.
int nCoordinates() const override SIP_HOLDGIL
Returns the number of nodes contained in the geometry.
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.
QgsPoint vertexAt(QgsVertexId) const override
Returns the point corresponding to a specified vertex id.
QgsPoint * createEmptyWithSameType() const override
Creates a new geometry with the same class and same WKB type as the original and transfers ownership.
bool moveVertex(QgsVertexId position, const QgsPoint &newPos) override
Moves a vertex within the geometry.
bool deleteVertex(QgsVertexId position) override
Deletes a vertex within the geometry.
QgsRectangle boundingBox() const override SIP_HOLDGIL
Returns the minimal bounding box for the geometry.
double distanceSquared3D(double x, double y, double z) const SIP_HOLDGIL
Returns the Cartesian 3D squared distance between this point and a specified x, y,...
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.
QgsPoint * 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.
bool convertTo(Qgis::WkbType type) override
Converts the geometry to a specified type.
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...
int vertexNumberFromVertexId(QgsVertexId id) const override
Returns the vertex number corresponding to a vertex id.
int childCount() const override
Returns number of child geometries (for geometries with child geometries) or child points (for geomet...
QString geometryType() const override SIP_HOLDGIL
Returns a unique string representing the geometry type.
json asJsonObject(int precision=17) const override
Returns a json object representation of the geometry.
int vertexCount(int=0, int=0) const override
Returns the number of vertices of which this geometry is built.
void swapXy() override
Swaps the x and y coordinates from the geometry.
void normalize() final SIP_HOLDGIL
Reorganizes the geometry into a normalized form (or "canonical" form).
double azimuth(const QgsPoint &other) const SIP_HOLDGIL
Calculates Cartesian azimuth between this point and other one (clockwise in degree,...
double segmentLength(QgsVertexId startVertex) const override
Returns the length of the segment of the geometry which begins at startVertex.
bool dropZValue() override
Drops any z-dimensions which exist in the geometry.
QgsPoint(double x=std::numeric_limits< double >::quiet_NaN(), double y=std::numeric_limits< double >::quiet_NaN(), double z=std::numeric_limits< double >::quiet_NaN(), double m=std::numeric_limits< double >::quiet_NaN(), Qgis::WkbType wkbType=Qgis::WkbType::Unknown)
Construct a point with the provided initial coordinate values.
bool nextVertex(QgsVertexId &id, QgsPoint &vertex) const override
Returns next vertex id and coordinates.
int partCount() const override
Returns count of parts contained in the geometry.
double inclination(const QgsPoint &other) const SIP_HOLDGIL
Calculates Cartesian inclination between this point and other one (starting from zenith = 0 to nadir ...
QString asKml(int precision=17) const override
Returns a KML 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...
A rectangle specified with double values.
bool contains(const QgsRectangle &rect) const SIP_HOLDGIL
Returns true when rectangle contains other rectangle.
Handles storage of information regarding WKB types and their properties.
static Qgis::WkbType addZ(Qgis::WkbType type) SIP_HOLDGIL
Adds the z dimension to a WKB type and returns the new type.
static Qgis::WkbType addM(Qgis::WkbType type) SIP_HOLDGIL
Adds the m dimension to a WKB type and returns the new type.
static bool hasZ(Qgis::WkbType type) SIP_HOLDGIL
Tests whether a WKB type contains the z-dimension.
static Qgis::WkbType dropZ(Qgis::WkbType type) SIP_HOLDGIL
Drops the z dimension (if present) for a WKB type and returns the new type.
static Qgis::WkbType dropM(Qgis::WkbType type) SIP_HOLDGIL
Drops the m dimension (if present) for a WKB type and returns the new type.
static bool hasM(Qgis::WkbType type) SIP_HOLDGIL
Tests whether a WKB type contains m values.
static Qgis::WkbType flatType(Qgis::WkbType type) SIP_HOLDGIL
Returns the flat type for a WKB 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 ...
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
double qgsRound(double number, int places)
Returns a double number, rounded (as close as possible) to the specified number of places.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
QVector< QgsRingSequence > QgsCoordinateSequence
QVector< QgsPointSequence > QgsRingSequence
QVector< QgsPoint > QgsPointSequence
Utility class for identifying a unique vertex within a geometry.