Skip to content

Commit

Permalink
[afs] Handle layers where the id field is not named "objectid"
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Feb 20, 2018
1 parent 93920d2 commit e3f55dc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
19 changes: 14 additions & 5 deletions src/providers/arcgisrest/qgsafsprovider.cpp
Expand Up @@ -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();
Expand All @@ -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" ) ) );
Expand All @@ -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 )
Expand Down
4 changes: 2 additions & 2 deletions src/providers/arcgisrest/qgsarcgisrestutils.cpp
Expand Up @@ -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 );
}
Expand Down
2 changes: 1 addition & 1 deletion src/providers/arcgisrest/qgsarcgisrestutils.h
Expand Up @@ -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 );
Expand Down

0 comments on commit e3f55dc

Please sign in to comment.