Skip to content
Permalink
Browse files

[afs] Handle layers where the id field is not named "objectid"

  • Loading branch information
nyalldawson committed Feb 20, 2018
1 parent 93920d2 commit e3f55dc5499ccb28933b168f36cf6b586cf0b10b
@@ -97,25 +97,34 @@ QgsAfsProvider::QgsAfsProvider( const QString &uri )
Q_NOWARN_DEPRECATED_POP
}

QString objectIdFieldName;

// Read fields
foreach ( const QVariant &fieldData, layerData["fields"].toList() )
{
const QVariantMap fieldDataMap = fieldData.toMap();
const QString fieldName = fieldDataMap[QStringLiteral( "name" )].toString();
QVariant::Type type = QgsArcGisRestUtils::mapEsriFieldType( fieldDataMap[QStringLiteral( "type" )].toString() );
if ( fieldName == QLatin1String( "geometry" ) || fieldDataMap[QStringLiteral( "type" )].toString() == QLatin1String( "esriFieldTypeGeometry" ) )
const QString fieldTypeString = fieldDataMap[QStringLiteral( "type" )].toString();
QVariant::Type type = QgsArcGisRestUtils::mapEsriFieldType( fieldTypeString );
if ( fieldName == QLatin1String( "geometry" ) || fieldTypeString == QLatin1String( "esriFieldTypeGeometry" ) )
{
// skip geometry field
continue;
}
if ( fieldTypeString == QLatin1String( "esriFieldTypeOID" ) )
{
objectIdFieldName = fieldName;
}
if ( type == QVariant::Invalid )
{
QgsDebugMsg( QString( "Skipping unsupported field %1 of type %2" ).arg( fieldName, fieldDataMap[QStringLiteral( "type" )].toString() ) );
QgsDebugMsg( QString( "Skipping unsupported field %1 of type %2" ).arg( fieldName, fieldTypeString ) );
continue;
}
QgsField field( fieldName, type, fieldDataMap[QStringLiteral( "type" )].toString(), fieldDataMap[QStringLiteral( "length" )].toInt() );
mSharedData->mFields.append( field );
}
if ( objectIdFieldName.isEmpty() )
objectIdFieldName = QStringLiteral( "objectid" );

// Determine geometry type
bool hasM = layerData[QStringLiteral( "hasM" )].toBool();
@@ -131,7 +140,7 @@ QgsAfsProvider::QgsAfsProvider( const QString &uri )
// Read OBJECTIDs of all features: these may not be a continuous sequence,
// and we need to store these to iterate through the features. This query
// also returns the name of the ObjectID field.
QVariantMap objectIdData = QgsArcGisRestUtils::getObjectIds( mSharedData->mDataSource.param( QStringLiteral( "url" ) ), errorTitle, errorMessage );
QVariantMap objectIdData = QgsArcGisRestUtils::getObjectIds( mSharedData->mDataSource.param( QStringLiteral( "url" ) ), objectIdFieldName, errorTitle, errorMessage );
if ( objectIdData.isEmpty() )
{
appendError( QgsErrorMessage( tr( "getObjectIds failed: %1 - %2" ).arg( errorTitle, errorMessage ), QStringLiteral( "AFSProvider" ) ) );
@@ -142,7 +151,7 @@ QgsAfsProvider::QgsAfsProvider( const QString &uri )
appendError( QgsErrorMessage( tr( "Failed to determine objectIdFieldName and/or objectIds" ), QStringLiteral( "AFSProvider" ) ) );
return;
}
QString objectIdFieldName = objectIdData[QStringLiteral( "objectIdFieldName" )].toString();
objectIdFieldName = objectIdData[QStringLiteral( "objectIdFieldName" )].toString();
for ( int idx = 0, nIdx = mSharedData->mFields.count(); idx < nIdx; ++idx )
{
if ( mSharedData->mFields.at( idx ).name() == objectIdFieldName )
@@ -356,12 +356,12 @@ QVariantMap QgsArcGisRestUtils::getLayerInfo( const QString &layerurl, QString &
return queryServiceJSON( queryUrl, errorTitle, errorText );
}

QVariantMap QgsArcGisRestUtils::getObjectIds( const QString &layerurl, QString &errorTitle, QString &errorText )
QVariantMap QgsArcGisRestUtils::getObjectIds( const QString &layerurl, const QString &objectIdFieldName, QString &errorTitle, QString &errorText )
{
// http://sampleserver5.arcgisonline.com/arcgis/rest/services/Energy/Geology/FeatureServer/1/query?where=objectid%3Dobjectid&returnIdsOnly=true&f=json
QUrl queryUrl( layerurl + "/query" );
queryUrl.addQueryItem( QStringLiteral( "f" ), QStringLiteral( "json" ) );
queryUrl.addQueryItem( QStringLiteral( "where" ), QStringLiteral( "objectid=objectid" ) );
queryUrl.addQueryItem( QStringLiteral( "where" ), QStringLiteral( "%1=%1" ).arg( objectIdFieldName ) );
queryUrl.addQueryItem( QStringLiteral( "returnIdsOnly" ), QStringLiteral( "true" ) );
return queryServiceJSON( queryUrl, errorTitle, errorText );
}
@@ -37,7 +37,7 @@ class QgsArcGisRestUtils

static QVariantMap getServiceInfo( const QString &baseurl, QString &errorTitle, QString &errorText );
static QVariantMap getLayerInfo( const QString &layerurl, QString &errorTitle, QString &errorText );
static QVariantMap getObjectIds( const QString &layerurl, QString &errorTitle, QString &errorText );
static QVariantMap getObjectIds( const QString &layerurl, const QString &objectIdFieldName, QString &errorTitle, QString &errorText );
static QVariantMap getObjects( const QString &layerurl, const QList<quint32> &objectIds, const QString &crs,
bool fetchGeometry, const QStringList &fetchAttributes, bool fetchM, bool fetchZ,
const QgsRectangle &filterRect, QString &errorTitle, QString &errorText );

0 comments on commit e3f55dc

Please sign in to comment.
You can’t perform that action at this time.