QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgsvirtuallayerdefinition.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvirtuallayerdefinition.h
3 begin : Feb, 2015
4 copyright : (C) 2015 Hugo Mercier, Oslandia
5 email : hugo dot mercier at oslandia dot com
6  ***************************************************************************/
7 
8 /***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #ifndef QGSVIRTUALLAYERDEFINITION_H
18 #define QGSVIRTUALLAYERDEFINITION_H
19 
20 #include <qgsfield.h>
21 #include <qgis.h>
22 
29 class CORE_EXPORT QgsVirtualLayerDefinition
30 {
31  public:
36  class CORE_EXPORT SourceLayer
37  {
38  public:
40  SourceLayer( const QString& name, const QString& ref )
41  : mName( name )
42  , mRef( ref )
43  {}
45  SourceLayer( const QString& name, const QString& source, const QString& provider, const QString& encoding )
46  : mName( name )
47  , mSource( source )
48  , mProvider( provider )
49  , mEncoding( encoding )
50  {}
51 
53  bool isReferenced() const { return !mRef.isEmpty(); }
54 
56  QString reference() const { return mRef; }
57 
59  QString name() const { return mName; }
60 
62  QString provider() const { return mProvider; }
63 
65  QString source() const { return mSource; }
66 
68  QString encoding() const { return mEncoding; }
69 
70  private:
71  QString mName;
72  QString mSource;
73  QString mProvider;
74  QString mRef;
75  QString mEncoding;
76  };
77 
79  QgsVirtualLayerDefinition( const QString& filePath = "" );
80 
93  static QgsVirtualLayerDefinition fromUrl( const QUrl& url );
94 
96  QUrl toUrl() const;
97 
99  QString toString() const;
100 
102  void addSource( const QString& name, const QString& ref );
103 
105  void addSource( const QString& name, const QString& source, const QString& provider, const QString& encoding = "" );
106 
109 
111  const SourceLayers& sourceLayers() const { return mSourceLayers; }
112 
114  QString query() const { return mQuery; }
116  void setQuery( const QString& query ) { mQuery = query; }
117 
119  QString filePath() const { return mFilePath; }
121  void setFilePath( const QString& filePath ) { mFilePath = filePath; }
122 
124  QString uid() const { return mUid; }
126  void setUid( const QString& uid ) { mUid = uid; }
127 
129  QString geometryField() const { return mGeometryField; }
131  void setGeometryField( const QString& geometryField ) { mGeometryField = geometryField; }
132 
136  QgsWKBTypes::Type geometryWkbType() const { return mGeometryWkbType; }
138  void setGeometryWkbType( QgsWKBTypes::Type t ) { mGeometryWkbType = t; }
139 
141  long geometrySrid() const { return mGeometrySrid; }
143  void setGeometrySrid( long srid ) { mGeometrySrid = srid; }
144 
146  const QgsFields& fields() const { return mFields; }
148  void setFields( const QgsFields& fields ) { mFields = fields; }
149 
151  bool hasSourceLayer( const QString& name ) const;
152 
154  bool hasReferencedLayers() const;
155 
157  bool hasDefinedGeometry() const
158  {
159  return geometryWkbType() != QgsWKBTypes::NoGeometry && geometryWkbType() != QgsWKBTypes::Unknown;
160  }
161 
162  private:
163  SourceLayers mSourceLayers;
164  QString mQuery;
165  QString mUid;
166  QString mGeometryField;
167  QString mFilePath;
168  QgsFields mFields;
169  QgsWKBTypes::Type mGeometryWkbType;
170  long mGeometrySrid;
171 };
172 
173 #endif
bool isReferenced() const
Is it a live layer or not ?
const SourceLayers & sourceLayers() const
Get access to the source layers.
void setFields(const QgsFields &fields)
Set field definitions.
SourceLayer(const QString &name, const QString &source, const QString &provider, const QString &encoding)
Constructor variant to build a layer with a provider and a source.
QString name() const
Name of the layer.
QString uid() const
Get the name of the field with unique identifiers.
QString encoding() const
Optional encoding for the provider.
Container of fields for a vector layer.
Definition: qgsfield.h:252
QString source() const
The source url used by the provider to build the layer.
void setFilePath(const QString &filePath)
Set the file path.
QString filePath() const
Get the file path. May be empty.
QString reference() const
The reference (id) of the live layer.
void setQuery(const QString &query)
Set the SQL query.
void setGeometryField(const QString &geometryField)
Set the name of the geometry field.
const QgsFields & fields() const
Get field definitions.
QString geometryField() const
Get the name of the geometry field. Empty if no geometry field.
void setGeometryWkbType(QgsWKBTypes::Type t)
Set the type of the geometry.
SourceLayer(const QString &name, const QString &ref)
Constructor variant to build a live layer reference.
bool hasDefinedGeometry() const
Convenient method to test if the geometry is defined (not NoGeometry and not Unknown) ...
void setUid(const QString &uid)
Set the name of the field with unique identifiers.
QgsWKBTypes::Type geometryWkbType() const
Get the type of the geometry QgsWKBTypes::NoGeometry to hide any geometry QgsWKBTypes::Unknown for un...
void setGeometrySrid(long srid)
Set the SRID of the geometry.
QList< SourceLayer > SourceLayers
List of source layers.
QString query() const
Get the SQL query.
A SourceLayer is either a reference to a live layer in the registry or all the parameters needed to l...
Class to manipulate the definition of a virtual layer.
long geometrySrid() const
Get the SRID of the geometry.