22#include <nlohmann/json.hpp>
34#include <QPainterPath>
35#include <QRegularExpression>
38#include "moc_qgspoint.cpp"
40using namespace Qt::StringLiterals;
59 else if ( std::isnan(
z ) )
61 if ( std::isnan(
m ) )
66 else if ( std::isnan(
m ) )
75 , mZ( std::numeric_limits<double>::quiet_NaN() )
76 , mM( std::numeric_limits<double>::quiet_NaN() )
81 mX = std::numeric_limits<double>::quiet_NaN();
82 mY = std::numeric_limits<double>::quiet_NaN();
89 , mZ( std::numeric_limits<double>::quiet_NaN() )
90 , mM( std::numeric_limits<double>::quiet_NaN() )
146 auto gridifyValue = [](
double value,
double spacing,
bool extraCondition = true ) ->
double {
147 if ( spacing > 0 && extraCondition )
148 return std::round( value / spacing ) * spacing;
154 const auto x = gridifyValue( mX, hSpacing );
155 const auto y = gridifyValue( mY, vSpacing );
211 QString secondWithoutParentheses =
parts.second;
212 secondWithoutParentheses = secondWithoutParentheses.remove(
'(' ).remove(
')' ).simplified().remove(
' ' );
213 parts.second =
parts.second.remove(
'(' ).remove(
')' );
214 if ( (
parts.second.compare(
"EMPTY"_L1, Qt::CaseInsensitive ) == 0 ) || secondWithoutParentheses.isEmpty() )
217 const thread_local QRegularExpression
rx( u
"\\s"_s );
218 QStringList coordinates =
parts.second.split(
rx, Qt::SkipEmptyParts );
231 const thread_local QRegularExpression rxIsNumber( u
"^[+-]?(\\d\\.?\\d*[Ee][+\\-]?\\d+|(\\d+\\.\\d*|\\d*\\.\\d+)|\\d+)$"_s );
232 if ( coordinates.filter( rxIsNumber ).size() != coordinates.size() )
235 if ( coordinates.size() < 2 )
240 else if ( coordinates.size() == 3 && !
is3D() && !
isMeasure() )
246 else if ( coordinates.size() >= 4 && ( !
is3D() || !
isMeasure() ) )
255 mX = coordinates[idx++].toDouble();
256 mY = coordinates[idx++].toDouble();
257 if (
is3D() && coordinates.length() > 2 )
258 mZ = coordinates[idx++].toDouble();
260 mM = coordinates[idx++].toDouble();
273 int binarySize =
sizeof( char ) +
sizeof( quint32 );
274 binarySize += ( 2 +
is3D() +
isMeasure() ) *
sizeof(
double );
284 wkb << static_cast<quint32>(
wkbType() );
318 QDomElement elemPoint = doc.createElementNS( ns, u
"Point"_s );
319 QDomElement elemCoordinates = doc.createElementNS( ns, u
"coordinates"_s );
322 const QString cs = u
","_s;
324 const QString ts = u
" "_s;
326 elemCoordinates.setAttribute( u
"cs"_s, cs );
327 elemCoordinates.setAttribute( u
"ts"_s, ts );
329 QString strCoordinates;
334 elemCoordinates.appendChild( doc.createTextNode( strCoordinates ) );
335 elemPoint.appendChild( elemCoordinates );
341 QDomElement elemPoint = doc.createElementNS( ns, u
"Point"_s );
342 QDomElement elemPosList = doc.createElementNS( ns, u
"pos"_s );
343 elemPosList.setAttribute( u
"srsDimension"_s,
is3D() ? 3 : 2 );
344 QString strCoordinates;
352 elemPosList.appendChild( doc.createTextNode( strCoordinates ) );
353 elemPoint.appendChild( elemPosList );
362 {
"coordinates", json::array() },
366 j[
"coordinates"].push_back(
qgsRound( mX, precision ) );
367 j[
"coordinates"].push_back(
qgsRound( mY, precision ) );
370 j[
"coordinates"].push_back(
qgsRound( mZ, precision ) );
374 j[
"coordinates"].push_back(
qgsRound( mM, precision ) );
387 p.drawRect( QRectF( mX - 2, mY - 2, 4, 4 ) );
392 return QPainterPath();
397 mX = mY = std::numeric_limits<double>::quiet_NaN();
401 mZ = std::numeric_limits<double>::quiet_NaN();
406 mM = std::numeric_limits<double>::quiet_NaN();
449 if (
id.vertex != 0 )
503 Q_UNUSED( positions )
509 return position.
part == 0 && position.
ring == 0 && position.
vertex == 0;
515 Q_UNUSED( segmentPt )
516 Q_UNUSED( vertexAfter )
589 return rectangle.
contains( mX, mY );
594 return box3d.
contains( mX, mY, mZ );
625void QgsPoint::transform(
const QTransform &t,
double zTranslate,
double zScale,
double mTranslate,
double mScale )
629 t.map( mX, mY, &
x, &
y );
635 mZ = mZ * zScale + zTranslate;
639 mM = mM * mScale + mTranslate;
650 mZ = std::numeric_limits<double>::quiet_NaN();
661 mM = std::numeric_limits<double>::quiet_NaN();
682 mZ = std::numeric_limits<double>::quiet_NaN();
683 mM = std::numeric_limits<double>::quiet_NaN();
688 mM = std::numeric_limits<double>::quiet_NaN();
692 mZ = std::numeric_limits<double>::quiet_NaN();
744 const double dz = other.
z() - mZ;
746 return ( std::acos( dz /
distance ) * 180.0 / M_PI );
752 const double radsXy =
azimuth * M_PI / 180.0;
753 double dx = 0.0, dy = 0.0, dz = 0.0;
768 dx =
distance * std::sin( radsZ ) * std::sin( radsXy );
769 dy =
distance * std::sin( radsZ ) * std::cos( radsXy );
773 return QgsPoint( mX + dx, mY + dy, mZ + dz, mM, pType );
783 return std::isnan( mX ) || std::isnan( mY );
788 return QgsBox3D( mX, mY, mZ, mX, mY, mZ );
808 Q_ASSERT( index == 0 );
814 const double nan = std::numeric_limits<double>::quiet_NaN();
824 if ( mX < otherPoint->mX )
828 else if ( mX > otherPoint->mX )
833 if ( mY < otherPoint->mY )
837 else if ( mY > otherPoint->mY )
842 if (
is3D() && !otherPoint->
is3D() )
844 else if ( !
is3D() && otherPoint->
is3D() )
846 else if (
is3D() && otherPoint->
is3D() )
848 if ( mZ < otherPoint->mZ )
852 else if ( mZ > otherPoint->mZ )
864 if ( mM < otherPoint->mM )
868 else if ( mM > otherPoint->mM )
QFlags< GeometryValidityFlag > GeometryValidityFlags
Geometry validity flags.
GeoJsonProfile
GeoJson export Profile according to OGC Features and Geometries JSON - Part 1: Core https://docs....
@ JsonFg
GeoJson profile from OGC Features and Geometries JSON Part 1: core "http://www.opengis....
@ JsonFgPlus
GeoJson profile from OGC Features and Geometries JSON Part 1: core "http://www.opengis....
WkbType
The WKB type describes the number of dimensions a geometry has.
TransformDirection
Indicates the direction (forward or inverse) of a transform.
bool isMeasure() const
Returns true if the geometry contains m values.
QFlags< WkbFlag > WkbFlags
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
AxisOrder
Axis order for GML generation.
@ XY
X comes before Y (or lon before lat).
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
Returns the WKB type of the geometry.
QgsAbstractGeometry()=default
QgsGeometryConstPartIterator parts() const
Returns Java-style iterator for traversal of parts of the geometry.
static endian_t endian()
Returns whether this machine uses big or little endian.
A 3-dimensional box composed of x, y, z coordinates.
bool contains(const QgsBox3D &other) const
Returns true when box contains other box.
Qgis::WkbType readHeader() const
readHeader
Base class for feedback objects to be used for cancellation of something running in a worker thread.
static double azimuth(double x1, double y1, double x2, double y2)
Calculates Cartesian azimuth between points (x1, y1) and (x2, y2) (clockwise in degree,...
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 (...
bool isEmpty() const
Returns true if the geometry is empty.
int wkbSize(QgsAbstractGeometry::WkbFlags flags=QgsAbstractGeometry::WkbFlags()) const override
Returns the length of the QByteArray returned by asWkb().
QgsBox3D boundingBox3D() const override
Returns the 3D bounding box for the geometry.
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...
double inclination(const QgsPoint &other) const
Calculates Cartesian inclination between this point and other one (starting from zenith = 0 to nadir ...
QgsCoordinateSequence coordinateSequence() const override
Retrieves the sequence of geometries, rings and nodes.
double vertexAngle(QgsVertexId vertex) const override
Angle undefined.
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
Returns a WKB representation of the geometry.
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 boundingBoxIntersects(const QgsRectangle &rectangle) const override
Returns true if the bounding box of this geometry intersects with a rectangle.
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.
double azimuth(const QgsPoint &other) const
Calculates Cartesian azimuth between this point and other one (clockwise in degree,...
void adjacentVertices(QgsVertexId vertex, QgsVertexId &previousVertex, QgsVertexId &nextVertex) const override
Returns the vertices adjacent to a specified vertex within a 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 * simplifyByDistance(double tolerance) const override
Simplifies the geometry by applying the Douglas Peucker simplification by distance algorithm.
bool deleteVertices(const QSet< QgsVertexId > &positions) override
Deletes vertices within the geometry.
QgsPoint * snappedToGrid(double hSpacing, double vSpacing, double dSpacing=0, double mSpacing=0, bool removeRedundantPoints=false) const override
Makes a new geometry with all the points or vertices snapped to the closest point of the grid.
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 isValid(QString &error, Qgis::GeometryValidityFlags flags=Qgis::GeometryValidityFlags()) const override
Checks validity of the geometry, and returns true if the geometry is valid.
int dimension() const override
Returns the inherent dimension of the geometry.
QgsPoint * clone() const override
Clones the geometry by performing a deep copy.
json asJsonObject(int precision=17, Qgis::GeoJsonProfile profile=Qgis::GeoJsonProfile::Legacy) const override
Returns a json object representation of the geometry with the given precision and profile.
double distance3D(double x, double y, double z) const
Returns the Cartesian 3D distance between this point and a specified x, y, z coordinate.
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.
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 hasVertex(QgsVertexId position) const override
Returns true if the geometry contains a vertex matching the given position.
bool moveVertex(QgsVertexId position, const QgsPoint &newPos) override
Moves a vertex within the geometry.
void normalize() final
Reorganizes the geometry into a normalized form (or "canonical" form).
bool deleteVertex(QgsVertexId position) override
Deletes a vertex within 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.
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...
bool isEmpty() const override
Returns true if the geometry is empty.
void transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection d=Qgis::TransformDirection::Forward, bool transformZ=false) override
Transforms the geometry using a coordinate transform.
double distance(double x, double y) const
Returns the Cartesian 2D distance between this point and a specified x, y coordinate.
int vertexCount(int=0, int=0) const override
Returns the number of vertices of which this geometry is built.
int nCoordinates() const override
Returns the number of nodes contained in the geometry.
void swapXy() override
Swaps the x and y coordinates from the geometry.
QString geometryType() const override
Returns a unique string representing the geometry type.
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 project(double distance, double azimuth, double inclination=90.0) const
Returns a new point which corresponds to this point projected by a specified distance with specified ...
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.
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
Returns true when rectangle contains other rectangle.
A 3D vector (similar to QVector3D) with the difference that it uses double precision instead of singl...
Handles storage of information regarding WKB types and their properties.
static Qgis::WkbType dropM(Qgis::WkbType type)
Drops the m dimension (if present) for a WKB type and returns the new type.
static Qgis::WkbType zmType(Qgis::WkbType type, bool hasZ, bool hasM)
Returns the modified input geometry type according to hasZ / hasM.
static Qgis::WkbType dropZ(Qgis::WkbType type)
Drops the z dimension (if present) for a WKB type and returns the new type.
static Qgis::WkbType addM(Qgis::WkbType type)
Adds the m dimension to a WKB type and returns the new type.
static Qgis::WkbType addZ(Qgis::WkbType type)
Adds the z dimension to a WKB type and returns the new type.
static Q_INVOKABLE bool hasZ(Qgis::WkbType type)
Tests whether a WKB type contains the z-dimension.
static Q_INVOKABLE bool hasM(Qgis::WkbType type)
Tests whether a WKB type contains m values.
static Qgis::WkbType flatType(Qgis::WkbType type)
Returns the flat type for a WKB type.
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).
T qgsgeometry_cast(QgsAbstractGeometry *geom)
QVector< QgsRingSequence > QgsCoordinateSequence
QVector< QgsPointSequence > QgsRingSequence
QVector< QgsPoint > QgsPointSequence
Utility class for identifying a unique vertex within a geometry.