QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
Loading...
Searching...
No Matches
qgsvirtuallayerdefinition.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsvirtuallayerdefinition.cpp
3begin : December 2015
4copyright : (C) 2015 Hugo Mercier, Oslandia
5email : 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
18
20
21#include <QRegularExpression>
22#include <QString>
23#include <QStringList>
24#include <QUrl>
25#include <QUrlQuery>
26
27using namespace Qt::StringLiterals;
28
32
34{
36
37 def.setFilePath( url.toLocalFile() );
38
39 // regexp for column name
40 const QString columnNameRx( u"[a-zA-Z_\\x80-\\xFF][a-zA-Z0-9_\\x80-\\xFF]*"_s );
41
43
44 int layerIdx = 0;
45
46 const QList<QPair<QString, QString> > items = QUrlQuery( url ).queryItems( QUrl::FullyEncoded );
47 for ( int i = 0; i < items.size(); i++ )
48 {
49 const QString key = items.at( i ).first;
50 const QString value = items.at( i ).second;
51 if ( key == "layer_ref"_L1 )
52 {
53 layerIdx++;
54 // layer id, with optional layer_name
55 const int pos = value.indexOf( ':' );
56 QString layerId, vlayerName;
57 if ( pos == -1 )
58 {
59 layerId = QUrl::fromPercentEncoding( value.toUtf8() );
60 vlayerName = u"vtab%1"_s.arg( layerIdx );
61 }
62 else
63 {
64 layerId = QUrl::fromPercentEncoding( value.left( pos ).toUtf8() );
65 vlayerName = QUrl::fromPercentEncoding( value.mid( pos + 1 ).toUtf8() );
66 }
67 // add the layer to the list
68 def.addSource( vlayerName, layerId );
69 }
70 else if ( key == "layer"_L1 )
71 {
72 layerIdx++;
73 // syntax: layer=provider:url_encoded_source_URI(:name(:encoding)?)?
74 const int pos = value.indexOf( ':' );
75 if ( pos != -1 )
76 {
77 QString providerKey, source, vlayerName, encoding = u"UTF-8"_s;
78
79 providerKey = value.left( pos );
80 int pos2 = value.indexOf( ':', pos + 1 );
81 if ( pos2 - pos == 2 )
82 pos2 = value.indexOf( ':', pos + 3 );
83 if ( pos2 != -1 )
84 {
85 source = QUrl::fromPercentEncoding( value.mid( pos + 1, pos2 - pos - 1 ).toUtf8() );
86 const int pos3 = value.indexOf( ':', pos2 + 1 );
87 if ( pos3 != -1 )
88 {
89 vlayerName = QUrl::fromPercentEncoding( value.mid( pos2 + 1, pos3 - pos2 - 1 ).toUtf8() );
90 encoding = value.mid( pos3 + 1 );
91 }
92 else
93 {
94 vlayerName = QUrl::fromPercentEncoding( value.mid( pos2 + 1 ).toUtf8() );
95 }
96 }
97 else
98 {
99 source = QUrl::fromPercentEncoding( value.mid( pos + 1 ).toUtf8() );
100 vlayerName = u"vtab%1"_s.arg( layerIdx );
101 }
102
103 def.addSource( vlayerName, source, providerKey, encoding );
104 }
105 }
106 else if ( key == "geometry"_L1 )
107 {
108 // geometry field definition, optional
109 // geometry_column(:wkb_type:srid)?
110 const thread_local QRegularExpression reGeom( "(" + columnNameRx + ")(?::([a-zA-Z0-9]+):(\\d+))?" );
111 const QRegularExpressionMatch match = reGeom.match( value );
112 if ( match.hasMatch() )
113 {
114 def.setGeometryField( match.captured( 1 ) );
115 if ( match.capturedTexts().size() > 2 )
116 {
117 // not used by the spatialite provider for now ...
118 Qgis::WkbType wkbType = QgsWkbTypes::parseType( match.captured( 2 ) );
119 if ( wkbType == Qgis::WkbType::Unknown )
120 {
121 wkbType = static_cast<Qgis::WkbType>( match.captured( 2 ).toLong() );
122 }
123 def.setGeometryWkbType( wkbType );
124 def.setGeometrySrid( match.captured( 3 ).toLong() );
125 }
126 }
127 }
128 else if ( key == "nogeometry"_L1 )
129 {
131 }
132 else if ( key == "uid"_L1 )
133 {
134 def.setUid( value );
135 }
136 else if ( key == "query"_L1 )
137 {
138 // url encoded query
139 def.setQuery( QUrl::fromPercentEncoding( value.toUtf8() ) );
140 }
141 else if ( key == "field"_L1 )
142 {
143 // field_name:type (int, real, text)
144 const thread_local QRegularExpression reField( "(" + columnNameRx + "):(int|real|text)" );
145 const QRegularExpressionMatch match = reField.match( value );
146 if ( match.hasMatch() )
147 {
148 const QString fieldName( match.captured( 1 ) );
149 const QString fieldType( match.captured( 2 ) );
150 if ( fieldType == "int"_L1 )
151 {
152 fields.append( QgsField( fieldName, QMetaType::Type::LongLong, fieldType ) );
153 }
154 else if ( fieldType == "real"_L1 )
155 {
156 fields.append( QgsField( fieldName, QMetaType::Type::Double, fieldType ) );
157 }
158 if ( fieldType == "text"_L1 )
159 {
160 fields.append( QgsField( fieldName, QMetaType::Type::QString, fieldType ) );
161 }
162 }
163 }
164 else if ( key == "lazy"_L1 )
165 {
166 def.setLazy( true );
167 }
168 else if ( key == "subsetstring"_L1 )
169 {
170 def.setSubsetString( QUrl::fromPercentEncoding( value.toUtf8() ) );
171 }
172 }
173 def.setFields( fields );
174
175 return def;
176}
177
179{
180 QUrl url;
181 if ( !filePath().isEmpty() )
182 url = QUrl::fromLocalFile( filePath() );
183
184 QUrlQuery urlQuery( url );
185
186 const auto constSourceLayers = sourceLayers();
187 for ( const QgsVirtualLayerDefinition::SourceLayer &l : constSourceLayers )
188 {
189 if ( l.isReferenced() )
190 urlQuery.addQueryItem( u"layer_ref"_s, u"%1:%2"_s.arg( l.reference(), l.name() ) );
191 else
192 // if you can find a way to port this away from fromEncodedComponent_helper without breaking existing projects,
193 // please do so... this is GROSS!
194 urlQuery.addQueryItem(
197 u"%1:%4:%2:%3"_s // the order is important, since the 4th argument may contain '%2' as well
198 .arg( l.provider(), QString( QUrl::toPercentEncoding( l.name() ) ), l.encoding(), QString( QUrl::toPercentEncoding( l.source() ) ) )
199 .toUtf8()
200 )
201 );
202 }
203
204 if ( !query().isEmpty() )
205 {
206 urlQuery.addQueryItem( u"query"_s, query() );
207 }
208
209 if ( !uid().isEmpty() )
210 urlQuery.addQueryItem( u"uid"_s, uid() );
211
213 urlQuery.addQueryItem( u"nogeometry"_s, QString() );
214 else if ( !geometryField().isEmpty() )
215 {
216 if ( hasDefinedGeometry() )
217 urlQuery.addQueryItem( u"geometry"_s, u"%1:%2:%3"_s.arg( geometryField() ).arg( qgsEnumValueToKey( geometryWkbType() ) ).arg( geometrySrid() ).toUtf8() );
218 else
219 urlQuery.addQueryItem( u"geometry"_s, geometryField() );
220 }
221
222 const auto constFields = fields();
223 for ( const QgsField &f : constFields )
224 {
225 if ( f.type() == QMetaType::Type::Int || f.type() == QMetaType::Type::UInt || f.type() == QMetaType::Type::Bool || f.type() == QMetaType::Type::LongLong )
226 urlQuery.addQueryItem( u"field"_s, f.name() + ":int" );
227 else if ( f.type() == QMetaType::Type::Double )
228 urlQuery.addQueryItem( u"field"_s, f.name() + ":real" );
229 else if ( f.type() == QMetaType::Type::QString )
230 urlQuery.addQueryItem( u"field"_s, f.name() + ":text" );
231 }
232
233 if ( isLazy() )
234 {
235 urlQuery.addQueryItem( u"lazy"_s, QString() );
236 }
237
238 if ( !subsetString().isEmpty() )
239 {
240 urlQuery.addQueryItem( u"subsetstring"_s, QUrl::toPercentEncoding( subsetString() ) );
241 }
242
243 url.setQuery( urlQuery );
244
245 return url;
246}
247
249{
250 return QString( toUrl().toEncoded() );
251}
252
253void QgsVirtualLayerDefinition::addSource( const QString &name, const QString &ref )
254{
255 mSourceLayers.append( SourceLayer( name, ref ) );
256}
257
258void QgsVirtualLayerDefinition::addSource( const QString &name, const QString &source, const QString &provider, const QString &encoding )
259{
260 mSourceLayers.append( SourceLayer( name, source, provider, encoding ) );
261}
262
263bool QgsVirtualLayerDefinition::hasSourceLayer( const QString &name ) const
264{
265 const auto constSourceLayers = sourceLayers();
266 for ( const QgsVirtualLayerDefinition::SourceLayer &l : constSourceLayers )
267 {
268 if ( l.name() == name )
269 {
270 return true;
271 }
272 }
273 return false;
274}
275
277{
278 const auto constSourceLayers = sourceLayers();
279 for ( const QgsVirtualLayerDefinition::SourceLayer &l : constSourceLayers )
280 {
281 if ( l.isReferenced() )
282 {
283 return true;
284 }
285 }
286 return false;
287}
288
290{
291 return mSubsetString;
292}
293
295{
296 mSubsetString = subsetString;
297}
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:294
@ NoGeometry
No geometry.
Definition qgis.h:312
@ Unknown
Unknown.
Definition qgis.h:295
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
Container of fields for a vector layer.
Definition qgsfields.h:46
Either a reference to a live layer in the registry or all the parameters needed to load it (provider ...
QString geometryField() const
Gets the name of the geometry field. Empty if no geometry field.
QString query() const
Gets the SQL query.
Qgis::WkbType geometryWkbType() const
Gets the type of the geometry QgsWkbTypes::NoGeometry to hide any geometry QgsWkbTypes::Unknown for u...
QgsFields fields() const
Gets field definitions.
long geometrySrid() const
Gets the SRID of the geometry.
bool hasSourceLayer(const QString &name) const
Convenience method to test if a given source layer is part of the definition.
QgsVirtualLayerDefinition(const QString &filePath="")
Constructor with an optional file path.
void setUid(const QString &uid)
Sets the name of the field with unique identifiers.
bool hasDefinedGeometry() const
Convenient method to test if the geometry is defined (not NoGeometry and not Unknown).
void setSubsetString(const QString &subsetString)
Sets the subsetString.
void setLazy(bool lazy)
Sets the lazy mode.
bool hasReferencedLayers() const
Convenience method to test whether the definition has referenced (live) layers.
void setFilePath(const QString &filePath)
Sets the file path.
QString subsetString() const
Returns the subset string.
QUrl toUrl() const
Convert the definition into a QUrl.
void setGeometrySrid(long srid)
Sets the SRID of the geometry.
void addSource(const QString &name, const QString &ref)
Add a live layer source layer.
void setGeometryField(const QString &geometryField)
Sets the name of the geometry field.
const QgsVirtualLayerDefinition::SourceLayers & sourceLayers() const
Gets access to the source layers.
QString uid() const
Gets the name of the field with unique identifiers.
QString filePath() const
Gets the file path. May be empty.
static QgsVirtualLayerDefinition fromUrl(const QUrl &url)
Constructor to build a definition from a QUrl The path part of the URL is extracted as well as the fo...
QString toString() const
Converts the definition into a QString that can be read by the virtual layer provider.
void setFields(const QgsFields &fields)
Sets field definitions.
void setGeometryWkbType(Qgis::WkbType t)
Sets the type of the geometry.
void setQuery(const QString &query)
Sets the SQL query.
bool isLazy() const
Returns the lazy mode.
static Qgis::WkbType parseType(const QString &wktStr)
Attempts to extract the WKB type from a WKT string.
QString fromEncodedComponent_helper(const QByteArray &ba)
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:7157