QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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  case QVariant::ByteArray:
48  return QStringLiteral( "binary" );
49 
50  case QVariant::Bool:
51  return QStringLiteral( "boolean" );
52 
53  default:
54  break;
55  }
56  return QStringLiteral( "string" );
57 }
58 
60 {
61  QString geomType = QgsWkbTypes::displayString( geometryType );
62  if ( geomType.isNull() )
63  geomType = QStringLiteral( "none" );
64 
65  QStringList parts;
66  if ( crs.isValid() )
67  {
68  if ( !crs.authid().isEmpty() )
69  parts << QStringLiteral( "crs=%1" ).arg( crs.authid() );
70  else
71  parts << QStringLiteral( "crs=wkt:%1" ).arg( crs.toWkt( QgsCoordinateReferenceSystem::WKT_PREFERRED ) );
72  }
73  for ( const auto &field : fields )
74  {
75  const QString lengthPrecision = QStringLiteral( "(%1,%2)" ).arg( field.length() ).arg( field.precision() );
76  parts << QStringLiteral( "field=%1:%2%3" ).arg( QString( QUrl::toPercentEncoding( field.name() ) ), memoryLayerFieldType( field.type() ), lengthPrecision );
77  }
78 
79  QString uri = geomType + '?' + parts.join( '&' );
81  options.skipCrsValidation = true;
82  return new QgsVectorLayer( uri, name, QStringLiteral( "memory" ), options );
83 }
qgsfields.h
QgsCoordinateTransformContext
Contains information about the context in which a coordinate transform is executed.
Definition: qgscoordinatetransformcontext.h:58
QgsWkbTypes::displayString
static QString displayString(Type type) SIP_HOLDGIL
Returns a display string type for a WKB type, e.g., the geometry name used in WKT geometry representa...
Definition: qgswkbtypes.cpp:145
QgsField::length
int length
Definition: qgsfield.h:55
QgsCoordinateReferenceSystem::WKT_PREFERRED
@ WKT_PREFERRED
Preferred format, matching the most recent WKT ISO standard. Currently an alias to WKT2_2019,...
Definition: qgscoordinatereferencesystem.h:679
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:51
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:45
memoryLayerFieldType
QString memoryLayerFieldType(QVariant::Type type)
Definition: qgsmemoryproviderutils.cpp:22
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:70
field
const QgsField & field
Definition: qgsfield.h:456
QgsField::name
QString name
Definition: qgsfield.h:59
QgsField::precision
int precision
Definition: qgsfield.h:56
QgsMemoryProviderUtils::createMemoryLayer
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.
Definition: qgsmemoryproviderutils.cpp:59
QgsCoordinateReferenceSystem::authid
QString authid() const
Returns the authority identifier for the CRS.
Definition: qgscoordinatereferencesystem.cpp:1321
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:1954
QgsCoordinateReferenceSystem::isValid
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
Definition: qgscoordinatereferencesystem.cpp:924
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:206
qgsvectorlayer.h
QgsVectorLayer::LayerOptions
Setting options for loading vector layers.
Definition: qgsvectorlayer.h:425
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
qgsmemoryproviderutils.h
QgsField::type
QVariant::Type type
Definition: qgsfield.h:57