28 #include <QRegularExpression>
32 #define TEXT_PROVIDER_KEY QStringLiteral( "memory" )
33 #define TEXT_PROVIDER_DESCRIPTION QStringLiteral( "Memory provider" )
35 QgsMemoryProvider::QgsMemoryProvider(
const QString &uri,
const ProviderOptions &options, QgsDataProvider::ReadFlags flags )
40 const QUrl url = QUrl::fromEncoded( uri.toUtf8() );
41 const QUrlQuery query( url );
43 if ( query.hasQueryItem( QStringLiteral(
"geometry" ) ) )
45 geometry = query.queryItemValue( QStringLiteral(
"geometry" ) );
49 geometry = url.path();
52 if ( geometry.compare( QLatin1String(
"none" ), Qt::CaseInsensitive ) == 0 )
61 if ( query.hasQueryItem( QStringLiteral(
"crs" ) ) )
63 const QString crsDef = query.queryItemValue( QStringLiteral(
"crs" ) );
64 mCrs.createFromString( crsDef );
75 setNativeTypes( QList< NativeType >()
119 if ( query.hasQueryItem( QStringLiteral(
"field" ) ) )
121 QList<QgsField> attributes;
122 const thread_local QRegularExpression reFieldDef(
"\\:"
128 QRegularExpression::CaseInsensitiveOption );
129 const QStringList fields = query.allQueryItemValues( QStringLiteral(
"field" ) );
130 for (
int i = 0; i < fields.size(); i++ )
132 QString name = QUrl::fromPercentEncoding( fields.at( i ).toUtf8() );
133 const QRegularExpressionMatch regularExpressionMatch = reFieldDef.match( name );
136 QVariant::Type type = QVariant::String;
137 QVariant::Type subType = QVariant::Invalid;
138 QString
typeName( QStringLiteral(
"string" ) );
142 if ( regularExpressionMatch.hasMatch() )
144 name = name.mid( 0, regularExpressionMatch.capturedStart() );
145 typeName = regularExpressionMatch.captured( 1 ).toLower();
148 bool isNativeType =
false;
149 const QList<QgsVectorDataProvider::NativeType> nativeTypesList( nativeTypes() );
150 for (
const NativeType &nativeType : nativeTypesList )
152 if ( nativeType.mTypeName.toLower() ==
typeName )
155 type = nativeType.mType;
156 subType = nativeType.mSubType;
163 if ( isNativeType ==
false )
165 if (
typeName == QLatin1String(
"int" ) )
167 type = QVariant::Int;
168 typeName = QStringLiteral(
"integer" );
170 else if (
typeName == QLatin1String(
"long" ) )
172 type = QVariant::LongLong;
173 typeName = QStringLiteral(
"int8" );
175 else if (
typeName == QLatin1String(
"bool" ) )
177 type = QVariant::Bool;
178 typeName = QStringLiteral(
"boolean" );
183 type = QVariant::String;
184 typeName = QStringLiteral(
"string" );
189 if (
typeName == QLatin1String(
"real" ) ||
typeName == QLatin1String(
"double" ) )
195 if ( !regularExpressionMatch.captured( 2 ).isEmpty() )
196 length = regularExpressionMatch.captured( 2 ).toInt();
198 if ( !regularExpressionMatch.captured( 3 ).isEmpty() )
199 precision = regularExpressionMatch.captured( 3 ).toInt();
202 if ( !regularExpressionMatch.captured( 4 ).isEmpty() )
204 if ( subType == QVariant::Invalid )
207 if ( type != QVariant::List && type != QVariant::StringList )
208 type = type == QVariant::String ? QVariant::StringList : QVariant::List;
210 const QLatin1String listSuffix(
"list" );
211 if ( !
typeName.endsWith( listSuffix ) )
212 typeName += QLatin1String(
"list" );
218 addAttributes( attributes );
221 if ( query.hasQueryItem( QStringLiteral(
"index" ) ) && query.queryItemValue( QStringLiteral(
"index" ) ) == QLatin1String(
"yes" ) )
223 createSpatialIndex();
228 QgsMemoryProvider::~QgsMemoryProvider()
230 delete mSpatialIndex;
233 QString QgsMemoryProvider::providerKey()
235 return TEXT_PROVIDER_KEY;
238 QString QgsMemoryProvider::providerDescription()
240 return TEXT_PROVIDER_DESCRIPTION;
243 QgsMemoryProvider *QgsMemoryProvider::createProvider(
const QString &uri,
244 const ProviderOptions &options,
245 QgsDataProvider::ReadFlags flags )
247 return new QgsMemoryProvider( uri, options, flags );
252 return new QgsMemoryFeatureSource(
this );
255 QString QgsMemoryProvider::dataSourceUri(
bool expandAuthConfig )
const
257 Q_UNUSED( expandAuthConfig )
259 QUrl uri( QStringLiteral(
"memory" ) );
262 query.addQueryItem( QStringLiteral(
"geometry" ), geometry );
264 if ( mCrs.isValid() )
267 const QString authid = mCrs.authid();
268 if ( authid.startsWith( QLatin1String(
"EPSG:" ) ) )
276 query.addQueryItem( QStringLiteral(
"crs" ), crsDef );
280 query.addQueryItem( QStringLiteral(
"index" ), QStringLiteral(
"yes" ) );
284 for (
int i = 0; i < attrs.size(); i++ )
296 typeName = QStringLiteral(
"integer" );
299 case QVariant::LongLong:
300 typeName = QStringLiteral(
"long" );
303 case QVariant::Double:
304 typeName = QStringLiteral(
"double" );
307 case QVariant::String:
308 typeName = QStringLiteral(
"string" );
317 fieldDef.append( QStringLiteral(
":%2(%3,%4)%5" ).arg(
typeName ).arg(
field.
length() ).arg(
field.
precision() ).arg( isList ? QStringLiteral(
"[]" ) : QString() ) );
318 query.addQueryItem( QStringLiteral(
"field" ), fieldDef );
320 uri.setQuery( query );
322 return QString( uri.toEncoded() );
326 QString QgsMemoryProvider::storageType()
const
328 return QStringLiteral(
"Memory storage" );
333 return QgsFeatureIterator(
new QgsMemoryFeatureIterator(
new QgsMemoryFeatureSource(
this ),
true, request ) );
339 if ( mExtent.isEmpty() && !mFeatures.isEmpty() )
342 if ( mSubsetString.isEmpty() )
345 const auto constMFeatures = mFeatures;
346 for (
const QgsFeature &feat : constMFeatures )
348 if ( feat.hasGeometry() )
349 mExtent.combineExtentWith( feat.geometry().boundingBox() );
363 else if ( mFeatures.isEmpty() )
365 mExtent.setMinimal();
376 long long QgsMemoryProvider::featureCount()
const
378 if ( mSubsetString.isEmpty() )
379 return mFeatures.count();
392 QgsFields QgsMemoryProvider::fields()
const
397 bool QgsMemoryProvider::isValid()
const
410 if ( QgsMemoryProvider *other = qobject_cast< QgsMemoryProvider * >( source ) )
413 mFeatures = other->mFeatures;
414 mNextFeatureId = other->mNextFeatureId;
415 mExtent = other->mExtent;
420 bool QgsMemoryProvider::addFeatures(
QgsFeatureList &flist, Flags flags )
424 const bool updateExtent = mFeatures.isEmpty() || !mExtent.isEmpty();
426 const int fieldCount = mFields.count();
429 const auto oldExtent { mExtent };
430 const auto oldNextFeatureId { mNextFeatureId };
433 for ( QgsFeatureList::iterator it = flist.begin(); it != flist.end() && result ; ++it )
435 it->setId( mNextFeatureId );
436 it->setValid(
true );
437 if ( it->attributes().count() < fieldCount )
442 for (
int i = it->attributes().count(); i < mFields.count(); ++i )
444 attributes.append( QVariant( mFields.at( i ).type() ) );
446 it->setAttributes( attributes );
448 else if ( it->attributes().count() > fieldCount )
451 pushError( tr(
"Feature has too many attributes (expecting %1, received %2)" ).arg( fieldCount ).arg( it->attributes().count() ) );
453 attributes.resize( mFields.count() );
454 it->setAttributes( attributes );
464 pushError( tr(
"Could not add feature with geometry type %1 to layer of type %2" ).arg(
QgsWkbTypes::displayString( it->geometry().wkbType() ),
471 bool conversionError {
false };
472 QString errorMessage;
473 for (
int i = 0; i < mFields.count(); ++i )
475 const QVariant originalValue = it->attribute( i );
476 QVariant attrValue = originalValue;
477 if ( ! attrValue.isNull() && ! mFields.at( i ).convertCompatible( attrValue, &errorMessage ) )
482 pushError( tr(
"Could not store attribute \"%1\": %2" )
483 .arg( mFields.at( i ).name(), errorMessage ) );
486 conversionError =
true;
489 else if ( attrValue.type() != originalValue.type() )
492 it->setAttribute( i, attrValue );
497 if ( conversionError )
499 if ( flags.testFlag( QgsFeatureSink::Flag::RollBackOnErrors ) )
506 mFeatures.insert( mNextFeatureId, *it );
507 addedFids.insert( mNextFeatureId );
509 if ( it->hasGeometry() )
512 mExtent.combineExtentWith( it->geometry().boundingBox() );
516 mSpatialIndex->addFeature( *it );
523 if ( ! result && flags.testFlag( QgsFeatureSink::Flag::RollBackOnErrors ) )
527 mFeatures.remove( addedFid );
530 mNextFeatureId = oldNextFeatureId;
540 bool QgsMemoryProvider::deleteFeatures(
const QgsFeatureIds &
id )
542 for ( QgsFeatureIds::const_iterator it =
id.begin(); it !=
id.end(); ++it )
544 const QgsFeatureMap::iterator fit = mFeatures.find( *it );
547 if ( fit == mFeatures.end() )
552 mSpatialIndex->deleteFeature( *fit );
554 mFeatures.erase( fit );
563 bool QgsMemoryProvider::addAttributes(
const QList<QgsField> &attributes )
567 if ( !supportedType(
field ) )
571 bool isNativeTypeName =
false;
572 NativeType nativeTypeCandidate( QString(), QString(), QVariant::Invalid );
573 const QList<QgsVectorDataProvider::NativeType> nativeTypesList( nativeTypes() );
574 for (
const NativeType &nativeType : nativeTypesList )
576 if ( nativeType.mTypeName.toLower() ==
field.
typeName().toLower() )
578 isNativeTypeName =
true;
583 && nativeTypeCandidate.mType == QVariant::Invalid )
584 nativeTypeCandidate = nativeType;
586 if ( !isNativeTypeName )
588 if ( nativeTypeCandidate.mType == QVariant::Invalid )
598 mFields.append(
field );
600 for ( QgsFeatureMap::iterator fit = mFeatures.begin(); fit != mFeatures.end(); ++fit )
604 attr.append( QVariant() );
611 bool QgsMemoryProvider::renameAttributes(
const QgsFieldNameMap &renamedAttributes )
613 QgsFieldNameMap::const_iterator renameIt = renamedAttributes.constBegin();
615 for ( ; renameIt != renamedAttributes.constEnd(); ++renameIt )
617 const int fieldIndex = renameIt.key();
618 if ( fieldIndex < 0 || fieldIndex >= mFields.count() )
623 if ( mFields.indexFromName( renameIt.value() ) >= 0 )
630 mFields.rename( fieldIndex, renameIt.value() );
635 bool QgsMemoryProvider::deleteAttributes(
const QgsAttributeIds &attributes )
637 QList<int> attrIdx = qgis::setToList( attributes );
638 std::sort( attrIdx.begin(), attrIdx.end(), std::greater<int>() );
641 for ( QList<int>::const_iterator it = attrIdx.constBegin(); it != attrIdx.constEnd(); ++it )
644 mFields.remove( idx );
646 for ( QgsFeatureMap::iterator fit = mFeatures.begin(); fit != mFeatures.end(); ++fit )
660 bool result {
true };
664 QString errorMessage;
665 for ( QgsChangedAttributesMap::const_iterator it = attr_map.begin(); it != attr_map.end(); ++it )
667 const QgsFeatureMap::iterator fit = mFeatures.find( it.key() );
668 if ( fit == mFeatures.end() )
675 for ( QgsAttributeMap::const_iterator it2 = attrs.constBegin(); it2 != attrs.constEnd(); ++it2 )
677 QVariant attrValue = it2.value();
679 const bool conversionError { ! attrValue.isNull()
680 && ! mFields.at( it2.key() ).convertCompatible( attrValue, &errorMessage ) };
681 if ( conversionError )
686 pushError( tr(
"Could not change attribute %1 having type %2 for feature %4: %3" )
687 .arg( mFields.at( it2.key() ).name(), it2.value( ).typeName(),
688 errorMessage ).arg( it.key() ) );
693 rollBackAttrs.insert( it2.key(), fit->attribute( it2.key() ) );
694 fit->setAttribute( it2.key(), attrValue );
696 rollBackMap.insert( it.key(), rollBackAttrs );
702 changeAttributeValues( rollBackMap );
711 bool QgsMemoryProvider::changeGeometryValues(
const QgsGeometryMap &geometry_map )
713 for ( QgsGeometryMap::const_iterator it = geometry_map.begin(); it != geometry_map.end(); ++it )
715 const QgsFeatureMap::iterator fit = mFeatures.find( it.key() );
716 if ( fit == mFeatures.end() )
721 mSpatialIndex->deleteFeature( *fit );
723 fit->setGeometry( it.value() );
727 mSpatialIndex->addFeature( *fit );
735 QString QgsMemoryProvider::subsetString()
const
737 return mSubsetString;
740 bool QgsMemoryProvider::setSubsetString(
const QString &theSQL,
bool updateFeatureCount )
742 Q_UNUSED( updateFeatureCount )
744 if ( !theSQL.isEmpty() )
747 if ( tempExpression.hasParserError() )
751 if ( theSQL == mSubsetString )
754 mSubsetString = theSQL;
756 mExtent.setMinimal();
762 bool QgsMemoryProvider::createSpatialIndex()
764 if ( !mSpatialIndex )
769 for ( QgsFeatureMap::iterator it = mFeatures.begin(); it != mFeatures.end(); ++it )
771 mSpatialIndex->addFeature( *it );
779 return mSpatialIndex ? SpatialIndexPresent : SpatialIndexNotPresent;
782 QgsVectorDataProvider::Capabilities QgsMemoryProvider::capabilities()
const
784 return AddFeatures | DeleteFeatures | ChangeGeometries |
785 ChangeAttributeValues | AddAttributes | DeleteAttributes | RenameAttributes | CreateSpatialIndex |
786 SelectAtId | CircularGeometries | FastTruncate;
789 bool QgsMemoryProvider::truncate()
793 mExtent.setMinimal();
797 void QgsMemoryProvider::updateExtents()
799 mExtent.setMinimal();
802 QString QgsMemoryProvider::name()
const
804 return TEXT_PROVIDER_KEY;
807 QString QgsMemoryProvider::description()
const
809 return TEXT_PROVIDER_DESCRIPTION;
Base class that can be used for any class that is capable of returning features.
This class represents a coordinate reference system (CRS).
@ WKT_PREFERRED
Preferred format, matching the most recent WKT ISO standard. Currently an alias to WKT2_2019,...
Class for 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)
This class wraps a request for features to a vector layer (or directly its vector data provider).
SpatialIndexPresence
Enumeration of spatial index presence states.
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.
QVariant::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.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
static void warning(const QString &msg)
Goes to qWarning.
A rectangle specified with double values.
void setMinimal() SIP_HOLDGIL
Set a rectangle so that min corner is at max and max corner is at min.
A spatial index for QgsFeature objects.
This is the base class for vector data providers.
static GeometryType geometryType(Type type) SIP_HOLDGIL
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
static Type parseType(const QString &wktStr)
Attempts to extract the WKB type from a WKT string.
Type
The WKB type describes the number of dimensions a geometry has.
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...
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
const QgsCoordinateReferenceSystem & crs
const QgsAttributeList & attributeIndexes