29#include <QRegularExpression>
34#include "moc_qgsmemoryprovider.cpp"
36using namespace Qt::StringLiterals;
40#define TEXT_PROVIDER_KEY u"memory"_s
41#define TEXT_PROVIDER_DESCRIPTION u"Memory provider"_s
48 const QUrl url = QUrl::fromEncoded( uri.toUtf8() );
49 const QUrlQuery query( url );
51 if ( query.hasQueryItem( u
"geometry"_s ) )
53 geometry = query.queryItemValue( u
"geometry"_s );
57 geometry = url.path();
60 if ( geometry.compare(
"none"_L1, Qt::CaseInsensitive ) == 0 )
69 if ( query.hasQueryItem( u
"crs"_s ) )
71 const QString crsDef = query.queryItemValue( u
"crs"_s );
72 mCrs.createFromString( crsDef );
125 NativeType(
QgsVariantUtils::typeToDisplayString( QMetaType::Type::QVariantList, QMetaType::Type::Int ), u
"integerlist"_s, QMetaType::Type::QVariantList, 0, 0, 0, 0, QMetaType::Type::Int )
127 NativeType(
QgsVariantUtils::typeToDisplayString( QMetaType::Type::QVariantList, QMetaType::Type::Double ), u
"doublelist"_s, QMetaType::Type::QVariantList, 0, 0, 0, 0, QMetaType::Type::Double )
129 NativeType(
QgsVariantUtils::typeToDisplayString( QMetaType::Type::QVariantList, QMetaType::Type::LongLong ), u
"integer64list"_s, QMetaType::Type::QVariantList, 0, 0, 0, 0, QMetaType::Type::LongLong )
136 if ( query.hasQueryItem( u
"field"_s ) )
138 QList<QgsField> attributes;
139 const thread_local QRegularExpression reFieldDef(
146 QRegularExpression::CaseInsensitiveOption
148 const QStringList fields = query.allQueryItemValues( u
"field"_s );
149 for (
int i = 0; i < fields.size(); i++ )
151 QString name = QUrl::fromPercentEncoding( fields.at( i ).toUtf8() );
152 const QRegularExpressionMatch regularExpressionMatch = reFieldDef.match( name );
155 QMetaType::Type type = QMetaType::Type::QString;
156 QMetaType::Type subType = QMetaType::Type::UnknownType;
157 QString typeName( u
"string"_s );
161 if ( regularExpressionMatch.hasMatch() )
163 name = name.mid( 0, regularExpressionMatch.capturedStart() );
164 typeName = regularExpressionMatch.captured( 1 ).toLower();
167 bool isNativeType =
false;
168 const QList<QgsVectorDataProvider::NativeType> nativeTypesList( nativeTypes() );
169 for (
const NativeType &nativeType : nativeTypesList )
171 if ( nativeType.mTypeName.toLower() == typeName )
174 type = nativeType.mType;
175 subType = nativeType.mSubType;
176 typeName = nativeType.mTypeName;
182 if ( isNativeType ==
false )
184 if ( typeName ==
"int"_L1 )
186 type = QMetaType::Type::Int;
187 typeName = u
"integer"_s;
189 else if ( typeName ==
"long"_L1 )
191 type = QMetaType::Type::LongLong;
192 typeName = u
"int8"_s;
194 else if ( typeName ==
"bool"_L1 )
196 type = QMetaType::Type::Bool;
197 typeName = u
"boolean"_s;
201 QgsLogger::warning( tr(
"Unsupported typeName '%1'. Will be handled as string." ).arg( typeName ) );
202 type = QMetaType::Type::QString;
203 typeName = u
"string"_s;
208 if ( typeName ==
"real"_L1 || typeName ==
"double"_L1 )
214 if ( !regularExpressionMatch.captured( 2 ).isEmpty() )
215 length = regularExpressionMatch.captured( 2 ).toInt();
217 if ( !regularExpressionMatch.captured( 3 ).isEmpty() )
218 precision = regularExpressionMatch.captured( 3 ).toInt();
221 if ( !regularExpressionMatch.captured( 4 ).isEmpty() )
223 if ( subType == QMetaType::Type::UnknownType )
226 if ( type != QMetaType::Type::QVariantList && type != QMetaType::Type::QStringList )
227 type = type == QMetaType::Type::QString ? QMetaType::Type::QStringList : QMetaType::Type::QVariantList;
229 const QLatin1String listSuffix(
"list" );
230 if ( !typeName.endsWith( listSuffix ) )
231 typeName +=
"list"_L1;
235 attributes.append(
QgsField( name, type, typeName, length, precision, QString(), subType ) );
237 addAttributes( attributes );
240 if ( query.hasQueryItem( u
"index"_s ) && query.queryItemValue( u
"index"_s ) ==
"yes"_L1 )
242 createSpatialIndex();
246QgsMemoryProvider::~QgsMemoryProvider()
249QString QgsMemoryProvider::providerKey()
251 return TEXT_PROVIDER_KEY;
254QString QgsMemoryProvider::providerDescription()
256 return TEXT_PROVIDER_DESCRIPTION;
261 return new QgsMemoryFeatureSource(
this );
264QString QgsMemoryProvider::dataSourceUri(
bool expandAuthConfig )
const
266 Q_UNUSED( expandAuthConfig )
268 QUrl uri( u
"memory"_s );
271 query.addQueryItem( u
"geometry"_s, geometry );
273 if ( mCrs.isValid() )
276 const QString authid = mCrs.authid();
277 if ( authid.startsWith(
"EPSG:"_L1 ) )
285 query.addQueryItem( u
"crs"_s, crsDef );
289 query.addQueryItem( u
"index"_s, u
"yes"_s );
292 QgsAttributeList attrs =
const_cast<QgsMemoryProvider *
>( this )->attributeIndexes();
293 for (
int i = 0; i < attrs.size(); i++ )
295 const QgsField field = mFields.at( attrs[i] );
296 QString fieldDef = field.
name();
298 QString typeName = field.
typeName();
300 if ( field.
type() == QMetaType::Type::QVariantList || field.
type() == QMetaType::Type::QStringList )
304 case QMetaType::Type::Int:
305 typeName = u
"integer"_s;
308 case QMetaType::Type::LongLong:
309 typeName = u
"long"_s;
312 case QMetaType::Type::Double:
313 typeName = u
"double"_s;
316 case QMetaType::Type::QString:
317 typeName = u
"string"_s;
326 fieldDef.append( u
":%2(%3,%4)%5"_s.arg( typeName ).arg( field.
length() ).arg( field.
precision() ).arg( isList ? u
"[]"_s : QString() ) );
327 query.addQueryItem( u
"field"_s, fieldDef );
329 uri.setQuery( query );
331 return QString( uri.toEncoded() );
334QString QgsMemoryProvider::storageType()
const
336 return u
"Memory storage"_s;
341 return QgsFeatureIterator(
new QgsMemoryFeatureIterator(
new QgsMemoryFeatureSource(
this ),
true, request ) );
347 return extent3D().toRectangle();
350QgsBox3D QgsMemoryProvider::extent3D()
const
352 if ( mExtent3D.isEmpty() && !mFeatures.isEmpty() )
355 if ( mSubsetString.isEmpty() )
358 const auto constMFeatures = mFeatures;
359 for (
const QgsFeature &feat : constMFeatures )
361 if ( feat.hasGeometry() )
362 mExtent3D.combineWith( feat.geometry().boundingBox3D() );
376 else if ( mFeatures.isEmpty() )
389long long QgsMemoryProvider::featureCount()
const
391 if ( mSubsetString.isEmpty() )
392 return mFeatures.count();
405QgsFields QgsMemoryProvider::fields()
const
410bool QgsMemoryProvider::isValid()
const
423 if ( QgsMemoryProvider *other = qobject_cast< QgsMemoryProvider * >( source ) )
426 mFeatures = other->mFeatures;
427 mNextFeatureId = other->mNextFeatureId;
428 mExtent3D = other->mExtent3D;
429 if ( other->mSpatialIndex )
431 mSpatialIndex = std::make_unique<QgsSpatialIndex>( *other->mSpatialIndex.get() );
433 mSpatialIndex->detach();
439bool QgsMemoryProvider::addFeatures(
QgsFeatureList &flist, Flags flags )
443 const bool updateExtent = mFeatures.isEmpty() || !mExtent3D.isEmpty();
445 const int fieldCount = mFields.count();
448 const auto oldExtent3D { mExtent3D };
449 const auto oldNextFeatureId { mNextFeatureId };
452 for ( QgsFeatureList::iterator it = flist.begin(); it != flist.end() && result; ++it )
454 it->setId( mNextFeatureId );
455 it->setValid(
true );
456 const int attributeCount = it->attributeCount();
457 if ( attributeCount < fieldCount )
462 for (
int i = attributeCount; i < mFields.count(); ++i )
466 it->setAttributes( attributes );
468 else if ( attributeCount > fieldCount )
471 pushError( tr(
"Feature has too many attributes (expecting %1, received %2)" ).arg( fieldCount ).arg( attributeCount ) );
473 attributes.resize( mFields.count() );
474 it->setAttributes( attributes );
489 bool conversionError {
false };
490 QString errorMessage;
491 for (
int i = 0; i < mFields.count(); ++i )
493 const QVariant originalValue = it->attribute( i );
494 QVariant attrValue = originalValue;
500 pushError( tr(
"Could not store attribute \"%1\": %2" ).arg( mFields.at( i ).name(), errorMessage ) );
503 conversionError =
true;
506 else if ( attrValue.userType() != originalValue.userType() )
509 it->setAttribute( i, attrValue );
514 if ( conversionError )
523 mFeatures.insert( mNextFeatureId, *it );
524 addedFids.insert( mNextFeatureId );
526 if ( it->hasGeometry() )
530 mExtent3D.combineWith( it->geometry().boundingBox3D() );
535 mSpatialIndex->addFeature( *it );
546 mFeatures.remove( addedFid );
548 mExtent3D = oldExtent3D;
549 mNextFeatureId = oldNextFeatureId;
559bool QgsMemoryProvider::deleteFeatures(
const QgsFeatureIds &
id )
561 for ( QgsFeatureIds::const_iterator it =
id.begin(); it !=
id.end(); ++it )
563 const QgsFeatureMap::iterator fit = mFeatures.find( *it );
566 if ( fit == mFeatures.end() )
571 mSpatialIndex->deleteFeature( *fit );
573 mFeatures.erase( fit );
582bool QgsMemoryProvider::addAttributes(
const QList<QgsField> &attributes )
584 bool fieldWasAdded {
false };
587 if ( !supportedType( field ) )
591 bool isNativeTypeName =
false;
592 NativeType nativeTypeCandidate( QString(), QString(), QMetaType::Type::UnknownType );
593 const QList<QgsVectorDataProvider::NativeType> nativeTypesList( nativeTypes() );
594 for (
const NativeType &nativeType : nativeTypesList )
596 if ( nativeType.mTypeName.toLower() == field.
typeName().toLower() )
598 isNativeTypeName =
true;
602 if ( nativeType.mType == field.
type() && nativeTypeCandidate.mType == QMetaType::Type::UnknownType )
603 nativeTypeCandidate = nativeType;
605 if ( !isNativeTypeName )
607 if ( nativeTypeCandidate.mType == QMetaType::Type::UnknownType )
613 field.
setTypeName( nativeTypeCandidate.mTypeName );
617 mFields.append( field );
618 fieldWasAdded =
true;
620 for ( QgsFeatureMap::iterator fit = mFeatures.begin(); fit != mFeatures.end(); ++fit )
624 attr.append( QVariant() );
628 return fieldWasAdded;
631bool QgsMemoryProvider::renameAttributes(
const QgsFieldNameMap &renamedAttributes )
633 QgsFieldNameMap::const_iterator renameIt = renamedAttributes.constBegin();
635 for ( ; renameIt != renamedAttributes.constEnd(); ++renameIt )
637 const int fieldIndex = renameIt.key();
638 if ( fieldIndex < 0 || fieldIndex >= mFields.count() )
643 if ( mFields.indexFromName( renameIt.value() ) >= 0 )
650 mFields.rename( fieldIndex, renameIt.value() );
655bool QgsMemoryProvider::deleteAttributes(
const QgsAttributeIds &attributes )
657 QList<int> attrIdx( attributes.begin(), attributes.end() );
658 std::sort( attrIdx.begin(), attrIdx.end(), std::greater<int>() );
661 for ( QList<int>::const_iterator it = attrIdx.constBegin(); it != attrIdx.constEnd(); ++it )
664 mFields.remove( idx );
666 for ( QgsFeatureMap::iterator fit = mFeatures.begin(); fit != mFeatures.end(); ++fit )
680 bool result {
true };
684 QString errorMessage;
685 for ( QgsChangedAttributesMap::const_iterator it = attr_map.begin(); it != attr_map.end(); ++it )
687 const QgsFeatureMap::iterator fit = mFeatures.find( it.key() );
688 if ( fit == mFeatures.end() )
695 for ( QgsAttributeMap::const_iterator it2 = attrs.constBegin(); it2 != attrs.constEnd(); ++it2 )
697 const int fieldIndex = it2.key();
698 if ( fieldIndex < 0 || fieldIndex >= mFields.count() )
701 QVariant attrValue = it2.value();
706 const bool conversionError { !
QgsVariantUtils::isNull( attrValue ) && !mFields.at( it2.key() ).convertCompatible( attrValue, &errorMessage ) };
707 if ( conversionError )
712 pushError( tr(
"Could not change attribute %1 having type %2 for feature %4: %3" ).arg( mFields.at( it2.key() ).name(), it2.value().typeName(), errorMessage ).arg( it.key() ) );
717 rollBackAttrs.insert( it2.key(), fit->attribute( it2.key() ) );
718 fit->setAttribute( it2.key(), attrValue );
720 rollBackMap.insert( it.key(), rollBackAttrs );
726 changeAttributeValues( rollBackMap );
735bool QgsMemoryProvider::changeGeometryValues(
const QgsGeometryMap &geometry_map )
737 for ( QgsGeometryMap::const_iterator it = geometry_map.begin(); it != geometry_map.end(); ++it )
739 const QgsFeatureMap::iterator fit = mFeatures.find( it.key() );
740 if ( fit == mFeatures.end() )
745 mSpatialIndex->deleteFeature( *fit );
747 fit->setGeometry( it.value() );
751 mSpatialIndex->addFeature( *fit );
759QString QgsMemoryProvider::subsetString()
const
761 return mSubsetString;
764bool QgsMemoryProvider::setSubsetString(
const QString &theSQL,
bool updateFeatureCount )
766 Q_UNUSED( updateFeatureCount )
768 if ( !theSQL.isEmpty() )
771 if ( tempExpression.hasParserError() )
775 if ( theSQL == mSubsetString )
778 mSubsetString = theSQL;
786bool QgsMemoryProvider::supportsSubsetString()
const
791QString QgsMemoryProvider::subsetStringDialect()
const
793 return tr(
"QGIS expression" );
796QString QgsMemoryProvider::subsetStringHelpUrl()
const
802bool QgsMemoryProvider::createSpatialIndex()
804 if ( !mSpatialIndex )
806 mSpatialIndex = std::make_unique<QgsSpatialIndex>();
809 for ( QgsFeatureMap::iterator it = mFeatures.begin(); it != mFeatures.end(); ++it )
811 mSpatialIndex->addFeature( *it );
841bool QgsMemoryProvider::truncate()
849void QgsMemoryProvider::updateExtents()
854QString QgsMemoryProvider::name()
const
856 return TEXT_PROVIDER_KEY;
859QString QgsMemoryProvider::description()
const
861 return TEXT_PROVIDER_DESCRIPTION;
865QgsMemoryProviderMetadata::QgsMemoryProviderMetadata()
866 :
QgsProviderMetadata( QgsMemoryProvider::providerKey(), QgsMemoryProvider::providerDescription() )
869QIcon QgsMemoryProviderMetadata::icon()
const
876 return new QgsMemoryProvider( uri, options, flags );
879QList<Qgis::LayerType> QgsMemoryProviderMetadata::supportedLayerTypes()
const
884#undef TEXT_PROVIDER_KEY
885#undef TEXT_PROVIDER_DESCRIPTION
@ SelectAtId
Fast access to features using their ID.
@ FastTruncate
Supports fast truncation of the layer (removing all features).
@ AddFeatures
Allows adding features.
@ CircularGeometries
Supports circular geometry types (circularstring, compoundcurve, curvepolygon).
@ ChangeGeometries
Allows modifications of geometries.
@ AddAttributes
Allows addition of new attributes (fields).
@ CreateSpatialIndex
Allows creation of spatial index.
@ RenameAttributes
Supports renaming attributes (fields).
@ DeleteFeatures
Allows deletion of features.
@ DeleteAttributes
Allows deletion of attributes (fields).
@ ChangeAttributeValues
Allows modification of attribute values.
SpatialIndexPresence
Enumeration of spatial index presence states.
@ NotPresent
No spatial index exists for the source.
@ Present
A valid spatial index exists for the source.
QFlags< DataProviderReadFlag > DataProviderReadFlags
Flags which control data provider construction.
QFlags< VectorProviderCapability > VectorProviderCapabilities
Vector data provider capabilities.
WkbType
The WKB type describes the number of dimensions a geometry has.
@ Preferred
Preferred format, matching the most recent WKT ISO standard. Currently an alias to WKT2_2019,...
Base class that can be used for any class that is capable of returning features.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
A 3-dimensional box composed of x, y, z coordinates.
Represents a coordinate reference system (CRS).
Abstract base class for spatial data provider implementations.
Handles parsing and evaluation of expressions (formerly called "search strings").
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
Wraps a request for features to a vector layer (or directly its vector data provider).
@ RollBackOnErrors
Roll back the whole transaction if a single add feature operation fails.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Encapsulate a field in an attribute table or data source.
QString typeName() const
Gets the field type.
QMetaType::Type subType() const
If the field is a collection, gets its element's type.
void setTypeName(const QString &typeName)
Set the field type.
Container of fields for a vector layer.
QgsBox3D boundingBox3D() const
Returns the 3D bounding box of the geometry.
static void warning(const QString &msg)
Goes to qWarning.
A rectangle specified with double values.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
static QString typeToDisplayString(QMetaType::Type type, QMetaType::Type subType=QMetaType::Type::UnknownType)
Returns a user-friendly translated string representing a QVariant type.
static QVariant createNullVariant(QMetaType::Type metaType)
Helper method to properly create a null QVariant from a metaType Returns the created QVariant.
static bool isUnsetAttributeValue(const QVariant &variant)
Check if the variant is a QgsUnsetAttributeValue.
Base class for vector data providers.
static Qgis::GeometryType geometryType(Qgis::WkbType type)
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
static Qgis::WkbType parseType(const QString &wktStr)
Attempts to extract the WKB type from a WKT string.
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...
static Q_INVOKABLE bool hasZ(Qgis::WkbType type)
Tests whether a WKB type contains the z-dimension.
QMap< int, QString > QgsFieldNameMap
QMap< int, QVariant > QgsAttributeMap
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
QList< QgsFeature > QgsFeatureList
QSet< QgsFeatureId > QgsFeatureIds
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
QList< int > QgsAttributeList
QSet< int > QgsAttributeIds
Setting options for creating vector data providers.