28 #include <QRegularExpression> 29 #include <QJsonObject> 31 #include <nlohmann/json.hpp> 50 else if ( std::isnan( z ) )
52 if ( std::isnan( m ) )
57 else if ( std::isnan( m ) )
66 , mZ( std::numeric_limits<double>::quiet_NaN() )
67 , mM( std::numeric_limits<double>::quiet_NaN() )
75 , mZ( std::numeric_limits<double>::quiet_NaN() )
76 , mM( std::numeric_limits<double>::quiet_NaN() )
84 , mZ(
QgsWkbTypes::hasZ( wkbType ) ? z : std::numeric_limits<double>::quiet_NaN() )
85 , mM(
QgsWkbTypes::hasM( wkbType ) ? m : std::numeric_limits<double>::quiet_NaN() )
105 auto gridifyValue = [](
double value,
double spacing,
bool extraCondition = true ) ->
double 107 if ( spacing > 0 && extraCondition )
108 return std::round( value / spacing ) * spacing;
114 auto x = gridifyValue( mX, hSpacing );
115 auto y = gridifyValue( mY, vSpacing );
166 QRegularExpression
rx( QStringLiteral(
"\\s" ) );
167 QStringList coordinates = parts.second.split( rx, QString::SkipEmptyParts );
168 if ( coordinates.size() < 2 )
173 else if ( coordinates.size() == 3 && !
is3D() && !
isMeasure() )
179 else if ( coordinates.size() >= 4 && ( !
is3D() || !
isMeasure() ) )
188 mX = coordinates[idx++].toDouble();
189 mY = coordinates[idx++].toDouble();
190 if (
is3D() && coordinates.length() > 2 )
191 mZ = coordinates[idx++].toDouble();
193 mM = coordinates[idx++].toDouble();
206 int binarySize =
sizeof( char ) +
sizeof( quint32 );
207 binarySize += ( 2 +
is3D() +
isMeasure() ) *
sizeof(
double );
210 wkbArray.resize( binarySize );
213 wkb << static_cast<quint32>(
wkbType() );
228 QString wkt =
wktTypeStr() + QLatin1String(
" (" );
240 QDomElement elemPoint = doc.createElementNS( ns, QStringLiteral(
"Point" ) );
241 QDomElement elemCoordinates = doc.createElementNS( ns, QStringLiteral(
"coordinates" ) );
244 QString cs = QStringLiteral(
"," );
246 QString ts = QStringLiteral(
" " );
248 elemCoordinates.setAttribute( QStringLiteral(
"cs" ), cs );
249 elemCoordinates.setAttribute( QStringLiteral(
"ts" ), ts );
251 QString strCoordinates;
252 if ( axisOrder == QgsAbstractGeometry::AxisOrder::XY )
256 elemCoordinates.appendChild( doc.createTextNode( strCoordinates ) );
257 elemPoint.appendChild( elemCoordinates );
263 QDomElement elemPoint = doc.createElementNS( ns, QStringLiteral(
"Point" ) );
264 QDomElement elemPosList = doc.createElementNS( ns, QStringLiteral(
"pos" ) );
265 elemPosList.setAttribute( QStringLiteral(
"srsDimension" ),
is3D() ? 3 : 2 );
266 QString strCoordinates;
267 if ( axisOrder == QgsAbstractGeometry::AxisOrder::XY )
274 elemPosList.appendChild( doc.createTextNode( strCoordinates ) );
275 elemPoint.appendChild( elemPosList );
285 {
"coordinates", {
qgsRound( mX, precision ),
qgsRound( mY, precision ) } },
289 j[
"coordinates"].push_back(
qgsRound( mZ, precision ) );
296 p.drawRect( QRectF( mX - 2, mY - 2, 4, 4 ) );
305 mZ = std::numeric_limits<double>::quiet_NaN();
310 mM = std::numeric_limits<double>::quiet_NaN();
353 if (
id.vertex != 0 )
408 Q_UNUSED( segmentPt )
409 Q_UNUSED( vertexAfter )
508 void QgsPoint::transform(
const QTransform &t,
double zTranslate,
double zScale,
double mTranslate,
double mScale )
512 t.map( mX, mY, &x, &y );
518 mZ = mZ * zScale + zTranslate;
522 mM = mM * mScale + mTranslate;
533 mZ = std::numeric_limits<double>::quiet_NaN();
544 mM = std::numeric_limits<double>::quiet_NaN();
565 mZ = std::numeric_limits<double>::quiet_NaN();
566 mM = std::numeric_limits<double>::quiet_NaN();
571 mM = std::numeric_limits<double>::quiet_NaN();
575 mZ = std::numeric_limits<double>::quiet_NaN();
607 double zDistSquared = 0.0;
608 if (
is3D() || !std::isnan( z ) )
609 zDistSquared = ( mZ -
z ) * ( mZ - z );
611 return std::sqrt( ( mX - x ) * ( mX - x ) + ( mY - y ) * ( mY - y ) + zDistSquared );
616 double zDistSquared = 0.0;
618 zDistSquared = ( mZ - other.
z() ) * ( mZ - other.
z() );
620 return std::sqrt( ( mX - other.
x() ) * ( mX - other.
x() ) + ( mY - other.
y() ) * ( mY - other.
y() ) + zDistSquared );
625 double zDistSquared = 0.0;
626 if (
is3D() || !std::isnan( z ) )
627 zDistSquared = ( mZ -
z ) * ( mZ - z );
629 return ( mX - x ) * ( mX -
x ) + ( mY - y ) * ( mY -
y ) + zDistSquared;
634 double zDistSquared = 0.0;
636 zDistSquared = ( mZ - other.
z() ) * ( mZ - other.
z() );
638 return ( mX - other.
x() ) * ( mX - other.
x() ) + ( mY - other.
y() ) * ( mY - other.
y() ) + zDistSquared;
643 double dx = other.
x() - mX;
644 double dy = other.
y() - mY;
645 return ( std::atan2( dx, dy ) * 180.0 / M_PI );
655 double dz = other.
z() - mZ;
657 return ( std::acos( dz / distance ) * 180.0 / M_PI );
663 double radsXy = azimuth * M_PI / 180.0;
664 double dx = 0.0, dy = 0.0, dz = 0.0;
666 inclination = std::fmod( inclination, 360.0 );
673 dx = distance * std::sin( radsXy );
674 dy = distance * std::cos( radsXy );
678 double radsZ = inclination * M_PI / 180.0;
679 dx = distance * std::sin( radsZ ) * std::sin( radsXy );
680 dy = distance * std::sin( radsZ ) * std::cos( radsXy );
681 dz = distance * std::cos( radsZ );
684 return QgsPoint( mX + dx, mY + dy, mZ + dz, mM, pType );
699 return QStringLiteral(
"Point" );
714 Q_ASSERT( index == 0 );
720 double nan = std::numeric_limits<double>::quiet_NaN();
bool isMeasure() const
Returns true if the geometry contains m values.
void swapXy() override
Swaps the x and y coordinates from the geometry.
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.
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.
A rectangle specified with double values.
double segmentLength(QgsVertexId startVertex) const override
Returns the length of the segment of the geometry which begins at startVertex.
int nCoordinates() const override
Returns the number of nodes contained in 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 addZValue(double zValue=0) override
Adds a z-dimension to the geometry, initialized to a preset value.
bool fromWkb(QgsConstWkbPtr &wkb) override
Sets the geometry from a WKB string.
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 (...
int dimension() const override
Returns the inherent dimension of the geometry.
QgsRectangle boundingBox() const override
Returns the minimal bounding box for the geometry.
QVector< QgsRingSequence > QgsCoordinateSequence
QgsPoint * toCurveType() const override
Returns the geometry converted to the more generic curve type.
Handles storage of information regarding WKB types and their properties.
double vertexAngle(QgsVertexId vertex) const override
Angle undefined.
double distance(double x, double y) const
Returns the distance between this point and a specified x, y coordinate.
double azimuth(const QgsPoint &other) const
Calculates azimuth between this point and other one (clockwise in degree, starting from north) ...
A class to represent a 2D point.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
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...
bool isValid(QString &error, int flags=0) const override
Checks validity of the geometry, and returns true if the geometry is valid.
static endian_t endian()
Returns whether this machine uses big or little endian.
double inclination(const QgsPoint &other) const
Calculates inclination between this point and other one (starting from zenith = 0 to nadir = 180...
void clear() override
Clears the geometry, ie reset it to a null geometry.
static bool hasZ(Type type)
Tests whether a WKB type contains the z-dimension.
static Type dropM(Type type)
Drops the m dimension (if present) for a WKB type and returns the new type.
QgsPoint vertexAt(QgsVertexId) const override
Returns the point corresponding to a specified vertex id.
QgsWkbTypes::Type mWkbType
double y() const
Returns the point's y-coordinate.
bool dropZValue() override
Drops any z-dimensions which exist in the geometry.
bool moveVertex(QgsVertexId position, const QgsPoint &newPos) override
Moves a vertex within the geometry.
QString wktTypeStr() const
Returns the WKT type string of the geometry.
Type
The WKB type describes the number of dimensions a geometry has.
bool insertVertex(QgsVertexId position, const QgsPoint &vertex) override
Inserts a vertex into the geometry.
bool dropMValue() override
Drops any measure values which exist in the geometry.
virtual void clearCache() const
Clears any cached parameters associated with the geometry, e.g., bounding boxes.
static Type addM(Type type)
Adds the m dimension to a WKB type and returns the new type.
QgsGeometryConstPartIterator parts() const
Returns Java-style iterator for traversal of parts of the geometry.
Utility class for identifying a unique vertex within a geometry.
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
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.
QgsPoint * clone() const override
Clones the geometry by performing a deep copy.
QgsPoint * createEmptyWithSameType() const override
Creates a new geometry with the same class and same WKB type as the original and transfers ownership...
double & rx()
Returns a reference to the x-coordinate of this point.
Abstract base class for all geometries.
json asJsonObject(int precision=17) const override
Returns a json object representation of the geometry.
bool nextVertex(QgsVertexId &id, QgsPoint &vertex) const override
Returns next vertex id and coordinates.
double distanceSquared3D(double x, double y, double z) const
Returns the 3D squared distance between this point a specified x, y, z coordinate.
void adjacentVertices(QgsVertexId vertex, QgsVertexId &previousVertex, QgsVertexId &nextVertex) const override
Returns the vertices adjacent to a specified vertex within a geometry.
QgsWkbTypes::Type wkbType() const
Returns the WKB type of the geometry.
QgsPoint project(double distance, double azimuth, double inclination=90.0) const
Returns a new point which correspond to this point projected by a specified distance with specified a...
Point geometry type, with support for z-dimension and m-values.
int ringCount(int=0) const override
Returns the number of rings of which this geometry is built.
AxisOrder
Axis order for GML generation.
int vertexCount(int=0, int=0) const override
Returns the number of vertices of which this geometry is built.
QVector< QgsPoint > QgsPointSequence
double m() const
Returns the point's m value.
QVector< QgsPointSequence > QgsRingSequence
int partCount() const override
Returns count of parts contained in the geometry.
bool fromWkt(const QString &wkt) override
Sets the geometry from a WKT string.
bool isEmpty() const override
Returns true if the geometry is empty.
static Type dropZ(Type type)
Drops the z dimension (if present) for a WKB type and returns the new type.
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.
QByteArray asWkb() const override
Returns a WKB representation of the geometry.
double qgsRound(double number, int places)
Returns a double number, rounded (as close as possible) to the specified number of places...
QgsPoint(double x=0.0, double y=0.0, double z=std::numeric_limits< double >::quiet_NaN(), double m=std::numeric_limits< double >::quiet_NaN(), QgsWkbTypes::Type wkbType=QgsWkbTypes::Unknown)
Construct a point with the provided initial coordinate values.
bool convertTo(QgsWkbTypes::Type type) override
Converts the geometry to a specified type.
bool addMValue(double mValue=0) override
Adds a measure to the geometry, initialized to a preset value.
double z() const
Returns the point's z-coordinate.
QgsCoordinateSequence coordinateSequence() const override
Retrieves the sequence of geometries, rings and nodes.
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...
static bool hasM(Type type)
Tests whether a WKB type contains m values.
QString geometryType() const override
Returns a unique string representing the geometry type.
QgsPoint childPoint(int index) const override
Returns point at index (for geometries without child geometries - i.e.
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 deleteVertex(QgsVertexId position) override
Deletes a vertex within the geometry.
int childCount() const override
Returns number of child geometries (for geometries with child geometries) or child points (for geomet...
static Type flatType(Type type)
Returns the flat type for a WKB type.
QgsWkbTypes::Type readHeader() const
readHeader
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 is3D() const
Returns true if the geometry is 3D and contains a z-value.
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'. Negativ values mean left a...
double distance3D(double x, double y, double z) const
Returns the 3D distance between this point and a specified x, y, z coordinate.
double x() const
Returns the point's x-coordinate.
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...
int vertexNumberFromVertexId(QgsVertexId id) const override
Returns the vertex number corresponding to a vertex id.