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
62 const QString &namespacePrefix,
63 const QString &namespaceURI,
64 const QString &geometryName,
65 const QString &srsName,
66 bool honourAxisOrientation,
67 bool invertAxisOrientation,
68 const QMap<QString, QString> &fieldNameToXPathMap,
69 const QMap<QString, QString> &namespacePrefixToUriMap
72 , mGMLVersion( gmlVersion )
73 , mFilterVersion( filterVersion )
74 , mNamespacePrefix( namespacePrefix )
75 , mNamespaceURI( namespaceURI )
76 , mGeometryName( geometryName )
78 , mInvertAxisOrientation( invertAxisOrientation )
79 , mFieldNameToXPathMap( fieldNameToXPathMap )
80 , mNamespacePrefixToUriMap( namespacePrefixToUriMap )
81 , mFilterPrefix( ( filterVersion ==
QgsOgcUtils::FILTER_FES_2_0 ) ?
"fes" :
"ogc" )
82 , mPropertyName( ( filterVersion ==
QgsOgcUtils::FILTER_FES_2_0 ) ?
"ValueReference" :
"PropertyName" )
85 if ( !mSrsName.isEmpty() )
89 if ( honourAxisOrientation && crs.hasAxisInverted() )
91 mInvertAxisOrientation = !mInvertAxisOrientation;
98 QDomElement geometryTypeElement = geometryNode.toElement();
99 QString geomType = geometryTypeElement.tagName();
102 if ( !( geomType ==
"Point"_L1
103 || geomType ==
"LineString"_L1
104 || geomType ==
"Polygon"_L1
105 || geomType ==
"MultiPoint"_L1
106 || geomType ==
"MultiLineString"_L1
107 || geomType ==
"MultiPolygon"_L1
108 || geomType ==
"Box"_L1
109 || geomType ==
"Envelope"_L1
110 || geomType ==
"MultiCurve"_L1 ) )
112 const QDomNode geometryChild = geometryNode.firstChild();
113 if ( geometryChild.isNull() )
117 geometryTypeElement = geometryChild.toElement();
118 geomType = geometryTypeElement.tagName();
121 if ( !( geomType ==
"Point"_L1
122 || geomType ==
"LineString"_L1
123 || geomType ==
"Polygon"_L1
124 || geomType ==
"MultiPoint"_L1
125 || geomType ==
"MultiLineString"_L1
126 || geomType ==
"MultiPolygon"_L1
127 || geomType ==
"Box"_L1
128 || geomType ==
"Envelope"_L1
129 || geomType ==
"MultiCurve"_L1 ) )
132 if ( geomType ==
"Point"_L1 )
134 geometry = geometryFromGMLPoint( geometryTypeElement );
136 else if ( geomType ==
"LineString"_L1 )
138 geometry = geometryFromGMLLineString( geometryTypeElement );
140 else if ( geomType ==
"Polygon"_L1 )
142 geometry = geometryFromGMLPolygon( geometryTypeElement );
144 else if ( geomType ==
"MultiPoint"_L1 )
146 geometry = geometryFromGMLMultiPoint( geometryTypeElement );
148 else if ( geomType ==
"MultiLineString"_L1 )
150 geometry = geometryFromGMLMultiLineString( geometryTypeElement );
152 else if ( geomType ==
"MultiCurve"_L1 )
154 geometry = geometryFromGMLMultiCurve( geometryTypeElement );
156 else if ( geomType ==
"MultiPolygon"_L1 )
158 geometry = geometryFromGMLMultiPolygon( geometryTypeElement );
160 else if ( geomType ==
"Box"_L1 )
164 else if ( geomType ==
"Envelope"_L1 )
177 if ( geometryTypeElement.hasAttribute( u
"srsName"_s ) )
179 QString srsName { geometryTypeElement.attribute( u
"srsName"_s ) };
182 const bool ignoreAxisOrientation { srsName.startsWith(
"http://www.opengis.net/gml/srs/"_L1 ) || srsName.startsWith(
"EPSG:"_L1 ) };
186 if ( srsName.startsWith(
"http://www.opengis.net/gml/srs/"_L1 ) )
188 const auto parts { srsName.split( QRegularExpression( QStringLiteral( R
"raw(/|#|\.)raw" ) ) ) };
189 if ( parts.length() == 10 )
191 srsName = u
"http://www.opengis.net/def/crs/%1/0/%2"_s.arg( parts[7].toUpper(), parts[9] );
225 const QString xml = u
"<tmp xmlns=\"%1\" xmlns:gml=\"%1\">%2</tmp>"_s.arg(
GML_NAMESPACE, xmlString );
227 if ( !doc.setContent( xml,
true ) )
230 return geometryFromGML( doc.documentElement().firstChildElement(), context );
234QgsGeometry QgsOgcUtils::geometryFromGMLPoint(
const QDomElement &geometryElement )
238 const QDomNodeList coordList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, u
"coordinates"_s );
239 if ( !coordList.isEmpty() )
241 const QDomElement coordElement = coordList.at( 0 ).toElement();
242 if ( readGMLCoordinates( pointCoordinate, coordElement ) != 0 )
249 const QDomNodeList posList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, u
"pos"_s );
250 if ( posList.size() < 1 )
252 return QgsGeometry();
254 const QDomElement posElement = posList.at( 0 ).toElement();
255 if ( readGMLPositions( pointCoordinate, posElement ) != 0 )
257 return QgsGeometry();
261 if ( pointCoordinate.empty() )
263 return QgsGeometry();
266 const bool hasZ { !std::isnan( pointCoordinate.first().z() ) };
267 QgsPolyline::const_iterator point_it = pointCoordinate.constBegin();
268 const char e =
static_cast<char>( htonl( 1 ) != 1 );
269 const double x = point_it->x();
270 const double y = point_it->y();
271 const int size = 1 +
static_cast<int>(
sizeof( int ) ) + ( hasZ ? 3 : 2 ) *
static_cast<int>(
sizeof( double ) );
274 unsigned char *wkb =
new unsigned char[size];
277 memcpy( &( wkb )[wkbPosition], &e, 1 );
279 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
280 wkbPosition +=
sizeof( int );
281 memcpy( &( wkb )[wkbPosition], &x,
sizeof(
double ) );
282 wkbPosition +=
sizeof( double );
283 memcpy( &( wkb )[wkbPosition], &y,
sizeof(
double ) );
287 wkbPosition +=
sizeof( double );
288 double z = point_it->z();
289 memcpy( &( wkb )[wkbPosition], &z,
sizeof(
double ) );
297QgsGeometry QgsOgcUtils::geometryFromGMLLineString(
const QDomElement &geometryElement )
301 const QDomNodeList coordList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, u
"coordinates"_s );
302 if ( !coordList.isEmpty() )
304 const QDomElement coordElement = coordList.at( 0 ).toElement();
305 if ( readGMLCoordinates( lineCoordinates, coordElement ) != 0 )
307 return QgsGeometry();
312 const QDomNodeList posList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, u
"posList"_s );
313 if ( posList.size() < 1 )
315 return QgsGeometry();
317 const QDomElement posElement = posList.at( 0 ).toElement();
318 if ( readGMLPositions( lineCoordinates, posElement ) != 0 )
320 return QgsGeometry();
324 const bool hasZ { !std::isnan( lineCoordinates.first().z() ) };
326 char e =
static_cast<char>( htonl( 1 ) != 1 );
327 const int size = 1 + 2 *
static_cast<int>(
sizeof( int ) + lineCoordinates.size() ) * ( hasZ ? 3 : 2 ) *
static_cast<int>(
sizeof(
double ) );
330 unsigned char *wkb =
new unsigned char[size];
334 int nPoints = lineCoordinates.size();
337 memcpy( &( wkb )[wkbPosition], &e, 1 );
339 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
340 wkbPosition +=
sizeof( int );
341 memcpy( &( wkb )[wkbPosition], &nPoints,
sizeof(
int ) );
342 wkbPosition +=
sizeof( int );
344 QgsPolyline::const_iterator iter;
345 for ( iter = lineCoordinates.constBegin(); iter != lineCoordinates.constEnd(); ++iter )
349 memcpy( &( wkb )[wkbPosition], &x,
sizeof(
double ) );
350 wkbPosition +=
sizeof( double );
351 memcpy( &( wkb )[wkbPosition], &y,
sizeof(
double ) );
352 wkbPosition +=
sizeof( double );
356 double z = iter->z();
357 memcpy( &( wkb )[wkbPosition], &z,
sizeof(
double ) );
358 wkbPosition +=
sizeof( double );
367QgsGeometry QgsOgcUtils::geometryFromGMLPolygon(
const QDomElement &geometryElement )
374 const QDomNodeList outerBoundaryList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, u
"outerBoundaryIs"_s );
375 if ( !outerBoundaryList.isEmpty() )
377 QDomElement coordinatesElement = outerBoundaryList.at( 0 ).firstChild().firstChild().toElement();
378 if ( coordinatesElement.isNull() )
380 return QgsGeometry();
382 if ( readGMLCoordinates( exteriorPointList, coordinatesElement ) != 0 )
384 return QgsGeometry();
386 ringCoordinates.push_back( exteriorPointList );
389 const QDomNodeList innerBoundaryList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, u
"innerBoundaryIs"_s );
390 for (
int i = 0; i < innerBoundaryList.size(); ++i )
393 coordinatesElement = innerBoundaryList.at( i ).firstChild().firstChild().toElement();
394 if ( coordinatesElement.isNull() )
396 return QgsGeometry();
398 if ( readGMLCoordinates( interiorPointList, coordinatesElement ) != 0 )
400 return QgsGeometry();
402 ringCoordinates.push_back( interiorPointList );
408 const QDomNodeList exteriorList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, u
"exterior"_s );
409 if ( exteriorList.size() < 1 )
411 return QgsGeometry();
413 const QDomElement posElement = exteriorList.at( 0 ).firstChild().firstChild().toElement();
414 if ( posElement.isNull() )
416 return QgsGeometry();
418 if ( readGMLPositions( exteriorPointList, posElement ) != 0 )
420 return QgsGeometry();
422 ringCoordinates.push_back( exteriorPointList );
425 const QDomNodeList interiorList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, u
"interior"_s );
426 for (
int i = 0; i < interiorList.size(); ++i )
429 const QDomElement posElement = interiorList.at( i ).firstChild().firstChild().toElement();
430 if ( posElement.isNull() )
432 return QgsGeometry();
435 if ( readGMLPositions( interiorPointList, posElement ) )
437 return QgsGeometry();
439 ringCoordinates.push_back( interiorPointList );
444 int nrings = ringCoordinates.size();
446 return QgsGeometry();
449 for ( QgsMultiPolyline::const_iterator it = ringCoordinates.constBegin(); it != ringCoordinates.constEnd(); ++it )
451 npoints += it->size();
454 const bool hasZ { !std::isnan( ringCoordinates.first().first().z() ) };
456 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 ) );
459 unsigned char *wkb =
new unsigned char[size];
462 char e =
static_cast<char>( htonl( 1 ) != 1 );
464 int nPointsInRing = 0;
468 memcpy( &( wkb )[wkbPosition], &e, 1 );
470 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
471 wkbPosition +=
sizeof( int );
472 memcpy( &( wkb )[wkbPosition], &nrings,
sizeof(
int ) );
473 wkbPosition +=
sizeof( int );
474 for ( QgsMultiPolyline::const_iterator it = ringCoordinates.constBegin(); it != ringCoordinates.constEnd(); ++it )
476 nPointsInRing = it->size();
477 memcpy( &( wkb )[wkbPosition], &nPointsInRing,
sizeof(
int ) );
478 wkbPosition +=
sizeof( int );
480 QgsPolyline::const_iterator iter;
481 for ( iter = it->begin(); iter != it->end(); ++iter )
486 memcpy( &( wkb )[wkbPosition], &x,
sizeof(
double ) );
487 wkbPosition +=
sizeof( double );
488 memcpy( &( wkb )[wkbPosition], &y,
sizeof(
double ) );
489 wkbPosition +=
sizeof( double );
494 memcpy( &( wkb )[wkbPosition], &z,
sizeof(
double ) );
495 wkbPosition +=
sizeof( double );
505QgsGeometry QgsOgcUtils::geometryFromGMLMultiPoint(
const QDomElement &geometryElement )
509 const QDomNodeList pointMemberList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, u
"pointMember"_s );
510 if ( pointMemberList.size() < 1 )
512 return QgsGeometry();
514 QDomNodeList pointNodeList;
516 QDomNodeList coordinatesList;
517 QDomNodeList posList;
518 for (
int i = 0; i < pointMemberList.size(); ++i )
521 pointNodeList = pointMemberList.at( i ).toElement().elementsByTagNameNS(
GML_NAMESPACE, u
"Point"_s );
522 if ( pointNodeList.size() < 1 )
527 coordinatesList = pointNodeList.at( 0 ).toElement().elementsByTagNameNS(
GML_NAMESPACE, u
"coordinates"_s );
528 if ( !coordinatesList.isEmpty() )
530 currentPoint.clear();
531 if ( readGMLCoordinates( currentPoint, coordinatesList.at( 0 ).toElement() ) != 0 )
535 if ( currentPoint.empty() )
539 pointList.push_back( ( *currentPoint.begin() ) );
545 posList = pointNodeList.at( 0 ).toElement().elementsByTagNameNS(
GML_NAMESPACE, u
"pos"_s );
546 if ( posList.size() < 1 )
550 currentPoint.clear();
551 if ( readGMLPositions( currentPoint, posList.at( 0 ).toElement() ) != 0 )
555 if ( currentPoint.empty() )
559 pointList.push_back( ( *currentPoint.begin() ) );
563 int nPoints = pointList.size();
565 return QgsGeometry();
567 const bool hasZ { !std::isnan( pointList.first().z() ) };
570 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 ) ) );
573 unsigned char *wkb =
new unsigned char[size];
576 char e =
static_cast<char>( htonl( 1 ) != 1 );
579 memcpy( &( wkb )[wkbPosition], &e, 1 );
581 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
582 wkbPosition +=
sizeof( int );
583 memcpy( &( wkb )[wkbPosition], &nPoints,
sizeof(
int ) );
584 wkbPosition +=
sizeof( int );
586 for ( QgsPolyline::const_iterator it = pointList.constBegin(); it != pointList.constEnd(); ++it )
588 memcpy( &( wkb )[wkbPosition], &e, 1 );
590 memcpy( &( wkb )[wkbPosition], &pointType,
sizeof(
int ) );
591 wkbPosition +=
sizeof( int );
593 memcpy( &( wkb )[wkbPosition], &x,
sizeof(
double ) );
594 wkbPosition +=
sizeof( double );
596 memcpy( &( wkb )[wkbPosition], &y,
sizeof(
double ) );
597 wkbPosition +=
sizeof( double );
602 memcpy( &( wkb )[wkbPosition], &z,
sizeof(
double ) );
603 wkbPosition +=
sizeof( double );
612QgsGeometry QgsOgcUtils::geometryFromGMLMultiLineString(
const QDomElement &geometryElement )
624 QList< QgsPolyline > lineCoordinates;
625 QDomElement currentLineStringElement;
626 QDomNodeList currentCoordList;
627 QDomNodeList currentPosList;
629 const QDomNodeList lineStringMemberList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, u
"lineStringMember"_s );
630 if ( !lineStringMemberList.isEmpty() )
632 for (
int i = 0; i < lineStringMemberList.size(); ++i )
634 const QDomNodeList lineStringNodeList = lineStringMemberList.at( i ).toElement().elementsByTagNameNS(
GML_NAMESPACE, u
"LineString"_s );
635 if ( lineStringNodeList.size() < 1 )
637 return QgsGeometry();
639 currentLineStringElement = lineStringNodeList.at( 0 ).toElement();
640 currentCoordList = currentLineStringElement.elementsByTagNameNS(
GML_NAMESPACE, u
"coordinates"_s );
641 if ( !currentCoordList.isEmpty() )
644 if ( readGMLCoordinates( currentPointList, currentCoordList.at( 0 ).toElement() ) != 0 )
646 return QgsGeometry();
648 lineCoordinates.push_back( currentPointList );
652 currentPosList = currentLineStringElement.elementsByTagNameNS(
GML_NAMESPACE, u
"posList"_s );
653 if ( currentPosList.size() < 1 )
655 return QgsGeometry();
658 if ( readGMLPositions( currentPointList, currentPosList.at( 0 ).toElement() ) != 0 )
660 return QgsGeometry();
662 lineCoordinates.push_back( currentPointList );
668 const QDomNodeList lineStringList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, u
"LineString"_s );
669 if ( !lineStringList.isEmpty() )
671 for (
int i = 0; i < lineStringList.size(); ++i )
673 currentLineStringElement = lineStringList.at( i ).toElement();
674 currentCoordList = currentLineStringElement.elementsByTagNameNS(
GML_NAMESPACE, u
"coordinates"_s );
675 if ( !currentCoordList.isEmpty() )
678 if ( readGMLCoordinates( currentPointList, currentCoordList.at( 0 ).toElement() ) != 0 )
680 return QgsGeometry();
682 lineCoordinates.push_back( currentPointList );
683 return QgsGeometry();
687 currentPosList = currentLineStringElement.elementsByTagNameNS(
GML_NAMESPACE, u
"posList"_s );
688 if ( currentPosList.size() < 1 )
690 return QgsGeometry();
693 if ( readGMLPositions( currentPointList, currentPosList.at( 0 ).toElement() ) != 0 )
695 return QgsGeometry();
697 lineCoordinates.push_back( currentPointList );
703 return QgsGeometry();
707 int nLines = lineCoordinates.size();
709 return QgsGeometry();
711 const bool hasZ { !std::isnan( lineCoordinates.first().first().z() ) };
712 const int coordSize { hasZ ? 3 : 2 };
715 int size =
static_cast<int>( lineCoordinates.size() + 1 ) * ( 1 + 2 *
sizeof( int ) );
716 for ( QList< QgsPolyline >::const_iterator it = lineCoordinates.constBegin(); it != lineCoordinates.constEnd(); ++it )
718 size += it->size() * coordSize *
sizeof( double );
722 unsigned char *wkb =
new unsigned char[size];
725 char e =
static_cast<char>( htonl( 1 ) != 1 );
729 memcpy( &( wkb )[wkbPosition], &e, 1 );
731 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
732 wkbPosition +=
sizeof( int );
733 memcpy( &( wkb )[wkbPosition], &nLines,
sizeof(
int ) );
734 wkbPosition +=
sizeof( int );
736 for ( QList< QgsPolyline >::const_iterator it = lineCoordinates.constBegin(); it != lineCoordinates.constEnd(); ++it )
738 memcpy( &( wkb )[wkbPosition], &e, 1 );
740 memcpy( &( wkb )[wkbPosition], &lineType,
sizeof(
int ) );
741 wkbPosition +=
sizeof( int );
742 nPoints = it->size();
743 memcpy( &( wkb )[wkbPosition], &nPoints,
sizeof(
int ) );
744 wkbPosition +=
sizeof( int );
745 for ( QgsPolyline::const_iterator iter = it->begin(); iter != it->end(); ++iter )
750 memcpy( &( wkb )[wkbPosition], &x,
sizeof(
double ) );
751 wkbPosition +=
sizeof( double );
752 memcpy( &( wkb )[wkbPosition], &y,
sizeof(
double ) );
753 wkbPosition +=
sizeof( double );
758 memcpy( &( wkb )[wkbPosition], &z,
sizeof(
double ) );
759 wkbPosition +=
sizeof( double );
769QgsGeometry QgsOgcUtils::geometryFromGMLMultiPolygon(
const QDomElement &geometryElement )
772 QVector<QgsMultiPolyline> multiPolygonPoints;
773 QDomElement currentPolygonMemberElement;
774 QDomNodeList polygonList;
775 QDomElement currentPolygonElement;
777 QDomNodeList outerBoundaryList;
778 QDomElement currentOuterBoundaryElement;
779 QDomElement currentInnerBoundaryElement;
781 QDomNodeList exteriorList;
782 QDomElement currentExteriorElement;
783 QDomElement currentInteriorElement;
785 QDomNodeList linearRingNodeList;
786 QDomElement currentLinearRingElement;
788 QDomNodeList currentCoordinateList;
789 QDomNodeList currentPosList;
791 const QDomNodeList polygonMemberList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, u
"polygonMember"_s );
793 for (
int i = 0; i < polygonMemberList.size(); ++i )
795 currentPolygonList.resize( 0 );
796 currentPolygonMemberElement = polygonMemberList.at( i ).toElement();
797 polygonList = currentPolygonMemberElement.elementsByTagNameNS(
GML_NAMESPACE, u
"Polygon"_s );
798 if ( polygonList.size() < 1 )
802 currentPolygonElement = polygonList.at( 0 ).toElement();
805 outerBoundaryList = currentPolygonElement.elementsByTagNameNS(
GML_NAMESPACE, u
"outerBoundaryIs"_s );
806 if ( !outerBoundaryList.isEmpty() )
808 currentOuterBoundaryElement = outerBoundaryList.at( 0 ).toElement();
811 linearRingNodeList = currentOuterBoundaryElement.elementsByTagNameNS(
GML_NAMESPACE, u
"LinearRing"_s );
812 if ( linearRingNodeList.size() < 1 )
816 currentLinearRingElement = linearRingNodeList.at( 0 ).toElement();
817 currentCoordinateList = currentLinearRingElement.elementsByTagNameNS(
GML_NAMESPACE, u
"coordinates"_s );
818 if ( currentCoordinateList.size() < 1 )
822 if ( readGMLCoordinates( ringCoordinates, currentCoordinateList.at( 0 ).toElement() ) != 0 )
826 currentPolygonList.push_back( ringCoordinates );
829 const QDomNodeList innerBoundaryList = currentPolygonElement.elementsByTagNameNS(
GML_NAMESPACE, u
"innerBoundaryIs"_s );
830 for (
int j = 0; j < innerBoundaryList.size(); ++j )
833 currentInnerBoundaryElement = innerBoundaryList.at( j ).toElement();
834 linearRingNodeList = currentInnerBoundaryElement.elementsByTagNameNS(
GML_NAMESPACE, u
"LinearRing"_s );
835 if ( linearRingNodeList.size() < 1 )
839 currentLinearRingElement = linearRingNodeList.at( 0 ).toElement();
840 currentCoordinateList = currentLinearRingElement.elementsByTagNameNS(
GML_NAMESPACE, u
"coordinates"_s );
841 if ( currentCoordinateList.size() < 1 )
845 if ( readGMLCoordinates( ringCoordinates, currentCoordinateList.at( 0 ).toElement() ) != 0 )
849 currentPolygonList.push_back( ringCoordinates );
855 exteriorList = currentPolygonElement.elementsByTagNameNS(
GML_NAMESPACE, u
"exterior"_s );
856 if ( exteriorList.size() < 1 )
861 currentExteriorElement = exteriorList.at( 0 ).toElement();
864 linearRingNodeList = currentExteriorElement.elementsByTagNameNS(
GML_NAMESPACE, u
"LinearRing"_s );
865 if ( linearRingNodeList.size() < 1 )
869 currentLinearRingElement = linearRingNodeList.at( 0 ).toElement();
870 currentPosList = currentLinearRingElement.elementsByTagNameNS(
GML_NAMESPACE, u
"posList"_s );
871 if ( currentPosList.size() < 1 )
875 if ( readGMLPositions( ringPositions, currentPosList.at( 0 ).toElement() ) != 0 )
879 currentPolygonList.push_back( ringPositions );
882 const QDomNodeList interiorList = currentPolygonElement.elementsByTagNameNS(
GML_NAMESPACE, u
"interior"_s );
883 for (
int j = 0; j < interiorList.size(); ++j )
886 currentInteriorElement = interiorList.at( j ).toElement();
887 linearRingNodeList = currentInteriorElement.elementsByTagNameNS(
GML_NAMESPACE, u
"LinearRing"_s );
888 if ( linearRingNodeList.size() < 1 )
892 currentLinearRingElement = linearRingNodeList.at( 0 ).toElement();
893 currentPosList = currentLinearRingElement.elementsByTagNameNS(
GML_NAMESPACE, u
"posList"_s );
894 if ( currentPosList.size() < 1 )
898 if ( readGMLPositions( ringPositions, currentPosList.at( 0 ).toElement() ) != 0 )
902 currentPolygonList.push_back( ringPositions );
905 multiPolygonPoints.push_back( currentPolygonList );
908 int nPolygons = multiPolygonPoints.size();
910 return QgsGeometry();
912 const bool hasZ { !std::isnan( multiPolygonPoints.first().first().first().z() ) };
914 int size = 1 + 2 *
sizeof( int );
917 for (
auto it = multiPolygonPoints.constBegin(); it != multiPolygonPoints.constEnd(); ++it )
919 size += 1 + 2 *
sizeof( int );
920 for (
auto iter = it->begin(); iter != it->end(); ++iter )
922 size +=
static_cast<int>(
sizeof( int ) ) + ( hasZ ? 3 : 2 ) *
static_cast<int>( iter->size() *
sizeof(
double ) );
927 unsigned char *wkb =
new unsigned char[size];
929 char e =
static_cast<char>( htonl( 1 ) != 1 );
936 memcpy( &( wkb )[wkbPosition], &e, 1 );
938 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
939 wkbPosition +=
sizeof( int );
940 memcpy( &( wkb )[wkbPosition], &nPolygons,
sizeof(
int ) );
941 wkbPosition +=
sizeof( int );
945 for (
auto it = multiPolygonPoints.constBegin(); it != multiPolygonPoints.constEnd(); ++it )
947 memcpy( &( wkb )[wkbPosition], &e, 1 );
949 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
950 wkbPosition +=
sizeof( int );
952 memcpy( &( wkb )[wkbPosition], &nRings,
sizeof(
int ) );
953 wkbPosition +=
sizeof( int );
954 for (
auto iter = it->begin(); iter != it->end(); ++iter )
956 nPointsInRing = iter->size();
957 memcpy( &( wkb )[wkbPosition], &nPointsInRing,
sizeof(
int ) );
958 wkbPosition +=
sizeof( int );
959 for (
auto iterator = iter->begin(); iterator != iter->end(); ++iterator )
963 memcpy( &( wkb )[wkbPosition], &x,
sizeof(
double ) );
964 wkbPosition +=
sizeof( double );
965 memcpy( &( wkb )[wkbPosition], &y,
sizeof(
double ) );
966 wkbPosition +=
sizeof( double );
969 double z = iterator->z();
970 memcpy( &( wkb )[wkbPosition], &z,
sizeof(
double ) );
971 wkbPosition +=
sizeof( double );
982QDomElement QgsOgcUtils::filterElement( QDomDocument &doc, GMLVersion gmlVersion, FilterVersion filterVersion,
bool GMLUsed )
988 QDomAttr attr = doc.createAttribute( u
"xmlns:gml"_s );
993 filterElem.setAttributeNode( attr );
999bool QgsOgcUtils::readGMLCoordinates(
QgsPolyline &coords,
const QDomElement &elem )
1001 QString coordSeparator = u
","_s;
1002 QString tupleSeparator = u
" "_s;
1007 if ( elem.hasAttribute( u
"cs"_s ) )
1009 coordSeparator = elem.attribute( u
"cs"_s );
1011 if ( elem.hasAttribute( u
"ts"_s ) )
1013 tupleSeparator = elem.attribute( u
"ts"_s );
1016 const QStringList tupels = elem.text().split( tupleSeparator, Qt::SkipEmptyParts );
1017 QStringList tuple_coords;
1019 bool conversionSuccess;
1021 QStringList::const_iterator it;
1022 for ( it = tupels.constBegin(); it != tupels.constEnd(); ++it )
1024 tuple_coords = ( *it ).split( coordSeparator, Qt::SkipEmptyParts );
1025 if ( tuple_coords.size() < 2 )
1029 x = tuple_coords.at( 0 ).toDouble( &conversionSuccess );
1030 if ( !conversionSuccess )
1034 y = tuple_coords.at( 1 ).toDouble( &conversionSuccess );
1035 if ( !conversionSuccess )
1039 if ( tuple_coords.size() > 2 )
1041 z = tuple_coords.at( 2 ).toDouble( &conversionSuccess );
1042 if ( !conversionSuccess )
1049 z = std::numeric_limits<double>::quiet_NaN();
1051 coords.append( QgsPoint( x, y, z ) );
1060 const QDomElement boxElem = boxNode.toElement();
1061 if ( boxElem.tagName() !=
"Box"_L1 )
1064 const QDomElement bElem = boxElem.firstChild().toElement();
1065 QString coordSeparator = u
","_s;
1066 QString tupleSeparator = u
" "_s;
1067 if ( bElem.hasAttribute( u
"cs"_s ) )
1069 coordSeparator = bElem.attribute( u
"cs"_s );
1071 if ( bElem.hasAttribute( u
"ts"_s ) )
1073 tupleSeparator = bElem.attribute( u
"ts"_s );
1076 const QString bString = bElem.text();
1077 bool ok1, ok2, ok3, ok4;
1078 const double xmin = bString.section( tupleSeparator, 0, 0 ).section( coordSeparator, 0, 0 ).toDouble( &ok1 );
1079 const double ymin = bString.section( tupleSeparator, 0, 0 ).section( coordSeparator, 1, 1 ).toDouble( &ok2 );
1080 const double xmax = bString.section( tupleSeparator, 1, 1 ).section( coordSeparator, 0, 0 ).toDouble( &ok3 );
1081 const double ymax = bString.section( tupleSeparator, 1, 1 ).section( coordSeparator, 1, 1 ).toDouble( &ok4 );
1083 if ( ok1 && ok2 && ok3 && ok4 )
1092bool QgsOgcUtils::readGMLPositions(
QgsPolyline &coords,
const QDomElement &elem )
1096 const QStringList pos = elem.text().split(
' ', Qt::SkipEmptyParts );
1098 bool conversionSuccess;
1099 const int posSize = pos.size();
1101 int srsDimension = 2;
1102 if ( elem.hasAttribute( u
"srsDimension"_s ) )
1104 srsDimension = elem.attribute( u
"srsDimension"_s ).toInt( &conversionSuccess );
1105 if ( !conversionSuccess )
1110 else if ( elem.hasAttribute( u
"dimension"_s ) )
1112 srsDimension = elem.attribute( u
"dimension"_s ).toInt( &conversionSuccess );
1113 if ( !conversionSuccess )
1119 for (
int i = 0; i < posSize / srsDimension; i++ )
1121 x = pos.at( i * srsDimension ).toDouble( &conversionSuccess );
1122 if ( !conversionSuccess )
1126 y = pos.at( i * srsDimension + 1 ).toDouble( &conversionSuccess );
1127 if ( !conversionSuccess )
1131 if ( srsDimension > 2 )
1133 z = pos.at( i * srsDimension + 2 ).toDouble( &conversionSuccess );
1134 if ( !conversionSuccess )
1141 z = std::numeric_limits<double>::quiet_NaN();
1143 coords.append( QgsPoint( x, y, z ) );
1153 const QDomElement envelopeElem = envelopeNode.toElement();
1154 if ( envelopeElem.tagName() !=
"Envelope"_L1 )
1157 const QDomNodeList lowerCornerList = envelopeElem.elementsByTagNameNS(
GML_NAMESPACE, u
"lowerCorner"_s );
1158 if ( lowerCornerList.size() < 1 )
1161 const QDomNodeList upperCornerList = envelopeElem.elementsByTagNameNS(
GML_NAMESPACE, u
"upperCorner"_s );
1162 if ( upperCornerList.size() < 1 )
1165 bool conversionSuccess;
1166 int srsDimension = 2;
1168 QDomElement elem = lowerCornerList.at( 0 ).toElement();
1169 if ( elem.hasAttribute( u
"srsDimension"_s ) )
1171 srsDimension = elem.attribute( u
"srsDimension"_s ).toInt( &conversionSuccess );
1172 if ( !conversionSuccess )
1177 else if ( elem.hasAttribute( u
"dimension"_s ) )
1179 srsDimension = elem.attribute( u
"dimension"_s ).toInt( &conversionSuccess );
1180 if ( !conversionSuccess )
1185 QString bString = elem.text();
1187 const double xmin = bString.section(
' ', 0, 0 ).toDouble( &conversionSuccess );
1188 if ( !conversionSuccess )
1190 const double ymin = bString.section(
' ', 1, 1 ).toDouble( &conversionSuccess );
1191 if ( !conversionSuccess )
1194 elem = upperCornerList.at( 0 ).toElement();
1195 if ( elem.hasAttribute( u
"srsDimension"_s ) )
1197 srsDimension = elem.attribute( u
"srsDimension"_s ).toInt( &conversionSuccess );
1198 if ( !conversionSuccess )
1203 else if ( elem.hasAttribute( u
"dimension"_s ) )
1205 srsDimension = elem.attribute( u
"dimension"_s ).toInt( &conversionSuccess );
1206 if ( !conversionSuccess )
1212 Q_UNUSED( srsDimension )
1214 bString = elem.text();
1215 const double xmax = bString.section(
' ', 0, 0 ).toDouble( &conversionSuccess );
1216 if ( !conversionSuccess )
1218 const double ymax = bString.section(
' ', 1, 1 ).toDouble( &conversionSuccess );
1219 if ( !conversionSuccess )
1237 return QDomElement();
1240 QDomElement boxElem = doc.createElement( u
"gml:Box"_s );
1241 if ( !srsName.isEmpty() )
1243 boxElem.setAttribute( u
"srsName"_s, srsName );
1245 QDomElement coordElem = doc.createElement( u
"gml:coordinates"_s );
1246 coordElem.setAttribute( u
"cs"_s, u
","_s );
1247 coordElem.setAttribute( u
"ts"_s, u
" "_s );
1249 QString coordString;
1258 const QDomText coordText = doc.createTextNode( coordString );
1259 coordElem.appendChild( coordText );
1260 boxElem.appendChild( coordElem );
1274 return QDomElement();
1277 QDomElement envElem = doc.createElement( u
"gml:Envelope"_s );
1278 if ( !srsName.isEmpty() )
1280 envElem.setAttribute( u
"srsName"_s, srsName );
1284 QDomElement lowerCornerElem = doc.createElement( u
"gml:lowerCorner"_s );
1288 const QDomText lowerCornerText = doc.createTextNode( posList );
1289 lowerCornerElem.appendChild( lowerCornerText );
1290 envElem.appendChild( lowerCornerElem );
1292 QDomElement upperCornerElem = doc.createElement( u
"gml:upperCorner"_s );
1296 const QDomText upperCornerText = doc.createTextNode( posList );
1297 upperCornerElem.appendChild( upperCornerText );
1298 envElem.appendChild( upperCornerElem );
1311 return QDomElement();
1314 QString cs = u
","_s;
1316 const QString ts = u
" "_s;
1318 QDomElement baseCoordElem;
1320 bool hasZValue =
false;
1322 const QByteArray wkb( geometry.
asWkb() );
1332 return QDomElement();
1345 baseCoordElem = doc.createElement( u
"gml:pos"_s );
1348 baseCoordElem = doc.createElement( u
"gml:posList"_s );
1355 baseCoordElem = doc.createElement( u
"gml:coordinates"_s );
1356 baseCoordElem.setAttribute( u
"cs"_s, cs );
1357 baseCoordElem.setAttribute( u
"ts"_s, ts );
1371 QDomElement pointElem = doc.createElement( u
"gml:Point"_s );
1372 if ( gmlVersion ==
GML_3_2_1 && !gmlIdBase.isEmpty() )
1373 pointElem.setAttribute( u
"gml:id"_s, gmlIdBase );
1374 if ( !srsName.isEmpty() )
1375 pointElem.setAttribute( u
"srsName"_s, srsName );
1376 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1380 if ( invertAxisOrientation )
1394 const QDomText coordText = doc.createTextNode( coordString );
1396 coordElem.appendChild( coordText );
1398 coordElem.setAttribute( u
"srsDimension"_s, hasZValue ? u
"3"_s : u
"2"_s );
1399 pointElem.appendChild( coordElem );
1409 QDomElement multiPointElem = doc.createElement( u
"gml:MultiPoint"_s );
1410 if ( gmlVersion ==
GML_3_2_1 && !gmlIdBase.isEmpty() )
1411 multiPointElem.setAttribute( u
"gml:id"_s, gmlIdBase );
1412 if ( !srsName.isEmpty() )
1413 multiPointElem.setAttribute( u
"srsName"_s, srsName );
1418 for (
int idx = 0; idx < nPoints; ++idx )
1420 QDomElement pointMemberElem = doc.createElement( u
"gml:pointMember"_s );
1421 QDomElement pointElem = doc.createElement( u
"gml:Point"_s );
1422 if ( gmlVersion ==
GML_3_2_1 && !gmlIdBase.isEmpty() )
1423 pointElem.setAttribute( u
"gml:id"_s, gmlIdBase + u
".%1"_s.arg( idx + 1 ) );
1424 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1430 if ( invertAxisOrientation )
1444 const QDomText coordText = doc.createTextNode( coordString );
1446 coordElem.appendChild( coordText );
1448 coordElem.setAttribute( u
"srsDimension"_s, hasZValue ? u
"3"_s : u
"2"_s );
1449 pointElem.appendChild( coordElem );
1452 pointMemberElem.appendChild( pointElem );
1453 multiPointElem.appendChild( pointMemberElem );
1455 return multiPointElem;
1464 QDomElement lineStringElem = doc.createElement( u
"gml:LineString"_s );
1465 if ( gmlVersion ==
GML_3_2_1 && !gmlIdBase.isEmpty() )
1466 lineStringElem.setAttribute( u
"gml:id"_s, gmlIdBase );
1467 if ( !srsName.isEmpty() )
1468 lineStringElem.setAttribute( u
"srsName"_s, srsName );
1474 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1475 QString coordString;
1476 for (
int idx = 0; idx < nPoints; ++idx )
1485 if ( invertAxisOrientation )
1498 const QDomText coordText = doc.createTextNode( coordString );
1499 coordElem.appendChild( coordText );
1501 coordElem.setAttribute( u
"srsDimension"_s, hasZValue ? u
"3"_s : u
"2"_s );
1502 lineStringElem.appendChild( coordElem );
1503 return lineStringElem;
1512 QDomElement multiLineStringElem = doc.createElement( u
"gml:MultiLineString"_s );
1513 if ( gmlVersion ==
GML_3_2_1 && !gmlIdBase.isEmpty() )
1514 multiLineStringElem.setAttribute( u
"gml:id"_s, gmlIdBase );
1515 if ( !srsName.isEmpty() )
1516 multiLineStringElem.setAttribute( u
"srsName"_s, srsName );
1521 for (
int jdx = 0; jdx < nLines; jdx++ )
1523 QDomElement lineStringMemberElem = doc.createElement( u
"gml:lineStringMember"_s );
1524 QDomElement lineStringElem = doc.createElement( u
"gml:LineString"_s );
1525 if ( gmlVersion ==
GML_3_2_1 && !gmlIdBase.isEmpty() )
1526 lineStringElem.setAttribute( u
"gml:id"_s, gmlIdBase + u
".%1"_s.arg( jdx + 1 ) );
1533 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1534 QString coordString;
1535 for (
int idx = 0; idx < nPoints; idx++ )
1544 if ( invertAxisOrientation )
1558 const QDomText coordText = doc.createTextNode( coordString );
1559 coordElem.appendChild( coordText );
1561 coordElem.setAttribute( u
"srsDimension"_s, hasZValue ? u
"3"_s : u
"2"_s );
1562 lineStringElem.appendChild( coordElem );
1563 lineStringMemberElem.appendChild( lineStringElem );
1564 multiLineStringElem.appendChild( lineStringMemberElem );
1566 return multiLineStringElem;
1575 QDomElement polygonElem = doc.createElement( u
"gml:Polygon"_s );
1576 if ( gmlVersion ==
GML_3_2_1 && !gmlIdBase.isEmpty() )
1577 polygonElem.setAttribute( u
"gml:id"_s, gmlIdBase );
1578 if ( !srsName.isEmpty() )
1579 polygonElem.setAttribute( u
"srsName"_s, srsName );
1585 if ( numRings == 0 )
1586 return QDomElement();
1588 for (
int idx = 0; idx < numRings; idx++ )
1590 QString boundaryName = ( gmlVersion ==
GML_2_1_2 ) ?
"gml:outerBoundaryIs" :
"gml:exterior";
1593 boundaryName = ( gmlVersion ==
GML_2_1_2 ) ?
"gml:innerBoundaryIs" :
"gml:interior";
1595 QDomElement boundaryElem = doc.createElement( boundaryName );
1596 QDomElement ringElem = doc.createElement( u
"gml:LinearRing"_s );
1601 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1602 QString coordString;
1603 for (
int jdx = 0; jdx < nPoints; jdx++ )
1612 if ( invertAxisOrientation )
1626 const QDomText coordText = doc.createTextNode( coordString );
1627 coordElem.appendChild( coordText );
1629 coordElem.setAttribute( u
"srsDimension"_s, hasZValue ? u
"3"_s : u
"2"_s );
1630 ringElem.appendChild( coordElem );
1631 boundaryElem.appendChild( ringElem );
1632 polygonElem.appendChild( boundaryElem );
1644 QDomElement multiPolygonElem = doc.createElement( u
"gml:MultiPolygon"_s );
1645 if ( gmlVersion ==
GML_3_2_1 && !gmlIdBase.isEmpty() )
1646 multiPolygonElem.setAttribute( u
"gml:id"_s, gmlIdBase );
1647 if ( !srsName.isEmpty() )
1648 multiPolygonElem.setAttribute( u
"srsName"_s, srsName );
1651 wkbPtr >> numPolygons;
1653 for (
int kdx = 0; kdx < numPolygons; kdx++ )
1655 QDomElement polygonMemberElem = doc.createElement( u
"gml:polygonMember"_s );
1656 QDomElement polygonElem = doc.createElement( u
"gml:Polygon"_s );
1657 if ( gmlVersion ==
GML_3_2_1 && !gmlIdBase.isEmpty() )
1658 polygonElem.setAttribute( u
"gml:id"_s, gmlIdBase + u
".%1"_s.arg( kdx + 1 ) );
1665 for (
int idx = 0; idx < numRings; idx++ )
1667 QString boundaryName = ( gmlVersion ==
GML_2_1_2 ) ?
"gml:outerBoundaryIs" :
"gml:exterior";
1670 boundaryName = ( gmlVersion ==
GML_2_1_2 ) ?
"gml:innerBoundaryIs" :
"gml:interior";
1672 QDomElement boundaryElem = doc.createElement( boundaryName );
1673 QDomElement ringElem = doc.createElement( u
"gml:LinearRing"_s );
1678 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1679 QString coordString;
1680 for (
int jdx = 0; jdx < nPoints; jdx++ )
1689 if ( invertAxisOrientation )
1703 const QDomText coordText = doc.createTextNode( coordString );
1704 coordElem.appendChild( coordText );
1706 coordElem.setAttribute( u
"srsDimension"_s, hasZValue ? u
"3"_s : u
"2"_s );
1707 ringElem.appendChild( coordElem );
1708 boundaryElem.appendChild( ringElem );
1709 polygonElem.appendChild( boundaryElem );
1710 polygonMemberElem.appendChild( polygonElem );
1711 multiPolygonElem.appendChild( polygonMemberElem );
1714 return multiPolygonElem;
1717 return QDomElement();
1723 return QDomElement();
1729 return geometryToGML( geometry, doc, u
"GML2"_s, precision );
1732QDomElement QgsOgcUtils::createGMLCoordinates(
const QgsPolylineXY &points, QDomDocument &doc )
1734 QDomElement coordElem = doc.createElement( u
"gml:coordinates"_s );
1735 coordElem.setAttribute( u
"cs"_s, u
","_s );
1736 coordElem.setAttribute( u
"ts"_s, u
" "_s );
1738 QString coordString;
1739 QVector<QgsPointXY>::const_iterator pointIt = points.constBegin();
1740 for ( ; pointIt != points.constEnd(); ++pointIt )
1742 if ( pointIt != points.constBegin() )
1751 const QDomText coordText = doc.createTextNode( coordString );
1752 coordElem.appendChild( coordText );
1756QDomElement QgsOgcUtils::createGMLPositions(
const QgsPolylineXY &points, QDomDocument &doc )
1758 QDomElement posElem = doc.createElement( u
"gml:pos"_s );
1759 if ( points.size() > 1 )
1760 posElem = doc.createElement( u
"gml:posList"_s );
1761 posElem.setAttribute( u
"srsDimension"_s, u
"2"_s );
1763 QString coordString;
1764 QVector<QgsPointXY>::const_iterator pointIt = points.constBegin();
1765 for ( ; pointIt != points.constEnd(); ++pointIt )
1767 if ( pointIt != points.constBegin() )
1776 const QDomText coordText = doc.createTextNode( coordString );
1777 posElem.appendChild( coordText );
1785 if ( fillElement.isNull() || !fillElement.hasChildNodes() )
1793 QDomElement cssElem = fillElement.firstChildElement( u
"CssParameter"_s );
1794 while ( !cssElem.isNull() )
1796 cssName = cssElem.attribute( u
"name"_s, u
"not_found"_s );
1797 if ( cssName !=
"not_found"_L1 )
1799 elemText = cssElem.text();
1800 if ( cssName ==
"fill"_L1 )
1802 color.setNamedColor( elemText );
1804 else if ( cssName ==
"fill-opacity"_L1 )
1807 const double opacity = elemText.toDouble( &ok );
1810 color.setAlphaF( opacity );
1815 cssElem = cssElem.nextSiblingElement( u
"CssParameter"_s );
1829 if ( element.isNull() || !element.hasChildNodes() )
1836 if ( element.firstChild().nodeType() == QDomNode::TextNode )
1846 QDomElement childElem = element.firstChildElement();
1847 while ( !childElem.isNull() )
1859 if ( !expr->d->mRootNode )
1861 expr->d->mRootNode.reset( node );
1868 childElem = childElem.nextSiblingElement();
1872 expr->d->mExp = expr->
dump();
1880 BINARY_OPERATORS_TAG_NAMES_MAP,
1901static int binaryOperatorFromTagName(
const QString &tagName )
1903 return BINARY_OPERATORS_TAG_NAMES_MAP()->value( tagName, -1 );
1910 return u
"PropertyIsLike"_s;
1912 return BINARY_OPERATORS_TAG_NAMES_MAP()->key( op, QString() );
1915static bool isBinaryOperator(
const QString &tagName )
1917 return binaryOperatorFromTagName( tagName ) >= 0;
1921static bool isSpatialOperator(
const QString &tagName )
1923 static QStringList spatialOps;
1924 if ( spatialOps.isEmpty() )
1926 spatialOps << u
"BBOX"_s << u
"Intersects"_s << u
"Contains"_s << u
"Crosses"_s << u
"Equals"_s << u
"Disjoint"_s << u
"Overlaps"_s << u
"Touches"_s << u
"Within"_s;
1929 return spatialOps.contains( tagName );
1935 QgsExpressionNode *node = utils.nodeFromOgcFilter( element );
1936 errorMessage = utils.errorMessage();
1943 QgsExpressionNodeBinaryOperator *node = utils.nodeBinaryOperatorFromOgcFilter( element );
1944 errorMessage = utils.errorMessage();
1951 QgsExpressionNodeFunction *node = utils.nodeSpatialOperatorFromOgcFilter( element );
1952 errorMessage = utils.errorMessage();
1959 QgsExpressionNodeUnaryOperator *node = utils.nodeNotFromOgcFilter( element );
1960 errorMessage = utils.errorMessage();
1967 QgsExpressionNodeFunction *node = utils.nodeFunctionFromOgcFilter( element );
1968 errorMessage = utils.errorMessage();
1975 QgsExpressionNode *node = utils.nodeLiteralFromOgcFilter( element );
1976 errorMessage = utils.errorMessage();
1983 QgsExpressionNodeColumnRef *node = utils.nodeColumnRefFromOgcFilter( element );
1984 errorMessage = utils.errorMessage();
1988QgsExpressionNode *QgsOgcUtils::nodeIsBetweenFromOgcFilter( QDomElement &element, QString &errorMessage )
1991 QgsExpressionNode *node = utils.nodeIsBetweenFromOgcFilter( element );
1992 errorMessage = utils.errorMessage();
1999 QgsExpressionNodeBinaryOperator *node = utils.nodePropertyIsNullFromOgcFilter( element );
2000 errorMessage = utils.errorMessage();
2010 return expressionToOgcFilter( exp, doc,
GML_2_1_2,
FILTER_OGC_1_0, QString(), QString(), u
"geometry"_s, QString(),
false,
false, errorMessage );
2020 return doc.createElementNS(
SE_NAMESPACE, u
"se:ElseFilter"_s );
2029 const QString &namespacePrefix,
2030 const QString &namespaceURI,
2031 const QString &geometryName,
2032 const QString &srsName,
2033 bool honourAxisOrientation,
2034 bool invertAxisOrientation,
2035 QString *errorMessage,
2036 const QMap<QString, QString> &fieldNameToXPathMap,
2037 const QMap<QString, QString> &namespacePrefixToUriMap
2041 return QDomElement();
2048 utils( doc, gmlVersion, filterVersion, namespacePrefix, namespaceURI, geometryName, srsName, honourAxisOrientation, invertAxisOrientation, fieldNameToXPathMap, namespacePrefixToUriMap );
2052 if ( exprRootElem.isNull() )
2053 return QDomElement();
2055 QDomElement filterElem = filterElement( doc, gmlVersion, filterVersion, utils.
GMLNamespaceUsed() );
2057 if ( !namespacePrefix.isEmpty() && !namespaceURI.isEmpty() )
2059 QDomAttr attr = doc.createAttribute( u
"xmlns:"_s + namespacePrefix );
2060 attr.setValue( namespaceURI );
2061 filterElem.setAttributeNode( attr );
2064 filterElem.appendChild( exprRootElem );
2073 const QString &geometryName,
2074 const QString &srsName,
2075 bool honourAxisOrientation,
2076 bool invertAxisOrientation,
2077 QString *errorMessage,
2078 bool requiresFilterElement,
2079 const QMap<QString, QString> &fieldNameToXPathMap,
2080 const QMap<QString, QString> &namespacePrefixToUriMap
2090 return QDomElement();
2092 QgsOgcUtilsExprToFilter utils( doc, gmlVersion, filterVersion, QString(), QString(), geometryName, srsName, honourAxisOrientation, invertAxisOrientation, fieldNameToXPathMap, namespacePrefixToUriMap );
2100 if ( !exprRootElem.isNull() )
2102 if ( requiresFilterElement )
2104 QDomElement filterElem = filterElement( doc, gmlVersion, filterVersion, utils.
GMLNamespaceUsed() );
2106 filterElem.appendChild( exprRootElem );
2109 return exprRootElem;
2112 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
2130 return QDomElement();
2133 utils( doc, gmlVersion, filterVersion, layerProperties, honourAxisOrientation, invertAxisOrientation, 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() && !setNamespaceURI.contains( props.mNamespaceURI ) )
2147 setNamespaceURI.insert( props.mNamespaceURI );
2148 QDomAttr attr = doc.createAttribute( u
"xmlns:"_s + props.mNamespacePrefix );
2149 attr.setValue( props.mNamespaceURI );
2150 filterElem.setAttributeNode( attr );
2153 filterElem.appendChild( exprRootElem );
2161 if ( gmlGeomType ==
"Point"_L1 )
2163 if ( gmlGeomType ==
"LineString"_L1 || gmlGeomType ==
"Curve"_L1 )
2165 if ( gmlGeomType ==
"Polygon"_L1 || gmlGeomType ==
"Surface"_L1 )
2167 if ( gmlGeomType ==
"MultiPoint"_L1 )
2169 if ( gmlGeomType ==
"MultiLineString"_L1 || gmlGeomType ==
"MultiCurve"_L1 )
2171 if ( gmlGeomType ==
"MultiPolygon"_L1 || gmlGeomType ==
"MultiSurface"_L1 )
2197 mErrorMessage = QObject::tr(
"Node type not supported: %1" ).arg( node->
nodeType() );
2198 return QDomElement();
2205 if ( !mErrorMessage.isEmpty() )
2206 return QDomElement();
2209 switch ( node->
op() )
2212 uoElem = mDoc.createElement( mFilterPrefix +
":Literal" );
2217 uoElem.appendChild( mDoc.createTextNode(
"-" + operandElem.text() ) );
2218 mDoc.removeChild( operandElem );
2222 mErrorMessage = QObject::tr(
"This use of unary operator not implemented yet" );
2223 return QDomElement();
2227 uoElem = mDoc.createElement( mFilterPrefix +
":Not" );
2228 uoElem.appendChild( operandElem );
2232 mErrorMessage = QObject::tr(
"Unary operator '%1' not implemented yet" ).arg( node->
text() );
2233 return QDomElement();
2243 if ( !mErrorMessage.isEmpty() )
2244 return QDomElement();
2253 const QgsExpressionNodeLiteral *rightLit =
static_cast<const QgsExpressionNodeLiteral *
>( node->
opRight() );
2256 QDomElement elem = mDoc.createElement( mFilterPrefix +
":PropertyIsNull" );
2257 elem.appendChild( leftElem );
2261 QDomElement notElem = mDoc.createElement( mFilterPrefix +
":Not" );
2262 notElem.appendChild( elem );
2275 if ( !mErrorMessage.isEmpty() )
2276 return QDomElement();
2279 const QString opText = binaryOperatorToTagName( op );
2280 if ( opText.isEmpty() )
2284 mErrorMessage = QObject::tr(
"Binary operator %1 not implemented yet" ).arg( node->
text() );
2285 return QDomElement();
2288 QDomElement boElem = mDoc.createElement( mFilterPrefix +
":" + opText );
2293 boElem.setAttribute( u
"matchCase"_s, u
"false"_s );
2296 boElem.setAttribute( u
"wildCard"_s, u
"%"_s );
2297 boElem.setAttribute( u
"singleChar"_s, u
"_"_s );
2299 boElem.setAttribute( u
"escape"_s, u
"\\"_s );
2301 boElem.setAttribute( u
"escapeChar"_s, u
"\\"_s );
2304 boElem.appendChild( leftElem );
2305 boElem.appendChild( rightElem );
2312 Q_UNUSED( expression )
2315 switch ( node->
value().userType() )
2317 case QMetaType::Type::Int:
2318 value = QString::number( node->
value().toInt() );
2320 case QMetaType::Type::Double:
2323 case QMetaType::Type::QString:
2324 value = node->
value().toString();
2326 case QMetaType::Type::QDate:
2327 value = node->
value().toDate().toString( Qt::ISODate );
2329 case QMetaType::Type::QDateTime:
2330 value = node->
value().toDateTime().toString( Qt::ISODate );
2334 mErrorMessage = QObject::tr(
"Literal type not supported: %1" ).arg(
static_cast<QMetaType::Type
>( node->
value().userType() ) );
2335 return QDomElement();
2338 QDomElement litElem = mDoc.createElement( mFilterPrefix +
":Literal" );
2339 litElem.appendChild( mDoc.createTextNode( value ) );
2346 Q_UNUSED( expression )
2348 QDomElement propElem = mDoc.createElement( mFilterPrefix +
":" + mPropertyName );
2349 if ( !mFieldNameToXPathMap.isEmpty() )
2351 const auto iterFieldName = mFieldNameToXPathMap.constFind( node->
name() );
2352 if ( iterFieldName != mFieldNameToXPathMap.constEnd() )
2354 const QString xpath( *iterFieldName );
2356 if ( !mNamespacePrefixToUriMap.isEmpty() )
2358 const QStringList parts = xpath.split(
'/' );
2359 QSet<QString> setNamespacePrefix;
2360 for (
const QString &part : std::as_const( parts ) )
2362 const QStringList subparts = part.split(
':' );
2363 if ( subparts.size() == 2 && !setNamespacePrefix.contains( subparts[0] ) )
2365 const auto iterNamespacePrefix = mNamespacePrefixToUriMap.constFind( subparts[0] );
2366 if ( iterNamespacePrefix != mNamespacePrefixToUriMap.constEnd() )
2368 setNamespacePrefix.insert( subparts[0] );
2369 QDomAttr attr = mDoc.createAttribute( u
"xmlns:"_s + subparts[0] );
2370 attr.setValue( *iterNamespacePrefix );
2371 propElem.setAttributeNode( attr );
2377 propElem.appendChild( mDoc.createTextNode( xpath ) );
2382 QString columnRef( node->
name() );
2383 if ( !mNamespacePrefix.isEmpty() && !mNamespaceURI.isEmpty() )
2384 columnRef = mNamespacePrefix + u
":"_s + columnRef;
2385 propElem.appendChild( mDoc.createTextNode( columnRef ) );
2392 if ( node->
list()->
list().size() == 1 )
2396 QDomElement eqElem = mDoc.createElement( mFilterPrefix +
":PropertyIsEqualTo" );
2397 eqElem.appendChild( leftNode );
2398 eqElem.appendChild( firstListNode );
2401 QDomElement notElem = mDoc.createElement( mFilterPrefix +
":Not" );
2402 notElem.appendChild( eqElem );
2408 QDomElement orElem = mDoc.createElement( mFilterPrefix +
":Or" );
2411 const auto constList = node->
list()->
list();
2412 for ( QgsExpressionNode *n : constList )
2415 if ( !mErrorMessage.isEmpty() )
2416 return QDomElement();
2418 QDomElement eqElem = mDoc.createElement( mFilterPrefix +
":PropertyIsEqualTo" );
2419 eqElem.appendChild( leftNode.cloneNode() );
2420 eqElem.appendChild( listNode );
2422 orElem.appendChild( eqElem );
2427 QDomElement notElem = mDoc.createElement( mFilterPrefix +
":Not" );
2428 notElem.appendChild( orElem );
2437 BINARY_SPATIAL_OPS_MAP,
2439 { {
"disjoint"_L1,
"Disjoint"_L1 },
2440 {
"intersects"_L1,
"Intersects"_L1 },
2441 {
"touches"_L1,
"Touches"_L1 },
2442 {
"crosses"_L1,
"Crosses"_L1 },
2443 {
"contains"_L1,
"Contains"_L1 },
2444 {
"overlaps"_L1,
"Overlaps"_L1 },
2445 {
"within"_L1,
"Within"_L1 } }
2449static bool isBinarySpatialOperator(
const QString &fnName )
2451 return BINARY_SPATIAL_OPS_MAP()->contains( fnName );
2454static QString tagNameForSpatialOperator(
const QString &fnName )
2456 return BINARY_SPATIAL_OPS_MAP()->value( fnName );
2478 if ( fnDef->
name() ==
"geom_from_wkt"_L1 )
2480 const QList<QgsExpressionNode *> &args = fnNode->
args()->
list();
2496 if ( fd->
name() ==
"intersects_bbox"_L1 )
2498 QList<QgsExpressionNode *> argNodes = node->
args()->
list();
2499 Q_ASSERT( argNodes.count() == 2 );
2501 const QgsGeometry geom = geometryFromConstExpr( argNodes[1] );
2502 if ( !geom.
isNull() && isGeometryColumn( argNodes[0] ) )
2511 QDomElement funcElem = mDoc.createElement( mFilterPrefix +
":BBOX" );
2513 if ( !mGeometryName.isEmpty() )
2516 QDomElement geomProperty = mDoc.createElement( mFilterPrefix +
":" + mPropertyName );
2517 QString columnRef( mGeometryName );
2518 if ( !mNamespacePrefix.isEmpty() && !mNamespaceURI.isEmpty() )
2519 columnRef = mNamespacePrefix + u
":"_s + columnRef;
2520 geomProperty.appendChild( mDoc.createTextNode( columnRef ) );
2522 funcElem.appendChild( geomProperty );
2524 funcElem.appendChild( elemBox );
2529 mErrorMessage = QObject::tr(
"<BBOX> is currently supported only in form: bbox(@geometry, geomFromWKT('…'))" );
2530 return QDomElement();
2534 if ( isBinarySpatialOperator( fd->
name() ) )
2536 QList<QgsExpressionNode *> argNodes = node->
args()->
list();
2537 Q_ASSERT( argNodes.count() == 2 );
2539 QgsExpressionNode *otherNode =
nullptr;
2540 if ( isGeometryColumn( argNodes[0] ) )
2541 otherNode = argNodes[1];
2542 else if ( isGeometryColumn( argNodes[1] ) )
2543 otherNode = argNodes[0];
2546 mErrorMessage = QObject::tr(
"Unable to translate spatial operator: at least one must refer to geometry." );
2547 return QDomElement();
2550 QDomElement otherGeomElem;
2555 mErrorMessage = QObject::tr(
"spatial operator: the other operator must be a geometry constructor function" );
2556 return QDomElement();
2559 const QgsExpressionNodeFunction *otherFn =
static_cast<const QgsExpressionNodeFunction *
>( otherNode );
2561 if ( otherFnDef->
name() ==
"geom_from_wkt"_L1 )
2563 QgsExpressionNode *firstFnArg = otherFn->
args()->
list()[0];
2566 mErrorMessage = QObject::tr(
"geom_from_wkt: argument must be string literal" );
2567 return QDomElement();
2569 const QString wkt =
static_cast<const QgsExpressionNodeLiteral *
>( firstFnArg )->value().toString();
2571 otherGeomElem =
QgsOgcUtils::geometryToGML( geom, mDoc, mGMLVersion, mSrsName, mInvertAxisOrientation, u
"qgis_id_geom_%1"_s.arg( mGeomId ) );
2572 if ( otherGeomElem.isNull() )
2574 mErrorMessage = QObject::tr(
"geom_from_wkt: unable to generate GML from wkt geometry" );
2575 return QDomElement();
2579 else if ( otherFnDef->
name() ==
"geom_from_gml"_L1 )
2581 QgsExpressionNode *firstFnArg = otherFn->
args()->
list()[0];
2584 mErrorMessage = QObject::tr(
"geom_from_gml: argument must be string literal" );
2585 return QDomElement();
2588 QDomDocument geomDoc;
2589 const QString gml =
static_cast<const QgsExpressionNodeLiteral *
>( firstFnArg )->value().toString();
2591 const QString xml = u
"<tmp xmlns:gml=\"%1\">%2</tmp>"_s.arg(
GML_NAMESPACE, gml );
2592 if ( !geomDoc.setContent( xml,
true ) )
2594 mErrorMessage = QObject::tr(
"geom_from_gml: unable to parse XML" );
2595 return QDomElement();
2598 const QDomNode geomNode = mDoc.importNode( geomDoc.documentElement().firstChildElement(),
true );
2599 otherGeomElem = geomNode.toElement();
2604 otherGeomElem =
QgsOgcUtils::geometryToGML( geom, mDoc, mGMLVersion, mSrsName, mInvertAxisOrientation, u
"qgis_id_geom_%1"_s.arg( mGeomId ) );
2605 if ( otherGeomElem.
isNull() )
2607 mErrorMessage = QObject::tr(
"geom from static value: unable to generate GML from static variable" );
2608 return QDomElement();
2614 mErrorMessage = QObject::tr(
"spatial operator: unknown geometry constructor function" );
2615 return QDomElement();
2620 QDomElement funcElem = mDoc.createElement( mFilterPrefix +
":" + tagNameForSpatialOperator( fd->
name() ) );
2621 QDomElement geomProperty = mDoc.createElement( mFilterPrefix +
":" + mPropertyName );
2622 QString columnRef( mGeometryName );
2623 if ( !mNamespacePrefix.isEmpty() && !mNamespaceURI.isEmpty() )
2624 columnRef = mNamespacePrefix + u
":"_s + columnRef;
2625 geomProperty.appendChild( mDoc.createTextNode( columnRef ) );
2626 funcElem.appendChild( geomProperty );
2627 funcElem.appendChild( otherGeomElem );
2631 if ( fd->
isStatic( node, expression, context ) )
2633 const QVariant result = fd->
run( node->
args(), context, expression, node );
2634 const QgsExpressionNodeLiteral literal( result );
2635 return expressionLiteralToOgcFilter( &literal, expression, context );
2640 mErrorMessage = QObject::tr(
"Special columns/constants are not supported." );
2641 return QDomElement();
2645 QDomElement funcElem = mDoc.createElement( mFilterPrefix +
":Function" );
2646 funcElem.setAttribute( u
"name"_s, fd->
name() );
2647 const auto constList = node->
args()->
list();
2648 for ( QgsExpressionNode *n : constList )
2651 if ( !mErrorMessage.isEmpty() )
2652 return QDomElement();
2654 funcElem.appendChild( childElem );
2666 const QList<QgsOgcUtils::LayerProperties> &layerProperties,
2667 bool honourAxisOrientation,
2668 bool invertAxisOrientation,
2669 const QMap< QString, QString> &mapUnprefixedTypenameToPrefixedTypename,
2670 const QMap<QString, QString> &fieldNameToXPathMap,
2671 const QMap<QString, QString> &namespacePrefixToUriMap
2674 , mGMLVersion( gmlVersion )
2675 , mFilterVersion( filterVersion )
2676 , mLayerProperties( layerProperties )
2677 , mHonourAxisOrientation( honourAxisOrientation )
2678 , mInvertAxisOrientation( invertAxisOrientation )
2679 , mFilterPrefix( ( filterVersion ==
QgsOgcUtils::FILTER_FES_2_0 ) ?
"fes" :
"ogc" )
2680 , mPropertyName( ( filterVersion ==
QgsOgcUtils::FILTER_FES_2_0 ) ?
"ValueReference" :
"PropertyName" )
2681 , mMapUnprefixedTypenameToPrefixedTypename( mapUnprefixedTypenameToPrefixedTypename )
2682 , mFieldNameToXPathMap( fieldNameToXPathMap )
2683 , mNamespacePrefixToUriMap( namespacePrefixToUriMap )
2708 mErrorMessage = QObject::tr(
"Node type not supported: %1" ).arg( node->
nodeType() );
2709 return QDomElement();
2717 if ( !mErrorMessage.isEmpty() )
2718 return QDomElement();
2721 switch ( node->
op() )
2724 uoElem = mDoc.createElement( mFilterPrefix +
":Literal" );
2729 uoElem.appendChild( mDoc.createTextNode(
"-" + operandElem.text() ) );
2730 mDoc.removeChild( operandElem );
2734 mErrorMessage = QObject::tr(
"This use of unary operator not implemented yet" );
2735 return QDomElement();
2739 uoElem = mDoc.createElement( mFilterPrefix +
":Not" );
2740 uoElem.appendChild( operandElem );
2745 return QDomElement();
2755 if ( !mErrorMessage.isEmpty() )
2756 return QDomElement();
2765 const QgsSQLStatement::NodeLiteral *rightLit =
static_cast<const QgsSQLStatement::NodeLiteral *
>( node->
opRight() );
2768 QDomElement elem = mDoc.createElement( mFilterPrefix +
":PropertyIsNull" );
2769 elem.appendChild( leftElem );
2773 QDomElement notElem = mDoc.createElement( mFilterPrefix +
":Not" );
2774 notElem.appendChild( elem );
2787 if ( !mErrorMessage.isEmpty() )
2788 return QDomElement();
2797 opText = u
"PropertyIsEqualTo"_s;
2799 opText = u
"PropertyIsNotEqualTo"_s;
2801 opText = u
"PropertyIsLessThanOrEqualTo"_s;
2803 opText = u
"PropertyIsGreaterThanOrEqualTo"_s;
2805 opText = u
"PropertyIsLessThan"_s;
2807 opText = u
"PropertyIsGreaterThan"_s;
2809 opText = u
"PropertyIsLike"_s;
2811 opText = u
"PropertyIsLike"_s;
2813 if ( opText.isEmpty() )
2817 return QDomElement();
2820 QDomElement boElem = mDoc.createElement( mFilterPrefix +
":" + opText );
2825 boElem.setAttribute( u
"matchCase"_s, u
"false"_s );
2828 boElem.setAttribute( u
"wildCard"_s, u
"%"_s );
2829 boElem.setAttribute( u
"singleChar"_s, u
"_"_s );
2831 boElem.setAttribute( u
"escape"_s, u
"\\"_s );
2833 boElem.setAttribute( u
"escapeChar"_s, u
"\\"_s );
2836 boElem.appendChild( leftElem );
2837 boElem.appendChild( rightElem );
2845 switch ( node->
value().userType() )
2847 case QMetaType::Type::Int:
2848 value = QString::number( node->
value().toInt() );
2850 case QMetaType::Type::LongLong:
2851 value = QString::number( node->
value().toLongLong() );
2853 case QMetaType::Type::Double:
2856 case QMetaType::Type::QString:
2857 value = node->
value().toString();
2861 mErrorMessage = QObject::tr(
"Literal type not supported: %1" ).arg(
static_cast<QMetaType::Type
>( node->
value().userType() ) );
2862 return QDomElement();
2865 QDomElement litElem = mDoc.createElement( mFilterPrefix +
":Literal" );
2866 litElem.appendChild( mDoc.createTextNode( value ) );
2873 QDomElement propElem = mDoc.createElement( mFilterPrefix +
":" + mPropertyName );
2874 if ( node->
tableName().isEmpty() || mLayerProperties.size() == 1 )
2876 if ( !mFieldNameToXPathMap.isEmpty() )
2878 const auto iterFieldName = mFieldNameToXPathMap.constFind( node->
name() );
2879 if ( iterFieldName != mFieldNameToXPathMap.constEnd() )
2881 const QString xpath( *iterFieldName );
2883 if ( !mNamespacePrefixToUriMap.isEmpty() )
2885 const QStringList parts = xpath.split(
'/' );
2886 QSet<QString> setNamespacePrefix;
2887 for (
const QString &part : std::as_const( parts ) )
2889 const QStringList subparts = part.split(
':' );
2890 if ( subparts.size() == 2 && !setNamespacePrefix.contains( subparts[0] ) )
2892 const auto iterNamespacePrefix = mNamespacePrefixToUriMap.constFind( subparts[0] );
2893 if ( iterNamespacePrefix != mNamespacePrefixToUriMap.constEnd() )
2895 setNamespacePrefix.insert( subparts[0] );
2896 QDomAttr attr = mDoc.createAttribute( u
"xmlns:"_s + subparts[0] );
2897 attr.setValue( *iterNamespacePrefix );
2898 propElem.setAttributeNode( attr );
2904 propElem.appendChild( mDoc.createTextNode( xpath ) );
2909 if ( mLayerProperties.size() == 1 && !mLayerProperties[0].mNamespacePrefix.isEmpty() && !mLayerProperties[0].mNamespaceURI.isEmpty() )
2910 propElem.appendChild( mDoc.createTextNode( mLayerProperties[0].mNamespacePrefix + u
":"_s + node->
name() ) );
2912 propElem.appendChild( mDoc.createTextNode( node->
name() ) );
2916 QString tableName( mMapTableAliasToNames[node->
tableName()] );
2917 if ( mMapUnprefixedTypenameToPrefixedTypename.contains( tableName ) )
2918 tableName = mMapUnprefixedTypenameToPrefixedTypename[tableName];
2919 propElem.appendChild( mDoc.createTextNode( tableName +
"/" + node->
name() ) );
2926 if ( node->
list()->
list().size() == 1 )
2930 QDomElement eqElem = mDoc.createElement( mFilterPrefix +
":PropertyIsEqualTo" );
2931 eqElem.appendChild( leftNode );
2932 eqElem.appendChild( firstListNode );
2935 QDomElement notElem = mDoc.createElement( mFilterPrefix +
":Not" );
2936 notElem.appendChild( eqElem );
2942 QDomElement orElem = mDoc.createElement( mFilterPrefix +
":Or" );
2945 const auto constList = node->
list()->
list();
2946 for ( QgsSQLStatement::Node *n : constList )
2949 if ( !mErrorMessage.isEmpty() )
2950 return QDomElement();
2952 QDomElement eqElem = mDoc.createElement( mFilterPrefix +
":PropertyIsEqualTo" );
2953 eqElem.appendChild( leftNode.cloneNode() );
2954 eqElem.appendChild( listNode );
2956 orElem.appendChild( eqElem );
2961 QDomElement notElem = mDoc.createElement( mFilterPrefix +
":Not" );
2962 notElem.appendChild( orElem );
2971 QDomElement elem = mDoc.createElement( mFilterPrefix +
":PropertyIsBetween" );
2973 QDomElement lowerBoundary = mDoc.createElement( mFilterPrefix +
":LowerBoundary" );
2975 elem.appendChild( lowerBoundary );
2976 QDomElement upperBoundary = mDoc.createElement( mFilterPrefix +
":UpperBoundary" );
2978 elem.appendChild( upperBoundary );
2982 QDomElement notElem = mDoc.createElement( mFilterPrefix +
":Not" );
2983 notElem.appendChild( elem );
2990static QString mapBinarySpatialToOgc(
const QString &name )
2992 QString nameCompare( name );
2993 if ( name.size() > 3 && QStringView { name }.mid( 0, 3 ).toString().compare(
"ST_"_L1, Qt::CaseInsensitive ) == 0 )
2994 nameCompare = name.mid( 3 );
2995 QStringList spatialOps;
2996 spatialOps << u
"BBOX"_s << u
"Intersects"_s << u
"Contains"_s << u
"Crosses"_s << u
"Equals"_s << u
"Disjoint"_s << u
"Overlaps"_s << u
"Touches"_s << u
"Within"_s;
2997 const auto constSpatialOps = spatialOps;
2998 for ( QString op : constSpatialOps )
3000 if ( nameCompare.compare( op, Qt::CaseInsensitive ) == 0 )
3006static QString mapTernarySpatialToOgc(
const QString &name )
3008 QString nameCompare( name );
3009 if ( name.size() > 3 && QStringView { name }.mid( 0, 3 ).compare(
"ST_"_L1, Qt::CaseInsensitive ) == 0 )
3010 nameCompare = name.mid( 3 );
3011 if ( nameCompare.compare(
"DWithin"_L1, Qt::CaseInsensitive ) == 0 )
3012 return u
"DWithin"_s;
3013 if ( nameCompare.compare(
"Beyond"_L1, Qt::CaseInsensitive ) == 0 )
3018QString QgsOgcUtilsSQLStatementToFilter::getGeometryColumnSRSName(
const QgsSQLStatement::Node *node )
3023 const QgsSQLStatement::NodeColumnRef *col =
static_cast<const QgsSQLStatement::NodeColumnRef *
>( node );
3026 const auto constMLayerProperties = mLayerProperties;
3027 for (
const QgsOgcUtils::LayerProperties &prop : constMLayerProperties )
3029 if ( prop.mName.compare( mMapTableAliasToNames[col->
tableName()], Qt::CaseInsensitive ) == 0 && prop.mGeometryAttribute.compare( col->
name(), Qt::CaseInsensitive ) == 0 )
3031 return prop.mSRSName;
3035 if ( !mLayerProperties.empty() && mLayerProperties.at( 0 ).mGeometryAttribute.compare( col->
name(), Qt::CaseInsensitive ) == 0 )
3037 return mLayerProperties.at( 0 ).mSRSName;
3042bool QgsOgcUtilsSQLStatementToFilter::processSRSName(
const QgsSQLStatement::NodeFunction *mainNode, QList<QgsSQLStatement::Node *> args,
bool lastArgIsSRSName, QString &srsName,
bool &axisInversion )
3044 srsName = mCurrentSRSName;
3045 axisInversion = mInvertAxisOrientation;
3047 if ( lastArgIsSRSName )
3049 QgsSQLStatement::Node *lastArg = args[args.size() - 1];
3052 mErrorMessage = QObject::tr(
"%1: Last argument must be string or integer literal" ).arg( mainNode->
name() );
3055 const QgsSQLStatement::NodeLiteral *lit =
static_cast<const QgsSQLStatement::NodeLiteral *
>( lastArg );
3056 if ( lit->
value().userType() == QMetaType::Type::Int )
3060 srsName =
"EPSG:" + QString::number( lit->
value().toInt() );
3064 srsName =
"urn:ogc:def:crs:EPSG::" + QString::number( lit->
value().toInt() );
3069 srsName = lit->
value().toString();
3070 if ( srsName.startsWith(
"EPSG:"_L1, Qt::CaseInsensitive ) )
3075 QgsCoordinateReferenceSystem crs;
3076 if ( !srsName.isEmpty() )
3082 axisInversion = !axisInversion;
3092 if ( node->
name().compare(
"ST_GeometryFromText"_L1, Qt::CaseInsensitive ) == 0 )
3094 QList<QgsSQLStatement::Node *> args = node->
args()->
list();
3095 if ( args.size() != 1 && args.size() != 2 )
3097 mErrorMessage = QObject::tr(
"Function %1 should have 1 or 2 arguments" ).arg( node->
name() );
3098 return QDomElement();
3101 QgsSQLStatement::Node *firstFnArg = args[0];
3104 mErrorMessage = QObject::tr(
"%1: First argument must be string literal" ).arg( node->
name() );
3105 return QDomElement();
3110 if ( !processSRSName( node, args, args.size() == 2, srsName, axisInversion ) )
3112 return QDomElement();
3115 const QString wkt =
static_cast<const QgsSQLStatement::NodeLiteral *
>( firstFnArg )->value().toString();
3117 const QDomElement geomElem =
QgsOgcUtils::geometryToGML( geom, mDoc, mGMLVersion, srsName, axisInversion, u
"qgis_id_geom_%1"_s.arg( mGeomId ) );
3119 if ( geomElem.isNull() )
3121 mErrorMessage = QObject::tr(
"%1: invalid WKT" ).arg( node->
name() );
3122 return QDomElement();
3129 if ( node->
name().compare(
"ST_MakeEnvelope"_L1, Qt::CaseInsensitive ) == 0 )
3131 QList<QgsSQLStatement::Node *> args = node->
args()->
list();
3132 if ( args.size() != 4 && args.size() != 5 )
3134 mErrorMessage = QObject::tr(
"Function %1 should have 4 or 5 arguments" ).arg( node->
name() );
3135 return QDomElement();
3140 for (
int i = 0; i < 4; i++ )
3142 QgsSQLStatement::Node *arg = args[i];
3145 mErrorMessage = QObject::tr(
"%1: Argument %2 must be numeric literal" ).arg( node->
name() ).arg( i + 1 );
3146 return QDomElement();
3148 const QgsSQLStatement::NodeLiteral *lit =
static_cast<const QgsSQLStatement::NodeLiteral *
>( arg );
3150 if ( lit->
value().userType() == QMetaType::Type::Int )
3151 val = lit->
value().toInt();
3152 else if ( lit->
value().userType() == QMetaType::Type::LongLong )
3153 val = lit->
value().toLongLong();
3154 else if ( lit->
value().userType() == QMetaType::Type::Double )
3155 val = lit->
value().toDouble();
3158 mErrorMessage = QObject::tr(
"%1 Argument %2 must be numeric literal" ).arg( node->
name() ).arg( i + 1 );
3159 return QDomElement();
3173 if ( !processSRSName( node, args, args.size() == 5, srsName, axisInversion ) )
3175 return QDomElement();
3181 : QgsOgcUtils::rectangleToGMLEnvelope( &rect, mDoc, srsName, axisInversion, 15 );
3185 if ( node->
name().compare(
"ST_GeomFromGML"_L1, Qt::CaseInsensitive ) == 0 )
3187 QList<QgsSQLStatement::Node *> args = node->
args()->
list();
3188 if ( args.size() != 1 )
3190 mErrorMessage = QObject::tr(
"Function %1 should have 1 argument" ).arg( node->
name() );
3191 return QDomElement();
3194 QgsSQLStatement::Node *firstFnArg = args[0];
3197 mErrorMessage = QObject::tr(
"%1: Argument must be string literal" ).arg( node->
name() );
3198 return QDomElement();
3201 QDomDocument geomDoc;
3202 const QString gml =
static_cast<const QgsSQLStatement::NodeLiteral *
>( firstFnArg )->value().toString();
3204 const QString xml = u
"<tmp xmlns:gml=\"%1\">%2</tmp>"_s.arg(
GML_NAMESPACE, gml );
3205 if ( !geomDoc.setContent( xml,
true ) )
3207 mErrorMessage = QObject::tr(
"ST_GeomFromGML: unable to parse XML" );
3208 return QDomElement();
3211 const QDomNode geomNode = mDoc.importNode( geomDoc.documentElement().firstChildElement(),
true );
3213 return geomNode.toElement();
3217 QString ogcName( mapBinarySpatialToOgc( node->
name() ) );
3218 if ( !ogcName.isEmpty() )
3220 QList<QgsSQLStatement::Node *> args = node->
args()->
list();
3221 if ( args.size() != 2 )
3223 mErrorMessage = QObject::tr(
"Function %1 should have 2 arguments" ).arg( node->
name() );
3224 return QDomElement();
3227 for (
int i = 0; i < 2; i++ )
3230 (
static_cast<const QgsSQLStatement::NodeFunction *
>( args[i] )->name().compare(
"ST_GeometryFromText"_L1, Qt::CaseInsensitive ) == 0 ||
3231 static_cast<const QgsSQLStatement::NodeFunction *
>( args[i] )->name().compare(
"ST_MakeEnvelope"_L1, Qt::CaseInsensitive ) == 0 ) )
3233 mCurrentSRSName = getGeometryColumnSRSName( args[1 - i] );
3240 QDomElement funcElem = mDoc.createElement( mFilterPrefix +
":" + ogcName );
3241 const auto constArgs = args;
3242 for ( QgsSQLStatement::Node *n : constArgs )
3245 if ( !mErrorMessage.isEmpty() )
3247 mCurrentSRSName.clear();
3248 return QDomElement();
3251 funcElem.appendChild( childElem );
3254 mCurrentSRSName.clear();
3258 ogcName = mapTernarySpatialToOgc( node->
name() );
3259 if ( !ogcName.isEmpty() )
3261 QList<QgsSQLStatement::Node *> args = node->
args()->
list();
3262 if ( args.size() != 3 )
3264 mErrorMessage = QObject::tr(
"Function %1 should have 3 arguments" ).arg( node->
name() );
3265 return QDomElement();
3268 for (
int i = 0; i < 2; i++ )
3271 (
static_cast<const QgsSQLStatement::NodeFunction *
>( args[i] )->name().compare(
"ST_GeometryFromText"_L1, Qt::CaseInsensitive ) == 0 ||
3272 static_cast<const QgsSQLStatement::NodeFunction *
>( args[i] )->name().compare(
"ST_MakeEnvelope"_L1, Qt::CaseInsensitive ) == 0 ) )
3274 mCurrentSRSName = getGeometryColumnSRSName( args[1 - i] );
3279 QDomElement funcElem = mDoc.createElement( mFilterPrefix +
":" + node->
name().mid( 3 ) );
3280 for (
int i = 0; i < 2; i++ )
3282 const QDomElement childElem =
toOgcFilter( args[i] );
3283 if ( !mErrorMessage.isEmpty() )
3285 mCurrentSRSName.clear();
3286 return QDomElement();
3289 funcElem.appendChild( childElem );
3291 mCurrentSRSName.clear();
3293 QgsSQLStatement::Node *distanceNode = args[2];
3296 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() );
3297 return QDomElement();
3299 const QgsSQLStatement::NodeLiteral *lit =
static_cast<const QgsSQLStatement::NodeLiteral *
>( distanceNode );
3302 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() );
3303 return QDomElement();
3306 QString unit( u
"m"_s );
3307 switch ( lit->
value().userType() )
3309 case QMetaType::Type::Int:
3310 distance = QString::number( lit->
value().toInt() );
3312 case QMetaType::Type::LongLong:
3313 distance = QString::number( lit->
value().toLongLong() );
3315 case QMetaType::Type::Double:
3318 case QMetaType::Type::QString:
3320 distance = lit->
value().toString();
3321 for (
int i = 0; i < distance.size(); i++ )
3323 if ( !( ( distance[i] >=
'0' && distance[i] <=
'9' ) || distance[i] ==
'-' || distance[i] ==
'.' || distance[i] ==
'e' || distance[i] ==
'E' ) )
3325 unit = distance.mid( i ).trimmed();
3326 distance = distance.mid( 0, i );
3334 mErrorMessage = QObject::tr(
"Literal type not supported: %1" ).arg(
static_cast<QMetaType::Type
>( lit->
value().userType() ) );
3335 return QDomElement();
3338 QDomElement distanceElem = mDoc.createElement( mFilterPrefix +
":Distance" );
3340 distanceElem.setAttribute( u
"uom"_s, unit );
3342 distanceElem.setAttribute( u
"unit"_s, unit );
3343 distanceElem.appendChild( mDoc.createTextNode( distance ) );
3344 funcElem.appendChild( distanceElem );
3349 QDomElement funcElem = mDoc.createElement( mFilterPrefix +
":Function" );
3350 funcElem.setAttribute( u
"name"_s, node->
name() );
3351 const auto constList = node->
args()->
list();
3352 for ( QgsSQLStatement::Node *n : constList )
3355 if ( !mErrorMessage.isEmpty() )
3356 return QDomElement();
3358 funcElem.appendChild( childElem );
3365 QgsSQLStatement::Node *onExpr = node->
onExpr();
3371 QList<QDomElement> listElem;
3373 for (
const QString &columnName : constUsingColumns )
3375 QDomElement eqElem = mDoc.createElement( mFilterPrefix +
":PropertyIsEqualTo" );
3376 QDomElement propElem1 = mDoc.createElement( mFilterPrefix +
":" + mPropertyName );
3377 propElem1.appendChild( mDoc.createTextNode( leftTable +
"/" + columnName ) );
3378 eqElem.appendChild( propElem1 );
3379 QDomElement propElem2 = mDoc.createElement( mFilterPrefix +
":" + mPropertyName );
3380 propElem2.appendChild( mDoc.createTextNode( node->
tableDef()->
name() +
"/" + columnName ) );
3381 eqElem.appendChild( propElem2 );
3382 listElem.append( eqElem );
3385 if ( listElem.size() == 1 )
3389 else if ( listElem.size() > 1 )
3391 QDomElement andElem = mDoc.createElement( mFilterPrefix +
":And" );
3392 const auto constListElem = listElem;
3393 for (
const QDomElement &elem : constListElem )
3395 andElem.appendChild( elem );
3400 return QDomElement();
3405 if ( node->
alias().isEmpty() )
3407 mMapTableAliasToNames[node->
name()] = node->
name();
3411 mMapTableAliasToNames[node->
alias()] = node->
name();
3417 QList<QDomElement> listElem;
3421 mErrorMessage = QObject::tr(
"Joins are only supported with WFS 2.0" );
3422 return QDomElement();
3426 const auto constTables = node->
tables();
3427 for ( QgsSQLStatement::NodeTableDef *table : constTables )
3431 const auto constJoins = node->
joins();
3432 for ( QgsSQLStatement::NodeJoin *join : constJoins )
3434 visit( join->tableDef() );
3438 const QList< QgsSQLStatement::NodeTableDef *> nodeTables = node->
tables();
3439 QString leftTable = nodeTables.at( nodeTables.length() - 1 )->name();
3440 for ( QgsSQLStatement::NodeJoin *join : constJoins )
3442 const QDomElement joinElem =
toOgcFilter( join, leftTable );
3443 if ( !mErrorMessage.isEmpty() )
3444 return QDomElement();
3445 listElem.append( joinElem );
3446 leftTable = join->tableDef()->name();
3450 if ( node->
where() )
3453 if ( !mErrorMessage.isEmpty() )
3454 return QDomElement();
3455 listElem.append( whereElem );
3459 if ( listElem.size() == 1 )
3463 else if ( listElem.size() > 1 )
3465 QDomElement andElem = mDoc.createElement( mFilterPrefix +
":And" );
3466 const auto constListElem = listElem;
3467 for (
const QDomElement &elem : constListElem )
3469 andElem.appendChild( elem );
3474 return QDomElement();
3480 mPropertyName = u
"PropertyName"_s;
3485 mPropertyName = u
"ValueReference"_s;
3492 if ( element.isNull() )
3496 if ( isBinaryOperator( element.tagName() ) )
3502 if ( isSpatialOperator( element.tagName() ) )
3508 if ( element.tagName() ==
"Not"_L1 )
3512 else if ( element.tagName() ==
"PropertyIsNull"_L1 )
3516 else if ( element.tagName() ==
"Literal"_L1 )
3520 else if ( element.tagName() ==
"Function"_L1 )
3524 else if ( element.tagName() == mPropertyName )
3528 else if ( element.tagName() ==
"PropertyIsBetween"_L1 )
3533 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() );
3539 if ( element.isNull() )
3542 int op = binaryOperatorFromTagName( element.tagName() );
3545 mErrorMessage = QObject::tr(
"'%1' binary operator not supported." ).arg( element.tagName() );
3554 QDomElement operandElem = element.firstChildElement();
3559 mErrorMessage = QObject::tr(
"invalid left operand for '%1' binary operator" ).arg( element.tagName() );
3563 const std::unique_ptr<QgsExpressionNode> leftOp( expr->clone() );
3564 for ( operandElem = operandElem.nextSiblingElement(); !operandElem.isNull(); operandElem = operandElem.nextSiblingElement() )
3569 mErrorMessage = QObject::tr(
"invalid right operand for '%1' binary operator" ).arg( element.tagName() );
3576 if ( element.hasAttribute( u
"wildCard"_s ) )
3578 wildCard = element.attribute( u
"wildCard"_s );
3581 if ( element.hasAttribute( u
"singleChar"_s ) )
3583 singleChar = element.attribute( u
"singleChar"_s );
3585 QString escape = u
"\\"_s;
3586 if ( element.hasAttribute( u
"escape"_s ) )
3588 escape = element.attribute( u
"escape"_s );
3590 if ( element.hasAttribute( u
"escapeChar"_s ) )
3592 escape = element.attribute( u
"escapeChar"_s );
3596 if ( !wildCard.isEmpty() && wildCard !=
"%"_L1 )
3598 oprValue.replace(
'%',
"\\%"_L1 );
3599 if ( oprValue.startsWith( wildCard ) )
3601 oprValue.replace( 0, 1, u
"%"_s );
3604 QRegularExpressionMatch match = rx.match( oprValue );
3606 while ( match.hasMatch() )
3608 pos = match.capturedStart();
3609 oprValue.replace( pos + 1, 1, u
"%"_s );
3611 match = rx.match( oprValue, pos );
3613 oprValue.replace( escape + wildCard, wildCard );
3615 if ( !singleChar.isEmpty() && singleChar !=
"_"_L1 )
3617 oprValue.replace(
'_',
"\\_"_L1 );
3618 if ( oprValue.startsWith( singleChar ) )
3620 oprValue.replace( 0, 1, u
"_"_s );
3623 QRegularExpressionMatch match = rx.match( oprValue );
3625 while ( match.hasMatch() )
3627 pos = match.capturedStart();
3628 oprValue.replace( pos + 1, 1, u
"_"_s );
3630 match = rx.match( oprValue, pos );
3632 oprValue.replace( escape + singleChar, singleChar );
3634 if ( !escape.isEmpty() && escape !=
"\\"_L1 )
3636 oprValue.replace( escape + escape, escape );
3638 opRight = std::make_unique<QgsExpressionNodeLiteral>( oprValue );
3644 if ( expr == leftOp )
3646 mErrorMessage = QObject::tr(
"only one operand for '%1' binary operator" ).arg( element.tagName() );
3659 auto gml2Args = std::make_unique<QgsExpressionNode::NodeList>();
3660 QDomElement childElem = element.firstChildElement();
3662 while ( !childElem.isNull() && gml2Str.isEmpty() )
3664 if ( childElem.tagName() != mPropertyName )
3666 QTextStream gml2Stream( &gml2Str );
3667 childElem.save( gml2Stream, 0 );
3669 childElem = childElem.nextSiblingElement();
3671 if ( !gml2Str.isEmpty() )
3677 mErrorMessage = QObject::tr(
"No OGC Geometry found" );
3681 auto opArgs = std::make_unique<QgsExpressionNode::NodeList>();
3690 if ( element.isNull() || element.tagName() != mPropertyName )
3692 mErrorMessage = QObject::tr(
"%1:PropertyName expected, got %2" ).arg( mPrefix, element.tagName() );
3701 if ( element.isNull() || element.tagName() !=
"Literal"_L1 )
3703 mErrorMessage = QObject::tr(
"%1:Literal expected, got %2" ).arg( mPrefix, element.tagName() );
3707 std::unique_ptr<QgsExpressionNode> root;
3708 if ( !element.hasChildNodes() )
3710 root = std::make_unique<QgsExpressionNodeLiteral>( QVariant(
"" ) );
3711 return root.release();
3715 QDomNode childNode = element.firstChild();
3716 while ( !childNode.isNull() )
3718 std::unique_ptr<QgsExpressionNode> operand;
3720 if ( childNode.nodeType() == QDomNode::ElementNode )
3723 const QDomElement operandElem = childNode.toElement();
3727 mErrorMessage = QObject::tr(
"'%1' is an invalid or not supported content for %2:Literal" ).arg( operandElem.tagName(), mPrefix );
3734 QVariant value = childNode.nodeValue();
3736 bool converted =
false;
3741 QDomElement propertyNameElement = element.previousSiblingElement( mPropertyName );
3742 if ( propertyNameElement.isNull() || propertyNameElement.tagName() != mPropertyName )
3744 propertyNameElement = element.nextSiblingElement( mPropertyName );
3746 if ( !propertyNameElement.isNull() || propertyNameElement.tagName() == mPropertyName )
3748 const int fieldIndex = mLayer->fields().indexOf( propertyNameElement.firstChild().nodeValue() );
3749 if ( fieldIndex != -1 )
3751 const QgsField field = mLayer->fields().field( propertyNameElement.firstChild().nodeValue() );
3762 const double d = value.toDouble( &ok );
3767 operand = std::make_unique<QgsExpressionNodeLiteral>( value );
3773 root = std::move( operand );
3780 childNode = childNode.nextSibling();
3784 return root.release();
3791 if ( element.tagName() !=
"Not"_L1 )
3794 const QDomElement operandElem = element.firstChildElement();
3798 mErrorMessage = QObject::tr(
"invalid operand for '%1' unary operator" ).arg( element.tagName() );
3808 if ( element.tagName() !=
"PropertyIsNull"_L1 )
3813 const QDomElement operandElem = element.firstChildElement();
3824 if ( element.isNull() || element.tagName() !=
"Function"_L1 )
3826 mErrorMessage = QObject::tr(
"%1:Function expected, got %2" ).arg( mPrefix, element.tagName() );
3834 if ( element.attribute( u
"name"_s ) != funcDef->
name() )
3837 auto args = std::make_unique<QgsExpressionNode::NodeList>();
3839 QDomElement operandElem = element.firstChildElement();
3840 while ( !operandElem.isNull() )
3847 args->append( op.release() );
3849 operandElem = operandElem.nextSiblingElement();
3861 std::unique_ptr<QgsExpressionNode> operand;
3862 std::unique_ptr<QgsExpressionNode> lowerBound;
3863 std::unique_ptr<QgsExpressionNode> upperBound;
3865 QDomElement operandElem = element.firstChildElement();
3866 while ( !operandElem.isNull() )
3868 if ( operandElem.tagName() ==
"LowerBoundary"_L1 )
3870 const QDomElement lowerBoundElem = operandElem.firstChildElement();
3873 else if ( operandElem.tagName() ==
"UpperBoundary"_L1 )
3875 const QDomElement upperBoundElem = operandElem.firstChildElement();
3884 if ( operand && lowerBound && upperBound )
3887 operandElem = operandElem.nextSiblingElement();
3890 if ( !operand || !lowerBound || !upperBound )
3892 mErrorMessage = QObject::tr(
"missing some required sub-elements in %1:PropertyIsBetween" ).arg( mPrefix );
3903 return mErrorMessage;
3908 const thread_local QRegularExpression re_url( QRegularExpression::anchoredPattern( u
"http://www\\.opengis\\.net/gml/srs/epsg\\.xml#(.+)"_s ), QRegularExpression::CaseInsensitiveOption );
3909 if (
const QRegularExpressionMatch match = re_url.match( crsName ); match.hasMatch() )
3911 authority = u
"EPSG"_s;
3912 code = match.captured( 1 );
3916 const thread_local QRegularExpression re_ogc_urn( QRegularExpression::anchoredPattern( u
"urn:ogc:def:crs:([^:]+).+(?<=:)([^:]+)"_s ), QRegularExpression::CaseInsensitiveOption );
3917 if (
const QRegularExpressionMatch match = re_ogc_urn.match( crsName ); match.hasMatch() )
3919 authority = match.captured( 1 );
3920 code = match.captured( 2 );
3924 const thread_local QRegularExpression re_x_ogc_urn( QRegularExpression::anchoredPattern( u
"urn:x-ogc:def:crs:([^:]+).+(?<=:)([^:]+)"_s ), QRegularExpression::CaseInsensitiveOption );
3925 if (
const QRegularExpressionMatch match = re_x_ogc_urn.match( crsName ); match.hasMatch() )
3927 authority = match.captured( 1 );
3928 code = match.captured( 2 );
3932 const thread_local QRegularExpression re_http_uri( QRegularExpression::anchoredPattern( u
"http://www\\.opengis\\.net/def/crs/([^/]+).+/([^/]+)"_s ), QRegularExpression::CaseInsensitiveOption );
3933 if (
const QRegularExpressionMatch match = re_http_uri.match( crsName ); match.hasMatch() )
3935 authority = match.captured( 1 );
3936 code = match.captured( 2 );
3940 const thread_local QRegularExpression re_auth_code( QRegularExpression::anchoredPattern( u
"([^:]+):(.+)"_s ), QRegularExpression::CaseInsensitiveOption );
3941 if (
const QRegularExpressionMatch match = re_auth_code.match( crsName ); match.hasMatch() )
3943 authority = match.captured( 1 );
3944 code = match.captured( 2 );
3951QgsGeometry QgsOgcUtils::geometryFromGMLUsingGdal(
const QDomElement &geometryElement )
3954 QTextStream gmlStream( &gml );
3955 geometryElement.save( gmlStream, 0 );
3960QgsGeometry QgsOgcUtils::geometryFromGMLMultiCurve(
const QDomElement &geometryElement )
3962 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