From 872056188f83a86b274024b0e0e9cb219b06fe68 Mon Sep 17 00:00:00 2001 From: Craig de Stigter Date: Mon, 24 Jul 2023 12:32:14 +1200 Subject: [PATCH] WFS: Don't issue STARTINDEX if feature count is small Backports https://github.com/OSGeo/gdal/pull/8146 Datasources with no primary key cannot be naturally ordered by the server, and so using the STARTINDEX argument causes a 400 error with the response: > Cannot do natural order without a primary key, please add it or > specify a manual sort over existing attributes This change avoids issuing STARTINDEX if: * the feature count is known by the time the first GetFeature request is issued * the feature count is less than the page size. In other words, this change allows us to use small datasets that have no primary key, while still allowing paging in larger datasets (as long as they have a primary key) --- gdal/ogr/ogrsf_frmts/wfs/ogrwfslayer.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/gdal/ogr/ogrsf_frmts/wfs/ogrwfslayer.cpp b/gdal/ogr/ogrsf_frmts/wfs/ogrwfslayer.cpp index b722de840576..99fd97906c9a 100644 --- a/gdal/ogr/ogrsf_frmts/wfs/ogrwfslayer.cpp +++ b/gdal/ogr/ogrsf_frmts/wfs/ogrwfslayer.cpp @@ -383,12 +383,22 @@ CPLString OGRWFSLayer::MakeGetFeatureURL(int nRequestMaxFeatures, int bRequestHi if (poDS->IsPagingAllowed() && !bRequestHits) { - osURL = CPLURLAddKVP(osURL, "STARTINDEX", - CPLSPrintf("%d", nPagingStartIndex + - poDS->GetBaseStartIndex())); nRequestMaxFeatures = poDS->GetPageSize(); + /* If the feature count is known and is less than the page size, we don't + * need to do paging. Skipping the pagination parameters improves compatibility + * with remote datasources that don't have a primary key. + * Without a primary key, the WFS server can't support paging, since there + * is no natural sort order defined. */ + if (nFeatures < 0 || + (nRequestMaxFeatures && nFeatures > nRequestMaxFeatures)) + { + osURL = + CPLURLAddKVP(osURL, "STARTINDEX", + CPLSPrintf("%d", nPagingStartIndex + + poDS->GetBaseStartIndex())); + bPagingActive = true; + } nFeatureCountRequested = nRequestMaxFeatures; - bPagingActive = true; } if (nRequestMaxFeatures)