QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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  case QVariant::Map:
55  return QStringLiteral( "map" );
56 
57  default:
58  break;
59  }
60  return QStringLiteral( "string" );
61 }
62 
64 {
65  QString geomType = QgsWkbTypes::displayString( geometryType );
66  if ( geomType.isNull() )
67  geomType = QStringLiteral( "none" );
68 
69  QStringList parts;
70  if ( crs.isValid() )
71  {
72  if ( !crs.authid().isEmpty() )
73  parts << QStringLiteral( "crs=%1" ).arg( crs.authid() );
74  else
75  parts << QStringLiteral( "crs=wkt:%1" ).arg( crs.toWkt( QgsCoordinateReferenceSystem::WKT_PREFERRED ) );
76  }
77  for ( const auto &field : fields )
78  {
79  const QString lengthPrecision = QStringLiteral( "(%1,%2)" ).arg( field.length() ).arg( field.precision() );
80  parts << QStringLiteral( "field=%1:%2%3%4" ).arg( QString( QUrl::toPercentEncoding( field.name() ) ),
81  memoryLayerFieldType( field.type() == QVariant::List || field.type() == QVariant::StringList ? field.subType() : field.type() ),
82  lengthPrecision,
83  field.type() == QVariant::List || field.type() == QVariant::StringList ? QStringLiteral( "[]" ) : QString() );
84  }
85 
86  const QString uri = geomType + '?' + parts.join( '&' );
88  options.skipCrsValidation = true;
89  return new QgsVectorLayer( uri, name, QStringLiteral( "memory" ), options );
90 }
qgsfields.h
QgsCoordinateTransformContext
Contains information about the context in which a coordinate transform is executed.
Definition: qgscoordinatetransformcontext.h:57
QgsWkbTypes::displayString
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...
Definition: qgswkbtypes.cpp:145
QgsField::length
int length
Definition: qgsfield.h:56
QgsCoordinateReferenceSystem::WKT_PREFERRED
@ WKT_PREFERRED
Preferred format, matching the most recent WKT ISO standard. Currently an alias to WKT2_2019,...
Definition: qgscoordinatereferencesystem.h:680
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:105
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
QgsMemoryProviderUtils::createMemoryLayer
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.
Definition: qgsmemoryproviderutils.cpp:63
memoryLayerFieldType
QString memoryLayerFieldType(QVariant::Type type)
Definition: qgsmemoryproviderutils.cpp:23
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:69
field
const QgsField & field
Definition: qgsfield.h:463
QgsField::name
QString name
Definition: qgsfield.h:60
QgsField::precision
int precision
Definition: qgsfield.h:57
QgsCoordinateReferenceSystem::toWkt
QString toWkt(WktVariant variant=WKT1_GDAL, bool multiline=false, int indentationWidth=4) const
Returns a WKT representation of this CRS.
Definition: qgscoordinatereferencesystem.cpp:1810
QgsCoordinateReferenceSystem::isValid
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
Definition: qgscoordinatereferencesystem.cpp:977
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:211
qgsvectorlayer.h
QgsVectorLayer::LayerOptions
Setting options for loading vector layers.
Definition: qgsvectorlayer.h:408
QgsField::subType
QVariant::Type subType() const
If the field is a collection, gets its element's type.
Definition: qgsfield.cpp:134
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsCoordinateReferenceSystem::authid
QString authid
Definition: qgscoordinatereferencesystem.h:217
qgsmemoryproviderutils.h
QgsField::type
QVariant::Type type
Definition: qgsfield.h:58