Skip to content

Commit e3f55dc

Browse files
committed
[afs] Handle layers where the id field is not named "objectid"
1 parent 93920d2 commit e3f55dc

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

src/providers/arcgisrest/qgsafsprovider.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,25 +97,34 @@ QgsAfsProvider::QgsAfsProvider( const QString &uri )
9797
Q_NOWARN_DEPRECATED_POP
9898
}
9999

100+
QString objectIdFieldName;
101+
100102
// Read fields
101103
foreach ( const QVariant &fieldData, layerData["fields"].toList() )
102104
{
103105
const QVariantMap fieldDataMap = fieldData.toMap();
104106
const QString fieldName = fieldDataMap[QStringLiteral( "name" )].toString();
105-
QVariant::Type type = QgsArcGisRestUtils::mapEsriFieldType( fieldDataMap[QStringLiteral( "type" )].toString() );
106-
if ( fieldName == QLatin1String( "geometry" ) || fieldDataMap[QStringLiteral( "type" )].toString() == QLatin1String( "esriFieldTypeGeometry" ) )
107+
const QString fieldTypeString = fieldDataMap[QStringLiteral( "type" )].toString();
108+
QVariant::Type type = QgsArcGisRestUtils::mapEsriFieldType( fieldTypeString );
109+
if ( fieldName == QLatin1String( "geometry" ) || fieldTypeString == QLatin1String( "esriFieldTypeGeometry" ) )
107110
{
108111
// skip geometry field
109112
continue;
110113
}
114+
if ( fieldTypeString == QLatin1String( "esriFieldTypeOID" ) )
115+
{
116+
objectIdFieldName = fieldName;
117+
}
111118
if ( type == QVariant::Invalid )
112119
{
113-
QgsDebugMsg( QString( "Skipping unsupported field %1 of type %2" ).arg( fieldName, fieldDataMap[QStringLiteral( "type" )].toString() ) );
120+
QgsDebugMsg( QString( "Skipping unsupported field %1 of type %2" ).arg( fieldName, fieldTypeString ) );
114121
continue;
115122
}
116123
QgsField field( fieldName, type, fieldDataMap[QStringLiteral( "type" )].toString(), fieldDataMap[QStringLiteral( "length" )].toInt() );
117124
mSharedData->mFields.append( field );
118125
}
126+
if ( objectIdFieldName.isEmpty() )
127+
objectIdFieldName = QStringLiteral( "objectid" );
119128

120129
// Determine geometry type
121130
bool hasM = layerData[QStringLiteral( "hasM" )].toBool();
@@ -131,7 +140,7 @@ QgsAfsProvider::QgsAfsProvider( const QString &uri )
131140
// Read OBJECTIDs of all features: these may not be a continuous sequence,
132141
// and we need to store these to iterate through the features. This query
133142
// also returns the name of the ObjectID field.
134-
QVariantMap objectIdData = QgsArcGisRestUtils::getObjectIds( mSharedData->mDataSource.param( QStringLiteral( "url" ) ), errorTitle, errorMessage );
143+
QVariantMap objectIdData = QgsArcGisRestUtils::getObjectIds( mSharedData->mDataSource.param( QStringLiteral( "url" ) ), objectIdFieldName, errorTitle, errorMessage );
135144
if ( objectIdData.isEmpty() )
136145
{
137146
appendError( QgsErrorMessage( tr( "getObjectIds failed: %1 - %2" ).arg( errorTitle, errorMessage ), QStringLiteral( "AFSProvider" ) ) );
@@ -142,7 +151,7 @@ QgsAfsProvider::QgsAfsProvider( const QString &uri )
142151
appendError( QgsErrorMessage( tr( "Failed to determine objectIdFieldName and/or objectIds" ), QStringLiteral( "AFSProvider" ) ) );
143152
return;
144153
}
145-
QString objectIdFieldName = objectIdData[QStringLiteral( "objectIdFieldName" )].toString();
154+
objectIdFieldName = objectIdData[QStringLiteral( "objectIdFieldName" )].toString();
146155
for ( int idx = 0, nIdx = mSharedData->mFields.count(); idx < nIdx; ++idx )
147156
{
148157
if ( mSharedData->mFields.at( idx ).name() == objectIdFieldName )

src/providers/arcgisrest/qgsarcgisrestutils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,12 +356,12 @@ QVariantMap QgsArcGisRestUtils::getLayerInfo( const QString &layerurl, QString &
356356
return queryServiceJSON( queryUrl, errorTitle, errorText );
357357
}
358358

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

src/providers/arcgisrest/qgsarcgisrestutils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class QgsArcGisRestUtils
3737

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

0 commit comments

Comments
 (0)