QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
qgsogrutils.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsogrutils.h
3 -------------
4 begin : February 2016
5 copyright : (C) 2016 Nyall Dawson
6 email : nyall dot dawson at gmail 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#ifndef QGSOGRUTILS_H
17#define QGSOGRUTILS_H
18
19#define SIP_NO_FILE
20
21#include "qgis_core.h"
22#include "qgsfeature.h"
24
25#include <ogr_api.h>
26#include <gdal.h>
27#include <gdalwarper.h>
28#include "cpl_conv.h"
29#include "cpl_string.h"
30
32class QgsFieldDomain;
33
34class QTextCodec;
35class QgsWeakRelation;
36
37namespace gdal
38{
39
44 {
45
49 void CORE_EXPORT operator()( OGRDataSourceH source ) const;
50
51 };
52
57 {
58
62 void CORE_EXPORT operator()( OGRGeometryH geometry ) const;
63
64 };
65
70 {
71
75 void CORE_EXPORT operator()( OGRFieldDefnH definition ) const;
76
77 };
78
83 {
84
88 void CORE_EXPORT operator()( OGRFeatureH feature ) const;
89
90 };
91
96 {
97
101 void CORE_EXPORT operator()( GDALDatasetH datasource ) const;
102
103 };
104
109 {
110
114 void CORE_EXPORT operator()( GDALWarpOptions *options ) const;
115
116 };
117
118#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3,6,0)
119
124 {
125
129 void CORE_EXPORT operator()( GDALRelationshipH relationship ) const;
130
131 };
132#endif
133
137 using ogr_datasource_unique_ptr = std::unique_ptr< std::remove_pointer<OGRDataSourceH>::type, OGRDataSourceDeleter >;
138
142 using ogr_geometry_unique_ptr = std::unique_ptr< std::remove_pointer<OGRGeometryH>::type, OGRGeometryDeleter >;
143
147 using ogr_field_def_unique_ptr = std::unique_ptr< std::remove_pointer<OGRFieldDefnH>::type, OGRFldDeleter >;
148
152 using ogr_feature_unique_ptr = std::unique_ptr< std::remove_pointer<OGRFeatureH>::type, OGRFeatureDeleter >;
153
157 using dataset_unique_ptr = std::unique_ptr< std::remove_pointer<GDALDatasetH>::type, GDALDatasetCloser >;
158
167 void CORE_EXPORT fast_delete_and_close( dataset_unique_ptr &dataset, GDALDriverH driver, const QString &path );
168
172 using warp_options_unique_ptr = std::unique_ptr< GDALWarpOptions, GDALWarpOptionsDeleter >;
173
174#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3,6,0)
175
179 using relationship_unique_ptr = std::unique_ptr< std::remove_pointer<GDALRelationshipH>::type, GDALRelationshipDeleter >;
180#endif
181}
182
192class CORE_EXPORT QgsOgrUtils
193{
194 public:
195
200 static QVariant OGRFieldtoVariant( const OGRField *value, OGRFieldType type );
201
207 static std::unique_ptr<OGRField> variantToOGRField( const QVariant &value );
208
214 static int OGRTZFlagFromQt( const QDateTime &datetime );
215
223 static QgsFeature readOgrFeature( OGRFeatureH ogrFet, const QgsFields &fields, QTextCodec *encoding );
224
231 static QgsFields readOgrFields( OGRFeatureH ogrFet, QTextCodec *encoding );
232
243 static QVariant getOgrFeatureAttribute( OGRFeatureH ogrFet, const QgsFields &fields, int attIndex, QTextCodec *encoding, bool *ok = nullptr );
244
257 static QVariant getOgrFeatureAttribute( OGRFeatureH ogrFet, const QgsField &field, int attIndex, QTextCodec *encoding, bool *ok = nullptr );
258
268 static bool readOgrFeatureAttributes( OGRFeatureH ogrFet, const QgsFields &fields, QgsFeature &feature, QTextCodec *encoding );
269
278 static bool readOgrFeatureGeometry( OGRFeatureH ogrFet, QgsFeature &feature );
279
287 static QgsGeometry ogrGeometryToQgsGeometry( OGRGeometryH geom );
288
298 static QgsFeatureList stringToFeatureList( const QString &string, const QgsFields &fields, QTextCodec *encoding );
299
307 static QgsFields stringToFields( const QString &string, QTextCodec *encoding );
308
314 static QStringList cStringListToQStringList( char **stringList );
315
321 static Qgis::WkbType ogrGeometryTypeToQgsWkbType( OGRwkbGeometryType ogrGeomType );
322
330 static QString OGRSpatialReferenceToWkt( OGRSpatialReferenceH srs );
331
338 static QgsCoordinateReferenceSystem OGRSpatialReferenceToCrs( OGRSpatialReferenceH srs );
339
347 static OGRSpatialReferenceH crsToOGRSpatialReference( const QgsCoordinateReferenceSystem &crs );
348
360 static QString readShapefileEncoding( const QString &path );
361
371 static QString readShapefileEncodingFromCpg( const QString &path );
372
382 static QString readShapefileEncodingFromLdid( const QString &path );
383
389 static QVariantMap parseStyleString( const QString &string );
390
396 static std::unique_ptr< QgsSymbol > symbolFromStyleString( const QString &string, Qgis::SymbolType type ) SIP_FACTORY;
397
409 static void ogrFieldTypeToQVariantType( OGRFieldType ogrType, OGRFieldSubType ogrSubType, QVariant::Type &variantType, QVariant::Type &variantSubType ) SIP_SKIP;
410
421 static void variantTypeToOgrFieldType( QVariant::Type variantType, OGRFieldType &ogrType, OGRFieldSubType &ogrSubType ) SIP_SKIP;
422
430 static QVariant stringToVariant( OGRFieldType type, OGRFieldSubType subType, const QString &string ) SIP_SKIP;
431
437 static QList<QgsVectorDataProvider::NativeType> nativeFieldTypesForDriver( GDALDriverH driver ) SIP_SKIP;
438
439#ifndef SIP_RUN
440#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3,3,0)
441
449 static std::unique_ptr< QgsFieldDomain > convertFieldDomain( OGRFieldDomainH domain );
450
458 static OGRFieldDomainH convertFieldDomain( const QgsFieldDomain *domain );
459#endif
460#endif
461
462#ifndef SIP_RUN
463#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3,6,0)
464
472 static QgsWeakRelation convertRelationship( GDALRelationshipH relationship, const QString &datasetUri );
473
481 static gdal::relationship_unique_ptr convertRelationship( const QgsWeakRelation &relation, QString &error );
482#endif
483#endif
484
485};
486
487#endif // QGSOGRUTILS_H
SymbolType
Symbol types.
Definition: qgis.h:320
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition: qgis.h:155
This class represents a coordinate reference system (CRS).
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
Base class for field domains.
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:52
Container of fields for a vector layer.
Definition: qgsfields.h:45
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:164
Utilities for working with OGR features and layers.
Definition: qgsogrutils.h:193
The QgsWeakRelation class represent a QgsRelation with possibly unresolved layers or unmatched fields...
void CORE_EXPORT fast_delete_and_close(dataset_unique_ptr &dataset, GDALDriverH driver, const QString &path)
Performs a fast close of an unwanted GDAL dataset handle by deleting the underlying data store.
Definition: qgsogrutils.cpp:88
std::unique_ptr< std::remove_pointer< OGRFeatureH >::type, OGRFeatureDeleter > ogr_feature_unique_ptr
Scoped OGR feature.
Definition: qgsogrutils.h:152
std::unique_ptr< std::remove_pointer< GDALDatasetH >::type, GDALDatasetCloser > dataset_unique_ptr
Scoped GDAL dataset.
Definition: qgsogrutils.h:157
std::unique_ptr< std::remove_pointer< OGRDataSourceH >::type, OGRDataSourceDeleter > ogr_datasource_unique_ptr
Scoped OGR data source.
Definition: qgsogrutils.h:137
std::unique_ptr< std::remove_pointer< OGRFieldDefnH >::type, OGRFldDeleter > ogr_field_def_unique_ptr
Scoped OGR field definition.
Definition: qgsogrutils.h:147
std::unique_ptr< std::remove_pointer< OGRGeometryH >::type, OGRGeometryDeleter > ogr_geometry_unique_ptr
Scoped OGR geometry.
Definition: qgsogrutils.h:142
std::unique_ptr< GDALWarpOptions, GDALWarpOptionsDeleter > warp_options_unique_ptr
Scoped GDAL warp options.
Definition: qgsogrutils.h:172
std::unique_ptr< std::remove_pointer< GDALRelationshipH >::type, GDALRelationshipDeleter > relationship_unique_ptr
Scoped GDAL relationship.
Definition: qgsogrutils.h:179
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_FACTORY
Definition: qgis_sip.h:76
void * GDALDatasetH
void * OGRSpatialReferenceH
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:920
const QgsField & field
Definition: qgsfield.h:501
const QgsCoordinateReferenceSystem & crs
Closes and cleanups GDAL dataset.
Definition: qgsogrutils.h:96
void CORE_EXPORT operator()(GDALDatasetH datasource) const
Destroys an gdal dataset, using the correct gdal calls.
Definition: qgsogrutils.cpp:83
Closes and cleanups GDAL relationship.
Definition: qgsogrutils.h:124
void CORE_EXPORT operator()(GDALRelationshipH relationship) const
Destroys GDAL relationship, using the correct gdal calls.
Closes and cleanups GDAL warp options.
Definition: qgsogrutils.h:109
void CORE_EXPORT operator()(GDALWarpOptions *options) const
Destroys GDAL warp options, using the correct gdal calls.
Destroys OGR data sources.
Definition: qgsogrutils.h:44
void CORE_EXPORT operator()(OGRDataSourceH source) const
Destroys an OGR data source, using the correct gdal calls.
Definition: qgsogrutils.cpp:62
Destroys OGR feature.
Definition: qgsogrutils.h:83
void CORE_EXPORT operator()(OGRFeatureH feature) const
Destroys an OGR feature, using the correct gdal calls.
Definition: qgsogrutils.cpp:78
Destroys OGR field definition.
Definition: qgsogrutils.h:70
void CORE_EXPORT operator()(OGRFieldDefnH definition) const
Destroys an OGR field definition, using the correct gdal calls.
Definition: qgsogrutils.cpp:73
Destroys OGR geometries.
Definition: qgsogrutils.h:57
void CORE_EXPORT operator()(OGRGeometryH geometry) const
Destroys an OGR geometry, using the correct gdal calls.
Definition: qgsogrutils.cpp:68