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 Q_FOREACH (
const QgsFeature &feat, mFeatures )
316 else if ( mFeatures.isEmpty() )
318 mExtent.setMinimal();
329 long QgsMemoryProvider::featureCount()
const 331 if ( mSubsetString.isEmpty() )
332 return mFeatures.count();
345 QgsFields QgsMemoryProvider::fields()
const 350 bool QgsMemoryProvider::isValid()
const 362 bool QgsMemoryProvider::addFeatures(
QgsFeatureList &flist, Flags )
366 bool updateExtent = mFeatures.isEmpty() || !mExtent.isEmpty();
368 int fieldCount = mFields.count();
371 for ( QgsFeatureList::iterator it = flist.begin(); it != flist.end(); ++it )
373 it->setId( mNextFeatureId );
374 it->setValid(
true );
375 if ( it->attributes().count() < fieldCount )
380 for (
int i = it->attributes().count(); i < mFields.count(); ++i )
382 attributes.append( QVariant( mFields.at( i ).type() ) );
384 it->setAttributes( attributes );
386 else if ( it->attributes().count() > fieldCount )
389 pushError( tr(
"Feature has too many attributes (expecting %1, received %2)" ).arg( fieldCount ).arg( it->attributes().count() ) );
391 attributes.resize( mFields.count() );
392 it->setAttributes( attributes );
402 pushError( tr(
"Could not add feature with geometry type %1 to layer of type %2" ).arg(
QgsWkbTypes::displayString( it->geometry().wkbType() ),
408 mFeatures.insert( mNextFeatureId, *it );
410 if ( it->hasGeometry() )
413 mExtent.combineExtentWith( it->geometry().boundingBox() );
417 mSpatialIndex->addFeature( *it );
427 bool QgsMemoryProvider::deleteFeatures(
const QgsFeatureIds &
id )
429 for ( QgsFeatureIds::const_iterator it =
id.begin(); it !=
id.end(); ++it )
431 QgsFeatureMap::iterator fit = mFeatures.find( *it );
434 if ( fit == mFeatures.end() )
439 mSpatialIndex->deleteFeature( *fit );
441 mFeatures.erase( fit );
450 bool QgsMemoryProvider::addAttributes(
const QList<QgsField> &attributes )
452 for ( QList<QgsField>::const_iterator it = attributes.begin(); it != attributes.end(); ++it )
454 switch ( it->type() )
457 case QVariant::Double:
458 case QVariant::String:
461 case QVariant::DateTime:
462 case QVariant::LongLong:
463 case QVariant::StringList:
466 case QVariant::ByteArray:
469 QgsDebugMsg(
"Field type not supported: " + it->typeName() );
473 mFields.append( *it );
475 for ( QgsFeatureMap::iterator fit = mFeatures.begin(); fit != mFeatures.end(); ++fit )
479 attr.append( QVariant() );
486 bool QgsMemoryProvider::renameAttributes(
const QgsFieldNameMap &renamedAttributes )
488 QgsFieldNameMap::const_iterator renameIt = renamedAttributes.constBegin();
490 for ( ; renameIt != renamedAttributes.constEnd(); ++renameIt )
492 int fieldIndex = renameIt.key();
493 if ( fieldIndex < 0 || fieldIndex >= mFields.count() )
498 if ( mFields.indexFromName( renameIt.value() ) >= 0 )
505 mFields[ fieldIndex ].setName( renameIt.value() );
510 bool QgsMemoryProvider::deleteAttributes(
const QgsAttributeIds &attributes )
512 QList<int> attrIdx = attributes.toList();
513 std::sort( attrIdx.begin(), attrIdx.end(), std::greater<int>() );
516 for ( QList<int>::const_iterator it = attrIdx.constBegin(); it != attrIdx.constEnd(); ++it )
519 mFields.remove( idx );
521 for ( QgsFeatureMap::iterator fit = mFeatures.begin(); fit != mFeatures.end(); ++fit )
535 for ( QgsChangedAttributesMap::const_iterator it = attr_map.begin(); it != attr_map.end(); ++it )
537 QgsFeatureMap::iterator fit = mFeatures.find( it.key() );
538 if ( fit == mFeatures.end() )
542 for ( QgsAttributeMap::const_iterator it2 = attrs.constBegin(); it2 != attrs.constEnd(); ++it2 )
543 fit->setAttribute( it2.key(), it2.value() );
549 bool QgsMemoryProvider::changeGeometryValues(
const QgsGeometryMap &geometry_map )
551 for ( QgsGeometryMap::const_iterator it = geometry_map.begin(); it != geometry_map.end(); ++it )
553 QgsFeatureMap::iterator fit = mFeatures.find( it.key() );
554 if ( fit == mFeatures.end() )
559 mSpatialIndex->deleteFeature( *fit );
561 fit->setGeometry( it.value() );
565 mSpatialIndex->addFeature( *fit );
573 QString QgsMemoryProvider::subsetString()
const 575 return mSubsetString;
578 bool QgsMemoryProvider::setSubsetString(
const QString &theSQL,
bool updateFeatureCount )
580 Q_UNUSED( updateFeatureCount );
582 if ( !theSQL.isEmpty() )
585 if ( tempExpression.hasParserError() )
589 if ( theSQL == mSubsetString )
592 mSubsetString = theSQL;
594 mExtent.setMinimal();
600 bool QgsMemoryProvider::createSpatialIndex()
602 if ( !mSpatialIndex )
607 for ( QgsFeatureMap::iterator it = mFeatures.begin(); it != mFeatures.end(); ++it )
609 mSpatialIndex->addFeature( *it );
615 QgsVectorDataProvider::Capabilities QgsMemoryProvider::capabilities()
const 617 return AddFeatures | DeleteFeatures | ChangeGeometries |
618 ChangeAttributeValues | AddAttributes | DeleteAttributes | RenameAttributes | CreateSpatialIndex |
619 SelectAtId | CircularGeometries | FastTruncate;
622 bool QgsMemoryProvider::truncate()
626 mExtent.setMinimal();
630 void QgsMemoryProvider::updateExtents()
632 mExtent.setMinimal();
635 QString QgsMemoryProvider::name()
const 637 return TEXT_PROVIDER_KEY;
640 QString QgsMemoryProvider::description()
const 642 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.