Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: WFS Translator
4 : * Purpose: Implements OGRWFSLayer class.
5 : * Author: Even Rouault, <even dot rouault at spatialys.com>
6 : *
7 : ******************************************************************************
8 : * Copyright (c) 2010-2013, Even Rouault <even dot rouault at spatialys.com>
9 : *
10 : * SPDX-License-Identifier: MIT
11 : ****************************************************************************/
12 :
13 : #include "cpl_port.h"
14 : #include "ogr_wfs.h"
15 : #include "ogr_api.h"
16 : #include "cpl_minixml.h"
17 : #include "cpl_http.h"
18 : #include "parsexsd.h"
19 : #include "ogrwfsfilter.h"
20 :
21 : /************************************************************************/
22 : /* OGRWFSLayer() */
23 : /************************************************************************/
24 :
25 209 : OGRWFSLayer::OGRWFSLayer(OGRWFSDataSource *poDSIn, OGRSpatialReference *poSRSIn,
26 : int bAxisOrderAlreadyInvertedIn,
27 : const char *pszBaseURLIn, const char *pszNameIn,
28 209 : const char *pszNSIn, const char *pszNSValIn)
29 : : poDS(poDSIn), poFeatureDefn(nullptr), bGotApproximateLayerDefn(false),
30 : poGMLFeatureClass(nullptr),
31 : bAxisOrderAlreadyInverted(bAxisOrderAlreadyInvertedIn), m_poSRS(poSRSIn),
32 209 : pszBaseURL(CPLStrdup(pszBaseURLIn)), pszName(CPLStrdup(pszNameIn)),
33 209 : pszNS(pszNSIn ? CPLStrdup(pszNSIn) : nullptr),
34 209 : pszNSVal(pszNSValIn ? CPLStrdup(pszNSValIn) : nullptr),
35 : bStreamingDS(false), poBaseDS(nullptr), poBaseLayer(nullptr),
36 : bHasFetched(false), bReloadNeeded(false), eGeomType(wkbUnknown),
37 : nFeatures(-1), bCountFeaturesInGetNextFeature(false),
38 : poFetchedFilterGeom(nullptr), nExpectedInserts(0), bInTransaction(false),
39 : bUseFeatureIdAtLayerLevel(false), bPagingActive(false),
40 836 : nPagingStartIndex(0), nFeatureRead(0), pszRequiredOutputFormat(nullptr)
41 : {
42 209 : SetDescription(pszName);
43 :
44 : // If changing that, change in the GML driver too
45 209 : m_osTmpDir = VSIMemGenerateHiddenFilename("_ogr_wfs_");
46 209 : }
47 :
48 : /************************************************************************/
49 : /* Clone() */
50 : /************************************************************************/
51 :
52 0 : OGRWFSLayer *OGRWFSLayer::Clone()
53 : {
54 : OGRWFSLayer *poDupLayer =
55 0 : new OGRWFSLayer(poDS, m_poSRS, bAxisOrderAlreadyInverted, pszBaseURL,
56 0 : pszName, pszNS, pszNSVal);
57 0 : if (m_poSRS)
58 0 : m_poSRS->Reference();
59 0 : poDupLayer->poFeatureDefn = GetLayerDefn()->Clone();
60 0 : poDupLayer->poFeatureDefn->Reference();
61 0 : poDupLayer->bGotApproximateLayerDefn = bGotApproximateLayerDefn;
62 0 : poDupLayer->eGeomType = poDupLayer->poFeatureDefn->GetGeomType();
63 0 : poDupLayer->pszRequiredOutputFormat =
64 0 : pszRequiredOutputFormat ? CPLStrdup(pszRequiredOutputFormat) : nullptr;
65 :
66 : /* Copy existing schema file if already found */
67 0 : CPLString osSrcFileName = CPLSPrintf("%s/file.xsd", m_osTmpDir.c_str());
68 : CPLString osTargetFileName =
69 0 : CPLSPrintf("%s/file.xsd", poDupLayer->m_osTmpDir.c_str());
70 0 : CPL_IGNORE_RET_VAL(CPLCopyFile(osTargetFileName, osSrcFileName));
71 :
72 0 : return poDupLayer;
73 : }
74 :
75 : /************************************************************************/
76 : /* ~OGRWFSLayer() */
77 : /************************************************************************/
78 :
79 418 : OGRWFSLayer::~OGRWFSLayer()
80 :
81 : {
82 209 : if (bInTransaction)
83 0 : OGRWFSLayer::CommitTransaction();
84 :
85 209 : if (m_poSRS != nullptr)
86 191 : m_poSRS->Release();
87 :
88 209 : if (poFeatureDefn != nullptr)
89 157 : poFeatureDefn->Release();
90 209 : delete poGMLFeatureClass;
91 :
92 209 : CPLFree(pszBaseURL);
93 209 : CPLFree(pszName);
94 209 : CPLFree(pszNS);
95 209 : CPLFree(pszNSVal);
96 :
97 209 : GDALClose(poBaseDS);
98 :
99 209 : delete poFetchedFilterGeom;
100 :
101 209 : VSIRmdirRecursive(m_osTmpDir.c_str());
102 :
103 209 : CPLFree(pszRequiredOutputFormat);
104 418 : }
105 :
106 : /************************************************************************/
107 : /* SetActiveSRS() */
108 : /************************************************************************/
109 :
110 4 : OGRErr OGRWFSLayer::SetActiveSRS(int /*iGeomField*/,
111 : const OGRSpatialReference *poSRS)
112 : {
113 4 : if (poSRS == nullptr)
114 1 : return OGRERR_FAILURE;
115 3 : const char *const apszOptions[] = {
116 : "IGNORE_DATA_AXIS_TO_SRS_AXIS_MAPPING=YES", nullptr};
117 3 : size_t i = 0;
118 9 : for (const auto &poSupportedSRS : m_apoSupportedCRSList)
119 : {
120 8 : if (poSupportedSRS->IsSame(poSRS, apszOptions))
121 : {
122 2 : m_osSRSName = m_aosSupportedCRSList[i];
123 2 : if (m_poSRS)
124 2 : m_poSRS->Release();
125 2 : m_poSRS = poSRS->Clone();
126 2 : m_poSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
127 2 : if (poFeatureDefn)
128 : {
129 0 : auto poGeomFieldDefn = poFeatureDefn->GetGeomFieldDefn(0);
130 0 : if (poGeomFieldDefn)
131 : {
132 0 : poGeomFieldDefn->SetSpatialRef(m_poSRS);
133 : }
134 : }
135 2 : m_oExtents = OGREnvelope();
136 2 : if (m_oWGS84Extents.IsInit())
137 : {
138 4 : OGRSpatialReference oWGS84;
139 2 : oWGS84.SetWellKnownGeogCS("WGS84");
140 2 : oWGS84.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
141 : auto poCT = std::unique_ptr<OGRCoordinateTransformation>(
142 4 : OGRCreateCoordinateTransformation(&oWGS84, m_poSRS));
143 2 : if (poCT)
144 : {
145 2 : poCT->TransformBounds(
146 : m_oWGS84Extents.MinX, m_oWGS84Extents.MinY,
147 : m_oWGS84Extents.MaxX, m_oWGS84Extents.MaxY,
148 : &m_oExtents.MinX, &m_oExtents.MinY, &m_oExtents.MaxX,
149 2 : &m_oExtents.MaxY, 20);
150 : }
151 : }
152 2 : SetSpatialFilter(nullptr);
153 2 : ResetReading();
154 2 : return OGRERR_NONE;
155 : }
156 6 : ++i;
157 : }
158 1 : return OGRERR_FAILURE;
159 : }
160 :
161 : /************************************************************************/
162 : /* GetDescribeFeatureTypeURL() */
163 : /************************************************************************/
164 :
165 157 : CPLString OGRWFSLayer::GetDescribeFeatureTypeURL(CPL_UNUSED int bWithNS)
166 : {
167 157 : CPLString osURL(pszBaseURL);
168 157 : osURL = CPLURLAddKVP(osURL, "SERVICE", "WFS");
169 157 : osURL = CPLURLAddKVP(osURL, "VERSION", poDS->GetVersion());
170 157 : osURL = CPLURLAddKVP(osURL, "REQUEST", "DescribeFeatureType");
171 157 : osURL = CPLURLAddKVP(osURL, "TYPENAME", WFS_EscapeURL(pszName));
172 157 : osURL = CPLURLAddKVP(osURL, "PROPERTYNAME", nullptr);
173 157 : osURL = CPLURLAddKVP(osURL, "MAXFEATURES", nullptr);
174 157 : osURL = CPLURLAddKVP(osURL, "COUNT", nullptr);
175 157 : osURL = CPLURLAddKVP(osURL, "FILTER", nullptr);
176 314 : osURL = CPLURLAddKVP(osURL, "OUTPUTFORMAT",
177 157 : pszRequiredOutputFormat
178 157 : ? WFS_EscapeURL(pszRequiredOutputFormat).c_str()
179 157 : : nullptr);
180 :
181 157 : if (pszNS && poDS->GetNeedNAMESPACE())
182 : {
183 : /* Older Deegree version require NAMESPACE (e.g.
184 : * http://www.nokis.org/deegree2/ogcwebservice) */
185 : /* This has been now corrected */
186 0 : CPLString osValue("xmlns(");
187 0 : osValue += pszNS;
188 0 : osValue += "=";
189 0 : osValue += pszNSVal;
190 0 : osValue += ")";
191 0 : osURL = CPLURLAddKVP(osURL, "NAMESPACE", WFS_EscapeURL(osValue));
192 : }
193 :
194 157 : return osURL;
195 : }
196 :
197 : /************************************************************************/
198 : /* DescribeFeatureType() */
199 : /************************************************************************/
200 :
201 95 : OGRFeatureDefn *OGRWFSLayer::DescribeFeatureType()
202 : {
203 190 : CPLString osURL = GetDescribeFeatureTypeURL(TRUE);
204 :
205 95 : CPLDebug("WFS", "%s", osURL.c_str());
206 :
207 95 : CPLHTTPResult *psResult = poDS->HTTPFetch(osURL, nullptr);
208 95 : if (psResult == nullptr)
209 : {
210 11 : return nullptr;
211 : }
212 :
213 84 : if (strstr((const char *)psResult->pabyData, "<ServiceExceptionReport") !=
214 : nullptr)
215 : {
216 0 : if (poDS->IsOldDeegree((const char *)psResult->pabyData))
217 : {
218 0 : CPLHTTPDestroyResult(psResult);
219 0 : return DescribeFeatureType();
220 : }
221 0 : CPLError(CE_Failure, CPLE_AppDefined, "Error returned by server : %s",
222 : psResult->pabyData);
223 0 : CPLHTTPDestroyResult(psResult);
224 0 : return nullptr;
225 : }
226 :
227 84 : CPLXMLNode *psXML = CPLParseXMLString((const char *)psResult->pabyData);
228 84 : if (psXML == nullptr)
229 : {
230 1 : CPLError(CE_Failure, CPLE_AppDefined, "Invalid XML content : %s",
231 : psResult->pabyData);
232 1 : CPLHTTPDestroyResult(psResult);
233 1 : return nullptr;
234 : }
235 83 : CPLHTTPDestroyResult(psResult);
236 :
237 83 : const CPLXMLNode *psSchema = WFSFindNode(psXML, "schema");
238 83 : if (psSchema == nullptr)
239 : {
240 1 : CPLError(CE_Failure, CPLE_AppDefined, "Cannot find <Schema>");
241 1 : CPLDestroyXMLNode(psXML);
242 :
243 1 : return nullptr;
244 : }
245 :
246 82 : OGRFeatureDefn *poFDefn = ParseSchema(psSchema);
247 82 : if (poFDefn)
248 77 : poDS->SaveLayerSchema(pszName, psSchema);
249 :
250 82 : CPLDestroyXMLNode(psXML);
251 82 : return poFDefn;
252 : }
253 :
254 : /************************************************************************/
255 : /* ParseSchema() */
256 : /************************************************************************/
257 :
258 144 : OGRFeatureDefn *OGRWFSLayer::ParseSchema(const CPLXMLNode *psSchema)
259 : {
260 144 : osTargetNamespace = CPLGetXMLValue(psSchema, "targetNamespace", "");
261 :
262 288 : CPLString osTmpFileName;
263 :
264 144 : osTmpFileName = CPLSPrintf("%s/file.xsd", m_osTmpDir.c_str());
265 144 : CPLSerializeXMLTreeToFile(psSchema, osTmpFileName);
266 :
267 288 : std::vector<GMLFeatureClass *> aosClasses;
268 144 : bool bFullyUnderstood = false;
269 144 : bool bUseSchemaImports = false;
270 144 : bool bHaveSchema = GMLParseXSD(osTmpFileName, bUseSchemaImports, aosClasses,
271 : bFullyUnderstood);
272 :
273 144 : if (bHaveSchema && aosClasses.size() == 1)
274 : {
275 : // CPLDebug("WFS", "Creating %s for %s", osTmpFileName.c_str(),
276 : // GetName());
277 139 : return BuildLayerDefnFromFeatureClass(aosClasses[0]);
278 : }
279 5 : else if (bHaveSchema)
280 : {
281 : std::vector<GMLFeatureClass *>::const_iterator oIter =
282 0 : aosClasses.begin();
283 : std::vector<GMLFeatureClass *>::const_iterator oEndIter =
284 0 : aosClasses.end();
285 0 : while (oIter != oEndIter)
286 : {
287 0 : GMLFeatureClass *poClass = *oIter;
288 0 : ++oIter;
289 0 : delete poClass;
290 : }
291 : }
292 :
293 5 : VSIUnlink(osTmpFileName);
294 :
295 5 : return nullptr;
296 : }
297 :
298 : /************************************************************************/
299 : /* BuildLayerDefnFromFeatureClass() */
300 : /************************************************************************/
301 :
302 : OGRFeatureDefn *
303 139 : OGRWFSLayer::BuildLayerDefnFromFeatureClass(GMLFeatureClass *poClass)
304 : {
305 139 : poGMLFeatureClass = poClass;
306 :
307 139 : OGRFeatureDefn *poFDefn = new OGRFeatureDefn(pszName);
308 139 : poFDefn->SetGeomType(wkbNone);
309 139 : if (poGMLFeatureClass->GetGeometryPropertyCount() > 0)
310 : {
311 124 : poFDefn->SetGeomType(
312 124 : (OGRwkbGeometryType)poGMLFeatureClass->GetGeometryProperty(0)
313 124 : ->GetType());
314 124 : poFDefn->GetGeomFieldDefn(0)->SetSpatialRef(m_poSRS);
315 : }
316 :
317 : /* -------------------------------------------------------------------- */
318 : /* Added attributes (properties). */
319 : /* -------------------------------------------------------------------- */
320 139 : if (poDS->ExposeGMLId())
321 : {
322 272 : OGRFieldDefn oField("gml_id", OFTString);
323 136 : oField.SetNullable(FALSE);
324 136 : poFDefn->AddFieldDefn(&oField);
325 : }
326 :
327 686 : for (int iField = 0; iField < poGMLFeatureClass->GetPropertyCount();
328 : iField++)
329 : {
330 547 : GMLPropertyDefn *poProperty = poGMLFeatureClass->GetProperty(iField);
331 547 : OGRFieldSubType eSubType = OFSTNone;
332 : const OGRFieldType eFType =
333 547 : GML_GetOGRFieldType(poProperty->GetType(), eSubType);
334 :
335 1094 : OGRFieldDefn oField(poProperty->GetName(), eFType);
336 547 : oField.SetSubType(eSubType);
337 547 : if (STARTS_WITH_CI(oField.GetNameRef(), "ogr:"))
338 0 : oField.SetName(poProperty->GetName() + 4);
339 547 : if (poProperty->GetWidth() > 0)
340 0 : oField.SetWidth(poProperty->GetWidth());
341 547 : if (poProperty->GetPrecision() > 0)
342 0 : oField.SetPrecision(poProperty->GetPrecision());
343 547 : if (!poDS->IsEmptyAsNull())
344 0 : oField.SetNullable(poProperty->IsNullable());
345 :
346 547 : poFDefn->AddFieldDefn(&oField);
347 : }
348 :
349 139 : if (poGMLFeatureClass->GetGeometryPropertyCount() > 0)
350 : {
351 : const char *pszGeometryColumnName =
352 124 : poGMLFeatureClass->GetGeometryProperty(0)->GetSrcElement();
353 124 : if (pszGeometryColumnName[0] != '\0')
354 : {
355 124 : osGeometryColumnName = pszGeometryColumnName;
356 124 : if (poFDefn->GetGeomFieldCount() > 0)
357 : {
358 248 : poFDefn->GetGeomFieldDefn(0)->SetNullable(
359 124 : poGMLFeatureClass->GetGeometryProperty(0)->IsNullable());
360 124 : poFDefn->GetGeomFieldDefn(0)->SetName(pszGeometryColumnName);
361 : }
362 : }
363 : }
364 :
365 139 : return poFDefn;
366 : }
367 :
368 : /************************************************************************/
369 : /* MakeGetFeatureURL() */
370 : /************************************************************************/
371 :
372 136 : CPLString OGRWFSLayer::MakeGetFeatureURL(int nRequestMaxFeatures,
373 : int bRequestHits)
374 : {
375 136 : CPLString osURL(pszBaseURL);
376 136 : osURL = CPLURLAddKVP(osURL, "SERVICE", "WFS");
377 136 : osURL = CPLURLAddKVP(osURL, "VERSION", poDS->GetVersion());
378 136 : osURL = CPLURLAddKVP(osURL, "REQUEST", "GetFeature");
379 136 : if (atoi(poDS->GetVersion()) >= 2)
380 30 : osURL = CPLURLAddKVP(osURL, "TYPENAMES", WFS_EscapeURL(pszName));
381 : else
382 106 : osURL = CPLURLAddKVP(osURL, "TYPENAME", WFS_EscapeURL(pszName));
383 136 : if (!m_osSRSName.empty())
384 : osURL =
385 2 : CPLURLAddKVP(osURL, "SRSNAME", WFS_EscapeURL(m_osSRSName.c_str()));
386 136 : if (pszRequiredOutputFormat)
387 0 : osURL = CPLURLAddKVP(osURL, "OUTPUTFORMAT",
388 0 : WFS_EscapeURL(pszRequiredOutputFormat));
389 :
390 136 : if (poDS->IsPagingAllowed() && !bRequestHits)
391 : {
392 20 : nRequestMaxFeatures = poDS->GetPageSize();
393 : /* If the feature count is known and is less than the page size, we don't
394 : * need to do paging. Skipping the pagination parameters improves compatibility
395 : * with remote datasources that don't have a primary key.
396 : * Without a primary key, the WFS server can't support paging, since there
397 : * is no natural sort order defined. */
398 20 : if (nFeatures < 0 ||
399 6 : (nRequestMaxFeatures && nFeatures > nRequestMaxFeatures))
400 : {
401 : osURL =
402 36 : CPLURLAddKVP(osURL, "STARTINDEX",
403 18 : CPLSPrintf("%d", nPagingStartIndex +
404 36 : poDS->GetBaseStartIndex()));
405 18 : bPagingActive = true;
406 : }
407 : }
408 :
409 136 : if (nRequestMaxFeatures)
410 : {
411 114 : osURL = CPLURLAddKVP(
412 38 : osURL, atoi(poDS->GetVersion()) >= 2 ? "COUNT" : "MAXFEATURES",
413 38 : CPLSPrintf("%d", nRequestMaxFeatures));
414 : }
415 136 : if (pszNS && poDS->GetNeedNAMESPACE())
416 : {
417 : /* Older Deegree version require NAMESPACE (e.g.
418 : * http://www.nokis.org/deegree2/ogcwebservice) */
419 : /* This has been now corrected */
420 0 : CPLString osValue("xmlns(");
421 0 : osValue += pszNS;
422 0 : osValue += "=";
423 0 : osValue += pszNSVal;
424 0 : osValue += ")";
425 0 : osURL = CPLURLAddKVP(osURL, "NAMESPACE", WFS_EscapeURL(osValue));
426 : }
427 :
428 136 : delete poFetchedFilterGeom;
429 136 : poFetchedFilterGeom = nullptr;
430 :
431 272 : CPLString osGeomFilter;
432 :
433 136 : if (m_poFilterGeom != nullptr && !osGeometryColumnName.empty())
434 : {
435 5 : OGREnvelope oEnvelope;
436 5 : m_poFilterGeom->getEnvelope(&oEnvelope);
437 :
438 5 : poFetchedFilterGeom = m_poFilterGeom->clone();
439 :
440 5 : osGeomFilter = "<BBOX>";
441 5 : if (atoi(poDS->GetVersion()) >= 2)
442 0 : osGeomFilter += "<ValueReference>";
443 : else
444 5 : osGeomFilter += "<PropertyName>";
445 5 : if (pszNS)
446 : {
447 0 : osGeomFilter += pszNS;
448 0 : osGeomFilter += ":";
449 : }
450 5 : osGeomFilter += osGeometryColumnName;
451 5 : if (atoi(poDS->GetVersion()) >= 2)
452 0 : osGeomFilter += "</ValueReference>";
453 : else
454 5 : osGeomFilter += "</PropertyName>";
455 :
456 5 : if (atoi(poDS->GetVersion()) >= 2)
457 : {
458 0 : osGeomFilter += "<gml:Envelope";
459 :
460 0 : CPLString osSRSName = CPLURLGetValue(pszBaseURL, "SRSNAME");
461 0 : if (!osSRSName.empty())
462 : {
463 0 : osGeomFilter += " srsName=\"";
464 0 : osGeomFilter += osSRSName;
465 0 : osGeomFilter += "\"";
466 : }
467 :
468 0 : osGeomFilter += ">";
469 0 : if (bAxisOrderAlreadyInverted)
470 : {
471 : osGeomFilter +=
472 : CPLSPrintf("<gml:lowerCorner>%.16f "
473 : "%.16f</gml:lowerCorner><gml:upperCorner>%.16f "
474 : "%.16f</gml:upperCorner>",
475 : oEnvelope.MinY, oEnvelope.MinX, oEnvelope.MaxY,
476 0 : oEnvelope.MaxX);
477 : }
478 : else
479 : osGeomFilter +=
480 : CPLSPrintf("<gml:lowerCorner>%.16f "
481 : "%.16f</gml:lowerCorner><gml:upperCorner>%.16f "
482 : "%.16f</gml:upperCorner>",
483 : oEnvelope.MinX, oEnvelope.MinY, oEnvelope.MaxX,
484 0 : oEnvelope.MaxY);
485 0 : osGeomFilter += "</gml:Envelope>";
486 : }
487 5 : else if (poDS->RequiresEnvelopeSpatialFilter())
488 : {
489 0 : osGeomFilter += "<Envelope xmlns=\"http://www.opengis.net/gml\">";
490 0 : if (bAxisOrderAlreadyInverted)
491 : {
492 : /* We can go here in WFS 1.1 with geographic coordinate systems
493 : */
494 : /* that are natively return in lat,long order, but as we have */
495 : /* presented long,lat order to the user, we must switch back */
496 : /* for the server... */
497 : osGeomFilter +=
498 : CPLSPrintf("<coord><X>%.16f</X><Y>%.16f</Y></"
499 : "coord><coord><X>%.16f</X><Y>%.16f</Y></coord>",
500 : oEnvelope.MinY, oEnvelope.MinX, oEnvelope.MaxY,
501 0 : oEnvelope.MaxX);
502 : }
503 : else
504 : osGeomFilter +=
505 : CPLSPrintf("<coord><X>%.16f</X><Y>%.16f</Y></"
506 : "coord><coord><X>%.16f</X><Y>%.16f</Y></coord>",
507 : oEnvelope.MinX, oEnvelope.MinY, oEnvelope.MaxX,
508 0 : oEnvelope.MaxY);
509 0 : osGeomFilter += "</Envelope>";
510 : }
511 : else
512 : {
513 5 : osGeomFilter += "<gml:Box>";
514 5 : osGeomFilter += "<gml:coordinates>";
515 5 : if (bAxisOrderAlreadyInverted)
516 : {
517 : /* We can go here in WFS 1.1 with geographic coordinate systems
518 : */
519 : /* that are natively return in lat,long order, but as we have */
520 : /* presented long,lat order to the user, we must switch back */
521 : /* for the server... */
522 : osGeomFilter +=
523 : CPLSPrintf("%.16f,%.16f %.16f,%.16f", oEnvelope.MinY,
524 5 : oEnvelope.MinX, oEnvelope.MaxY, oEnvelope.MaxX);
525 : }
526 : else
527 : osGeomFilter +=
528 : CPLSPrintf("%.16f,%.16f %.16f,%.16f", oEnvelope.MinX,
529 0 : oEnvelope.MinY, oEnvelope.MaxX, oEnvelope.MaxY);
530 5 : osGeomFilter += "</gml:coordinates>";
531 5 : osGeomFilter += "</gml:Box>";
532 : }
533 5 : osGeomFilter += "</BBOX>";
534 : }
535 :
536 136 : if (!osGeomFilter.empty() || !osWFSWhere.empty())
537 : {
538 33 : CPLString osFilter;
539 33 : if (atoi(poDS->GetVersion()) >= 2)
540 0 : osFilter = "<Filter xmlns=\"http://www.opengis.net/fes/2.0\"";
541 : else
542 33 : osFilter = "<Filter xmlns=\"http://www.opengis.net/ogc\"";
543 33 : if (pszNS)
544 : {
545 0 : osFilter += " xmlns:";
546 0 : osFilter += pszNS;
547 0 : osFilter += "=\"";
548 0 : osFilter += pszNSVal;
549 0 : osFilter += "\"";
550 : }
551 33 : if (atoi(poDS->GetVersion()) >= 2)
552 0 : osFilter += " xmlns:gml=\"http://www.opengis.net/gml/3.2\">";
553 : else
554 33 : osFilter += " xmlns:gml=\"http://www.opengis.net/gml\">";
555 33 : if (!osGeomFilter.empty() && !osWFSWhere.empty())
556 2 : osFilter += "<And>";
557 33 : osFilter += osWFSWhere;
558 33 : osFilter += osGeomFilter;
559 33 : if (!osGeomFilter.empty() && !osWFSWhere.empty())
560 2 : osFilter += "</And>";
561 33 : osFilter += "</Filter>";
562 :
563 33 : osURL = CPLURLAddKVP(osURL, "FILTER", WFS_EscapeURL(osFilter));
564 : }
565 :
566 136 : if (bRequestHits)
567 : {
568 9 : osURL = CPLURLAddKVP(osURL, "RESULTTYPE", "hits");
569 : }
570 127 : else if (!aoSortColumns.empty())
571 : {
572 0 : CPLString osSortBy;
573 0 : for (int i = 0; i < (int)aoSortColumns.size(); i++)
574 : {
575 0 : if (i > 0)
576 0 : osSortBy += ",";
577 0 : osSortBy += aoSortColumns[i].osColumn;
578 0 : if (!aoSortColumns[i].bAsc)
579 : {
580 0 : if (atoi(poDS->GetVersion()) >= 2)
581 0 : osSortBy += " DESC";
582 : else
583 0 : osSortBy += " D";
584 : }
585 : }
586 0 : osURL = CPLURLAddKVP(osURL, "SORTBY", WFS_EscapeURL(osSortBy));
587 : }
588 :
589 : /* If no PROPERTYNAME is specified, build one if there are ignored fields */
590 272 : CPLString osPropertyName = CPLURLGetValue(osURL, "PROPERTYNAME");
591 136 : const char *pszPropertyName = osPropertyName.c_str();
592 136 : if (pszPropertyName[0] == 0 && poFeatureDefn != nullptr)
593 : {
594 127 : bool bHasIgnoredField = false;
595 127 : osPropertyName.clear();
596 885 : for (int iField = 0; iField < poFeatureDefn->GetFieldCount(); iField++)
597 : {
598 758 : if (EQUAL(poFeatureDefn->GetFieldDefn(iField)->GetNameRef(),
599 : "gml_id"))
600 : {
601 : /* fake field : skip it */
602 : }
603 653 : else if (poFeatureDefn->GetFieldDefn(iField)->IsIgnored())
604 : {
605 24 : bHasIgnoredField = true;
606 : }
607 : else
608 : {
609 629 : if (!osPropertyName.empty())
610 528 : osPropertyName += ",";
611 : osPropertyName +=
612 629 : poFeatureDefn->GetFieldDefn(iField)->GetNameRef();
613 : }
614 : }
615 127 : if (!osGeometryColumnName.empty())
616 : {
617 98 : if (poFeatureDefn->IsGeometryIgnored())
618 : {
619 0 : bHasIgnoredField = true;
620 : }
621 : else
622 : {
623 98 : if (!osPropertyName.empty())
624 96 : osPropertyName += ",";
625 98 : osPropertyName += osGeometryColumnName;
626 : }
627 : }
628 :
629 127 : if (bHasIgnoredField && !osPropertyName.empty())
630 : {
631 8 : osURL = CPLURLAddKVP(osURL, "PROPERTYNAME",
632 12 : WFS_EscapeURL(osPropertyName));
633 : }
634 : }
635 :
636 272 : return osURL;
637 : }
638 :
639 : /************************************************************************/
640 : /* OGRWFSFetchContentDispositionFilename() */
641 : /************************************************************************/
642 :
643 49 : static const char *OGRWFSFetchContentDispositionFilename(char **papszHeaders)
644 : {
645 : const char *pszContentDisposition =
646 49 : CSLFetchNameValue(papszHeaders, "Content-Disposition");
647 49 : if (pszContentDisposition &&
648 6 : STARTS_WITH(pszContentDisposition, "attachment; filename="))
649 : {
650 6 : return pszContentDisposition + strlen("attachment; filename=");
651 : }
652 43 : return nullptr;
653 : }
654 :
655 : /************************************************************************/
656 : /* MustRetryIfNonCompliantServer() */
657 : /************************************************************************/
658 :
659 49 : bool OGRWFSLayer::MustRetryIfNonCompliantServer(const char *pszServerAnswer)
660 : {
661 49 : bool bRetry = false;
662 :
663 : /* Deegree server does not support PropertyIsNotEqualTo */
664 : /* We have to turn it into <Not><PropertyIsEqualTo> */
665 62 : if (!osWFSWhere.empty() && poDS->PropertyIsNotEqualToSupported() &&
666 13 : strstr(pszServerAnswer,
667 : "Unknown comparison operation: 'PropertyIsNotEqualTo'") !=
668 : nullptr)
669 : {
670 0 : poDS->SetPropertyIsNotEqualToUnSupported();
671 0 : bRetry = true;
672 : }
673 :
674 : /* Deegree server requires the gml: prefix in GmlObjectId element, but ESRI
675 : */
676 : /* doesn't like it at all ! Other servers don't care... */
677 62 : if (!osWFSWhere.empty() && !poDS->DoesGmlObjectIdNeedGMLPrefix() &&
678 13 : strstr(pszServerAnswer,
679 : "<GmlObjectId> requires 'gml:id'-attribute!") != nullptr)
680 : {
681 0 : poDS->SetGmlObjectIdNeedsGMLPrefix();
682 0 : bRetry = true;
683 : }
684 :
685 : /* GeoServer can return the error 'Only FeatureIds are supported when
686 : * encoding id filters to SDE' */
687 62 : if (!osWFSWhere.empty() && !bUseFeatureIdAtLayerLevel &&
688 13 : strstr(pszServerAnswer, "Only FeatureIds are supported") != nullptr)
689 : {
690 0 : bUseFeatureIdAtLayerLevel = true;
691 0 : bRetry = true;
692 : }
693 :
694 49 : if (bRetry)
695 : {
696 0 : SetAttributeFilter(osSQLWhere);
697 0 : bHasFetched = true;
698 0 : bReloadNeeded = false;
699 : }
700 :
701 49 : return bRetry;
702 : }
703 :
704 : /************************************************************************/
705 : /* FetchGetFeature() */
706 : /************************************************************************/
707 :
708 113 : GDALDataset *OGRWFSLayer::FetchGetFeature(int nRequestMaxFeatures)
709 : {
710 :
711 226 : CPLString osURL = MakeGetFeatureURL(nRequestMaxFeatures, FALSE);
712 113 : CPLDebug("WFS", "%s", osURL.c_str());
713 :
714 113 : CPLHTTPResult *psResult = nullptr;
715 :
716 226 : CPLString osOutputFormat = CPLURLGetValue(osURL, "OUTPUTFORMAT");
717 :
718 81 : const auto ReadNumberMatched = [this](const char *pszData)
719 : {
720 73 : const char *pszNumberMatched = strstr(pszData, " numberMatched=\"");
721 73 : if (!pszNumberMatched)
722 69 : pszNumberMatched = strstr(pszData, "\n"
723 : "numberMatched=\"");
724 73 : if (pszNumberMatched)
725 : {
726 16 : pszNumberMatched += strlen(" numberMatched=\"");
727 16 : if (*pszNumberMatched >= '0' && *pszNumberMatched <= '9')
728 : {
729 2 : m_nNumberMatched = CPLAtoGIntBig(pszNumberMatched);
730 2 : CPLDebug("WFS", "numberMatched = " CPL_FRMT_GIB,
731 : m_nNumberMatched);
732 2 : if (!bCountFeaturesInGetNextFeature)
733 2 : nFeatures = m_nNumberMatched;
734 : }
735 : }
736 186 : };
737 :
738 113 : if (CPLTestBool(CPLGetConfigOption("OGR_WFS_USE_STREAMING", "YES")))
739 : {
740 64 : CPLString osStreamingName;
741 125 : if (STARTS_WITH(osURL, "/vsimem/") &&
742 61 : CPLTestBool(CPLGetConfigOption("CPL_CURL_ENABLE_VSIMEM", "FALSE")))
743 : {
744 61 : osStreamingName = osURL;
745 : }
746 : else
747 : {
748 3 : osStreamingName += "/vsicurl_streaming/";
749 3 : osStreamingName += osURL;
750 : }
751 :
752 : /* Try streaming when the output format is GML and that we have a .xsd
753 : */
754 : /* that we are able to understand */
755 64 : CPLString osXSDFileName = CPLSPrintf("%s/file.xsd", m_osTmpDir.c_str());
756 : VSIStatBufL sBuf;
757 64 : GDALDriver *poDriver = nullptr;
758 64 : if ((osOutputFormat.empty() ||
759 6 : osOutputFormat.ifind("GML") != std::string::npos) &&
760 102 : VSIStatL(osXSDFileName, &sBuf) == 0 &&
761 44 : (poDriver = GDALDriver::FromHandle(GDALGetDriverByName("GML"))) !=
762 128 : nullptr &&
763 44 : poDriver->pfnOpen)
764 : {
765 44 : bStreamingDS = true;
766 44 : CPLStringList aosOptions;
767 44 : aosOptions.SetNameValue("XSD", osXSDFileName.c_str());
768 : aosOptions.SetNameValue("EMPTY_AS_NULL",
769 44 : poDS->IsEmptyAsNull() ? "YES" : "NO");
770 44 : if (CPLGetConfigOption("GML_INVERT_AXIS_ORDER_IF_LAT_LONG",
771 44 : nullptr) == nullptr)
772 : {
773 : aosOptions.SetNameValue(
774 : "INVERT_AXIS_ORDER_IF_LAT_LONG",
775 44 : poDS->InvertAxisOrderIfLatLong() ? "YES" : "NO");
776 : }
777 44 : if (CPLGetConfigOption("GML_CONSIDER_EPSG_AS_URN", nullptr) ==
778 : nullptr)
779 : {
780 : aosOptions.SetNameValue("CONSIDER_EPSG_AS_URN",
781 44 : poDS->GetConsiderEPSGAsURN().c_str());
782 : }
783 44 : if (CPLGetConfigOption("GML_EXPOSE_GML_ID", nullptr) == nullptr)
784 : {
785 : aosOptions.SetNameValue("EXPOSE_GML_ID",
786 44 : poDS->ExposeGMLId() ? "YES" : "NO");
787 : // iGMLOOIdex ++;
788 : }
789 :
790 44 : GDALOpenInfo oOpenInfo(osStreamingName.c_str(), GA_ReadOnly);
791 44 : if (oOpenInfo.nHeaderBytes && m_nNumberMatched < 0)
792 : {
793 29 : const char *pszData =
794 : reinterpret_cast<const char *>(oOpenInfo.pabyHeader);
795 29 : ReadNumberMatched(pszData);
796 : }
797 44 : oOpenInfo.papszOpenOptions = aosOptions.List();
798 :
799 44 : auto poOutputDS = poDriver->Open(&oOpenInfo, true);
800 44 : if (poOutputDS)
801 : {
802 29 : return poOutputDS;
803 : }
804 : }
805 : /* Try streaming when the output format is FlatGeobuf */
806 20 : else if ((osOutputFormat.empty() ||
807 6 : osOutputFormat.ifind("flatgeobuf") != std::string::npos) &&
808 27 : VSIStatL(osXSDFileName, &sBuf) == 0 &&
809 1 : GDALGetDriverByName("FlatGeobuf") != nullptr)
810 : {
811 1 : bStreamingDS = true;
812 1 : const char *const apszAllowedDrivers[] = {"FlatGeobuf", nullptr};
813 :
814 : GDALDataset *poFlatGeobuf_DS =
815 1 : (GDALDataset *)GDALOpenEx(osStreamingName, GDAL_OF_VECTOR,
816 : apszAllowedDrivers, nullptr, nullptr);
817 1 : if (poFlatGeobuf_DS)
818 : {
819 1 : return poFlatGeobuf_DS;
820 : }
821 : }
822 : else
823 : {
824 19 : bStreamingDS = false;
825 : }
826 :
827 34 : if (bStreamingDS)
828 : {
829 : /* In case of failure, read directly the content to examine */
830 : /* it, if it is XML error content */
831 : char szBuffer[2048];
832 15 : int nRead = 0;
833 15 : VSILFILE *fp = VSIFOpenL(osStreamingName, "rb");
834 15 : if (fp)
835 : {
836 2 : nRead = (int)VSIFReadL(szBuffer, 1, sizeof(szBuffer) - 1, fp);
837 2 : szBuffer[nRead] = '\0';
838 2 : VSIFCloseL(fp);
839 : }
840 :
841 15 : if (nRead != 0)
842 : {
843 2 : if (MustRetryIfNonCompliantServer(szBuffer))
844 1 : return FetchGetFeature(nRequestMaxFeatures);
845 :
846 2 : if (strstr(szBuffer, "<ServiceExceptionReport") != nullptr ||
847 1 : strstr(szBuffer, "<ows:ExceptionReport") != nullptr)
848 : {
849 1 : if (poDS->IsOldDeegree(szBuffer))
850 : {
851 0 : return FetchGetFeature(nRequestMaxFeatures);
852 : }
853 :
854 1 : CPLError(CE_Failure, CPLE_AppDefined,
855 : "Error returned by server : %s", szBuffer);
856 1 : return nullptr;
857 : }
858 : }
859 : }
860 : }
861 :
862 82 : bStreamingDS = false;
863 82 : psResult = poDS->HTTPFetch(osURL, nullptr);
864 82 : if (psResult == nullptr)
865 : {
866 35 : return nullptr;
867 : }
868 :
869 47 : const char *pszContentType = "";
870 47 : if (psResult->pszContentType)
871 6 : pszContentType = psResult->pszContentType;
872 :
873 47 : VSIMkdir(m_osTmpDir.c_str(), 0);
874 :
875 47 : GByte *pabyData = psResult->pabyData;
876 47 : int nDataLen = psResult->nDataLen;
877 47 : bool bIsMultiPart = false;
878 47 : const char *pszAttachmentFilename = nullptr;
879 :
880 53 : if (strstr(pszContentType, "multipart") &&
881 6 : CPLHTTPParseMultipartMime(psResult))
882 : {
883 6 : bIsMultiPart = true;
884 6 : VSIRmdirRecursive(m_osTmpDir.c_str());
885 6 : VSIMkdir(m_osTmpDir.c_str(), 0);
886 14 : for (int i = 0; i < psResult->nMimePartCount; i++)
887 : {
888 16 : CPLString osTmpFileName = m_osTmpDir + "/";
889 16 : pszAttachmentFilename = OGRWFSFetchContentDispositionFilename(
890 8 : psResult->pasMimePart[i].papszHeaders);
891 :
892 8 : if (pszAttachmentFilename)
893 6 : osTmpFileName += pszAttachmentFilename;
894 : else
895 2 : osTmpFileName += CPLSPrintf("file_%d", i);
896 :
897 : GByte *pData =
898 8 : (GByte *)VSI_MALLOC_VERBOSE(psResult->pasMimePart[i].nDataLen);
899 8 : if (pData)
900 : {
901 8 : memcpy(pData, psResult->pasMimePart[i].pabyData,
902 8 : psResult->pasMimePart[i].nDataLen);
903 8 : VSILFILE *fp = VSIFileFromMemBuffer(
904 8 : osTmpFileName, pData, psResult->pasMimePart[i].nDataLen,
905 : TRUE);
906 8 : VSIFCloseL(fp);
907 : }
908 : }
909 : }
910 : else
911 : pszAttachmentFilename =
912 41 : OGRWFSFetchContentDispositionFilename(psResult->papszHeaders);
913 :
914 47 : bool bJSON = false;
915 47 : bool bCSV = false;
916 47 : bool bKML = false;
917 47 : bool bKMZ = false;
918 47 : bool bFlatGeobuf = false;
919 47 : bool bZIP = false;
920 47 : bool bGZIP = false;
921 :
922 47 : const char *pszOutputFormat = osOutputFormat.c_str();
923 :
924 94 : if (FindSubStringInsensitive(pszContentType, "json") ||
925 47 : FindSubStringInsensitive(pszOutputFormat, "json"))
926 : {
927 4 : bJSON = true;
928 : }
929 86 : else if (FindSubStringInsensitive(pszContentType, "csv") ||
930 43 : FindSubStringInsensitive(pszOutputFormat, "csv"))
931 : {
932 0 : bCSV = true;
933 : }
934 86 : else if (FindSubStringInsensitive(pszContentType, "kml") ||
935 43 : FindSubStringInsensitive(pszOutputFormat, "kml"))
936 : {
937 0 : bKML = true;
938 : }
939 86 : else if (FindSubStringInsensitive(pszContentType, "kmz") ||
940 43 : FindSubStringInsensitive(pszOutputFormat, "kmz"))
941 : {
942 0 : bKMZ = true;
943 : }
944 86 : else if (FindSubStringInsensitive(pszContentType, "flatgeobuf") ||
945 43 : FindSubStringInsensitive(pszOutputFormat, "flatgeobuf"))
946 : {
947 0 : bFlatGeobuf = true;
948 : }
949 43 : else if (strstr(pszContentType, "application/zip") != nullptr)
950 : {
951 0 : bZIP = true;
952 : }
953 43 : else if (strstr(pszContentType, "application/gzip") != nullptr)
954 : {
955 0 : bGZIP = true;
956 : }
957 :
958 47 : const char *pszData = reinterpret_cast<const char *>(pabyData);
959 47 : if (MustRetryIfNonCompliantServer(pszData))
960 : {
961 0 : CPLHTTPDestroyResult(psResult);
962 0 : return FetchGetFeature(nRequestMaxFeatures);
963 : }
964 :
965 47 : if (strstr(pszData, "<ServiceExceptionReport") != nullptr ||
966 46 : strstr(pszData, "<ows:ExceptionReport") != nullptr)
967 : {
968 1 : if (poDS->IsOldDeegree(pszData))
969 : {
970 0 : CPLHTTPDestroyResult(psResult);
971 0 : return FetchGetFeature(nRequestMaxFeatures);
972 : }
973 :
974 1 : CPLError(CE_Failure, CPLE_AppDefined, "Error returned by server : %s",
975 : pabyData);
976 1 : CPLHTTPDestroyResult(psResult);
977 1 : return nullptr;
978 : }
979 :
980 46 : if (m_nNumberMatched < 0)
981 44 : ReadNumberMatched(pszData);
982 :
983 92 : CPLString osTmpFileName;
984 :
985 46 : if (!bIsMultiPart)
986 : {
987 40 : if (bJSON)
988 4 : osTmpFileName = m_osTmpDir + "/file.geojson";
989 36 : else if (bZIP)
990 0 : osTmpFileName = m_osTmpDir + "/file.zip";
991 36 : else if (bCSV)
992 0 : osTmpFileName = m_osTmpDir + "/file.csv";
993 36 : else if (bKML)
994 0 : osTmpFileName = m_osTmpDir + "/file.kml";
995 36 : else if (bKMZ)
996 0 : osTmpFileName = m_osTmpDir + "/file.kmz";
997 36 : else if (bFlatGeobuf)
998 0 : osTmpFileName = m_osTmpDir + "/file.fgb";
999 : /* GML is a special case. It needs the .xsd file that has been saved */
1000 : /* as file.xsd, so we cannot used the attachment filename */
1001 36 : else if (pszAttachmentFilename &&
1002 0 : !EQUAL(CPLGetExtension(pszAttachmentFilename), "GML"))
1003 : {
1004 0 : osTmpFileName = m_osTmpDir + "/";
1005 0 : osTmpFileName += pszAttachmentFilename;
1006 : }
1007 : else
1008 : {
1009 36 : osTmpFileName = m_osTmpDir + "/file.gfs";
1010 36 : VSIUnlink(osTmpFileName);
1011 :
1012 36 : osTmpFileName = m_osTmpDir + "/file.gml";
1013 : }
1014 :
1015 : VSILFILE *fp =
1016 40 : VSIFileFromMemBuffer(osTmpFileName, pabyData, nDataLen, TRUE);
1017 40 : VSIFCloseL(fp);
1018 40 : psResult->pabyData = nullptr;
1019 :
1020 40 : if (bZIP)
1021 : {
1022 0 : osTmpFileName = "/vsizip/" + osTmpFileName;
1023 : }
1024 40 : else if (bGZIP)
1025 : {
1026 0 : osTmpFileName = "/vsigzip/" + osTmpFileName;
1027 : }
1028 : }
1029 : else
1030 : {
1031 6 : pabyData = nullptr;
1032 6 : nDataLen = 0;
1033 6 : osTmpFileName = m_osTmpDir;
1034 : }
1035 :
1036 46 : CPLHTTPDestroyResult(psResult);
1037 :
1038 46 : const char *const *papszOpenOptions = nullptr;
1039 46 : const char *apszGMLOpenOptions[4] = {nullptr, nullptr, nullptr, nullptr};
1040 46 : int iGMLOOIdex = 0;
1041 46 : if (CPLGetConfigOption("GML_INVERT_AXIS_ORDER_IF_LAT_LONG", nullptr) ==
1042 : nullptr)
1043 : {
1044 46 : apszGMLOpenOptions[iGMLOOIdex] =
1045 46 : CPLSPrintf("INVERT_AXIS_ORDER_IF_LAT_LONG=%s",
1046 46 : poDS->InvertAxisOrderIfLatLong() ? "YES" : "NO");
1047 46 : iGMLOOIdex++;
1048 : }
1049 46 : if (CPLGetConfigOption("GML_CONSIDER_EPSG_AS_URN", nullptr) == nullptr)
1050 : {
1051 46 : apszGMLOpenOptions[iGMLOOIdex] = CPLSPrintf(
1052 46 : "CONSIDER_EPSG_AS_URN=%s", poDS->GetConsiderEPSGAsURN().c_str());
1053 46 : iGMLOOIdex++;
1054 : }
1055 46 : if (CPLGetConfigOption("GML_EXPOSE_GML_ID", nullptr) == nullptr)
1056 : {
1057 46 : apszGMLOpenOptions[iGMLOOIdex] =
1058 46 : CPLSPrintf("EXPOSE_GML_ID=%s", poDS->ExposeGMLId() ? "YES" : "NO");
1059 : // iGMLOOIdex ++;
1060 : }
1061 :
1062 46 : GDALDriverH hDrv = GDALIdentifyDriver(osTmpFileName, nullptr);
1063 46 : if (hDrv != nullptr && hDrv == GDALGetDriverByName("GML"))
1064 34 : papszOpenOptions = apszGMLOpenOptions;
1065 :
1066 46 : GDALDataset *poPageDS = (GDALDataset *)GDALOpenEx(
1067 : osTmpFileName, GDAL_OF_VECTOR, nullptr, papszOpenOptions, nullptr);
1068 46 : if (poPageDS == nullptr && (bZIP || bIsMultiPart))
1069 : {
1070 4 : char **papszFileList = VSIReadDir(osTmpFileName);
1071 4 : for (int i = 0; papszFileList != nullptr && papszFileList[i] != nullptr;
1072 : i++)
1073 : {
1074 : CPLString osFullFilename =
1075 4 : CPLFormFilename(osTmpFileName, papszFileList[i], nullptr);
1076 4 : hDrv = GDALIdentifyDriver(osFullFilename, nullptr);
1077 4 : if (hDrv != nullptr && hDrv == GDALGetDriverByName("GML"))
1078 0 : papszOpenOptions = apszGMLOpenOptions;
1079 : poPageDS =
1080 4 : (GDALDataset *)GDALOpenEx(osFullFilename, GDAL_OF_VECTOR,
1081 : nullptr, papszOpenOptions, nullptr);
1082 4 : if (poPageDS != nullptr)
1083 4 : break;
1084 : }
1085 :
1086 4 : CSLDestroy(papszFileList);
1087 : }
1088 :
1089 46 : if (poPageDS == nullptr)
1090 : {
1091 2 : if (pabyData != nullptr && !bJSON && !bZIP &&
1092 2 : strstr((const char *)pabyData, "<wfs:FeatureCollection") ==
1093 2 : nullptr &&
1094 2 : strstr((const char *)pabyData, "<gml:FeatureCollection") == nullptr)
1095 : {
1096 2 : if (nDataLen > 1000)
1097 0 : pabyData[1000] = 0;
1098 2 : CPLError(CE_Failure, CPLE_AppDefined, "Error: cannot parse %s",
1099 : pabyData);
1100 : }
1101 2 : return nullptr;
1102 : }
1103 :
1104 44 : OGRLayer *poLayer = poPageDS->GetLayer(0);
1105 44 : if (poLayer == nullptr)
1106 : {
1107 0 : GDALClose(poPageDS);
1108 0 : return nullptr;
1109 : }
1110 :
1111 44 : return poPageDS;
1112 : }
1113 :
1114 : /************************************************************************/
1115 : /* GetLayerDefn() */
1116 : /************************************************************************/
1117 :
1118 2022 : OGRFeatureDefn *OGRWFSLayer::GetLayerDefn()
1119 : {
1120 2022 : if (poFeatureDefn)
1121 1899 : return poFeatureDefn;
1122 :
1123 123 : if (poDS->GetLayerCount() > 1)
1124 : {
1125 47 : poDS->LoadMultipleLayerDefn(GetName(), pszNS, pszNSVal);
1126 :
1127 47 : if (poFeatureDefn)
1128 28 : return poFeatureDefn;
1129 : }
1130 :
1131 95 : return BuildLayerDefn();
1132 : }
1133 :
1134 : /************************************************************************/
1135 : /* BuildLayerDefn() */
1136 : /************************************************************************/
1137 :
1138 157 : OGRFeatureDefn *OGRWFSLayer::BuildLayerDefn(OGRFeatureDefn *poSrcFDefn)
1139 : {
1140 157 : bool bUnsetWidthPrecision = false;
1141 :
1142 157 : poFeatureDefn = new OGRFeatureDefn(pszName);
1143 157 : poFeatureDefn->GetGeomFieldDefn(0)->SetSpatialRef(m_poSRS);
1144 157 : poFeatureDefn->Reference();
1145 :
1146 157 : GDALDataset *l_poDS = nullptr;
1147 :
1148 157 : if (poSrcFDefn == nullptr)
1149 95 : poSrcFDefn = DescribeFeatureType();
1150 157 : if (poSrcFDefn == nullptr)
1151 : {
1152 18 : l_poDS = FetchGetFeature(1);
1153 18 : if (l_poDS == nullptr)
1154 : {
1155 15 : return poFeatureDefn;
1156 : }
1157 3 : OGRLayer *l_poLayer = l_poDS->GetLayer(0);
1158 3 : if (l_poLayer == nullptr)
1159 : {
1160 0 : return poFeatureDefn;
1161 : }
1162 3 : poSrcFDefn = l_poLayer->GetLayerDefn();
1163 3 : bGotApproximateLayerDefn = true;
1164 : /* We cannot trust width and precision based on a single feature */
1165 3 : bUnsetWidthPrecision = true;
1166 : }
1167 :
1168 : const CPLStringList aosPropertyName(CSLTokenizeString2(
1169 142 : CPLURLGetValue(pszBaseURL, "PROPERTYNAME"), "(,)", 0));
1170 :
1171 142 : poFeatureDefn->SetGeomType(poSrcFDefn->GetGeomType());
1172 142 : if (poSrcFDefn->GetGeomFieldCount() > 0)
1173 254 : poFeatureDefn->GetGeomFieldDefn(0)->SetName(
1174 127 : poSrcFDefn->GetGeomFieldDefn(0)->GetNameRef());
1175 843 : for (int i = 0; i < poSrcFDefn->GetFieldCount(); i++)
1176 : {
1177 701 : if (!aosPropertyName.empty())
1178 : {
1179 0 : if (aosPropertyName.FindString(
1180 0 : poSrcFDefn->GetFieldDefn(i)->GetNameRef()) >= 0)
1181 0 : poFeatureDefn->AddFieldDefn(poSrcFDefn->GetFieldDefn(i));
1182 : else
1183 0 : bGotApproximateLayerDefn = true;
1184 : }
1185 : else
1186 : {
1187 1402 : OGRFieldDefn oFieldDefn(poSrcFDefn->GetFieldDefn(i));
1188 701 : if (bUnsetWidthPrecision)
1189 : {
1190 18 : oFieldDefn.SetWidth(0);
1191 18 : oFieldDefn.SetPrecision(0);
1192 : }
1193 701 : poFeatureDefn->AddFieldDefn(&oFieldDefn);
1194 : }
1195 : }
1196 :
1197 142 : if (l_poDS)
1198 3 : GDALClose(l_poDS);
1199 : else
1200 139 : delete poSrcFDefn;
1201 :
1202 142 : return poFeatureDefn;
1203 : }
1204 :
1205 : /************************************************************************/
1206 : /* ResetReading() */
1207 : /************************************************************************/
1208 :
1209 192 : void OGRWFSLayer::ResetReading()
1210 :
1211 : {
1212 192 : if (poFeatureDefn == nullptr)
1213 17 : return;
1214 175 : if (bPagingActive)
1215 0 : bReloadNeeded = true;
1216 175 : nPagingStartIndex = 0;
1217 175 : nFeatureRead = 0;
1218 175 : m_nNumberMatched = -1;
1219 175 : m_bHasReadAtLeastOneFeatureInThisPage = false;
1220 175 : if (bReloadNeeded)
1221 : {
1222 39 : GDALClose(poBaseDS);
1223 39 : poBaseDS = nullptr;
1224 39 : poBaseLayer = nullptr;
1225 39 : bHasFetched = false;
1226 39 : bReloadNeeded = false;
1227 : }
1228 175 : if (poBaseLayer)
1229 47 : poBaseLayer->ResetReading();
1230 : }
1231 :
1232 : /************************************************************************/
1233 : /* SetIgnoredFields() */
1234 : /************************************************************************/
1235 :
1236 16 : OGRErr OGRWFSLayer::SetIgnoredFields(CSLConstList papszFields)
1237 : {
1238 16 : bReloadNeeded = true;
1239 16 : ResetReading();
1240 16 : return OGRLayer::SetIgnoredFields(papszFields);
1241 : }
1242 :
1243 : /************************************************************************/
1244 : /* GetNextFeature() */
1245 : /************************************************************************/
1246 :
1247 123 : OGRFeature *OGRWFSLayer::GetNextFeature()
1248 : {
1249 123 : GetLayerDefn();
1250 :
1251 : while (true)
1252 : {
1253 135 : if (bReloadNeeded)
1254 : {
1255 41 : m_bHasReadAtLeastOneFeatureInThisPage = false;
1256 41 : GDALClose(poBaseDS);
1257 41 : poBaseDS = nullptr;
1258 41 : poBaseLayer = nullptr;
1259 41 : bHasFetched = false;
1260 41 : bReloadNeeded = false;
1261 : }
1262 135 : if (poBaseDS == nullptr && !bHasFetched)
1263 : {
1264 95 : bHasFetched = true;
1265 95 : poBaseDS = FetchGetFeature(0);
1266 95 : poBaseLayer = nullptr;
1267 95 : if (poBaseDS)
1268 : {
1269 71 : poBaseLayer = poBaseDS->GetLayer(0);
1270 71 : if (poBaseLayer == nullptr)
1271 0 : return nullptr;
1272 71 : poBaseLayer->ResetReading();
1273 :
1274 : /* Check that the layer field definition is consistent with the
1275 : * one */
1276 : /* we got in BuildLayerDefn() */
1277 71 : if (poFeatureDefn->GetFieldCount() !=
1278 71 : poBaseLayer->GetLayerDefn()->GetFieldCount())
1279 5 : bGotApproximateLayerDefn = true;
1280 : else
1281 : {
1282 502 : for (int iField = 0;
1283 502 : iField < poFeatureDefn->GetFieldCount(); iField++)
1284 : {
1285 : OGRFieldDefn *poFDefn1 =
1286 442 : poFeatureDefn->GetFieldDefn(iField);
1287 : OGRFieldDefn *poFDefn2 =
1288 442 : poBaseLayer->GetLayerDefn()->GetFieldDefn(iField);
1289 442 : if (strcmp(poFDefn1->GetNameRef(),
1290 878 : poFDefn2->GetNameRef()) != 0 ||
1291 436 : poFDefn1->GetType() != poFDefn2->GetType())
1292 : {
1293 6 : bGotApproximateLayerDefn = true;
1294 6 : break;
1295 : }
1296 : }
1297 : }
1298 : }
1299 : }
1300 135 : if (poBaseDS == nullptr || poBaseLayer == nullptr)
1301 37 : return nullptr;
1302 :
1303 98 : OGRFeature *poSrcFeature = poBaseLayer->GetNextFeature();
1304 98 : if (poSrcFeature == nullptr)
1305 : {
1306 22 : if (bPagingActive && m_bHasReadAtLeastOneFeatureInThisPage &&
1307 14 : (m_nNumberMatched < 0 || nFeatureRead < m_nNumberMatched))
1308 : {
1309 12 : bReloadNeeded = true;
1310 12 : nPagingStartIndex = nFeatureRead;
1311 12 : continue;
1312 : }
1313 10 : return nullptr;
1314 : }
1315 76 : nFeatureRead++;
1316 76 : m_bHasReadAtLeastOneFeatureInThisPage = true;
1317 76 : if (bCountFeaturesInGetNextFeature)
1318 2 : nFeatures++;
1319 :
1320 76 : OGRGeometry *poGeom = poSrcFeature->GetGeometryRef();
1321 82 : if (m_poFilterGeom != nullptr && poGeom != nullptr &&
1322 6 : !FilterGeometry(poGeom))
1323 : {
1324 0 : delete poSrcFeature;
1325 0 : continue;
1326 : }
1327 :
1328 : /* Client-side attribute filtering with underlying layer defn */
1329 : /* identical to exposed layer defn. */
1330 64 : if (!bGotApproximateLayerDefn && osWFSWhere.empty() &&
1331 140 : m_poAttrQuery != nullptr && !m_poAttrQuery->Evaluate(poSrcFeature))
1332 : {
1333 0 : delete poSrcFeature;
1334 0 : continue;
1335 : }
1336 :
1337 76 : OGRFeature *poNewFeature = new OGRFeature(poFeatureDefn);
1338 76 : if (bGotApproximateLayerDefn)
1339 : {
1340 12 : poNewFeature->SetFrom(poSrcFeature);
1341 :
1342 : /* Client-side attribute filtering. */
1343 12 : if (m_poAttrQuery != nullptr && osWFSWhere.empty() &&
1344 0 : !m_poAttrQuery->Evaluate(poNewFeature))
1345 : {
1346 0 : delete poSrcFeature;
1347 0 : delete poNewFeature;
1348 0 : continue;
1349 : }
1350 : }
1351 : else
1352 : {
1353 524 : for (int iField = 0; iField < poFeatureDefn->GetFieldCount();
1354 : iField++)
1355 : {
1356 460 : poNewFeature->SetField(iField,
1357 460 : poSrcFeature->GetRawFieldRef(iField));
1358 : }
1359 64 : poNewFeature->SetStyleString(poSrcFeature->GetStyleString());
1360 64 : poNewFeature->SetGeometryDirectly(poSrcFeature->StealGeometry());
1361 : }
1362 76 : poNewFeature->SetFID(poSrcFeature->GetFID());
1363 76 : poGeom = poNewFeature->GetGeometryRef();
1364 :
1365 : /* FIXME? I don't really know what we should do with WFS 1.1.0 */
1366 : /* and non-GML format !!! I guess 50% WFS servers must do it wrong
1367 : * anyway */
1368 : /* GeoServer does currently axis inversion for non GML output, but */
1369 : /* apparently this is not correct :
1370 : * http://jira.codehaus.org/browse/GEOS-3657 */
1371 116 : if (poGeom != nullptr && bAxisOrderAlreadyInverted &&
1372 40 : strcmp(poBaseDS->GetDriverName(), "GML") != 0)
1373 : {
1374 9 : poGeom->swapXY();
1375 : }
1376 :
1377 76 : if (poGeom && m_poSRS)
1378 42 : poGeom->assignSpatialReference(m_poSRS);
1379 76 : delete poSrcFeature;
1380 76 : return poNewFeature;
1381 12 : }
1382 : }
1383 :
1384 : /************************************************************************/
1385 : /* SetSpatialFilter() */
1386 : /************************************************************************/
1387 :
1388 29 : void OGRWFSLayer::SetSpatialFilter(OGRGeometry *poGeom)
1389 : {
1390 29 : if (bStreamingDS)
1391 : {
1392 7 : bReloadNeeded = true;
1393 : }
1394 22 : else if (poFetchedFilterGeom == nullptr && poBaseDS != nullptr)
1395 : {
1396 : /* If there was no filter set, and that we set one */
1397 : /* the new result set can only be a subset of the whole */
1398 : /* so no need to reload from source */
1399 5 : bReloadNeeded = false;
1400 : }
1401 17 : else if (poFetchedFilterGeom != nullptr && poGeom != nullptr &&
1402 2 : poBaseDS != nullptr)
1403 : {
1404 1 : OGREnvelope oOldEnvelope, oNewEnvelope;
1405 1 : poFetchedFilterGeom->getEnvelope(&oOldEnvelope);
1406 1 : poGeom->getEnvelope(&oNewEnvelope);
1407 : /* Optimization : we don't need to request the server */
1408 : /* if the new BBOX is inside the old BBOX as we have */
1409 : /* already all the features */
1410 1 : bReloadNeeded = !oOldEnvelope.Contains(oNewEnvelope);
1411 : }
1412 : else
1413 : {
1414 16 : bReloadNeeded = true;
1415 : }
1416 29 : nFeatures = -1;
1417 29 : OGRLayer::SetSpatialFilter(poGeom);
1418 29 : ResetReading();
1419 29 : }
1420 :
1421 : /************************************************************************/
1422 : /* SetAttributeFilter() */
1423 : /************************************************************************/
1424 :
1425 90 : OGRErr OGRWFSLayer::SetAttributeFilter(const char *pszFilter)
1426 : {
1427 90 : if (pszFilter != nullptr && pszFilter[0] == 0)
1428 8 : pszFilter = nullptr;
1429 :
1430 180 : CPLString osOldWFSWhere(osWFSWhere);
1431 :
1432 90 : CPLFree(m_pszAttrQueryString);
1433 90 : m_pszAttrQueryString = (pszFilter) ? CPLStrdup(pszFilter) : nullptr;
1434 :
1435 90 : delete m_poAttrQuery;
1436 90 : m_poAttrQuery = nullptr;
1437 :
1438 90 : if (pszFilter != nullptr)
1439 : {
1440 58 : m_poAttrQuery = new OGRFeatureQuery();
1441 :
1442 58 : OGRErr eErr = m_poAttrQuery->Compile(GetLayerDefn(), pszFilter, TRUE,
1443 : WFSGetCustomFuncRegistrar());
1444 58 : if (eErr != OGRERR_NONE)
1445 : {
1446 28 : delete m_poAttrQuery;
1447 28 : m_poAttrQuery = nullptr;
1448 28 : return eErr;
1449 : }
1450 : }
1451 :
1452 62 : if (poDS->HasMinOperators() && m_poAttrQuery != nullptr)
1453 : {
1454 30 : swq_expr_node *poNode = (swq_expr_node *)m_poAttrQuery->GetSWQExpr();
1455 30 : poNode->ReplaceBetweenByGEAndLERecurse();
1456 :
1457 30 : int bNeedsNullCheck = FALSE;
1458 60 : int nVersion = (strcmp(poDS->GetVersion(), "1.0.0") == 0) ? 100
1459 30 : : (atoi(poDS->GetVersion()) >= 2) ? 200
1460 30 : : 110;
1461 30 : if (poNode->field_type != SWQ_BOOLEAN)
1462 0 : osWFSWhere = "";
1463 : else
1464 60 : osWFSWhere = WFS_TurnSQLFilterToOGCFilter(
1465 : poNode, nullptr, GetLayerDefn(), nVersion,
1466 30 : poDS->PropertyIsNotEqualToSupported(),
1467 30 : poDS->UseFeatureId() || bUseFeatureIdAtLayerLevel,
1468 60 : poDS->DoesGmlObjectIdNeedGMLPrefix(), "", &bNeedsNullCheck);
1469 30 : if (bNeedsNullCheck && !poDS->HasNullCheck())
1470 0 : osWFSWhere = "";
1471 : }
1472 : else
1473 32 : osWFSWhere = "";
1474 :
1475 62 : if (m_poAttrQuery != nullptr && osWFSWhere.empty())
1476 : {
1477 0 : CPLDebug("WFS", "Using client-side only mode for filter \"%s\"",
1478 : pszFilter);
1479 0 : OGRErr eErr = OGRLayer::SetAttributeFilter(pszFilter);
1480 0 : if (eErr != OGRERR_NONE)
1481 0 : return eErr;
1482 : }
1483 62 : ResetReading();
1484 :
1485 62 : osSQLWhere = (pszFilter) ? pszFilter : "";
1486 :
1487 62 : if (osWFSWhere != osOldWFSWhere)
1488 46 : bReloadNeeded = true;
1489 : else
1490 16 : bReloadNeeded = false;
1491 62 : nFeatures = -1;
1492 :
1493 62 : return OGRERR_NONE;
1494 : }
1495 :
1496 : /************************************************************************/
1497 : /* TestCapability() */
1498 : /************************************************************************/
1499 :
1500 160 : int OGRWFSLayer::TestCapability(const char *pszCap)
1501 :
1502 : {
1503 160 : if (EQUAL(pszCap, OLCFastFeatureCount))
1504 : {
1505 17 : if (nFeatures >= 0)
1506 0 : return TRUE;
1507 :
1508 0 : return poBaseLayer != nullptr && m_poFilterGeom == nullptr &&
1509 0 : m_poAttrQuery == nullptr &&
1510 17 : poBaseLayer->TestCapability(pszCap) &&
1511 0 : (!poDS->IsPagingAllowed() &&
1512 17 : poBaseLayer->GetFeatureCount() < poDS->GetPageSize());
1513 : }
1514 :
1515 143 : else if (EQUAL(pszCap, OLCFastGetExtent))
1516 : {
1517 7 : if (m_oExtents.IsInit())
1518 0 : return TRUE;
1519 :
1520 7 : return poBaseLayer != nullptr && poBaseLayer->TestCapability(pszCap);
1521 : }
1522 :
1523 136 : else if (EQUAL(pszCap, OLCStringsAsUTF8))
1524 0 : return poBaseLayer != nullptr && poBaseLayer->TestCapability(pszCap);
1525 :
1526 136 : else if (EQUAL(pszCap, OLCSequentialWrite) ||
1527 98 : EQUAL(pszCap, OLCDeleteFeature) || EQUAL(pszCap, OLCRandomWrite))
1528 : {
1529 78 : GetLayerDefn();
1530 156 : return poDS->SupportTransactions() && poDS->UpdateMode() &&
1531 156 : poFeatureDefn->GetFieldIndex("gml_id") == 0;
1532 : }
1533 58 : else if (EQUAL(pszCap, OLCTransactions))
1534 : {
1535 50 : return poDS->SupportTransactions() && poDS->UpdateMode();
1536 : }
1537 8 : else if (EQUAL(pszCap, OLCIgnoreFields))
1538 : {
1539 0 : return TRUE;
1540 : }
1541 :
1542 8 : return FALSE;
1543 : }
1544 :
1545 : /************************************************************************/
1546 : /* ExecuteGetFeatureResultTypeHits() */
1547 : /************************************************************************/
1548 :
1549 9 : GIntBig OGRWFSLayer::ExecuteGetFeatureResultTypeHits()
1550 : {
1551 9 : char *pabyData = nullptr;
1552 18 : CPLString osURL = MakeGetFeatureURL(0, TRUE);
1553 9 : if (pszRequiredOutputFormat)
1554 0 : osURL = CPLURLAddKVP(osURL, "OUTPUTFORMAT",
1555 0 : WFS_EscapeURL(pszRequiredOutputFormat));
1556 9 : CPLDebug("WFS", "%s", osURL.c_str());
1557 :
1558 9 : CPLHTTPResult *psResult = poDS->HTTPFetch(osURL, nullptr);
1559 9 : if (psResult == nullptr)
1560 : {
1561 1 : return -1;
1562 : }
1563 :
1564 : /* http://demo.snowflakesoftware.com:8080/Obstacle_AIXM_ZIP/GOPublisherWFS
1565 : * returns */
1566 : /* zip content, including for RESULTTYPE=hits */
1567 8 : if (psResult->pszContentType != nullptr &&
1568 0 : strstr(psResult->pszContentType, "application/zip") != nullptr)
1569 : {
1570 : const CPLString osTmpFileName(
1571 0 : VSIMemGenerateHiddenFilename("wfstemphits.zip"));
1572 0 : VSILFILE *fp = VSIFileFromMemBuffer(osTmpFileName, psResult->pabyData,
1573 0 : psResult->nDataLen, FALSE);
1574 0 : VSIFCloseL(fp);
1575 :
1576 0 : CPLString osZipTmpFileName("/vsizip/" + osTmpFileName);
1577 :
1578 0 : char **papszDirContent = VSIReadDir(osZipTmpFileName);
1579 0 : if (CSLCount(papszDirContent) != 1)
1580 : {
1581 0 : CPLError(CE_Failure, CPLE_AppDefined,
1582 : "Cannot parse result of RESULTTYPE=hits request : more "
1583 : "than one file in zip");
1584 0 : CSLDestroy(papszDirContent);
1585 0 : CPLHTTPDestroyResult(psResult);
1586 0 : VSIUnlink(osTmpFileName);
1587 0 : return -1;
1588 : }
1589 :
1590 0 : CPLString osFileInZipTmpFileName = osZipTmpFileName + "/";
1591 0 : osFileInZipTmpFileName += papszDirContent[0];
1592 :
1593 0 : fp = VSIFOpenL(osFileInZipTmpFileName.c_str(), "rb");
1594 : VSIStatBufL sBuf;
1595 0 : if (fp == nullptr ||
1596 0 : VSIStatL(osFileInZipTmpFileName.c_str(), &sBuf) != 0)
1597 : {
1598 0 : CPLError(CE_Failure, CPLE_AppDefined,
1599 : "Cannot parse result of RESULTTYPE=hits request : cannot "
1600 : "open one file in zip");
1601 0 : CSLDestroy(papszDirContent);
1602 0 : CPLHTTPDestroyResult(psResult);
1603 0 : VSIUnlink(osTmpFileName);
1604 0 : if (fp)
1605 0 : VSIFCloseL(fp);
1606 0 : return -1;
1607 : }
1608 0 : pabyData = (char *)CPLMalloc((size_t)(sBuf.st_size + 1));
1609 0 : pabyData[sBuf.st_size] = 0;
1610 0 : VSIFReadL(pabyData, 1, (size_t)sBuf.st_size, fp);
1611 0 : VSIFCloseL(fp);
1612 :
1613 0 : CSLDestroy(papszDirContent);
1614 0 : VSIUnlink(osTmpFileName);
1615 : }
1616 : else
1617 : {
1618 8 : pabyData = (char *)psResult->pabyData;
1619 8 : psResult->pabyData = nullptr;
1620 : }
1621 :
1622 8 : if (strstr(pabyData, "<ServiceExceptionReport") != nullptr ||
1623 7 : strstr(pabyData, "<ows:ExceptionReport") != nullptr)
1624 : {
1625 1 : if (poDS->IsOldDeegree(pabyData))
1626 : {
1627 0 : CPLHTTPDestroyResult(psResult);
1628 0 : return ExecuteGetFeatureResultTypeHits();
1629 : }
1630 1 : CPLError(CE_Failure, CPLE_AppDefined, "Error returned by server : %s",
1631 : pabyData);
1632 1 : CPLHTTPDestroyResult(psResult);
1633 1 : CPLFree(pabyData);
1634 1 : return -1;
1635 : }
1636 :
1637 7 : CPLXMLNode *psXML = CPLParseXMLString(pabyData);
1638 7 : if (psXML == nullptr)
1639 : {
1640 2 : CPLError(CE_Failure, CPLE_AppDefined, "Invalid XML content : %s",
1641 : pabyData);
1642 2 : CPLHTTPDestroyResult(psResult);
1643 2 : CPLFree(pabyData);
1644 2 : return -1;
1645 : }
1646 :
1647 5 : CPLStripXMLNamespace(psXML, nullptr, TRUE);
1648 5 : CPLXMLNode *psRoot = CPLGetXMLNode(psXML, "=FeatureCollection");
1649 5 : if (psRoot == nullptr)
1650 : {
1651 1 : CPLError(CE_Failure, CPLE_AppDefined,
1652 : "Cannot find <FeatureCollection>");
1653 1 : CPLDestroyXMLNode(psXML);
1654 1 : CPLHTTPDestroyResult(psResult);
1655 1 : CPLFree(pabyData);
1656 1 : return -1;
1657 : }
1658 :
1659 4 : const char *pszValue = CPLGetXMLValue(psRoot, "numberOfFeatures", nullptr);
1660 4 : if (pszValue == nullptr)
1661 : pszValue =
1662 3 : CPLGetXMLValue(psRoot, "numberMatched", nullptr); /* WFS 2.0.0 */
1663 4 : if (pszValue == nullptr)
1664 : {
1665 1 : CPLError(CE_Failure, CPLE_AppDefined, "Cannot find numberOfFeatures");
1666 1 : CPLDestroyXMLNode(psXML);
1667 1 : CPLHTTPDestroyResult(psResult);
1668 1 : CPLFree(pabyData);
1669 :
1670 1 : poDS->DisableSupportHits();
1671 1 : return -1;
1672 : }
1673 :
1674 3 : GIntBig l_nFeatures = CPLAtoGIntBig(pszValue);
1675 : /* Hum,
1676 : * http://deegree3-testing.deegree.org:80/deegree-inspire-node/services?MAXFEATURES=10&SERVICE=WFS&VERSION=1.1.0&REQUEST=GetFeature&TYPENAME=ad:Address&OUTPUTFORMAT=text/xml;%20subtype=gml/3.2.1&RESULTTYPE=hits
1677 : */
1678 : /* returns more than MAXFEATURES features... So truncate to MAXFEATURES */
1679 : CPLString osMaxFeatures = CPLURLGetValue(
1680 3 : osURL, atoi(poDS->GetVersion()) >= 2 ? "COUNT" : "MAXFEATURES");
1681 3 : if (!osMaxFeatures.empty())
1682 : {
1683 0 : GIntBig nMaxFeatures = CPLAtoGIntBig(osMaxFeatures);
1684 0 : if (l_nFeatures > nMaxFeatures)
1685 : {
1686 0 : CPLDebug("WFS",
1687 : "Truncating result from " CPL_FRMT_GIB " to " CPL_FRMT_GIB,
1688 : l_nFeatures, nMaxFeatures);
1689 0 : l_nFeatures = nMaxFeatures;
1690 : }
1691 : }
1692 :
1693 3 : CPLDestroyXMLNode(psXML);
1694 3 : CPLHTTPDestroyResult(psResult);
1695 3 : CPLFree(pabyData);
1696 :
1697 3 : return l_nFeatures;
1698 : }
1699 :
1700 : /************************************************************************/
1701 : /* CanRunGetFeatureCountAndGetExtentTogether() */
1702 : /************************************************************************/
1703 :
1704 14 : int OGRWFSLayer::CanRunGetFeatureCountAndGetExtentTogether()
1705 : {
1706 : /* In some cases, we can evaluate the result of GetFeatureCount() */
1707 : /* and GetExtent() with the same data */
1708 14 : CPLString osRequestURL = MakeGetFeatureURL(0, FALSE);
1709 22 : return (!m_oExtents.IsInit() && nFeatures < 0 &&
1710 8 : osRequestURL.ifind("FILTER") == std::string::npos &&
1711 8 : osRequestURL.ifind("MAXFEATURES") == std::string::npos &&
1712 30 : osRequestURL.ifind("COUNT") == std::string::npos &&
1713 36 : !(GetLayerDefn()->IsGeometryIgnored()));
1714 : }
1715 :
1716 : /************************************************************************/
1717 : /* GetFeatureCount() */
1718 : /************************************************************************/
1719 :
1720 12 : GIntBig OGRWFSLayer::GetFeatureCount(int bForce)
1721 : {
1722 12 : if (nFeatures >= 0)
1723 2 : return nFeatures;
1724 :
1725 10 : if (TestCapability(OLCFastFeatureCount))
1726 0 : return poBaseLayer->GetFeatureCount(bForce);
1727 :
1728 20 : if ((m_poAttrQuery == nullptr || !osWFSWhere.empty()) &&
1729 10 : poDS->GetFeatureSupportHits())
1730 : {
1731 9 : nFeatures = ExecuteGetFeatureResultTypeHits();
1732 9 : if (nFeatures >= 0)
1733 3 : return nFeatures;
1734 : }
1735 :
1736 : /* If we have not yet the base layer, try to read one */
1737 : /* feature, and then query again OLCFastFeatureCount on the */
1738 : /* base layer. In case the WFS response would contain the */
1739 : /* number of features */
1740 7 : if (poBaseLayer == nullptr)
1741 : {
1742 7 : ResetReading();
1743 7 : OGRFeature *poFeature = GetNextFeature();
1744 7 : delete poFeature;
1745 7 : ResetReading();
1746 :
1747 7 : if (TestCapability(OLCFastFeatureCount))
1748 0 : return poBaseLayer->GetFeatureCount(bForce);
1749 : }
1750 :
1751 : /* In some cases, we can evaluate the result of GetFeatureCount() */
1752 : /* and GetExtent() with the same data */
1753 7 : if (CanRunGetFeatureCountAndGetExtentTogether())
1754 : {
1755 1 : OGREnvelope sDummy;
1756 1 : GetExtent(&sDummy);
1757 : }
1758 :
1759 7 : if (nFeatures < 0)
1760 7 : nFeatures = OGRLayer::GetFeatureCount(bForce);
1761 :
1762 7 : return nFeatures;
1763 : }
1764 :
1765 : /************************************************************************/
1766 : /* SetExtents() */
1767 : /************************************************************************/
1768 :
1769 152 : void OGRWFSLayer::SetExtents(double dfMinXIn, double dfMinYIn, double dfMaxXIn,
1770 : double dfMaxYIn)
1771 : {
1772 152 : m_oExtents.MinX = dfMinXIn;
1773 152 : m_oExtents.MinY = dfMinYIn;
1774 152 : m_oExtents.MaxX = dfMaxXIn;
1775 152 : m_oExtents.MaxY = dfMaxYIn;
1776 152 : }
1777 :
1778 : /************************************************************************/
1779 : /* SetWGS84Extents() */
1780 : /************************************************************************/
1781 :
1782 152 : void OGRWFSLayer::SetWGS84Extents(double dfMinXIn, double dfMinYIn,
1783 : double dfMaxXIn, double dfMaxYIn)
1784 : {
1785 152 : m_oWGS84Extents.MinX = dfMinXIn;
1786 152 : m_oWGS84Extents.MinY = dfMinYIn;
1787 152 : m_oWGS84Extents.MaxX = dfMaxXIn;
1788 152 : m_oWGS84Extents.MaxY = dfMaxYIn;
1789 152 : }
1790 :
1791 : /************************************************************************/
1792 : /* GetExtent() */
1793 : /************************************************************************/
1794 :
1795 16 : OGRErr OGRWFSLayer::GetExtent(OGREnvelope *psExtent, int bForce)
1796 : {
1797 16 : if (m_oExtents.IsInit())
1798 : {
1799 9 : *psExtent = m_oExtents;
1800 9 : return OGRERR_NONE;
1801 : }
1802 :
1803 : /* If we have not yet the base layer, try to read one */
1804 : /* feature, and then query again OLCFastGetExtent on the */
1805 : /* base layer. In case the WFS response would contain the */
1806 : /* global extent */
1807 7 : if (poBaseLayer == nullptr)
1808 : {
1809 7 : ResetReading();
1810 7 : OGRFeature *poFeature = GetNextFeature();
1811 7 : delete poFeature;
1812 7 : ResetReading();
1813 : }
1814 :
1815 7 : if (TestCapability(OLCFastGetExtent))
1816 0 : return poBaseLayer->GetExtent(psExtent, bForce);
1817 :
1818 : /* In some cases, we can evaluate the result of GetFeatureCount() */
1819 : /* and GetExtent() with the same data */
1820 7 : if (CanRunGetFeatureCountAndGetExtentTogether())
1821 : {
1822 7 : bCountFeaturesInGetNextFeature = true;
1823 7 : nFeatures = 0;
1824 : }
1825 :
1826 7 : OGRErr eErr = OGRLayer::GetExtent(psExtent, bForce);
1827 :
1828 7 : if (bCountFeaturesInGetNextFeature)
1829 : {
1830 7 : if (eErr == OGRERR_NONE)
1831 : {
1832 2 : m_oExtents = *psExtent;
1833 : }
1834 : else
1835 : {
1836 5 : nFeatures = -1;
1837 : }
1838 7 : bCountFeaturesInGetNextFeature = false;
1839 : }
1840 :
1841 7 : return eErr;
1842 : }
1843 :
1844 : /************************************************************************/
1845 : /* GetShortName() */
1846 : /************************************************************************/
1847 :
1848 718 : const char *OGRWFSLayer::GetShortName()
1849 : {
1850 718 : const char *pszShortName = strchr(pszName, ':');
1851 718 : if (pszShortName == nullptr)
1852 622 : pszShortName = pszName;
1853 : else
1854 96 : pszShortName++;
1855 718 : return pszShortName;
1856 : }
1857 :
1858 : /************************************************************************/
1859 : /* GetPostHeader() */
1860 : /************************************************************************/
1861 :
1862 62 : CPLString OGRWFSLayer::GetPostHeader()
1863 : {
1864 62 : CPLString osPost;
1865 62 : osPost += "<?xml version=\"1.0\"?>\n";
1866 62 : osPost += "<wfs:Transaction xmlns:wfs=\"http://www.opengis.net/wfs\"\n";
1867 : osPost += " "
1868 62 : "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
1869 62 : osPost += " service=\"WFS\" version=\"";
1870 62 : osPost += poDS->GetVersion();
1871 62 : osPost += "\"\n";
1872 62 : osPost += " xmlns:gml=\"http://www.opengis.net/gml\"\n";
1873 62 : osPost += " xmlns:ogc=\"http://www.opengis.net/ogc\"\n";
1874 : osPost +=
1875 : " xsi:schemaLocation=\"http://www.opengis.net/wfs "
1876 62 : "http://schemas.opengis.net/wfs/";
1877 62 : osPost += poDS->GetVersion();
1878 62 : osPost += "/wfs.xsd ";
1879 62 : osPost += osTargetNamespace;
1880 62 : osPost += " ";
1881 :
1882 : char *pszXMLEncoded =
1883 62 : CPLEscapeString(GetDescribeFeatureTypeURL(FALSE), -1, CPLES_XML);
1884 62 : osPost += pszXMLEncoded;
1885 62 : CPLFree(pszXMLEncoded);
1886 :
1887 62 : osPost += "\">\n";
1888 :
1889 62 : return osPost;
1890 : }
1891 :
1892 : /************************************************************************/
1893 : /* ICreateFeature() */
1894 : /************************************************************************/
1895 :
1896 38 : OGRErr OGRWFSLayer::ICreateFeature(OGRFeature *poFeature)
1897 : {
1898 38 : if (!TestCapability(OLCSequentialWrite))
1899 : {
1900 0 : if (!poDS->SupportTransactions())
1901 0 : CPLError(CE_Failure, CPLE_AppDefined,
1902 : "CreateFeature() not supported: no WMS-T features "
1903 : "advertized by server");
1904 0 : else if (!poDS->UpdateMode())
1905 0 : CPLError(CE_Failure, CPLE_AppDefined,
1906 : "CreateFeature() not supported: datasource opened as "
1907 : "read-only");
1908 0 : return OGRERR_FAILURE;
1909 : }
1910 :
1911 38 : if (poGMLFeatureClass == nullptr)
1912 : {
1913 0 : CPLError(CE_Failure, CPLE_AppDefined,
1914 : "Cannot insert feature because we didn't manage to parse the "
1915 : ".XSD schema");
1916 0 : return OGRERR_FAILURE;
1917 : }
1918 :
1919 38 : if (poFeatureDefn->GetFieldIndex("gml_id") != 0)
1920 : {
1921 0 : CPLError(CE_Failure, CPLE_AppDefined, "Cannot find gml_id field");
1922 0 : return OGRERR_FAILURE;
1923 : }
1924 :
1925 38 : if (poFeature->IsFieldSetAndNotNull(0))
1926 : {
1927 2 : CPLError(CE_Failure, CPLE_AppDefined,
1928 : "Cannot insert a feature when gml_id field is already set");
1929 2 : return OGRERR_FAILURE;
1930 : }
1931 :
1932 72 : CPLString osPost;
1933 :
1934 36 : const char *pszShortName = GetShortName();
1935 :
1936 36 : if (!bInTransaction)
1937 : {
1938 20 : osPost += GetPostHeader();
1939 20 : osPost += " <wfs:Insert>\n";
1940 : }
1941 36 : osPost += " <feature:";
1942 36 : osPost += pszShortName;
1943 36 : osPost += " xmlns:feature=\"";
1944 36 : osPost += osTargetNamespace;
1945 36 : osPost += "\">\n";
1946 :
1947 288 : for (int i = 1; i <= poFeature->GetFieldCount(); i++)
1948 : {
1949 576 : if (poGMLFeatureClass->GetGeometryPropertyCount() == 1 &&
1950 288 : poGMLFeatureClass->GetGeometryProperty(0)->GetAttributeIndex() ==
1951 288 : i - 1)
1952 : {
1953 36 : OGRGeometry *poGeom = poFeature->GetGeometryRef();
1954 36 : if (poGeom != nullptr && !osGeometryColumnName.empty())
1955 : {
1956 2 : if (poGeom->getSpatialReference() == nullptr)
1957 2 : poGeom->assignSpatialReference(m_poSRS);
1958 2 : char *pszGML = nullptr;
1959 2 : if (strcmp(poDS->GetVersion(), "1.1.0") == 0 ||
1960 0 : atoi(poDS->GetVersion()) >= 2)
1961 : {
1962 2 : char **papszOptions = CSLAddString(nullptr, "FORMAT=GML3");
1963 : pszGML =
1964 2 : OGR_G_ExportToGMLEx((OGRGeometryH)poGeom, papszOptions);
1965 2 : CSLDestroy(papszOptions);
1966 : }
1967 : else
1968 0 : pszGML = OGR_G_ExportToGML((OGRGeometryH)poGeom);
1969 2 : osPost += " <feature:";
1970 2 : osPost += osGeometryColumnName;
1971 2 : osPost += ">";
1972 2 : osPost += pszGML;
1973 2 : osPost += "</feature:";
1974 2 : osPost += osGeometryColumnName;
1975 2 : osPost += ">\n";
1976 2 : CPLFree(pszGML);
1977 : }
1978 : }
1979 288 : if (i == poFeature->GetFieldCount())
1980 36 : break;
1981 :
1982 : #ifdef notdef
1983 : if (poFeature->IsFieldNull(i))
1984 : {
1985 : OGRFieldDefn *poFDefn = poFeature->GetFieldDefnRef(i);
1986 : osPost += " <feature:";
1987 : osPost += poFDefn->GetNameRef();
1988 : osPost += " xsi:nil=\"true\" />\n";
1989 : }
1990 : else
1991 : #endif
1992 252 : if (poFeature->IsFieldSet(i) && !poFeature->IsFieldNull(i))
1993 : {
1994 6 : OGRFieldDefn *poFDefn = poFeature->GetFieldDefnRef(i);
1995 6 : osPost += " <feature:";
1996 6 : osPost += poFDefn->GetNameRef();
1997 6 : osPost += ">";
1998 6 : if (poFDefn->GetType() == OFTInteger)
1999 2 : osPost += CPLSPrintf("%d", poFeature->GetFieldAsInteger(i));
2000 4 : else if (poFDefn->GetType() == OFTInteger64)
2001 : osPost +=
2002 0 : CPLSPrintf(CPL_FRMT_GIB, poFeature->GetFieldAsInteger64(i));
2003 4 : else if (poFDefn->GetType() == OFTReal)
2004 2 : osPost += CPLSPrintf("%.16g", poFeature->GetFieldAsDouble(i));
2005 : else
2006 : {
2007 2 : char *pszXMLEncoded = CPLEscapeString(
2008 : poFeature->GetFieldAsString(i), -1, CPLES_XML);
2009 2 : osPost += pszXMLEncoded;
2010 2 : CPLFree(pszXMLEncoded);
2011 : }
2012 6 : osPost += "</feature:";
2013 6 : osPost += poFDefn->GetNameRef();
2014 6 : osPost += ">\n";
2015 : }
2016 : }
2017 :
2018 36 : osPost += " </feature:";
2019 36 : osPost += pszShortName;
2020 36 : osPost += ">\n";
2021 :
2022 36 : if (!bInTransaction)
2023 : {
2024 20 : osPost += " </wfs:Insert>\n";
2025 20 : osPost += "</wfs:Transaction>\n";
2026 : }
2027 : else
2028 : {
2029 16 : osGlobalInsert += osPost;
2030 16 : nExpectedInserts++;
2031 16 : return OGRERR_NONE;
2032 : }
2033 :
2034 20 : CPLDebug("WFS", "Post : %s", osPost.c_str());
2035 :
2036 20 : char **papszOptions = nullptr;
2037 20 : papszOptions = CSLAddNameValue(papszOptions, "POSTFIELDS", osPost.c_str());
2038 : papszOptions =
2039 20 : CSLAddNameValue(papszOptions, "HEADERS",
2040 : "Content-Type: application/xml; charset=UTF-8");
2041 :
2042 : CPLHTTPResult *psResult =
2043 20 : poDS->HTTPFetch(poDS->GetPostTransactionURL(), papszOptions);
2044 20 : CSLDestroy(papszOptions);
2045 :
2046 20 : if (psResult == nullptr)
2047 : {
2048 6 : return OGRERR_FAILURE;
2049 : }
2050 :
2051 14 : if (strstr((const char *)psResult->pabyData, "<ServiceExceptionReport") !=
2052 12 : nullptr ||
2053 12 : strstr((const char *)psResult->pabyData, "<ows:ExceptionReport") !=
2054 : nullptr)
2055 : {
2056 2 : CPLError(CE_Failure, CPLE_AppDefined, "Error returned by server : %s",
2057 : psResult->pabyData);
2058 2 : CPLHTTPDestroyResult(psResult);
2059 2 : return OGRERR_FAILURE;
2060 : }
2061 :
2062 12 : CPLDebug("WFS", "Response: %s", psResult->pabyData);
2063 :
2064 12 : CPLXMLNode *psXML = CPLParseXMLString((const char *)psResult->pabyData);
2065 12 : if (psXML == nullptr)
2066 : {
2067 2 : CPLError(CE_Failure, CPLE_AppDefined, "Invalid XML content : %s",
2068 : psResult->pabyData);
2069 2 : CPLHTTPDestroyResult(psResult);
2070 2 : return OGRERR_FAILURE;
2071 : }
2072 :
2073 10 : CPLStripXMLNamespace(psXML, nullptr, TRUE);
2074 10 : bool bUse100Schema = false;
2075 10 : CPLXMLNode *psRoot = CPLGetXMLNode(psXML, "=TransactionResponse");
2076 10 : if (psRoot == nullptr)
2077 : {
2078 2 : psRoot = CPLGetXMLNode(psXML, "=WFS_TransactionResponse");
2079 2 : if (psRoot)
2080 0 : bUse100Schema = true;
2081 : }
2082 :
2083 10 : if (psRoot == nullptr)
2084 : {
2085 2 : CPLError(CE_Failure, CPLE_AppDefined,
2086 : "Cannot find <TransactionResponse>");
2087 2 : CPLDestroyXMLNode(psXML);
2088 2 : CPLHTTPDestroyResult(psResult);
2089 2 : return OGRERR_FAILURE;
2090 : }
2091 :
2092 8 : CPLXMLNode *psFeatureID = nullptr;
2093 :
2094 8 : if (bUse100Schema)
2095 : {
2096 0 : if (CPLGetXMLNode(psRoot, "TransactionResult.Status.FAILED"))
2097 : {
2098 0 : CPLError(CE_Failure, CPLE_AppDefined, "Insert failed : %s",
2099 : psResult->pabyData);
2100 0 : CPLDestroyXMLNode(psXML);
2101 0 : CPLHTTPDestroyResult(psResult);
2102 0 : return OGRERR_FAILURE;
2103 : }
2104 :
2105 0 : psFeatureID = CPLGetXMLNode(psRoot, "InsertResult.FeatureId");
2106 0 : if (psFeatureID == nullptr)
2107 : {
2108 0 : CPLError(CE_Failure, CPLE_AppDefined,
2109 : "Cannot find InsertResult.FeatureId");
2110 0 : CPLDestroyXMLNode(psXML);
2111 0 : CPLHTTPDestroyResult(psResult);
2112 0 : return OGRERR_FAILURE;
2113 : }
2114 : }
2115 : else
2116 : {
2117 8 : const char *pszFeatureIdElt = atoi(poDS->GetVersion()) >= 2
2118 8 : ? "InsertResults.Feature.ResourceId"
2119 8 : : "InsertResults.Feature.FeatureId";
2120 8 : psFeatureID = CPLGetXMLNode(psRoot, pszFeatureIdElt);
2121 8 : if (psFeatureID == nullptr)
2122 : {
2123 2 : CPLError(CE_Failure, CPLE_AppDefined, "Cannot find %s",
2124 : pszFeatureIdElt);
2125 2 : CPLDestroyXMLNode(psXML);
2126 2 : CPLHTTPDestroyResult(psResult);
2127 2 : return OGRERR_FAILURE;
2128 : }
2129 : }
2130 :
2131 6 : const char *pszFIDAttr = atoi(poDS->GetVersion()) >= 2 ? "rid" : "fid";
2132 6 : const char *pszFID = CPLGetXMLValue(psFeatureID, pszFIDAttr, nullptr);
2133 6 : if (pszFID == nullptr)
2134 : {
2135 2 : CPLError(CE_Failure, CPLE_AppDefined, "Cannot find %s", pszFIDAttr);
2136 2 : CPLDestroyXMLNode(psXML);
2137 2 : CPLHTTPDestroyResult(psResult);
2138 2 : return OGRERR_FAILURE;
2139 : }
2140 :
2141 4 : poFeature->SetField("gml_id", pszFID);
2142 :
2143 : /* If the returned fid is of the form layer_name.num, then use */
2144 : /* num as the OGR FID */
2145 4 : if (strncmp(pszFID, pszShortName, strlen(pszShortName)) == 0 &&
2146 4 : pszFID[strlen(pszShortName)] == '.')
2147 : {
2148 4 : GIntBig nFID = CPLAtoGIntBig(pszFID + strlen(pszShortName) + 1);
2149 4 : poFeature->SetFID(nFID);
2150 : }
2151 :
2152 4 : CPLDebug("WFS", "Got FID = " CPL_FRMT_GIB, poFeature->GetFID());
2153 :
2154 4 : CPLDestroyXMLNode(psXML);
2155 4 : CPLHTTPDestroyResult(psResult);
2156 :
2157 : /* Invalidate layer */
2158 4 : bReloadNeeded = true;
2159 4 : nFeatures = -1;
2160 4 : m_oExtents = OGREnvelope();
2161 :
2162 4 : return OGRERR_NONE;
2163 : }
2164 :
2165 : /************************************************************************/
2166 : /* ISetFeature() */
2167 : /************************************************************************/
2168 :
2169 16 : OGRErr OGRWFSLayer::ISetFeature(OGRFeature *poFeature)
2170 : {
2171 16 : if (!TestCapability(OLCRandomWrite))
2172 : {
2173 0 : if (!poDS->SupportTransactions())
2174 0 : CPLError(CE_Failure, CPLE_AppDefined,
2175 : "SetFeature() not supported: no WMS-T features advertized "
2176 : "by server");
2177 0 : else if (!poDS->UpdateMode())
2178 0 : CPLError(
2179 : CE_Failure, CPLE_AppDefined,
2180 : "SetFeature() not supported: datasource opened as read-only");
2181 0 : return OGRERR_FAILURE;
2182 : }
2183 :
2184 16 : if (poFeatureDefn->GetFieldIndex("gml_id") != 0)
2185 : {
2186 0 : CPLError(CE_Failure, CPLE_AppDefined, "Cannot find gml_id field");
2187 0 : return OGRERR_FAILURE;
2188 : }
2189 :
2190 16 : if (poFeature->IsFieldSetAndNotNull(0) == FALSE)
2191 : {
2192 2 : CPLError(CE_Failure, CPLE_AppDefined,
2193 : "Cannot update a feature when gml_id field is not set");
2194 2 : return OGRERR_FAILURE;
2195 : }
2196 :
2197 14 : if (bInTransaction)
2198 : {
2199 0 : CPLError(
2200 : CE_Warning, CPLE_AppDefined,
2201 : "SetFeature() not yet dealt in transaction. Issued immediately");
2202 : }
2203 :
2204 14 : const char *pszShortName = GetShortName();
2205 :
2206 28 : CPLString osPost;
2207 14 : osPost += GetPostHeader();
2208 :
2209 14 : osPost += " <wfs:Update typeName=\"feature:";
2210 14 : osPost += pszShortName;
2211 14 : osPost += "\" xmlns:feature=\"";
2212 14 : osPost += osTargetNamespace;
2213 14 : osPost += "\">\n";
2214 :
2215 14 : OGRGeometry *poGeom = poFeature->GetGeometryRef();
2216 14 : if (!osGeometryColumnName.empty())
2217 : {
2218 14 : osPost += " <wfs:Property>\n";
2219 14 : osPost += " <wfs:Name>";
2220 14 : osPost += osGeometryColumnName;
2221 14 : osPost += "</wfs:Name>\n";
2222 14 : if (poGeom != nullptr)
2223 : {
2224 2 : if (poGeom->getSpatialReference() == nullptr)
2225 2 : poGeom->assignSpatialReference(m_poSRS);
2226 2 : char *pszGML = nullptr;
2227 2 : if (strcmp(poDS->GetVersion(), "1.1.0") == 0 ||
2228 0 : atoi(poDS->GetVersion()) >= 2)
2229 : {
2230 2 : char **papszOptions = CSLAddString(nullptr, "FORMAT=GML3");
2231 : pszGML =
2232 2 : OGR_G_ExportToGMLEx((OGRGeometryH)poGeom, papszOptions);
2233 2 : CSLDestroy(papszOptions);
2234 : }
2235 : else
2236 0 : pszGML = OGR_G_ExportToGML((OGRGeometryH)poGeom);
2237 2 : osPost += " <wfs:Value>";
2238 2 : osPost += pszGML;
2239 2 : osPost += "</wfs:Value>\n";
2240 2 : CPLFree(pszGML);
2241 : }
2242 14 : osPost += " </wfs:Property>\n";
2243 : }
2244 :
2245 112 : for (int i = 1; i < poFeature->GetFieldCount(); i++)
2246 : {
2247 98 : OGRFieldDefn *poFDefn = poFeature->GetFieldDefnRef(i);
2248 :
2249 98 : osPost += " <wfs:Property>\n";
2250 98 : osPost += " <wfs:Name>";
2251 98 : osPost += poFDefn->GetNameRef();
2252 98 : osPost += "</wfs:Name>\n";
2253 98 : if (poFeature->IsFieldSetAndNotNull(i))
2254 : {
2255 6 : osPost += " <wfs:Value>";
2256 6 : if (poFDefn->GetType() == OFTInteger)
2257 2 : osPost += CPLSPrintf("%d", poFeature->GetFieldAsInteger(i));
2258 4 : else if (poFDefn->GetType() == OFTInteger64)
2259 : osPost +=
2260 0 : CPLSPrintf(CPL_FRMT_GIB, poFeature->GetFieldAsInteger64(i));
2261 4 : else if (poFDefn->GetType() == OFTReal)
2262 2 : osPost += CPLSPrintf("%.16g", poFeature->GetFieldAsDouble(i));
2263 : else
2264 : {
2265 2 : char *pszXMLEncoded = CPLEscapeString(
2266 : poFeature->GetFieldAsString(i), -1, CPLES_XML);
2267 2 : osPost += pszXMLEncoded;
2268 2 : CPLFree(pszXMLEncoded);
2269 : }
2270 6 : osPost += "</wfs:Value>\n";
2271 : }
2272 98 : osPost += " </wfs:Property>\n";
2273 : }
2274 14 : osPost += " <ogc:Filter>\n";
2275 14 : if (poDS->UseFeatureId() || bUseFeatureIdAtLayerLevel)
2276 0 : osPost += " <ogc:FeatureId fid=\"";
2277 14 : else if (atoi(poDS->GetVersion()) >= 2)
2278 0 : osPost += " <ogc:ResourceId rid=\"";
2279 : else
2280 14 : osPost += " <ogc:GmlObjectId gml:id=\"";
2281 14 : osPost += poFeature->GetFieldAsString(0);
2282 14 : osPost += "\"/>\n";
2283 14 : osPost += " </ogc:Filter>\n";
2284 14 : osPost += " </wfs:Update>\n";
2285 14 : osPost += "</wfs:Transaction>\n";
2286 :
2287 14 : CPLDebug("WFS", "Post : %s", osPost.c_str());
2288 :
2289 14 : char **papszOptions = nullptr;
2290 14 : papszOptions = CSLAddNameValue(papszOptions, "POSTFIELDS", osPost.c_str());
2291 : papszOptions =
2292 14 : CSLAddNameValue(papszOptions, "HEADERS",
2293 : "Content-Type: application/xml; charset=UTF-8");
2294 :
2295 : CPLHTTPResult *psResult =
2296 14 : poDS->HTTPFetch(poDS->GetPostTransactionURL(), papszOptions);
2297 14 : CSLDestroy(papszOptions);
2298 :
2299 14 : if (psResult == nullptr)
2300 : {
2301 4 : return OGRERR_FAILURE;
2302 : }
2303 :
2304 10 : if (strstr((const char *)psResult->pabyData, "<ServiceExceptionReport") !=
2305 8 : nullptr ||
2306 8 : strstr((const char *)psResult->pabyData, "<ows:ExceptionReport") !=
2307 : nullptr)
2308 : {
2309 2 : CPLError(CE_Failure, CPLE_AppDefined, "Error returned by server : %s",
2310 : psResult->pabyData);
2311 2 : CPLHTTPDestroyResult(psResult);
2312 2 : return OGRERR_FAILURE;
2313 : }
2314 :
2315 8 : CPLDebug("WFS", "Response: %s", psResult->pabyData);
2316 :
2317 8 : CPLXMLNode *psXML = CPLParseXMLString((const char *)psResult->pabyData);
2318 8 : if (psXML == nullptr)
2319 : {
2320 2 : CPLError(CE_Failure, CPLE_AppDefined, "Invalid XML content : %s",
2321 : psResult->pabyData);
2322 2 : CPLHTTPDestroyResult(psResult);
2323 2 : return OGRERR_FAILURE;
2324 : }
2325 :
2326 6 : CPLStripXMLNamespace(psXML, nullptr, TRUE);
2327 6 : int bUse100Schema = false;
2328 6 : CPLXMLNode *psRoot = CPLGetXMLNode(psXML, "=TransactionResponse");
2329 6 : if (psRoot == nullptr)
2330 : {
2331 2 : psRoot = CPLGetXMLNode(psXML, "=WFS_TransactionResponse");
2332 2 : if (psRoot)
2333 0 : bUse100Schema = true;
2334 : }
2335 6 : if (psRoot == nullptr)
2336 : {
2337 2 : CPLError(CE_Failure, CPLE_AppDefined,
2338 : "Cannot find <TransactionResponse>");
2339 2 : CPLDestroyXMLNode(psXML);
2340 2 : CPLHTTPDestroyResult(psResult);
2341 2 : return OGRERR_FAILURE;
2342 : }
2343 :
2344 4 : if (bUse100Schema)
2345 : {
2346 0 : if (CPLGetXMLNode(psRoot, "TransactionResult.Status.FAILED"))
2347 : {
2348 0 : CPLError(CE_Failure, CPLE_AppDefined, "Update failed : %s",
2349 : psResult->pabyData);
2350 0 : CPLDestroyXMLNode(psXML);
2351 0 : CPLHTTPDestroyResult(psResult);
2352 0 : return OGRERR_FAILURE;
2353 : }
2354 : }
2355 :
2356 4 : CPLDestroyXMLNode(psXML);
2357 4 : CPLHTTPDestroyResult(psResult);
2358 :
2359 : /* Invalidate layer */
2360 4 : bReloadNeeded = true;
2361 4 : nFeatures = -1;
2362 4 : m_oExtents = OGREnvelope();
2363 :
2364 4 : return OGRERR_NONE;
2365 : }
2366 :
2367 : /************************************************************************/
2368 : /* GetFeature() */
2369 : /************************************************************************/
2370 :
2371 14 : OGRFeature *OGRWFSLayer::GetFeature(GIntBig nFID)
2372 : {
2373 14 : GetLayerDefn();
2374 14 : if (poBaseLayer == nullptr && poFeatureDefn->GetFieldIndex("gml_id") == 0)
2375 : {
2376 : /* This is lovely hackish. We assume that then gml_id will be */
2377 : /* layer_name.number. This is actually what we can observe with */
2378 : /* GeoServer and TinyOWS */
2379 : CPLString osVal =
2380 14 : CPLSPrintf("gml_id = '%s." CPL_FRMT_GIB "'", GetShortName(), nFID);
2381 14 : CPLString osOldSQLWhere(osSQLWhere);
2382 14 : SetAttributeFilter(osVal);
2383 14 : OGRFeature *poFeature = GetNextFeature();
2384 : const char *pszOldFilter =
2385 14 : osOldSQLWhere.size() ? osOldSQLWhere.c_str() : nullptr;
2386 14 : SetAttributeFilter(pszOldFilter);
2387 14 : if (poFeature)
2388 12 : return poFeature;
2389 : }
2390 :
2391 2 : return OGRLayer::GetFeature(nFID);
2392 : }
2393 :
2394 : /************************************************************************/
2395 : /* DeleteFromFilter() */
2396 : /************************************************************************/
2397 :
2398 12 : OGRErr OGRWFSLayer::DeleteFromFilter(const std::string &osOGCFilter)
2399 : {
2400 12 : if (!TestCapability(OLCDeleteFeature))
2401 : {
2402 0 : if (!poDS->SupportTransactions())
2403 0 : CPLError(CE_Failure, CPLE_AppDefined,
2404 : "DeleteFromFilter() not supported: no WMS-T features "
2405 : "advertized by server");
2406 0 : else if (!poDS->UpdateMode())
2407 0 : CPLError(CE_Failure, CPLE_AppDefined,
2408 : "DeleteFromFilter() not supported: datasource opened as "
2409 : "read-only");
2410 0 : return OGRERR_FAILURE;
2411 : }
2412 :
2413 12 : if (poFeatureDefn->GetFieldIndex("gml_id") != 0)
2414 : {
2415 0 : CPLError(CE_Failure, CPLE_AppDefined, "Cannot find gml_id field");
2416 0 : return OGRERR_FAILURE;
2417 : }
2418 12 : const char *pszShortName = GetShortName();
2419 :
2420 24 : CPLString osPost;
2421 12 : osPost += GetPostHeader();
2422 :
2423 12 : osPost += " <wfs:Delete xmlns:feature=\"";
2424 12 : osPost += osTargetNamespace;
2425 12 : osPost += "\" typeName=\"feature:";
2426 12 : osPost += pszShortName;
2427 12 : osPost += "\">\n";
2428 12 : osPost += " <ogc:Filter>\n";
2429 12 : osPost += osOGCFilter;
2430 12 : osPost += " </ogc:Filter>\n";
2431 12 : osPost += " </wfs:Delete>\n";
2432 12 : osPost += "</wfs:Transaction>\n";
2433 :
2434 12 : CPLDebug("WFS", "Post : %s", osPost.c_str());
2435 :
2436 12 : char **papszOptions = nullptr;
2437 12 : papszOptions = CSLAddNameValue(papszOptions, "POSTFIELDS", osPost.c_str());
2438 : papszOptions =
2439 12 : CSLAddNameValue(papszOptions, "HEADERS",
2440 : "Content-Type: application/xml; charset=UTF-8");
2441 :
2442 : CPLHTTPResult *psResult =
2443 12 : poDS->HTTPFetch(poDS->GetPostTransactionURL(), papszOptions);
2444 12 : CSLDestroy(papszOptions);
2445 :
2446 12 : if (psResult == nullptr)
2447 : {
2448 4 : return OGRERR_FAILURE;
2449 : }
2450 :
2451 8 : if (strstr((const char *)psResult->pabyData, "<ServiceExceptionReport") !=
2452 8 : nullptr ||
2453 8 : strstr((const char *)psResult->pabyData, "<ows:ExceptionReport") !=
2454 : nullptr)
2455 : {
2456 0 : CPLError(CE_Failure, CPLE_AppDefined, "Error returned by server : %s",
2457 : psResult->pabyData);
2458 0 : CPLHTTPDestroyResult(psResult);
2459 0 : return OGRERR_FAILURE;
2460 : }
2461 :
2462 8 : CPLDebug("WFS", "Response: %s", psResult->pabyData);
2463 :
2464 8 : CPLXMLNode *psXML = CPLParseXMLString((const char *)psResult->pabyData);
2465 8 : if (psXML == nullptr)
2466 : {
2467 2 : CPLError(CE_Failure, CPLE_AppDefined, "Invalid XML content : %s",
2468 : psResult->pabyData);
2469 2 : CPLHTTPDestroyResult(psResult);
2470 2 : return OGRERR_FAILURE;
2471 : }
2472 :
2473 6 : CPLStripXMLNamespace(psXML, nullptr, TRUE);
2474 6 : bool bUse100Schema = false;
2475 6 : CPLXMLNode *psRoot = CPLGetXMLNode(psXML, "=TransactionResponse");
2476 6 : if (psRoot == nullptr)
2477 : {
2478 2 : psRoot = CPLGetXMLNode(psXML, "=WFS_TransactionResponse");
2479 2 : if (psRoot)
2480 0 : bUse100Schema = true;
2481 : }
2482 6 : if (psRoot == nullptr)
2483 : {
2484 2 : CPLError(CE_Failure, CPLE_AppDefined,
2485 : "Cannot find <TransactionResponse>");
2486 2 : CPLDestroyXMLNode(psXML);
2487 2 : CPLHTTPDestroyResult(psResult);
2488 2 : return OGRERR_FAILURE;
2489 : }
2490 :
2491 4 : if (bUse100Schema)
2492 : {
2493 0 : if (CPLGetXMLNode(psRoot, "TransactionResult.Status.FAILED"))
2494 : {
2495 0 : CPLError(CE_Failure, CPLE_AppDefined, "Delete failed : %s",
2496 : psResult->pabyData);
2497 0 : CPLDestroyXMLNode(psXML);
2498 0 : CPLHTTPDestroyResult(psResult);
2499 0 : return OGRERR_FAILURE;
2500 : }
2501 : }
2502 :
2503 4 : CPLDestroyXMLNode(psXML);
2504 4 : CPLHTTPDestroyResult(psResult);
2505 :
2506 : /* Invalidate layer */
2507 4 : bReloadNeeded = true;
2508 4 : nFeatures = -1;
2509 4 : m_oExtents = OGREnvelope();
2510 :
2511 4 : return OGRERR_NONE;
2512 : }
2513 :
2514 : /************************************************************************/
2515 : /* DeleteFeature() */
2516 : /************************************************************************/
2517 :
2518 12 : OGRErr OGRWFSLayer::DeleteFeature(GIntBig nFID)
2519 : {
2520 12 : if (!TestCapability(OLCDeleteFeature))
2521 : {
2522 0 : if (!poDS->SupportTransactions())
2523 0 : CPLError(CE_Failure, CPLE_AppDefined,
2524 : "DeleteFeature() not supported: no WMS-T features "
2525 : "advertized by server");
2526 0 : else if (!poDS->UpdateMode())
2527 0 : CPLError(CE_Failure, CPLE_AppDefined,
2528 : "DeleteFeature() not supported: datasource opened as "
2529 : "read-only");
2530 0 : return OGRERR_FAILURE;
2531 : }
2532 :
2533 12 : if (poFeatureDefn->GetFieldIndex("gml_id") != 0)
2534 : {
2535 0 : CPLError(CE_Failure, CPLE_AppDefined, "Cannot find gml_id field");
2536 0 : return OGRERR_FAILURE;
2537 : }
2538 :
2539 12 : OGRFeature *poFeature = GetFeature(nFID);
2540 12 : if (poFeature == nullptr)
2541 : {
2542 2 : CPLError(CE_Failure, CPLE_AppDefined,
2543 : "Cannot find feature " CPL_FRMT_GIB, nFID);
2544 2 : return OGRERR_FAILURE;
2545 : }
2546 :
2547 10 : const char *pszGMLID = poFeature->GetFieldAsString("gml_id");
2548 10 : if (pszGMLID == nullptr)
2549 : {
2550 0 : CPLError(CE_Failure, CPLE_AppDefined,
2551 : "Cannot delete a feature with gml_id unset");
2552 0 : delete poFeature;
2553 0 : return OGRERR_FAILURE;
2554 : }
2555 :
2556 10 : if (bInTransaction)
2557 : {
2558 0 : CPLError(
2559 : CE_Warning, CPLE_AppDefined,
2560 : "DeleteFeature() not yet dealt in transaction. Issued immediately");
2561 : }
2562 :
2563 20 : CPLString osGMLID = pszGMLID;
2564 10 : pszGMLID = nullptr;
2565 10 : delete poFeature;
2566 10 : poFeature = nullptr;
2567 :
2568 20 : CPLString osFilter;
2569 10 : osFilter = "<ogc:FeatureId fid=\"";
2570 10 : osFilter += osGMLID;
2571 10 : osFilter += "\"/>\n";
2572 10 : return DeleteFromFilter(osFilter);
2573 : }
2574 :
2575 : /************************************************************************/
2576 : /* StartTransaction() */
2577 : /************************************************************************/
2578 :
2579 24 : OGRErr OGRWFSLayer::StartTransaction()
2580 : {
2581 24 : if (!TestCapability(OLCTransactions))
2582 : {
2583 0 : if (!poDS->SupportTransactions())
2584 0 : CPLError(CE_Failure, CPLE_AppDefined,
2585 : "StartTransaction() not supported: no WMS-T features "
2586 : "advertized by server");
2587 0 : else if (!poDS->UpdateMode())
2588 0 : CPLError(CE_Failure, CPLE_AppDefined,
2589 : "StartTransaction() not supported: datasource opened as "
2590 : "read-only");
2591 0 : return OGRERR_FAILURE;
2592 : }
2593 :
2594 24 : if (bInTransaction)
2595 : {
2596 2 : CPLError(CE_Failure, CPLE_AppDefined,
2597 : "StartTransaction() has already been called");
2598 2 : return OGRERR_FAILURE;
2599 : }
2600 :
2601 22 : bInTransaction = true;
2602 22 : osGlobalInsert = "";
2603 22 : nExpectedInserts = 0;
2604 22 : aosFIDList.resize(0);
2605 :
2606 22 : return OGRERR_NONE;
2607 : }
2608 :
2609 : /************************************************************************/
2610 : /* CommitTransaction() */
2611 : /************************************************************************/
2612 :
2613 20 : OGRErr OGRWFSLayer::CommitTransaction()
2614 : {
2615 20 : if (!TestCapability(OLCTransactions))
2616 : {
2617 0 : if (!poDS->SupportTransactions())
2618 0 : CPLError(CE_Failure, CPLE_AppDefined,
2619 : "CommitTransaction() not supported: no WMS-T features "
2620 : "advertized by server");
2621 0 : else if (!poDS->UpdateMode())
2622 0 : CPLError(CE_Failure, CPLE_AppDefined,
2623 : "CommitTransaction() not supported: datasource opened as "
2624 : "read-only");
2625 0 : return OGRERR_FAILURE;
2626 : }
2627 :
2628 20 : if (!bInTransaction)
2629 : {
2630 2 : CPLError(CE_Failure, CPLE_AppDefined,
2631 : "StartTransaction() has not yet been called");
2632 2 : return OGRERR_FAILURE;
2633 : }
2634 :
2635 18 : if (!osGlobalInsert.empty())
2636 : {
2637 16 : CPLString osPost = GetPostHeader();
2638 16 : osPost += " <wfs:Insert>\n";
2639 16 : osPost += osGlobalInsert;
2640 16 : osPost += " </wfs:Insert>\n";
2641 16 : osPost += "</wfs:Transaction>\n";
2642 :
2643 16 : bInTransaction = false;
2644 16 : osGlobalInsert = "";
2645 16 : int l_nExpectedInserts = nExpectedInserts;
2646 16 : nExpectedInserts = 0;
2647 :
2648 16 : CPLDebug("WFS", "Post : %s", osPost.c_str());
2649 :
2650 16 : char **papszOptions = nullptr;
2651 : papszOptions =
2652 16 : CSLAddNameValue(papszOptions, "POSTFIELDS", osPost.c_str());
2653 : papszOptions =
2654 16 : CSLAddNameValue(papszOptions, "HEADERS",
2655 : "Content-Type: application/xml; charset=UTF-8");
2656 :
2657 : CPLHTTPResult *psResult =
2658 16 : poDS->HTTPFetch(poDS->GetPostTransactionURL(), papszOptions);
2659 16 : CSLDestroy(papszOptions);
2660 :
2661 16 : if (psResult == nullptr)
2662 : {
2663 0 : return OGRERR_FAILURE;
2664 : }
2665 :
2666 16 : if (strstr((const char *)psResult->pabyData,
2667 14 : "<ServiceExceptionReport") != nullptr ||
2668 14 : strstr((const char *)psResult->pabyData, "<ows:ExceptionReport") !=
2669 : nullptr)
2670 : {
2671 2 : CPLError(CE_Failure, CPLE_AppDefined,
2672 : "Error returned by server : %s", psResult->pabyData);
2673 2 : CPLHTTPDestroyResult(psResult);
2674 2 : return OGRERR_FAILURE;
2675 : }
2676 :
2677 14 : CPLDebug("WFS", "Response: %s", psResult->pabyData);
2678 :
2679 14 : CPLXMLNode *psXML = CPLParseXMLString((const char *)psResult->pabyData);
2680 14 : if (psXML == nullptr)
2681 : {
2682 2 : CPLError(CE_Failure, CPLE_AppDefined, "Invalid XML content : %s",
2683 : psResult->pabyData);
2684 2 : CPLHTTPDestroyResult(psResult);
2685 2 : return OGRERR_FAILURE;
2686 : }
2687 :
2688 12 : CPLStripXMLNamespace(psXML, nullptr, TRUE);
2689 12 : bool bUse100Schema = false;
2690 12 : CPLXMLNode *psRoot = CPLGetXMLNode(psXML, "=TransactionResponse");
2691 12 : if (psRoot == nullptr)
2692 : {
2693 2 : psRoot = CPLGetXMLNode(psXML, "=WFS_TransactionResponse");
2694 2 : if (psRoot)
2695 0 : bUse100Schema = true;
2696 : }
2697 :
2698 12 : if (psRoot == nullptr)
2699 : {
2700 2 : CPLError(CE_Failure, CPLE_AppDefined,
2701 : "Cannot find <TransactionResponse>");
2702 2 : CPLDestroyXMLNode(psXML);
2703 2 : CPLHTTPDestroyResult(psResult);
2704 2 : return OGRERR_FAILURE;
2705 : }
2706 :
2707 10 : if (bUse100Schema)
2708 : {
2709 0 : if (CPLGetXMLNode(psRoot, "TransactionResult.Status.FAILED"))
2710 : {
2711 0 : CPLError(CE_Failure, CPLE_AppDefined, "Insert failed : %s",
2712 : psResult->pabyData);
2713 0 : CPLDestroyXMLNode(psXML);
2714 0 : CPLHTTPDestroyResult(psResult);
2715 0 : return OGRERR_FAILURE;
2716 : }
2717 :
2718 : /* TODO */
2719 : }
2720 : else
2721 : {
2722 10 : int nGotInserted = atoi(
2723 : CPLGetXMLValue(psRoot, "TransactionSummary.totalInserted", ""));
2724 10 : if (nGotInserted != l_nExpectedInserts)
2725 : {
2726 2 : CPLError(
2727 : CE_Failure, CPLE_AppDefined,
2728 : "Only %d features were inserted whereas %d where expected",
2729 : nGotInserted, l_nExpectedInserts);
2730 2 : CPLDestroyXMLNode(psXML);
2731 2 : CPLHTTPDestroyResult(psResult);
2732 2 : return OGRERR_FAILURE;
2733 : }
2734 :
2735 : CPLXMLNode *psInsertResults =
2736 8 : CPLGetXMLNode(psRoot, "InsertResults");
2737 8 : if (psInsertResults == nullptr)
2738 : {
2739 2 : CPLError(CE_Failure, CPLE_AppDefined,
2740 : "Cannot find node InsertResults");
2741 2 : CPLDestroyXMLNode(psXML);
2742 2 : CPLHTTPDestroyResult(psResult);
2743 2 : return OGRERR_FAILURE;
2744 : }
2745 :
2746 6 : aosFIDList.resize(0);
2747 :
2748 6 : CPLXMLNode *psChild = psInsertResults->psChild;
2749 8 : while (psChild)
2750 : {
2751 : const char *pszFID =
2752 4 : CPLGetXMLValue(psChild, "FeatureId.fid", nullptr);
2753 4 : if (pszFID == nullptr)
2754 : {
2755 2 : CPLError(CE_Failure, CPLE_AppDefined, "Cannot find fid");
2756 2 : CPLDestroyXMLNode(psXML);
2757 2 : CPLHTTPDestroyResult(psResult);
2758 2 : return OGRERR_FAILURE;
2759 : }
2760 2 : aosFIDList.push_back(pszFID);
2761 :
2762 2 : psChild = psChild->psNext;
2763 : }
2764 :
2765 4 : if ((int)aosFIDList.size() != nGotInserted)
2766 : {
2767 2 : CPLError(CE_Failure, CPLE_AppDefined,
2768 : "Inconsistent InsertResults: did not get expected FID "
2769 : "count");
2770 2 : CPLDestroyXMLNode(psXML);
2771 2 : CPLHTTPDestroyResult(psResult);
2772 2 : return OGRERR_FAILURE;
2773 : }
2774 : }
2775 :
2776 2 : CPLDestroyXMLNode(psXML);
2777 2 : CPLHTTPDestroyResult(psResult);
2778 : }
2779 :
2780 4 : bInTransaction = false;
2781 4 : osGlobalInsert = "";
2782 4 : nExpectedInserts = 0;
2783 :
2784 4 : return OGRERR_NONE;
2785 : }
2786 :
2787 : /************************************************************************/
2788 : /* RollbackTransaction() */
2789 : /************************************************************************/
2790 :
2791 6 : OGRErr OGRWFSLayer::RollbackTransaction()
2792 : {
2793 6 : if (!TestCapability(OLCTransactions))
2794 : {
2795 0 : if (!poDS->SupportTransactions())
2796 0 : CPLError(CE_Failure, CPLE_AppDefined,
2797 : "RollbackTransaction() not supported: no WMS-T features "
2798 : "advertized by server");
2799 0 : else if (!poDS->UpdateMode())
2800 0 : CPLError(CE_Failure, CPLE_AppDefined,
2801 : "RollbackTransaction() not supported: datasource opened "
2802 : "as read-only");
2803 0 : return OGRERR_FAILURE;
2804 : }
2805 :
2806 6 : if (!bInTransaction)
2807 : {
2808 2 : CPLError(CE_Failure, CPLE_AppDefined,
2809 : "StartTransaction() has not yet been called");
2810 2 : return OGRERR_FAILURE;
2811 : }
2812 :
2813 4 : bInTransaction = false;
2814 4 : osGlobalInsert = "";
2815 4 : nExpectedInserts = 0;
2816 :
2817 4 : return OGRERR_NONE;
2818 : }
2819 :
2820 : /************************************************************************/
2821 : /* SetRequiredOutputFormat() */
2822 : /************************************************************************/
2823 :
2824 0 : void OGRWFSLayer::SetRequiredOutputFormat(const char *pszRequiredOutputFormatIn)
2825 : {
2826 0 : CPLFree(pszRequiredOutputFormat);
2827 0 : if (pszRequiredOutputFormatIn)
2828 : {
2829 0 : pszRequiredOutputFormat = CPLStrdup(pszRequiredOutputFormatIn);
2830 : }
2831 : else
2832 : {
2833 0 : pszRequiredOutputFormat = nullptr;
2834 : }
2835 0 : }
2836 :
2837 : /************************************************************************/
2838 : /* SetOrderBy() */
2839 : /************************************************************************/
2840 :
2841 0 : void OGRWFSLayer::SetOrderBy(const std::vector<OGRWFSSortDesc> &aoSortColumnsIn)
2842 : {
2843 0 : aoSortColumns = aoSortColumnsIn;
2844 0 : }
|