QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 #include "qgsfields.h"
20 #include "qgsvectorlayer.h"
21 #include <QUrl>
22 
23 QString 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  default:
55  break;
56  }
57  return QStringLiteral( "string" );
58 }
59 
61 {
62  QString geomType = QgsWkbTypes::displayString( geometryType );
63  if ( geomType.isNull() )
64  geomType = QStringLiteral( "none" );
65 
66  QStringList parts;
67  if ( crs.isValid() )
68  {
69  if ( !crs.authid().isEmpty() )
70  parts << QStringLiteral( "crs=%1" ).arg( crs.authid() );
71  else
72  parts << QStringLiteral( "crs=wkt:%1" ).arg( crs.toWkt( QgsCoordinateReferenceSystem::WKT_PREFERRED ) );
73  }
74  for ( const auto &field : fields )
75  {
76  const QString lengthPrecision = QStringLiteral( "(%1,%2)" ).arg( field.length() ).arg( field.precision() );
77  parts << QStringLiteral( "field=%1:%2%3%4" ).arg( QString( QUrl::toPercentEncoding( field.name() ) ),
78  memoryLayerFieldType( field.type() == QVariant::List || field.type() == QVariant::StringList ? field.subType() : field.type() ),
79  lengthPrecision,
80  field.type() == QVariant::List || field.type() == QVariant::StringList ? QStringLiteral( "[]" ) : QString() );
81  }
82 
83  const QString uri = geomType + '?' + parts.join( '&' );
85  options.skipCrsValidation = true;
86  return new QgsVectorLayer( uri, name, QStringLiteral( "memory" ), options );
87 }
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:134
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.