/
qgsogrprovider.h
278 lines (210 loc) · 9.77 KB
/
qgsogrprovider.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/***************************************************************************
qgsogrprovider.h Data provider for ESRI shapefile format
Formerly known as qgsshapefileprovider.h
begin : Oct 29, 2003
copyright : (C) 2003 by Gary E.Sherman
email : sherman at mrcc.com
***************************************************************************/
/***************************************************************************
* *
* 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 QGSOGRPROVIDER_H
#define QGSOGRPROVIDER_H
#include "QTextCodec"
#include "qgsrectangle.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorfilewriter.h"
#include "qgsvectorlayerimport.h"
class QgsField;
class QgsVectorLayerImport;
class QgsOgrFeatureIterator;
#include <ogr_api.h>
/**
\class QgsOgrProvider
\brief Data provider for ESRI shapefiles
*/
class QgsOgrProvider : public QgsVectorDataProvider
{
Q_OBJECT
public:
//! Convert a vector layer to a vector file
static QgsVectorLayerImport::ImportError createEmptyLayer(
const QString &uri,
const QgsFields &fields,
QgsWkbTypes::Type wkbType,
const QgsCoordinateReferenceSystem &srs,
bool overwrite,
QMap<int, int> *oldToNewAttrIdxMap,
QString *errorMessage = nullptr,
const QMap<QString, QVariant> *options = nullptr
);
/**
* Constructor of the vector provider
* \param uri uniform resource locator (URI) for a dataset
*/
explicit QgsOgrProvider( QString const &uri = "" );
virtual ~QgsOgrProvider();
virtual QgsAbstractFeatureSource *featureSource() const override;
virtual QgsCoordinateReferenceSystem crs() const override;
virtual QStringList subLayers() const override;
virtual QString storageType() const override;
virtual QgsFeatureIterator getFeatures( const QgsFeatureRequest &request ) const override;
virtual QString subsetString() const override;
virtual bool supportsSubsetString() const override { return true; }
virtual bool setSubsetString( const QString &theSQL, bool updateFeatureCount = true ) override;
virtual QgsWkbTypes::Type wkbType() const override;
virtual size_t layerCount() const;
virtual long featureCount() const override;
virtual QgsFields fields() const override;
virtual QgsRectangle extent() const override;
QVariant defaultValue( int fieldId ) const override;
virtual void updateExtents() override;
virtual bool addFeatures( QgsFeatureList &flist ) override;
virtual bool deleteFeatures( const QgsFeatureIds &id ) override;
virtual bool addAttributes( const QList<QgsField> &attributes ) override;
virtual bool deleteAttributes( const QgsAttributeIds &attributes ) override;
virtual bool renameAttributes( const QgsFieldNameMap &renamedAttributes ) override;
virtual bool changeAttributeValues( const QgsChangedAttributesMap &attr_map ) override;
virtual bool changeGeometryValues( const QgsGeometryMap &geometry_map ) override;
virtual bool createSpatialIndex() override;
virtual bool createAttributeIndex( int field ) override;
virtual QgsVectorDataProvider::Capabilities capabilities() const override;
virtual void setEncoding( const QString &e ) override;
virtual bool enterUpdateMode() override;
virtual bool leaveUpdateMode() override;
virtual bool isSaveAndLoadStyleToDatabaseSupported() const override;
QString fileVectorFilters() const override;
//! Return a string containing the available database drivers
QString databaseDrivers() const;
//! Return a string containing the available directory drivers
QString protocolDrivers() const;
//! Return a string containing the available protocol drivers
QString directoryDrivers() const;
bool isValid() const override;
QVariant minimumValue( int index ) const override;
QVariant maximumValue( int index ) const override;
virtual void uniqueValues( int index, QList<QVariant> &uniqueValues, int limit = -1 ) const override;
virtual QStringList uniqueStringsMatching( int index, const QString &substring, int limit = -1,
QgsFeedback *feedback = nullptr ) const override;
QString name() const override;
QString description() const override;
virtual bool doesStrictFeatureTypeCheck() const override;
//! Return OGR geometry type
static OGRwkbGeometryType getOgrGeomType( OGRLayerH ogrLayer );
//! Get single flatten geometry type
static OGRwkbGeometryType ogrWkbSingleFlatten( OGRwkbGeometryType type );
QString layerName() const { return mLayerName; }
QString filePath() const { return mFilePath; }
int layerIndex() const { return mLayerIndex; }
QByteArray quotedIdentifier( const QByteArray &field ) const;
/**
* A forced reload invalidates the underlying connection.
* E.g. in case a shapefile is replaced, the old file will be closed
* and the new file will be opened.
*/
void forceReload() override;
void reloadData() override;
protected:
//! Loads fields from input file to member attributeFields
void loadFields();
//! Find out the number of features of the whole layer
void recalculateFeatureCount();
//! Tell OGR, which fields to fetch in nextFeature/featureAtId (ie. which not to ignore)
void setRelevantFields( OGRLayerH ogrLayer, bool fetchGeometry, const QgsAttributeList &fetchAttributes );
//! Convert a QgsField to work with OGR
static bool convertField( QgsField &field, const QTextCodec &encoding );
//! Clean shapefile from features which are marked as deleted
void repack();
//! Invalidate extent and optionally force its low level recomputation
void invalidateCachedExtent( bool bForceRecomputeExtent );
enum OpenMode
{
OpenModeInitial,
OpenModeSameAsCurrent,
OpenModeForceReadOnly,
OpenModeForceUpdate,
};
void open( OpenMode mode );
void close();
private:
unsigned char *getGeometryPointer( OGRFeatureH fet );
QString ogrWkbGeometryTypeName( OGRwkbGeometryType type ) const;
//! Starts a transaction if possible and return true in that case
bool startTransaction();
//! Commits a transaction
bool commitTransaction();
QgsFields mAttributeFields;
//! Map of field index to default value
QMap<int, QString> mDefaultValues;
bool mFirstFieldIsFid;
OGRDataSourceH ogrDataSource;
mutable OGREnvelope *mExtent;
bool mForceRecomputeExtent;
/** This member variable receives the same value as extent_
in the method QgsOgrProvider::extent(). The purpose is to prevent a memory leak*/
mutable QgsRectangle mExtentRect;
OGRLayerH ogrLayer;
OGRLayerH ogrOrigLayer;
//! path to filename
QString mFilePath;
//! layer name
QString mLayerName;
//! layer index
int mLayerIndex;
//! was a sub layer requested?
bool mIsSubLayer;
/** Optional geometry type for layers with multiple geometries,
* otherwise wkbUnknown. This type is always flatten (2D) and single, it means
* that 2D, 25D, single and multi types are mixed in one sublayer */
OGRwkbGeometryType mOgrGeometryTypeFilter;
//! current spatial filter
QgsRectangle mFetchRect;
//! String used to define a subset of the layer
QString mSubsetString;
// OGR Driver that was actually used to open the layer
OGRSFDriverH ogrDriver;
// Friendly name of the OGR Driver that was actually used to open the layer
QString ogrDriverName;
bool mValid;
OGRwkbGeometryType mOGRGeomType;
long mFeaturesCounted;
mutable QStringList mSubLayerList;
bool addFeature( QgsFeature &f ) override;
//! Deletes one feature
bool deleteFeature( QgsFeatureId id );
//! Calls OGR_L_SyncToDisk and recreates the spatial index if present
bool syncToDisc();
OGRLayerH setSubsetString( OGRLayerH layer, OGRDataSourceH ds );
friend class QgsOgrFeatureSource;
//! Whether the file is opened in write mode
bool mWriteAccess;
//! Whether the file can potentially be opened in write mode (but not necessarily currently)
bool mWriteAccessPossible;
//! Whether the open mode of the datasource changes w.r.t calls to enterUpdateMode() / leaveUpdateMode()
bool mDynamicWriteAccess;
bool mShapefileMayBeCorrupted;
//! Converts the geometry to the layer type if necessary. Takes ownership of the passed geometry
OGRGeometryH ConvertGeometryIfNecessary( OGRGeometryH );
int mUpdateModeStackDepth;
void computeCapabilities();
QgsVectorDataProvider::Capabilities mCapabilities;
bool doInitialActionsForEdition();
};
class QgsOgrProviderUtils
{
public:
static void setRelevantFields( OGRLayerH ogrLayer, int fieldCount, bool fetchGeometry, const QgsAttributeList &fetchAttributes, bool firstAttrIsFid );
static OGRLayerH setSubsetString( OGRLayerH layer, OGRDataSourceH ds, QTextCodec *encoding, const QString &subsetString );
static QByteArray quotedIdentifier( QByteArray field, const QString &ogrDriverName );
/** Quote a value for placement in a SQL string.
*/
static QString quotedValue( const QVariant &value );
static OGRDataSourceH OGROpenWrapper( const char *pszPath, bool bUpdate, OGRSFDriverH *phDriver );
static void OGRDestroyWrapper( OGRDataSourceH ogrDataSource );
};
#endif // QGSOGRPROVIDER_H