QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
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( QMetaType::Type type, const QString &typeString )
24{
25 switch ( type )
26 {
27 case QMetaType::Type::Int:
28 return QStringLiteral( "integer" );
29
30 case QMetaType::Type::LongLong:
31 return QStringLiteral( "long" );
32
33 case QMetaType::Type::Double:
34 return QStringLiteral( "double" );
35
36 case QMetaType::Type::QString:
37 return QStringLiteral( "string" );
38
39 case QMetaType::Type::QDate:
40 return QStringLiteral( "date" );
41
42 case QMetaType::Type::QTime:
43 return QStringLiteral( "time" );
44
45 case QMetaType::Type::QDateTime:
46 return QStringLiteral( "datetime" );
47
48 case QMetaType::Type::QByteArray:
49 return QStringLiteral( "binary" );
50
51 case QMetaType::Type::Bool:
52 return QStringLiteral( "boolean" );
53
54 case QMetaType::Type::QVariantMap:
55 return QStringLiteral( "map" );
56
57 case QMetaType::Type::User:
58 if ( typeString.compare( QLatin1String( "geometry" ), Qt::CaseInsensitive ) == 0 )
59 {
60 return QStringLiteral( "geometry" );
61 }
62 break;
63
64 default:
65 break;
66 }
67 return QStringLiteral( "string" );
68}
69
70QgsVectorLayer *QgsMemoryProviderUtils::createMemoryLayer( const QString &name, const QgsFields &fields, Qgis::WkbType geometryType, const QgsCoordinateReferenceSystem &crs, bool loadDefaultStyle )
71{
72 QString geomType = QgsWkbTypes::displayString( geometryType );
73 if ( geomType.isNull() )
74 geomType = QStringLiteral( "none" );
75
76 QStringList parts;
77 if ( crs.isValid() )
78 {
79 if ( !crs.authid().isEmpty() )
80 parts << QStringLiteral( "crs=%1" ).arg( crs.authid() );
81 else
82 parts << QStringLiteral( "crs=wkt:%1" ).arg( crs.toWkt( Qgis::CrsWktVariant::Preferred ) );
83 }
84 else
85 {
86 parts << QStringLiteral( "crs=" );
87 }
88 for ( const QgsField &field : fields )
89 {
90 const QString lengthPrecision = QStringLiteral( "(%1,%2)" ).arg( field.length() ).arg( field.precision() );
91 parts << QStringLiteral( "field=%1:%2%3%4" ).arg( QString( QUrl::toPercentEncoding( field.name() ) ),
92 memoryLayerFieldType( field.type() == QMetaType::Type::QVariantList || field.type() == QMetaType::Type::QStringList ? field.subType() : field.type(), field.typeName() ),
93 lengthPrecision,
94 field.type() == QMetaType::Type::QVariantList || field.type() == QMetaType::Type::QStringList ? QStringLiteral( "[]" ) : QString() );
95 }
96
97 const QString uri = geomType + '?' + parts.join( '&' );
99 options.skipCrsValidation = true;
100 options.loadDefaultStyle = loadDefaultStyle;
101 return new QgsVectorLayer( uri, name, QStringLiteral( "memory" ), options );
102}
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:256
@ Preferred
Preferred format, matching the most recent WKT ISO standard. Currently an alias to WKT2_2019,...
This class represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
QString toWkt(Qgis::CrsWktVariant variant=Qgis::CrsWktVariant::Wkt1Gdal, 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.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
Container of fields for a vector layer.
Definition qgsfields.h:46
static QgsVectorLayer * createMemoryLayer(const QString &name, const QgsFields &fields, Qgis::WkbType geometryType=Qgis::WkbType::NoGeometry, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem(), bool loadDefaultStyle=true) SIP_FACTORY
Creates a new memory layer using the specified parameters.
Represents a vector layer which manages a vector based data sets.
static QString displayString(Qgis::WkbType type)
Returns a non-translated display string type for a WKB type, e.g., the geometry name used in WKT geom...
QString memoryLayerFieldType(QMetaType::Type type, const QString &typeString)
const QgsCoordinateReferenceSystem & crs
Setting options for loading vector layers.