QGIS API Documentation
4.3.0-Master (16eddb2670e)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
core
providers
memory
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
18
#include "
qgsmemoryproviderutils.h
"
19
20
#include "
qgsfields.h
"
21
#include "
qgsvectorlayer.h
"
22
23
#include <QString>
24
#include <QUrl>
25
26
using namespace
Qt::StringLiterals;
27
28
QString
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
75
QgsVectorLayer
*
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(
97
QString( QUrl::toPercentEncoding( field.name() ) ),
98
memoryLayerFieldType
( field.type() == QMetaType::Type::QVariantList || field.type() == QMetaType::Type::QStringList ? field.subType() : field.type(), field.typeName() ),
99
lengthPrecision,
100
field.type() == QMetaType::Type::QVariantList || field.type() == QMetaType::Type::QStringList ? u
"[]"
_s : QString()
101
);
102
}
103
104
const
QString uri = geomType +
'?'
+ parts.join(
'&'
);
105
QgsVectorLayer::LayerOptions
options {
QgsCoordinateTransformContext
() };
106
options.
skipCrsValidation
=
true
;
107
options.
loadDefaultStyle
= loadDefaultStyle;
108
return
new
QgsVectorLayer
( uri, name, u
"memory"
_s, options );
109
}
Qgis::WkbType
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition
qgis.h:314
Qgis::CrsWktVariant::Preferred
@ Preferred
Preferred format, matching the most recent WKT ISO standard. Currently an alias to WKT2_2019,...
Definition
qgis.h:2631
QgsCoordinateReferenceSystem
Represents a coordinate reference system (CRS).
Definition
qgscoordinatereferencesystem.h:216
QgsCoordinateReferenceSystem::isValid
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
Definition
qgscoordinatereferencesystem.cpp:1024
QgsCoordinateReferenceSystem::authid
QString authid
Definition
qgscoordinatereferencesystem.h:221
QgsCoordinateReferenceSystem::toWkt
QString toWkt(Qgis::CrsWktVariant variant=Qgis::CrsWktVariant::Wkt1Gdal, bool multiline=false, int indentationWidth=4) const
Returns a WKT representation of this CRS.
Definition
qgscoordinatereferencesystem.cpp:2088
QgsCoordinateTransformContext
Contains information about the context in which a coordinate transform is executed.
Definition
qgscoordinatetransformcontext.h:61
QgsField
Encapsulate a field in an attribute table or data source.
Definition
qgsfield.h:56
QgsFields
Container of fields for a vector layer.
Definition
qgsfields.h:45
QgsMemoryProviderUtils::createMemoryLayer
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.
Definition
qgsmemoryproviderutils.cpp:75
QgsVectorLayer
Represents a vector layer which manages a vector based dataset.
Definition
qgsvectorlayer.h:414
QgsWkbTypes::displayString
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...
Definition
qgswkbtypes.cpp:196
qgsfields.h
memoryLayerFieldType
QString memoryLayerFieldType(QMetaType::Type type, const QString &typeString)
Definition
qgsmemoryproviderutils.cpp:28
qgsmemoryproviderutils.h
qgsvectorlayer.h
QgsVectorLayer::LayerOptions
Setting options for loading vector layers.
Definition
qgsvectorlayer.h:436
QgsVectorLayer::LayerOptions::skipCrsValidation
bool skipCrsValidation
Controls whether the layer is allowed to have an invalid/unknown CRS.
Definition
qgsvectorlayer.h:509
QgsVectorLayer::LayerOptions::loadDefaultStyle
bool loadDefaultStyle
Set to true if the default layer style should be loaded.
Definition
qgsvectorlayer.h:456
Generated on
for QGIS API Documentation by
1.17.0