QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgsogcutils.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsogcutils.cpp
3 ---------------------
4 begin : March 2013
5 copyright : (C) 2013 by Martin Dobias
6 email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15#include "qgsogcutils.h"
16
17#include <memory>
18#include <ogr_api.h>
19
21#include "qgsexpression.h"
22#include "qgsexpression_p.h"
26#include "qgsgeometry.h"
27#include "qgslogger.h"
28#include "qgsmultipolygon.h"
29#include "qgsogrutils.h"
30#include "qgspolygon.h"
31#include "qgsrectangle.h"
32#include "qgsstringutils.h"
33#include "qgsvectorlayer.h"
34#include "qgswkbptr.h"
35
36#include <QColor>
37#include <QObject>
38#include <QRegularExpression>
39#include <QString>
40#include <QStringList>
41#include <QTextStream>
42
43using namespace Qt::StringLiterals;
44
45#ifndef Q_OS_WIN
46#include <netinet/in.h>
47#else
48#include <winsock.h>
49#endif
50
51
52#define GML_NAMESPACE u"http://www.opengis.net/gml"_s
53#define GML32_NAMESPACE u"http://www.opengis.net/gml/3.2"_s
54#define OGC_NAMESPACE u"http://www.opengis.net/ogc"_s
55#define FES_NAMESPACE u"http://www.opengis.net/fes/2.0"_s
56#define SE_NAMESPACE u"http://www.opengis.net/se"_s
57
59 QgsOgcUtils::GMLVersion gmlVersion,
60 QgsOgcUtils::FilterVersion filterVersion,
61 const QString &namespacePrefix,
62 const QString &namespaceURI,
63 const QString &geometryName,
64 const QString &srsName,
65 bool honourAxisOrientation,
66 bool invertAxisOrientation,
67 const QMap<QString, QString> &fieldNameToXPathMap,
68 const QMap<QString, QString> &namespacePrefixToUriMap )
69 : mDoc( doc )
70 , mGMLVersion( gmlVersion )
71 , mFilterVersion( filterVersion )
72 , mNamespacePrefix( namespacePrefix )
73 , mNamespaceURI( namespaceURI )
74 , mGeometryName( geometryName )
75 , mSrsName( srsName )
76 , mInvertAxisOrientation( invertAxisOrientation )
77 , mFieldNameToXPathMap( fieldNameToXPathMap )
78 , mNamespacePrefixToUriMap( namespacePrefixToUriMap )
79 , mFilterPrefix( ( filterVersion == QgsOgcUtils::FILTER_FES_2_0 ) ? "fes" : "ogc" )
80 , mPropertyName( ( filterVersion == QgsOgcUtils::FILTER_FES_2_0 ) ? "ValueReference" : "PropertyName" )
81{
83 if ( !mSrsName.isEmpty() )
85 if ( crs.isValid() )
86 {
87 if ( honourAxisOrientation && crs.hasAxisInverted() )
88 {
89 mInvertAxisOrientation = !mInvertAxisOrientation;
90 }
91 }
92}
93
94QgsGeometry QgsOgcUtils::geometryFromGML( const QDomNode &geometryNode, const Context &context )
95{
96 QDomElement geometryTypeElement = geometryNode.toElement();
97 QString geomType = geometryTypeElement.tagName();
98 QgsGeometry geometry;
99
100 if ( !( geomType == "Point"_L1 || geomType == "LineString"_L1 || geomType == "Polygon"_L1 ||
101 geomType == "MultiPoint"_L1 || geomType == "MultiLineString"_L1 || geomType == "MultiPolygon"_L1 ||
102 geomType == "Box"_L1 || geomType == "Envelope"_L1 || geomType == "MultiCurve"_L1 ) )
103 {
104 const QDomNode geometryChild = geometryNode.firstChild();
105 if ( geometryChild.isNull() )
106 {
107 return geometry;
108 }
109 geometryTypeElement = geometryChild.toElement();
110 geomType = geometryTypeElement.tagName();
111 }
112
113 if ( !( geomType == "Point"_L1 || geomType == "LineString"_L1 || geomType == "Polygon"_L1 ||
114 geomType == "MultiPoint"_L1 || geomType == "MultiLineString"_L1 || geomType == "MultiPolygon"_L1 ||
115 geomType == "Box"_L1 || geomType == "Envelope"_L1 || geomType == "MultiCurve"_L1 ) )
116 return QgsGeometry();
117
118 if ( geomType == "Point"_L1 )
119 {
120 geometry = geometryFromGMLPoint( geometryTypeElement );
121 }
122 else if ( geomType == "LineString"_L1 )
123 {
124 geometry = geometryFromGMLLineString( geometryTypeElement );
125 }
126 else if ( geomType == "Polygon"_L1 )
127 {
128 geometry = geometryFromGMLPolygon( geometryTypeElement );
129 }
130 else if ( geomType == "MultiPoint"_L1 )
131 {
132 geometry = geometryFromGMLMultiPoint( geometryTypeElement );
133 }
134 else if ( geomType == "MultiLineString"_L1 )
135 {
136 geometry = geometryFromGMLMultiLineString( geometryTypeElement );
137 }
138 else if ( geomType == "MultiCurve"_L1 )
139 {
140 geometry = geometryFromGMLMultiCurve( geometryTypeElement );
141 }
142 else if ( geomType == "MultiPolygon"_L1 )
143 {
144 geometry = geometryFromGMLMultiPolygon( geometryTypeElement );
145 }
146 else if ( geomType == "Box"_L1 )
147 {
148 geometry = QgsGeometry::fromRect( rectangleFromGMLBox( geometryTypeElement ) );
149 }
150 else if ( geomType == "Envelope"_L1 )
151 {
152 geometry = QgsGeometry::fromRect( rectangleFromGMLEnvelope( geometryTypeElement ) );
153 }
154 else //unknown type
155 {
156 QgsDebugMsgLevel( u"Unknown geometry type %1"_s.arg( geomType ), 2 );
157 return geometry;
158 }
159
160 // Handle srsName
161 // Check if the XY coordinates of geometry need to be swapped by checking the srs from the GML
163 if ( geometryTypeElement.hasAttribute( u"srsName"_s ) )
164 {
165 QString srsName { geometryTypeElement.attribute( u"srsName"_s ) };
166
167 // The logic here follows WFS GeoServer conventions from https://docs.geoserver.org/latest/en/user/services/wfs/axis_order.html
168 const bool ignoreAxisOrientation { srsName.startsWith( "http://www.opengis.net/gml/srs/"_L1 ) || srsName.startsWith( "EPSG:"_L1 ) };
169
170 // GDAL does not recognise http://www.opengis.net/gml/srs/epsg.xml#4326 but it does
171 // http://www.opengis.net/def/crs/EPSG/0/4326 so, let's try that
172 if ( srsName.startsWith( "http://www.opengis.net/gml/srs/"_L1 ) )
173 {
174 const auto parts { srsName.split( QRegularExpression( QStringLiteral( R"raw(/|#|\.)raw" ) ) ) };
175 if ( parts.length() == 10 )
176 {
177 srsName = u"http://www.opengis.net/def/crs/%1/0/%2"_s.arg( parts[ 7 ].toUpper(), parts[ 9 ] );
178 }
179 }
180 geomSrs.createFromUserInput( srsName );
181 if ( geomSrs.isValid() && geomSrs.hasAxisInverted() && !ignoreAxisOrientation )
182 {
183 geometry.get()->swapXy();
184 }
185 }
186
187 // Apply a coordinate transformation if context has information about the layer and the transformation context
188 if ( geomSrs.isValid() && context.layer && geomSrs != context.layer->crs() )
189 {
190 const QgsCoordinateTransform transformer { geomSrs, context.layer->crs(), context.transformContext };
191 try
192 {
193 const Qgis::GeometryOperationResult result = geometry.transform( transformer );
195 {
196 QgsDebugMsgLevel( u"Error transforming geometry: %1"_s.arg( qgsEnumValueToKey( result ) ), 2 );
197 }
198 }
199 catch ( QgsCsException & )
200 {
201 QgsDebugMsgLevel( u"CS error transforming geometry"_s, 2 );
202 }
203 }
204
205 return geometry;
206}
207
208QgsGeometry QgsOgcUtils::geometryFromGML( const QString &xmlString, const Context &context )
209{
210 // wrap the string into a root tag to have "gml" namespace (and also as a default namespace)
211 const QString xml = u"<tmp xmlns=\"%1\" xmlns:gml=\"%1\">%2</tmp>"_s.arg( GML_NAMESPACE, xmlString );
212 QDomDocument doc;
213 if ( !doc.setContent( xml, true ) )
214 return QgsGeometry();
215
216 return geometryFromGML( doc.documentElement().firstChildElement(), context );
217}
218
219
220QgsGeometry QgsOgcUtils::geometryFromGMLPoint( const QDomElement &geometryElement )
221{
222 QgsPolyline pointCoordinate;
223
224 const QDomNodeList coordList = geometryElement.elementsByTagNameNS( GML_NAMESPACE, u"coordinates"_s );
225 if ( !coordList.isEmpty() )
226 {
227 const QDomElement coordElement = coordList.at( 0 ).toElement();
228 if ( readGMLCoordinates( pointCoordinate, coordElement ) != 0 )
229 {
230 return QgsGeometry();
231 }
232 }
233 else
234 {
235 const QDomNodeList posList = geometryElement.elementsByTagNameNS( GML_NAMESPACE, u"pos"_s );
236 if ( posList.size() < 1 )
237 {
238 return QgsGeometry();
239 }
240 const QDomElement posElement = posList.at( 0 ).toElement();
241 if ( readGMLPositions( pointCoordinate, posElement ) != 0 )
242 {
243 return QgsGeometry();
244 }
245 }
246
247 if ( pointCoordinate.empty() )
248 {
249 return QgsGeometry();
250 }
251
252 const bool hasZ { !std::isnan( pointCoordinate.first().z() ) };
253 QgsPolyline::const_iterator point_it = pointCoordinate.constBegin();
254 const char e = static_cast<char>( htonl( 1 ) != 1 );
255 const double x = point_it->x();
256 const double y = point_it->y();
257 const int size = 1 + static_cast<int>( sizeof( int ) ) + ( hasZ ? 3 : 2 ) * static_cast<int>( sizeof( double ) );
258
260 unsigned char *wkb = new unsigned char[size];
261
262 int wkbPosition = 0; //current offset from wkb beginning (in bytes)
263 memcpy( &( wkb )[wkbPosition], &e, 1 );
264 wkbPosition += 1;
265 memcpy( &( wkb )[wkbPosition], &type, sizeof( int ) );
266 wkbPosition += sizeof( int );
267 memcpy( &( wkb )[wkbPosition], &x, sizeof( double ) );
268 wkbPosition += sizeof( double );
269 memcpy( &( wkb )[wkbPosition], &y, sizeof( double ) );
270
271 if ( hasZ )
272 {
273 wkbPosition += sizeof( double );
274 double z = point_it->z();
275 memcpy( &( wkb )[wkbPosition], &z, sizeof( double ) );
276 }
277
278 QgsGeometry g;
279 g.fromWkb( wkb, size );
280 return g;
281}
282
283QgsGeometry QgsOgcUtils::geometryFromGMLLineString( const QDomElement &geometryElement )
284{
285 QgsPolyline lineCoordinates;
286
287 const QDomNodeList coordList = geometryElement.elementsByTagNameNS( GML_NAMESPACE, u"coordinates"_s );
288 if ( !coordList.isEmpty() )
289 {
290 const QDomElement coordElement = coordList.at( 0 ).toElement();
291 if ( readGMLCoordinates( lineCoordinates, coordElement ) != 0 )
292 {
293 return QgsGeometry();
294 }
295 }
296 else
297 {
298 const QDomNodeList posList = geometryElement.elementsByTagNameNS( GML_NAMESPACE, u"posList"_s );
299 if ( posList.size() < 1 )
300 {
301 return QgsGeometry();
302 }
303 const QDomElement posElement = posList.at( 0 ).toElement();
304 if ( readGMLPositions( lineCoordinates, posElement ) != 0 )
305 {
306 return QgsGeometry();
307 }
308 }
309
310 const bool hasZ { !std::isnan( lineCoordinates.first().z() ) };
311
312 char e = static_cast<char>( htonl( 1 ) != 1 );
313 const int size = 1 + 2 * static_cast<int>( sizeof( int ) + lineCoordinates.size() ) * ( hasZ ? 3 : 2 ) * static_cast<int>( sizeof( double ) );
314
316 unsigned char *wkb = new unsigned char[size];
317
318 int wkbPosition = 0; //current offset from wkb beginning (in bytes)
319 double x, y;
320 int nPoints = lineCoordinates.size();
321
322 //fill the contents into *wkb
323 memcpy( &( wkb )[wkbPosition], &e, 1 );
324 wkbPosition += 1;
325 memcpy( &( wkb )[wkbPosition], &type, sizeof( int ) );
326 wkbPosition += sizeof( int );
327 memcpy( &( wkb )[wkbPosition], &nPoints, sizeof( int ) );
328 wkbPosition += sizeof( int );
329
330 QgsPolyline::const_iterator iter;
331 for ( iter = lineCoordinates.constBegin(); iter != lineCoordinates.constEnd(); ++iter )
332 {
333 x = iter->x();
334 y = iter->y();
335 memcpy( &( wkb )[wkbPosition], &x, sizeof( double ) );
336 wkbPosition += sizeof( double );
337 memcpy( &( wkb )[wkbPosition], &y, sizeof( double ) );
338 wkbPosition += sizeof( double );
339
340 if ( hasZ )
341 {
342 double z = iter->z();
343 memcpy( &( wkb )[wkbPosition], &z, sizeof( double ) );
344 wkbPosition += sizeof( double );
345 }
346
347 }
348
349 QgsGeometry g;
350 g.fromWkb( wkb, size );
351 return g;
352}
353
354QgsGeometry QgsOgcUtils::geometryFromGMLPolygon( const QDomElement &geometryElement )
355{
356 //read all the coordinates (as QgsPoint) into memory. Each linear ring has an entry in the vector
357 QgsMultiPolyline ringCoordinates;
358
359 //read coordinates for outer boundary
360 QgsPolyline exteriorPointList;
361 const QDomNodeList outerBoundaryList = geometryElement.elementsByTagNameNS( GML_NAMESPACE, u"outerBoundaryIs"_s );
362 if ( !outerBoundaryList.isEmpty() ) //outer ring is necessary
363 {
364 QDomElement coordinatesElement = outerBoundaryList.at( 0 ).firstChild().firstChild().toElement();
365 if ( coordinatesElement.isNull() )
366 {
367 return QgsGeometry();
368 }
369 if ( readGMLCoordinates( exteriorPointList, coordinatesElement ) != 0 )
370 {
371 return QgsGeometry();
372 }
373 ringCoordinates.push_back( exteriorPointList );
374
375 //read coordinates for inner boundary
376 const QDomNodeList innerBoundaryList = geometryElement.elementsByTagNameNS( GML_NAMESPACE, u"innerBoundaryIs"_s );
377 for ( int i = 0; i < innerBoundaryList.size(); ++i )
378 {
379 QgsPolyline interiorPointList;
380 coordinatesElement = innerBoundaryList.at( i ).firstChild().firstChild().toElement();
381 if ( coordinatesElement.isNull() )
382 {
383 return QgsGeometry();
384 }
385 if ( readGMLCoordinates( interiorPointList, coordinatesElement ) != 0 )
386 {
387 return QgsGeometry();
388 }
389 ringCoordinates.push_back( interiorPointList );
390 }
391 }
392 else
393 {
394 //read coordinates for exterior
395 const QDomNodeList exteriorList = geometryElement.elementsByTagNameNS( GML_NAMESPACE, u"exterior"_s );
396 if ( exteriorList.size() < 1 ) //outer ring is necessary
397 {
398 return QgsGeometry();
399 }
400 const QDomElement posElement = exteriorList.at( 0 ).firstChild().firstChild().toElement();
401 if ( posElement.isNull() )
402 {
403 return QgsGeometry();
404 }
405 if ( readGMLPositions( exteriorPointList, posElement ) != 0 )
406 {
407 return QgsGeometry();
408 }
409 ringCoordinates.push_back( exteriorPointList );
410
411 //read coordinates for inner boundary
412 const QDomNodeList interiorList = geometryElement.elementsByTagNameNS( GML_NAMESPACE, u"interior"_s );
413 for ( int i = 0; i < interiorList.size(); ++i )
414 {
415 QgsPolyline interiorPointList;
416 const QDomElement posElement = interiorList.at( i ).firstChild().firstChild().toElement();
417 if ( posElement.isNull() )
418 {
419 return QgsGeometry();
420 }
421 // Note: readGMLPositions returns true on errors and false on success
422 if ( readGMLPositions( interiorPointList, posElement ) )
423 {
424 return QgsGeometry();
425 }
426 ringCoordinates.push_back( interiorPointList );
427 }
428 }
429
430 //calculate number of bytes to allocate
431 int nrings = ringCoordinates.size();
432 if ( nrings < 1 )
433 return QgsGeometry();
434
435 int npoints = 0;//total number of points
436 for ( QgsMultiPolyline::const_iterator it = ringCoordinates.constBegin(); it != ringCoordinates.constEnd(); ++it )
437 {
438 npoints += it->size();
439 }
440
441 const bool hasZ { !std::isnan( ringCoordinates.first().first().z() ) };
442
443 const int size = 1 + 2 * static_cast<int>( sizeof( int ) ) + nrings * static_cast<int>( sizeof( int ) ) + ( hasZ ? 3 : 2 ) * npoints * static_cast<int>( sizeof( double ) );
444
446 unsigned char *wkb = new unsigned char[size];
447
448 //char e = QgsApplication::endian();
449 char e = static_cast<char>( htonl( 1 ) != 1 );
450 int wkbPosition = 0; //current offset from wkb beginning (in bytes)
451 int nPointsInRing = 0;
452 double x, y, z;
453
454 //fill the contents into *wkb
455 memcpy( &( wkb )[wkbPosition], &e, 1 );
456 wkbPosition += 1;
457 memcpy( &( wkb )[wkbPosition], &type, sizeof( int ) );
458 wkbPosition += sizeof( int );
459 memcpy( &( wkb )[wkbPosition], &nrings, sizeof( int ) );
460 wkbPosition += sizeof( int );
461 for ( QgsMultiPolyline::const_iterator it = ringCoordinates.constBegin(); it != ringCoordinates.constEnd(); ++it )
462 {
463 nPointsInRing = it->size();
464 memcpy( &( wkb )[wkbPosition], &nPointsInRing, sizeof( int ) );
465 wkbPosition += sizeof( int );
466 //iterate through the string list converting the strings to x-/y- doubles
467 QgsPolyline::const_iterator iter;
468 for ( iter = it->begin(); iter != it->end(); ++iter )
469 {
470 x = iter->x();
471 y = iter->y();
472 //qWarning("currentCoordinate: " + QString::number(x) + " // " + QString::number(y));
473 memcpy( &( wkb )[wkbPosition], &x, sizeof( double ) );
474 wkbPosition += sizeof( double );
475 memcpy( &( wkb )[wkbPosition], &y, sizeof( double ) );
476 wkbPosition += sizeof( double );
477
478 if ( hasZ )
479 {
480 z = iter->z();
481 memcpy( &( wkb )[wkbPosition], &z, sizeof( double ) );
482 wkbPosition += sizeof( double );
483 }
484 }
485 }
486
487 QgsGeometry g;
488 g.fromWkb( wkb, size );
489 return g;
490}
491
492QgsGeometry QgsOgcUtils::geometryFromGMLMultiPoint( const QDomElement &geometryElement )
493{
494 QgsPolyline pointList;
495 QgsPolyline currentPoint;
496 const QDomNodeList pointMemberList = geometryElement.elementsByTagNameNS( GML_NAMESPACE, u"pointMember"_s );
497 if ( pointMemberList.size() < 1 )
498 {
499 return QgsGeometry();
500 }
501 QDomNodeList pointNodeList;
502 // coordinates or pos element
503 QDomNodeList coordinatesList;
504 QDomNodeList posList;
505 for ( int i = 0; i < pointMemberList.size(); ++i )
506 {
507 //<Point> element
508 pointNodeList = pointMemberList.at( i ).toElement().elementsByTagNameNS( GML_NAMESPACE, u"Point"_s );
509 if ( pointNodeList.size() < 1 )
510 {
511 continue;
512 }
513 //<coordinates> element
514 coordinatesList = pointNodeList.at( 0 ).toElement().elementsByTagNameNS( GML_NAMESPACE, u"coordinates"_s );
515 if ( !coordinatesList.isEmpty() )
516 {
517 currentPoint.clear();
518 if ( readGMLCoordinates( currentPoint, coordinatesList.at( 0 ).toElement() ) != 0 )
519 {
520 continue;
521 }
522 if ( currentPoint.empty() )
523 {
524 continue;
525 }
526 pointList.push_back( ( *currentPoint.begin() ) );
527 continue;
528 }
529 else
530 {
531 //<pos> element
532 posList = pointNodeList.at( 0 ).toElement().elementsByTagNameNS( GML_NAMESPACE, u"pos"_s );
533 if ( posList.size() < 1 )
534 {
535 continue;
536 }
537 currentPoint.clear();
538 if ( readGMLPositions( currentPoint, posList.at( 0 ).toElement() ) != 0 )
539 {
540 continue;
541 }
542 if ( currentPoint.empty() )
543 {
544 continue;
545 }
546 pointList.push_back( ( *currentPoint.begin() ) );
547 }
548 }
549
550 int nPoints = pointList.size(); //number of points
551 if ( nPoints < 1 )
552 return QgsGeometry();
553
554 const bool hasZ { !std::isnan( pointList.first().z() ) };
555
556 //calculate the required wkb size
557 const int size = 1 + 2 * static_cast<int>( sizeof( int ) ) + static_cast<int>( pointList.size() ) * ( ( hasZ ? 3 : 2 ) * static_cast<int>( sizeof( double ) ) + 1 + static_cast<int>( sizeof( int ) ) );
558
560 unsigned char *wkb = new unsigned char[size];
561
562 //fill the wkb content
563 char e = static_cast<char>( htonl( 1 ) != 1 );
564 int wkbPosition = 0; //current offset from wkb beginning (in bytes)
565 double x, y, z;
566 memcpy( &( wkb )[wkbPosition], &e, 1 );
567 wkbPosition += 1;
568 memcpy( &( wkb )[wkbPosition], &type, sizeof( int ) );
569 wkbPosition += sizeof( int );
570 memcpy( &( wkb )[wkbPosition], &nPoints, sizeof( int ) );
571 wkbPosition += sizeof( int );
572 const Qgis::WkbType pointType { hasZ ? Qgis::WkbType::PointZ : Qgis::WkbType::Point };
573 for ( QgsPolyline::const_iterator it = pointList.constBegin(); it != pointList.constEnd(); ++it )
574 {
575 memcpy( &( wkb )[wkbPosition], &e, 1 );
576 wkbPosition += 1;
577 memcpy( &( wkb )[wkbPosition], &pointType, sizeof( int ) );
578 wkbPosition += sizeof( int );
579 x = it->x();
580 memcpy( &( wkb )[wkbPosition], &x, sizeof( double ) );
581 wkbPosition += sizeof( double );
582 y = it->y();
583 memcpy( &( wkb )[wkbPosition], &y, sizeof( double ) );
584 wkbPosition += sizeof( double );
585
586 if ( hasZ )
587 {
588 z = it->z();
589 memcpy( &( wkb )[wkbPosition], &z, sizeof( double ) );
590 wkbPosition += sizeof( double );
591 }
592 }
593
594 QgsGeometry g;
595 g.fromWkb( wkb, size );
596 return g;
597}
598
599QgsGeometry QgsOgcUtils::geometryFromGMLMultiLineString( const QDomElement &geometryElement )
600{
601 //geoserver has
602 //<gml:MultiLineString>
603 //<gml:lineStringMember>
604 //<gml:LineString>
605
606 //mapserver has directly
607 //<gml:MultiLineString
608 //<gml:LineString
609
610
611 QList< QgsPolyline > lineCoordinates; //first list: lines, second list: points of one line
612 QDomElement currentLineStringElement;
613 QDomNodeList currentCoordList;
614 QDomNodeList currentPosList;
615
616 const QDomNodeList lineStringMemberList = geometryElement.elementsByTagNameNS( GML_NAMESPACE, u"lineStringMember"_s );
617 if ( !lineStringMemberList.isEmpty() ) //geoserver
618 {
619 for ( int i = 0; i < lineStringMemberList.size(); ++i )
620 {
621 const QDomNodeList lineStringNodeList = lineStringMemberList.at( i ).toElement().elementsByTagNameNS( GML_NAMESPACE, u"LineString"_s );
622 if ( lineStringNodeList.size() < 1 )
623 {
624 return QgsGeometry();
625 }
626 currentLineStringElement = lineStringNodeList.at( 0 ).toElement();
627 currentCoordList = currentLineStringElement.elementsByTagNameNS( GML_NAMESPACE, u"coordinates"_s );
628 if ( !currentCoordList.isEmpty() )
629 {
630 QgsPolyline currentPointList;
631 if ( readGMLCoordinates( currentPointList, currentCoordList.at( 0 ).toElement() ) != 0 )
632 {
633 return QgsGeometry();
634 }
635 lineCoordinates.push_back( currentPointList );
636 }
637 else
638 {
639 currentPosList = currentLineStringElement.elementsByTagNameNS( GML_NAMESPACE, u"posList"_s );
640 if ( currentPosList.size() < 1 )
641 {
642 return QgsGeometry();
643 }
644 QgsPolyline currentPointList;
645 if ( readGMLPositions( currentPointList, currentPosList.at( 0 ).toElement() ) != 0 )
646 {
647 return QgsGeometry();
648 }
649 lineCoordinates.push_back( currentPointList );
650 }
651 }
652 }
653 else
654 {
655 const QDomNodeList lineStringList = geometryElement.elementsByTagNameNS( GML_NAMESPACE, u"LineString"_s );
656 if ( !lineStringList.isEmpty() ) //mapserver
657 {
658 for ( int i = 0; i < lineStringList.size(); ++i )
659 {
660 currentLineStringElement = lineStringList.at( i ).toElement();
661 currentCoordList = currentLineStringElement.elementsByTagNameNS( GML_NAMESPACE, u"coordinates"_s );
662 if ( !currentCoordList.isEmpty() )
663 {
664 QgsPolyline currentPointList;
665 if ( readGMLCoordinates( currentPointList, currentCoordList.at( 0 ).toElement() ) != 0 )
666 {
667 return QgsGeometry();
668 }
669 lineCoordinates.push_back( currentPointList );
670 return QgsGeometry();
671 }
672 else
673 {
674 currentPosList = currentLineStringElement.elementsByTagNameNS( GML_NAMESPACE, u"posList"_s );
675 if ( currentPosList.size() < 1 )
676 {
677 return QgsGeometry();
678 }
679 QgsPolyline currentPointList;
680 if ( readGMLPositions( currentPointList, currentPosList.at( 0 ).toElement() ) != 0 )
681 {
682 return QgsGeometry();
683 }
684 lineCoordinates.push_back( currentPointList );
685 }
686 }
687 }
688 else
689 {
690 return QgsGeometry();
691 }
692 }
693
694 int nLines = lineCoordinates.size();
695 if ( nLines < 1 )
696 return QgsGeometry();
697
698 const bool hasZ { !std::isnan( lineCoordinates.first().first().z() ) };
699 const int coordSize { hasZ ? 3 : 2 };
700
701 //calculate the required wkb size
702 int size = static_cast<int>( lineCoordinates.size() + 1 ) * ( 1 + 2 * sizeof( int ) );
703 for ( QList< QgsPolyline >::const_iterator it = lineCoordinates.constBegin(); it != lineCoordinates.constEnd(); ++it )
704 {
705 size += it->size() * coordSize * sizeof( double );
706 }
707
709 unsigned char *wkb = new unsigned char[size];
710
711 //fill the wkb content
712 char e = static_cast<char>( htonl( 1 ) != 1 );
713 int wkbPosition = 0; //current offset from wkb beginning (in bytes)
714 int nPoints; //number of points in a line
715 double x, y, z;
716 memcpy( &( wkb )[wkbPosition], &e, 1 );
717 wkbPosition += 1;
718 memcpy( &( wkb )[wkbPosition], &type, sizeof( int ) );
719 wkbPosition += sizeof( int );
720 memcpy( &( wkb )[wkbPosition], &nLines, sizeof( int ) );
721 wkbPosition += sizeof( int );
723 for ( QList< QgsPolyline >::const_iterator it = lineCoordinates.constBegin(); it != lineCoordinates.constEnd(); ++it )
724 {
725 memcpy( &( wkb )[wkbPosition], &e, 1 );
726 wkbPosition += 1;
727 memcpy( &( wkb )[wkbPosition], &lineType, sizeof( int ) );
728 wkbPosition += sizeof( int );
729 nPoints = it->size();
730 memcpy( &( wkb )[wkbPosition], &nPoints, sizeof( int ) );
731 wkbPosition += sizeof( int );
732 for ( QgsPolyline::const_iterator iter = it->begin(); iter != it->end(); ++iter )
733 {
734 x = iter->x();
735 y = iter->y();
736 // QgsDebugMsgLevel( u"x, y is %1,%2"_s.arg( x, 'f' ).arg( y, 'f' ), 2 );
737 memcpy( &( wkb )[wkbPosition], &x, sizeof( double ) );
738 wkbPosition += sizeof( double );
739 memcpy( &( wkb )[wkbPosition], &y, sizeof( double ) );
740 wkbPosition += sizeof( double );
741
742 if ( hasZ )
743 {
744 z = iter->z();
745 memcpy( &( wkb )[wkbPosition], &z, sizeof( double ) );
746 wkbPosition += sizeof( double );
747 }
748 }
749 }
750
751 QgsGeometry g;
752 g.fromWkb( wkb, size );
753 return g;
754}
755
756QgsGeometry QgsOgcUtils::geometryFromGMLMultiPolygon( const QDomElement &geometryElement )
757{
758 //first list: different polygons, second list: different rings, third list: different points
759 QVector<QgsMultiPolyline> multiPolygonPoints;
760 QDomElement currentPolygonMemberElement;
761 QDomNodeList polygonList;
762 QDomElement currentPolygonElement;
763 // rings in GML2
764 QDomNodeList outerBoundaryList;
765 QDomElement currentOuterBoundaryElement;
766 QDomElement currentInnerBoundaryElement;
767 // rings in GML3
768 QDomNodeList exteriorList;
769 QDomElement currentExteriorElement;
770 QDomElement currentInteriorElement;
771 // linear ring
772 QDomNodeList linearRingNodeList;
773 QDomElement currentLinearRingElement;
774 // Coordinates or position list
775 QDomNodeList currentCoordinateList;
776 QDomNodeList currentPosList;
777
778 const QDomNodeList polygonMemberList = geometryElement.elementsByTagNameNS( GML_NAMESPACE, u"polygonMember"_s );
779 QgsMultiPolyline currentPolygonList;
780 for ( int i = 0; i < polygonMemberList.size(); ++i )
781 {
782 currentPolygonList.resize( 0 ); // preserve capacity - don't use clear
783 currentPolygonMemberElement = polygonMemberList.at( i ).toElement();
784 polygonList = currentPolygonMemberElement.elementsByTagNameNS( GML_NAMESPACE, u"Polygon"_s );
785 if ( polygonList.size() < 1 )
786 {
787 continue;
788 }
789 currentPolygonElement = polygonList.at( 0 ).toElement();
790
791 //find exterior ring
792 outerBoundaryList = currentPolygonElement.elementsByTagNameNS( GML_NAMESPACE, u"outerBoundaryIs"_s );
793 if ( !outerBoundaryList.isEmpty() )
794 {
795 currentOuterBoundaryElement = outerBoundaryList.at( 0 ).toElement();
796 QgsPolyline ringCoordinates;
797
798 linearRingNodeList = currentOuterBoundaryElement.elementsByTagNameNS( GML_NAMESPACE, u"LinearRing"_s );
799 if ( linearRingNodeList.size() < 1 )
800 {
801 continue;
802 }
803 currentLinearRingElement = linearRingNodeList.at( 0 ).toElement();
804 currentCoordinateList = currentLinearRingElement.elementsByTagNameNS( GML_NAMESPACE, u"coordinates"_s );
805 if ( currentCoordinateList.size() < 1 )
806 {
807 continue;
808 }
809 if ( readGMLCoordinates( ringCoordinates, currentCoordinateList.at( 0 ).toElement() ) != 0 )
810 {
811 continue;
812 }
813 currentPolygonList.push_back( ringCoordinates );
814
815 //find interior rings
816 const QDomNodeList innerBoundaryList = currentPolygonElement.elementsByTagNameNS( GML_NAMESPACE, u"innerBoundaryIs"_s );
817 for ( int j = 0; j < innerBoundaryList.size(); ++j )
818 {
819 QgsPolyline ringCoordinates;
820 currentInnerBoundaryElement = innerBoundaryList.at( j ).toElement();
821 linearRingNodeList = currentInnerBoundaryElement.elementsByTagNameNS( GML_NAMESPACE, u"LinearRing"_s );
822 if ( linearRingNodeList.size() < 1 )
823 {
824 continue;
825 }
826 currentLinearRingElement = linearRingNodeList.at( 0 ).toElement();
827 currentCoordinateList = currentLinearRingElement.elementsByTagNameNS( GML_NAMESPACE, u"coordinates"_s );
828 if ( currentCoordinateList.size() < 1 )
829 {
830 continue;
831 }
832 if ( readGMLCoordinates( ringCoordinates, currentCoordinateList.at( 0 ).toElement() ) != 0 )
833 {
834 continue;
835 }
836 currentPolygonList.push_back( ringCoordinates );
837 }
838 }
839 else
840 {
841 //find exterior ring
842 exteriorList = currentPolygonElement.elementsByTagNameNS( GML_NAMESPACE, u"exterior"_s );
843 if ( exteriorList.size() < 1 )
844 {
845 continue;
846 }
847
848 currentExteriorElement = exteriorList.at( 0 ).toElement();
849 QgsPolyline ringPositions;
850
851 linearRingNodeList = currentExteriorElement.elementsByTagNameNS( GML_NAMESPACE, u"LinearRing"_s );
852 if ( linearRingNodeList.size() < 1 )
853 {
854 continue;
855 }
856 currentLinearRingElement = linearRingNodeList.at( 0 ).toElement();
857 currentPosList = currentLinearRingElement.elementsByTagNameNS( GML_NAMESPACE, u"posList"_s );
858 if ( currentPosList.size() < 1 )
859 {
860 continue;
861 }
862 if ( readGMLPositions( ringPositions, currentPosList.at( 0 ).toElement() ) != 0 )
863 {
864 continue;
865 }
866 currentPolygonList.push_back( ringPositions );
867
868 //find interior rings
869 const QDomNodeList interiorList = currentPolygonElement.elementsByTagNameNS( GML_NAMESPACE, u"interior"_s );
870 for ( int j = 0; j < interiorList.size(); ++j )
871 {
872 QgsPolyline ringPositions;
873 currentInteriorElement = interiorList.at( j ).toElement();
874 linearRingNodeList = currentInteriorElement.elementsByTagNameNS( GML_NAMESPACE, u"LinearRing"_s );
875 if ( linearRingNodeList.size() < 1 )
876 {
877 continue;
878 }
879 currentLinearRingElement = linearRingNodeList.at( 0 ).toElement();
880 currentPosList = currentLinearRingElement.elementsByTagNameNS( GML_NAMESPACE, u"posList"_s );
881 if ( currentPosList.size() < 1 )
882 {
883 continue;
884 }
885 if ( readGMLPositions( ringPositions, currentPosList.at( 0 ).toElement() ) != 0 )
886 {
887 continue;
888 }
889 currentPolygonList.push_back( ringPositions );
890 }
891 }
892 multiPolygonPoints.push_back( currentPolygonList );
893 }
894
895 int nPolygons = multiPolygonPoints.size();
896 if ( nPolygons < 1 )
897 return QgsGeometry();
898
899 const bool hasZ { !std::isnan( multiPolygonPoints.first().first().first().z() ) };
900
901 int size = 1 + 2 * sizeof( int );
902 //calculate the wkb size
903
904 for ( auto it = multiPolygonPoints.constBegin(); it != multiPolygonPoints.constEnd(); ++it )
905 {
906 size += 1 + 2 * sizeof( int );
907 for ( auto iter = it->begin(); iter != it->end(); ++iter )
908 {
909 size += static_cast<int>( sizeof( int ) ) + ( hasZ ? 3 : 2 ) * static_cast<int>( iter->size() * sizeof( double ) );
910 }
911 }
912
914 unsigned char *wkb = new unsigned char[size];
915
916 char e = static_cast<char>( htonl( 1 ) != 1 );
917 int wkbPosition = 0; //current offset from wkb beginning (in bytes)
918 double x, y;
919 int nRings;
920 int nPointsInRing;
921
922 //fill the contents into *wkb
923 memcpy( &( wkb )[wkbPosition], &e, 1 );
924 wkbPosition += 1;
925 memcpy( &( wkb )[wkbPosition], &type, sizeof( int ) );
926 wkbPosition += sizeof( int );
927 memcpy( &( wkb )[wkbPosition], &nPolygons, sizeof( int ) );
928 wkbPosition += sizeof( int );
929
931
932 for ( auto it = multiPolygonPoints.constBegin(); it != multiPolygonPoints.constEnd(); ++it )
933 {
934 memcpy( &( wkb )[wkbPosition], &e, 1 );
935 wkbPosition += 1;
936 memcpy( &( wkb )[wkbPosition], &type, sizeof( int ) );
937 wkbPosition += sizeof( int );
938 nRings = it->size();
939 memcpy( &( wkb )[wkbPosition], &nRings, sizeof( int ) );
940 wkbPosition += sizeof( int );
941 for ( auto iter = it->begin(); iter != it->end(); ++iter )
942 {
943 nPointsInRing = iter->size();
944 memcpy( &( wkb )[wkbPosition], &nPointsInRing, sizeof( int ) );
945 wkbPosition += sizeof( int );
946 for ( auto iterator = iter->begin(); iterator != iter->end(); ++iterator )
947 {
948 x = iterator->x();
949 y = iterator->y();
950 memcpy( &( wkb )[wkbPosition], &x, sizeof( double ) );
951 wkbPosition += sizeof( double );
952 memcpy( &( wkb )[wkbPosition], &y, sizeof( double ) );
953 wkbPosition += sizeof( double );
954 if ( hasZ )
955 {
956 double z = iterator->z();
957 memcpy( &( wkb )[wkbPosition], &z, sizeof( double ) );
958 wkbPosition += sizeof( double );
959 }
960 }
961 }
962 }
963
964 QgsGeometry g;
965 g.fromWkb( wkb, size );
966 return g;
967}
968
969QDomElement QgsOgcUtils::filterElement( QDomDocument &doc, GMLVersion gmlVersion, FilterVersion filterVersion, bool GMLUsed )
970{
971 QDomElement filterElem =
972 ( filterVersion == FILTER_FES_2_0 ) ?
973 doc.createElementNS( FES_NAMESPACE, u"fes:Filter"_s ) :
974 doc.createElementNS( OGC_NAMESPACE, u"ogc:Filter"_s );
975
976 if ( GMLUsed )
977 {
978 QDomAttr attr = doc.createAttribute( u"xmlns:gml"_s );
979 if ( gmlVersion == GML_3_2_1 )
980 attr.setValue( GML32_NAMESPACE );
981 else
982 attr.setValue( GML_NAMESPACE );
983 filterElem.setAttributeNode( attr );
984 }
985 return filterElem;
986}
987
988
989bool QgsOgcUtils::readGMLCoordinates( QgsPolyline &coords, const QDomElement &elem )
990{
991 QString coordSeparator = u","_s;
992 QString tupleSeparator = u" "_s;
993 //"decimal" has to be "."
994
995 coords.clear();
996
997 if ( elem.hasAttribute( u"cs"_s ) )
998 {
999 coordSeparator = elem.attribute( u"cs"_s );
1000 }
1001 if ( elem.hasAttribute( u"ts"_s ) )
1002 {
1003 tupleSeparator = elem.attribute( u"ts"_s );
1004 }
1005
1006 const QStringList tupels = elem.text().split( tupleSeparator, Qt::SkipEmptyParts );
1007 QStringList tuple_coords;
1008 double x, y, z;
1009 bool conversionSuccess;
1010
1011 QStringList::const_iterator it;
1012 for ( it = tupels.constBegin(); it != tupels.constEnd(); ++it )
1013 {
1014 tuple_coords = ( *it ).split( coordSeparator, Qt::SkipEmptyParts );
1015 if ( tuple_coords.size() < 2 )
1016 {
1017 continue;
1018 }
1019 x = tuple_coords.at( 0 ).toDouble( &conversionSuccess );
1020 if ( !conversionSuccess )
1021 {
1022 return true;
1023 }
1024 y = tuple_coords.at( 1 ).toDouble( &conversionSuccess );
1025 if ( !conversionSuccess )
1026 {
1027 return true;
1028 }
1029 if ( tuple_coords.size() > 2 )
1030 {
1031 z = tuple_coords.at( 2 ).toDouble( &conversionSuccess );
1032 if ( !conversionSuccess )
1033 {
1034 return true;
1035 }
1036 }
1037 else
1038 {
1039 z = std::numeric_limits<double>::quiet_NaN();
1040 }
1041 coords.append( QgsPoint( x, y, z ) );
1042 }
1043 return false;
1044}
1045
1047{
1048 QgsRectangle rect;
1049
1050 const QDomElement boxElem = boxNode.toElement();
1051 if ( boxElem.tagName() != "Box"_L1 )
1052 return rect;
1053
1054 const QDomElement bElem = boxElem.firstChild().toElement();
1055 QString coordSeparator = u","_s;
1056 QString tupleSeparator = u" "_s;
1057 if ( bElem.hasAttribute( u"cs"_s ) )
1058 {
1059 coordSeparator = bElem.attribute( u"cs"_s );
1060 }
1061 if ( bElem.hasAttribute( u"ts"_s ) )
1062 {
1063 tupleSeparator = bElem.attribute( u"ts"_s );
1064 }
1065
1066 const QString bString = bElem.text();
1067 bool ok1, ok2, ok3, ok4;
1068 const double xmin = bString.section( tupleSeparator, 0, 0 ).section( coordSeparator, 0, 0 ).toDouble( &ok1 );
1069 const double ymin = bString.section( tupleSeparator, 0, 0 ).section( coordSeparator, 1, 1 ).toDouble( &ok2 );
1070 const double xmax = bString.section( tupleSeparator, 1, 1 ).section( coordSeparator, 0, 0 ).toDouble( &ok3 );
1071 const double ymax = bString.section( tupleSeparator, 1, 1 ).section( coordSeparator, 1, 1 ).toDouble( &ok4 );
1072
1073 if ( ok1 && ok2 && ok3 && ok4 )
1074 {
1075 rect = QgsRectangle( xmin, ymin, xmax, ymax );
1076 rect.normalize();
1077 }
1078
1079 return rect;
1080}
1081
1082bool QgsOgcUtils::readGMLPositions( QgsPolyline &coords, const QDomElement &elem )
1083{
1084 coords.clear();
1085
1086 const QStringList pos = elem.text().split( ' ', Qt::SkipEmptyParts );
1087 double x, y, z;
1088 bool conversionSuccess;
1089 const int posSize = pos.size();
1090
1091 int srsDimension = 2;
1092 if ( elem.hasAttribute( u"srsDimension"_s ) )
1093 {
1094 srsDimension = elem.attribute( u"srsDimension"_s ).toInt( &conversionSuccess );
1095 if ( !conversionSuccess )
1096 {
1097 srsDimension = 2;
1098 }
1099 }
1100 else if ( elem.hasAttribute( u"dimension"_s ) )
1101 {
1102 srsDimension = elem.attribute( u"dimension"_s ).toInt( &conversionSuccess );
1103 if ( !conversionSuccess )
1104 {
1105 srsDimension = 2;
1106 }
1107 }
1108
1109 for ( int i = 0; i < posSize / srsDimension; i++ )
1110 {
1111 x = pos.at( i * srsDimension ).toDouble( &conversionSuccess );
1112 if ( !conversionSuccess )
1113 {
1114 return true;
1115 }
1116 y = pos.at( i * srsDimension + 1 ).toDouble( &conversionSuccess );
1117 if ( !conversionSuccess )
1118 {
1119 return true;
1120 }
1121 if ( srsDimension > 2 )
1122 {
1123 z = pos.at( i * srsDimension + 2 ).toDouble( &conversionSuccess );
1124 if ( !conversionSuccess )
1125 {
1126 return true;
1127 }
1128 }
1129 else
1130 {
1131 z = std::numeric_limits<double>::quiet_NaN();
1132 }
1133 coords.append( QgsPoint( x, y, z ) );
1134 }
1135 return false;
1136}
1137
1138
1140{
1141 QgsRectangle rect;
1142
1143 const QDomElement envelopeElem = envelopeNode.toElement();
1144 if ( envelopeElem.tagName() != "Envelope"_L1 )
1145 return rect;
1146
1147 const QDomNodeList lowerCornerList = envelopeElem.elementsByTagNameNS( GML_NAMESPACE, u"lowerCorner"_s );
1148 if ( lowerCornerList.size() < 1 )
1149 return rect;
1150
1151 const QDomNodeList upperCornerList = envelopeElem.elementsByTagNameNS( GML_NAMESPACE, u"upperCorner"_s );
1152 if ( upperCornerList.size() < 1 )
1153 return rect;
1154
1155 bool conversionSuccess;
1156 int srsDimension = 2;
1157
1158 QDomElement elem = lowerCornerList.at( 0 ).toElement();
1159 if ( elem.hasAttribute( u"srsDimension"_s ) )
1160 {
1161 srsDimension = elem.attribute( u"srsDimension"_s ).toInt( &conversionSuccess );
1162 if ( !conversionSuccess )
1163 {
1164 srsDimension = 2;
1165 }
1166 }
1167 else if ( elem.hasAttribute( u"dimension"_s ) )
1168 {
1169 srsDimension = elem.attribute( u"dimension"_s ).toInt( &conversionSuccess );
1170 if ( !conversionSuccess )
1171 {
1172 srsDimension = 2;
1173 }
1174 }
1175 QString bString = elem.text();
1176
1177 const double xmin = bString.section( ' ', 0, 0 ).toDouble( &conversionSuccess );
1178 if ( !conversionSuccess )
1179 return rect;
1180 const double ymin = bString.section( ' ', 1, 1 ).toDouble( &conversionSuccess );
1181 if ( !conversionSuccess )
1182 return rect;
1183
1184 elem = upperCornerList.at( 0 ).toElement();
1185 if ( elem.hasAttribute( u"srsDimension"_s ) )
1186 {
1187 srsDimension = elem.attribute( u"srsDimension"_s ).toInt( &conversionSuccess );
1188 if ( !conversionSuccess )
1189 {
1190 srsDimension = 2;
1191 }
1192 }
1193 else if ( elem.hasAttribute( u"dimension"_s ) )
1194 {
1195 srsDimension = elem.attribute( u"dimension"_s ).toInt( &conversionSuccess );
1196 if ( !conversionSuccess )
1197 {
1198 srsDimension = 2;
1199 }
1200 }
1201
1202 Q_UNUSED( srsDimension )
1203
1204 bString = elem.text();
1205 const double xmax = bString.section( ' ', 0, 0 ).toDouble( &conversionSuccess );
1206 if ( !conversionSuccess )
1207 return rect;
1208 const double ymax = bString.section( ' ', 1, 1 ).toDouble( &conversionSuccess );
1209 if ( !conversionSuccess )
1210 return rect;
1211
1212 rect = QgsRectangle( xmin, ymin, xmax, ymax );
1213 rect.normalize();
1214
1215 return rect;
1216}
1217
1218QDomElement QgsOgcUtils::rectangleToGMLBox( const QgsRectangle *box, QDomDocument &doc, int precision )
1219{
1220 return rectangleToGMLBox( box, doc, QString(), false, precision );
1221}
1222
1223QDomElement QgsOgcUtils::rectangleToGMLBox( const QgsRectangle *box, QDomDocument &doc,
1224 const QString &srsName,
1225 bool invertAxisOrientation,
1226 int precision )
1227{
1228 if ( !box )
1229 {
1230 return QDomElement();
1231 }
1232
1233 QDomElement boxElem = doc.createElement( u"gml:Box"_s );
1234 if ( !srsName.isEmpty() )
1235 {
1236 boxElem.setAttribute( u"srsName"_s, srsName );
1237 }
1238 QDomElement coordElem = doc.createElement( u"gml:coordinates"_s );
1239 coordElem.setAttribute( u"cs"_s, u","_s );
1240 coordElem.setAttribute( u"ts"_s, u" "_s );
1241
1242 QString coordString;
1243 coordString += qgsDoubleToString( invertAxisOrientation ? box->yMinimum() : box->xMinimum(), precision );
1244 coordString += ',';
1245 coordString += qgsDoubleToString( invertAxisOrientation ? box->xMinimum() : box->yMinimum(), precision );
1246 coordString += ' ';
1247 coordString += qgsDoubleToString( invertAxisOrientation ? box->yMaximum() : box->xMaximum(), precision );
1248 coordString += ',';
1249 coordString += qgsDoubleToString( invertAxisOrientation ? box->xMaximum() : box->yMaximum(), precision );
1250
1251 const QDomText coordText = doc.createTextNode( coordString );
1252 coordElem.appendChild( coordText );
1253 boxElem.appendChild( coordElem );
1254
1255 return boxElem;
1256}
1257
1258QDomElement QgsOgcUtils::rectangleToGMLEnvelope( const QgsRectangle *env, QDomDocument &doc, int precision )
1259{
1260 return rectangleToGMLEnvelope( env, doc, QString(), false, precision );
1261}
1262
1263QDomElement QgsOgcUtils::rectangleToGMLEnvelope( const QgsRectangle *env, QDomDocument &doc,
1264 const QString &srsName,
1265 bool invertAxisOrientation,
1266 int precision )
1267{
1268 if ( !env )
1269 {
1270 return QDomElement();
1271 }
1272
1273 QDomElement envElem = doc.createElement( u"gml:Envelope"_s );
1274 if ( !srsName.isEmpty() )
1275 {
1276 envElem.setAttribute( u"srsName"_s, srsName );
1277 }
1278 QString posList;
1279
1280 QDomElement lowerCornerElem = doc.createElement( u"gml:lowerCorner"_s );
1281 posList = qgsDoubleToString( invertAxisOrientation ? env->yMinimum() : env->xMinimum(), precision );
1282 posList += ' ';
1283 posList += qgsDoubleToString( invertAxisOrientation ? env->xMinimum() : env->yMinimum(), precision );
1284 const QDomText lowerCornerText = doc.createTextNode( posList );
1285 lowerCornerElem.appendChild( lowerCornerText );
1286 envElem.appendChild( lowerCornerElem );
1287
1288 QDomElement upperCornerElem = doc.createElement( u"gml:upperCorner"_s );
1289 posList = qgsDoubleToString( invertAxisOrientation ? env->yMaximum() : env->xMaximum(), precision );
1290 posList += ' ';
1291 posList += qgsDoubleToString( invertAxisOrientation ? env->xMaximum() : env->yMaximum(), precision );
1292 const QDomText upperCornerText = doc.createTextNode( posList );
1293 upperCornerElem.appendChild( upperCornerText );
1294 envElem.appendChild( upperCornerElem );
1295
1296 return envElem;
1297}
1298
1299QDomElement QgsOgcUtils::geometryToGML( const QgsGeometry &geometry, QDomDocument &doc, const QString &format, int precision )
1300{
1301 return geometryToGML( geometry, doc, ( format == "GML2"_L1 ) ? GML_2_1_2 : GML_3_2_1, QString(), false, QString(), precision );
1302}
1303
1304QDomElement QgsOgcUtils::geometryToGML( const QgsGeometry &geometry,
1305 QDomDocument &doc,
1306 GMLVersion gmlVersion,
1307 const QString &srsName,
1308 bool invertAxisOrientation,
1309 const QString &gmlIdBase,
1310 int precision )
1311{
1312 if ( geometry.isNull() )
1313 return QDomElement();
1314
1315 // coordinate separator
1316 QString cs = u","_s;
1317 // tuple separator
1318 const QString ts = u" "_s;
1319 // coord element tagname
1320 QDomElement baseCoordElem;
1321
1322 bool hasZValue = false;
1323
1324 const QByteArray wkb( geometry.asWkb() );
1325 QgsConstWkbPtr wkbPtr( wkb );
1326 try
1327 {
1328 wkbPtr.readHeader();
1329 }
1330 catch ( const QgsWkbException &e )
1331 {
1332 Q_UNUSED( e )
1333 // WKB exception while reading header
1334 return QDomElement();
1335 }
1336
1337 if ( gmlVersion != GML_2_1_2 )
1338 {
1339 switch ( geometry.wkbType() )
1340 {
1347 baseCoordElem = doc.createElement( u"gml:pos"_s );
1348 break;
1349 default:
1350 baseCoordElem = doc.createElement( u"gml:posList"_s );
1351 break;
1352 }
1353 cs = ' ';
1354 }
1355 else
1356 {
1357 baseCoordElem = doc.createElement( u"gml:coordinates"_s );
1358 baseCoordElem.setAttribute( u"cs"_s, cs );
1359 baseCoordElem.setAttribute( u"ts"_s, ts );
1360 }
1361
1362 try
1363 {
1364 switch ( geometry.wkbType() )
1365 {
1368 hasZValue = true;
1369 //intentional fall-through
1370 [[fallthrough]];
1372 {
1373 QDomElement pointElem = doc.createElement( u"gml:Point"_s );
1374 if ( gmlVersion == GML_3_2_1 && !gmlIdBase.isEmpty() )
1375 pointElem.setAttribute( u"gml:id"_s, gmlIdBase );
1376 if ( !srsName.isEmpty() )
1377 pointElem.setAttribute( u"srsName"_s, srsName );
1378 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1379
1380 double x, y;
1381
1382 if ( invertAxisOrientation )
1383 wkbPtr >> y >> x;
1384 else
1385 wkbPtr >> x >> y;
1386
1387 QString coordString = qgsDoubleToString( x, precision ) + cs + qgsDoubleToString( y, precision );
1388
1389 // Add Z
1390 if ( hasZValue )
1391 {
1392 double z = 0;
1393 wkbPtr >> z;
1394 coordString += cs + qgsDoubleToString( z, precision );
1395 }
1396 const QDomText coordText = doc.createTextNode( coordString );
1397
1398 coordElem.appendChild( coordText );
1399 if ( gmlVersion != GML_2_1_2 )
1400 coordElem.setAttribute( u"srsDimension"_s, hasZValue ? u"3"_s : u"2"_s );
1401 pointElem.appendChild( coordElem );
1402 return pointElem;
1403 }
1406 hasZValue = true;
1407 //intentional fall-through
1408 [[fallthrough]];
1410 {
1411 QDomElement multiPointElem = doc.createElement( u"gml:MultiPoint"_s );
1412 if ( gmlVersion == GML_3_2_1 && !gmlIdBase.isEmpty() )
1413 multiPointElem.setAttribute( u"gml:id"_s, gmlIdBase );
1414 if ( !srsName.isEmpty() )
1415 multiPointElem.setAttribute( u"srsName"_s, srsName );
1416
1417 int nPoints;
1418 wkbPtr >> nPoints;
1419
1420 for ( int idx = 0; idx < nPoints; ++idx )
1421 {
1422 QDomElement pointMemberElem = doc.createElement( u"gml:pointMember"_s );
1423 QDomElement pointElem = doc.createElement( u"gml:Point"_s );
1424 if ( gmlVersion == GML_3_2_1 && !gmlIdBase.isEmpty() )
1425 pointElem.setAttribute( u"gml:id"_s, gmlIdBase + u".%1"_s.arg( idx + 1 ) );
1426 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1427
1428 wkbPtr.readHeader();
1429
1430 double x = 0;
1431 double y = 0;
1432 if ( invertAxisOrientation )
1433 wkbPtr >> y >> x;
1434 else
1435 wkbPtr >> x >> y;
1436
1437 QString coordString = qgsDoubleToString( x, precision ) + cs + qgsDoubleToString( y, precision );
1438 // Add Z
1439 if ( hasZValue )
1440 {
1441 double z = 0;
1442 wkbPtr >> z;
1443 coordString += cs + qgsDoubleToString( z, precision );
1444 }
1445
1446 const QDomText coordText = doc.createTextNode( coordString );
1447
1448 coordElem.appendChild( coordText );
1449 if ( gmlVersion != GML_2_1_2 )
1450 coordElem.setAttribute( u"srsDimension"_s, hasZValue ? u"3"_s : u"2"_s );
1451 pointElem.appendChild( coordElem );
1452
1453
1454 pointMemberElem.appendChild( pointElem );
1455 multiPointElem.appendChild( pointMemberElem );
1456 }
1457 return multiPointElem;
1458 }
1461 hasZValue = true;
1462 //intentional fall-through
1463 [[fallthrough]];
1465 {
1466 QDomElement lineStringElem = doc.createElement( u"gml:LineString"_s );
1467 if ( gmlVersion == GML_3_2_1 && !gmlIdBase.isEmpty() )
1468 lineStringElem.setAttribute( u"gml:id"_s, gmlIdBase );
1469 if ( !srsName.isEmpty() )
1470 lineStringElem.setAttribute( u"srsName"_s, srsName );
1471 // get number of points in the line
1472
1473 int nPoints;
1474 wkbPtr >> nPoints;
1475
1476 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1477 QString coordString;
1478 for ( int idx = 0; idx < nPoints; ++idx )
1479 {
1480 if ( idx != 0 )
1481 {
1482 coordString += ts;
1483 }
1484
1485 double x = 0;
1486 double y = 0;
1487 if ( invertAxisOrientation )
1488 wkbPtr >> y >> x;
1489 else
1490 wkbPtr >> x >> y;
1491 coordString += qgsDoubleToString( x, precision ) + cs + qgsDoubleToString( y, precision );
1492
1493 if ( hasZValue )
1494 {
1495 double z = 0;
1496 wkbPtr >> z;
1497 coordString += cs + qgsDoubleToString( z, precision );
1498 }
1499
1500 }
1501 const QDomText coordText = doc.createTextNode( coordString );
1502 coordElem.appendChild( coordText );
1503 if ( gmlVersion != GML_2_1_2 )
1504 coordElem.setAttribute( u"srsDimension"_s, hasZValue ? u"3"_s : u"2"_s );
1505 lineStringElem.appendChild( coordElem );
1506 return lineStringElem;
1507 }
1510 hasZValue = true;
1511 //intentional fall-through
1512 [[fallthrough]];
1514 {
1515 QDomElement multiLineStringElem = doc.createElement( u"gml:MultiLineString"_s );
1516 if ( gmlVersion == GML_3_2_1 && !gmlIdBase.isEmpty() )
1517 multiLineStringElem.setAttribute( u"gml:id"_s, gmlIdBase );
1518 if ( !srsName.isEmpty() )
1519 multiLineStringElem.setAttribute( u"srsName"_s, srsName );
1520
1521 int nLines;
1522 wkbPtr >> nLines;
1523
1524 for ( int jdx = 0; jdx < nLines; jdx++ )
1525 {
1526 QDomElement lineStringMemberElem = doc.createElement( u"gml:lineStringMember"_s );
1527 QDomElement lineStringElem = doc.createElement( u"gml:LineString"_s );
1528 if ( gmlVersion == GML_3_2_1 && !gmlIdBase.isEmpty() )
1529 lineStringElem.setAttribute( u"gml:id"_s, gmlIdBase + u".%1"_s.arg( jdx + 1 ) );
1530
1531 wkbPtr.readHeader();
1532
1533 int nPoints;
1534 wkbPtr >> nPoints;
1535
1536 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1537 QString coordString;
1538 for ( int idx = 0; idx < nPoints; idx++ )
1539 {
1540 if ( idx != 0 )
1541 {
1542 coordString += ts;
1543 }
1544
1545 double x = 0;
1546 double y = 0;
1547 if ( invertAxisOrientation )
1548 wkbPtr >> y >> x;
1549 else
1550 wkbPtr >> x >> y;
1551
1552 coordString += qgsDoubleToString( x, precision ) + cs + qgsDoubleToString( y, precision );
1553
1554 if ( hasZValue )
1555 {
1556 double z = 0;
1557 wkbPtr >> z;
1558 coordString += cs + qgsDoubleToString( z, precision );
1559 }
1560
1561 }
1562 const QDomText coordText = doc.createTextNode( coordString );
1563 coordElem.appendChild( coordText );
1564 if ( gmlVersion != GML_2_1_2 )
1565 coordElem.setAttribute( u"srsDimension"_s, hasZValue ? u"3"_s : u"2"_s );
1566 lineStringElem.appendChild( coordElem );
1567 lineStringMemberElem.appendChild( lineStringElem );
1568 multiLineStringElem.appendChild( lineStringMemberElem );
1569 }
1570 return multiLineStringElem;
1571 }
1574 hasZValue = true;
1575 //intentional fall-through
1576 [[fallthrough]];
1578 {
1579 QDomElement polygonElem = doc.createElement( u"gml:Polygon"_s );
1580 if ( gmlVersion == GML_3_2_1 && !gmlIdBase.isEmpty() )
1581 polygonElem.setAttribute( u"gml:id"_s, gmlIdBase );
1582 if ( !srsName.isEmpty() )
1583 polygonElem.setAttribute( u"srsName"_s, srsName );
1584
1585 // get number of rings in the polygon
1586 int numRings;
1587 wkbPtr >> numRings;
1588
1589 if ( numRings == 0 ) // sanity check for zero rings in polygon
1590 return QDomElement();
1591
1592 for ( int idx = 0; idx < numRings; idx++ )
1593 {
1594 QString boundaryName = ( gmlVersion == GML_2_1_2 ) ? "gml:outerBoundaryIs" : "gml:exterior";
1595 if ( idx != 0 )
1596 {
1597 boundaryName = ( gmlVersion == GML_2_1_2 ) ? "gml:innerBoundaryIs" : "gml:interior";
1598 }
1599 QDomElement boundaryElem = doc.createElement( boundaryName );
1600 QDomElement ringElem = doc.createElement( u"gml:LinearRing"_s );
1601 // get number of points in the ring
1602 int nPoints = 0;
1603 wkbPtr >> nPoints;
1604
1605 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1606 QString coordString;
1607 for ( int jdx = 0; jdx < nPoints; jdx++ )
1608 {
1609 if ( jdx != 0 )
1610 {
1611 coordString += ts;
1612 }
1613
1614 double x = 0;
1615 double y = 0;
1616 if ( invertAxisOrientation )
1617 wkbPtr >> y >> x;
1618 else
1619 wkbPtr >> x >> y;
1620
1621 coordString += qgsDoubleToString( x, precision ) + cs + qgsDoubleToString( y, precision );
1622
1623 if ( hasZValue )
1624 {
1625 double z = 0;
1626 wkbPtr >> z;
1627 coordString += cs + qgsDoubleToString( z, precision );
1628 }
1629 }
1630 const QDomText coordText = doc.createTextNode( coordString );
1631 coordElem.appendChild( coordText );
1632 if ( gmlVersion != GML_2_1_2 )
1633 coordElem.setAttribute( u"srsDimension"_s, hasZValue ? u"3"_s : u"2"_s );
1634 ringElem.appendChild( coordElem );
1635 boundaryElem.appendChild( ringElem );
1636 polygonElem.appendChild( boundaryElem );
1637 }
1638
1639 return polygonElem;
1640 }
1643 hasZValue = true;
1644 //intentional fall-through
1645 [[fallthrough]];
1647 {
1648 QDomElement multiPolygonElem = doc.createElement( u"gml:MultiPolygon"_s );
1649 if ( gmlVersion == GML_3_2_1 && !gmlIdBase.isEmpty() )
1650 multiPolygonElem.setAttribute( u"gml:id"_s, gmlIdBase );
1651 if ( !srsName.isEmpty() )
1652 multiPolygonElem.setAttribute( u"srsName"_s, srsName );
1653
1654 int numPolygons;
1655 wkbPtr >> numPolygons;
1656
1657 for ( int kdx = 0; kdx < numPolygons; kdx++ )
1658 {
1659 QDomElement polygonMemberElem = doc.createElement( u"gml:polygonMember"_s );
1660 QDomElement polygonElem = doc.createElement( u"gml:Polygon"_s );
1661 if ( gmlVersion == GML_3_2_1 && !gmlIdBase.isEmpty() )
1662 polygonElem.setAttribute( u"gml:id"_s, gmlIdBase + u".%1"_s.arg( kdx + 1 ) );
1663
1664 wkbPtr.readHeader();
1665
1666 int numRings;
1667 wkbPtr >> numRings;
1668
1669 for ( int idx = 0; idx < numRings; idx++ )
1670 {
1671 QString boundaryName = ( gmlVersion == GML_2_1_2 ) ? "gml:outerBoundaryIs" : "gml:exterior";
1672 if ( idx != 0 )
1673 {
1674 boundaryName = ( gmlVersion == GML_2_1_2 ) ? "gml:innerBoundaryIs" : "gml:interior";
1675 }
1676 QDomElement boundaryElem = doc.createElement( boundaryName );
1677 QDomElement ringElem = doc.createElement( u"gml:LinearRing"_s );
1678
1679 int nPoints;
1680 wkbPtr >> nPoints;
1681
1682 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1683 QString coordString;
1684 for ( int jdx = 0; jdx < nPoints; jdx++ )
1685 {
1686 if ( jdx != 0 )
1687 {
1688 coordString += ts;
1689 }
1690
1691 double x = 0;
1692 double y = 0;
1693 if ( invertAxisOrientation )
1694 wkbPtr >> y >> x;
1695 else
1696 wkbPtr >> x >> y;
1697
1698 coordString += qgsDoubleToString( x, precision ) + cs + qgsDoubleToString( y, precision );
1699
1700 if ( hasZValue )
1701 {
1702 double z = 0;
1703 wkbPtr >> z;
1704 coordString += cs + qgsDoubleToString( z, precision );
1705 }
1706
1707 }
1708 const QDomText coordText = doc.createTextNode( coordString );
1709 coordElem.appendChild( coordText );
1710 if ( gmlVersion != GML_2_1_2 )
1711 coordElem.setAttribute( u"srsDimension"_s, hasZValue ? u"3"_s : u"2"_s );
1712 ringElem.appendChild( coordElem );
1713 boundaryElem.appendChild( ringElem );
1714 polygonElem.appendChild( boundaryElem );
1715 polygonMemberElem.appendChild( polygonElem );
1716 multiPolygonElem.appendChild( polygonMemberElem );
1717 }
1718 }
1719 return multiPolygonElem;
1720 }
1721 default:
1722 return QDomElement();
1723 }
1724 }
1725 catch ( const QgsWkbException &e )
1726 {
1727 Q_UNUSED( e )
1728 return QDomElement();
1729 }
1730}
1731
1732QDomElement QgsOgcUtils::geometryToGML( const QgsGeometry &geometry, QDomDocument &doc, int precision )
1733{
1734 return geometryToGML( geometry, doc, u"GML2"_s, precision );
1735}
1736
1737QDomElement QgsOgcUtils::createGMLCoordinates( const QgsPolylineXY &points, QDomDocument &doc )
1738{
1739 QDomElement coordElem = doc.createElement( u"gml:coordinates"_s );
1740 coordElem.setAttribute( u"cs"_s, u","_s );
1741 coordElem.setAttribute( u"ts"_s, u" "_s );
1742
1743 QString coordString;
1744 QVector<QgsPointXY>::const_iterator pointIt = points.constBegin();
1745 for ( ; pointIt != points.constEnd(); ++pointIt )
1746 {
1747 if ( pointIt != points.constBegin() )
1748 {
1749 coordString += ' ';
1750 }
1751 coordString += qgsDoubleToString( pointIt->x() );
1752 coordString += ',';
1753 coordString += qgsDoubleToString( pointIt->y() );
1754 }
1755
1756 const QDomText coordText = doc.createTextNode( coordString );
1757 coordElem.appendChild( coordText );
1758 return coordElem;
1759}
1760
1761QDomElement QgsOgcUtils::createGMLPositions( const QgsPolylineXY &points, QDomDocument &doc )
1762{
1763 QDomElement posElem = doc.createElement( u"gml:pos"_s );
1764 if ( points.size() > 1 )
1765 posElem = doc.createElement( u"gml:posList"_s );
1766 posElem.setAttribute( u"srsDimension"_s, u"2"_s );
1767
1768 QString coordString;
1769 QVector<QgsPointXY>::const_iterator pointIt = points.constBegin();
1770 for ( ; pointIt != points.constEnd(); ++pointIt )
1771 {
1772 if ( pointIt != points.constBegin() )
1773 {
1774 coordString += ' ';
1775 }
1776 coordString += qgsDoubleToString( pointIt->x() );
1777 coordString += ' ';
1778 coordString += qgsDoubleToString( pointIt->y() );
1779 }
1780
1781 const QDomText coordText = doc.createTextNode( coordString );
1782 posElem.appendChild( coordText );
1783 return posElem;
1784}
1785
1786// -----------------------------------------
1787
1788QColor QgsOgcUtils::colorFromOgcFill( const QDomElement &fillElement )
1789{
1790 if ( fillElement.isNull() || !fillElement.hasChildNodes() )
1791 {
1792 return QColor();
1793 }
1794
1795 QString cssName;
1796 QString elemText;
1797 QColor color;
1798 QDomElement cssElem = fillElement.firstChildElement( u"CssParameter"_s );
1799 while ( !cssElem.isNull() )
1800 {
1801 cssName = cssElem.attribute( u"name"_s, u"not_found"_s );
1802 if ( cssName != "not_found"_L1 )
1803 {
1804 elemText = cssElem.text();
1805 if ( cssName == "fill"_L1 )
1806 {
1807 color.setNamedColor( elemText );
1808 }
1809 else if ( cssName == "fill-opacity"_L1 )
1810 {
1811 bool ok;
1812 const double opacity = elemText.toDouble( &ok );
1813 if ( ok )
1814 {
1815 color.setAlphaF( opacity );
1816 }
1817 }
1818 }
1819
1820 cssElem = cssElem.nextSiblingElement( u"CssParameter"_s );
1821 }
1822
1823 return color;
1824}
1825
1826
1828{
1829 return expressionFromOgcFilter( element, QgsOgcUtils::FILTER_OGC_1_0, layer );
1830}
1831
1832QgsExpression *QgsOgcUtils::expressionFromOgcFilter( const QDomElement &element, const FilterVersion version, QgsVectorLayer *layer )
1833{
1834 if ( element.isNull() || !element.hasChildNodes() )
1835 return nullptr;
1836
1837 QgsExpression *expr = new QgsExpression();
1838
1839 // check if it is a single string value not having DOM elements
1840 // that express OGC operators
1841 if ( element.firstChild().nodeType() == QDomNode::TextNode )
1842 {
1843 expr->setExpression( element.firstChild().nodeValue() );
1844 return expr;
1845 }
1846
1847 QgsOgcUtilsExpressionFromFilter utils( version, layer );
1848
1849 // then check OGC DOM elements that contain OGC tags specifying
1850 // OGC operators.
1851 QDomElement childElem = element.firstChildElement();
1852 while ( !childElem.isNull() )
1853 {
1854 QgsExpressionNode *node = utils.nodeFromOgcFilter( childElem );
1855
1856 if ( !node )
1857 {
1858 // invalid expression, parser error
1859 expr->d->mParserErrorString = utils.errorMessage();
1860 return expr;
1861 }
1862
1863 // use the concat binary operator to append to the root node
1864 if ( !expr->d->mRootNode )
1865 {
1866 expr->d->mRootNode.reset( node );
1867 }
1868 else
1869 {
1870 expr->d->mRootNode = std::make_unique<QgsExpressionNodeBinaryOperator>( QgsExpressionNodeBinaryOperator::boConcat, expr->d->mRootNode.release(), node );
1871 }
1872
1873 childElem = childElem.nextSiblingElement();
1874 }
1875
1876 // update expression string
1877 expr->d->mExp = expr->dump();
1878
1879 return expr;
1880}
1881
1882typedef QMap<QString, int> IntMap;
1883Q_GLOBAL_STATIC_WITH_ARGS( IntMap, BINARY_OPERATORS_TAG_NAMES_MAP, (
1884{
1885 // logical
1888 // comparison
1889 { "PropertyIsEqualTo"_L1, QgsExpressionNodeBinaryOperator::boEQ },
1890 { "PropertyIsNotEqualTo"_L1, QgsExpressionNodeBinaryOperator::boNE },
1891 { "PropertyIsLessThanOrEqualTo"_L1, QgsExpressionNodeBinaryOperator::boLE },
1892 { "PropertyIsGreaterThanOrEqualTo"_L1, QgsExpressionNodeBinaryOperator::boGE },
1893 { "PropertyIsLessThan"_L1, QgsExpressionNodeBinaryOperator::boLT },
1894 { "PropertyIsGreaterThan"_L1, QgsExpressionNodeBinaryOperator::boGT },
1895 { "PropertyIsLike"_L1, QgsExpressionNodeBinaryOperator::boLike },
1896 // arithmetic
1901} ) )
1902
1903static int binaryOperatorFromTagName( const QString &tagName )
1904{
1905
1906 return BINARY_OPERATORS_TAG_NAMES_MAP()->value( tagName, -1 );
1907}
1908
1909static QString binaryOperatorToTagName( QgsExpressionNodeBinaryOperator::BinaryOperator op )
1910{
1912 {
1913 return u"PropertyIsLike"_s;
1914 }
1915 return BINARY_OPERATORS_TAG_NAMES_MAP()->key( op, QString() );
1916}
1917
1918static bool isBinaryOperator( const QString &tagName )
1919{
1920 return binaryOperatorFromTagName( tagName ) >= 0;
1921}
1922
1923
1924static bool isSpatialOperator( const QString &tagName )
1925{
1926 static QStringList spatialOps;
1927 if ( spatialOps.isEmpty() )
1928 {
1929 spatialOps << u"BBOX"_s << u"Intersects"_s << u"Contains"_s << u"Crosses"_s << u"Equals"_s
1930 << u"Disjoint"_s << u"Overlaps"_s << u"Touches"_s << u"Within"_s;
1931 }
1932
1933 return spatialOps.contains( tagName );
1934}
1935
1936QgsExpressionNode *QgsOgcUtils::nodeFromOgcFilter( QDomElement &element, QString &errorMessage, QgsVectorLayer *layer )
1937{
1938 QgsOgcUtilsExpressionFromFilter utils( QgsOgcUtils::FILTER_OGC_1_0, layer );
1939 QgsExpressionNode *node = utils.nodeFromOgcFilter( element );
1940 errorMessage = utils.errorMessage();
1941 return node;
1942}
1943
1944QgsExpressionNodeBinaryOperator *QgsOgcUtils::nodeBinaryOperatorFromOgcFilter( QDomElement &element, QString &errorMessage, QgsVectorLayer *layer )
1945{
1946 QgsOgcUtilsExpressionFromFilter utils( QgsOgcUtils::FILTER_OGC_1_0, layer );
1947 QgsExpressionNodeBinaryOperator *node = utils.nodeBinaryOperatorFromOgcFilter( element );
1948 errorMessage = utils.errorMessage();
1949 return node;
1950}
1951
1952QgsExpressionNodeFunction *QgsOgcUtils::nodeSpatialOperatorFromOgcFilter( QDomElement &element, QString &errorMessage )
1953{
1954 QgsOgcUtilsExpressionFromFilter utils( QgsOgcUtils::FILTER_OGC_1_0 );
1955 QgsExpressionNodeFunction *node = utils.nodeSpatialOperatorFromOgcFilter( element );
1956 errorMessage = utils.errorMessage();
1957 return node;
1958}
1959
1960QgsExpressionNodeUnaryOperator *QgsOgcUtils::nodeNotFromOgcFilter( QDomElement &element, QString &errorMessage )
1961{
1962 QgsOgcUtilsExpressionFromFilter utils( QgsOgcUtils::FILTER_OGC_1_0 );
1963 QgsExpressionNodeUnaryOperator *node = utils.nodeNotFromOgcFilter( element );
1964 errorMessage = utils.errorMessage();
1965 return node;
1966}
1967
1968QgsExpressionNodeFunction *QgsOgcUtils::nodeFunctionFromOgcFilter( QDomElement &element, QString &errorMessage )
1969{
1970 QgsOgcUtilsExpressionFromFilter utils( QgsOgcUtils::FILTER_OGC_1_0 );
1971 QgsExpressionNodeFunction *node = utils.nodeFunctionFromOgcFilter( element );
1972 errorMessage = utils.errorMessage();
1973 return node;
1974}
1975
1976QgsExpressionNode *QgsOgcUtils::nodeLiteralFromOgcFilter( QDomElement &element, QString &errorMessage, QgsVectorLayer *layer )
1977{
1978 QgsOgcUtilsExpressionFromFilter utils( QgsOgcUtils::FILTER_OGC_1_0, layer );
1979 QgsExpressionNode *node = utils.nodeLiteralFromOgcFilter( element );
1980 errorMessage = utils.errorMessage();
1981 return node;
1982}
1983
1984QgsExpressionNodeColumnRef *QgsOgcUtils::nodeColumnRefFromOgcFilter( QDomElement &element, QString &errorMessage )
1985{
1986 QgsOgcUtilsExpressionFromFilter utils( QgsOgcUtils::FILTER_OGC_1_0 );
1987 QgsExpressionNodeColumnRef *node = utils.nodeColumnRefFromOgcFilter( element );
1988 errorMessage = utils.errorMessage();
1989 return node;
1990}
1991
1992QgsExpressionNode *QgsOgcUtils::nodeIsBetweenFromOgcFilter( QDomElement &element, QString &errorMessage )
1993{
1994 QgsOgcUtilsExpressionFromFilter utils( QgsOgcUtils::FILTER_OGC_1_0 );
1995 QgsExpressionNode *node = utils.nodeIsBetweenFromOgcFilter( element );
1996 errorMessage = utils.errorMessage();
1997 return node;
1998}
1999
2000QgsExpressionNodeBinaryOperator *QgsOgcUtils::nodePropertyIsNullFromOgcFilter( QDomElement &element, QString &errorMessage )
2001{
2002 QgsOgcUtilsExpressionFromFilter utils( QgsOgcUtils::FILTER_OGC_1_0 );
2003 QgsExpressionNodeBinaryOperator *node = utils.nodePropertyIsNullFromOgcFilter( element );
2004 errorMessage = utils.errorMessage();
2005 return node;
2006}
2007
2008
2010
2011
2012QDomElement QgsOgcUtils::expressionToOgcFilter( const QgsExpression &exp, QDomDocument &doc, QString *errorMessage )
2013{
2014 return expressionToOgcFilter( exp, doc, GML_2_1_2, FILTER_OGC_1_0, QString(), QString(),
2015 u"geometry"_s, QString(), false, false, errorMessage );
2016}
2017
2018QDomElement QgsOgcUtils::expressionToOgcExpression( const QgsExpression &exp, QDomDocument &doc, QString *errorMessage, bool requiresFilterElement )
2019{
2021 u"geometry"_s, QString(), false, false, errorMessage, requiresFilterElement );
2022}
2023
2024QDomElement QgsOgcUtils::elseFilterExpression( QDomDocument &doc )
2025{
2026 return doc.createElementNS( SE_NAMESPACE, u"se:ElseFilter"_s );
2027}
2028
2029
2031 QDomDocument &doc,
2032 GMLVersion gmlVersion,
2033 FilterVersion filterVersion,
2034 const QString &namespacePrefix,
2035 const QString &namespaceURI,
2036 const QString &geometryName,
2037 const QString &srsName,
2038 bool honourAxisOrientation,
2039 bool invertAxisOrientation,
2040 QString *errorMessage,
2041 const QMap<QString, QString> &fieldNameToXPathMap,
2042 const QMap<QString, QString> &namespacePrefixToUriMap )
2043{
2044 if ( !expression.rootNode() )
2045 return QDomElement();
2046
2047 QgsExpression exp = expression;
2048
2049 QgsExpressionContext context;
2051 QgsOgcUtilsExprToFilter utils( doc, gmlVersion, filterVersion, namespacePrefix, namespaceURI, geometryName, srsName, honourAxisOrientation, invertAxisOrientation, fieldNameToXPathMap, namespacePrefixToUriMap );
2052 const QDomElement exprRootElem = utils.expressionNodeToOgcFilter( exp.rootNode(), &exp, &context );
2053 if ( errorMessage )
2054 *errorMessage = utils.errorMessage();
2055 if ( exprRootElem.isNull() )
2056 return QDomElement();
2057
2058 QDomElement filterElem = filterElement( doc, gmlVersion, filterVersion, utils.GMLNamespaceUsed() );
2059
2060 if ( !namespacePrefix.isEmpty() && !namespaceURI.isEmpty() )
2061 {
2062 QDomAttr attr = doc.createAttribute( u"xmlns:"_s + namespacePrefix );
2063 attr.setValue( namespaceURI );
2064 filterElem.setAttributeNode( attr );
2065 }
2066
2067 filterElem.appendChild( exprRootElem );
2068 return filterElem;
2069}
2070
2072 QDomDocument &doc,
2073 GMLVersion gmlVersion,
2074 FilterVersion filterVersion,
2075 const QString &geometryName,
2076 const QString &srsName,
2077 bool honourAxisOrientation,
2078 bool invertAxisOrientation,
2079 QString *errorMessage,
2080 bool requiresFilterElement,
2081 const QMap<QString, QString> &fieldNameToXPathMap,
2082 const QMap<QString, QString> &namespacePrefixToUriMap )
2083{
2084 QgsExpressionContext context;
2086
2087 QgsExpression exp = expression;
2088
2089 const QgsExpressionNode *node = exp.rootNode();
2090 if ( !node )
2091 return QDomElement();
2092
2093 QgsOgcUtilsExprToFilter utils( doc, gmlVersion, filterVersion, QString(), QString(), geometryName, srsName, honourAxisOrientation, invertAxisOrientation, fieldNameToXPathMap, namespacePrefixToUriMap );
2094 const QDomElement exprRootElem = utils.expressionNodeToOgcFilter( node, &exp, &context );
2095
2096 if ( errorMessage )
2097 {
2098 *errorMessage = utils.errorMessage();
2099 }
2100
2101 if ( !exprRootElem.isNull() )
2102 {
2103 if ( requiresFilterElement )
2104 {
2105 QDomElement filterElem = filterElement( doc, gmlVersion, filterVersion, utils.GMLNamespaceUsed() );
2106
2107 filterElem.appendChild( exprRootElem );
2108 return filterElem;
2109 }
2110 return exprRootElem;
2111 }
2112
2113 return QDomElement();
2114}
2115
2117 QDomDocument &doc,
2118 GMLVersion gmlVersion,
2119 FilterVersion filterVersion,
2120 const QList<LayerProperties> &layerProperties,
2121 bool honourAxisOrientation,
2122 bool invertAxisOrientation,
2123 const QMap< QString, QString> &mapUnprefixedTypenameToPrefixedTypename,
2124 QString *errorMessage,
2125 const QMap<QString, QString> &fieldNameToXPathMap,
2126 const QMap<QString, QString> &namespacePrefixToUriMap )
2127{
2128 if ( !statement.rootNode() )
2129 return QDomElement();
2130
2131 QgsOgcUtilsSQLStatementToFilter utils( doc, gmlVersion, filterVersion,
2132 layerProperties, honourAxisOrientation, invertAxisOrientation,
2133 mapUnprefixedTypenameToPrefixedTypename, fieldNameToXPathMap, namespacePrefixToUriMap );
2134 const QDomElement exprRootElem = utils.toOgcFilter( statement.rootNode() );
2135 if ( errorMessage )
2136 *errorMessage = utils.errorMessage();
2137 if ( exprRootElem.isNull() )
2138 return QDomElement();
2139
2140 QDomElement filterElem = filterElement( doc, gmlVersion, filterVersion, utils.GMLNamespaceUsed() );
2141
2142 QSet<QString> setNamespaceURI;
2143 for ( const LayerProperties &props : layerProperties )
2144 {
2145 if ( !props.mNamespacePrefix.isEmpty() && !props.mNamespaceURI.isEmpty() &&
2146 !setNamespaceURI.contains( props.mNamespaceURI ) )
2147 {
2148 setNamespaceURI.insert( props.mNamespaceURI );
2149 QDomAttr attr = doc.createAttribute( u"xmlns:"_s + props.mNamespacePrefix );
2150 attr.setValue( props.mNamespaceURI );
2151 filterElem.setAttributeNode( attr );
2152 }
2153 }
2154 filterElem.appendChild( exprRootElem );
2155 return filterElem;
2156}
2157
2158//
2159
2160/* static */ Qgis::WkbType QgsOgcUtils::geomTypeFromPropertyType( const QString &gmlGeomType )
2161{
2162 if ( gmlGeomType == "Point"_L1 )
2163 return Qgis::WkbType::Point;
2164 if ( gmlGeomType == "LineString"_L1 || gmlGeomType == "Curve"_L1 )
2166 if ( gmlGeomType == "Polygon"_L1 || gmlGeomType == "Surface"_L1 )
2168 if ( gmlGeomType == "MultiPoint"_L1 )
2170 if ( gmlGeomType == "MultiLineString"_L1 || gmlGeomType == "MultiCurve"_L1 )
2172 if ( gmlGeomType == "MultiPolygon"_L1 || gmlGeomType == "MultiSurface"_L1 )
2175}
2176
2177//
2178
2179
2181{
2182 switch ( node->nodeType() )
2183 {
2185 return expressionUnaryOperatorToOgcFilter( static_cast<const QgsExpressionNodeUnaryOperator *>( node ), expression, context );
2187 return expressionBinaryOperatorToOgcFilter( static_cast<const QgsExpressionNodeBinaryOperator *>( node ), expression, context );
2189 return expressionInOperatorToOgcFilter( static_cast<const QgsExpressionNodeInOperator *>( node ), expression, context );
2191 return expressionFunctionToOgcFilter( static_cast<const QgsExpressionNodeFunction *>( node ), expression, context );
2193 return expressionLiteralToOgcFilter( static_cast<const QgsExpressionNodeLiteral *>( node ), expression, context );
2195 return expressionColumnRefToOgcFilter( static_cast<const QgsExpressionNodeColumnRef *>( node ), expression, context );
2196
2197 default:
2198 mErrorMessage = QObject::tr( "Node type not supported: %1" ).arg( node->nodeType() );
2199 return QDomElement();
2200 }
2201}
2202
2203QDomElement QgsOgcUtilsExprToFilter::expressionUnaryOperatorToOgcFilter( const QgsExpressionNodeUnaryOperator *node, QgsExpression *expression, const QgsExpressionContext *context )
2204{
2205 const QDomElement operandElem = expressionNodeToOgcFilter( node->operand(), expression, context );
2206 if ( !mErrorMessage.isEmpty() )
2207 return QDomElement();
2208
2209 QDomElement uoElem;
2210 switch ( node->op() )
2211 {
2213 uoElem = mDoc.createElement( mFilterPrefix + ":Literal" );
2214 if ( node->operand()->nodeType() == QgsExpressionNode::ntLiteral )
2215 {
2216 // operand expression already created a Literal node:
2217 // take the literal value, prepend - and remove old literal node
2218 uoElem.appendChild( mDoc.createTextNode( "-" + operandElem.text() ) );
2219 mDoc.removeChild( operandElem );
2220 }
2221 else
2222 {
2223 mErrorMessage = QObject::tr( "This use of unary operator not implemented yet" );
2224 return QDomElement();
2225 }
2226 break;
2228 uoElem = mDoc.createElement( mFilterPrefix + ":Not" );
2229 uoElem.appendChild( operandElem );
2230 break;
2231
2232 default:
2233 mErrorMessage = QObject::tr( "Unary operator '%1' not implemented yet" ).arg( node->text() );
2234 return QDomElement();
2235 }
2236
2237 return uoElem;
2238}
2239
2240
2241QDomElement QgsOgcUtilsExprToFilter::expressionBinaryOperatorToOgcFilter( const QgsExpressionNodeBinaryOperator *node, QgsExpression *expression, const QgsExpressionContext *context )
2242{
2243 const QDomElement leftElem = expressionNodeToOgcFilter( node->opLeft(), expression, context );
2244 if ( !mErrorMessage.isEmpty() )
2245 return QDomElement();
2246
2248
2249 // before right operator is parsed: to allow NULL handling
2251 {
2252 if ( node->opRight()->nodeType() == QgsExpressionNode::ntLiteral )
2253 {
2254 const QgsExpressionNodeLiteral *rightLit = static_cast<const QgsExpressionNodeLiteral *>( node->opRight() );
2255 if ( QgsVariantUtils::isNull( rightLit->value() ) )
2256 {
2257
2258 QDomElement elem = mDoc.createElement( mFilterPrefix + ":PropertyIsNull" );
2259 elem.appendChild( leftElem );
2260
2262 {
2263 QDomElement notElem = mDoc.createElement( mFilterPrefix + ":Not" );
2264 notElem.appendChild( elem );
2265 return notElem;
2266 }
2267
2268 return elem;
2269 }
2270
2271 // continue with equal / not equal operator once the null case is handled
2273 }
2274
2275 }
2276
2277 const QDomElement rightElem = expressionNodeToOgcFilter( node->opRight(), expression, context );
2278 if ( !mErrorMessage.isEmpty() )
2279 return QDomElement();
2280
2281
2282 const QString opText = binaryOperatorToTagName( op );
2283 if ( opText.isEmpty() )
2284 {
2285 // not implemented binary operators
2286 // TODO: regex, % (mod), ^ (pow) are not supported yet
2287 mErrorMessage = QObject::tr( "Binary operator %1 not implemented yet" ).arg( node->text() );
2288 return QDomElement();
2289 }
2290
2291 QDomElement boElem = mDoc.createElement( mFilterPrefix + ":" + opText );
2292
2294 {
2296 boElem.setAttribute( u"matchCase"_s, u"false"_s );
2297
2298 // setup wildCards to <ogc:PropertyIsLike>
2299 boElem.setAttribute( u"wildCard"_s, u"%"_s );
2300 boElem.setAttribute( u"singleChar"_s, u"_"_s );
2301 if ( mFilterVersion == QgsOgcUtils::FILTER_OGC_1_0 )
2302 boElem.setAttribute( u"escape"_s, u"\\"_s );
2303 else
2304 boElem.setAttribute( u"escapeChar"_s, u"\\"_s );
2305 }
2306
2307 boElem.appendChild( leftElem );
2308 boElem.appendChild( rightElem );
2309 return boElem;
2310}
2311
2312
2313QDomElement QgsOgcUtilsExprToFilter::expressionLiteralToOgcFilter( const QgsExpressionNodeLiteral *node, QgsExpression *expression, const QgsExpressionContext *context )
2314{
2315 Q_UNUSED( expression )
2316 Q_UNUSED( context )
2317 QString value;
2318 switch ( node->value().userType() )
2319 {
2320 case QMetaType::Type::Int:
2321 value = QString::number( node->value().toInt() );
2322 break;
2323 case QMetaType::Type::Double:
2324 value = qgsDoubleToString( node->value().toDouble() );
2325 break;
2326 case QMetaType::Type::QString:
2327 value = node->value().toString();
2328 break;
2329 case QMetaType::Type::QDate:
2330 value = node->value().toDate().toString( Qt::ISODate );
2331 break;
2332 case QMetaType::Type::QDateTime:
2333 value = node->value().toDateTime().toString( Qt::ISODate );
2334 break;
2335
2336 default:
2337 mErrorMessage = QObject::tr( "Literal type not supported: %1" ).arg( static_cast<QMetaType::Type>( node->value().userType() ) );
2338 return QDomElement();
2339 }
2340
2341 QDomElement litElem = mDoc.createElement( mFilterPrefix + ":Literal" );
2342 litElem.appendChild( mDoc.createTextNode( value ) );
2343 return litElem;
2344}
2345
2346
2347QDomElement QgsOgcUtilsExprToFilter::expressionColumnRefToOgcFilter( const QgsExpressionNodeColumnRef *node, QgsExpression *expression, const QgsExpressionContext *context )
2348{
2349 Q_UNUSED( expression )
2350 Q_UNUSED( context )
2351 QDomElement propElem = mDoc.createElement( mFilterPrefix + ":" + mPropertyName );
2352 if ( !mFieldNameToXPathMap.isEmpty() )
2353 {
2354 const auto iterFieldName = mFieldNameToXPathMap.constFind( node->name() );
2355 if ( iterFieldName != mFieldNameToXPathMap.constEnd() )
2356 {
2357 const QString xpath( *iterFieldName );
2358
2359 if ( !mNamespacePrefixToUriMap.isEmpty() )
2360 {
2361 const QStringList parts = xpath.split( '/' );
2362 QSet<QString> setNamespacePrefix;
2363 for ( const QString &part : std::as_const( parts ) )
2364 {
2365 const QStringList subparts = part.split( ':' );
2366 if ( subparts.size() == 2 && !setNamespacePrefix.contains( subparts[0] ) )
2367 {
2368 const auto iterNamespacePrefix = mNamespacePrefixToUriMap.constFind( subparts[0] );
2369 if ( iterNamespacePrefix != mNamespacePrefixToUriMap.constEnd() )
2370 {
2371 setNamespacePrefix.insert( subparts[0] );
2372 QDomAttr attr = mDoc.createAttribute( u"xmlns:"_s + subparts[0] );
2373 attr.setValue( *iterNamespacePrefix );
2374 propElem.setAttributeNode( attr );
2375 }
2376 }
2377 }
2378 }
2379
2380 propElem.appendChild( mDoc.createTextNode( xpath ) );
2381
2382 return propElem;
2383 }
2384 }
2385 QString columnRef( node->name() );
2386 if ( !mNamespacePrefix.isEmpty() && !mNamespaceURI.isEmpty() )
2387 columnRef = mNamespacePrefix + u":"_s + columnRef;
2388 propElem.appendChild( mDoc.createTextNode( columnRef ) );
2389 return propElem;
2390}
2391
2392
2393
2394QDomElement QgsOgcUtilsExprToFilter::expressionInOperatorToOgcFilter( const QgsExpressionNodeInOperator *node, QgsExpression *expression, const QgsExpressionContext *context )
2395{
2396 if ( node->list()->list().size() == 1 )
2397 {
2398 const QDomElement leftNode = expressionNodeToOgcFilter( node->node(), expression, context );
2399 const QDomElement firstListNode = expressionNodeToOgcFilter( node->list()->list().first(), expression, context );
2400 QDomElement eqElem = mDoc.createElement( mFilterPrefix + ":PropertyIsEqualTo" );
2401 eqElem.appendChild( leftNode );
2402 eqElem.appendChild( firstListNode );
2403 if ( node->isNotIn() )
2404 {
2405 QDomElement notElem = mDoc.createElement( mFilterPrefix + ":Not" );
2406 notElem.appendChild( eqElem );
2407 return notElem;
2408 }
2409 return eqElem;
2410 }
2411
2412 QDomElement orElem = mDoc.createElement( mFilterPrefix + ":Or" );
2413 const QDomElement leftNode = expressionNodeToOgcFilter( node->node(), expression, context );
2414
2415 const auto constList = node->list()->list();
2416 for ( QgsExpressionNode *n : constList )
2417 {
2418 const QDomElement listNode = expressionNodeToOgcFilter( n, expression, context );
2419 if ( !mErrorMessage.isEmpty() )
2420 return QDomElement();
2421
2422 QDomElement eqElem = mDoc.createElement( mFilterPrefix + ":PropertyIsEqualTo" );
2423 eqElem.appendChild( leftNode.cloneNode() );
2424 eqElem.appendChild( listNode );
2425
2426 orElem.appendChild( eqElem );
2427 }
2428
2429 if ( node->isNotIn() )
2430 {
2431 QDomElement notElem = mDoc.createElement( mFilterPrefix + ":Not" );
2432 notElem.appendChild( orElem );
2433 return notElem;
2434 }
2435
2436 return orElem;
2437}
2438
2439Q_GLOBAL_STATIC_WITH_ARGS( QgsStringMap, BINARY_SPATIAL_OPS_MAP, (
2440{
2441 { "disjoint"_L1, "Disjoint"_L1 },
2442 { "intersects"_L1, "Intersects"_L1},
2443 { "touches"_L1, "Touches"_L1 },
2444 { "crosses"_L1, "Crosses"_L1 },
2445 { "contains"_L1, "Contains"_L1 },
2446 { "overlaps"_L1, "Overlaps"_L1 },
2447 { "within"_L1, "Within"_L1 }
2448} ) )
2449
2450static bool isBinarySpatialOperator( const QString &fnName )
2451{
2452 return BINARY_SPATIAL_OPS_MAP()->contains( fnName );
2453}
2454
2455static QString tagNameForSpatialOperator( const QString &fnName )
2456{
2457 return BINARY_SPATIAL_OPS_MAP()->value( fnName );
2458}
2459
2460static bool isGeometryColumn( const QgsExpressionNode *node )
2461{
2462 if ( node->nodeType() != QgsExpressionNode::ntFunction )
2463 return false;
2464
2465 const QgsExpressionNodeFunction *fn = static_cast<const QgsExpressionNodeFunction *>( node );
2467 return fd->name() == "$geometry"_L1 || ( fd->name() == "var"_L1 && fn->referencedVariables().contains( "geometry"_L1 ) );
2468}
2469
2470static QgsGeometry geometryFromConstExpr( const QgsExpressionNode *node )
2471{
2472 // Right now we support only geomFromWKT(' ..... ')
2473 // Ideally we should support any constant sub-expression (not dependent on feature's geometry or attributes)
2474
2475 if ( node->nodeType() == QgsExpressionNode::ntFunction )
2476 {
2477 const QgsExpressionNodeFunction *fnNode = static_cast<const QgsExpressionNodeFunction *>( node );
2479 if ( fnDef->name() == "geom_from_wkt"_L1 )
2480 {
2481 const QList<QgsExpressionNode *> &args = fnNode->args()->list();
2482 if ( args[0]->nodeType() == QgsExpressionNode::ntLiteral )
2483 {
2484 const QString wkt = static_cast<const QgsExpressionNodeLiteral *>( args[0] )->value().toString();
2485 return QgsGeometry::fromWkt( wkt );
2486 }
2487 }
2488 }
2489 return QgsGeometry();
2490}
2491
2492
2493QDomElement QgsOgcUtilsExprToFilter::expressionFunctionToOgcFilter( const QgsExpressionNodeFunction *node, QgsExpression *expression, const QgsExpressionContext *context )
2494{
2495 QgsExpressionFunction *fd = QgsExpression::Functions()[node->fnIndex()];
2496
2497 if ( fd->name() == "intersects_bbox"_L1 )
2498 {
2499 QList<QgsExpressionNode *> argNodes = node->args()->list();
2500 Q_ASSERT( argNodes.count() == 2 ); // binary spatial ops must have two args
2501
2502 const QgsGeometry geom = geometryFromConstExpr( argNodes[1] );
2503 if ( !geom.isNull() && isGeometryColumn( argNodes[0] ) )
2504 {
2505 QgsRectangle rect = geom.boundingBox();
2506
2507 mGMLUsed = true;
2508
2509 const QDomElement elemBox = ( mGMLVersion == QgsOgcUtils::GML_2_1_2 ) ?
2510 QgsOgcUtils::rectangleToGMLBox( &rect, mDoc, mSrsName, mInvertAxisOrientation ) :
2511 QgsOgcUtils::rectangleToGMLEnvelope( &rect, mDoc, mSrsName, mInvertAxisOrientation );
2512
2513 QDomElement funcElem = mDoc.createElement( mFilterPrefix + ":BBOX" );
2514
2515 if ( !mGeometryName.isEmpty() )
2516 {
2517 // Geometry column is optional for a BBOX filter.
2518 QDomElement geomProperty = mDoc.createElement( mFilterPrefix + ":" + mPropertyName );
2519 QString columnRef( mGeometryName );
2520 if ( !mNamespacePrefix.isEmpty() && !mNamespaceURI.isEmpty() )
2521 columnRef = mNamespacePrefix + u":"_s + columnRef;
2522 geomProperty.appendChild( mDoc.createTextNode( columnRef ) );
2523
2524 funcElem.appendChild( geomProperty );
2525 }
2526 funcElem.appendChild( elemBox );
2527 return funcElem;
2528 }
2529 else
2530 {
2531 mErrorMessage = QObject::tr( "<BBOX> is currently supported only in form: bbox(@geometry, geomFromWKT('…'))" );
2532 return QDomElement();
2533 }
2534 }
2535
2536 if ( isBinarySpatialOperator( fd->name() ) )
2537 {
2538 QList<QgsExpressionNode *> argNodes = node->args()->list();
2539 Q_ASSERT( argNodes.count() == 2 ); // binary spatial ops must have two args
2540
2541 QgsExpressionNode *otherNode = nullptr;
2542 if ( isGeometryColumn( argNodes[0] ) )
2543 otherNode = argNodes[1];
2544 else if ( isGeometryColumn( argNodes[1] ) )
2545 otherNode = argNodes[0];
2546 else
2547 {
2548 mErrorMessage = QObject::tr( "Unable to translate spatial operator: at least one must refer to geometry." );
2549 return QDomElement();
2550 }
2551
2552 QDomElement otherGeomElem;
2553
2554 // the other node must be a geometry constructor
2555 if ( otherNode->nodeType() != QgsExpressionNode::ntFunction )
2556 {
2557 mErrorMessage = QObject::tr( "spatial operator: the other operator must be a geometry constructor function" );
2558 return QDomElement();
2559 }
2560
2561 const QgsExpressionNodeFunction *otherFn = static_cast<const QgsExpressionNodeFunction *>( otherNode );
2562 QgsExpressionFunction *otherFnDef = QgsExpression::Functions()[otherFn->fnIndex()];
2563 if ( otherFnDef->name() == "geom_from_wkt"_L1 )
2564 {
2565 QgsExpressionNode *firstFnArg = otherFn->args()->list()[0];
2566 if ( firstFnArg->nodeType() != QgsExpressionNode::ntLiteral )
2567 {
2568 mErrorMessage = QObject::tr( "geom_from_wkt: argument must be string literal" );
2569 return QDomElement();
2570 }
2571 const QString wkt = static_cast<const QgsExpressionNodeLiteral *>( firstFnArg )->value().toString();
2572 const QgsGeometry geom = QgsGeometry::fromWkt( wkt );
2573 otherGeomElem = QgsOgcUtils::geometryToGML( geom, mDoc, mGMLVersion, mSrsName, mInvertAxisOrientation,
2574 u"qgis_id_geom_%1"_s.arg( mGeomId ) );
2575 if ( otherGeomElem.isNull() )
2576 {
2577 mErrorMessage = QObject::tr( "geom_from_wkt: unable to generate GML from wkt geometry" );
2578 return QDomElement();
2579 }
2580 mGeomId ++;
2581 }
2582 else if ( otherFnDef->name() == "geom_from_gml"_L1 )
2583 {
2584 QgsExpressionNode *firstFnArg = otherFn->args()->list()[0];
2585 if ( firstFnArg->nodeType() != QgsExpressionNode::ntLiteral )
2586 {
2587 mErrorMessage = QObject::tr( "geom_from_gml: argument must be string literal" );
2588 return QDomElement();
2589 }
2590
2591 QDomDocument geomDoc;
2592 const QString gml = static_cast<const QgsExpressionNodeLiteral *>( firstFnArg )->value().toString();
2593 // wrap the string into a root tag to have "gml" namespace
2594 const QString xml = u"<tmp xmlns:gml=\"%1\">%2</tmp>"_s.arg( GML_NAMESPACE, gml );
2595 if ( !geomDoc.setContent( xml, true ) )
2596 {
2597 mErrorMessage = QObject::tr( "geom_from_gml: unable to parse XML" );
2598 return QDomElement();
2599 }
2600
2601 const QDomNode geomNode = mDoc.importNode( geomDoc.documentElement().firstChildElement(), true );
2602 otherGeomElem = geomNode.toElement();
2603 }
2604 else if ( otherNode->hasCachedStaticValue() && otherNode->cachedStaticValue().userType() == qMetaTypeId< QgsGeometry>() )
2605 {
2606 QgsGeometry geom = otherNode->cachedStaticValue().value<QgsGeometry>();
2607 otherGeomElem = QgsOgcUtils::geometryToGML( geom, mDoc, mGMLVersion, mSrsName, mInvertAxisOrientation,
2608 u"qgis_id_geom_%1"_s.arg( mGeomId ) );
2609 if ( otherGeomElem.isNull() )
2610 {
2611 mErrorMessage = QObject::tr( "geom from static value: unable to generate GML from static variable" );
2612 return QDomElement();
2613 }
2614 mGeomId ++;
2615 }
2616 else
2617 {
2618 mErrorMessage = QObject::tr( "spatial operator: unknown geometry constructor function" );
2619 return QDomElement();
2620 }
2621
2622 mGMLUsed = true;
2623
2624 QDomElement funcElem = mDoc.createElement( mFilterPrefix + ":" + tagNameForSpatialOperator( fd->name() ) );
2625 QDomElement geomProperty = mDoc.createElement( mFilterPrefix + ":" + mPropertyName );
2626 QString columnRef( mGeometryName );
2627 if ( !mNamespacePrefix.isEmpty() && !mNamespaceURI.isEmpty() )
2628 columnRef = mNamespacePrefix + u":"_s + columnRef;
2629 geomProperty.appendChild( mDoc.createTextNode( columnRef ) );
2630 funcElem.appendChild( geomProperty );
2631 funcElem.appendChild( otherGeomElem );
2632 return funcElem;
2633 }
2634
2635 if ( fd->isStatic( node, expression, context ) )
2636 {
2637 const QVariant result = fd->run( node->args(), context, expression, node );
2638 const QgsExpressionNodeLiteral literal( result );
2639 return expressionLiteralToOgcFilter( &literal, expression, context );
2640 }
2641
2642 if ( fd->params() == 0 )
2643 {
2644 mErrorMessage = QObject::tr( "Special columns/constants are not supported." );
2645 return QDomElement();
2646 }
2647
2648 // this is somehow wrong - we are just hoping that the other side supports the same functions as we do...
2649 QDomElement funcElem = mDoc.createElement( mFilterPrefix + ":Function" );
2650 funcElem.setAttribute( u"name"_s, fd->name() );
2651 const auto constList = node->args()->list();
2652 for ( QgsExpressionNode *n : constList )
2653 {
2654 const QDomElement childElem = expressionNodeToOgcFilter( n, expression, context );
2655 if ( !mErrorMessage.isEmpty() )
2656 return QDomElement();
2657
2658 funcElem.appendChild( childElem );
2659 }
2660
2661 return funcElem;
2662}
2663
2664//
2665
2667 QgsOgcUtils::GMLVersion gmlVersion,
2668 QgsOgcUtils::FilterVersion filterVersion,
2669 const QList<QgsOgcUtils::LayerProperties> &layerProperties,
2670 bool honourAxisOrientation,
2671 bool invertAxisOrientation,
2672 const QMap< QString, QString> &mapUnprefixedTypenameToPrefixedTypename,
2673 const QMap<QString, QString> &fieldNameToXPathMap,
2674 const QMap<QString, QString> &namespacePrefixToUriMap )
2675 : mDoc( doc )
2676 , mGMLVersion( gmlVersion )
2677 , mFilterVersion( filterVersion )
2678 , mLayerProperties( layerProperties )
2679 , mHonourAxisOrientation( honourAxisOrientation )
2680 , mInvertAxisOrientation( invertAxisOrientation )
2681 , mFilterPrefix( ( filterVersion == QgsOgcUtils::FILTER_FES_2_0 ) ? "fes" : "ogc" )
2682 , mPropertyName( ( filterVersion == QgsOgcUtils::FILTER_FES_2_0 ) ? "ValueReference" : "PropertyName" )
2683 , mMapUnprefixedTypenameToPrefixedTypename( mapUnprefixedTypenameToPrefixedTypename )
2684 , mFieldNameToXPathMap( fieldNameToXPathMap )
2685 , mNamespacePrefixToUriMap( namespacePrefixToUriMap )
2686{
2687}
2688
2690{
2691 switch ( node->nodeType() )
2692 {
2694 return toOgcFilter( static_cast<const QgsSQLStatement::NodeUnaryOperator *>( node ) );
2696 return toOgcFilter( static_cast<const QgsSQLStatement::NodeBinaryOperator *>( node ) );
2698 return toOgcFilter( static_cast<const QgsSQLStatement::NodeInOperator *>( node ) );
2700 return toOgcFilter( static_cast<const QgsSQLStatement::NodeBetweenOperator *>( node ) );
2702 return toOgcFilter( static_cast<const QgsSQLStatement::NodeFunction *>( node ) );
2704 return toOgcFilter( static_cast<const QgsSQLStatement::NodeLiteral *>( node ) );
2706 return toOgcFilter( static_cast<const QgsSQLStatement::NodeColumnRef *>( node ) );
2708 return toOgcFilter( static_cast<const QgsSQLStatement::NodeSelect *>( node ) );
2709
2710 default:
2711 mErrorMessage = QObject::tr( "Node type not supported: %1" ).arg( node->nodeType() );
2712 return QDomElement();
2713 }
2714}
2715
2716
2718{
2719
2720 const QDomElement operandElem = toOgcFilter( node->operand() );
2721 if ( !mErrorMessage.isEmpty() )
2722 return QDomElement();
2723
2724 QDomElement uoElem;
2725 switch ( node->op() )
2726 {
2728 uoElem = mDoc.createElement( mFilterPrefix + ":Literal" );
2729 if ( node->operand()->nodeType() == QgsSQLStatement::ntLiteral )
2730 {
2731 // operand expression already created a Literal node:
2732 // take the literal value, prepend - and remove old literal node
2733 uoElem.appendChild( mDoc.createTextNode( "-" + operandElem.text() ) );
2734 mDoc.removeChild( operandElem );
2735 }
2736 else
2737 {
2738 mErrorMessage = QObject::tr( "This use of unary operator not implemented yet" );
2739 return QDomElement();
2740 }
2741 break;
2743 uoElem = mDoc.createElement( mFilterPrefix + ":Not" );
2744 uoElem.appendChild( operandElem );
2745 break;
2746
2747 default:
2748 mErrorMessage = QObject::tr( "Unary operator %1 not implemented yet" ).arg( QgsSQLStatement::UNARY_OPERATOR_TEXT[node->op()] );
2749 return QDomElement();
2750 }
2751
2752 return uoElem;
2753}
2754
2755
2757{
2758 const QDomElement leftElem = toOgcFilter( node->opLeft() );
2759 if ( !mErrorMessage.isEmpty() )
2760 return QDomElement();
2761
2763
2764 // before right operator is parsed: to allow NULL handling
2766 {
2767 if ( node->opRight()->nodeType() == QgsSQLStatement::ntLiteral )
2768 {
2769 const QgsSQLStatement::NodeLiteral *rightLit = static_cast<const QgsSQLStatement::NodeLiteral *>( node->opRight() );
2770 if ( QgsVariantUtils::isNull( rightLit->value() ) )
2771 {
2772
2773 QDomElement elem = mDoc.createElement( mFilterPrefix + ":PropertyIsNull" );
2774 elem.appendChild( leftElem );
2775
2776 if ( op == QgsSQLStatement::boIsNot )
2777 {
2778 QDomElement notElem = mDoc.createElement( mFilterPrefix + ":Not" );
2779 notElem.appendChild( elem );
2780 return notElem;
2781 }
2782
2783 return elem;
2784 }
2785
2786 // continue with equal / not equal operator once the null case is handled
2788 }
2789
2790 }
2791
2792 const QDomElement rightElem = toOgcFilter( node->opRight() );
2793 if ( !mErrorMessage.isEmpty() )
2794 return QDomElement();
2795
2796
2797 QString opText;
2798 if ( op == QgsSQLStatement::boOr )
2799 opText = u"Or"_s;
2800 else if ( op == QgsSQLStatement::boAnd )
2801 opText = u"And"_s;
2802 else if ( op == QgsSQLStatement::boEQ )
2803 opText = u"PropertyIsEqualTo"_s;
2804 else if ( op == QgsSQLStatement::boNE )
2805 opText = u"PropertyIsNotEqualTo"_s;
2806 else if ( op == QgsSQLStatement::boLE )
2807 opText = u"PropertyIsLessThanOrEqualTo"_s;
2808 else if ( op == QgsSQLStatement::boGE )
2809 opText = u"PropertyIsGreaterThanOrEqualTo"_s;
2810 else if ( op == QgsSQLStatement::boLT )
2811 opText = u"PropertyIsLessThan"_s;
2812 else if ( op == QgsSQLStatement::boGT )
2813 opText = u"PropertyIsGreaterThan"_s;
2814 else if ( op == QgsSQLStatement::boLike )
2815 opText = u"PropertyIsLike"_s;
2816 else if ( op == QgsSQLStatement::boILike )
2817 opText = u"PropertyIsLike"_s;
2818
2819 if ( opText.isEmpty() )
2820 {
2821 // not implemented binary operators
2822 mErrorMessage = QObject::tr( "Binary operator %1 not implemented yet" ).arg( QgsSQLStatement::BINARY_OPERATOR_TEXT[op] );
2823 return QDomElement();
2824 }
2825
2826 QDomElement boElem = mDoc.createElement( mFilterPrefix + ":" + opText );
2827
2829 {
2830 if ( op == QgsSQLStatement::boILike )
2831 boElem.setAttribute( u"matchCase"_s, u"false"_s );
2832
2833 // setup wildCards to <ogc:PropertyIsLike>
2834 boElem.setAttribute( u"wildCard"_s, u"%"_s );
2835 boElem.setAttribute( u"singleChar"_s, u"_"_s );
2836 if ( mFilterVersion == QgsOgcUtils::FILTER_OGC_1_0 )
2837 boElem.setAttribute( u"escape"_s, u"\\"_s );
2838 else
2839 boElem.setAttribute( u"escapeChar"_s, u"\\"_s );
2840 }
2841
2842 boElem.appendChild( leftElem );
2843 boElem.appendChild( rightElem );
2844 return boElem;
2845}
2846
2847
2849{
2850 QString value;
2851 switch ( node->value().userType() )
2852 {
2853 case QMetaType::Type::Int:
2854 value = QString::number( node->value().toInt() );
2855 break;
2856 case QMetaType::Type::LongLong:
2857 value = QString::number( node->value().toLongLong() );
2858 break;
2859 case QMetaType::Type::Double:
2860 value = qgsDoubleToString( node->value().toDouble() );
2861 break;
2862 case QMetaType::Type::QString:
2863 value = node->value().toString();
2864 break;
2865
2866 default:
2867 mErrorMessage = QObject::tr( "Literal type not supported: %1" ).arg( static_cast<QMetaType::Type>( node->value().userType() ) );
2868 return QDomElement();
2869 }
2870
2871 QDomElement litElem = mDoc.createElement( mFilterPrefix + ":Literal" );
2872 litElem.appendChild( mDoc.createTextNode( value ) );
2873 return litElem;
2874}
2875
2876
2878{
2879 QDomElement propElem = mDoc.createElement( mFilterPrefix + ":" + mPropertyName );
2880 if ( node->tableName().isEmpty() || mLayerProperties.size() == 1 )
2881 {
2882 if ( !mFieldNameToXPathMap.isEmpty() )
2883 {
2884 const auto iterFieldName = mFieldNameToXPathMap.constFind( node->name() );
2885 if ( iterFieldName != mFieldNameToXPathMap.constEnd() )
2886 {
2887 const QString xpath( *iterFieldName );
2888
2889 if ( !mNamespacePrefixToUriMap.isEmpty() )
2890 {
2891 const QStringList parts = xpath.split( '/' );
2892 QSet<QString> setNamespacePrefix;
2893 for ( const QString &part : std::as_const( parts ) )
2894 {
2895 const QStringList subparts = part.split( ':' );
2896 if ( subparts.size() == 2 && !setNamespacePrefix.contains( subparts[0] ) )
2897 {
2898 const auto iterNamespacePrefix = mNamespacePrefixToUriMap.constFind( subparts[0] );
2899 if ( iterNamespacePrefix != mNamespacePrefixToUriMap.constEnd() )
2900 {
2901 setNamespacePrefix.insert( subparts[0] );
2902 QDomAttr attr = mDoc.createAttribute( u"xmlns:"_s + subparts[0] );
2903 attr.setValue( *iterNamespacePrefix );
2904 propElem.setAttributeNode( attr );
2905 }
2906 }
2907 }
2908 }
2909
2910 propElem.appendChild( mDoc.createTextNode( xpath ) );
2911
2912 return propElem;
2913 }
2914 }
2915 if ( mLayerProperties.size() == 1 && !mLayerProperties[0].mNamespacePrefix.isEmpty() && !mLayerProperties[0].mNamespaceURI.isEmpty() )
2916 propElem.appendChild( mDoc.createTextNode(
2917 mLayerProperties[0].mNamespacePrefix + u":"_s + node->name() ) );
2918 else
2919 propElem.appendChild( mDoc.createTextNode( node->name() ) );
2920 }
2921 else
2922 {
2923 QString tableName( mMapTableAliasToNames[node->tableName()] );
2924 if ( mMapUnprefixedTypenameToPrefixedTypename.contains( tableName ) )
2925 tableName = mMapUnprefixedTypenameToPrefixedTypename[tableName];
2926 propElem.appendChild( mDoc.createTextNode( tableName + "/" + node->name() ) );
2927 }
2928 return propElem;
2929}
2930
2932{
2933 if ( node->list()->list().size() == 1 )
2934 {
2935 const QDomElement leftNode = toOgcFilter( node->node() );
2936 const QDomElement firstListNode = toOgcFilter( node->list()->list().first() );
2937 QDomElement eqElem = mDoc.createElement( mFilterPrefix + ":PropertyIsEqualTo" );
2938 eqElem.appendChild( leftNode );
2939 eqElem.appendChild( firstListNode );
2940 if ( node->isNotIn() )
2941 {
2942 QDomElement notElem = mDoc.createElement( mFilterPrefix + ":Not" );
2943 notElem.appendChild( eqElem );
2944 return notElem;
2945 }
2946 return eqElem;
2947 }
2948
2949 QDomElement orElem = mDoc.createElement( mFilterPrefix + ":Or" );
2950 const QDomElement leftNode = toOgcFilter( node->node() );
2951
2952 const auto constList = node->list()->list();
2953 for ( QgsSQLStatement::Node *n : constList )
2954 {
2955 const QDomElement listNode = toOgcFilter( n );
2956 if ( !mErrorMessage.isEmpty() )
2957 return QDomElement();
2958
2959 QDomElement eqElem = mDoc.createElement( mFilterPrefix + ":PropertyIsEqualTo" );
2960 eqElem.appendChild( leftNode.cloneNode() );
2961 eqElem.appendChild( listNode );
2962
2963 orElem.appendChild( eqElem );
2964 }
2965
2966 if ( node->isNotIn() )
2967 {
2968 QDomElement notElem = mDoc.createElement( mFilterPrefix + ":Not" );
2969 notElem.appendChild( orElem );
2970 return notElem;
2971 }
2972
2973 return orElem;
2974}
2975
2977{
2978 QDomElement elem = mDoc.createElement( mFilterPrefix + ":PropertyIsBetween" );
2979 elem.appendChild( toOgcFilter( node->node() ) );
2980 QDomElement lowerBoundary = mDoc.createElement( mFilterPrefix + ":LowerBoundary" );
2981 lowerBoundary.appendChild( toOgcFilter( node->minVal() ) );
2982 elem.appendChild( lowerBoundary );
2983 QDomElement upperBoundary = mDoc.createElement( mFilterPrefix + ":UpperBoundary" );
2984 upperBoundary.appendChild( toOgcFilter( node->maxVal() ) );
2985 elem.appendChild( upperBoundary );
2986
2987 if ( node->isNotBetween() )
2988 {
2989 QDomElement notElem = mDoc.createElement( mFilterPrefix + ":Not" );
2990 notElem.appendChild( elem );
2991 return notElem;
2992 }
2993
2994 return elem;
2995}
2996
2997static QString mapBinarySpatialToOgc( const QString &name )
2998{
2999 QString nameCompare( name );
3000 if ( name.size() > 3 && QStringView {name} .mid( 0, 3 ).toString().compare( "ST_"_L1, Qt::CaseInsensitive ) == 0 )
3001 nameCompare = name.mid( 3 );
3002 QStringList spatialOps;
3003 spatialOps << u"BBOX"_s << u"Intersects"_s << u"Contains"_s << u"Crosses"_s << u"Equals"_s
3004 << u"Disjoint"_s << u"Overlaps"_s << u"Touches"_s << u"Within"_s;
3005 const auto constSpatialOps = spatialOps;
3006 for ( QString op : constSpatialOps )
3007 {
3008 if ( nameCompare.compare( op, Qt::CaseInsensitive ) == 0 )
3009 return op;
3010 }
3011 return QString();
3012}
3013
3014static QString mapTernarySpatialToOgc( const QString &name )
3015{
3016 QString nameCompare( name );
3017 if ( name.size() > 3 && QStringView {name} .mid( 0, 3 ).compare( "ST_"_L1, Qt::CaseInsensitive ) == 0 )
3018 nameCompare = name.mid( 3 );
3019 if ( nameCompare.compare( "DWithin"_L1, Qt::CaseInsensitive ) == 0 )
3020 return u"DWithin"_s;
3021 if ( nameCompare.compare( "Beyond"_L1, Qt::CaseInsensitive ) == 0 )
3022 return u"Beyond"_s;
3023 return QString();
3024}
3025
3026QString QgsOgcUtilsSQLStatementToFilter::getGeometryColumnSRSName( const QgsSQLStatement::Node *node )
3027{
3028 if ( node->nodeType() != QgsSQLStatement::ntColumnRef )
3029 return QString();
3030
3031 const QgsSQLStatement::NodeColumnRef *col = static_cast<const QgsSQLStatement::NodeColumnRef *>( node );
3032 if ( !col->tableName().isEmpty() )
3033 {
3034 const auto constMLayerProperties = mLayerProperties;
3035 for ( const QgsOgcUtils::LayerProperties &prop : constMLayerProperties )
3036 {
3037 if ( prop.mName.compare( mMapTableAliasToNames[col->tableName()], Qt::CaseInsensitive ) == 0 &&
3038 prop.mGeometryAttribute.compare( col->name(), Qt::CaseInsensitive ) == 0 )
3039 {
3040 return prop.mSRSName;
3041 }
3042 }
3043 }
3044 if ( !mLayerProperties.empty() &&
3045 mLayerProperties.at( 0 ).mGeometryAttribute.compare( col->name(), Qt::CaseInsensitive ) == 0 )
3046 {
3047 return mLayerProperties.at( 0 ).mSRSName;
3048 }
3049 return QString();
3050}
3051
3052bool QgsOgcUtilsSQLStatementToFilter::processSRSName( const QgsSQLStatement::NodeFunction *mainNode,
3053 QList<QgsSQLStatement::Node *> args,
3054 bool lastArgIsSRSName,
3055 QString &srsName,
3056 bool &axisInversion )
3057{
3058 srsName = mCurrentSRSName;
3059 axisInversion = mInvertAxisOrientation;
3060
3061 if ( lastArgIsSRSName )
3062 {
3063 QgsSQLStatement::Node *lastArg = args[ args.size() - 1 ];
3064 if ( lastArg->nodeType() != QgsSQLStatement::ntLiteral )
3065 {
3066 mErrorMessage = QObject::tr( "%1: Last argument must be string or integer literal" ).arg( mainNode->name() );
3067 return false;
3068 }
3069 const QgsSQLStatement::NodeLiteral *lit = static_cast<const QgsSQLStatement::NodeLiteral *>( lastArg );
3070 if ( lit->value().userType() == QMetaType::Type::Int )
3071 {
3072 if ( mFilterVersion == QgsOgcUtils::FILTER_OGC_1_0 )
3073 {
3074 srsName = "EPSG:" + QString::number( lit->value().toInt() );
3075 }
3076 else
3077 {
3078 srsName = "urn:ogc:def:crs:EPSG::" + QString::number( lit->value().toInt() );
3079 }
3080 }
3081 else
3082 {
3083 srsName = lit->value().toString();
3084 if ( srsName.startsWith( "EPSG:"_L1, Qt::CaseInsensitive ) )
3085 return true;
3086 }
3087 }
3088
3089 QgsCoordinateReferenceSystem crs;
3090 if ( !srsName.isEmpty() )
3092 if ( crs.isValid() )
3093 {
3094 if ( mHonourAxisOrientation && crs.hasAxisInverted() )
3095 {
3096 axisInversion = !axisInversion;
3097 }
3098 }
3099
3100 return true;
3101}
3102
3104{
3105 // ST_GeometryFromText
3106 if ( node->name().compare( "ST_GeometryFromText"_L1, Qt::CaseInsensitive ) == 0 )
3107 {
3108 QList<QgsSQLStatement::Node *> args = node->args()->list();
3109 if ( args.size() != 1 && args.size() != 2 )
3110 {
3111 mErrorMessage = QObject::tr( "Function %1 should have 1 or 2 arguments" ).arg( node->name() );
3112 return QDomElement();
3113 }
3114
3115 QgsSQLStatement::Node *firstFnArg = args[0];
3116 if ( firstFnArg->nodeType() != QgsSQLStatement::ntLiteral )
3117 {
3118 mErrorMessage = QObject::tr( "%1: First argument must be string literal" ).arg( node->name() );
3119 return QDomElement();
3120 }
3121
3122 QString srsName;
3123 bool axisInversion;
3124 if ( ! processSRSName( node, args, args.size() == 2, srsName, axisInversion ) )
3125 {
3126 return QDomElement();
3127 }
3128
3129 const QString wkt = static_cast<const QgsSQLStatement::NodeLiteral *>( firstFnArg )->value().toString();
3130 const QgsGeometry geom = QgsGeometry::fromWkt( wkt );
3131 const QDomElement geomElem = QgsOgcUtils::geometryToGML( geom, mDoc, mGMLVersion, srsName, axisInversion,
3132 u"qgis_id_geom_%1"_s.arg( mGeomId ) );
3133 mGeomId ++;
3134 if ( geomElem.isNull() )
3135 {
3136 mErrorMessage = QObject::tr( "%1: invalid WKT" ).arg( node->name() );
3137 return QDomElement();
3138 }
3139 mGMLUsed = true;
3140 return geomElem;
3141 }
3142
3143 // ST_MakeEnvelope
3144 if ( node->name().compare( "ST_MakeEnvelope"_L1, Qt::CaseInsensitive ) == 0 )
3145 {
3146 QList<QgsSQLStatement::Node *> args = node->args()->list();
3147 if ( args.size() != 4 && args.size() != 5 )
3148 {
3149 mErrorMessage = QObject::tr( "Function %1 should have 4 or 5 arguments" ).arg( node->name() );
3150 return QDomElement();
3151 }
3152
3153 QgsRectangle rect;
3154
3155 for ( int i = 0; i < 4; i++ )
3156 {
3157 QgsSQLStatement::Node *arg = args[i];
3158 if ( arg->nodeType() != QgsSQLStatement::ntLiteral )
3159 {
3160 mErrorMessage = QObject::tr( "%1: Argument %2 must be numeric literal" ).arg( node->name() ).arg( i + 1 );
3161 return QDomElement();
3162 }
3163 const QgsSQLStatement::NodeLiteral *lit = static_cast<const QgsSQLStatement::NodeLiteral *>( arg );
3164 double val = 0.0;
3165 if ( lit->value().userType() == QMetaType::Type::Int )
3166 val = lit->value().toInt();
3167 else if ( lit->value().userType() == QMetaType::Type::LongLong )
3168 val = lit->value().toLongLong();
3169 else if ( lit->value().userType() == QMetaType::Type::Double )
3170 val = lit->value().toDouble();
3171 else
3172 {
3173 mErrorMessage = QObject::tr( "%1 Argument %2 must be numeric literal" ).arg( node->name() ).arg( i + 1 );
3174 return QDomElement();
3175 }
3176 if ( i == 0 )
3177 rect.setXMinimum( val );
3178 else if ( i == 1 )
3179 rect.setYMinimum( val );
3180 else if ( i == 2 )
3181 rect.setXMaximum( val );
3182 else
3183 rect.setYMaximum( val );
3184 }
3185
3186 QString srsName;
3187 bool axisInversion;
3188 if ( ! processSRSName( node, args, args.size() == 5, srsName, axisInversion ) )
3189 {
3190 return QDomElement();
3191 }
3192
3193 mGMLUsed = true;
3194
3195 return ( mGMLVersion == QgsOgcUtils::GML_2_1_2 ) ?
3196 QgsOgcUtils::rectangleToGMLBox( &rect, mDoc, srsName, axisInversion, 15 ) :
3197 QgsOgcUtils::rectangleToGMLEnvelope( &rect, mDoc, srsName, axisInversion, 15 );
3198 }
3199
3200 // ST_GeomFromGML
3201 if ( node->name().compare( "ST_GeomFromGML"_L1, Qt::CaseInsensitive ) == 0 )
3202 {
3203 QList<QgsSQLStatement::Node *> args = node->args()->list();
3204 if ( args.size() != 1 )
3205 {
3206 mErrorMessage = QObject::tr( "Function %1 should have 1 argument" ).arg( node->name() );
3207 return QDomElement();
3208 }
3209
3210 QgsSQLStatement::Node *firstFnArg = args[0];
3211 if ( firstFnArg->nodeType() != QgsSQLStatement::ntLiteral )
3212 {
3213 mErrorMessage = QObject::tr( "%1: Argument must be string literal" ).arg( node->name() );
3214 return QDomElement();
3215 }
3216
3217 QDomDocument geomDoc;
3218 const QString gml = static_cast<const QgsSQLStatement::NodeLiteral *>( firstFnArg )->value().toString();
3219 // wrap the string into a root tag to have "gml" namespace
3220 const QString xml = u"<tmp xmlns:gml=\"%1\">%2</tmp>"_s.arg( GML_NAMESPACE, gml );
3221 if ( !geomDoc.setContent( xml, true ) )
3222 {
3223 mErrorMessage = QObject::tr( "ST_GeomFromGML: unable to parse XML" );
3224 return QDomElement();
3225 }
3226
3227 const QDomNode geomNode = mDoc.importNode( geomDoc.documentElement().firstChildElement(), true );
3228 mGMLUsed = true;
3229 return geomNode.toElement();
3230 }
3231
3232 // Binary geometry operators
3233 QString ogcName( mapBinarySpatialToOgc( node->name() ) );
3234 if ( !ogcName.isEmpty() )
3235 {
3236 QList<QgsSQLStatement::Node *> args = node->args()->list();
3237 if ( args.size() != 2 )
3238 {
3239 mErrorMessage = QObject::tr( "Function %1 should have 2 arguments" ).arg( node->name() );
3240 return QDomElement();
3241 }
3242
3243 for ( int i = 0; i < 2; i ++ )
3244 {
3245 if ( args[i]->nodeType() == QgsSQLStatement::ntFunction &&
3246 ( static_cast<const QgsSQLStatement::NodeFunction *>( args[i] )->name().compare( "ST_GeometryFromText"_L1, Qt::CaseInsensitive ) == 0 ||
3247 static_cast<const QgsSQLStatement::NodeFunction *>( args[i] )->name().compare( "ST_MakeEnvelope"_L1, Qt::CaseInsensitive ) == 0 ) )
3248 {
3249 mCurrentSRSName = getGeometryColumnSRSName( args[1 - i] );
3250 break;
3251 }
3252 }
3253
3254 //if( ogcName == "Intersects" && mFilterVersion == QgsOgcUtils::FILTER_OGC_1_0 )
3255 // ogcName = "Intersect";
3256 QDomElement funcElem = mDoc.createElement( mFilterPrefix + ":" + ogcName );
3257 const auto constArgs = args;
3258 for ( QgsSQLStatement::Node *n : constArgs )
3259 {
3260 const QDomElement childElem = toOgcFilter( n );
3261 if ( !mErrorMessage.isEmpty() )
3262 {
3263 mCurrentSRSName.clear();
3264 return QDomElement();
3265 }
3266
3267 funcElem.appendChild( childElem );
3268 }
3269
3270 mCurrentSRSName.clear();
3271 return funcElem;
3272 }
3273
3274 ogcName = mapTernarySpatialToOgc( node->name() );
3275 if ( !ogcName.isEmpty() )
3276 {
3277 QList<QgsSQLStatement::Node *> args = node->args()->list();
3278 if ( args.size() != 3 )
3279 {
3280 mErrorMessage = QObject::tr( "Function %1 should have 3 arguments" ).arg( node->name() );
3281 return QDomElement();
3282 }
3283
3284 for ( int i = 0; i < 2; i ++ )
3285 {
3286 if ( args[i]->nodeType() == QgsSQLStatement::ntFunction &&
3287 ( static_cast<const QgsSQLStatement::NodeFunction *>( args[i] )->name().compare( "ST_GeometryFromText"_L1, Qt::CaseInsensitive ) == 0 ||
3288 static_cast<const QgsSQLStatement::NodeFunction *>( args[i] )->name().compare( "ST_MakeEnvelope"_L1, Qt::CaseInsensitive ) == 0 ) )
3289 {
3290 mCurrentSRSName = getGeometryColumnSRSName( args[1 - i] );
3291 break;
3292 }
3293 }
3294
3295 QDomElement funcElem = mDoc.createElement( mFilterPrefix + ":" + node->name().mid( 3 ) );
3296 for ( int i = 0; i < 2; i++ )
3297 {
3298 const QDomElement childElem = toOgcFilter( args[i] );
3299 if ( !mErrorMessage.isEmpty() )
3300 {
3301 mCurrentSRSName.clear();
3302 return QDomElement();
3303 }
3304
3305 funcElem.appendChild( childElem );
3306 }
3307 mCurrentSRSName.clear();
3308
3309 QgsSQLStatement::Node *distanceNode = args[2];
3310 if ( distanceNode->nodeType() != QgsSQLStatement::ntLiteral )
3311 {
3312 mErrorMessage = QObject::tr( "Function %1 3rd argument should be a numeric value or a string made of a numeric value followed by a string" ).arg( node->name() );
3313 return QDomElement();
3314 }
3315 const QgsSQLStatement::NodeLiteral *lit = static_cast<const QgsSQLStatement::NodeLiteral *>( distanceNode );
3316 if ( QgsVariantUtils::isNull( lit->value() ) )
3317 {
3318 mErrorMessage = QObject::tr( "Function %1 3rd argument should be a numeric value or a string made of a numeric value followed by a string" ).arg( node->name() );
3319 return QDomElement();
3320 }
3321 QString distance;
3322 QString unit( u"m"_s );
3323 switch ( lit->value().userType() )
3324 {
3325 case QMetaType::Type::Int:
3326 distance = QString::number( lit->value().toInt() );
3327 break;
3328 case QMetaType::Type::LongLong:
3329 distance = QString::number( lit->value().toLongLong() );
3330 break;
3331 case QMetaType::Type::Double:
3332 distance = qgsDoubleToString( lit->value().toDouble() );
3333 break;
3334 case QMetaType::Type::QString:
3335 {
3336 distance = lit->value().toString();
3337 for ( int i = 0; i < distance.size(); i++ )
3338 {
3339 if ( !( ( distance[i] >= '0' && distance[i] <= '9' ) || distance[i] == '-' || distance[i] == '.' || distance[i] == 'e' || distance[i] == 'E' ) )
3340 {
3341 unit = distance.mid( i ).trimmed();
3342 distance = distance.mid( 0, i );
3343 break;
3344 }
3345 }
3346 break;
3347 }
3348
3349 default:
3350 mErrorMessage = QObject::tr( "Literal type not supported: %1" ).arg( static_cast<QMetaType::Type>( lit->value().userType() ) );
3351 return QDomElement();
3352 }
3353
3354 QDomElement distanceElem = mDoc.createElement( mFilterPrefix + ":Distance" );
3355 if ( mFilterVersion == QgsOgcUtils::FILTER_FES_2_0 )
3356 distanceElem.setAttribute( u"uom"_s, unit );
3357 else
3358 distanceElem.setAttribute( u"unit"_s, unit );
3359 distanceElem.appendChild( mDoc.createTextNode( distance ) );
3360 funcElem.appendChild( distanceElem );
3361 return funcElem;
3362 }
3363
3364 // Other function
3365 QDomElement funcElem = mDoc.createElement( mFilterPrefix + ":Function" );
3366 funcElem.setAttribute( u"name"_s, node->name() );
3367 const auto constList = node->args()->list();
3368 for ( QgsSQLStatement::Node *n : constList )
3369 {
3370 const QDomElement childElem = toOgcFilter( n );
3371 if ( !mErrorMessage.isEmpty() )
3372 return QDomElement();
3373
3374 funcElem.appendChild( childElem );
3375 }
3376 return funcElem;
3377}
3378
3380 const QString &leftTable )
3381{
3382 QgsSQLStatement::Node *onExpr = node->onExpr();
3383 if ( onExpr )
3384 {
3385 return toOgcFilter( onExpr );
3386 }
3387
3388 QList<QDomElement> listElem;
3389 const auto constUsingColumns = node->usingColumns();
3390 for ( const QString &columnName : constUsingColumns )
3391 {
3392 QDomElement eqElem = mDoc.createElement( mFilterPrefix + ":PropertyIsEqualTo" );
3393 QDomElement propElem1 = mDoc.createElement( mFilterPrefix + ":" + mPropertyName );
3394 propElem1.appendChild( mDoc.createTextNode( leftTable + "/" + columnName ) );
3395 eqElem.appendChild( propElem1 );
3396 QDomElement propElem2 = mDoc.createElement( mFilterPrefix + ":" + mPropertyName );
3397 propElem2.appendChild( mDoc.createTextNode( node->tableDef()->name() + "/" + columnName ) );
3398 eqElem.appendChild( propElem2 );
3399 listElem.append( eqElem );
3400 }
3401
3402 if ( listElem.size() == 1 )
3403 {
3404 return listElem[0];
3405 }
3406 else if ( listElem.size() > 1 )
3407 {
3408 QDomElement andElem = mDoc.createElement( mFilterPrefix + ":And" );
3409 const auto constListElem = listElem;
3410 for ( const QDomElement &elem : constListElem )
3411 {
3412 andElem.appendChild( elem );
3413 }
3414 return andElem;
3415 }
3416
3417 return QDomElement();
3418}
3419
3420void QgsOgcUtilsSQLStatementToFilter::visit( const QgsSQLStatement::NodeTableDef *node )
3421{
3422 if ( node->alias().isEmpty() )
3423 {
3424 mMapTableAliasToNames[ node->name()] = node->name();
3425 }
3426 else
3427 {
3428 mMapTableAliasToNames[ node->alias()] = node->name();
3429 }
3430}
3431
3433{
3434 QList<QDomElement> listElem;
3435
3436 if ( mFilterVersion != QgsOgcUtils::FILTER_FES_2_0 &&
3437 ( node->tables().size() != 1 || !node->joins().empty() ) )
3438 {
3439 mErrorMessage = QObject::tr( "Joins are only supported with WFS 2.0" );
3440 return QDomElement();
3441 }
3442
3443 // Register all table name aliases
3444 const auto constTables = node->tables();
3445 for ( QgsSQLStatement::NodeTableDef *table : constTables )
3446 {
3447 visit( table );
3448 }
3449 const auto constJoins = node->joins();
3450 for ( QgsSQLStatement::NodeJoin *join : constJoins )
3451 {
3452 visit( join->tableDef() );
3453 }
3454
3455 // Process JOIN conditions
3456 const QList< QgsSQLStatement::NodeTableDef *> nodeTables = node->tables();
3457 QString leftTable = nodeTables.at( nodeTables.length() - 1 )->name();
3458 for ( QgsSQLStatement::NodeJoin *join : constJoins )
3459 {
3460 const QDomElement joinElem = toOgcFilter( join, leftTable );
3461 if ( !mErrorMessage.isEmpty() )
3462 return QDomElement();
3463 listElem.append( joinElem );
3464 leftTable = join->tableDef()->name();
3465 }
3466
3467 // Process WHERE conditions
3468 if ( node->where() )
3469 {
3470 const QDomElement whereElem = toOgcFilter( node->where() );
3471 if ( !mErrorMessage.isEmpty() )
3472 return QDomElement();
3473 listElem.append( whereElem );
3474 }
3475
3476 // Concatenate all conditions
3477 if ( listElem.size() == 1 )
3478 {
3479 return listElem[0];
3480 }
3481 else if ( listElem.size() > 1 )
3482 {
3483 QDomElement andElem = mDoc.createElement( mFilterPrefix + ":And" );
3484 const auto constListElem = listElem;
3485 for ( const QDomElement &elem : constListElem )
3486 {
3487 andElem.appendChild( elem );
3488 }
3489 return andElem;
3490 }
3491
3492 return QDomElement();
3493}
3494
3496 : mLayer( layer )
3497{
3498 mPropertyName = u"PropertyName"_s;
3499 mPrefix = u"ogc"_s;
3500
3501 if ( version == QgsOgcUtils::FILTER_FES_2_0 )
3502 {
3503 mPropertyName = u"ValueReference"_s;
3504 mPrefix = u"fes"_s;
3505 }
3506}
3507
3509{
3510 if ( element.isNull() )
3511 return nullptr;
3512
3513 // check for binary operators
3514 if ( isBinaryOperator( element.tagName() ) )
3515 {
3516 return nodeBinaryOperatorFromOgcFilter( element );
3517 }
3518
3519 // check for spatial operators
3520 if ( isSpatialOperator( element.tagName() ) )
3521 {
3522 return nodeSpatialOperatorFromOgcFilter( element );
3523 }
3524
3525 // check for other OGC operators, convert them to expressions
3526 if ( element.tagName() == "Not"_L1 )
3527 {
3528 return nodeNotFromOgcFilter( element );
3529 }
3530 else if ( element.tagName() == "PropertyIsNull"_L1 )
3531 {
3532 return nodePropertyIsNullFromOgcFilter( element );
3533 }
3534 else if ( element.tagName() == "Literal"_L1 )
3535 {
3536 return nodeLiteralFromOgcFilter( element );
3537 }
3538 else if ( element.tagName() == "Function"_L1 )
3539 {
3540 return nodeFunctionFromOgcFilter( element );
3541 }
3542 else if ( element.tagName() == mPropertyName )
3543 {
3544 return nodeColumnRefFromOgcFilter( element );
3545 }
3546 else if ( element.tagName() == "PropertyIsBetween"_L1 )
3547 {
3548 return nodeIsBetweenFromOgcFilter( element );
3549 }
3550
3551 mErrorMessage += QObject::tr( "unable to convert '%1' element to a valid expression: it is not supported yet or it has invalid arguments" ).arg( element.tagName() );
3552 return nullptr;
3553}
3554
3556{
3557 if ( element.isNull() )
3558 return nullptr;
3559
3560 int op = binaryOperatorFromTagName( element.tagName() );
3561 if ( op < 0 )
3562 {
3563 mErrorMessage = QObject::tr( "'%1' binary operator not supported." ).arg( element.tagName() );
3564 return nullptr;
3565 }
3566
3567 if ( op == QgsExpressionNodeBinaryOperator::boLike && element.hasAttribute( u"matchCase"_s ) && element.attribute( u"matchCase"_s ) == "false"_L1 )
3568 {
3570 }
3571
3572 QDomElement operandElem = element.firstChildElement();
3573 std::unique_ptr<QgsExpressionNode> expr( nodeFromOgcFilter( operandElem ) );
3574
3575 if ( !expr )
3576 {
3577 mErrorMessage = QObject::tr( "invalid left operand for '%1' binary operator" ).arg( element.tagName() );
3578 return nullptr;
3579 }
3580
3581 const std::unique_ptr<QgsExpressionNode> leftOp( expr->clone() );
3582 for ( operandElem = operandElem.nextSiblingElement(); !operandElem.isNull(); operandElem = operandElem.nextSiblingElement() )
3583 {
3584 std::unique_ptr<QgsExpressionNode> opRight( nodeFromOgcFilter( operandElem ) );
3585 if ( !opRight )
3586 {
3587 mErrorMessage = QObject::tr( "invalid right operand for '%1' binary operator" ).arg( element.tagName() );
3588 return nullptr;
3589 }
3590
3592 {
3593 QString wildCard;
3594 if ( element.hasAttribute( u"wildCard"_s ) )
3595 {
3596 wildCard = element.attribute( u"wildCard"_s );
3597 }
3598 QString singleChar;
3599 if ( element.hasAttribute( u"singleChar"_s ) )
3600 {
3601 singleChar = element.attribute( u"singleChar"_s );
3602 }
3603 QString escape = u"\\"_s;
3604 if ( element.hasAttribute( u"escape"_s ) )
3605 {
3606 escape = element.attribute( u"escape"_s );
3607 }
3608 if ( element.hasAttribute( u"escapeChar"_s ) )
3609 {
3610 escape = element.attribute( u"escapeChar"_s );
3611 }
3612 // replace
3613 QString oprValue = static_cast<const QgsExpressionNodeLiteral *>( opRight.get() )->value().toString();
3614 if ( !wildCard.isEmpty() && wildCard != "%"_L1 )
3615 {
3616 oprValue.replace( '%', "\\%"_L1 );
3617 if ( oprValue.startsWith( wildCard ) )
3618 {
3619 oprValue.replace( 0, 1, u"%"_s );
3620 }
3621 const QRegularExpression rx( "[^" + QgsStringUtils::qRegExpEscape( escape ) + "](" + QgsStringUtils::qRegExpEscape( wildCard ) + ")" );
3622 QRegularExpressionMatch match = rx.match( oprValue );
3623 int pos;
3624 while ( match.hasMatch() )
3625 {
3626 pos = match.capturedStart();
3627 oprValue.replace( pos + 1, 1, u"%"_s );
3628 pos += 1;
3629 match = rx.match( oprValue, pos );
3630 }
3631 oprValue.replace( escape + wildCard, wildCard );
3632 }
3633 if ( !singleChar.isEmpty() && singleChar != "_"_L1 )
3634 {
3635 oprValue.replace( '_', "\\_"_L1 );
3636 if ( oprValue.startsWith( singleChar ) )
3637 {
3638 oprValue.replace( 0, 1, u"_"_s );
3639 }
3640 const QRegularExpression rx( "[^" + QgsStringUtils::qRegExpEscape( escape ) + "](" + QgsStringUtils::qRegExpEscape( singleChar ) + ")" );
3641 QRegularExpressionMatch match = rx.match( oprValue );
3642 int pos;
3643 while ( match.hasMatch() )
3644 {
3645 pos = match.capturedStart();
3646 oprValue.replace( pos + 1, 1, u"_"_s );
3647 pos += 1;
3648 match = rx.match( oprValue, pos );
3649 }
3650 oprValue.replace( escape + singleChar, singleChar );
3651 }
3652 if ( !escape.isEmpty() && escape != "\\"_L1 )
3653 {
3654 oprValue.replace( escape + escape, escape );
3655 }
3656 opRight = std::make_unique<QgsExpressionNodeLiteral>( oprValue );
3657 }
3658
3659 expr = std::make_unique<QgsExpressionNodeBinaryOperator>( static_cast< QgsExpressionNodeBinaryOperator::BinaryOperator >( op ), expr.release(), opRight.release() );
3660 }
3661
3662 if ( expr == leftOp )
3663 {
3664 mErrorMessage = QObject::tr( "only one operand for '%1' binary operator" ).arg( element.tagName() );
3665 return nullptr;
3666 }
3667
3668 return dynamic_cast< QgsExpressionNodeBinaryOperator * >( expr.release() );
3669}
3670
3671
3673{
3674 // we are exploiting the fact that our function names are the same as the XML tag names
3675 const int opIdx = QgsExpression::functionIndex( element.tagName().toLower() );
3676
3677 auto gml2Args = std::make_unique<QgsExpressionNode::NodeList>();
3678 QDomElement childElem = element.firstChildElement();
3679 QString gml2Str;
3680 while ( !childElem.isNull() && gml2Str.isEmpty() )
3681 {
3682 if ( childElem.tagName() != mPropertyName )
3683 {
3684 QTextStream gml2Stream( &gml2Str );
3685 childElem.save( gml2Stream, 0 );
3686 }
3687 childElem = childElem.nextSiblingElement();
3688 }
3689 if ( !gml2Str.isEmpty() )
3690 {
3691 gml2Args->append( new QgsExpressionNodeLiteral( QVariant( gml2Str.remove( '\n' ) ) ) );
3692 }
3693 else
3694 {
3695 mErrorMessage = QObject::tr( "No OGC Geometry found" );
3696 return nullptr;
3697 }
3698
3699 auto opArgs = std::make_unique<QgsExpressionNode::NodeList>();
3700 opArgs->append( new QgsExpressionNodeFunction( QgsExpression::functionIndex( u"$geometry"_s ), new QgsExpressionNode::NodeList() ) );
3701 opArgs->append( new QgsExpressionNodeFunction( QgsExpression::functionIndex( u"geomFromGML"_s ), gml2Args.release() ) );
3702
3703 return new QgsExpressionNodeFunction( opIdx, opArgs.release() );
3704}
3705
3707{
3708 if ( element.isNull() || element.tagName() != mPropertyName )
3709 {
3710 mErrorMessage = QObject::tr( "%1:PropertyName expected, got %2" ).arg( mPrefix, element.tagName() );
3711 return nullptr;
3712 }
3713
3714 return new QgsExpressionNodeColumnRef( element.firstChild().nodeValue() );
3715}
3716
3718{
3719 if ( element.isNull() || element.tagName() != "Literal"_L1 )
3720 {
3721 mErrorMessage = QObject::tr( "%1:Literal expected, got %2" ).arg( mPrefix, element.tagName() );
3722 return nullptr;
3723 }
3724
3725 std::unique_ptr<QgsExpressionNode> root;
3726 if ( !element.hasChildNodes() )
3727 {
3728 root = std::make_unique<QgsExpressionNodeLiteral>( QVariant( "" ) );
3729 return root.release();
3730 }
3731
3732 // the literal content can have more children (e.g. CDATA section, text, ...)
3733 QDomNode childNode = element.firstChild();
3734 while ( !childNode.isNull() )
3735 {
3736 std::unique_ptr<QgsExpressionNode> operand;
3737
3738 if ( childNode.nodeType() == QDomNode::ElementNode )
3739 {
3740 // found a element node (e.g. PropertyName), convert it
3741 const QDomElement operandElem = childNode.toElement();
3742 operand.reset( nodeFromOgcFilter( operandElem ) );
3743 if ( !operand )
3744 {
3745 mErrorMessage = QObject::tr( "'%1' is an invalid or not supported content for %2:Literal" ).arg( operandElem.tagName(), mPrefix );
3746 return nullptr;
3747 }
3748 }
3749 else
3750 {
3751 // probably a text/CDATA node
3752 QVariant value = childNode.nodeValue();
3753
3754 bool converted = false;
3755
3756 // try to convert the node content to corresponding field type if possible
3757 if ( mLayer )
3758 {
3759 QDomElement propertyNameElement = element.previousSiblingElement( mPropertyName );
3760 if ( propertyNameElement.isNull() || propertyNameElement.tagName() != mPropertyName )
3761 {
3762 propertyNameElement = element.nextSiblingElement( mPropertyName );
3763 }
3764 if ( !propertyNameElement.isNull() || propertyNameElement.tagName() == mPropertyName )
3765 {
3766 const int fieldIndex = mLayer->fields().indexOf( propertyNameElement.firstChild().nodeValue() );
3767 if ( fieldIndex != -1 )
3768 {
3769 const QgsField field = mLayer->fields().field( propertyNameElement.firstChild().nodeValue() );
3770 field.convertCompatible( value );
3771 converted = true;
3772 }
3773 }
3774 }
3775 if ( !converted )
3776 {
3777 // try to convert the node content to number if possible,
3778 // otherwise let's use it as string
3779 bool ok;
3780 const double d = value.toDouble( &ok );
3781 if ( ok )
3782 value = d;
3783 }
3784
3785 operand = std::make_unique<QgsExpressionNodeLiteral>( value );
3786 }
3787
3788 // use the concat operator to merge the ogc:Literal children
3789 if ( !root )
3790 {
3791 root = std::move( operand );
3792 }
3793 else
3794 {
3795 root = std::make_unique<QgsExpressionNodeBinaryOperator>( QgsExpressionNodeBinaryOperator::boConcat, root.release(), operand.release() );
3796 }
3797
3798 childNode = childNode.nextSibling();
3799 }
3800
3801 if ( root )
3802 return root.release();
3803
3804 return nullptr;
3805}
3806
3808{
3809 if ( element.tagName() != "Not"_L1 )
3810 return nullptr;
3811
3812 const QDomElement operandElem = element.firstChildElement();
3813 std::unique_ptr<QgsExpressionNode> operand( nodeFromOgcFilter( operandElem ) );
3814 if ( !operand )
3815 {
3816 mErrorMessage = QObject::tr( "invalid operand for '%1' unary operator" ).arg( element.tagName() );
3817 return nullptr;
3818 }
3819
3821}
3822
3824{
3825 // convert ogc:PropertyIsNull to IS operator with NULL right operand
3826 if ( element.tagName() != "PropertyIsNull"_L1 )
3827 {
3828 return nullptr;
3829 }
3830
3831 const QDomElement operandElem = element.firstChildElement();
3832 std::unique_ptr<QgsExpressionNode> opLeft( nodeFromOgcFilter( operandElem ) );
3833 if ( !opLeft )
3834 return nullptr;
3835
3836 std::unique_ptr<QgsExpressionNode> opRight( new QgsExpressionNodeLiteral( QVariant() ) );
3837 return new QgsExpressionNodeBinaryOperator( QgsExpressionNodeBinaryOperator::boIs, opLeft.release(), opRight.release() );
3838}
3839
3841{
3842 if ( element.isNull() || element.tagName() != "Function"_L1 )
3843 {
3844 mErrorMessage = QObject::tr( "%1:Function expected, got %2" ).arg( mPrefix, element.tagName() );
3845 return nullptr;
3846 }
3847
3848 for ( int i = 0; i < QgsExpression::Functions().size(); i++ )
3849 {
3850 const QgsExpressionFunction *funcDef = QgsExpression::Functions()[i];
3851
3852 if ( element.attribute( u"name"_s ) != funcDef->name() )
3853 continue;
3854
3855 auto args = std::make_unique<QgsExpressionNode::NodeList>();
3856
3857 QDomElement operandElem = element.firstChildElement();
3858 while ( !operandElem.isNull() )
3859 {
3860 std::unique_ptr<QgsExpressionNode> op( nodeFromOgcFilter( operandElem ) );
3861 if ( !op )
3862 {
3863 return nullptr;
3864 }
3865 args->append( op.release() );
3866
3867 operandElem = operandElem.nextSiblingElement();
3868 }
3869
3870 return new QgsExpressionNodeFunction( i, args.release() );
3871 }
3872
3873 return nullptr;
3874}
3875
3877{
3878 // <ogc:PropertyIsBetween> encode a Range check
3879 std::unique_ptr<QgsExpressionNode> operand;
3880 std::unique_ptr<QgsExpressionNode> lowerBound;
3881 std::unique_ptr<QgsExpressionNode> upperBound;
3882
3883 QDomElement operandElem = element.firstChildElement();
3884 while ( !operandElem.isNull() )
3885 {
3886 if ( operandElem.tagName() == "LowerBoundary"_L1 )
3887 {
3888 const QDomElement lowerBoundElem = operandElem.firstChildElement();
3889 lowerBound.reset( nodeFromOgcFilter( lowerBoundElem ) );
3890 }
3891 else if ( operandElem.tagName() == "UpperBoundary"_L1 )
3892 {
3893 const QDomElement upperBoundElem = operandElem.firstChildElement();
3894 upperBound.reset( nodeFromOgcFilter( upperBoundElem ) );
3895 }
3896 else
3897 {
3898 // <ogc:expression>
3899 operand.reset( nodeFromOgcFilter( operandElem ) );
3900 }
3901
3902 if ( operand && lowerBound && upperBound )
3903 break;
3904
3905 operandElem = operandElem.nextSiblingElement();
3906 }
3907
3908 if ( !operand || !lowerBound || !upperBound )
3909 {
3910 mErrorMessage = QObject::tr( "missing some required sub-elements in %1:PropertyIsBetween" ).arg( mPrefix );
3911 return nullptr;
3912 }
3913
3914 std::unique_ptr<QgsExpressionNode> leOperator( new QgsExpressionNodeBinaryOperator( QgsExpressionNodeBinaryOperator::boLE, operand->clone(), upperBound.release() ) );
3915 std::unique_ptr<QgsExpressionNode> geOperator( new QgsExpressionNodeBinaryOperator( QgsExpressionNodeBinaryOperator::boGE, operand.release(), lowerBound.release() ) );
3916 return new QgsExpressionNodeBinaryOperator( QgsExpressionNodeBinaryOperator::boAnd, geOperator.release(), leOperator.release() );
3917}
3918
3920{
3921 return mErrorMessage;
3922}
3923
3924QgsOgcCrsUtils::CRSFlavor QgsOgcCrsUtils::parseCrsName( const QString &crsName, QString &authority, QString &code )
3925{
3926 const thread_local QRegularExpression re_url( QRegularExpression::anchoredPattern( u"http://www\\.opengis\\.net/gml/srs/epsg\\.xml#(.+)"_s ), QRegularExpression::CaseInsensitiveOption );
3927 if ( const QRegularExpressionMatch match = re_url.match( crsName ); match.hasMatch() )
3928 {
3929 authority = u"EPSG"_s;
3930 code = match.captured( 1 );
3932 }
3933
3934 const thread_local QRegularExpression re_ogc_urn( QRegularExpression::anchoredPattern( u"urn:ogc:def:crs:([^:]+).+(?<=:)([^:]+)"_s ), QRegularExpression::CaseInsensitiveOption );
3935 if ( const QRegularExpressionMatch match = re_ogc_urn.match( crsName ); match.hasMatch() )
3936 {
3937 authority = match.captured( 1 );
3938 code = match.captured( 2 );
3939 return CRSFlavor::OGC_URN;
3940 }
3941
3942 const thread_local QRegularExpression re_x_ogc_urn( QRegularExpression::anchoredPattern( u"urn:x-ogc:def:crs:([^:]+).+(?<=:)([^:]+)"_s ), QRegularExpression::CaseInsensitiveOption );
3943 if ( const QRegularExpressionMatch match = re_x_ogc_urn.match( crsName ); match.hasMatch() )
3944 {
3945 authority = match.captured( 1 );
3946 code = match.captured( 2 );
3947 return CRSFlavor::X_OGC_URN;
3948 }
3949
3950 const thread_local QRegularExpression re_http_uri( QRegularExpression::anchoredPattern( u"http://www\\.opengis\\.net/def/crs/([^/]+).+/([^/]+)"_s ), QRegularExpression::CaseInsensitiveOption );
3951 if ( const QRegularExpressionMatch match = re_http_uri.match( crsName ); match.hasMatch() )
3952 {
3953 authority = match.captured( 1 );
3954 code = match.captured( 2 );
3956 }
3957
3958 const thread_local QRegularExpression re_auth_code( QRegularExpression::anchoredPattern( u"([^:]+):(.+)"_s ), QRegularExpression::CaseInsensitiveOption );
3959 if ( const QRegularExpressionMatch match = re_auth_code.match( crsName ); match.hasMatch() )
3960 {
3961 authority = match.captured( 1 );
3962 code = match.captured( 2 );
3963 return CRSFlavor::AUTH_CODE;
3964 }
3965
3966 return CRSFlavor::UNKNOWN;
3967}
3968
3969QgsGeometry QgsOgcUtils::geometryFromGMLUsingGdal( const QDomElement &geometryElement )
3970{
3971 QString gml;
3972 QTextStream gmlStream( &gml );
3973 geometryElement.save( gmlStream, 0 );
3974 gdal::ogr_geometry_unique_ptr ogrGeom { OGR_G_CreateFromGML( gml.toUtf8().constData() ) };
3975 return QgsOgrUtils::ogrGeometryToQgsGeometry( ogrGeom.get() );
3976}
3977
3978QgsGeometry QgsOgcUtils::geometryFromGMLMultiCurve( const QDomElement &geometryElement )
3979{
3980 return geometryFromGMLUsingGdal( geometryElement );
3981}
GeometryOperationResult
Success or failure of a geometry operation.
Definition qgis.h:2100
@ Success
Operation succeeded.
Definition qgis.h:2101
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:280
@ LineString25D
LineString25D.
Definition qgis.h:348
@ MultiPointZ
MultiPointZ.
Definition qgis.h:303
@ Point
Point.
Definition qgis.h:282
@ LineString
LineString.
Definition qgis.h:283
@ MultiPolygon25D
MultiPolygon25D.
Definition qgis.h:352
@ MultiPoint
MultiPoint.
Definition qgis.h:286
@ Polygon
Polygon.
Definition qgis.h:284
@ MultiLineString25D
MultiLineString25D.
Definition qgis.h:351
@ MultiPolygon
MultiPolygon.
Definition qgis.h:288
@ MultiLineString
MultiLineString.
Definition qgis.h:287
@ MultiPoint25D
MultiPoint25D.
Definition qgis.h:350
@ Unknown
Unknown.
Definition qgis.h:281
@ PointZ
PointZ.
Definition qgis.h:299
@ MultiLineStringZ
MultiLineStringZ.
Definition qgis.h:304
@ MultiPolygonZ
MultiPolygonZ.
Definition qgis.h:305
@ Point25D
Point25D.
Definition qgis.h:347
@ LineStringZ
LineStringZ.
Definition qgis.h:300
@ PolygonZ
PolygonZ.
Definition qgis.h:301
@ Polygon25D
Polygon25D.
Definition qgis.h:349
virtual void swapXy()=0
Swaps the x and y coordinates from the geometry.
A const WKB pointer.
Definition qgswkbptr.h:139
Qgis::WkbType readHeader() const
readHeader
Definition qgswkbptr.cpp:60
Represents a coordinate reference system (CRS).
static QgsCoordinateReferenceSystem fromOgcWmsCrs(const QString &ogcCrs)
Creates a CRS from a given OGC WMS-format Coordinate Reference System string.
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
bool createFromUserInput(const QString &definition)
Set up this CRS from various text formats.
bool hasAxisInverted() const
Returns whether the axis order is inverted for the CRS compared to the order east/north (longitude/la...
Handles coordinate transforms between two coordinate systems.
Custom exception class for Coordinate Reference System related exceptions.
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
An abstract base class for defining QgsExpression functions.
int params() const
The number of parameters this function takes.
virtual bool isStatic(const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context) const
Will be called during prepare to determine if the function is static.
QString name() const
The name of the function.
virtual QVariant run(QgsExpressionNode::NodeList *args, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node)
Evaluates the function, first evaluating all required arguments before passing them to the function's...
A binary expression operator, which operates on two values.
QgsExpressionNode * opLeft() const
Returns the node to the left of the operator.
QgsExpressionNode * opRight() const
Returns the node to the right of the operator.
QgsExpressionNodeBinaryOperator::BinaryOperator op() const
Returns the binary operator.
QString text() const
Returns a the name of this operator without the operands.
An expression node which takes its value from a feature's field.
QString name() const
The name of the column.
An expression node for expression functions.
int fnIndex() const
Returns the index of the node's function.
QgsExpressionNode::NodeList * args() const
Returns a list of arguments specified for the function.
QSet< QString > referencedVariables() const override
Returns a set of all variables which are used in this expression.
An expression node for value IN or NOT IN clauses.
QgsExpressionNode * node() const
Returns the expression node.
QgsExpressionNode::NodeList * list() const
Returns the list of nodes to search for matching values within.
bool isNotIn() const
Returns true if this node is a "NOT IN" operator, or false if the node is a normal "IN" operator.
An expression node for literal values.
QVariant value() const
The value of the literal.
A unary node is either negative as in boolean (not) or as in numbers (minus).
QgsExpressionNodeUnaryOperator::UnaryOperator op() const
Returns the unary operator.
QString text() const
Returns a the name of this operator without the operands.
QgsExpressionNode * operand() const
Returns the node the operator will operate upon.
A list of expression nodes.
QList< QgsExpressionNode * > list()
Gets a list of all the nodes.
Abstract base class for all nodes that can appear in an expression.
bool hasCachedStaticValue() const
Returns true if the node can be replaced by a static cached value.
virtual QgsExpressionNode::NodeType nodeType() const =0
Gets the type of this node.
QVariant cachedStaticValue() const
Returns the node's static cached value.
Handles parsing and evaluation of expressions (formerly called "search strings").
static const QList< QgsExpressionFunction * > & Functions()
void setExpression(const QString &expression)
Set the expression string, will reset the whole internal structure.
static int functionIndex(const QString &name)
Returns index of the function in Functions array.
QString dump() const
Returns an expression string, constructed from the internal abstract syntax tree.
const QgsExpressionNode * rootNode() const
Returns the root node of the expression.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
bool convertCompatible(QVariant &v, QString *errorMessage=nullptr) const
Converts the provided variant to a compatible format.
Definition qgsfield.cpp:479
A geometry is the spatial representation of a feature.
static QgsGeometry fromRect(const QgsRectangle &rect)
Creates a new geometry from a QgsRectangle.
Qgis::GeometryOperationResult transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool transformZ=false)
Transforms this geometry as described by the coordinate transform ct.
QgsAbstractGeometry * get()
Returns a modifiable (non-const) reference to the underlying abstract geometry primitive.
static Q_INVOKABLE QgsGeometry fromWkt(const QString &wkt)
Creates a new geometry from a WKT string.
void fromWkb(unsigned char *wkb, int length)
Set the geometry, feeding in the buffer containing OGC Well-Known Binary and the buffer's length.
QByteArray asWkb(QgsAbstractGeometry::WkbFlags flags=QgsAbstractGeometry::WkbFlags()) const
Export the geometry to WKB.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
Qgis::WkbType wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.).
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:90
CRSFlavor
CRS flavor.
@ HTTP_EPSG_DOT_XML
E.g. http://www.opengis.net/gml/srs/epsg.xml#4326 (called "OGC HTTP URL" in GeoServer WFS configurati...
@ OGC_HTTP_URI
E.g. http://www.opengis.net/def/crs/EPSG/0/4326.
@ X_OGC_URN
E.g. urn:x-ogc:def:crs:EPSG::4326.
@ UNKNOWN
Unknown/unhandled flavor.
@ OGC_URN
E.g. urn:ogc:def:crs:EPSG::4326.
@ AUTH_CODE
E.g EPSG:4326.
static CRSFlavor parseCrsName(const QString &crsName, QString &authority, QString &code)
Parse a CRS name in one of the flavors of OGC services, and decompose it as authority and code.
Internal use by QgsOgcUtils.
QgsOgcUtilsExprToFilter(QDomDocument &doc, QgsOgcUtils::GMLVersion gmlVersion, QgsOgcUtils::FilterVersion filterVersion, const QString &namespacePrefix, const QString &namespaceURI, const QString &geometryName, const QString &srsName, bool honourAxisOrientation, bool invertAxisOrientation, const QMap< QString, QString > &fieldNameToXPathMap, const QMap< QString, QString > &namespacePrefixToUriMap)
Constructor.
bool GMLNamespaceUsed() const
Returns whether the gml: namespace is used.
QDomElement expressionNodeToOgcFilter(const QgsExpressionNode *node, QgsExpression *expression, const QgsExpressionContext *context)
Convert an expression to a OGC filter.
QString errorMessage() const
Returns the error message.
Internal use by QgsOgcUtils.
QgsExpressionNodeFunction * nodeSpatialOperatorFromOgcFilter(const QDomElement &element)
Returns an expression node from a WFS filter embedded in a document with spatial operators.
QgsExpressionNodeUnaryOperator * nodeNotFromOgcFilter(const QDomElement &element)
Returns an expression node from a WFS filter embedded in a document with Not operator.
QgsExpressionNodeColumnRef * nodeColumnRefFromOgcFilter(const QDomElement &element)
Returns an expression node from a WFS filter embedded in a document with column references.
QgsExpressionNode * nodeIsBetweenFromOgcFilter(const QDomElement &element)
Returns an expression node from a WFS filter embedded in a document with boundaries operator.
QgsOgcUtilsExpressionFromFilter(QgsOgcUtils::FilterVersion version=QgsOgcUtils::FILTER_OGC_1_0, const QgsVectorLayer *layer=nullptr)
Constructor for QgsOgcUtilsExpressionFromFilter.
QgsExpressionNodeBinaryOperator * nodeBinaryOperatorFromOgcFilter(const QDomElement &element)
Returns an expression node from a WFS filter embedded in a document with binary operators.
QgsExpressionNodeFunction * nodeFunctionFromOgcFilter(const QDomElement &element)
Returns an expression node from a WFS filter embedded in a document with functions.
QgsExpressionNode * nodeFromOgcFilter(const QDomElement &element)
Returns an expression node from a WFS filter embedded in a document element.
QgsExpressionNodeBinaryOperator * nodePropertyIsNullFromOgcFilter(const QDomElement &element)
Returns an expression node from a WFS filter embedded in a document with IsNull operator.
QString errorMessage() const
Returns the underlying error message, or an empty string in case of no error.
QgsExpressionNode * nodeLiteralFromOgcFilter(const QDomElement &element)
Returns an expression node from a WFS filter embedded in a document with literal tag.
Internal use by QgsOgcUtils.
QgsOgcUtilsSQLStatementToFilter(QDomDocument &doc, QgsOgcUtils::GMLVersion gmlVersion, QgsOgcUtils::FilterVersion filterVersion, const QList< QgsOgcUtils::LayerProperties > &layerProperties, bool honourAxisOrientation, bool invertAxisOrientation, const QMap< QString, QString > &mapUnprefixedTypenameToPrefixedTypename, const QMap< QString, QString > &fieldNameToXPathMap, const QMap< QString, QString > &namespacePrefixToUriMap)
Constructor.
QDomElement toOgcFilter(const QgsSQLStatement::Node *node)
Convert a SQL statement to a OGC filter.
bool GMLNamespaceUsed() const
Returns whether the gml: namespace is used.
QString errorMessage() const
Returns the error message.
Provides various utility functions for conversion between OGC (Open Geospatial Consortium) standards ...
Definition qgsogcutils.h:56
static QDomElement elseFilterExpression(QDomDocument &doc)
Creates an ElseFilter from doc.
static QgsRectangle rectangleFromGMLBox(const QDomNode &boxNode)
Read rectangle from GML2 Box.
static QDomElement expressionToOgcExpression(const QgsExpression &exp, QDomDocument &doc, QString *errorMessage=nullptr, bool requiresFilterElement=false)
Creates an OGC expression XML element from the exp expression with default values for the geometry na...
static QColor colorFromOgcFill(const QDomElement &fillElement)
Parse XML with OGC fill into QColor.
static QDomElement expressionToOgcFilter(const QgsExpression &exp, QDomDocument &doc, QString *errorMessage=nullptr)
Creates OGC filter XML element.
GMLVersion
GML version.
Definition qgsogcutils.h:82
FilterVersion
OGC filter version.
static QDomElement SQLStatementToOgcFilter(const QgsSQLStatement &statement, QDomDocument &doc, QgsOgcUtils::GMLVersion gmlVersion, FilterVersion filterVersion, const QList< LayerProperties > &layerProperties, bool honourAxisOrientation, bool invertAxisOrientation, const QMap< QString, QString > &mapUnprefixedTypenameToPrefixedTypename, QString *errorMessage=nullptr, const QMap< QString, QString > &fieldNameToXPathMap=QMap< QString, QString >(), const QMap< QString, QString > &namespacePrefixToUriMap=QMap< QString, QString >())
Creates OGC filter XML element from the WHERE and JOIN clauses of a SQL statement.
static QDomElement rectangleToGMLEnvelope(const QgsRectangle *env, QDomDocument &doc, int precision=17)
Exports the rectangle to GML3 Envelope.
static QgsRectangle rectangleFromGMLEnvelope(const QDomNode &envelopeNode)
Read rectangle from GML3 Envelope.
static QDomElement geometryToGML(const QgsGeometry &geometry, QDomDocument &doc, QgsOgcUtils::GMLVersion gmlVersion, const QString &srsName, bool invertAxisOrientation, const QString &gmlIdBase, int precision=17)
Exports the geometry to GML.
static QgsGeometry geometryFromGML(const QString &xmlString, const QgsOgcUtils::Context &context=QgsOgcUtils::Context())
Static method that creates geometry from GML.
static Qgis::WkbType geomTypeFromPropertyType(const QString &gmlGeomType)
Returns the Qgis::WkbType corresponding to a GML geometry type.
static QgsExpression * expressionFromOgcFilter(const QDomElement &element, QgsVectorLayer *layer=nullptr)
Parse XML with OGC filter into QGIS expression.
static QDomElement rectangleToGMLBox(const QgsRectangle *box, QDomDocument &doc, int precision=17)
Exports the rectangle to GML2 Box.
static QgsGeometry ogrGeometryToQgsGeometry(OGRGeometryH geom)
Converts an OGR geometry representation to a QgsGeometry object.
A rectangle specified with double values.
double xMinimum
double yMinimum
double xMaximum
void setYMinimum(double y)
Set the minimum y value.
void setXMinimum(double x)
Set the minimum x value.
void setYMaximum(double y)
Set the maximum y value.
void setXMaximum(double x)
Set the maximum x value.
double yMaximum
void normalize()
Normalize the rectangle so it has non-negative width/height.
An 'X BETWEEN y and z' operator.
QgsSQLStatement::Node * node() const
Variable at the left of BETWEEN.
QgsSQLStatement::Node * minVal() const
Minimum bound.
bool isNotBetween() const
Whether this is a NOT BETWEEN operator.
QgsSQLStatement::Node * maxVal() const
Maximum bound.
Binary logical/arithmetical operator (AND, OR, =, +, ...).
QgsSQLStatement::Node * opLeft() const
Left operand.
QgsSQLStatement::BinaryOperator op() const
Operator.
QgsSQLStatement::Node * opRight() const
Right operand.
QString name() const
The name of the column.
QString tableName() const
The name of the table. May be empty.
Function with a name and arguments node.
QgsSQLStatement::NodeList * args() const
Returns arguments.
QString name() const
Returns function name.
An 'x IN (y, z)' operator.
bool isNotIn() const
Whether this is a NOT IN operator.
QgsSQLStatement::Node * node() const
Variable at the left of IN.
QgsSQLStatement::NodeList * list() const
Values list.
QgsSQLStatement::NodeTableDef * tableDef() const
Table definition.
QgsSQLStatement::Node * onExpr() const
On expression. Will be nullptr if usingColumns() is not empty.
QList< QString > usingColumns() const
Columns referenced by USING.
QList< QgsSQLStatement::Node * > list()
Returns list.
Literal value (integer, integer64, double, string).
QVariant value() const
The value of the literal.
QList< QgsSQLStatement::NodeJoin * > joins() const
Returns the list of joins.
QgsSQLStatement::Node * where() const
Returns the where clause.
QList< QgsSQLStatement::NodeTableDef * > tables() const
Returns the list of tables.
QString name() const
Table name.
QString alias() const
Table alias.
Unary logical/arithmetical operator ( NOT, - ).
QgsSQLStatement::UnaryOperator op() const
Operator.
QgsSQLStatement::Node * operand() const
Operand.
Abstract node class for SQL statement nodes.
virtual QgsSQLStatement::NodeType nodeType() const =0
Abstract virtual that returns the type of this node.
Parses SQL statements.
BinaryOperator
list of binary operators
static const char * BINARY_OPERATOR_TEXT[]
const QgsSQLStatement::Node * rootNode() const
Returns the root node of the statement.
static const char * UNARY_OPERATOR_TEXT[]
static QString qRegExpEscape(const QString &string)
Returns an escaped string matching the behavior of QRegExp::escape.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
Represents a vector layer which manages a vector based dataset.
Custom exception class for Wkb related exceptions.
Definition qgswkbptr.h:32
std::unique_ptr< std::remove_pointer< OGRGeometryH >::type, OGRGeometryDeleter > ogr_geometry_unique_ptr
Scoped OGR geometry.
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:6817
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:7091
QMap< QString, QString > QgsStringMap
Definition qgis.h:7413
QVector< QgsPointXY > QgsPolylineXY
Polyline as represented as a vector of two-dimensional points.
Definition qgsgeometry.h:63
QVector< QgsPolyline > QgsMultiPolyline
Multi polyline represented as a vector of polylines.
Definition qgsgeometry.h:85
QgsPointSequence QgsPolyline
Polyline as represented as a vector of points.
Definition qgsgeometry.h:72
#define GML_NAMESPACE
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:63
#define GML32_NAMESPACE
#define SE_NAMESPACE
#define FES_NAMESPACE
#define OGC_NAMESPACE
Q_GLOBAL_STATIC_WITH_ARGS(IntMap, BINARY_OPERATORS_TAG_NAMES_MAP,({ { "Or"_L1, QgsExpressionNodeBinaryOperator::boOr }, { "And"_L1, QgsExpressionNodeBinaryOperator::boAnd }, { "PropertyIsEqualTo"_L1, QgsExpressionNodeBinaryOperator::boEQ }, { "PropertyIsNotEqualTo"_L1, QgsExpressionNodeBinaryOperator::boNE }, { "PropertyIsLessThanOrEqualTo"_L1, QgsExpressionNodeBinaryOperator::boLE }, { "PropertyIsGreaterThanOrEqualTo"_L1, QgsExpressionNodeBinaryOperator::boGE }, { "PropertyIsLessThan"_L1, QgsExpressionNodeBinaryOperator::boLT }, { "PropertyIsGreaterThan"_L1, QgsExpressionNodeBinaryOperator::boGT }, { "PropertyIsLike"_L1, QgsExpressionNodeBinaryOperator::boLike }, { "Add"_L1, QgsExpressionNodeBinaryOperator::boPlus }, { "Sub"_L1, QgsExpressionNodeBinaryOperator::boMinus }, { "Mul"_L1, QgsExpressionNodeBinaryOperator::boMul }, { "Div"_L1, QgsExpressionNodeBinaryOperator::boDiv }, })) static int binaryOperatorFromTagName(const QString &tagName)
QMap< QString, int > IntMap
The Context struct stores the current layer and coordinate transform context.
Definition qgsogcutils.h:64
const QgsMapLayer * layer
Definition qgsogcutils.h:74
QgsCoordinateTransformContext transformContext
Definition qgsogcutils.h:75