QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 
22 QString memoryLayerFieldType( QVariant::Type type )
23 {
24  switch ( type )
25  {
26  case QVariant::Int:
27  return QStringLiteral( "integer" );
28 
29  case QVariant::LongLong:
30  return QStringLiteral( "long" );
31 
32  case QVariant::Double:
33  return QStringLiteral( "double" );
34 
35  case QVariant::String:
36  return QStringLiteral( "string" );
37 
38  case QVariant::Date:
39  return QStringLiteral( "date" );
40 
41  case QVariant::Time:
42  return QStringLiteral( "time" );
43 
44  case QVariant::DateTime:
45  return QStringLiteral( "datetime" );
46 
47  default:
48  break;
49  }
50  return QStringLiteral( "string" );
51 }
52 
54 {
55  QString geomType = QgsWkbTypes::displayString( geometryType );
56  if ( geomType.isNull() )
57  geomType = QStringLiteral( "none" );
58 
59  QStringList parts;
60  if ( crs.isValid() )
61  {
62  parts << QStringLiteral( "crs=" ) + crs.authid();
63  }
64  for ( const auto &field : fields )
65  {
66  const QString lengthPrecision = QStringLiteral( "(%1,%2)" ).arg( field.length() ).arg( field.precision() );
67  parts << QStringLiteral( "field=%1:%2%3" ).arg( QString( QUrl::toPercentEncoding( field.name() ) ), memoryLayerFieldType( field.type() ), lengthPrecision );
68  }
69 
70  QString uri = geomType + '?' + parts.join( '&' );
71 
72  return new QgsVectorLayer( uri, name, QStringLiteral( "memory" ) );
73 }
Container of fields for a vector layer.
Definition: qgsfields.h:42
const QgsCoordinateReferenceSystem & crs
QString memoryLayerFieldType(QVariant::Type type)
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
This class represents a coordinate reference system (CRS).
QString authid() const
Returns the authority identifier for the CRS.
static QString displayString(Type type)
Returns a display string type for a WKB type, e.g., the geometry name used in WKT geometry representa...
static QgsVectorLayer * createMemoryLayer(const QString &name, const QgsFields &fields, QgsWkbTypes::Type geometryType=QgsWkbTypes::NoGeometry, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem())
Creates a new memory layer using the specified parameters.
Represents a vector layer which manages a vector based data sets.