QGIS API Documentation  2.10.1-Pisa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsabstractgeometryv2.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsabstractgeometryv2.cpp
3  -------------------------------------------------------------------
4 Date : 04 Sept 2014
5 Copyright : (C) 2014 by Marco Hugentobler
6 email : marco.hugentobler at sourcepole dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgsapplication.h"
17 #include "qgsabstractgeometryv2.h"
18 #include "qgswkbptr.h"
19 #include "qgsgeos.h"
20 #include "qgsmaptopixel.h"
21 #include <limits>
22 #include <QTransform>
23 
25 {
26 }
27 
29 {
30 }
31 
33 {
34  mWkbType = geom.mWkbType;
35 }
36 
38 {
39  if ( &geom != this )
40  {
41  clear();
42  mWkbType = geom.mWkbType;
43  }
44  return *this;
45 }
46 
48 {
49  if ( mBoundingBox.isNull() )
50  {
52  }
53  return mBoundingBox;
54 }
55 
57 {
58  return(( mWkbType >= 1001 && mWkbType <= 1012 ) || ( mWkbType > 3000 || mWkbType & 0x80000000 ) );
59 }
60 
62 {
63  return ( mWkbType >= 2001 && mWkbType <= 3012 );
64 }
65 
66 #if 0
67 void QgsAbstractGeometryV2::clip( const QgsRectangle& rect )
68 {
69  // TODO
70  // - Implementation
71  // - API doc in header
72 
73  // Don't insert Q_UNUSED, so we have a warning that reminds us of this TODO
74 }
75 #endif
76 
78 {
79  if ( !subgeom )
80  {
81  return;
82  }
83 
84  bool hasZ = subgeom->is3D();
85  bool hasM = subgeom->isMeasure();
86 
87  if ( hasZ && hasM )
88  {
89  mWkbType = ( QgsWKBTypes::Type )( baseGeomType + 3000 );
90  }
91  else if ( hasZ )
92  {
93  mWkbType = ( QgsWKBTypes::Type )( baseGeomType + 1000 );
94  }
95  else if ( hasM )
96  {
97  mWkbType = ( QgsWKBTypes::Type )( baseGeomType + 2000 );
98  }
99  else
100  {
101  mWkbType = baseGeomType;
102  }
103 }
104 
106 {
107  double xmin = std::numeric_limits<double>::max();
108  double ymin = std::numeric_limits<double>::max();
109  double xmax = -std::numeric_limits<double>::max();
110  double ymax = -std::numeric_limits<double>::max();
111 
112  QgsVertexId id;
113  QgsPointV2 vertex;
114  double x, y;
115  while ( nextVertex( id, vertex ) )
116  {
117  x = vertex.x();
118  y = vertex.y();
119  if ( x < xmin )
120  xmin = x;
121  if ( x > xmax )
122  xmax = x;
123  if ( y < ymin )
124  ymin = y;
125  if ( y > ymax )
126  ymax = y;
127  }
128 
129  return QgsRectangle( xmin, ymin, xmax, ymax );
130 }
131 
133 {
134  QList< QList< QList< QgsPointV2 > > > coordinates;
135  coordinateSequence( coordinates );
136 
137  if ( id.part >= coordinates.size() )
138  {
139  return QgsPointV2();
140  }
141  const QList< QList< QgsPointV2 > >& part = coordinates.at( id.part );
142  if ( id.ring >= part.size() )
143  {
144  return QgsPointV2();
145  }
146  const QList< QgsPointV2 >& ring = part.at( id.ring );
147  if ( id.vertex >= ring.size() )
148  {
149  return QgsPointV2();
150  }
151  return ring.at( id.vertex );
152 }
153 
155 {
156  QList< QList< QList< QgsPointV2 > > > coordinates;
157  coordinateSequence( coordinates );
158  int nCoords = 0;
159 
160  QList< QList< QList< QgsPointV2 > > >::const_iterator partIt = coordinates.constBegin();
161  for ( ; partIt != coordinates.constEnd(); ++partIt )
162  {
163  const QList< QList< QgsPointV2 > >& part = *partIt;
164  QList< QList< QgsPointV2 > >::const_iterator ringIt = part.constBegin();
165  for ( ; ringIt != part.constEnd(); ++ringIt )
166  {
167  nCoords += ringIt->size();
168  }
169  }
170 
171  return nCoords;
172 }
173 
175 {
176  QString wkt = geometryType();
177  if ( is3D() )
178  wkt += "Z";
179  if ( isMeasure() )
180  wkt += "M";
181  return wkt;
182 }
183 
184 bool QgsAbstractGeometryV2::readWkbHeader( QgsConstWkbPtr& wkbPtr, QgsWKBTypes::Type& wkbType, bool& endianSwap, QgsWKBTypes::Type expectedType )
185 {
186  if ( !static_cast<const unsigned char*>( wkbPtr ) )
187  {
188  return false;
189  }
190 
191  char wkbEndian;
192  wkbPtr >> wkbEndian;
193  endianSwap = wkbEndian != QgsApplication::endian();
194 
195  wkbPtr >> wkbType;
196  if ( endianSwap )
197  QgsApplication::endian_swap( wkbType );
198 
199  if ( QgsWKBTypes::flatType( wkbType ) != expectedType )
200  {
201  wkbType = QgsWKBTypes::Unknown;
202  return false;
203  }
204  return true;
205 }
206 
208 {
209  QgsVertexId vId; QgsPointV2 vertex;
210  return !nextVertex( vId, vertex );
211 }
static void endian_swap(T &value)
Swap the endianness of the specified value.
QgsWKBTypes::Type wkbType() const
Returns the WKB type of the geometry.
A rectangle specified with double values.
Definition: qgsrectangle.h:35
virtual QgsAbstractGeometryV2 & operator=(const QgsAbstractGeometryV2 &geom)
double x() const
Definition: qgspointv2.h:41
const T & at(int i) const
bool isNull() const
test if the rectangle is null (all coordinates zero or after call to setMinimal()).
Abstract base class for all geometries.
QgsPointV2 vertexAt(const QgsVertexId &id) const
Returns the point corresponding to a specified vertex id.
static endian_t endian()
Returns whether this machine uses big or little endian.
QString wktTypeStr() const
Returns the WKT type string of the geometry.
int nCoordinates() const
Returns the number of nodes contained in the geometry.
static bool readWkbHeader(QgsConstWkbPtr &wkbPtr, QgsWKBTypes::Type &wkbType, bool &endianSwap, QgsWKBTypes::Type expectedType)
Reads a WKB header and tests its validity.
int size() const
double y() const
Definition: qgspointv2.h:42
virtual void clear()=0
Clears the geometry, ie reset it to a null geometry.
static Type flatType(Type type)
Definition: qgswkbtypes.cpp:46
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
Utility class for identifying a unique vertex within a geometry.
bool isMeasure() const
Returns true if the geometry contains m values.
virtual QgsRectangle calculateBoundingBox() const
Calculates the minimal bounding box for the geometry.
bool isEmpty() const
Returns true if the geometry is empty.
Point geometry type.
Definition: qgspointv2.h:29
void setZMTypeFromSubGeometry(const QgsAbstractGeometryV2 *subggeom, QgsWKBTypes::Type baseGeomType)
Updates the geometry type based on whether sub geometries contain z or m values.
virtual bool nextVertex(QgsVertexId &id, QgsPointV2 &vertex) const =0
Returns next vertex id and coordinates.
virtual QString geometryType() const =0
Returns a unique string representing the geometry type.
Class to store information about wkb types.
Definition: qgswkbtypes.h:25
virtual void coordinateSequence(QList< QList< QList< QgsPointV2 > > > &coord) const =0
Retrieves the sequence of geometries, rings and nodes.
const_iterator constEnd() const
const_iterator constBegin() const
QgsRectangle boundingBox() const
Returns the minimal bounding box for the geometry.
int max(int a, int b)
Definition: util.h:87