QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
qgsmemoryproviderutils.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmemoryproviderutils.cpp
3 --------------------------
4 begin : May 2017
5 copyright : (C) 2017 by Nyall Dawson
6 email : nyall dot dawson at gmail dot 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
19#include "qgsfields.h"
20#include "qgsvectorlayer.h"
21#include <QUrl>
22
23QString memoryLayerFieldType( QVariant::Type type )
24{
25 switch ( type )
26 {
27 case QVariant::Int:
28 return QStringLiteral( "integer" );
29
30 case QVariant::LongLong:
31 return QStringLiteral( "long" );
32
33 case QVariant::Double:
34 return QStringLiteral( "double" );
35
36 case QVariant::String:
37 return QStringLiteral( "string" );
38
39 case QVariant::Date:
40 return QStringLiteral( "date" );
41
42 case QVariant::Time:
43 return QStringLiteral( "time" );
44
45 case QVariant::DateTime:
46 return QStringLiteral( "datetime" );
47
48 case QVariant::ByteArray:
49 return QStringLiteral( "binary" );
50
51 case QVariant::Bool:
52 return QStringLiteral( "boolean" );
53
54 case QVariant::Map:
55 return QStringLiteral( "map" );
56
57 default:
58 break;
59 }
60 return QStringLiteral( "string" );
61}
62
64{
65 QString geomType = QgsWkbTypes::displayString( geometryType );
66 if ( geomType.isNull() )
67 geomType = QStringLiteral( "none" );
68
69 QStringList parts;
70 if ( crs.isValid() )
71 {
72 if ( !crs.authid().isEmpty() )
73 parts << QStringLiteral( "crs=%1" ).arg( crs.authid() );
74 else
75 parts << QStringLiteral( "crs=wkt:%1" ).arg( crs.toWkt( QgsCoordinateReferenceSystem::WKT_PREFERRED ) );
76 }
77 for ( const auto &field : fields )
78 {
79 const QString lengthPrecision = QStringLiteral( "(%1,%2)" ).arg( field.length() ).arg( field.precision() );
80 parts << QStringLiteral( "field=%1:%2%3%4" ).arg( QString( QUrl::toPercentEncoding( field.name() ) ),
81 memoryLayerFieldType( field.type() == QVariant::List || field.type() == QVariant::StringList ? field.subType() : field.type() ),
82 lengthPrecision,
83 field.type() == QVariant::List || field.type() == QVariant::StringList ? QStringLiteral( "[]" ) : QString() );
84 }
85
86 const QString uri = geomType + '?' + parts.join( '&' );
88 options.skipCrsValidation = true;
89 return new QgsVectorLayer( uri, name, QStringLiteral( "memory" ), options );
90}
This class represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
@ WKT_PREFERRED
Preferred format, matching the most recent WKT ISO standard. Currently an alias to WKT2_2019,...
QString toWkt(WktVariant variant=WKT1_GDAL, bool multiline=false, int indentationWidth=4) const
Returns a WKT representation of this CRS.
Contains information about the context in which a coordinate transform is executed.
QString name
Definition: qgsfield.h:60
int precision
Definition: qgsfield.h:57
int length
Definition: qgsfield.h:56
QVariant::Type type
Definition: qgsfield.h:58
QVariant::Type subType() const
If the field is a collection, gets its element's type.
Definition: qgsfield.cpp:135
Container of fields for a vector layer.
Definition: qgsfields.h:45
static QgsVectorLayer * createMemoryLayer(const QString &name, const QgsFields &fields, QgsWkbTypes::Type geometryType=QgsWkbTypes::NoGeometry, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem()) SIP_FACTORY
Creates a new memory layer using the specified parameters.
Represents a vector layer which manages a vector based data sets.
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:70
static QString displayString(Type type) SIP_HOLDGIL
Returns a non-translated display string type for a WKB type, e.g., the geometry name used in WKT geom...
const QgsField & field
Definition: qgsfield.h:463
QString memoryLayerFieldType(QVariant::Type type)
const QgsCoordinateReferenceSystem & crs
Setting options for loading vector layers.