-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgsgeometrycheckerutils.h
163 lines (142 loc) · 6.77 KB
/
qgsgeometrycheckerutils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/***************************************************************************
* qgsgeometrycheckerutils.h *
* ------------------- *
* copyright : (C) 2014 by Sandro Mani / Sourcepole AG *
* email : smani@sourcepole.ch *
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGS_GEOMETRYCHECKERUTILS_H
#define QGS_GEOMETRYCHECKERUTILS_H
#include "qgsfeature.h"
#include "qgsvectorlayer.h"
#include "geometry/qgsabstractgeometry.h"
#include "geometry/qgspoint.h"
#include <qmath.h>
#include <QScrollArea>
#include <QScrollBar>
class QgsGeometryEngine;
class QgsFeaturePool;
namespace QgsGeometryCheckerUtils
{
class LayerFeature
{
public:
LayerFeature( const QgsFeaturePool *pool, const QgsFeature &feature, bool useMapCrs );
~LayerFeature();
const QgsFeature &feature() const { return mFeature; }
const QgsVectorLayer &layer() const;
double layerToMapUnits() const;
const QgsCoordinateTransform &layerToMapTransform() const;
const QgsAbstractGeometry *geometry() const { return mGeometry; }
QString geometryCrs() const { return mMapCrs ? layerToMapTransform().destinationCrs().authid() : layerToMapTransform().sourceCrs().authid(); }
QString id() const { return QString( "%1:%2" ).arg( layer().name() ).arg( mFeature.id() ); }
bool operator==( const LayerFeature &other ) const { return layer().id() == other.layer().id() && feature().id() == other.feature().id(); }
bool operator!=( const LayerFeature &other ) const { return layer().id() != other.layer().id() || feature().id() != other.feature().id(); }
private:
const QgsFeaturePool *mFeaturePool;
QgsFeature mFeature;
double mLayerToMapUnits;
QgsCoordinateTransform mLayerToMapTransform;
QgsAbstractGeometry *mGeometry = nullptr;
bool mMapCrs;
};
class LayerFeatures
{
public:
LayerFeatures( const QMap<QString, QgsFeaturePool *> &featurePools,
const QMap<QString, QgsFeatureIds> &featureIds,
const QList<QgsWkbTypes::GeometryType> &geometryTypes,
QAtomicInt *progressCounter, bool useMapCrs = false );
LayerFeatures( const QMap<QString, QgsFeaturePool *> &featurePools,
const QList<QString> &layerIds, const QgsRectangle &extent,
const QList<QgsWkbTypes::GeometryType> &geometryTypes );
class iterator
{
public:
iterator( const QList<QString>::iterator &layerIt, LayerFeatures *parent );
~iterator();
const iterator &operator++();
iterator operator++( int ) { iterator tmp( *this ); ++*this; return tmp; }
const LayerFeature &operator*() const { Q_ASSERT( mCurrentFeature ); return *mCurrentFeature; }
bool operator!=( const iterator &other ) { return mLayerIt != other.mLayerIt || mFeatureIt != other.mFeatureIt; }
private:
bool nextLayerFeature( bool begin );
bool nextLayer( bool begin );
bool nextFeature( bool begin );
QList<QString>::iterator mLayerIt;
QgsFeatureIds::const_iterator mFeatureIt;
LayerFeatures *mParent;
LayerFeature *mCurrentFeature = nullptr;
};
iterator begin() { return iterator( mLayerIds.begin(), this ); }
iterator end() { return iterator( mLayerIds.end(), this ); }
private:
QMap<QString, QgsFeaturePool *> mFeaturePools;
QMap<QString, QgsFeatureIds> mFeatureIds;
QList<QString> mLayerIds;
QgsRectangle mExtent;
QList<QgsWkbTypes::GeometryType> mGeometryTypes;
QAtomicInt *mProgressCounter = nullptr;
bool mUseMapCrs;
};
QSharedPointer<QgsGeometryEngine> createGeomEngine( const QgsAbstractGeometry *geometry, double tolerance );
QgsAbstractGeometry *getGeomPart( QgsAbstractGeometry *geom, int partIdx );
const QgsAbstractGeometry *getGeomPart( const QgsAbstractGeometry *geom, int partIdx );
QList<const QgsLineString *> polygonRings( const QgsPolygonV2 *polygon );
void filter1DTypes( QgsAbstractGeometry *geom );
/**
* \brief Return the number of points in a polyline, accounting for duplicate start and end point if the polyline is closed
* \param polyLine The polyline
* \returns The number of distinct points of the polyline
*/
inline int polyLineSize( const QgsAbstractGeometry *geom, int iPart, int iRing, bool *isClosed = nullptr )
{
if ( !geom->isEmpty() )
{
int nVerts = geom->vertexCount( iPart, iRing );
QgsPoint front = geom->vertexAt( QgsVertexId( iPart, iRing, 0 ) );
QgsPoint back = geom->vertexAt( QgsVertexId( iPart, iRing, nVerts - 1 ) );
bool closed = back == front;
if ( isClosed )
*isClosed = closed;
return closed ? nVerts - 1 : nVerts;
}
else
{
if ( isClosed )
*isClosed = true;
return 0;
}
}
bool pointOnLine( const QgsPoint &p, const QgsLineString *line, double tol, bool excludeExtremities = false );
bool linesIntersect( const QgsLineString *line1, const QgsLineString *line2, double tol, QgsPoint &inter );
double sharedEdgeLength( const QgsAbstractGeometry *geom1, const QgsAbstractGeometry *geom2, double tol );
/**
* \brief Determine whether two points are equal up to the specified tolerance
* \param p1 The first point
* \param p2 The second point
* \param tol The tolerance
* \returns Whether the points are equal
*/
inline bool pointsFuzzyEqual( const QgsPointXY &p1, const QgsPointXY &p2, double tol )
{
double dx = p1.x() - p2.x(), dy = p1.y() - p2.y();
return ( dx * dx + dy * dy ) < tol * tol;
}
inline bool canDeleteVertex( const QgsAbstractGeometry *geom, int iPart, int iRing )
{
int nVerts = geom->vertexCount( iPart, iRing );
QgsPoint front = geom->vertexAt( QgsVertexId( iPart, iRing, 0 ) );
QgsPoint back = geom->vertexAt( QgsVertexId( iPart, iRing, nVerts - 1 ) );
bool closed = back == front;
return closed ? nVerts > 4 : nVerts > 2;
}
} // QgsGeometryCheckerUtils
#endif // QGS_GEOMETRYCHECKERUTILS_H