38#include <QRegularExpression>
43using namespace Qt::StringLiterals;
46#include <netinet/in.h>
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
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 )
70 , mGMLVersion( gmlVersion )
71 , mFilterVersion( filterVersion )
72 , mNamespacePrefix( namespacePrefix )
73 , mNamespaceURI( namespaceURI )
74 , mGeometryName( geometryName )
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" )
83 if ( !mSrsName.isEmpty() )
87 if ( honourAxisOrientation && crs.hasAxisInverted() )
89 mInvertAxisOrientation = !mInvertAxisOrientation;
96 QDomElement geometryTypeElement = geometryNode.toElement();
97 QString geomType = geometryTypeElement.tagName();
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 ) )
104 const QDomNode geometryChild = geometryNode.firstChild();
105 if ( geometryChild.isNull() )
109 geometryTypeElement = geometryChild.toElement();
110 geomType = geometryTypeElement.tagName();
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 ) )
118 if ( geomType ==
"Point"_L1 )
120 geometry = geometryFromGMLPoint( geometryTypeElement );
122 else if ( geomType ==
"LineString"_L1 )
124 geometry = geometryFromGMLLineString( geometryTypeElement );
126 else if ( geomType ==
"Polygon"_L1 )
128 geometry = geometryFromGMLPolygon( geometryTypeElement );
130 else if ( geomType ==
"MultiPoint"_L1 )
132 geometry = geometryFromGMLMultiPoint( geometryTypeElement );
134 else if ( geomType ==
"MultiLineString"_L1 )
136 geometry = geometryFromGMLMultiLineString( geometryTypeElement );
138 else if ( geomType ==
"MultiCurve"_L1 )
140 geometry = geometryFromGMLMultiCurve( geometryTypeElement );
142 else if ( geomType ==
"MultiPolygon"_L1 )
144 geometry = geometryFromGMLMultiPolygon( geometryTypeElement );
146 else if ( geomType ==
"Box"_L1 )
150 else if ( geomType ==
"Envelope"_L1 )
163 if ( geometryTypeElement.hasAttribute( u
"srsName"_s ) )
165 QString srsName { geometryTypeElement.attribute( u
"srsName"_s ) };
168 const bool ignoreAxisOrientation { srsName.startsWith(
"http://www.opengis.net/gml/srs/"_L1 ) || srsName.startsWith(
"EPSG:"_L1 ) };
172 if ( srsName.startsWith(
"http://www.opengis.net/gml/srs/"_L1 ) )
174 const auto parts { srsName.split( QRegularExpression( QStringLiteral( R
"raw(/|#|\.)raw" ) ) ) };
175 if ( parts.length() == 10 )
177 srsName = u
"http://www.opengis.net/def/crs/%1/0/%2"_s.arg( parts[ 7 ].toUpper(), parts[ 9 ] );
211 const QString xml = u
"<tmp xmlns=\"%1\" xmlns:gml=\"%1\">%2</tmp>"_s.arg(
GML_NAMESPACE, xmlString );
213 if ( !doc.setContent( xml,
true ) )
216 return geometryFromGML( doc.documentElement().firstChildElement(), context );
220QgsGeometry QgsOgcUtils::geometryFromGMLPoint(
const QDomElement &geometryElement )
224 const QDomNodeList coordList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, u
"coordinates"_s );
225 if ( !coordList.isEmpty() )
227 const QDomElement coordElement = coordList.at( 0 ).toElement();
228 if ( readGMLCoordinates( pointCoordinate, coordElement ) != 0 )
235 const QDomNodeList posList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, u
"pos"_s );
236 if ( posList.size() < 1 )
238 return QgsGeometry();
240 const QDomElement posElement = posList.at( 0 ).toElement();
241 if ( readGMLPositions( pointCoordinate, posElement ) != 0 )
243 return QgsGeometry();
247 if ( pointCoordinate.empty() )
249 return QgsGeometry();
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 ) );
260 unsigned char *wkb =
new unsigned char[size];
263 memcpy( &( wkb )[wkbPosition], &e, 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 ) );
273 wkbPosition +=
sizeof( double );
274 double z = point_it->z();
275 memcpy( &( wkb )[wkbPosition], &z,
sizeof(
double ) );
283QgsGeometry QgsOgcUtils::geometryFromGMLLineString(
const QDomElement &geometryElement )
287 const QDomNodeList coordList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, u
"coordinates"_s );
288 if ( !coordList.isEmpty() )
290 const QDomElement coordElement = coordList.at( 0 ).toElement();
291 if ( readGMLCoordinates( lineCoordinates, coordElement ) != 0 )
293 return QgsGeometry();
298 const QDomNodeList posList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, u
"posList"_s );
299 if ( posList.size() < 1 )
301 return QgsGeometry();
303 const QDomElement posElement = posList.at( 0 ).toElement();
304 if ( readGMLPositions( lineCoordinates, posElement ) != 0 )
306 return QgsGeometry();
310 const bool hasZ { !std::isnan( lineCoordinates.first().z() ) };
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 ) );
316 unsigned char *wkb =
new unsigned char[size];
320 int nPoints = lineCoordinates.size();
323 memcpy( &( wkb )[wkbPosition], &e, 1 );
325 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
326 wkbPosition +=
sizeof( int );
327 memcpy( &( wkb )[wkbPosition], &nPoints,
sizeof(
int ) );
328 wkbPosition +=
sizeof( int );
330 QgsPolyline::const_iterator iter;
331 for ( iter = lineCoordinates.constBegin(); iter != lineCoordinates.constEnd(); ++iter )
335 memcpy( &( wkb )[wkbPosition], &x,
sizeof(
double ) );
336 wkbPosition +=
sizeof( double );
337 memcpy( &( wkb )[wkbPosition], &y,
sizeof(
double ) );
338 wkbPosition +=
sizeof( double );
342 double z = iter->z();
343 memcpy( &( wkb )[wkbPosition], &z,
sizeof(
double ) );
344 wkbPosition +=
sizeof( double );
354QgsGeometry QgsOgcUtils::geometryFromGMLPolygon(
const QDomElement &geometryElement )
361 const QDomNodeList outerBoundaryList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, u
"outerBoundaryIs"_s );
362 if ( !outerBoundaryList.isEmpty() )
364 QDomElement coordinatesElement = outerBoundaryList.at( 0 ).firstChild().firstChild().toElement();
365 if ( coordinatesElement.isNull() )
367 return QgsGeometry();
369 if ( readGMLCoordinates( exteriorPointList, coordinatesElement ) != 0 )
371 return QgsGeometry();
373 ringCoordinates.push_back( exteriorPointList );
376 const QDomNodeList innerBoundaryList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, u
"innerBoundaryIs"_s );
377 for (
int i = 0; i < innerBoundaryList.size(); ++i )
380 coordinatesElement = innerBoundaryList.at( i ).firstChild().firstChild().toElement();
381 if ( coordinatesElement.isNull() )
383 return QgsGeometry();
385 if ( readGMLCoordinates( interiorPointList, coordinatesElement ) != 0 )
387 return QgsGeometry();
389 ringCoordinates.push_back( interiorPointList );
395 const QDomNodeList exteriorList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, u
"exterior"_s );
396 if ( exteriorList.size() < 1 )
398 return QgsGeometry();
400 const QDomElement posElement = exteriorList.at( 0 ).firstChild().firstChild().toElement();
401 if ( posElement.isNull() )
403 return QgsGeometry();
405 if ( readGMLPositions( exteriorPointList, posElement ) != 0 )
407 return QgsGeometry();
409 ringCoordinates.push_back( exteriorPointList );
412 const QDomNodeList interiorList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, u
"interior"_s );
413 for (
int i = 0; i < interiorList.size(); ++i )
416 const QDomElement posElement = interiorList.at( i ).firstChild().firstChild().toElement();
417 if ( posElement.isNull() )
419 return QgsGeometry();
422 if ( readGMLPositions( interiorPointList, posElement ) )
424 return QgsGeometry();
426 ringCoordinates.push_back( interiorPointList );
431 int nrings = ringCoordinates.size();
433 return QgsGeometry();
436 for ( QgsMultiPolyline::const_iterator it = ringCoordinates.constBegin(); it != ringCoordinates.constEnd(); ++it )
438 npoints += it->size();
441 const bool hasZ { !std::isnan( ringCoordinates.first().first().z() ) };
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 ) );
446 unsigned char *wkb =
new unsigned char[size];
449 char e =
static_cast<char>( htonl( 1 ) != 1 );
451 int nPointsInRing = 0;
455 memcpy( &( wkb )[wkbPosition], &e, 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 )
463 nPointsInRing = it->size();
464 memcpy( &( wkb )[wkbPosition], &nPointsInRing,
sizeof(
int ) );
465 wkbPosition +=
sizeof( int );
467 QgsPolyline::const_iterator iter;
468 for ( iter = it->begin(); iter != it->end(); ++iter )
473 memcpy( &( wkb )[wkbPosition], &x,
sizeof(
double ) );
474 wkbPosition +=
sizeof( double );
475 memcpy( &( wkb )[wkbPosition], &y,
sizeof(
double ) );
476 wkbPosition +=
sizeof( double );
481 memcpy( &( wkb )[wkbPosition], &z,
sizeof(
double ) );
482 wkbPosition +=
sizeof( double );
492QgsGeometry QgsOgcUtils::geometryFromGMLMultiPoint(
const QDomElement &geometryElement )
496 const QDomNodeList pointMemberList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, u
"pointMember"_s );
497 if ( pointMemberList.size() < 1 )
499 return QgsGeometry();
501 QDomNodeList pointNodeList;
503 QDomNodeList coordinatesList;
504 QDomNodeList posList;
505 for (
int i = 0; i < pointMemberList.size(); ++i )
508 pointNodeList = pointMemberList.at( i ).toElement().elementsByTagNameNS(
GML_NAMESPACE, u
"Point"_s );
509 if ( pointNodeList.size() < 1 )
514 coordinatesList = pointNodeList.at( 0 ).toElement().elementsByTagNameNS(
GML_NAMESPACE, u
"coordinates"_s );
515 if ( !coordinatesList.isEmpty() )
517 currentPoint.clear();
518 if ( readGMLCoordinates( currentPoint, coordinatesList.at( 0 ).toElement() ) != 0 )
522 if ( currentPoint.empty() )
526 pointList.push_back( ( *currentPoint.begin() ) );
532 posList = pointNodeList.at( 0 ).toElement().elementsByTagNameNS(
GML_NAMESPACE, u
"pos"_s );
533 if ( posList.size() < 1 )
537 currentPoint.clear();
538 if ( readGMLPositions( currentPoint, posList.at( 0 ).toElement() ) != 0 )
542 if ( currentPoint.empty() )
546 pointList.push_back( ( *currentPoint.begin() ) );
550 int nPoints = pointList.size();
552 return QgsGeometry();
554 const bool hasZ { !std::isnan( pointList.first().z() ) };
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 ) ) );
560 unsigned char *wkb =
new unsigned char[size];
563 char e =
static_cast<char>( htonl( 1 ) != 1 );
566 memcpy( &( wkb )[wkbPosition], &e, 1 );
568 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
569 wkbPosition +=
sizeof( int );
570 memcpy( &( wkb )[wkbPosition], &nPoints,
sizeof(
int ) );
571 wkbPosition +=
sizeof( int );
573 for ( QgsPolyline::const_iterator it = pointList.constBegin(); it != pointList.constEnd(); ++it )
575 memcpy( &( wkb )[wkbPosition], &e, 1 );
577 memcpy( &( wkb )[wkbPosition], &pointType,
sizeof(
int ) );
578 wkbPosition +=
sizeof( int );
580 memcpy( &( wkb )[wkbPosition], &x,
sizeof(
double ) );
581 wkbPosition +=
sizeof( double );
583 memcpy( &( wkb )[wkbPosition], &y,
sizeof(
double ) );
584 wkbPosition +=
sizeof( double );
589 memcpy( &( wkb )[wkbPosition], &z,
sizeof(
double ) );
590 wkbPosition +=
sizeof( double );
599QgsGeometry QgsOgcUtils::geometryFromGMLMultiLineString(
const QDomElement &geometryElement )
611 QList< QgsPolyline > lineCoordinates;
612 QDomElement currentLineStringElement;
613 QDomNodeList currentCoordList;
614 QDomNodeList currentPosList;
616 const QDomNodeList lineStringMemberList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, u
"lineStringMember"_s );
617 if ( !lineStringMemberList.isEmpty() )
619 for (
int i = 0; i < lineStringMemberList.size(); ++i )
621 const QDomNodeList lineStringNodeList = lineStringMemberList.at( i ).toElement().elementsByTagNameNS(
GML_NAMESPACE, u
"LineString"_s );
622 if ( lineStringNodeList.size() < 1 )
624 return QgsGeometry();
626 currentLineStringElement = lineStringNodeList.at( 0 ).toElement();
627 currentCoordList = currentLineStringElement.elementsByTagNameNS(
GML_NAMESPACE, u
"coordinates"_s );
628 if ( !currentCoordList.isEmpty() )
631 if ( readGMLCoordinates( currentPointList, currentCoordList.at( 0 ).toElement() ) != 0 )
633 return QgsGeometry();
635 lineCoordinates.push_back( currentPointList );
639 currentPosList = currentLineStringElement.elementsByTagNameNS(
GML_NAMESPACE, u
"posList"_s );
640 if ( currentPosList.size() < 1 )
642 return QgsGeometry();
645 if ( readGMLPositions( currentPointList, currentPosList.at( 0 ).toElement() ) != 0 )
647 return QgsGeometry();
649 lineCoordinates.push_back( currentPointList );
655 const QDomNodeList lineStringList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, u
"LineString"_s );
656 if ( !lineStringList.isEmpty() )
658 for (
int i = 0; i < lineStringList.size(); ++i )
660 currentLineStringElement = lineStringList.at( i ).toElement();
661 currentCoordList = currentLineStringElement.elementsByTagNameNS(
GML_NAMESPACE, u
"coordinates"_s );
662 if ( !currentCoordList.isEmpty() )
665 if ( readGMLCoordinates( currentPointList, currentCoordList.at( 0 ).toElement() ) != 0 )
667 return QgsGeometry();
669 lineCoordinates.push_back( currentPointList );
670 return QgsGeometry();
674 currentPosList = currentLineStringElement.elementsByTagNameNS(
GML_NAMESPACE, u
"posList"_s );
675 if ( currentPosList.size() < 1 )
677 return QgsGeometry();
680 if ( readGMLPositions( currentPointList, currentPosList.at( 0 ).toElement() ) != 0 )
682 return QgsGeometry();
684 lineCoordinates.push_back( currentPointList );
690 return QgsGeometry();
694 int nLines = lineCoordinates.size();
696 return QgsGeometry();
698 const bool hasZ { !std::isnan( lineCoordinates.first().first().z() ) };
699 const int coordSize { hasZ ? 3 : 2 };
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 )
705 size += it->size() * coordSize *
sizeof( double );
709 unsigned char *wkb =
new unsigned char[size];
712 char e =
static_cast<char>( htonl( 1 ) != 1 );
716 memcpy( &( wkb )[wkbPosition], &e, 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 )
725 memcpy( &( wkb )[wkbPosition], &e, 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 )
737 memcpy( &( wkb )[wkbPosition], &x,
sizeof(
double ) );
738 wkbPosition +=
sizeof( double );
739 memcpy( &( wkb )[wkbPosition], &y,
sizeof(
double ) );
740 wkbPosition +=
sizeof( double );
745 memcpy( &( wkb )[wkbPosition], &z,
sizeof(
double ) );
746 wkbPosition +=
sizeof( double );
756QgsGeometry QgsOgcUtils::geometryFromGMLMultiPolygon(
const QDomElement &geometryElement )
759 QVector<QgsMultiPolyline> multiPolygonPoints;
760 QDomElement currentPolygonMemberElement;
761 QDomNodeList polygonList;
762 QDomElement currentPolygonElement;
764 QDomNodeList outerBoundaryList;
765 QDomElement currentOuterBoundaryElement;
766 QDomElement currentInnerBoundaryElement;
768 QDomNodeList exteriorList;
769 QDomElement currentExteriorElement;
770 QDomElement currentInteriorElement;
772 QDomNodeList linearRingNodeList;
773 QDomElement currentLinearRingElement;
775 QDomNodeList currentCoordinateList;
776 QDomNodeList currentPosList;
778 const QDomNodeList polygonMemberList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, u
"polygonMember"_s );
780 for (
int i = 0; i < polygonMemberList.size(); ++i )
782 currentPolygonList.resize( 0 );
783 currentPolygonMemberElement = polygonMemberList.at( i ).toElement();
784 polygonList = currentPolygonMemberElement.elementsByTagNameNS(
GML_NAMESPACE, u
"Polygon"_s );
785 if ( polygonList.size() < 1 )
789 currentPolygonElement = polygonList.at( 0 ).toElement();
792 outerBoundaryList = currentPolygonElement.elementsByTagNameNS(
GML_NAMESPACE, u
"outerBoundaryIs"_s );
793 if ( !outerBoundaryList.isEmpty() )
795 currentOuterBoundaryElement = outerBoundaryList.at( 0 ).toElement();
798 linearRingNodeList = currentOuterBoundaryElement.elementsByTagNameNS(
GML_NAMESPACE, u
"LinearRing"_s );
799 if ( linearRingNodeList.size() < 1 )
803 currentLinearRingElement = linearRingNodeList.at( 0 ).toElement();
804 currentCoordinateList = currentLinearRingElement.elementsByTagNameNS(
GML_NAMESPACE, u
"coordinates"_s );
805 if ( currentCoordinateList.size() < 1 )
809 if ( readGMLCoordinates( ringCoordinates, currentCoordinateList.at( 0 ).toElement() ) != 0 )
813 currentPolygonList.push_back( ringCoordinates );
816 const QDomNodeList innerBoundaryList = currentPolygonElement.elementsByTagNameNS(
GML_NAMESPACE, u
"innerBoundaryIs"_s );
817 for (
int j = 0; j < innerBoundaryList.size(); ++j )
820 currentInnerBoundaryElement = innerBoundaryList.at( j ).toElement();
821 linearRingNodeList = currentInnerBoundaryElement.elementsByTagNameNS(
GML_NAMESPACE, u
"LinearRing"_s );
822 if ( linearRingNodeList.size() < 1 )
826 currentLinearRingElement = linearRingNodeList.at( 0 ).toElement();
827 currentCoordinateList = currentLinearRingElement.elementsByTagNameNS(
GML_NAMESPACE, u
"coordinates"_s );
828 if ( currentCoordinateList.size() < 1 )
832 if ( readGMLCoordinates( ringCoordinates, currentCoordinateList.at( 0 ).toElement() ) != 0 )
836 currentPolygonList.push_back( ringCoordinates );
842 exteriorList = currentPolygonElement.elementsByTagNameNS(
GML_NAMESPACE, u
"exterior"_s );
843 if ( exteriorList.size() < 1 )
848 currentExteriorElement = exteriorList.at( 0 ).toElement();
851 linearRingNodeList = currentExteriorElement.elementsByTagNameNS(
GML_NAMESPACE, u
"LinearRing"_s );
852 if ( linearRingNodeList.size() < 1 )
856 currentLinearRingElement = linearRingNodeList.at( 0 ).toElement();
857 currentPosList = currentLinearRingElement.elementsByTagNameNS(
GML_NAMESPACE, u
"posList"_s );
858 if ( currentPosList.size() < 1 )
862 if ( readGMLPositions( ringPositions, currentPosList.at( 0 ).toElement() ) != 0 )
866 currentPolygonList.push_back( ringPositions );
869 const QDomNodeList interiorList = currentPolygonElement.elementsByTagNameNS(
GML_NAMESPACE, u
"interior"_s );
870 for (
int j = 0; j < interiorList.size(); ++j )
873 currentInteriorElement = interiorList.at( j ).toElement();
874 linearRingNodeList = currentInteriorElement.elementsByTagNameNS(
GML_NAMESPACE, u
"LinearRing"_s );
875 if ( linearRingNodeList.size() < 1 )
879 currentLinearRingElement = linearRingNodeList.at( 0 ).toElement();
880 currentPosList = currentLinearRingElement.elementsByTagNameNS(
GML_NAMESPACE, u
"posList"_s );
881 if ( currentPosList.size() < 1 )
885 if ( readGMLPositions( ringPositions, currentPosList.at( 0 ).toElement() ) != 0 )
889 currentPolygonList.push_back( ringPositions );
892 multiPolygonPoints.push_back( currentPolygonList );
895 int nPolygons = multiPolygonPoints.size();
897 return QgsGeometry();
899 const bool hasZ { !std::isnan( multiPolygonPoints.first().first().first().z() ) };
901 int size = 1 + 2 *
sizeof( int );
904 for (
auto it = multiPolygonPoints.constBegin(); it != multiPolygonPoints.constEnd(); ++it )
906 size += 1 + 2 *
sizeof( int );
907 for (
auto iter = it->begin(); iter != it->end(); ++iter )
909 size +=
static_cast<int>(
sizeof( int ) ) + ( hasZ ? 3 : 2 ) *
static_cast<int>( iter->size() *
sizeof(
double ) );
914 unsigned char *wkb =
new unsigned char[size];
916 char e =
static_cast<char>( htonl( 1 ) != 1 );
923 memcpy( &( wkb )[wkbPosition], &e, 1 );
925 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
926 wkbPosition +=
sizeof( int );
927 memcpy( &( wkb )[wkbPosition], &nPolygons,
sizeof(
int ) );
928 wkbPosition +=
sizeof( int );
932 for (
auto it = multiPolygonPoints.constBegin(); it != multiPolygonPoints.constEnd(); ++it )
934 memcpy( &( wkb )[wkbPosition], &e, 1 );
936 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
937 wkbPosition +=
sizeof( int );
939 memcpy( &( wkb )[wkbPosition], &nRings,
sizeof(
int ) );
940 wkbPosition +=
sizeof( int );
941 for (
auto iter = it->begin(); iter != it->end(); ++iter )
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 )
950 memcpy( &( wkb )[wkbPosition], &x,
sizeof(
double ) );
951 wkbPosition +=
sizeof( double );
952 memcpy( &( wkb )[wkbPosition], &y,
sizeof(
double ) );
953 wkbPosition +=
sizeof( double );
956 double z = iterator->z();
957 memcpy( &( wkb )[wkbPosition], &z,
sizeof(
double ) );
958 wkbPosition +=
sizeof( double );
969QDomElement QgsOgcUtils::filterElement( QDomDocument &doc, GMLVersion gmlVersion, FilterVersion filterVersion,
bool GMLUsed )
971 QDomElement filterElem =
978 QDomAttr attr = doc.createAttribute( u
"xmlns:gml"_s );
983 filterElem.setAttributeNode( attr );
989bool QgsOgcUtils::readGMLCoordinates(
QgsPolyline &coords,
const QDomElement &elem )
991 QString coordSeparator = u
","_s;
992 QString tupleSeparator = u
" "_s;
997 if ( elem.hasAttribute( u
"cs"_s ) )
999 coordSeparator = elem.attribute( u
"cs"_s );
1001 if ( elem.hasAttribute( u
"ts"_s ) )
1003 tupleSeparator = elem.attribute( u
"ts"_s );
1006 const QStringList tupels = elem.text().split( tupleSeparator, Qt::SkipEmptyParts );
1007 QStringList tuple_coords;
1009 bool conversionSuccess;
1011 QStringList::const_iterator it;
1012 for ( it = tupels.constBegin(); it != tupels.constEnd(); ++it )
1014 tuple_coords = ( *it ).split( coordSeparator, Qt::SkipEmptyParts );
1015 if ( tuple_coords.size() < 2 )
1019 x = tuple_coords.at( 0 ).toDouble( &conversionSuccess );
1020 if ( !conversionSuccess )
1024 y = tuple_coords.at( 1 ).toDouble( &conversionSuccess );
1025 if ( !conversionSuccess )
1029 if ( tuple_coords.size() > 2 )
1031 z = tuple_coords.at( 2 ).toDouble( &conversionSuccess );
1032 if ( !conversionSuccess )
1039 z = std::numeric_limits<double>::quiet_NaN();
1041 coords.append( QgsPoint( x, y, z ) );
1050 const QDomElement boxElem = boxNode.toElement();
1051 if ( boxElem.tagName() !=
"Box"_L1 )
1054 const QDomElement bElem = boxElem.firstChild().toElement();
1055 QString coordSeparator = u
","_s;
1056 QString tupleSeparator = u
" "_s;
1057 if ( bElem.hasAttribute( u
"cs"_s ) )
1059 coordSeparator = bElem.attribute( u
"cs"_s );
1061 if ( bElem.hasAttribute( u
"ts"_s ) )
1063 tupleSeparator = bElem.attribute( u
"ts"_s );
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 );
1073 if ( ok1 && ok2 && ok3 && ok4 )
1082bool QgsOgcUtils::readGMLPositions(
QgsPolyline &coords,
const QDomElement &elem )
1086 const QStringList pos = elem.text().split(
' ', Qt::SkipEmptyParts );
1088 bool conversionSuccess;
1089 const int posSize = pos.size();
1091 int srsDimension = 2;
1092 if ( elem.hasAttribute( u
"srsDimension"_s ) )
1094 srsDimension = elem.attribute( u
"srsDimension"_s ).toInt( &conversionSuccess );
1095 if ( !conversionSuccess )
1100 else if ( elem.hasAttribute( u
"dimension"_s ) )
1102 srsDimension = elem.attribute( u
"dimension"_s ).toInt( &conversionSuccess );
1103 if ( !conversionSuccess )
1109 for (
int i = 0; i < posSize / srsDimension; i++ )
1111 x = pos.at( i * srsDimension ).toDouble( &conversionSuccess );
1112 if ( !conversionSuccess )
1116 y = pos.at( i * srsDimension + 1 ).toDouble( &conversionSuccess );
1117 if ( !conversionSuccess )
1121 if ( srsDimension > 2 )
1123 z = pos.at( i * srsDimension + 2 ).toDouble( &conversionSuccess );
1124 if ( !conversionSuccess )
1131 z = std::numeric_limits<double>::quiet_NaN();
1133 coords.append( QgsPoint( x, y, z ) );
1143 const QDomElement envelopeElem = envelopeNode.toElement();
1144 if ( envelopeElem.tagName() !=
"Envelope"_L1 )
1147 const QDomNodeList lowerCornerList = envelopeElem.elementsByTagNameNS(
GML_NAMESPACE, u
"lowerCorner"_s );
1148 if ( lowerCornerList.size() < 1 )
1151 const QDomNodeList upperCornerList = envelopeElem.elementsByTagNameNS(
GML_NAMESPACE, u
"upperCorner"_s );
1152 if ( upperCornerList.size() < 1 )
1155 bool conversionSuccess;
1156 int srsDimension = 2;
1158 QDomElement elem = lowerCornerList.at( 0 ).toElement();
1159 if ( elem.hasAttribute( u
"srsDimension"_s ) )
1161 srsDimension = elem.attribute( u
"srsDimension"_s ).toInt( &conversionSuccess );
1162 if ( !conversionSuccess )
1167 else if ( elem.hasAttribute( u
"dimension"_s ) )
1169 srsDimension = elem.attribute( u
"dimension"_s ).toInt( &conversionSuccess );
1170 if ( !conversionSuccess )
1175 QString bString = elem.text();
1177 const double xmin = bString.section(
' ', 0, 0 ).toDouble( &conversionSuccess );
1178 if ( !conversionSuccess )
1180 const double ymin = bString.section(
' ', 1, 1 ).toDouble( &conversionSuccess );
1181 if ( !conversionSuccess )
1184 elem = upperCornerList.at( 0 ).toElement();
1185 if ( elem.hasAttribute( u
"srsDimension"_s ) )
1187 srsDimension = elem.attribute( u
"srsDimension"_s ).toInt( &conversionSuccess );
1188 if ( !conversionSuccess )
1193 else if ( elem.hasAttribute( u
"dimension"_s ) )
1195 srsDimension = elem.attribute( u
"dimension"_s ).toInt( &conversionSuccess );
1196 if ( !conversionSuccess )
1202 Q_UNUSED( srsDimension )
1204 bString = elem.text();
1205 const double xmax = bString.section(
' ', 0, 0 ).toDouble( &conversionSuccess );
1206 if ( !conversionSuccess )
1208 const double ymax = bString.section(
' ', 1, 1 ).toDouble( &conversionSuccess );
1209 if ( !conversionSuccess )
1224 const QString &srsName,
1225 bool invertAxisOrientation,
1230 return QDomElement();
1233 QDomElement boxElem = doc.createElement( u
"gml:Box"_s );
1234 if ( !srsName.isEmpty() )
1236 boxElem.setAttribute( u
"srsName"_s, srsName );
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 );
1242 QString coordString;
1251 const QDomText coordText = doc.createTextNode( coordString );
1252 coordElem.appendChild( coordText );
1253 boxElem.appendChild( coordElem );
1264 const QString &srsName,
1265 bool invertAxisOrientation,
1270 return QDomElement();
1273 QDomElement envElem = doc.createElement( u
"gml:Envelope"_s );
1274 if ( !srsName.isEmpty() )
1276 envElem.setAttribute( u
"srsName"_s, srsName );
1280 QDomElement lowerCornerElem = doc.createElement( u
"gml:lowerCorner"_s );
1284 const QDomText lowerCornerText = doc.createTextNode( posList );
1285 lowerCornerElem.appendChild( lowerCornerText );
1286 envElem.appendChild( lowerCornerElem );
1288 QDomElement upperCornerElem = doc.createElement( u
"gml:upperCorner"_s );
1292 const QDomText upperCornerText = doc.createTextNode( posList );
1293 upperCornerElem.appendChild( upperCornerText );
1294 envElem.appendChild( upperCornerElem );
1307 const QString &srsName,
1308 bool invertAxisOrientation,
1309 const QString &gmlIdBase,
1313 return QDomElement();
1316 QString cs = u
","_s;
1318 const QString ts = u
" "_s;
1320 QDomElement baseCoordElem;
1322 bool hasZValue =
false;
1324 const QByteArray wkb( geometry.
asWkb() );
1334 return QDomElement();
1347 baseCoordElem = doc.createElement( u
"gml:pos"_s );
1350 baseCoordElem = doc.createElement( u
"gml:posList"_s );
1357 baseCoordElem = doc.createElement( u
"gml:coordinates"_s );
1358 baseCoordElem.setAttribute( u
"cs"_s, cs );
1359 baseCoordElem.setAttribute( u
"ts"_s, ts );
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();
1382 if ( invertAxisOrientation )
1396 const QDomText coordText = doc.createTextNode( coordString );
1398 coordElem.appendChild( coordText );
1400 coordElem.setAttribute( u
"srsDimension"_s, hasZValue ? u
"3"_s : u
"2"_s );
1401 pointElem.appendChild( coordElem );
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 );
1420 for (
int idx = 0; idx < nPoints; ++idx )
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();
1432 if ( invertAxisOrientation )
1446 const QDomText coordText = doc.createTextNode( coordString );
1448 coordElem.appendChild( coordText );
1450 coordElem.setAttribute( u
"srsDimension"_s, hasZValue ? u
"3"_s : u
"2"_s );
1451 pointElem.appendChild( coordElem );
1454 pointMemberElem.appendChild( pointElem );
1455 multiPointElem.appendChild( pointMemberElem );
1457 return multiPointElem;
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 );
1476 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1477 QString coordString;
1478 for (
int idx = 0; idx < nPoints; ++idx )
1487 if ( invertAxisOrientation )
1501 const QDomText coordText = doc.createTextNode( coordString );
1502 coordElem.appendChild( coordText );
1504 coordElem.setAttribute( u
"srsDimension"_s, hasZValue ? u
"3"_s : u
"2"_s );
1505 lineStringElem.appendChild( coordElem );
1506 return lineStringElem;
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 );
1524 for (
int jdx = 0; jdx < nLines; jdx++ )
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 ) );
1536 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1537 QString coordString;
1538 for (
int idx = 0; idx < nPoints; idx++ )
1547 if ( invertAxisOrientation )
1562 const QDomText coordText = doc.createTextNode( coordString );
1563 coordElem.appendChild( coordText );
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 );
1570 return multiLineStringElem;
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 );
1589 if ( numRings == 0 )
1590 return QDomElement();
1592 for (
int idx = 0; idx < numRings; idx++ )
1594 QString boundaryName = ( gmlVersion ==
GML_2_1_2 ) ?
"gml:outerBoundaryIs" :
"gml:exterior";
1597 boundaryName = ( gmlVersion ==
GML_2_1_2 ) ?
"gml:innerBoundaryIs" :
"gml:interior";
1599 QDomElement boundaryElem = doc.createElement( boundaryName );
1600 QDomElement ringElem = doc.createElement( u
"gml:LinearRing"_s );
1605 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1606 QString coordString;
1607 for (
int jdx = 0; jdx < nPoints; jdx++ )
1616 if ( invertAxisOrientation )
1630 const QDomText coordText = doc.createTextNode( coordString );
1631 coordElem.appendChild( coordText );
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 );
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 );
1655 wkbPtr >> numPolygons;
1657 for (
int kdx = 0; kdx < numPolygons; kdx++ )
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 ) );
1669 for (
int idx = 0; idx < numRings; idx++ )
1671 QString boundaryName = ( gmlVersion ==
GML_2_1_2 ) ?
"gml:outerBoundaryIs" :
"gml:exterior";
1674 boundaryName = ( gmlVersion ==
GML_2_1_2 ) ?
"gml:innerBoundaryIs" :
"gml:interior";
1676 QDomElement boundaryElem = doc.createElement( boundaryName );
1677 QDomElement ringElem = doc.createElement( u
"gml:LinearRing"_s );
1682 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1683 QString coordString;
1684 for (
int jdx = 0; jdx < nPoints; jdx++ )
1693 if ( invertAxisOrientation )
1708 const QDomText coordText = doc.createTextNode( coordString );
1709 coordElem.appendChild( coordText );
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 );
1719 return multiPolygonElem;
1722 return QDomElement();
1728 return QDomElement();
1734 return geometryToGML( geometry, doc, u
"GML2"_s, precision );
1737QDomElement QgsOgcUtils::createGMLCoordinates(
const QgsPolylineXY &points, QDomDocument &doc )
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 );
1743 QString coordString;
1744 QVector<QgsPointXY>::const_iterator pointIt = points.constBegin();
1745 for ( ; pointIt != points.constEnd(); ++pointIt )
1747 if ( pointIt != points.constBegin() )
1756 const QDomText coordText = doc.createTextNode( coordString );
1757 coordElem.appendChild( coordText );
1761QDomElement QgsOgcUtils::createGMLPositions(
const QgsPolylineXY &points, QDomDocument &doc )
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 );
1768 QString coordString;
1769 QVector<QgsPointXY>::const_iterator pointIt = points.constBegin();
1770 for ( ; pointIt != points.constEnd(); ++pointIt )
1772 if ( pointIt != points.constBegin() )
1781 const QDomText coordText = doc.createTextNode( coordString );
1782 posElem.appendChild( coordText );
1790 if ( fillElement.isNull() || !fillElement.hasChildNodes() )
1798 QDomElement cssElem = fillElement.firstChildElement( u
"CssParameter"_s );
1799 while ( !cssElem.isNull() )
1801 cssName = cssElem.attribute( u
"name"_s, u
"not_found"_s );
1802 if ( cssName !=
"not_found"_L1 )
1804 elemText = cssElem.text();
1805 if ( cssName ==
"fill"_L1 )
1807 color.setNamedColor( elemText );
1809 else if ( cssName ==
"fill-opacity"_L1 )
1812 const double opacity = elemText.toDouble( &ok );
1815 color.setAlphaF( opacity );
1820 cssElem = cssElem.nextSiblingElement( u
"CssParameter"_s );
1834 if ( element.isNull() || !element.hasChildNodes() )
1841 if ( element.firstChild().nodeType() == QDomNode::TextNode )
1851 QDomElement childElem = element.firstChildElement();
1852 while ( !childElem.isNull() )
1864 if ( !expr->d->mRootNode )
1866 expr->d->mRootNode.reset( node );
1873 childElem = childElem.nextSiblingElement();
1877 expr->d->mExp = expr->
dump();
1903static int binaryOperatorFromTagName(
const QString &tagName )
1906 return BINARY_OPERATORS_TAG_NAMES_MAP()->value( tagName, -1 );
1913 return u
"PropertyIsLike"_s;
1915 return BINARY_OPERATORS_TAG_NAMES_MAP()->key( op, QString() );
1918static bool isBinaryOperator(
const QString &tagName )
1920 return binaryOperatorFromTagName( tagName ) >= 0;
1924static bool isSpatialOperator(
const QString &tagName )
1926 static QStringList spatialOps;
1927 if ( spatialOps.isEmpty() )
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;
1933 return spatialOps.contains( tagName );
1939 QgsExpressionNode *node = utils.nodeFromOgcFilter( element );
1940 errorMessage = utils.errorMessage();
1947 QgsExpressionNodeBinaryOperator *node = utils.nodeBinaryOperatorFromOgcFilter( element );
1948 errorMessage = utils.errorMessage();
1955 QgsExpressionNodeFunction *node = utils.nodeSpatialOperatorFromOgcFilter( element );
1956 errorMessage = utils.errorMessage();
1963 QgsExpressionNodeUnaryOperator *node = utils.nodeNotFromOgcFilter( element );
1964 errorMessage = utils.errorMessage();
1971 QgsExpressionNodeFunction *node = utils.nodeFunctionFromOgcFilter( element );
1972 errorMessage = utils.errorMessage();
1979 QgsExpressionNode *node = utils.nodeLiteralFromOgcFilter( element );
1980 errorMessage = utils.errorMessage();
1987 QgsExpressionNodeColumnRef *node = utils.nodeColumnRefFromOgcFilter( element );
1988 errorMessage = utils.errorMessage();
1992QgsExpressionNode *QgsOgcUtils::nodeIsBetweenFromOgcFilter( QDomElement &element, QString &errorMessage )
1995 QgsExpressionNode *node = utils.nodeIsBetweenFromOgcFilter( element );
1996 errorMessage = utils.errorMessage();
2003 QgsExpressionNodeBinaryOperator *node = utils.nodePropertyIsNullFromOgcFilter( element );
2004 errorMessage = utils.errorMessage();
2015 u
"geometry"_s, QString(),
false,
false, errorMessage );
2021 u
"geometry"_s, QString(),
false,
false, errorMessage, requiresFilterElement );
2026 return doc.createElementNS(
SE_NAMESPACE, u
"se:ElseFilter"_s );
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 )
2045 return QDomElement();
2051 QgsOgcUtilsExprToFilter utils( doc, gmlVersion, filterVersion, namespacePrefix, namespaceURI, geometryName, srsName, honourAxisOrientation, invertAxisOrientation, fieldNameToXPathMap, namespacePrefixToUriMap );
2055 if ( exprRootElem.isNull() )
2056 return QDomElement();
2058 QDomElement filterElem = filterElement( doc, gmlVersion, filterVersion, utils.
GMLNamespaceUsed() );
2060 if ( !namespacePrefix.isEmpty() && !namespaceURI.isEmpty() )
2062 QDomAttr attr = doc.createAttribute( u
"xmlns:"_s + namespacePrefix );
2063 attr.setValue( namespaceURI );
2064 filterElem.setAttributeNode( attr );
2067 filterElem.appendChild( exprRootElem );
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 )
2091 return QDomElement();
2093 QgsOgcUtilsExprToFilter utils( doc, gmlVersion, filterVersion, QString(), QString(), geometryName, srsName, honourAxisOrientation, invertAxisOrientation, fieldNameToXPathMap, namespacePrefixToUriMap );
2101 if ( !exprRootElem.isNull() )
2103 if ( requiresFilterElement )
2105 QDomElement filterElem = filterElement( doc, gmlVersion, filterVersion, utils.
GMLNamespaceUsed() );
2107 filterElem.appendChild( exprRootElem );
2110 return exprRootElem;
2113 return QDomElement();
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 )
2129 return QDomElement();
2132 layerProperties, honourAxisOrientation, invertAxisOrientation,
2133 mapUnprefixedTypenameToPrefixedTypename, fieldNameToXPathMap, namespacePrefixToUriMap );
2137 if ( exprRootElem.isNull() )
2138 return QDomElement();
2140 QDomElement filterElem = filterElement( doc, gmlVersion, filterVersion, utils.
GMLNamespaceUsed() );
2142 QSet<QString> setNamespaceURI;
2145 if ( !props.mNamespacePrefix.isEmpty() && !props.mNamespaceURI.isEmpty() &&
2146 !setNamespaceURI.contains( props.mNamespaceURI ) )
2148 setNamespaceURI.insert( props.mNamespaceURI );
2149 QDomAttr attr = doc.createAttribute( u
"xmlns:"_s + props.mNamespacePrefix );
2150 attr.setValue( props.mNamespaceURI );
2151 filterElem.setAttributeNode( attr );
2154 filterElem.appendChild( exprRootElem );
2162 if ( gmlGeomType ==
"Point"_L1 )
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 )
2198 mErrorMessage = QObject::tr(
"Node type not supported: %1" ).arg( node->
nodeType() );
2199 return QDomElement();
2206 if ( !mErrorMessage.isEmpty() )
2207 return QDomElement();
2210 switch ( node->
op() )
2213 uoElem = mDoc.createElement( mFilterPrefix +
":Literal" );
2218 uoElem.appendChild( mDoc.createTextNode(
"-" + operandElem.text() ) );
2219 mDoc.removeChild( operandElem );
2223 mErrorMessage = QObject::tr(
"This use of unary operator not implemented yet" );
2224 return QDomElement();
2228 uoElem = mDoc.createElement( mFilterPrefix +
":Not" );
2229 uoElem.appendChild( operandElem );
2233 mErrorMessage = QObject::tr(
"Unary operator '%1' not implemented yet" ).arg( node->
text() );
2234 return QDomElement();
2244 if ( !mErrorMessage.isEmpty() )
2245 return QDomElement();
2254 const QgsExpressionNodeLiteral *rightLit =
static_cast<const QgsExpressionNodeLiteral *
>( node->
opRight() );
2258 QDomElement elem = mDoc.createElement( mFilterPrefix +
":PropertyIsNull" );
2259 elem.appendChild( leftElem );
2263 QDomElement notElem = mDoc.createElement( mFilterPrefix +
":Not" );
2264 notElem.appendChild( elem );
2278 if ( !mErrorMessage.isEmpty() )
2279 return QDomElement();
2282 const QString opText = binaryOperatorToTagName( op );
2283 if ( opText.isEmpty() )
2287 mErrorMessage = QObject::tr(
"Binary operator %1 not implemented yet" ).arg( node->
text() );
2288 return QDomElement();
2291 QDomElement boElem = mDoc.createElement( mFilterPrefix +
":" + opText );
2296 boElem.setAttribute( u
"matchCase"_s, u
"false"_s );
2299 boElem.setAttribute( u
"wildCard"_s, u
"%"_s );
2300 boElem.setAttribute( u
"singleChar"_s, u
"_"_s );
2302 boElem.setAttribute( u
"escape"_s, u
"\\"_s );
2304 boElem.setAttribute( u
"escapeChar"_s, u
"\\"_s );
2307 boElem.appendChild( leftElem );
2308 boElem.appendChild( rightElem );
2315 Q_UNUSED( expression )
2318 switch ( node->
value().userType() )
2320 case QMetaType::Type::Int:
2321 value = QString::number( node->
value().toInt() );
2323 case QMetaType::Type::Double:
2326 case QMetaType::Type::QString:
2327 value = node->
value().toString();
2329 case QMetaType::Type::QDate:
2330 value = node->
value().toDate().toString( Qt::ISODate );
2332 case QMetaType::Type::QDateTime:
2333 value = node->
value().toDateTime().toString( Qt::ISODate );
2337 mErrorMessage = QObject::tr(
"Literal type not supported: %1" ).arg(
static_cast<QMetaType::Type
>( node->
value().userType() ) );
2338 return QDomElement();
2341 QDomElement litElem = mDoc.createElement( mFilterPrefix +
":Literal" );
2342 litElem.appendChild( mDoc.createTextNode( value ) );
2349 Q_UNUSED( expression )
2351 QDomElement propElem = mDoc.createElement( mFilterPrefix +
":" + mPropertyName );
2352 if ( !mFieldNameToXPathMap.isEmpty() )
2354 const auto iterFieldName = mFieldNameToXPathMap.constFind( node->
name() );
2355 if ( iterFieldName != mFieldNameToXPathMap.constEnd() )
2357 const QString xpath( *iterFieldName );
2359 if ( !mNamespacePrefixToUriMap.isEmpty() )
2361 const QStringList parts = xpath.split(
'/' );
2362 QSet<QString> setNamespacePrefix;
2363 for (
const QString &part : std::as_const( parts ) )
2365 const QStringList subparts = part.split(
':' );
2366 if ( subparts.size() == 2 && !setNamespacePrefix.contains( subparts[0] ) )
2368 const auto iterNamespacePrefix = mNamespacePrefixToUriMap.constFind( subparts[0] );
2369 if ( iterNamespacePrefix != mNamespacePrefixToUriMap.constEnd() )
2371 setNamespacePrefix.insert( subparts[0] );
2372 QDomAttr attr = mDoc.createAttribute( u
"xmlns:"_s + subparts[0] );
2373 attr.setValue( *iterNamespacePrefix );
2374 propElem.setAttributeNode( attr );
2380 propElem.appendChild( mDoc.createTextNode( xpath ) );
2385 QString columnRef( node->
name() );
2386 if ( !mNamespacePrefix.isEmpty() && !mNamespaceURI.isEmpty() )
2387 columnRef = mNamespacePrefix + u
":"_s + columnRef;
2388 propElem.appendChild( mDoc.createTextNode( columnRef ) );
2396 if ( node->
list()->
list().size() == 1 )
2400 QDomElement eqElem = mDoc.createElement( mFilterPrefix +
":PropertyIsEqualTo" );
2401 eqElem.appendChild( leftNode );
2402 eqElem.appendChild( firstListNode );
2405 QDomElement notElem = mDoc.createElement( mFilterPrefix +
":Not" );
2406 notElem.appendChild( eqElem );
2412 QDomElement orElem = mDoc.createElement( mFilterPrefix +
":Or" );
2415 const auto constList = node->
list()->
list();
2416 for ( QgsExpressionNode *n : constList )
2419 if ( !mErrorMessage.isEmpty() )
2420 return QDomElement();
2422 QDomElement eqElem = mDoc.createElement( mFilterPrefix +
":PropertyIsEqualTo" );
2423 eqElem.appendChild( leftNode.cloneNode() );
2424 eqElem.appendChild( listNode );
2426 orElem.appendChild( eqElem );
2431 QDomElement notElem = mDoc.createElement( mFilterPrefix +
":Not" );
2432 notElem.appendChild( orElem );
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 }
2450static bool isBinarySpatialOperator(
const QString &fnName )
2452 return BINARY_SPATIAL_OPS_MAP()->contains( fnName );
2455static QString tagNameForSpatialOperator(
const QString &fnName )
2457 return BINARY_SPATIAL_OPS_MAP()->value( fnName );
2479 if ( fnDef->
name() ==
"geom_from_wkt"_L1 )
2481 const QList<QgsExpressionNode *> &args = fnNode->
args()->
list();
2497 if ( fd->
name() ==
"intersects_bbox"_L1 )
2499 QList<QgsExpressionNode *> argNodes = node->
args()->
list();
2500 Q_ASSERT( argNodes.count() == 2 );
2502 const QgsGeometry geom = geometryFromConstExpr( argNodes[1] );
2503 if ( !geom.
isNull() && isGeometryColumn( argNodes[0] ) )
2513 QDomElement funcElem = mDoc.createElement( mFilterPrefix +
":BBOX" );
2515 if ( !mGeometryName.isEmpty() )
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 ) );
2524 funcElem.appendChild( geomProperty );
2526 funcElem.appendChild( elemBox );
2531 mErrorMessage = QObject::tr(
"<BBOX> is currently supported only in form: bbox(@geometry, geomFromWKT('…'))" );
2532 return QDomElement();
2536 if ( isBinarySpatialOperator( fd->
name() ) )
2538 QList<QgsExpressionNode *> argNodes = node->
args()->
list();
2539 Q_ASSERT( argNodes.count() == 2 );
2541 QgsExpressionNode *otherNode =
nullptr;
2542 if ( isGeometryColumn( argNodes[0] ) )
2543 otherNode = argNodes[1];
2544 else if ( isGeometryColumn( argNodes[1] ) )
2545 otherNode = argNodes[0];
2548 mErrorMessage = QObject::tr(
"Unable to translate spatial operator: at least one must refer to geometry." );
2549 return QDomElement();
2552 QDomElement otherGeomElem;
2557 mErrorMessage = QObject::tr(
"spatial operator: the other operator must be a geometry constructor function" );
2558 return QDomElement();
2561 const QgsExpressionNodeFunction *otherFn =
static_cast<const QgsExpressionNodeFunction *
>( otherNode );
2563 if ( otherFnDef->
name() ==
"geom_from_wkt"_L1 )
2565 QgsExpressionNode *firstFnArg = otherFn->
args()->
list()[0];
2568 mErrorMessage = QObject::tr(
"geom_from_wkt: argument must be string literal" );
2569 return QDomElement();
2571 const QString wkt =
static_cast<const QgsExpressionNodeLiteral *
>( firstFnArg )->value().toString();
2574 u
"qgis_id_geom_%1"_s.arg( mGeomId ) );
2575 if ( otherGeomElem.isNull() )
2577 mErrorMessage = QObject::tr(
"geom_from_wkt: unable to generate GML from wkt geometry" );
2578 return QDomElement();
2582 else if ( otherFnDef->
name() ==
"geom_from_gml"_L1 )
2584 QgsExpressionNode *firstFnArg = otherFn->
args()->
list()[0];
2587 mErrorMessage = QObject::tr(
"geom_from_gml: argument must be string literal" );
2588 return QDomElement();
2591 QDomDocument geomDoc;
2592 const QString gml =
static_cast<const QgsExpressionNodeLiteral *
>( firstFnArg )->value().toString();
2594 const QString xml = u
"<tmp xmlns:gml=\"%1\">%2</tmp>"_s.arg(
GML_NAMESPACE, gml );
2595 if ( !geomDoc.setContent( xml,
true ) )
2597 mErrorMessage = QObject::tr(
"geom_from_gml: unable to parse XML" );
2598 return QDomElement();
2601 const QDomNode geomNode = mDoc.importNode( geomDoc.documentElement().firstChildElement(),
true );
2602 otherGeomElem = geomNode.toElement();
2608 u
"qgis_id_geom_%1"_s.arg( mGeomId ) );
2609 if ( otherGeomElem.
isNull() )
2611 mErrorMessage = QObject::tr(
"geom from static value: unable to generate GML from static variable" );
2612 return QDomElement();
2618 mErrorMessage = QObject::tr(
"spatial operator: unknown geometry constructor function" );
2619 return QDomElement();
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 );
2635 if ( fd->
isStatic( node, expression, context ) )
2637 const QVariant result = fd->
run( node->
args(), context, expression, node );
2638 const QgsExpressionNodeLiteral literal( result );
2639 return expressionLiteralToOgcFilter( &literal, expression, context );
2644 mErrorMessage = QObject::tr(
"Special columns/constants are not supported." );
2645 return QDomElement();
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 )
2655 if ( !mErrorMessage.isEmpty() )
2656 return QDomElement();
2658 funcElem.appendChild( childElem );
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 )
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 )
2711 mErrorMessage = QObject::tr(
"Node type not supported: %1" ).arg( node->
nodeType() );
2712 return QDomElement();
2721 if ( !mErrorMessage.isEmpty() )
2722 return QDomElement();
2725 switch ( node->
op() )
2728 uoElem = mDoc.createElement( mFilterPrefix +
":Literal" );
2733 uoElem.appendChild( mDoc.createTextNode(
"-" + operandElem.text() ) );
2734 mDoc.removeChild( operandElem );
2738 mErrorMessage = QObject::tr(
"This use of unary operator not implemented yet" );
2739 return QDomElement();
2743 uoElem = mDoc.createElement( mFilterPrefix +
":Not" );
2744 uoElem.appendChild( operandElem );
2749 return QDomElement();
2759 if ( !mErrorMessage.isEmpty() )
2760 return QDomElement();
2769 const QgsSQLStatement::NodeLiteral *rightLit =
static_cast<const QgsSQLStatement::NodeLiteral *
>( node->
opRight() );
2773 QDomElement elem = mDoc.createElement( mFilterPrefix +
":PropertyIsNull" );
2774 elem.appendChild( leftElem );
2778 QDomElement notElem = mDoc.createElement( mFilterPrefix +
":Not" );
2779 notElem.appendChild( elem );
2793 if ( !mErrorMessage.isEmpty() )
2794 return QDomElement();
2803 opText = u
"PropertyIsEqualTo"_s;
2805 opText = u
"PropertyIsNotEqualTo"_s;
2807 opText = u
"PropertyIsLessThanOrEqualTo"_s;
2809 opText = u
"PropertyIsGreaterThanOrEqualTo"_s;
2811 opText = u
"PropertyIsLessThan"_s;
2813 opText = u
"PropertyIsGreaterThan"_s;
2815 opText = u
"PropertyIsLike"_s;
2817 opText = u
"PropertyIsLike"_s;
2819 if ( opText.isEmpty() )
2823 return QDomElement();
2826 QDomElement boElem = mDoc.createElement( mFilterPrefix +
":" + opText );
2831 boElem.setAttribute( u
"matchCase"_s, u
"false"_s );
2834 boElem.setAttribute( u
"wildCard"_s, u
"%"_s );
2835 boElem.setAttribute( u
"singleChar"_s, u
"_"_s );
2837 boElem.setAttribute( u
"escape"_s, u
"\\"_s );
2839 boElem.setAttribute( u
"escapeChar"_s, u
"\\"_s );
2842 boElem.appendChild( leftElem );
2843 boElem.appendChild( rightElem );
2851 switch ( node->
value().userType() )
2853 case QMetaType::Type::Int:
2854 value = QString::number( node->
value().toInt() );
2856 case QMetaType::Type::LongLong:
2857 value = QString::number( node->
value().toLongLong() );
2859 case QMetaType::Type::Double:
2862 case QMetaType::Type::QString:
2863 value = node->
value().toString();
2867 mErrorMessage = QObject::tr(
"Literal type not supported: %1" ).arg(
static_cast<QMetaType::Type
>( node->
value().userType() ) );
2868 return QDomElement();
2871 QDomElement litElem = mDoc.createElement( mFilterPrefix +
":Literal" );
2872 litElem.appendChild( mDoc.createTextNode( value ) );
2879 QDomElement propElem = mDoc.createElement( mFilterPrefix +
":" + mPropertyName );
2880 if ( node->
tableName().isEmpty() || mLayerProperties.size() == 1 )
2882 if ( !mFieldNameToXPathMap.isEmpty() )
2884 const auto iterFieldName = mFieldNameToXPathMap.constFind( node->
name() );
2885 if ( iterFieldName != mFieldNameToXPathMap.constEnd() )
2887 const QString xpath( *iterFieldName );
2889 if ( !mNamespacePrefixToUriMap.isEmpty() )
2891 const QStringList parts = xpath.split(
'/' );
2892 QSet<QString> setNamespacePrefix;
2893 for (
const QString &part : std::as_const( parts ) )
2895 const QStringList subparts = part.split(
':' );
2896 if ( subparts.size() == 2 && !setNamespacePrefix.contains( subparts[0] ) )
2898 const auto iterNamespacePrefix = mNamespacePrefixToUriMap.constFind( subparts[0] );
2899 if ( iterNamespacePrefix != mNamespacePrefixToUriMap.constEnd() )
2901 setNamespacePrefix.insert( subparts[0] );
2902 QDomAttr attr = mDoc.createAttribute( u
"xmlns:"_s + subparts[0] );
2903 attr.setValue( *iterNamespacePrefix );
2904 propElem.setAttributeNode( attr );
2910 propElem.appendChild( mDoc.createTextNode( xpath ) );
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() ) );
2919 propElem.appendChild( mDoc.createTextNode( node->
name() ) );
2923 QString tableName( mMapTableAliasToNames[node->
tableName()] );
2924 if ( mMapUnprefixedTypenameToPrefixedTypename.contains( tableName ) )
2925 tableName = mMapUnprefixedTypenameToPrefixedTypename[tableName];
2926 propElem.appendChild( mDoc.createTextNode( tableName +
"/" + node->
name() ) );
2933 if ( node->
list()->
list().size() == 1 )
2937 QDomElement eqElem = mDoc.createElement( mFilterPrefix +
":PropertyIsEqualTo" );
2938 eqElem.appendChild( leftNode );
2939 eqElem.appendChild( firstListNode );
2942 QDomElement notElem = mDoc.createElement( mFilterPrefix +
":Not" );
2943 notElem.appendChild( eqElem );
2949 QDomElement orElem = mDoc.createElement( mFilterPrefix +
":Or" );
2952 const auto constList = node->
list()->
list();
2953 for ( QgsSQLStatement::Node *n : constList )
2956 if ( !mErrorMessage.isEmpty() )
2957 return QDomElement();
2959 QDomElement eqElem = mDoc.createElement( mFilterPrefix +
":PropertyIsEqualTo" );
2960 eqElem.appendChild( leftNode.cloneNode() );
2961 eqElem.appendChild( listNode );
2963 orElem.appendChild( eqElem );
2968 QDomElement notElem = mDoc.createElement( mFilterPrefix +
":Not" );
2969 notElem.appendChild( orElem );
2978 QDomElement elem = mDoc.createElement( mFilterPrefix +
":PropertyIsBetween" );
2980 QDomElement lowerBoundary = mDoc.createElement( mFilterPrefix +
":LowerBoundary" );
2982 elem.appendChild( lowerBoundary );
2983 QDomElement upperBoundary = mDoc.createElement( mFilterPrefix +
":UpperBoundary" );
2985 elem.appendChild( upperBoundary );
2989 QDomElement notElem = mDoc.createElement( mFilterPrefix +
":Not" );
2990 notElem.appendChild( elem );
2997static QString mapBinarySpatialToOgc(
const QString &name )
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 )
3008 if ( nameCompare.compare( op, Qt::CaseInsensitive ) == 0 )
3014static QString mapTernarySpatialToOgc(
const QString &name )
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 )
3026QString QgsOgcUtilsSQLStatementToFilter::getGeometryColumnSRSName(
const QgsSQLStatement::Node *node )
3031 const QgsSQLStatement::NodeColumnRef *col =
static_cast<const QgsSQLStatement::NodeColumnRef *
>( node );
3034 const auto constMLayerProperties = mLayerProperties;
3035 for (
const QgsOgcUtils::LayerProperties &prop : constMLayerProperties )
3037 if ( prop.mName.compare( mMapTableAliasToNames[col->
tableName()], Qt::CaseInsensitive ) == 0 &&
3038 prop.mGeometryAttribute.compare( col->
name(), Qt::CaseInsensitive ) == 0 )
3040 return prop.mSRSName;
3044 if ( !mLayerProperties.empty() &&
3045 mLayerProperties.at( 0 ).mGeometryAttribute.compare( col->
name(), Qt::CaseInsensitive ) == 0 )
3047 return mLayerProperties.at( 0 ).mSRSName;
3053 QList<QgsSQLStatement::Node *> args,
3054 bool lastArgIsSRSName,
3056 bool &axisInversion )
3058 srsName = mCurrentSRSName;
3059 axisInversion = mInvertAxisOrientation;
3061 if ( lastArgIsSRSName )
3063 QgsSQLStatement::Node *lastArg = args[ args.size() - 1 ];
3066 mErrorMessage = QObject::tr(
"%1: Last argument must be string or integer literal" ).arg( mainNode->
name() );
3069 const QgsSQLStatement::NodeLiteral *lit =
static_cast<const QgsSQLStatement::NodeLiteral *
>( lastArg );
3070 if ( lit->
value().userType() == QMetaType::Type::Int )
3074 srsName =
"EPSG:" + QString::number( lit->
value().toInt() );
3078 srsName =
"urn:ogc:def:crs:EPSG::" + QString::number( lit->
value().toInt() );
3083 srsName = lit->
value().toString();
3084 if ( srsName.startsWith(
"EPSG:"_L1, Qt::CaseInsensitive ) )
3089 QgsCoordinateReferenceSystem crs;
3090 if ( !srsName.isEmpty() )
3096 axisInversion = !axisInversion;
3106 if ( node->
name().compare(
"ST_GeometryFromText"_L1, Qt::CaseInsensitive ) == 0 )
3108 QList<QgsSQLStatement::Node *> args = node->
args()->
list();
3109 if ( args.size() != 1 && args.size() != 2 )
3111 mErrorMessage = QObject::tr(
"Function %1 should have 1 or 2 arguments" ).arg( node->
name() );
3112 return QDomElement();
3115 QgsSQLStatement::Node *firstFnArg = args[0];
3118 mErrorMessage = QObject::tr(
"%1: First argument must be string literal" ).arg( node->
name() );
3119 return QDomElement();
3124 if ( ! processSRSName( node, args, args.size() == 2, srsName, axisInversion ) )
3126 return QDomElement();
3129 const QString wkt =
static_cast<const QgsSQLStatement::NodeLiteral *
>( firstFnArg )->value().toString();
3132 u
"qgis_id_geom_%1"_s.arg( mGeomId ) );
3134 if ( geomElem.isNull() )
3136 mErrorMessage = QObject::tr(
"%1: invalid WKT" ).arg( node->
name() );
3137 return QDomElement();
3144 if ( node->
name().compare(
"ST_MakeEnvelope"_L1, Qt::CaseInsensitive ) == 0 )
3146 QList<QgsSQLStatement::Node *> args = node->
args()->
list();
3147 if ( args.size() != 4 && args.size() != 5 )
3149 mErrorMessage = QObject::tr(
"Function %1 should have 4 or 5 arguments" ).arg( node->
name() );
3150 return QDomElement();
3155 for (
int i = 0; i < 4; i++ )
3157 QgsSQLStatement::Node *arg = args[i];
3160 mErrorMessage = QObject::tr(
"%1: Argument %2 must be numeric literal" ).arg( node->
name() ).arg( i + 1 );
3161 return QDomElement();
3163 const QgsSQLStatement::NodeLiteral *lit =
static_cast<const QgsSQLStatement::NodeLiteral *
>( arg );
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();
3173 mErrorMessage = QObject::tr(
"%1 Argument %2 must be numeric literal" ).arg( node->
name() ).arg( i + 1 );
3174 return QDomElement();
3188 if ( ! processSRSName( node, args, args.size() == 5, srsName, axisInversion ) )
3190 return QDomElement();
3197 QgsOgcUtils::rectangleToGMLEnvelope( &rect, mDoc, srsName, axisInversion, 15 );
3201 if ( node->
name().compare(
"ST_GeomFromGML"_L1, Qt::CaseInsensitive ) == 0 )
3203 QList<QgsSQLStatement::Node *> args = node->
args()->
list();
3204 if ( args.size() != 1 )
3206 mErrorMessage = QObject::tr(
"Function %1 should have 1 argument" ).arg( node->
name() );
3207 return QDomElement();
3210 QgsSQLStatement::Node *firstFnArg = args[0];
3213 mErrorMessage = QObject::tr(
"%1: Argument must be string literal" ).arg( node->
name() );
3214 return QDomElement();
3217 QDomDocument geomDoc;
3218 const QString gml =
static_cast<const QgsSQLStatement::NodeLiteral *
>( firstFnArg )->value().toString();
3220 const QString xml = u
"<tmp xmlns:gml=\"%1\">%2</tmp>"_s.arg(
GML_NAMESPACE, gml );
3221 if ( !geomDoc.setContent( xml,
true ) )
3223 mErrorMessage = QObject::tr(
"ST_GeomFromGML: unable to parse XML" );
3224 return QDomElement();
3227 const QDomNode geomNode = mDoc.importNode( geomDoc.documentElement().firstChildElement(),
true );
3229 return geomNode.toElement();
3233 QString ogcName( mapBinarySpatialToOgc( node->
name() ) );
3234 if ( !ogcName.isEmpty() )
3236 QList<QgsSQLStatement::Node *> args = node->
args()->
list();
3237 if ( args.size() != 2 )
3239 mErrorMessage = QObject::tr(
"Function %1 should have 2 arguments" ).arg( node->
name() );
3240 return QDomElement();
3243 for (
int i = 0; i < 2; i ++ )
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 ) )
3249 mCurrentSRSName = getGeometryColumnSRSName( args[1 - i] );
3256 QDomElement funcElem = mDoc.createElement( mFilterPrefix +
":" + ogcName );
3257 const auto constArgs = args;
3258 for ( QgsSQLStatement::Node *n : constArgs )
3261 if ( !mErrorMessage.isEmpty() )
3263 mCurrentSRSName.clear();
3264 return QDomElement();
3267 funcElem.appendChild( childElem );
3270 mCurrentSRSName.clear();
3274 ogcName = mapTernarySpatialToOgc( node->
name() );
3275 if ( !ogcName.isEmpty() )
3277 QList<QgsSQLStatement::Node *> args = node->
args()->
list();
3278 if ( args.size() != 3 )
3280 mErrorMessage = QObject::tr(
"Function %1 should have 3 arguments" ).arg( node->
name() );
3281 return QDomElement();
3284 for (
int i = 0; i < 2; i ++ )
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 ) )
3290 mCurrentSRSName = getGeometryColumnSRSName( args[1 - i] );
3295 QDomElement funcElem = mDoc.createElement( mFilterPrefix +
":" + node->
name().mid( 3 ) );
3296 for (
int i = 0; i < 2; i++ )
3298 const QDomElement childElem =
toOgcFilter( args[i] );
3299 if ( !mErrorMessage.isEmpty() )
3301 mCurrentSRSName.clear();
3302 return QDomElement();
3305 funcElem.appendChild( childElem );
3307 mCurrentSRSName.clear();
3309 QgsSQLStatement::Node *distanceNode = args[2];
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();
3315 const QgsSQLStatement::NodeLiteral *lit =
static_cast<const QgsSQLStatement::NodeLiteral *
>( distanceNode );
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();
3322 QString unit( u
"m"_s );
3323 switch ( lit->
value().userType() )
3325 case QMetaType::Type::Int:
3326 distance = QString::number( lit->
value().toInt() );
3328 case QMetaType::Type::LongLong:
3329 distance = QString::number( lit->
value().toLongLong() );
3331 case QMetaType::Type::Double:
3334 case QMetaType::Type::QString:
3336 distance = lit->
value().toString();
3337 for (
int i = 0; i < distance.size(); i++ )
3339 if ( !( ( distance[i] >=
'0' && distance[i] <=
'9' ) || distance[i] ==
'-' || distance[i] ==
'.' || distance[i] ==
'e' || distance[i] ==
'E' ) )
3341 unit = distance.mid( i ).trimmed();
3342 distance = distance.mid( 0, i );
3350 mErrorMessage = QObject::tr(
"Literal type not supported: %1" ).arg(
static_cast<QMetaType::Type
>( lit->
value().userType() ) );
3351 return QDomElement();
3354 QDomElement distanceElem = mDoc.createElement( mFilterPrefix +
":Distance" );
3356 distanceElem.setAttribute( u
"uom"_s, unit );
3358 distanceElem.setAttribute( u
"unit"_s, unit );
3359 distanceElem.appendChild( mDoc.createTextNode( distance ) );
3360 funcElem.appendChild( distanceElem );
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 )
3371 if ( !mErrorMessage.isEmpty() )
3372 return QDomElement();
3374 funcElem.appendChild( childElem );
3380 const QString &leftTable )
3382 QgsSQLStatement::Node *onExpr = node->
onExpr();
3388 QList<QDomElement> listElem;
3390 for (
const QString &columnName : constUsingColumns )
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 );
3402 if ( listElem.size() == 1 )
3406 else if ( listElem.size() > 1 )
3408 QDomElement andElem = mDoc.createElement( mFilterPrefix +
":And" );
3409 const auto constListElem = listElem;
3410 for (
const QDomElement &elem : constListElem )
3412 andElem.appendChild( elem );
3417 return QDomElement();
3422 if ( node->
alias().isEmpty() )
3424 mMapTableAliasToNames[ node->
name()] = node->
name();
3428 mMapTableAliasToNames[ node->
alias()] = node->
name();
3434 QList<QDomElement> listElem;
3437 ( node->
tables().size() != 1 || !node->
joins().empty() ) )
3439 mErrorMessage = QObject::tr(
"Joins are only supported with WFS 2.0" );
3440 return QDomElement();
3444 const auto constTables = node->
tables();
3445 for ( QgsSQLStatement::NodeTableDef *table : constTables )
3449 const auto constJoins = node->
joins();
3450 for ( QgsSQLStatement::NodeJoin *join : constJoins )
3452 visit( join->tableDef() );
3456 const QList< QgsSQLStatement::NodeTableDef *> nodeTables = node->
tables();
3457 QString leftTable = nodeTables.at( nodeTables.length() - 1 )->name();
3458 for ( QgsSQLStatement::NodeJoin *join : constJoins )
3460 const QDomElement joinElem =
toOgcFilter( join, leftTable );
3461 if ( !mErrorMessage.isEmpty() )
3462 return QDomElement();
3463 listElem.append( joinElem );
3464 leftTable = join->tableDef()->name();
3468 if ( node->
where() )
3471 if ( !mErrorMessage.isEmpty() )
3472 return QDomElement();
3473 listElem.append( whereElem );
3477 if ( listElem.size() == 1 )
3481 else if ( listElem.size() > 1 )
3483 QDomElement andElem = mDoc.createElement( mFilterPrefix +
":And" );
3484 const auto constListElem = listElem;
3485 for (
const QDomElement &elem : constListElem )
3487 andElem.appendChild( elem );
3492 return QDomElement();
3498 mPropertyName = u
"PropertyName"_s;
3503 mPropertyName = u
"ValueReference"_s;
3510 if ( element.isNull() )
3514 if ( isBinaryOperator( element.tagName() ) )
3520 if ( isSpatialOperator( element.tagName() ) )
3526 if ( element.tagName() ==
"Not"_L1 )
3530 else if ( element.tagName() ==
"PropertyIsNull"_L1 )
3534 else if ( element.tagName() ==
"Literal"_L1 )
3538 else if ( element.tagName() ==
"Function"_L1 )
3542 else if ( element.tagName() == mPropertyName )
3546 else if ( element.tagName() ==
"PropertyIsBetween"_L1 )
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() );
3557 if ( element.isNull() )
3560 int op = binaryOperatorFromTagName( element.tagName() );
3563 mErrorMessage = QObject::tr(
"'%1' binary operator not supported." ).arg( element.tagName() );
3572 QDomElement operandElem = element.firstChildElement();
3577 mErrorMessage = QObject::tr(
"invalid left operand for '%1' binary operator" ).arg( element.tagName() );
3581 const std::unique_ptr<QgsExpressionNode> leftOp( expr->clone() );
3582 for ( operandElem = operandElem.nextSiblingElement(); !operandElem.isNull(); operandElem = operandElem.nextSiblingElement() )
3587 mErrorMessage = QObject::tr(
"invalid right operand for '%1' binary operator" ).arg( element.tagName() );
3594 if ( element.hasAttribute( u
"wildCard"_s ) )
3596 wildCard = element.attribute( u
"wildCard"_s );
3599 if ( element.hasAttribute( u
"singleChar"_s ) )
3601 singleChar = element.attribute( u
"singleChar"_s );
3603 QString escape = u
"\\"_s;
3604 if ( element.hasAttribute( u
"escape"_s ) )
3606 escape = element.attribute( u
"escape"_s );
3608 if ( element.hasAttribute( u
"escapeChar"_s ) )
3610 escape = element.attribute( u
"escapeChar"_s );
3614 if ( !wildCard.isEmpty() && wildCard !=
"%"_L1 )
3616 oprValue.replace(
'%',
"\\%"_L1 );
3617 if ( oprValue.startsWith( wildCard ) )
3619 oprValue.replace( 0, 1, u
"%"_s );
3622 QRegularExpressionMatch match = rx.match( oprValue );
3624 while ( match.hasMatch() )
3626 pos = match.capturedStart();
3627 oprValue.replace( pos + 1, 1, u
"%"_s );
3629 match = rx.match( oprValue, pos );
3631 oprValue.replace( escape + wildCard, wildCard );
3633 if ( !singleChar.isEmpty() && singleChar !=
"_"_L1 )
3635 oprValue.replace(
'_',
"\\_"_L1 );
3636 if ( oprValue.startsWith( singleChar ) )
3638 oprValue.replace( 0, 1, u
"_"_s );
3641 QRegularExpressionMatch match = rx.match( oprValue );
3643 while ( match.hasMatch() )
3645 pos = match.capturedStart();
3646 oprValue.replace( pos + 1, 1, u
"_"_s );
3648 match = rx.match( oprValue, pos );
3650 oprValue.replace( escape + singleChar, singleChar );
3652 if ( !escape.isEmpty() && escape !=
"\\"_L1 )
3654 oprValue.replace( escape + escape, escape );
3656 opRight = std::make_unique<QgsExpressionNodeLiteral>( oprValue );
3662 if ( expr == leftOp )
3664 mErrorMessage = QObject::tr(
"only one operand for '%1' binary operator" ).arg( element.tagName() );
3677 auto gml2Args = std::make_unique<QgsExpressionNode::NodeList>();
3678 QDomElement childElem = element.firstChildElement();
3680 while ( !childElem.isNull() && gml2Str.isEmpty() )
3682 if ( childElem.tagName() != mPropertyName )
3684 QTextStream gml2Stream( &gml2Str );
3685 childElem.save( gml2Stream, 0 );
3687 childElem = childElem.nextSiblingElement();
3689 if ( !gml2Str.isEmpty() )
3695 mErrorMessage = QObject::tr(
"No OGC Geometry found" );
3699 auto opArgs = std::make_unique<QgsExpressionNode::NodeList>();
3708 if ( element.isNull() || element.tagName() != mPropertyName )
3710 mErrorMessage = QObject::tr(
"%1:PropertyName expected, got %2" ).arg( mPrefix, element.tagName() );
3719 if ( element.isNull() || element.tagName() !=
"Literal"_L1 )
3721 mErrorMessage = QObject::tr(
"%1:Literal expected, got %2" ).arg( mPrefix, element.tagName() );
3725 std::unique_ptr<QgsExpressionNode> root;
3726 if ( !element.hasChildNodes() )
3728 root = std::make_unique<QgsExpressionNodeLiteral>( QVariant(
"" ) );
3729 return root.release();
3733 QDomNode childNode = element.firstChild();
3734 while ( !childNode.isNull() )
3736 std::unique_ptr<QgsExpressionNode> operand;
3738 if ( childNode.nodeType() == QDomNode::ElementNode )
3741 const QDomElement operandElem = childNode.toElement();
3745 mErrorMessage = QObject::tr(
"'%1' is an invalid or not supported content for %2:Literal" ).arg( operandElem.tagName(), mPrefix );
3752 QVariant value = childNode.nodeValue();
3754 bool converted =
false;
3759 QDomElement propertyNameElement = element.previousSiblingElement( mPropertyName );
3760 if ( propertyNameElement.isNull() || propertyNameElement.tagName() != mPropertyName )
3762 propertyNameElement = element.nextSiblingElement( mPropertyName );
3764 if ( !propertyNameElement.isNull() || propertyNameElement.tagName() == mPropertyName )
3766 const int fieldIndex = mLayer->fields().indexOf( propertyNameElement.firstChild().nodeValue() );
3767 if ( fieldIndex != -1 )
3769 const QgsField field = mLayer->fields().field( propertyNameElement.firstChild().nodeValue() );
3780 const double d = value.toDouble( &ok );
3785 operand = std::make_unique<QgsExpressionNodeLiteral>( value );
3791 root = std::move( operand );
3798 childNode = childNode.nextSibling();
3802 return root.release();
3809 if ( element.tagName() !=
"Not"_L1 )
3812 const QDomElement operandElem = element.firstChildElement();
3816 mErrorMessage = QObject::tr(
"invalid operand for '%1' unary operator" ).arg( element.tagName() );
3826 if ( element.tagName() !=
"PropertyIsNull"_L1 )
3831 const QDomElement operandElem = element.firstChildElement();
3842 if ( element.isNull() || element.tagName() !=
"Function"_L1 )
3844 mErrorMessage = QObject::tr(
"%1:Function expected, got %2" ).arg( mPrefix, element.tagName() );
3852 if ( element.attribute( u
"name"_s ) != funcDef->
name() )
3855 auto args = std::make_unique<QgsExpressionNode::NodeList>();
3857 QDomElement operandElem = element.firstChildElement();
3858 while ( !operandElem.isNull() )
3865 args->append( op.release() );
3867 operandElem = operandElem.nextSiblingElement();
3879 std::unique_ptr<QgsExpressionNode> operand;
3880 std::unique_ptr<QgsExpressionNode> lowerBound;
3881 std::unique_ptr<QgsExpressionNode> upperBound;
3883 QDomElement operandElem = element.firstChildElement();
3884 while ( !operandElem.isNull() )
3886 if ( operandElem.tagName() ==
"LowerBoundary"_L1 )
3888 const QDomElement lowerBoundElem = operandElem.firstChildElement();
3891 else if ( operandElem.tagName() ==
"UpperBoundary"_L1 )
3893 const QDomElement upperBoundElem = operandElem.firstChildElement();
3902 if ( operand && lowerBound && upperBound )
3905 operandElem = operandElem.nextSiblingElement();
3908 if ( !operand || !lowerBound || !upperBound )
3910 mErrorMessage = QObject::tr(
"missing some required sub-elements in %1:PropertyIsBetween" ).arg( mPrefix );
3921 return mErrorMessage;
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() )
3929 authority = u
"EPSG"_s;
3930 code = match.captured( 1 );
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() )
3937 authority = match.captured( 1 );
3938 code = match.captured( 2 );
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() )
3945 authority = match.captured( 1 );
3946 code = match.captured( 2 );
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() )
3953 authority = match.captured( 1 );
3954 code = match.captured( 2 );
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() )
3961 authority = match.captured( 1 );
3962 code = match.captured( 2 );
3969QgsGeometry QgsOgcUtils::geometryFromGMLUsingGdal(
const QDomElement &geometryElement )
3972 QTextStream gmlStream( &gml );
3973 geometryElement.save( gmlStream, 0 );
3978QgsGeometry QgsOgcUtils::geometryFromGMLMultiCurve(
const QDomElement &geometryElement )
3980 return geometryFromGMLUsingGdal( geometryElement );
GeometryOperationResult
Success or failure of a geometry operation.
@ Success
Operation succeeded.
WkbType
The WKB type describes the number of dimensions a geometry has.
@ LineString25D
LineString25D.
@ MultiPointZ
MultiPointZ.
@ MultiPolygon25D
MultiPolygon25D.
@ MultiLineString25D
MultiLineString25D.
@ MultiPolygon
MultiPolygon.
@ MultiLineString
MultiLineString.
@ MultiPoint25D
MultiPoint25D.
@ MultiLineStringZ
MultiLineStringZ.
@ MultiPolygonZ
MultiPolygonZ.
@ LineStringZ
LineStringZ.
virtual void swapXy()=0
Swaps the x and y coordinates from the geometry.
Qgis::WkbType readHeader() const
readHeader
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...
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.
BinaryOperator
list of binary operators
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.
bool convertCompatible(QVariant &v, QString *errorMessage=nullptr) const
Converts the provided variant to a compatible format.
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
@ 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 ...
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.
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.
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.
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.
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.
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.
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
QMap< QString, QString > QgsStringMap
QVector< QgsPointXY > QgsPolylineXY
Polyline as represented as a vector of two-dimensional points.
QVector< QgsPolyline > QgsMultiPolyline
Multi polyline represented as a vector of polylines.
QgsPointSequence QgsPolyline
Polyline as represented as a vector of points.
#define QgsDebugMsgLevel(str, level)
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.
const QgsMapLayer * layer
QgsCoordinateTransformContext transformContext