QGIS API Documentation 3.99.0-Master (d270888f95f)
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
20#include "qgsfields.h"
21#include "qgsvectorlayer.h"
22
23#include <QString>
24#include <QUrl>
25
26using namespace Qt::StringLiterals;
27
28QString memoryLayerFieldType( QMetaType::Type type, const QString &typeString )
29{
30 switch ( type )
31 {
32 case QMetaType::Type::Int:
33 return u"integer"_s;
34
35 case QMetaType::Type::LongLong:
36 return u"long"_s;
37
38 case QMetaType::Type::Double:
39 return u"double"_s;
40
41 case QMetaType::Type::QString:
42 return u"string"_s;
43
44 case QMetaType::Type::QDate:
45 return u"date"_s;
46
47 case QMetaType::Type::QTime:
48 return u"time"_s;
49
50 case QMetaType::Type::QDateTime:
51 return u"datetime"_s;
52
53 case QMetaType::Type::QByteArray:
54 return u"binary"_s;
55
56 case QMetaType::Type::Bool:
57 return u"boolean"_s;
58
59 case QMetaType::Type::QVariantMap:
60 return u"map"_s;
61
62 case QMetaType::Type::User:
63 if ( typeString.compare( "geometry"_L1, Qt::CaseInsensitive ) == 0 )
64 {
65 return u"geometry"_s;
66 }
67 break;
68
69 default:
70 break;
71 }
72 return u"string"_s;
73}
74
75QgsVectorLayer *QgsMemoryProviderUtils::createMemoryLayer( const QString &name, const QgsFields &fields, Qgis::WkbType geometryType, const QgsCoordinateReferenceSystem &crs, bool loadDefaultStyle )
76{
77 QString geomType = QgsWkbTypes::displayString( geometryType );
78 if ( geomType.isNull() )
79 geomType = u"none"_s;
80
81 QStringList parts;
82 if ( crs.isValid() )
83 {
84 if ( !crs.authid().isEmpty() )
85 parts << u"crs=%1"_s.arg( crs.authid() );
86 else
87 parts << u"crs=wkt:%1"_s.arg( crs.toWkt( Qgis::CrsWktVariant::Preferred ) );
88 }
89 else
90 {
91 parts << u"crs="_s;
92 }
93 for ( const QgsField &field : fields )
94 {
95 const QString lengthPrecision = u"(%1,%2)"_s.arg( field.length() ).arg( field.precision() );
96 parts << u"field=%1:%2%3%4"_s.arg( QString( QUrl::toPercentEncoding( field.name() ) ),
97 memoryLayerFieldType( field.type() == QMetaType::Type::QVariantList || field.type() == QMetaType::Type::QStringList ? field.subType() : field.type(), field.typeName() ),
98 lengthPrecision,
99 field.type() == QMetaType::Type::QVariantList || field.type() == QMetaType::Type::QStringList ? u"[]"_s : QString() );
100 }
101
102 const QString uri = geomType + '?' + parts.join( '&' );
104 options.skipCrsValidation = true;
105 options.loadDefaultStyle = loadDefaultStyle;
106 return new QgsVectorLayer( uri, name, u"memory"_s, options );
107}
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:280
@ Preferred
Preferred format, matching the most recent WKT ISO standard. Currently an alias to WKT2_2019,...
Definition qgis.h:2497
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:56
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 dataset.
static Q_INVOKABLE 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)
Setting options for loading vector layers.
bool skipCrsValidation
Controls whether the layer is allowed to have an invalid/unknown CRS.
bool loadDefaultStyle
Set to true if the default layer style should be loaded.