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 if ( parts.second.compare( QLatin1String(
"EMPTY" ), Qt::CaseInsensitive ) == 0 )
169 QRegularExpression
rx( QStringLiteral(
"\\s" ) );
170 QStringList coordinates = parts.second.split( rx, QString::SkipEmptyParts );
171 if ( coordinates.size() < 2 )
176 else if ( coordinates.size() == 3 && !
is3D() && !
isMeasure() )
182 else if ( coordinates.size() >= 4 && ( !
is3D() || !
isMeasure() ) )
191 mX = coordinates[idx++].toDouble();
192 mY = coordinates[idx++].toDouble();
193 if (
is3D() && coordinates.length() > 2 )
194 mZ = coordinates[idx++].toDouble();
196 mM = coordinates[idx++].toDouble();
209 int binarySize =
sizeof( char ) +
sizeof( quint32 );
210 binarySize += ( 2 +
is3D() +
isMeasure() ) *
sizeof(
double );
213 wkbArray.resize( binarySize );
216 wkb << static_cast<quint32>(
wkbType() );
234 wkt += QStringLiteral(
" EMPTY" );
237 wkt += QLatin1String(
" (" );
250 QDomElement elemPoint = doc.createElementNS( ns, QStringLiteral(
"Point" ) );
251 QDomElement elemCoordinates = doc.createElementNS( ns, QStringLiteral(
"coordinates" ) );
254 QString cs = QStringLiteral(
"," );
256 QString ts = QStringLiteral(
" " );
258 elemCoordinates.setAttribute( QStringLiteral(
"cs" ), cs );
259 elemCoordinates.setAttribute( QStringLiteral(
"ts" ), ts );
261 QString strCoordinates;
262 if ( axisOrder == QgsAbstractGeometry::AxisOrder::XY )
266 elemCoordinates.appendChild( doc.createTextNode( strCoordinates ) );
267 elemPoint.appendChild( elemCoordinates );
273 QDomElement elemPoint = doc.createElementNS( ns, QStringLiteral(
"Point" ) );
274 QDomElement elemPosList = doc.createElementNS( ns, QStringLiteral(
"pos" ) );
275 elemPosList.setAttribute( QStringLiteral(
"srsDimension" ),
is3D() ? 3 : 2 );
276 QString strCoordinates;
277 if ( axisOrder == QgsAbstractGeometry::AxisOrder::XY )
284 elemPosList.appendChild( doc.createTextNode( strCoordinates ) );
285 elemPoint.appendChild( elemPosList );
295 {
"coordinates", json::array() },
299 j[
"coordinates"].push_back(
qgsRound( mX, precision ) );
300 j[
"coordinates"].push_back(
qgsRound( mY, precision ) );
303 j[
"coordinates"].push_back(
qgsRound( mZ, precision ) );
311 p.drawRect( QRectF( mX - 2, mY - 2, 4, 4 ) );
316 mX = mY = std::numeric_limits<double>::quiet_NaN();
320 mZ = std::numeric_limits<double>::quiet_NaN();
325 mM = std::numeric_limits<double>::quiet_NaN();
368 if (
id.vertex != 0 )
423 Q_UNUSED( segmentPt )
424 Q_UNUSED( vertexAfter )
523 void QgsPoint::transform(
const QTransform &t,
double zTranslate,
double zScale,
double mTranslate,
double mScale )
527 t.map( mX, mY, &x, &y );
533 mZ = mZ * zScale + zTranslate;
537 mM = mM * mScale + mTranslate;
548 mZ = std::numeric_limits<double>::quiet_NaN();
559 mM = std::numeric_limits<double>::quiet_NaN();
580 mZ = std::numeric_limits<double>::quiet_NaN();
581 mM = std::numeric_limits<double>::quiet_NaN();
586 mM = std::numeric_limits<double>::quiet_NaN();
590 mZ = std::numeric_limits<double>::quiet_NaN();
622 double zDistSquared = 0.0;
623 if (
is3D() || !std::isnan( z ) )
624 zDistSquared = ( mZ -
z ) * ( mZ - z );
626 return std::sqrt( ( mX - x ) * ( mX - x ) + ( mY - y ) * ( mY - y ) + zDistSquared );
631 double zDistSquared = 0.0;
633 zDistSquared = ( mZ - other.
z() ) * ( mZ - other.
z() );
635 return std::sqrt( ( mX - other.
x() ) * ( mX - other.
x() ) + ( mY - other.
y() ) * ( mY - other.
y() ) + zDistSquared );
640 double zDistSquared = 0.0;
641 if (
is3D() || !std::isnan( z ) )
642 zDistSquared = ( mZ -
z ) * ( mZ - z );
644 return ( mX - x ) * ( mX -
x ) + ( mY - y ) * ( mY -
y ) + zDistSquared;
649 double zDistSquared = 0.0;
651 zDistSquared = ( mZ - other.
z() ) * ( mZ - other.
z() );
653 return ( mX - other.
x() ) * ( mX - other.
x() ) + ( mY - other.
y() ) * ( mY - other.
y() ) + zDistSquared;
658 double dx = other.
x() - mX;
659 double dy = other.
y() - mY;
660 return ( std::atan2( dx, dy ) * 180.0 / M_PI );
670 double dz = other.
z() - mZ;
672 return ( std::acos( dz / distance ) * 180.0 / M_PI );
678 double radsXy = azimuth * M_PI / 180.0;
679 double dx = 0.0, dy = 0.0, dz = 0.0;
681 inclination = std::fmod( inclination, 360.0 );
688 dx = distance * std::sin( radsXy );
689 dy = distance * std::cos( radsXy );
693 double radsZ = inclination * M_PI / 180.0;
694 dx = distance * std::sin( radsZ ) * std::sin( radsXy );
695 dy = distance * std::sin( radsZ ) * std::cos( radsXy );
696 dz = distance * std::cos( radsZ );
699 return QgsPoint( mX + dx, mY + dy, mZ + dz, mM, pType );
704 return std::isnan( mX ) || std::isnan( mY );
714 return QStringLiteral(
"Point" );
729 Q_ASSERT( index == 0 );
735 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.
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(), QgsWkbTypes::Type wkbType=QgsWkbTypes::Unknown)
Construct a point with the provided initial coordinate values.
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 Cartesian 2D distance between this point and a specified x, y coordinate.
double azimuth(const QgsPoint &other) const
Calculates Cartesian 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 Cartesian inclination between this point and other one (starting from zenith = 0 to nadir ...
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 Cartesian 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...
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 Cartesian 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.