Statistics
| Branch: | Tag: | Revision:

qgis / src / providers / ogr / qgsogrprovider.h @ master

History | View | Annotate | Download (9.82 KB)

1
/***************************************************************************
2
            qgsogrprovider.h Data provider for ESRI shapefile format
3
                    Formerly known as qgsshapefileprovider.h
4
begin                : Oct 29, 2003
5
copyright            : (C) 2003 by Gary E.Sherman
6
email                : sherman at mrcc.com
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
#ifndef QGSOGRPROVIDER_H
19
#define QGSOGRPROVIDER_H
20

    
21
#include "QTextCodec"
22

    
23
#include "qgsrectangle.h"
24
#include "qgsvectordataprovider.h"
25
#include "qgsvectorfilewriter.h"
26
#include "qgsvectorlayerexporter.h"
27

    
28
class QgsField;
29
class QgsVectorLayerExporter;
30

    
31
class QgsOgrFeatureIterator;
32

    
33
#include <ogr_api.h>
34

    
35
/**
36
  \class QgsOgrProvider
37
  \brief Data provider for ESRI shapefiles
38
  */
39
class QgsOgrProvider : public QgsVectorDataProvider
40
{
41
    Q_OBJECT
42

    
43
  public:
44

    
45
    //! Convert a vector layer to a vector file
46
    static QgsVectorLayerExporter::ExportError createEmptyLayer(
47
      const QString &uri,
48
      const QgsFields &fields,
49
      QgsWkbTypes::Type wkbType,
50
      const QgsCoordinateReferenceSystem &srs,
51
      bool overwrite,
52
      QMap<int, int> *oldToNewAttrIdxMap,
53
      QString *errorMessage = nullptr,
54
      const QMap<QString, QVariant> *options = nullptr
55
    );
56

    
57
    /**
58
     * Constructor of the vector provider
59
     * \param uri  uniform resource locator (URI) for a dataset
60
     */
61
    explicit QgsOgrProvider( QString const &uri = "" );
62

    
63
    virtual ~QgsOgrProvider();
64

    
65
    virtual QgsAbstractFeatureSource *featureSource() const override;
66

    
67
    virtual QgsCoordinateReferenceSystem crs() const override;
68
    virtual QStringList subLayers() const override;
69
    virtual QString storageType() const override;
70
    virtual QgsFeatureIterator getFeatures( const QgsFeatureRequest &request ) const override;
71
    virtual QString subsetString() const override;
72
    virtual bool supportsSubsetString() const override { return true; }
73
    virtual bool setSubsetString( const QString &theSQL, bool updateFeatureCount = true ) override;
74
    virtual QgsWkbTypes::Type wkbType() const override;
75
    virtual size_t layerCount() const;
76
    virtual long featureCount() const override;
77
    virtual QgsFields fields() const override;
78
    virtual QgsRectangle extent() const override;
79
    QVariant defaultValue( int fieldId ) const override;
80
    virtual void updateExtents() override;
81
    virtual bool addFeatures( QgsFeatureList &flist, QgsFeatureSink::Flags flags = 0 ) override;
82
    virtual bool deleteFeatures( const QgsFeatureIds &id ) override;
83
    virtual bool addAttributes( const QList<QgsField> &attributes ) override;
84
    virtual bool deleteAttributes( const QgsAttributeIds &attributes ) override;
85
    virtual bool renameAttributes( const QgsFieldNameMap &renamedAttributes ) override;
86
    virtual bool changeAttributeValues( const QgsChangedAttributesMap &attr_map ) override;
87
    virtual bool changeGeometryValues( const QgsGeometryMap &geometry_map ) override;
88
    virtual bool createSpatialIndex() override;
89
    virtual bool createAttributeIndex( int field ) override;
90
    virtual QgsVectorDataProvider::Capabilities capabilities() const override;
91
    virtual void setEncoding( const QString &e ) override;
92
    virtual bool enterUpdateMode() override;
93
    virtual bool leaveUpdateMode() override;
94
    virtual bool isSaveAndLoadStyleToDatabaseSupported() const override;
95
    QString fileVectorFilters() const override;
96
    //! Return a string containing the available database drivers
97
    QString databaseDrivers() const;
98
    //! Return a string containing the available directory drivers
99
    QString protocolDrivers() const;
100
    //! Return a string containing the available protocol drivers
101
    QString directoryDrivers() const;
102

    
103
    bool isValid() const override;
104
    QVariant minimumValue( int index ) const override;
105
    QVariant maximumValue( int index ) const override;
106
    virtual QSet< QVariant > uniqueValues( int index, int limit = -1 ) const override;
107
    virtual QStringList uniqueStringsMatching( int index, const QString &substring, int limit = -1,
108
        QgsFeedback *feedback = nullptr ) const override;
109

    
110
    QString name() const override;
111
    QString description() const override;
112
    virtual bool doesStrictFeatureTypeCheck() const override;
113

    
114
    //! Return OGR geometry type
115
    static OGRwkbGeometryType getOgrGeomType( OGRLayerH ogrLayer );
116

    
117
    //! Get single flatten geometry type
118
    static OGRwkbGeometryType ogrWkbSingleFlatten( OGRwkbGeometryType type );
119

    
120
    QString layerName() const { return mLayerName; }
121

    
122
    QString filePath() const { return mFilePath; }
123

    
124
    int layerIndex() const { return mLayerIndex; }
125

    
126
    QByteArray quotedIdentifier( const QByteArray &field ) const;
127

    
128
    /**
129
     * A forced reload invalidates the underlying connection.
130
     * E.g. in case a shapefile is replaced, the old file will be closed
131
     * and the new file will be opened.
132
     */
133
    void forceReload() override;
134
    void reloadData() override;
135

    
136
  protected:
137
    //! Loads fields from input file to member attributeFields
138
    void loadFields();
139

    
140
    //! Find out the number of features of the whole layer
141
    void recalculateFeatureCount();
142

    
143
    //! Tell OGR, which fields to fetch in nextFeature/featureAtId (ie. which not to ignore)
144
    void setRelevantFields( OGRLayerH ogrLayer, bool fetchGeometry, const QgsAttributeList &fetchAttributes );
145

    
146
    //! Convert a QgsField to work with OGR
147
    static bool convertField( QgsField &field, const QTextCodec &encoding );
148

    
149
    //! Clean shapefile from features which are marked as deleted
150
    void repack();
151

    
152
    //! Invalidate extent and optionally force its low level recomputation
153
    void invalidateCachedExtent( bool bForceRecomputeExtent );
154

    
155
    enum OpenMode
156
    {
157
      OpenModeInitial,
158
      OpenModeSameAsCurrent,
159
      OpenModeForceReadOnly,
160
      OpenModeForceUpdate,
161
    };
162

    
163
    void open( OpenMode mode );
164
    void close();
165

    
166
  private:
167
    unsigned char *getGeometryPointer( OGRFeatureH fet );
168
    QString ogrWkbGeometryTypeName( OGRwkbGeometryType type ) const;
169

    
170
    //! Starts a transaction if possible and return true in that case
171
    bool startTransaction();
172

    
173
    //! Commits a transaction
174
    bool commitTransaction();
175

    
176
    QgsFields mAttributeFields;
177

    
178
    //! Map of field index to default value
179
    QMap<int, QString> mDefaultValues;
180

    
181
    bool mFirstFieldIsFid;
182
    OGRDataSourceH ogrDataSource;
183
    mutable OGREnvelope *mExtent;
184
    bool mForceRecomputeExtent;
185

    
186
    /** This member variable receives the same value as extent_
187
     in the method QgsOgrProvider::extent(). The purpose is to prevent a memory leak*/
188
    mutable QgsRectangle mExtentRect;
189
    OGRLayerH ogrLayer;
190
    OGRLayerH ogrOrigLayer;
191

    
192
    //! path to filename
193
    QString mFilePath;
194

    
195
    //! layer name
196
    QString mLayerName;
197

    
198
    //! layer index
199
    int mLayerIndex;
200

    
201
    //! was a sub layer requested?
202
    bool mIsSubLayer;
203

    
204
    /** Optional geometry type for layers with multiple geometries,
205
     *  otherwise wkbUnknown. This type is always flatten (2D) and single, it means
206
     *  that 2D, 25D, single and multi types are mixed in one sublayer */
207
    OGRwkbGeometryType mOgrGeometryTypeFilter;
208

    
209
    //! current spatial filter
210
    QgsRectangle mFetchRect;
211

    
212
    //! String used to define a subset of the layer
213
    QString mSubsetString;
214

    
215
    // OGR Driver that was actually used to open the layer
216
    OGRSFDriverH ogrDriver;
217

    
218
    // Friendly name of the OGR Driver that was actually used to open the layer
219
    QString ogrDriverName;
220

    
221
    bool mValid;
222

    
223
    OGRwkbGeometryType mOGRGeomType;
224
    long mFeaturesCounted;
225

    
226
    mutable QStringList mSubLayerList;
227

    
228
    bool addFeaturePrivate( QgsFeature &f, QgsFeatureSink::Flags flags );
229
    //! Deletes one feature
230
    bool deleteFeature( QgsFeatureId id );
231

    
232
    //! Calls OGR_L_SyncToDisk and recreates the spatial index if present
233
    bool syncToDisc();
234

    
235
    OGRLayerH setSubsetString( OGRLayerH layer, OGRDataSourceH ds );
236

    
237
    friend class QgsOgrFeatureSource;
238

    
239
    //! Whether the file is opened in write mode
240
    bool mWriteAccess;
241

    
242
    //! Whether the file can potentially be opened in write mode (but not necessarily currently)
243
    bool mWriteAccessPossible;
244

    
245
    //! Whether the open mode of the datasource changes w.r.t calls to enterUpdateMode() / leaveUpdateMode()
246
    bool mDynamicWriteAccess;
247

    
248
    bool mShapefileMayBeCorrupted;
249

    
250
    //! Converts the geometry to the layer type if necessary. Takes ownership of the passed geometry
251
    OGRGeometryH ConvertGeometryIfNecessary( OGRGeometryH );
252

    
253
    int mUpdateModeStackDepth;
254

    
255
    void computeCapabilities();
256

    
257
    QgsVectorDataProvider::Capabilities mCapabilities;
258

    
259
    bool doInitialActionsForEdition();
260
};
261

    
262

    
263
class QgsOgrProviderUtils
264
{
265
  public:
266
    static void setRelevantFields( OGRLayerH ogrLayer, int fieldCount, bool fetchGeometry, const QgsAttributeList &fetchAttributes, bool firstAttrIsFid );
267
    static OGRLayerH setSubsetString( OGRLayerH layer, OGRDataSourceH ds, QTextCodec *encoding, const QString &subsetString );
268
    static QByteArray quotedIdentifier( QByteArray field, const QString &ogrDriverName );
269

    
270
    /** Quote a value for placement in a SQL string.
271
     */
272
    static QString quotedValue( const QVariant &value );
273

    
274
    static OGRDataSourceH OGROpenWrapper( const char *pszPath, bool bUpdate, OGRSFDriverH *phDriver );
275
    static void OGRDestroyWrapper( OGRDataSourceH ogrDataSource );
276
};
277

    
278
#endif // QGSOGRPROVIDER_H