QGIS API Documentation  3.25.0-Master (6b426f5f8a)
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"
23 
24 #include <ogr_api.h>
25 #include <gdal.h>
26 #include <gdalwarper.h>
27 #include "cpl_conv.h"
28 #include "cpl_string.h"
29 
31 class QgsFieldDomain;
32 
33 class QTextCodec;
34 
35 namespace gdal
36 {
37 
42  {
43 
47  void CORE_EXPORT operator()( OGRDataSourceH source ) const;
48 
49  };
50 
55  {
56 
60  void CORE_EXPORT operator()( OGRGeometryH geometry ) const;
61 
62  };
63 
68  {
69 
73  void CORE_EXPORT operator()( OGRFieldDefnH definition ) const;
74 
75  };
76 
81  {
82 
86  void CORE_EXPORT operator()( OGRFeatureH feature ) const;
87 
88  };
89 
94  {
95 
99  void CORE_EXPORT operator()( GDALDatasetH datasource ) const;
100 
101  };
102 
107  {
108 
112  void CORE_EXPORT operator()( GDALWarpOptions *options ) const;
113 
114  };
115 
119  using ogr_datasource_unique_ptr = std::unique_ptr< std::remove_pointer<OGRDataSourceH>::type, OGRDataSourceDeleter >;
120 
124  using ogr_geometry_unique_ptr = std::unique_ptr< std::remove_pointer<OGRGeometryH>::type, OGRGeometryDeleter >;
125 
129  using ogr_field_def_unique_ptr = std::unique_ptr< std::remove_pointer<OGRFieldDefnH>::type, OGRFldDeleter >;
130 
134  using ogr_feature_unique_ptr = std::unique_ptr< std::remove_pointer<OGRFeatureH>::type, OGRFeatureDeleter >;
135 
139  using dataset_unique_ptr = std::unique_ptr< std::remove_pointer<GDALDatasetH>::type, GDALDatasetCloser >;
140 
149  void CORE_EXPORT fast_delete_and_close( dataset_unique_ptr &dataset, GDALDriverH driver, const QString &path );
150 
154  using warp_options_unique_ptr = std::unique_ptr< GDALWarpOptions, GDALWarpOptionsDeleter >;
155 }
156 
166 class CORE_EXPORT QgsOgrUtils
167 {
168  public:
169 
174  static QVariant OGRFieldtoVariant( const OGRField *value, OGRFieldType type );
175 
181  static std::unique_ptr<OGRField> variantToOGRField( const QVariant &value );
182 
190  static QgsFeature readOgrFeature( OGRFeatureH ogrFet, const QgsFields &fields, QTextCodec *encoding );
191 
198  static QgsFields readOgrFields( OGRFeatureH ogrFet, QTextCodec *encoding );
199 
210  static QVariant getOgrFeatureAttribute( OGRFeatureH ogrFet, const QgsFields &fields, int attIndex, QTextCodec *encoding, bool *ok = nullptr );
211 
224  static QVariant getOgrFeatureAttribute( OGRFeatureH ogrFet, const QgsField &field, int attIndex, QTextCodec *encoding, bool *ok = nullptr );
225 
235  static bool readOgrFeatureAttributes( OGRFeatureH ogrFet, const QgsFields &fields, QgsFeature &feature, QTextCodec *encoding );
236 
245  static bool readOgrFeatureGeometry( OGRFeatureH ogrFet, QgsFeature &feature );
246 
254  static QgsGeometry ogrGeometryToQgsGeometry( OGRGeometryH geom );
255 
265  static QgsFeatureList stringToFeatureList( const QString &string, const QgsFields &fields, QTextCodec *encoding );
266 
274  static QgsFields stringToFields( const QString &string, QTextCodec *encoding );
275 
281  static QStringList cStringListToQStringList( char **stringList );
282 
288  static QgsWkbTypes::Type ogrGeometryTypeToQgsWkbType( OGRwkbGeometryType ogrGeomType );
289 
297  static QString OGRSpatialReferenceToWkt( OGRSpatialReferenceH srs );
298 
305  static QgsCoordinateReferenceSystem OGRSpatialReferenceToCrs( OGRSpatialReferenceH srs );
306 
314  static OGRSpatialReferenceH crsToOGRSpatialReference( const QgsCoordinateReferenceSystem &crs );
315 
327  static QString readShapefileEncoding( const QString &path );
328 
338  static QString readShapefileEncodingFromCpg( const QString &path );
339 
349  static QString readShapefileEncodingFromLdid( const QString &path );
350 
356  static QVariantMap parseStyleString( const QString &string );
357 
363  static std::unique_ptr< QgsSymbol > symbolFromStyleString( const QString &string, Qgis::SymbolType type ) SIP_FACTORY;
364 
376  static void ogrFieldTypeToQVariantType( OGRFieldType ogrType, OGRFieldSubType ogrSubType, QVariant::Type &variantType, QVariant::Type &variantSubType ) SIP_SKIP;
377 
388  static void variantTypeToOgrFieldType( QVariant::Type variantType, OGRFieldType &ogrType, OGRFieldSubType &ogrSubType ) SIP_SKIP;
389 
397  static QVariant stringToVariant( OGRFieldType type, OGRFieldSubType subType, const QString &string ) SIP_SKIP;
398 
399 #ifndef SIP_RUN
400 #if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3,3,0)
401 
409  static std::unique_ptr< QgsFieldDomain > convertFieldDomain( OGRFieldDomainH domain );
410 
418  static OGRFieldDomainH convertFieldDomain( const QgsFieldDomain *domain );
419 #endif
420 #endif
421 };
422 
423 #endif // QGSOGRUTILS_H
SymbolType
Symbol types.
Definition: qgis.h:205
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:51
Container of fields for a vector layer.
Definition: qgsfields.h:45
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:125
Utilities for working with OGR features and layers.
Definition: qgsogrutils.h:167
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:70
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:79
std::unique_ptr< std::remove_pointer< OGRFeatureH >::type, OGRFeatureDeleter > ogr_feature_unique_ptr
Scoped OGR feature.
Definition: qgsogrutils.h:134
std::unique_ptr< std::remove_pointer< GDALDatasetH >::type, GDALDatasetCloser > dataset_unique_ptr
Scoped GDAL dataset.
Definition: qgsogrutils.h:139
std::unique_ptr< std::remove_pointer< OGRDataSourceH >::type, OGRDataSourceDeleter > ogr_datasource_unique_ptr
Scoped OGR data source.
Definition: qgsogrutils.h:119
std::unique_ptr< std::remove_pointer< OGRFieldDefnH >::type, OGRFldDeleter > ogr_field_def_unique_ptr
Scoped OGR field definition.
Definition: qgsogrutils.h:129
std::unique_ptr< std::remove_pointer< OGRGeometryH >::type, OGRGeometryDeleter > ogr_geometry_unique_ptr
Scoped OGR geometry.
Definition: qgsogrutils.h:124
std::unique_ptr< GDALWarpOptions, GDALWarpOptionsDeleter > warp_options_unique_ptr
Scoped GDAL warp options.
Definition: qgsogrutils.h:154
#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:882
const QgsField & field
Definition: qgsfield.h:463
const QgsCoordinateReferenceSystem & crs
Closes and cleanups GDAL dataset.
Definition: qgsogrutils.h:94
void CORE_EXPORT operator()(GDALDatasetH datasource) const
Destroys an gdal dataset, using the correct gdal calls.
Definition: qgsogrutils.cpp:74
Closes and cleanups GDAL warp options.
Definition: qgsogrutils.h:107
void CORE_EXPORT operator()(GDALWarpOptions *options) const
Destroys GDAL warp options, using the correct gdal calls.
Definition: qgsogrutils.cpp:99
Destroys OGR data sources.
Definition: qgsogrutils.h:42
void CORE_EXPORT operator()(OGRDataSourceH source) const
Destroys an OGR data source, using the correct gdal calls.
Definition: qgsogrutils.cpp:53
Destroys OGR feature.
Definition: qgsogrutils.h:81
void CORE_EXPORT operator()(OGRFeatureH feature) const
Destroys an OGR feature, using the correct gdal calls.
Definition: qgsogrutils.cpp:69
Destroys OGR field definition.
Definition: qgsogrutils.h:68
void CORE_EXPORT operator()(OGRFieldDefnH definition) const
Destroys an OGR field definition, using the correct gdal calls.
Definition: qgsogrutils.cpp:64
Destroys OGR geometries.
Definition: qgsogrutils.h:55
void CORE_EXPORT operator()(OGRGeometryH geometry) const
Destroys an OGR geometry, using the correct gdal calls.
Definition: qgsogrutils.cpp:59