Skip to content

Commit ce11ebb

Browse files
authoredSep 25, 2017
Merge pull request #5251 from manisandro/orig_ocg_fid_218
[2.18][OGR] orig_ogc_fid followups
2 parents b2b494d + 2c3cbcf commit ce11ebb

File tree

6 files changed

+35
-6
lines changed

6 files changed

+35
-6
lines changed
 

‎src/providers/ogr/qgsogrfeatureiterator.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ QgsOgrFeatureIterator::QgsOgrFeatureIterator( QgsOgrFeatureSource* source, bool
152152
OGR_L_SetAttributeFilter( ogrLayer, nullptr );
153153
}
154154

155+
155156
//start with first feature
156157
rewind();
157158
}
@@ -327,11 +328,16 @@ bool QgsOgrFeatureIterator::readFeature( OGRFeatureH fet, QgsFeature& feature )
327328
{
328329
if ( mOrigFidAdded )
329330
{
331+
OGRFeatureDefnH fdef = OGR_L_GetLayerDefn( ogrLayer );
332+
int lastField = OGR_FD_GetFieldCount( fdef ) - 1;
333+
if ( lastField >= 0 )
330334
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 2000000
331-
feature.setFeatureId( OGR_F_GetFieldAsInteger64( fet, 0 ) );
335+
feature.setFeatureId( OGR_F_GetFieldAsInteger64( fet, lastField ) );
332336
#else
333-
feature.setFeatureId( OGR_F_GetFieldAsInteger( fet, 0 ) );
337+
feature.setFeatureId( OGR_F_GetFieldAsInteger( fet, lastField ) );
334338
#endif
339+
else
340+
feature.setFeatureId( OGR_F_GetFID( fet ) );
335341
}
336342
else
337343
{

‎src/providers/ogr/qgsogrprovider.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ class QgsCPLErrorHandler
9595
}
9696
};
9797
98+
static const QByteArray ORIG_OGC_FID = "orig_ogc_fid";
99+
98100
99101
bool QgsOgrProvider::convertField( QgsField &field, const QTextCodec &encoding )
100102
{
@@ -1030,7 +1032,11 @@ void QgsOgrProviderUtils::setRelevantFields( OGRLayerH ogrLayer, int fieldCount,
10301032
if ( !fetchAttributes.contains( i ) )
10311033
{
10321034
// add to ignored fields
1033-
ignoredFields.append( OGR_Fld_GetNameRef( OGR_FD_GetFieldDefn( featDefn, firstAttrIsFid ? i - 1 : i ) ) );
1035+
const char *fieldName = OGR_Fld_GetNameRef( OGR_FD_GetFieldDefn( featDefn, firstAttrIsFid ? i - 1 : i ) );
1036+
if ( qstrcmp( fieldName, ORIG_OGC_FID ) != 0 )
1037+
{
1038+
ignoredFields.append( fieldName );
1039+
}
10341040
}
10351041
}
10361042

@@ -3466,15 +3472,15 @@ OGRLayerH QgsOgrProviderUtils::setSubsetString( OGRLayerH layer, OGRDataSourceH
34663472
fidColumn = "FID";
34673473
}
34683474

3469-
QByteArray sql = sqlPart1 + ", " + fidColumn + " as orig_ogc_fid" + sqlPart3;
3475+
QByteArray sql = sqlPart1 + ", " + fidColumn + " as " + ORIG_OGC_FID + sqlPart3;
34703476
QgsDebugMsg( QString( "SQL: %1" ).arg( encoding->toUnicode( sql ) ) );
34713477
subsetLayer = OGR_DS_ExecuteSQL( ds, sql.constData(), nullptr, nullptr );
34723478

34733479
// See https://lists.osgeo.org/pipermail/qgis-developer/2017-September/049802.html
34743480
// If execute SQL fails because it did not find the fidColumn, retry with hardcoded FID
34753481
if ( !subsetLayer )
34763482
{
3477-
QByteArray sql = sqlPart1 + ", " + "FID as orig_ogc_fid" + sqlPart3;
3483+
QByteArray sql = sqlPart1 + ", " + "FID as " + ORIG_OGC_FID + sqlPart3;
34783484
QgsDebugMsg( QString( "SQL: %1" ).arg( encoding->toUnicode( sql ) ) );
34793485
subsetLayer = OGR_DS_ExecuteSQL( ds, sql.constData(), nullptr, nullptr );
34803486
}
@@ -3496,7 +3502,7 @@ OGRLayerH QgsOgrProviderUtils::setSubsetString( OGRLayerH layer, OGRDataSourceH
34963502
if ( fieldCount > 0 )
34973503
{
34983504
OGRFieldDefnH fldDef = OGR_FD_GetFieldDefn( fdef, fieldCount - 1 );
3499-
origFidAdded = qstrcmp( OGR_Fld_GetNameRef( fldDef ), "orig_ogc_fid" ) == 0;
3505+
origFidAdded = qstrcmp( OGR_Fld_GetNameRef( fldDef ), ORIG_OGC_FID ) == 0;
35003506
}
35013507
}
35023508

‎tests/src/python/test_provider_ogr_sqlite.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,18 @@ def testSubsetStringFids(self):
184184
self.assertTrue(it.nextFeature(f))
185185
self.assertTrue(f.id() == 5)
186186

187+
# Ensure that orig_ogc_fid is still retrieved even if attribute subset is passed
188+
req = QgsFeatureRequest()
189+
req.setSubsetOfAttributes([])
190+
it = vl.getFeatures(req)
191+
ids = []
192+
while it.nextFeature(f):
193+
ids.append(f.id())
194+
self.assertTrue(len(ids) == 3)
195+
self.assertTrue(3 in ids)
196+
self.assertTrue(4 in ids)
197+
self.assertTrue(5 in ids)
198+
187199
# Check that subset string is correctly set on reload
188200
vl.reload()
189201
self.assertTrue(vl.fields().at(vl.fields().count() - 1).name() == "orig_ogc_fid")

‎tests/testdata/qgis_server/wms_getfeatureinfo_filter.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Content-Type: text/xml; charset=utf-8
88
<Attribute value="2" name="id"/>
99
<Attribute value="two" name="name"/>
1010
<Attribute value="two àò" name="utf8nameè"/>
11+
<Attribute value="1" name="orig_ogc_fid"/>
1112
<BoundingBox CRS="EPSG:3857" maxx="913214.6741" minx="913214.6741" maxy="5606017.8743" miny="5606017.8743"/>
1213
<Attribute value="Point (913214.6741 5606017.8743)" type="derived" name="geometry"/>
1314
</Feature>

‎tests/testdata/qgis_server/wms_getfeatureinfo_filter_or.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ Content-Type: text/xml; charset=utf-8
88
<Attribute value="2" name="id"/>
99
<Attribute value="two" name="name"/>
1010
<Attribute value="two àò" name="utf8nameè"/>
11+
<Attribute value="1" name="orig_ogc_fid"/>
1112
<BoundingBox CRS="EPSG:3857" maxx="913214.6741" minx="913214.6741" maxy="5606017.8743" miny="5606017.8743"/>
1213
<Attribute value="Point (913214.6741 5606017.8743)" type="derived" name="geometry"/>
1314
</Feature>
1415
<Feature id="2">
1516
<Attribute value="3" name="id"/>
1617
<Attribute value="three" name="name"/>
1718
<Attribute value="three èé↓" name="utf8nameè"/>
19+
<Attribute value="2" name="orig_ogc_fid"/>
1820
<BoundingBox CRS="EPSG:3857" maxx="913204.9128" minx="913204.9128" maxy="5606011.4565" miny="5606011.4565"/>
1921
<Attribute value="Point (913204.9128 5606011.4565)" type="derived" name="geometry"/>
2022
</Feature>

‎tests/testdata/qgis_server/wms_getfeatureinfo_filter_or_utf8.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ Content-Type: text/xml; charset=utf-8
88
<Attribute value="2" name="id"/>
99
<Attribute value="two" name="name"/>
1010
<Attribute value="two àò" name="utf8nameè"/>
11+
<Attribute value="1" name="orig_ogc_fid"/>
1112
<BoundingBox CRS="EPSG:3857" maxx="913214.6741" minx="913214.6741" maxy="5606017.8743" miny="5606017.8743"/>
1213
<Attribute value="Point (913214.6741 5606017.8743)" type="derived" name="geometry"/>
1314
</Feature>
1415
<Feature id="2">
1516
<Attribute value="3" name="id"/>
1617
<Attribute value="three" name="name"/>
1718
<Attribute value="three èé↓" name="utf8nameè"/>
19+
<Attribute value="2" name="orig_ogc_fid"/>
1820
<BoundingBox CRS="EPSG:3857" maxx="913204.9128" minx="913204.9128" maxy="5606011.4565" miny="5606011.4565"/>
1921
<Attribute value="Point (913204.9128 5606011.4565)" type="derived" name="geometry"/>
2022
</Feature>

0 commit comments

Comments
 (0)
Please sign in to comment.