31 static const QString TEXT_PROVIDER_KEY = QStringLiteral(
"memory" );
32 static const QString TEXT_PROVIDER_DESCRIPTION = QStringLiteral(
"Memory provider" );
34 QgsMemoryProvider::QgsMemoryProvider(
const QString &uri,
const ProviderOptions &options )
39 QUrl url = QUrl::fromEncoded( uri.toUtf8() );
41 if ( url.hasQueryItem( QStringLiteral(
"geometry" ) ) )
43 geometry = url.queryItemValue( QStringLiteral(
"geometry" ) );
47 geometry = url.path();
50 if ( geometry.compare( QLatin1String(
"none" ), Qt::CaseInsensitive ) == 0 )
59 if ( url.hasQueryItem( QStringLiteral(
"crs" ) ) )
61 QString crsDef = url.queryItemValue( QStringLiteral(
"crs" ) );
62 mCrs.createFromString( crsDef );
67 setNativeTypes( QList< NativeType >()
105 if ( url.hasQueryItem( QStringLiteral(
"field" ) ) )
107 QList<QgsField> attributes;
108 QRegExp reFieldDef(
"\\:" 109 "(int|integer|long|int8|real|double|string|date|time|datetime|binary|bool|boolean)" 113 "$", Qt::CaseInsensitive );
114 QStringList fields = url.allQueryItemValues( QStringLiteral(
"field" ) );
115 for (
int i = 0; i < fields.size(); i++ )
117 QString name = QUrl::fromPercentEncoding( fields.at( i ).toUtf8() );
118 QVariant::Type type = QVariant::String;
119 QVariant::Type subType = QVariant::Invalid;
120 QString
typeName( QStringLiteral(
"string" ) );
124 int pos = reFieldDef.indexIn( name );
127 name = name.mid( 0, pos );
128 typeName = reFieldDef.cap( 1 ).toLower();
129 if (
typeName == QLatin1String(
"int" ) ||
typeName == QLatin1String(
"integer" ) )
131 type = QVariant::Int;
132 typeName = QStringLiteral(
"integer" );
135 else if (
typeName == QLatin1String(
"int8" ) ||
typeName == QLatin1String(
"long" ) )
137 type = QVariant::LongLong;
138 typeName = QStringLiteral(
"int8" );
141 else if (
typeName == QLatin1String(
"real" ) ||
typeName == QLatin1String(
"double" ) )
143 type = QVariant::Double;
144 typeName = QStringLiteral(
"double" );
148 else if (
typeName == QLatin1String(
"date" ) )
150 type = QVariant::Date;
151 typeName = QStringLiteral(
"date" );
154 else if (
typeName == QLatin1String(
"time" ) )
156 type = QVariant::Time;
157 typeName = QStringLiteral(
"time" );
160 else if (
typeName == QLatin1String(
"datetime" ) )
162 type = QVariant::DateTime;
163 typeName = QStringLiteral(
"datetime" );
166 else if (
typeName == QLatin1String(
"bool" ) ||
typeName == QLatin1String(
"boolean" ) )
168 type = QVariant::Bool;
169 typeName = QStringLiteral(
"boolean" );
172 else if (
typeName == QLatin1String(
"binary" ) )
174 type = QVariant::ByteArray;
175 typeName = QStringLiteral(
"binary" );
179 if ( !reFieldDef.cap( 2 ).isEmpty() )
181 length = reFieldDef.cap( 2 ).toInt();
183 if ( !reFieldDef.cap( 3 ).isEmpty() )
185 precision = reFieldDef.cap( 3 ).toInt();
187 if ( !reFieldDef.cap( 4 ).isEmpty() )
191 type = ( subType == QVariant::String ? QVariant::StringList : QVariant::List );
194 if ( !name.isEmpty() )
195 attributes.append(
QgsField( name, type,
typeName, length, precision, QString(), subType ) );
197 addAttributes( attributes );
200 if ( url.hasQueryItem( QStringLiteral(
"index" ) ) && url.queryItemValue( QStringLiteral(
"index" ) ) == QLatin1String(
"yes" ) )
202 createSpatialIndex();
207 QgsMemoryProvider::~QgsMemoryProvider()
209 delete mSpatialIndex;
212 QString QgsMemoryProvider::providerKey()
214 return TEXT_PROVIDER_KEY;
217 QString QgsMemoryProvider::providerDescription()
219 return TEXT_PROVIDER_DESCRIPTION;
222 QgsMemoryProvider *QgsMemoryProvider::createProvider(
const QString &uri,
const ProviderOptions &options )
224 return new QgsMemoryProvider( uri, options );
229 return new QgsMemoryFeatureSource(
this );
232 QString QgsMemoryProvider::dataSourceUri(
bool expandAuthConfig )
const 234 Q_UNUSED( expandAuthConfig )
236 QUrl uri( QStringLiteral(
"memory" ) );
238 uri.addQueryItem( QStringLiteral(
"geometry" ), geometry );
240 if ( mCrs.isValid() )
243 QString authid = mCrs.authid();
244 if ( authid.startsWith( QLatin1String(
"EPSG:" ) ) )
250 int srid = mCrs.postgisSrid();
253 crsDef = QStringLiteral(
"postgis:%1" ).arg( srid );
257 crsDef = QStringLiteral(
"wkt:%1" ).arg( mCrs.toWkt() );
260 uri.addQueryItem( QStringLiteral(
"crs" ), crsDef );
264 uri.addQueryItem( QStringLiteral(
"index" ), QStringLiteral(
"yes" ) );
268 for (
int i = 0; i < attrs.size(); i++ )
270 QgsField field = mFields.at( attrs[i] );
271 QString fieldDef = field.
name();
272 fieldDef.append( QStringLiteral(
":%2(%3,%4)" ).arg( field.
typeName() ).arg( field.
length() ).arg( field.
precision() ) );
273 uri.addQueryItem( QStringLiteral(
"field" ), fieldDef );
276 return QString( uri.toEncoded() );
280 QString QgsMemoryProvider::storageType()
const 282 return QStringLiteral(
"Memory storage" );
287 return QgsFeatureIterator(
new QgsMemoryFeatureIterator(
new QgsMemoryFeatureSource(
this ),
true, request ) );
293 if ( mExtent.isEmpty() && !mFeatures.isEmpty() )
296 if ( mSubsetString.isEmpty() )
299 const auto constMFeatures = mFeatures;
300 for (
const QgsFeature &feat : constMFeatures )
302 if ( feat.hasGeometry() )
303 mExtent.combineExtentWith( feat.geometry().boundingBox() );
317 else if ( mFeatures.isEmpty() )
319 mExtent.setMinimal();
330 long QgsMemoryProvider::featureCount()
const 332 if ( mSubsetString.isEmpty() )
333 return mFeatures.count();
346 QgsFields QgsMemoryProvider::fields()
const 351 bool QgsMemoryProvider::isValid()
const 363 bool QgsMemoryProvider::addFeatures(
QgsFeatureList &flist, Flags )
367 bool updateExtent = mFeatures.isEmpty() || !mExtent.isEmpty();
369 int fieldCount = mFields.count();
372 for ( QgsFeatureList::iterator it = flist.begin(); it != flist.end(); ++it )
374 it->setId( mNextFeatureId );
375 it->setValid(
true );
376 if ( it->attributes().count() < fieldCount )
381 for (
int i = it->attributes().count(); i < mFields.count(); ++i )
383 attributes.append( QVariant( mFields.at( i ).type() ) );
385 it->setAttributes( attributes );
387 else if ( it->attributes().count() > fieldCount )
390 pushError( tr(
"Feature has too many attributes (expecting %1, received %2)" ).arg( fieldCount ).arg( it->attributes().count() ) );
392 attributes.resize( mFields.count() );
393 it->setAttributes( attributes );
403 pushError( tr(
"Could not add feature with geometry type %1 to layer of type %2" ).arg(
QgsWkbTypes::displayString( it->geometry().wkbType() ),
409 mFeatures.insert( mNextFeatureId, *it );
411 if ( it->hasGeometry() )
414 mExtent.combineExtentWith( it->geometry().boundingBox() );
418 mSpatialIndex->addFeature( *it );
428 bool QgsMemoryProvider::deleteFeatures(
const QgsFeatureIds &
id )
430 for ( QgsFeatureIds::const_iterator it =
id.begin(); it !=
id.end(); ++it )
432 QgsFeatureMap::iterator fit = mFeatures.find( *it );
435 if ( fit == mFeatures.end() )
440 mSpatialIndex->deleteFeature( *fit );
442 mFeatures.erase( fit );
451 bool QgsMemoryProvider::addAttributes(
const QList<QgsField> &attributes )
453 for ( QList<QgsField>::const_iterator it = attributes.begin(); it != attributes.end(); ++it )
455 switch ( it->type() )
458 case QVariant::Double:
459 case QVariant::String:
462 case QVariant::DateTime:
463 case QVariant::LongLong:
464 case QVariant::StringList:
467 case QVariant::ByteArray:
470 QgsDebugMsg(
"Field type not supported: " + it->typeName() );
474 mFields.append( *it );
476 for ( QgsFeatureMap::iterator fit = mFeatures.begin(); fit != mFeatures.end(); ++fit )
480 attr.append( QVariant() );
487 bool QgsMemoryProvider::renameAttributes(
const QgsFieldNameMap &renamedAttributes )
489 QgsFieldNameMap::const_iterator renameIt = renamedAttributes.constBegin();
491 for ( ; renameIt != renamedAttributes.constEnd(); ++renameIt )
493 int fieldIndex = renameIt.key();
494 if ( fieldIndex < 0 || fieldIndex >= mFields.count() )
499 if ( mFields.indexFromName( renameIt.value() ) >= 0 )
506 mFields[ fieldIndex ].setName( renameIt.value() );
511 bool QgsMemoryProvider::deleteAttributes(
const QgsAttributeIds &attributes )
513 QList<int> attrIdx = attributes.toList();
514 std::sort( attrIdx.begin(), attrIdx.end(), std::greater<int>() );
517 for ( QList<int>::const_iterator it = attrIdx.constBegin(); it != attrIdx.constEnd(); ++it )
520 mFields.remove( idx );
522 for ( QgsFeatureMap::iterator fit = mFeatures.begin(); fit != mFeatures.end(); ++fit )
536 for ( QgsChangedAttributesMap::const_iterator it = attr_map.begin(); it != attr_map.end(); ++it )
538 QgsFeatureMap::iterator fit = mFeatures.find( it.key() );
539 if ( fit == mFeatures.end() )
543 for ( QgsAttributeMap::const_iterator it2 = attrs.constBegin(); it2 != attrs.constEnd(); ++it2 )
544 fit->setAttribute( it2.key(), it2.value() );
550 bool QgsMemoryProvider::changeGeometryValues(
const QgsGeometryMap &geometry_map )
552 for ( QgsGeometryMap::const_iterator it = geometry_map.begin(); it != geometry_map.end(); ++it )
554 QgsFeatureMap::iterator fit = mFeatures.find( it.key() );
555 if ( fit == mFeatures.end() )
560 mSpatialIndex->deleteFeature( *fit );
562 fit->setGeometry( it.value() );
566 mSpatialIndex->addFeature( *fit );
574 QString QgsMemoryProvider::subsetString()
const 576 return mSubsetString;
579 bool QgsMemoryProvider::setSubsetString(
const QString &theSQL,
bool updateFeatureCount )
581 Q_UNUSED( updateFeatureCount )
583 if ( !theSQL.isEmpty() )
586 if ( tempExpression.hasParserError() )
590 if ( theSQL == mSubsetString )
593 mSubsetString = theSQL;
595 mExtent.setMinimal();
601 bool QgsMemoryProvider::createSpatialIndex()
603 if ( !mSpatialIndex )
608 for ( QgsFeatureMap::iterator it = mFeatures.begin(); it != mFeatures.end(); ++it )
610 mSpatialIndex->addFeature( *it );
616 QgsVectorDataProvider::Capabilities QgsMemoryProvider::capabilities()
const 618 return AddFeatures | DeleteFeatures | ChangeGeometries |
619 ChangeAttributeValues | AddAttributes | DeleteAttributes | RenameAttributes | CreateSpatialIndex |
620 SelectAtId | CircularGeometries | FastTruncate;
623 bool QgsMemoryProvider::truncate()
627 mExtent.setMinimal();
631 void QgsMemoryProvider::updateExtents()
633 mExtent.setMinimal();
636 QString QgsMemoryProvider::name()
const 638 return TEXT_PROVIDER_KEY;
641 QString QgsMemoryProvider::description()
const 643 return TEXT_PROVIDER_DESCRIPTION;
Class for parsing and evaluation of expressions (formerly called "search strings").
Wrapper for iterator of features from vector data provider or vector layer.
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
A rectangle specified with double values.
QSet< QgsFeatureId > QgsFeatureIds
void setMinimal()
Set a rectangle so that min corner is at max and max corner is at min.
QList< QgsFeature > QgsFeatureList
Container of fields for a vector layer.
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
static Type parseType(const QString &wktStr)
Attempts to extract the WKB type from a WKT string.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
const QgsCoordinateReferenceSystem & crs
const QgsAttributeList & attributeIndexes
bool hasGeometry() const
Returns true if the feature has an associated geometry.
QSet< int > QgsAttributeIds
Type
The WKB type describes the number of dimensions a geometry has.
QString typeName() const
Gets the field type.
QMap< int, QVariant > QgsAttributeMap
static GeometryType geometryType(Type type)
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
This class wraps a request for features to a vector layer (or directly its vector data provider)...
Encapsulate a field in an attribute table or data source.
Base class that can be used for any class that is capable of returning features.
QMap< int, QString > QgsFieldNameMap
A spatial index for QgsFeature objects.
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
This class represents a coordinate reference system (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...
QList< int > QgsAttributeList
bool nextFeature(QgsFeature &f)
This is the base class for vector data providers.