QGIS API Documentation  2.10.1-Pisa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgspointv2.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgspointv2.cpp
3  --------------
4  begin : September 2014
5  copyright : (C) 2014 by Marco Hugentobler
6  email : marco at sourcepole dot ch
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 
19 #include "qgspointv2.h"
20 #include "qgsapplication.h"
21 #include "qgscoordinatetransform.h"
22 #include "qgsgeometryutils.h"
23 #include "qgsmaptopixel.h"
24 #include "qgswkbptr.h"
25 #include <QPainter>
26 
27 QgsPointV2::QgsPointV2( double x, double y ): QgsAbstractGeometryV2(), mX( x ), mY( y ), mZ( 0.0 ), mM( 0.0 )
28 {
30 }
31 
32 QgsPointV2::QgsPointV2( QgsWKBTypes::Type type, double x, double y, double z, double m ): mX( x ), mY( y ), mZ( z ), mM( m )
33 {
34  mWkbType = type;
35 }
36 
37 bool QgsPointV2::operator==( const QgsPointV2& pt ) const
38 {
39  return ( pt.wkbType() == wkbType() &&
40  qgsDoubleNear( pt.x(), mX, 1E-8 ) &&
41  qgsDoubleNear( pt.y(), mY, 1E-8 ) &&
42  qgsDoubleNear( pt.z(), mZ, 1E-8 ) &&
43  qgsDoubleNear( pt.m(), mM, 1E-8 ) );
44 }
45 
46 bool QgsPointV2::operator!=( const QgsPointV2& pt ) const
47 {
48  return !operator==( pt );
49 }
50 
52 {
53  return new QgsPointV2( *this );
54 }
55 
56 bool QgsPointV2::fromWkb( const unsigned char* wkb )
57 {
58  QgsConstWkbPtr wkbPtr( wkb );
59  QgsWKBTypes::Type type = wkbPtr.readHeader();
61  {
62  return false;
63  }
64  mWkbType = type;
65 
66  wkbPtr >> mX;
67  wkbPtr >> mY;
68  if ( is3D() )
69  wkbPtr >> mZ;
70  if ( isMeasure() )
71  wkbPtr >> mM;
72 
73  return true;
74 }
75 
76 bool QgsPointV2::fromWkt( const QString& wkt )
77 {
78  clear();
79 
81 
82  if ( QgsWKBTypes::flatType( parts.first ) != QgsWKBTypes::parseType( geometryType() ) )
83  return false;
84  mWkbType = parts.first;
85 
86  QStringList coordinates = parts.second.split( " ", QString::SkipEmptyParts );
87  if ( coordinates.size() < 2 + is3D() + isMeasure() )
88  {
89  clear();
90  return false;
91  }
92 
93  int idx = 0;
94  mX = coordinates[idx++].toDouble();
95  mY = coordinates[idx++].toDouble();
96  if ( is3D() )
97  mZ = coordinates[idx++].toDouble();
98  if ( isMeasure() )
99  mM = coordinates[idx++].toDouble();
100 
101  return true;
102 }
103 
105 {
106  int size = sizeof( char ) + sizeof( quint32 );
107  size += ( 2 + is3D() + isMeasure() ) * sizeof( double );
108  return size;
109 }
110 
111 unsigned char* QgsPointV2::asWkb( int& binarySize ) const
112 {
113  binarySize = wkbSize();
114  unsigned char* geomPtr = new unsigned char[binarySize];
115  QgsWkbPtr wkb( geomPtr );
116  wkb << static_cast<char>( QgsApplication::endian() );
117  wkb << static_cast<quint32>( wkbType() );
118  wkb << mX << mY;
119  if ( is3D() )
120  {
121  wkb << mZ;
122  }
123  if ( isMeasure() )
124  {
125  wkb << mM;
126  }
127  return geomPtr;
128 }
129 
130 QString QgsPointV2::asWkt( int precision ) const
131 {
132  QString wkt = wktTypeStr() + " (";
133  wkt += qgsDoubleToString( mX, precision ) + " " + qgsDoubleToString( mY, precision );
134  if ( is3D() )
135  wkt += " " + qgsDoubleToString( mZ, precision );
136  if ( isMeasure() )
137  wkt += " " + qgsDoubleToString( mM, precision );
138  wkt += ")";
139  return wkt;
140 }
141 
142 QDomElement QgsPointV2::asGML2( QDomDocument& doc, int precision, const QString& ns ) const
143 {
144  QDomElement elemPoint = doc.createElementNS( ns, "Point" );
145  QDomElement elemCoordinates = doc.createElementNS( ns, "coordinates" );
146  QString strCoordinates = qgsDoubleToString( mX, precision ) + "," + qgsDoubleToString( mY, precision );
147  elemCoordinates.appendChild( doc.createTextNode( strCoordinates ) );
148  elemPoint.appendChild( elemCoordinates );
149  return elemPoint;
150 }
151 
152 QDomElement QgsPointV2::asGML3( QDomDocument& doc, int precision, const QString& ns ) const
153 {
154  QDomElement elemPoint = doc.createElementNS( ns, "Point" );
155  QDomElement elemPosList = doc.createElementNS( ns, "posList" );
156  elemPosList.setAttribute( "srsDimension", is3D() ? 3 : 2 );
157  QString strCoordinates = qgsDoubleToString( mX, precision ) + " " + qgsDoubleToString( mY, precision );
158  if ( is3D() )
159  strCoordinates += " " + qgsDoubleToString( mZ, precision );
160 
161  elemPosList.appendChild( doc.createTextNode( strCoordinates ) );
162  elemPoint.appendChild( elemPosList );
163  return elemPoint;
164 }
165 
166 QString QgsPointV2::asJSON( int precision ) const
167 {
168  return "{\"type\": \"Point\", \"coordinates\": ["
169  + qgsDoubleToString( mX, precision ) + ", " + qgsDoubleToString( mY, precision )
170  + "]}";
171 }
172 
173 void QgsPointV2::draw( QPainter& p ) const
174 {
175  p.drawRect( mX - 2, mY - 2, 4, 4 );
176 }
177 
179 {
181  mX = mY = mZ = mM = 0.;
182 }
183 
185 {
186  ct.transformInPlace( mX, mY, mZ );
187 }
188 
190 {
191  coord.clear();
192  QList< QList< QgsPointV2 > > featureCoord;
193  featureCoord.append( QList< QgsPointV2 >() << QgsPointV2( *this ) );
194  coord.append( featureCoord );
195 }
196 
197 bool QgsPointV2::moveVertex( const QgsVertexId& position, const QgsPointV2& newPos )
198 {
199  Q_UNUSED( position );
200  mX = newPos.mX;
201  mY = newPos.mY;
202  if ( is3D() && newPos.is3D() )
203  {
204  mZ = newPos.mZ;
205  }
206  if ( isMeasure() && newPos.isMeasure() )
207  {
208  mM = newPos.mM;
209  }
210  mBoundingBox = QgsRectangle(); //set bounding box invalid
211  return true;
212 }
213 
214 double QgsPointV2::closestSegment( const QgsPointV2& pt, QgsPointV2& segmentPt, QgsVertexId& vertexAfter, bool* leftOf, double epsilon ) const
215 {
216  Q_UNUSED( segmentPt ); Q_UNUSED( vertexAfter ); Q_UNUSED( leftOf ); Q_UNUSED( leftOf ); Q_UNUSED( epsilon );
217  return QgsGeometryUtils::sqrDistance2D( *this, pt );
218 }
219 
221 {
222  if ( id.vertex < 0 )
223  {
224  id.vertex = 0;
225  if ( id.part < 0 )
226  {
227  id.part = 0;
228  }
229  if ( id.ring < 0 )
230  {
231  id.ring = 0;
232  }
233  vertex = *this;
234  return true;
235  }
236  else
237  {
238  return false;
239  }
240 }
241 
243 {
244 #ifdef QT_ARCH_ARM
245  qreal x, y;
246  t.map( mX, mY, &x, &y );
247  mX = x; mY = y;
248 #else
249  t.map( mX, mY, &mX, &mY );
250 #endif
251 }
QgsWKBTypes::Type wkbType() const
Returns the WKB type of the geometry.
A rectangle specified with double values.
Definition: qgsrectangle.h:35
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 (...
QDomElement asGML3(QDomDocument &doc, int precision=17, const QString &ns="gml") const override
Returns a GML3 representation of the geometry.
Definition: qgspointv2.cpp:152
QDomNode appendChild(const QDomNode &newChild)
double x() const
Definition: qgspointv2.h:41
QPoint map(const QPoint &point) const
Abstract base class for all geometries.
void draw(QPainter &p) const override
Draws the geometry using the specified QPainter.
Definition: qgspointv2.cpp:173
bool operator==(const QgsPointV2 &pt) const
Definition: qgspointv2.cpp:37
QDomElement createElementNS(const QString &nsURI, const QString &qName)
bool operator!=(const QgsPointV2 &pt) const
Definition: qgspointv2.cpp:46
static endian_t endian()
Returns whether this machine uses big or little endian.
QString wktTypeStr() const
Returns the WKT type string of the geometry.
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Definition: qgis.h:350
int size() const
bool nextVertex(QgsVertexId &id, QgsPointV2 &vertex) const override
Returns next vertex id and coordinates.
Definition: qgspointv2.cpp:220
double y() const
Definition: qgspointv2.h:42
QgsWKBTypes::Type readHeader() const
Definition: qgswkbptr.cpp:8
void drawRect(const QRectF &rectangle)
static Type flatType(Type type)
Definition: qgswkbtypes.cpp:46
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
void transformInPlace(double &x, double &y, double &z, TransformDirection direction=ForwardTransform) const
virtual QString geometryType() const override
Returns a unique string representing the geometry type.
Definition: qgspointv2.h:51
void append(const T &value)
QgsPointV2(double x=0.0, double y=0.0)
Definition: qgspointv2.cpp:27
static double sqrDistance2D(const QgsPointV2 &pt1, const QgsPointV2 &pt2)
Returns the squared 2D distance between two points.
Utility class for identifying a unique vertex within a geometry.
bool isMeasure() const
Returns true if the geometry contains m values.
virtual QgsAbstractGeometryV2 * clone() const override
Clones the geometry by performing a deep copy.
Definition: qgspointv2.cpp:51
double z() const
Definition: qgspointv2.h:43
void setAttribute(const QString &name, const QString &value)
void clear() override
Clears the geometry, ie reset it to a null geometry.
Definition: qgspointv2.cpp:178
Point geometry type.
Definition: qgspointv2.h:29
virtual void coordinateSequence(QList< QList< QList< QgsPointV2 > > > &coord) const override
Retrieves the sequence of geometries, rings and nodes.
Definition: qgspointv2.cpp:189
QDomElement asGML2(QDomDocument &doc, int precision=17, const QString &ns="gml") const override
Returns a GML2 representation of the geometry.
Definition: qgspointv2.cpp:142
virtual bool fromWkt(const QString &wkt) override
Sets the geometry from a WKT string.
Definition: qgspointv2.cpp:76
QDomText createTextNode(const QString &value)
QString qgsDoubleToString(const double &a, const int &precision=17)
Definition: qgis.h:339
double closestSegment(const QgsPointV2 &pt, QgsPointV2 &segmentPt, QgsVertexId &vertexAfter, bool *leftOf, double epsilon) const override
Searches for the closest segment of the geometry to a given point.
Definition: qgspointv2.cpp:214
QString asWkt(int precision=17) const override
Returns a WKT representation of the geometry.
Definition: qgspointv2.cpp:130
unsigned char * asWkb(int &binarySize) const override
Returns a WKB representation of the geometry.
Definition: qgspointv2.cpp:111
QString asJSON(int precision=17) const override
Returns a GeoJSON representation of the geometry.
Definition: qgspointv2.cpp:166
virtual bool fromWkb(const unsigned char *wkb) override
Sets the geometry from a WKB string.
Definition: qgspointv2.cpp:56
Class for doing transforms between two map coordinate systems.
double m() const
Definition: qgspointv2.h:44
static Type parseType(const QString &wktStr)
Definition: qgswkbtypes.cpp:56
double ANALYSIS_EXPORT leftOf(Point3D *thepoint, Point3D *p1, Point3D *p2)
Returns whether 'thepoint' is left or right of the line from 'p1' to 'p2'.
void transform(const QgsCoordinateTransform &ct) override
Transforms the geometry using a coordinate transform.
Definition: qgspointv2.cpp:184
virtual bool moveVertex(const QgsVertexId &position, const QgsPointV2 &newPos) override
Moves a vertex within the geometry.
Definition: qgspointv2.cpp:197
int wkbSize() const override
Returns the size of the WKB representation of the geometry.
Definition: qgspointv2.cpp:104