30 #include <QStringList>
31 #include <QTextStream>
35 #include <netinet/in.h>
41 #define GML_NAMESPACE QStringLiteral( "http://www.opengis.net/gml" )
42 #define GML32_NAMESPACE QStringLiteral( "http://www.opengis.net/gml/3.2" )
43 #define OGC_NAMESPACE QStringLiteral( "http://www.opengis.net/ogc" )
44 #define FES_NAMESPACE QStringLiteral( "http://www.opengis.net/fes/2.0" )
50 const QString &srsName,
51 bool honourAxisOrientation,
52 bool invertAxisOrientation )
55 , mGMLVersion( gmlVersion )
56 , mFilterVersion( filterVersion )
59 , mInvertAxisOrientation( invertAxisOrientation )
60 , mFilterPrefix( ( filterVersion ==
QgsOgcUtils::FILTER_FES_2_0 ) ?
"fes" :
"ogc" )
61 , mPropertyName( ( filterVersion ==
QgsOgcUtils::FILTER_FES_2_0 ) ?
"ValueReference" :
"PropertyName" )
65 if ( !mSrsName.isEmpty() )
71 mInvertAxisOrientation = !mInvertAxisOrientation;
78 QDomElement geometryTypeElement = geometryNode.toElement();
79 QString geomType = geometryTypeElement.tagName();
82 if ( !( geomType == QLatin1String(
"Point" ) || geomType == QLatin1String(
"LineString" ) || geomType == QLatin1String(
"Polygon" ) ||
83 geomType == QLatin1String(
"MultiPoint" ) || geomType == QLatin1String(
"MultiLineString" ) || geomType == QLatin1String(
"MultiPolygon" ) ||
84 geomType == QLatin1String(
"Box" ) || geomType == QLatin1String(
"Envelope" ) ) )
86 QDomNode geometryChild = geometryNode.firstChild();
87 if ( geometryChild.isNull() )
91 geometryTypeElement = geometryChild.toElement();
92 geomType = geometryTypeElement.tagName();
95 if ( !( geomType == QLatin1String(
"Point" ) || geomType == QLatin1String(
"LineString" ) || geomType == QLatin1String(
"Polygon" ) ||
96 geomType == QLatin1String(
"MultiPoint" ) || geomType == QLatin1String(
"MultiLineString" ) || geomType == QLatin1String(
"MultiPolygon" ) ||
97 geomType == QLatin1String(
"Box" ) || geomType == QLatin1String(
"Envelope" ) ) )
100 if ( geomType == QLatin1String(
"Point" ) )
102 geometry = geometryFromGMLPoint( geometryTypeElement );
104 else if ( geomType == QLatin1String(
"LineString" ) )
106 geometry = geometryFromGMLLineString( geometryTypeElement );
108 else if ( geomType == QLatin1String(
"Polygon" ) )
110 geometry = geometryFromGMLPolygon( geometryTypeElement );
112 else if ( geomType == QLatin1String(
"MultiPoint" ) )
114 geometry = geometryFromGMLMultiPoint( geometryTypeElement );
116 else if ( geomType == QLatin1String(
"MultiLineString" ) )
118 geometry = geometryFromGMLMultiLineString( geometryTypeElement );
120 else if ( geomType == QLatin1String(
"MultiPolygon" ) )
122 geometry = geometryFromGMLMultiPolygon( geometryTypeElement );
124 else if ( geomType == QLatin1String(
"Box" ) )
128 else if ( geomType == QLatin1String(
"Envelope" ) )
142 if ( geometryTypeElement.hasAttribute( QStringLiteral(
"srsName" ) ) )
144 geomSrs.
createFromString( geometryTypeElement.attribute( QStringLiteral(
"srsName" ) ) );
151 if ( result != QgsGeometry::OperationResult::Success )
153 QgsDebugMsgLevel( QStringLiteral(
"Error transforming geometry: %1" ).arg( result ), 2 );
170 QString xml = QStringLiteral(
"<tmp xmlns=\"%1\" xmlns:gml=\"%1\">%2</tmp>" ).arg(
GML_NAMESPACE, xmlString );
172 if ( !doc.setContent( xml,
true ) )
175 return geometryFromGML( doc.documentElement().firstChildElement(), context );
179 QgsGeometry QgsOgcUtils::geometryFromGMLPoint(
const QDomElement &geometryElement )
183 QDomNodeList coordList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"coordinates" ) );
184 if ( !coordList.isEmpty() )
186 QDomElement coordElement = coordList.at( 0 ).toElement();
187 if ( readGMLCoordinates( pointCoordinate, coordElement ) != 0 )
194 QDomNodeList posList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"pos" ) );
195 if ( posList.size() < 1 )
199 QDomElement posElement = posList.at( 0 ).toElement();
200 if ( readGMLPositions( pointCoordinate, posElement ) != 0 )
206 if ( pointCoordinate.empty() )
211 QgsPolylineXY::const_iterator point_it = pointCoordinate.constBegin();
212 char e = htonl( 1 ) != 1;
213 double x = point_it->x();
214 double y = point_it->y();
215 int size = 1 +
sizeof( int ) + 2 *
sizeof(
double );
218 unsigned char *wkb =
new unsigned char[size];
221 memcpy( &( wkb )[wkbPosition], &e, 1 );
223 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
224 wkbPosition +=
sizeof( int );
225 memcpy( &( wkb )[wkbPosition], &x,
sizeof(
double ) );
226 wkbPosition +=
sizeof( double );
227 memcpy( &( wkb )[wkbPosition], &y,
sizeof(
double ) );
234 QgsGeometry QgsOgcUtils::geometryFromGMLLineString(
const QDomElement &geometryElement )
238 QDomNodeList coordList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"coordinates" ) );
239 if ( !coordList.isEmpty() )
241 QDomElement coordElement = coordList.at( 0 ).toElement();
242 if ( readGMLCoordinates( lineCoordinates, coordElement ) != 0 )
249 QDomNodeList posList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"posList" ) );
250 if ( posList.size() < 1 )
254 QDomElement posElement = posList.at( 0 ).toElement();
255 if ( readGMLPositions( lineCoordinates, posElement ) != 0 )
261 char e = htonl( 1 ) != 1;
262 int size = 1 + 2 *
sizeof( int ) + lineCoordinates.size() * 2 *
sizeof( double );
265 unsigned char *wkb =
new unsigned char[size];
269 int nPoints = lineCoordinates.size();
272 memcpy( &( wkb )[wkbPosition], &e, 1 );
274 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
275 wkbPosition +=
sizeof( int );
276 memcpy( &( wkb )[wkbPosition], &nPoints,
sizeof(
int ) );
277 wkbPosition +=
sizeof( int );
279 QgsPolylineXY::const_iterator iter;
280 for ( iter = lineCoordinates.constBegin(); iter != lineCoordinates.constEnd(); ++iter )
284 memcpy( &( wkb )[wkbPosition], &x,
sizeof(
double ) );
285 wkbPosition +=
sizeof( double );
286 memcpy( &( wkb )[wkbPosition], &y,
sizeof(
double ) );
287 wkbPosition +=
sizeof( double );
295 QgsGeometry QgsOgcUtils::geometryFromGMLPolygon(
const QDomElement &geometryElement )
302 QDomNodeList outerBoundaryList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"outerBoundaryIs" ) );
303 if ( !outerBoundaryList.isEmpty() )
305 QDomElement coordinatesElement = outerBoundaryList.at( 0 ).firstChild().firstChild().toElement();
306 if ( coordinatesElement.isNull() )
310 if ( readGMLCoordinates( exteriorPointList, coordinatesElement ) != 0 )
314 ringCoordinates.push_back( exteriorPointList );
317 QDomNodeList innerBoundaryList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"innerBoundaryIs" ) );
318 for (
int i = 0; i < innerBoundaryList.size(); ++i )
321 coordinatesElement = innerBoundaryList.at( i ).firstChild().firstChild().toElement();
322 if ( coordinatesElement.isNull() )
326 if ( readGMLCoordinates( interiorPointList, coordinatesElement ) != 0 )
330 ringCoordinates.push_back( interiorPointList );
336 QDomNodeList exteriorList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"exterior" ) );
337 if ( exteriorList.size() < 1 )
341 QDomElement posElement = exteriorList.at( 0 ).firstChild().firstChild().toElement();
342 if ( posElement.isNull() )
346 if ( readGMLPositions( exteriorPointList, posElement ) != 0 )
350 ringCoordinates.push_back( exteriorPointList );
353 QDomNodeList interiorList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"interior" ) );
354 for (
int i = 0; i < interiorList.size(); ++i )
357 QDomElement posElement = interiorList.at( i ).firstChild().firstChild().toElement();
358 if ( posElement.isNull() )
363 if ( readGMLPositions( interiorPointList, posElement ) )
367 ringCoordinates.push_back( interiorPointList );
372 int nrings = ringCoordinates.size();
377 for ( QgsMultiPolylineXY::const_iterator it = ringCoordinates.constBegin(); it != ringCoordinates.constEnd(); ++it )
379 npoints += it->size();
381 int size = 1 + 2 *
sizeof( int ) + nrings *
sizeof(
int ) + 2 * npoints *
sizeof( double );
384 unsigned char *wkb =
new unsigned char[size];
387 char e = htonl( 1 ) != 1;
389 int nPointsInRing = 0;
393 memcpy( &( wkb )[wkbPosition], &e, 1 );
395 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
396 wkbPosition +=
sizeof( int );
397 memcpy( &( wkb )[wkbPosition], &nrings,
sizeof(
int ) );
398 wkbPosition +=
sizeof( int );
399 for ( QgsMultiPolylineXY::const_iterator it = ringCoordinates.constBegin(); it != ringCoordinates.constEnd(); ++it )
401 nPointsInRing = it->size();
402 memcpy( &( wkb )[wkbPosition], &nPointsInRing,
sizeof(
int ) );
403 wkbPosition +=
sizeof( int );
405 QgsPolylineXY::const_iterator iter;
406 for ( iter = it->begin(); iter != it->end(); ++iter )
411 memcpy( &( wkb )[wkbPosition], &x,
sizeof(
double ) );
412 wkbPosition +=
sizeof( double );
413 memcpy( &( wkb )[wkbPosition], &y,
sizeof(
double ) );
414 wkbPosition +=
sizeof( double );
423 QgsGeometry QgsOgcUtils::geometryFromGMLMultiPoint(
const QDomElement &geometryElement )
427 QDomNodeList pointMemberList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"pointMember" ) );
428 if ( pointMemberList.size() < 1 )
432 QDomNodeList pointNodeList;
434 QDomNodeList coordinatesList;
435 QDomNodeList posList;
436 for (
int i = 0; i < pointMemberList.size(); ++i )
439 pointNodeList = pointMemberList.at( i ).toElement().elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"Point" ) );
440 if ( pointNodeList.size() < 1 )
445 coordinatesList = pointNodeList.at( 0 ).toElement().elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"coordinates" ) );
446 if ( !coordinatesList.isEmpty() )
448 currentPoint.clear();
449 if ( readGMLCoordinates( currentPoint, coordinatesList.at( 0 ).toElement() ) != 0 )
453 if ( currentPoint.empty() )
457 pointList.push_back( ( *currentPoint.begin() ) );
463 posList = pointNodeList.at( 0 ).toElement().elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"pos" ) );
464 if ( posList.size() < 1 )
468 currentPoint.clear();
469 if ( readGMLPositions( currentPoint, posList.at( 0 ).toElement() ) != 0 )
473 if ( currentPoint.empty() )
477 pointList.push_back( ( *currentPoint.begin() ) );
481 int nPoints = pointList.size();
486 int size = 1 + 2 *
sizeof( int ) + pointList.size() * ( 2 *
sizeof( double ) + 1 +
sizeof(
int ) );
489 unsigned char *wkb =
new unsigned char[size];
492 char e = htonl( 1 ) != 1;
495 memcpy( &( wkb )[wkbPosition], &e, 1 );
497 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
498 wkbPosition +=
sizeof( int );
499 memcpy( &( wkb )[wkbPosition], &nPoints,
sizeof(
int ) );
500 wkbPosition +=
sizeof( int );
502 for ( QgsPolylineXY::const_iterator it = pointList.constBegin(); it != pointList.constEnd(); ++it )
504 memcpy( &( wkb )[wkbPosition], &e, 1 );
506 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
507 wkbPosition +=
sizeof( int );
509 memcpy( &( wkb )[wkbPosition], &x,
sizeof(
double ) );
510 wkbPosition +=
sizeof( double );
512 memcpy( &( wkb )[wkbPosition], &y,
sizeof(
double ) );
513 wkbPosition +=
sizeof( double );
521 QgsGeometry QgsOgcUtils::geometryFromGMLMultiLineString(
const QDomElement &geometryElement )
532 QList< QgsPolylineXY > lineCoordinates;
533 QDomElement currentLineStringElement;
534 QDomNodeList currentCoordList;
535 QDomNodeList currentPosList;
537 QDomNodeList lineStringMemberList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"lineStringMember" ) );
538 if ( !lineStringMemberList.isEmpty() )
540 for (
int i = 0; i < lineStringMemberList.size(); ++i )
542 QDomNodeList lineStringNodeList = lineStringMemberList.at( i ).toElement().elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"LineString" ) );
543 if ( lineStringNodeList.size() < 1 )
547 currentLineStringElement = lineStringNodeList.at( 0 ).toElement();
548 currentCoordList = currentLineStringElement.elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"coordinates" ) );
549 if ( !currentCoordList.isEmpty() )
552 if ( readGMLCoordinates( currentPointList, currentCoordList.at( 0 ).toElement() ) != 0 )
556 lineCoordinates.push_back( currentPointList );
560 currentPosList = currentLineStringElement.elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"posList" ) );
561 if ( currentPosList.size() < 1 )
566 if ( readGMLPositions( currentPointList, currentPosList.at( 0 ).toElement() ) != 0 )
570 lineCoordinates.push_back( currentPointList );
576 QDomNodeList lineStringList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"LineString" ) );
577 if ( !lineStringList.isEmpty() )
579 for (
int i = 0; i < lineStringList.size(); ++i )
581 currentLineStringElement = lineStringList.at( i ).toElement();
582 currentCoordList = currentLineStringElement.elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"coordinates" ) );
583 if ( !currentCoordList.isEmpty() )
586 if ( readGMLCoordinates( currentPointList, currentCoordList.at( 0 ).toElement() ) != 0 )
590 lineCoordinates.push_back( currentPointList );
595 currentPosList = currentLineStringElement.elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"posList" ) );
596 if ( currentPosList.size() < 1 )
601 if ( readGMLPositions( currentPointList, currentPosList.at( 0 ).toElement() ) != 0 )
605 lineCoordinates.push_back( currentPointList );
615 int nLines = lineCoordinates.size();
620 int size = ( lineCoordinates.size() + 1 ) * ( 1 + 2 *
sizeof(
int ) );
621 for ( QList< QgsPolylineXY >::const_iterator it = lineCoordinates.constBegin(); it != lineCoordinates.constEnd(); ++it )
623 size += it->size() * 2 *
sizeof( double );
627 unsigned char *wkb =
new unsigned char[size];
630 char e = htonl( 1 ) != 1;
634 memcpy( &( wkb )[wkbPosition], &e, 1 );
636 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
637 wkbPosition +=
sizeof( int );
638 memcpy( &( wkb )[wkbPosition], &nLines,
sizeof(
int ) );
639 wkbPosition +=
sizeof( int );
641 for ( QList< QgsPolylineXY >::const_iterator it = lineCoordinates.constBegin(); it != lineCoordinates.constEnd(); ++it )
643 memcpy( &( wkb )[wkbPosition], &e, 1 );
645 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
646 wkbPosition +=
sizeof( int );
647 nPoints = it->size();
648 memcpy( &( wkb )[wkbPosition], &nPoints,
sizeof(
int ) );
649 wkbPosition +=
sizeof( int );
650 for ( QgsPolylineXY::const_iterator iter = it->begin(); iter != it->end(); ++iter )
655 memcpy( &( wkb )[wkbPosition], &x,
sizeof(
double ) );
656 wkbPosition +=
sizeof( double );
657 memcpy( &( wkb )[wkbPosition], &y,
sizeof(
double ) );
658 wkbPosition +=
sizeof( double );
667 QgsGeometry QgsOgcUtils::geometryFromGMLMultiPolygon(
const QDomElement &geometryElement )
671 QDomElement currentPolygonMemberElement;
672 QDomNodeList polygonList;
673 QDomElement currentPolygonElement;
675 QDomNodeList outerBoundaryList;
676 QDomElement currentOuterBoundaryElement;
677 QDomNodeList innerBoundaryList;
678 QDomElement currentInnerBoundaryElement;
680 QDomNodeList exteriorList;
681 QDomElement currentExteriorElement;
682 QDomElement currentInteriorElement;
683 QDomNodeList interiorList;
685 QDomNodeList linearRingNodeList;
686 QDomElement currentLinearRingElement;
688 QDomNodeList currentCoordinateList;
689 QDomNodeList currentPosList;
691 QDomNodeList polygonMemberList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"polygonMember" ) );
693 for (
int i = 0; i < polygonMemberList.size(); ++i )
695 currentPolygonList.resize( 0 );
696 currentPolygonMemberElement = polygonMemberList.at( i ).toElement();
697 polygonList = currentPolygonMemberElement.elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"Polygon" ) );
698 if ( polygonList.size() < 1 )
702 currentPolygonElement = polygonList.at( 0 ).toElement();
705 outerBoundaryList = currentPolygonElement.elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"outerBoundaryIs" ) );
706 if ( !outerBoundaryList.isEmpty() )
708 currentOuterBoundaryElement = outerBoundaryList.at( 0 ).toElement();
711 linearRingNodeList = currentOuterBoundaryElement.elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"LinearRing" ) );
712 if ( linearRingNodeList.size() < 1 )
716 currentLinearRingElement = linearRingNodeList.at( 0 ).toElement();
717 currentCoordinateList = currentLinearRingElement.elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"coordinates" ) );
718 if ( currentCoordinateList.size() < 1 )
722 if ( readGMLCoordinates( ringCoordinates, currentCoordinateList.at( 0 ).toElement() ) != 0 )
726 currentPolygonList.push_back( ringCoordinates );
729 QDomNodeList innerBoundaryList = currentPolygonElement.elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"innerBoundaryIs" ) );
730 for (
int j = 0; j < innerBoundaryList.size(); ++j )
733 currentInnerBoundaryElement = innerBoundaryList.at( j ).toElement();
734 linearRingNodeList = currentInnerBoundaryElement.elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"LinearRing" ) );
735 if ( linearRingNodeList.size() < 1 )
739 currentLinearRingElement = linearRingNodeList.at( 0 ).toElement();
740 currentCoordinateList = currentLinearRingElement.elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"coordinates" ) );
741 if ( currentCoordinateList.size() < 1 )
745 if ( readGMLCoordinates( ringCoordinates, currentCoordinateList.at( 0 ).toElement() ) != 0 )
749 currentPolygonList.push_back( ringCoordinates );
755 exteriorList = currentPolygonElement.elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"exterior" ) );
756 if ( exteriorList.size() < 1 )
761 currentExteriorElement = exteriorList.at( 0 ).toElement();
764 linearRingNodeList = currentExteriorElement.elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"LinearRing" ) );
765 if ( linearRingNodeList.size() < 1 )
769 currentLinearRingElement = linearRingNodeList.at( 0 ).toElement();
770 currentPosList = currentLinearRingElement.elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"posList" ) );
771 if ( currentPosList.size() < 1 )
775 if ( readGMLPositions( ringPositions, currentPosList.at( 0 ).toElement() ) != 0 )
779 currentPolygonList.push_back( ringPositions );
782 QDomNodeList interiorList = currentPolygonElement.elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"interior" ) );
783 for (
int j = 0; j < interiorList.size(); ++j )
786 currentInteriorElement = interiorList.at( j ).toElement();
787 linearRingNodeList = currentInteriorElement.elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"LinearRing" ) );
788 if ( linearRingNodeList.size() < 1 )
792 currentLinearRingElement = linearRingNodeList.at( 0 ).toElement();
793 currentPosList = currentLinearRingElement.elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"posList" ) );
794 if ( currentPosList.size() < 1 )
798 if ( readGMLPositions( ringPositions, currentPosList.at( 0 ).toElement() ) != 0 )
802 currentPolygonList.push_back( ringPositions );
805 multiPolygonPoints.push_back( currentPolygonList );
808 int nPolygons = multiPolygonPoints.size();
812 int size = 1 + 2 *
sizeof( int );
814 for ( QgsMultiPolygonXY::const_iterator it = multiPolygonPoints.constBegin(); it != multiPolygonPoints.constEnd(); ++it )
816 size += 1 + 2 *
sizeof( int );
817 for ( QgsPolygonXY::const_iterator iter = it->begin(); iter != it->end(); ++iter )
819 size +=
sizeof( int ) + 2 * iter->size() *
sizeof( double );
824 unsigned char *wkb =
new unsigned char[size];
826 char e = htonl( 1 ) != 1;
833 memcpy( &( wkb )[wkbPosition], &e, 1 );
835 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
836 wkbPosition +=
sizeof( int );
837 memcpy( &( wkb )[wkbPosition], &nPolygons,
sizeof(
int ) );
838 wkbPosition +=
sizeof( int );
842 for ( QgsMultiPolygonXY::const_iterator it = multiPolygonPoints.constBegin(); it != multiPolygonPoints.constEnd(); ++it )
844 memcpy( &( wkb )[wkbPosition], &e, 1 );
846 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
847 wkbPosition +=
sizeof( int );
849 memcpy( &( wkb )[wkbPosition], &nRings,
sizeof(
int ) );
850 wkbPosition +=
sizeof( int );
851 for ( QgsPolygonXY::const_iterator iter = it->begin(); iter != it->end(); ++iter )
853 nPointsInRing = iter->size();
854 memcpy( &( wkb )[wkbPosition], &nPointsInRing,
sizeof(
int ) );
855 wkbPosition +=
sizeof( int );
856 for ( QgsPolylineXY::const_iterator iterator = iter->begin(); iterator != iter->end(); ++iterator )
860 memcpy( &( wkb )[wkbPosition], &x,
sizeof(
double ) );
861 wkbPosition +=
sizeof( double );
862 memcpy( &( wkb )[wkbPosition], &y,
sizeof(
double ) );
863 wkbPosition +=
sizeof( double );
873 bool QgsOgcUtils::readGMLCoordinates(
QgsPolylineXY &coords,
const QDomElement &elem )
875 QString coordSeparator = QStringLiteral(
"," );
876 QString tupelSeparator = QStringLiteral(
" " );
881 if ( elem.hasAttribute( QStringLiteral(
"cs" ) ) )
883 coordSeparator = elem.attribute( QStringLiteral(
"cs" ) );
885 if ( elem.hasAttribute( QStringLiteral(
"ts" ) ) )
887 tupelSeparator = elem.attribute( QStringLiteral(
"ts" ) );
890 QStringList tupels = elem.text().split( tupelSeparator, QString::SkipEmptyParts );
891 QStringList tuple_coords;
893 bool conversionSuccess;
895 QStringList::const_iterator it;
896 for ( it = tupels.constBegin(); it != tupels.constEnd(); ++it )
898 tuple_coords = ( *it ).split( coordSeparator, QString::SkipEmptyParts );
899 if ( tuple_coords.size() < 2 )
903 x = tuple_coords.at( 0 ).toDouble( &conversionSuccess );
904 if ( !conversionSuccess )
908 y = tuple_coords.at( 1 ).toDouble( &conversionSuccess );
909 if ( !conversionSuccess )
922 QDomElement boxElem = boxNode.toElement();
923 if ( boxElem.tagName() != QLatin1String(
"Box" ) )
926 QDomElement bElem = boxElem.firstChild().toElement();
927 QString coordSeparator = QStringLiteral(
"," );
928 QString tupelSeparator = QStringLiteral(
" " );
929 if ( bElem.hasAttribute( QStringLiteral(
"cs" ) ) )
931 coordSeparator = bElem.attribute( QStringLiteral(
"cs" ) );
933 if ( bElem.hasAttribute( QStringLiteral(
"ts" ) ) )
935 tupelSeparator = bElem.attribute( QStringLiteral(
"ts" ) );
938 QString bString = bElem.text();
939 bool ok1, ok2, ok3, ok4;
940 double xmin = bString.section( tupelSeparator, 0, 0 ).section( coordSeparator, 0, 0 ).toDouble( &ok1 );
941 double ymin = bString.section( tupelSeparator, 0, 0 ).section( coordSeparator, 1, 1 ).toDouble( &ok2 );
942 double xmax = bString.section( tupelSeparator, 1, 1 ).section( coordSeparator, 0, 0 ).toDouble( &ok3 );
943 double ymax = bString.section( tupelSeparator, 1, 1 ).section( coordSeparator, 1, 1 ).toDouble( &ok4 );
945 if ( ok1 && ok2 && ok3 && ok4 )
954 bool QgsOgcUtils::readGMLPositions(
QgsPolylineXY &coords,
const QDomElement &elem )
958 QStringList pos = elem.text().split(
' ', QString::SkipEmptyParts );
960 bool conversionSuccess;
961 int posSize = pos.size();
963 int srsDimension = 2;
964 if ( elem.hasAttribute( QStringLiteral(
"srsDimension" ) ) )
966 srsDimension = elem.attribute( QStringLiteral(
"srsDimension" ) ).toInt( &conversionSuccess );
967 if ( !conversionSuccess )
972 else if ( elem.hasAttribute( QStringLiteral(
"dimension" ) ) )
974 srsDimension = elem.attribute( QStringLiteral(
"dimension" ) ).toInt( &conversionSuccess );
975 if ( !conversionSuccess )
981 for (
int i = 0; i < posSize / srsDimension; i++ )
983 x = pos.at( i * srsDimension ).toDouble( &conversionSuccess );
984 if ( !conversionSuccess )
988 y = pos.at( i * srsDimension + 1 ).toDouble( &conversionSuccess );
989 if ( !conversionSuccess )
1003 QDomElement envelopeElem = envelopeNode.toElement();
1004 if ( envelopeElem.tagName() != QLatin1String(
"Envelope" ) )
1007 QDomNodeList lowerCornerList = envelopeElem.elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"lowerCorner" ) );
1008 if ( lowerCornerList.size() < 1 )
1011 QDomNodeList upperCornerList = envelopeElem.elementsByTagNameNS(
GML_NAMESPACE, QStringLiteral(
"upperCorner" ) );
1012 if ( upperCornerList.size() < 1 )
1015 bool conversionSuccess;
1016 int srsDimension = 2;
1018 QDomElement elem = lowerCornerList.at( 0 ).toElement();
1019 if ( elem.hasAttribute( QStringLiteral(
"srsDimension" ) ) )
1021 srsDimension = elem.attribute( QStringLiteral(
"srsDimension" ) ).toInt( &conversionSuccess );
1022 if ( !conversionSuccess )
1027 else if ( elem.hasAttribute( QStringLiteral(
"dimension" ) ) )
1029 srsDimension = elem.attribute( QStringLiteral(
"dimension" ) ).toInt( &conversionSuccess );
1030 if ( !conversionSuccess )
1035 QString bString = elem.text();
1037 double xmin = bString.section(
' ', 0, 0 ).toDouble( &conversionSuccess );
1038 if ( !conversionSuccess )
1040 double ymin = bString.section(
' ', 1, 1 ).toDouble( &conversionSuccess );
1041 if ( !conversionSuccess )
1044 elem = upperCornerList.at( 0 ).toElement();
1045 if ( elem.hasAttribute( QStringLiteral(
"srsDimension" ) ) )
1047 srsDimension = elem.attribute( QStringLiteral(
"srsDimension" ) ).toInt( &conversionSuccess );
1048 if ( !conversionSuccess )
1053 else if ( elem.hasAttribute( QStringLiteral(
"dimension" ) ) )
1055 srsDimension = elem.attribute( QStringLiteral(
"dimension" ) ).toInt( &conversionSuccess );
1056 if ( !conversionSuccess )
1062 Q_UNUSED( srsDimension )
1064 bString = elem.text();
1065 double xmax = bString.section(
' ', 0, 0 ).toDouble( &conversionSuccess );
1066 if ( !conversionSuccess )
1068 double ymax = bString.section(
' ', 1, 1 ).toDouble( &conversionSuccess );
1069 if ( !conversionSuccess )
1084 const QString &srsName,
1085 bool invertAxisOrientation,
1090 return QDomElement();
1093 QDomElement boxElem = doc.createElement( QStringLiteral(
"gml:Box" ) );
1094 if ( !srsName.isEmpty() )
1096 boxElem.setAttribute( QStringLiteral(
"srsName" ), srsName );
1098 QDomElement coordElem = doc.createElement( QStringLiteral(
"gml:coordinates" ) );
1099 coordElem.setAttribute( QStringLiteral(
"cs" ), QStringLiteral(
"," ) );
1100 coordElem.setAttribute( QStringLiteral(
"ts" ), QStringLiteral(
" " ) );
1102 QString coordString;
1111 QDomText coordText = doc.createTextNode( coordString );
1112 coordElem.appendChild( coordText );
1113 boxElem.appendChild( coordElem );
1124 const QString &srsName,
1125 bool invertAxisOrientation,
1130 return QDomElement();
1133 QDomElement envElem = doc.createElement( QStringLiteral(
"gml:Envelope" ) );
1134 if ( !srsName.isEmpty() )
1136 envElem.setAttribute( QStringLiteral(
"srsName" ), srsName );
1140 QDomElement lowerCornerElem = doc.createElement( QStringLiteral(
"gml:lowerCorner" ) );
1144 QDomText lowerCornerText = doc.createTextNode( posList );
1145 lowerCornerElem.appendChild( lowerCornerText );
1146 envElem.appendChild( lowerCornerElem );
1148 QDomElement upperCornerElem = doc.createElement( QStringLiteral(
"gml:upperCorner" ) );
1152 QDomText upperCornerText = doc.createTextNode( posList );
1153 upperCornerElem.appendChild( upperCornerText );
1154 envElem.appendChild( upperCornerElem );
1166 const QString &srsName,
1167 bool invertAxisOrientation,
1168 const QString &gmlIdBase,
1172 return QDomElement();
1175 QString cs = QStringLiteral(
"," );
1177 QString ts = QStringLiteral(
" " );
1179 QDomElement baseCoordElem;
1181 bool hasZValue =
false;
1183 QByteArray wkb( geometry.
asWkb() );
1193 return QDomElement();
1204 baseCoordElem = doc.createElement( QStringLiteral(
"gml:pos" ) );
1207 baseCoordElem = doc.createElement( QStringLiteral(
"gml:posList" ) );
1210 baseCoordElem.setAttribute( QStringLiteral(
"srsDimension" ), QStringLiteral(
"2" ) );
1215 baseCoordElem = doc.createElement( QStringLiteral(
"gml:coordinates" ) );
1216 baseCoordElem.setAttribute( QStringLiteral(
"cs" ), cs );
1217 baseCoordElem.setAttribute( QStringLiteral(
"ts" ), ts );
1227 QDomElement pointElem = doc.createElement( QStringLiteral(
"gml:Point" ) );
1228 if ( gmlVersion ==
GML_3_2_1 && !gmlIdBase.isEmpty() )
1229 pointElem.setAttribute( QStringLiteral(
"gml:id" ), gmlIdBase );
1230 if ( !srsName.isEmpty() )
1231 pointElem.setAttribute( QStringLiteral(
"srsName" ), srsName );
1232 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1236 if ( invertAxisOrientation )
1242 coordElem.appendChild( coordText );
1243 pointElem.appendChild( coordElem );
1252 QDomElement multiPointElem = doc.createElement( QStringLiteral(
"gml:MultiPoint" ) );
1253 if ( gmlVersion ==
GML_3_2_1 && !gmlIdBase.isEmpty() )
1254 multiPointElem.setAttribute( QStringLiteral(
"gml:id" ), gmlIdBase );
1255 if ( !srsName.isEmpty() )
1256 multiPointElem.setAttribute( QStringLiteral(
"srsName" ), srsName );
1261 for (
int idx = 0; idx < nPoints; ++idx )
1263 QDomElement pointMemberElem = doc.createElement( QStringLiteral(
"gml:pointMember" ) );
1264 QDomElement pointElem = doc.createElement( QStringLiteral(
"gml:Point" ) );
1265 if ( gmlVersion ==
GML_3_2_1 && !gmlIdBase.isEmpty() )
1266 pointElem.setAttribute( QStringLiteral(
"gml:id" ), gmlIdBase + QStringLiteral(
".%1" ).arg( idx + 1 ) );
1267 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1272 if ( invertAxisOrientation )
1278 coordElem.appendChild( coordText );
1279 pointElem.appendChild( coordElem );
1283 wkbPtr +=
sizeof( double );
1285 pointMemberElem.appendChild( pointElem );
1286 multiPointElem.appendChild( pointMemberElem );
1288 return multiPointElem;
1296 QDomElement lineStringElem = doc.createElement( QStringLiteral(
"gml:LineString" ) );
1297 if ( gmlVersion ==
GML_3_2_1 && !gmlIdBase.isEmpty() )
1298 lineStringElem.setAttribute( QStringLiteral(
"gml:id" ), gmlIdBase );
1299 if ( !srsName.isEmpty() )
1300 lineStringElem.setAttribute( QStringLiteral(
"srsName" ), srsName );
1306 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1307 QString coordString;
1308 for (
int idx = 0; idx < nPoints; ++idx )
1316 if ( invertAxisOrientation )
1324 wkbPtr +=
sizeof( double );
1327 QDomText coordText = doc.createTextNode( coordString );
1328 coordElem.appendChild( coordText );
1329 lineStringElem.appendChild( coordElem );
1330 return lineStringElem;
1338 QDomElement multiLineStringElem = doc.createElement( QStringLiteral(
"gml:MultiLineString" ) );
1339 if ( gmlVersion ==
GML_3_2_1 && !gmlIdBase.isEmpty() )
1340 multiLineStringElem.setAttribute( QStringLiteral(
"gml:id" ), gmlIdBase );
1341 if ( !srsName.isEmpty() )
1342 multiLineStringElem.setAttribute( QStringLiteral(
"srsName" ), srsName );
1347 for (
int jdx = 0; jdx < nLines; jdx++ )
1349 QDomElement lineStringMemberElem = doc.createElement( QStringLiteral(
"gml:lineStringMember" ) );
1350 QDomElement lineStringElem = doc.createElement( QStringLiteral(
"gml:LineString" ) );
1351 if ( gmlVersion ==
GML_3_2_1 && !gmlIdBase.isEmpty() )
1352 lineStringElem.setAttribute( QStringLiteral(
"gml:id" ), gmlIdBase + QStringLiteral(
".%1" ).arg( jdx + 1 ) );
1359 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1360 QString coordString;
1361 for (
int idx = 0; idx < nPoints; idx++ )
1369 if ( invertAxisOrientation )
1378 wkbPtr +=
sizeof( double );
1381 QDomText coordText = doc.createTextNode( coordString );
1382 coordElem.appendChild( coordText );
1383 lineStringElem.appendChild( coordElem );
1384 lineStringMemberElem.appendChild( lineStringElem );
1385 multiLineStringElem.appendChild( lineStringMemberElem );
1387 return multiLineStringElem;
1395 QDomElement polygonElem = doc.createElement( QStringLiteral(
"gml:Polygon" ) );
1396 if ( gmlVersion ==
GML_3_2_1 && !gmlIdBase.isEmpty() )
1397 polygonElem.setAttribute( QStringLiteral(
"gml:id" ), gmlIdBase );
1398 if ( !srsName.isEmpty() )
1399 polygonElem.setAttribute( QStringLiteral(
"srsName" ), srsName );
1405 if ( numRings == 0 )
1406 return QDomElement();
1408 int *ringNumPoints =
new int[numRings];
1410 for (
int idx = 0; idx < numRings; idx++ )
1412 QString boundaryName = ( gmlVersion ==
GML_2_1_2 ) ?
"gml:outerBoundaryIs" :
"gml:exterior";
1415 boundaryName = ( gmlVersion ==
GML_2_1_2 ) ?
"gml:innerBoundaryIs" :
"gml:interior";
1417 QDomElement boundaryElem = doc.createElement( boundaryName );
1418 QDomElement ringElem = doc.createElement( QStringLiteral(
"gml:LinearRing" ) );
1422 ringNumPoints[idx] = nPoints;
1424 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1425 QString coordString;
1426 for (
int jdx = 0; jdx < nPoints; jdx++ )
1434 if ( invertAxisOrientation )
1442 wkbPtr +=
sizeof( double );
1445 QDomText coordText = doc.createTextNode( coordString );
1446 coordElem.appendChild( coordText );
1447 ringElem.appendChild( coordElem );
1448 boundaryElem.appendChild( ringElem );
1449 polygonElem.appendChild( boundaryElem );
1451 delete [] ringNumPoints;
1460 QDomElement multiPolygonElem = doc.createElement( QStringLiteral(
"gml:MultiPolygon" ) );
1461 if ( gmlVersion ==
GML_3_2_1 && !gmlIdBase.isEmpty() )
1462 multiPolygonElem.setAttribute( QStringLiteral(
"gml:id" ), gmlIdBase );
1463 if ( !srsName.isEmpty() )
1464 multiPolygonElem.setAttribute( QStringLiteral(
"srsName" ), srsName );
1467 wkbPtr >> numPolygons;
1469 for (
int kdx = 0; kdx < numPolygons; kdx++ )
1471 QDomElement polygonMemberElem = doc.createElement( QStringLiteral(
"gml:polygonMember" ) );
1472 QDomElement polygonElem = doc.createElement( QStringLiteral(
"gml:Polygon" ) );
1473 if ( gmlVersion ==
GML_3_2_1 && !gmlIdBase.isEmpty() )
1474 polygonElem.setAttribute( QStringLiteral(
"gml:id" ), gmlIdBase + QStringLiteral(
".%1" ).arg( kdx + 1 ) );
1481 for (
int idx = 0; idx < numRings; idx++ )
1483 QString boundaryName = ( gmlVersion ==
GML_2_1_2 ) ?
"gml:outerBoundaryIs" :
"gml:exterior";
1486 boundaryName = ( gmlVersion ==
GML_2_1_2 ) ?
"gml:innerBoundaryIs" :
"gml:interior";
1488 QDomElement boundaryElem = doc.createElement( boundaryName );
1489 QDomElement ringElem = doc.createElement( QStringLiteral(
"gml:LinearRing" ) );
1494 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1495 QString coordString;
1496 for (
int jdx = 0; jdx < nPoints; jdx++ )
1504 if ( invertAxisOrientation )
1513 wkbPtr +=
sizeof( double );
1516 QDomText coordText = doc.createTextNode( coordString );
1517 coordElem.appendChild( coordText );
1518 ringElem.appendChild( coordElem );
1519 boundaryElem.appendChild( ringElem );
1520 polygonElem.appendChild( boundaryElem );
1521 polygonMemberElem.appendChild( polygonElem );
1522 multiPolygonElem.appendChild( polygonMemberElem );
1525 return multiPolygonElem;
1528 return QDomElement();
1534 return QDomElement();
1543 QDomElement QgsOgcUtils::createGMLCoordinates(
const QgsPolylineXY &points, QDomDocument &doc )
1545 QDomElement coordElem = doc.createElement( QStringLiteral(
"gml:coordinates" ) );
1546 coordElem.setAttribute( QStringLiteral(
"cs" ), QStringLiteral(
"," ) );
1547 coordElem.setAttribute( QStringLiteral(
"ts" ), QStringLiteral(
" " ) );
1549 QString coordString;
1550 QVector<QgsPointXY>::const_iterator pointIt = points.constBegin();
1551 for ( ; pointIt != points.constEnd(); ++pointIt )
1553 if ( pointIt != points.constBegin() )
1562 QDomText coordText = doc.createTextNode( coordString );
1563 coordElem.appendChild( coordText );
1567 QDomElement QgsOgcUtils::createGMLPositions(
const QgsPolylineXY &points, QDomDocument &doc )
1569 QDomElement posElem = doc.createElement( QStringLiteral(
"gml:pos" ) );
1570 if ( points.size() > 1 )
1571 posElem = doc.createElement( QStringLiteral(
"gml:posList" ) );
1572 posElem.setAttribute( QStringLiteral(
"srsDimension" ), QStringLiteral(
"2" ) );
1574 QString coordString;
1575 QVector<QgsPointXY>::const_iterator pointIt = points.constBegin();
1576 for ( ; pointIt != points.constEnd(); ++pointIt )
1578 if ( pointIt != points.constBegin() )
1587 QDomText coordText = doc.createTextNode( coordString );
1588 posElem.appendChild( coordText );
1598 if ( fillElement.isNull() || !fillElement.hasChildNodes() )
1606 QDomElement cssElem = fillElement.firstChildElement( QStringLiteral(
"CssParameter" ) );
1607 while ( !cssElem.isNull() )
1609 cssName = cssElem.attribute( QStringLiteral(
"name" ), QStringLiteral(
"not_found" ) );
1610 if ( cssName != QLatin1String(
"not_found" ) )
1612 elemText = cssElem.text();
1613 if ( cssName == QLatin1String(
"fill" ) )
1615 color.setNamedColor( elemText );
1617 else if ( cssName == QLatin1String(
"fill-opacity" ) )
1620 double opacity = elemText.toDouble( &ok );
1623 color.setAlphaF( opacity );
1628 cssElem = cssElem.nextSiblingElement( QStringLiteral(
"CssParameter" ) );
1642 if ( element.isNull() || !element.hasChildNodes() )
1649 if ( element.firstChild().nodeType() == QDomNode::TextNode )
1659 QDomElement childElem = element.firstChildElement();
1660 while ( !childElem.isNull() )
1672 if ( !expr->d->mRootNode )
1674 expr->d->mRootNode = node;
1681 childElem = childElem.nextSiblingElement();
1685 expr->d->mExp = expr->
dump();
1711 static
int binaryOperatorFromTagName( const QString &tagName )
1714 return BINARY_OPERATORS_TAG_NAMES_MAP()->value( tagName, -1 );
1721 return QStringLiteral(
"PropertyIsLike" );
1723 return BINARY_OPERATORS_TAG_NAMES_MAP()->key( op, QString() );
1726 static bool isBinaryOperator(
const QString &tagName )
1728 return binaryOperatorFromTagName( tagName ) >= 0;
1732 static bool isSpatialOperator(
const QString &tagName )
1734 static QStringList spatialOps;
1735 if ( spatialOps.isEmpty() )
1737 spatialOps << QStringLiteral(
"BBOX" ) << QStringLiteral(
"Intersects" ) << QStringLiteral(
"Contains" ) << QStringLiteral(
"Crosses" ) << QStringLiteral(
"Equals" )
1738 << QStringLiteral(
"Disjoint" ) << QStringLiteral(
"Overlaps" ) << QStringLiteral(
"Touches" ) << QStringLiteral(
"Within" );
1741 return spatialOps.contains( tagName );
1748 errorMessage = utils.errorMessage();
1756 errorMessage = utils.errorMessage();
1764 errorMessage = utils.errorMessage();
1772 errorMessage = utils.errorMessage();
1780 errorMessage = utils.errorMessage();
1788 errorMessage = utils.errorMessage();
1796 errorMessage = utils.errorMessage();
1800 QgsExpressionNode *QgsOgcUtils::nodeIsBetweenFromOgcFilter( QDomElement &element, QString &errorMessage )
1804 errorMessage = utils.errorMessage();
1812 errorMessage = utils.errorMessage();
1823 QStringLiteral(
"geometry" ), QString(),
false,
false, errorMessage );
1829 QStringLiteral(
"geometry" ), QString(),
false,
false, errorMessage );
1837 const QString &srsName,
1838 bool honourAxisOrientation,
1839 bool invertAxisOrientation,
1840 QString *errorMessage )
1843 return QDomElement();
1853 if ( exprRootElem.isNull() )
1854 return QDomElement();
1856 QDomElement filterElem =
1858 doc.createElementNS(
FES_NAMESPACE, QStringLiteral(
"fes:Filter" ) ) :
1859 doc.createElementNS(
OGC_NAMESPACE, QStringLiteral(
"ogc:Filter" ) );
1862 QDomAttr attr = doc.createAttribute( QStringLiteral(
"xmlns:gml" ) );
1867 filterElem.setAttributeNode( attr );
1869 filterElem.appendChild( exprRootElem );
1878 const QString &srsName,
1879 bool honourAxisOrientation,
1880 bool invertAxisOrientation,
1881 QString *errorMessage )
1890 return QDomElement();
1904 if ( !exprRootElem.isNull() )
1906 return exprRootElem;
1913 *errorMessage = QObject::tr(
"Node type not supported in expression translation: %1" ).arg( node->
nodeType() );
1917 return QDomElement();
1924 const QList<LayerProperties> &layerProperties,
1925 bool honourAxisOrientation,
1926 bool invertAxisOrientation,
1927 const QMap< QString, QString> &mapUnprefixedTypenameToPrefixedTypename,
1928 QString *errorMessage )
1931 return QDomElement();
1934 layerProperties, honourAxisOrientation, invertAxisOrientation,
1935 mapUnprefixedTypenameToPrefixedTypename );
1939 if ( exprRootElem.isNull() )
1940 return QDomElement();
1942 QDomElement filterElem =
1944 doc.createElementNS(
FES_NAMESPACE, QStringLiteral(
"fes:Filter" ) ) :
1945 doc.createElementNS(
OGC_NAMESPACE, QStringLiteral(
"ogc:Filter" ) );
1948 QDomAttr attr = doc.createAttribute( QStringLiteral(
"xmlns:gml" ) );
1953 filterElem.setAttributeNode( attr );
1955 filterElem.appendChild( exprRootElem );
1980 mErrorMessage = QObject::tr(
"Node type not supported: %1" ).arg( node->
nodeType() );
1981 return QDomElement();
1988 if ( !mErrorMessage.isEmpty() )
1989 return QDomElement();
1992 switch ( node->
op() )
1995 uoElem = mDoc.createElement( mFilterPrefix +
":Literal" );
2000 uoElem.appendChild( mDoc.createTextNode(
"-" + operandElem.text() ) );
2001 mDoc.removeChild( operandElem );
2005 mErrorMessage = QObject::tr(
"This use of unary operator not implemented yet" );
2006 return QDomElement();
2010 uoElem = mDoc.createElement( mFilterPrefix +
":Not" );
2011 uoElem.appendChild( operandElem );
2015 mErrorMessage = QObject::tr(
"Unary operator '%1' not implemented yet" ).arg( node->
text() );
2016 return QDomElement();
2026 if ( !mErrorMessage.isEmpty() )
2027 return QDomElement();
2037 if ( rightLit->
value().isNull() )
2040 QDomElement elem = mDoc.createElement( mFilterPrefix +
":PropertyIsNull" );
2041 elem.appendChild( leftElem );
2045 QDomElement notElem = mDoc.createElement( mFilterPrefix +
":Not" );
2046 notElem.appendChild( elem );
2060 if ( !mErrorMessage.isEmpty() )
2061 return QDomElement();
2064 QString opText = binaryOperatorToTagName( op );
2065 if ( opText.isEmpty() )
2069 mErrorMessage = QObject::tr(
"Binary operator %1 not implemented yet" ).arg( node->
text() );
2070 return QDomElement();
2073 QDomElement boElem = mDoc.createElement( mFilterPrefix +
":" + opText );
2078 boElem.setAttribute( QStringLiteral(
"matchCase" ), QStringLiteral(
"false" ) );
2081 boElem.setAttribute( QStringLiteral(
"wildCard" ), QStringLiteral(
"%" ) );
2082 boElem.setAttribute( QStringLiteral(
"singleChar" ), QStringLiteral(
"_" ) );
2084 boElem.setAttribute( QStringLiteral(
"escape" ), QStringLiteral(
"\\" ) );
2086 boElem.setAttribute( QStringLiteral(
"escapeChar" ), QStringLiteral(
"\\" ) );
2089 boElem.appendChild( leftElem );
2090 boElem.appendChild( rightElem );
2097 Q_UNUSED( expression )
2100 switch ( node->
value().type() )
2103 value = QString::number( node->
value().toInt() );
2105 case QVariant::Double:
2108 case QVariant::String:
2109 value = node->
value().toString();
2111 case QVariant::Date:
2112 value = node->
value().toDate().toString( Qt::ISODate );
2114 case QVariant::DateTime:
2115 value = node->
value().toDateTime().toString( Qt::ISODate );
2119 mErrorMessage = QObject::tr(
"Literal type not supported: %1" ).arg( node->
value().type() );
2120 return QDomElement();
2123 QDomElement litElem = mDoc.createElement( mFilterPrefix +
":Literal" );
2124 litElem.appendChild( mDoc.createTextNode( value ) );
2131 Q_UNUSED( expression )
2133 QDomElement propElem = mDoc.createElement( mFilterPrefix +
":" + mPropertyName );
2134 propElem.appendChild( mDoc.createTextNode( node->
name() ) );
2142 if ( node->
list()->
list().size() == 1 )
2145 QDomElement orElem = mDoc.createElement( mFilterPrefix +
":Or" );
2148 const auto constList = node->
list()->
list();
2152 if ( !mErrorMessage.isEmpty() )
2153 return QDomElement();
2155 QDomElement eqElem = mDoc.createElement( mFilterPrefix +
":PropertyIsEqualTo" );
2156 eqElem.appendChild( leftNode.cloneNode() );
2157 eqElem.appendChild( listNode );
2159 orElem.appendChild( eqElem );
2164 QDomElement notElem = mDoc.createElement( mFilterPrefix +
":Not" );
2165 notElem.appendChild( orElem );
2174 { QLatin1String(
"disjoint" ), QLatin1String(
"Disjoint" ) },
2175 { QLatin1String(
"intersects" ), QLatin1String(
"Intersects" )},
2176 { QLatin1String(
"touches" ), QLatin1String(
"Touches" ) },
2177 { QLatin1String(
"crosses" ), QLatin1String(
"Crosses" ) },
2178 { QLatin1String(
"contains" ), QLatin1String(
"Contains" ) },
2179 { QLatin1String(
"overlaps" ), QLatin1String(
"Overlaps" ) },
2180 { QLatin1String(
"within" ), QLatin1String(
"Within" ) }
2183 static
bool isBinarySpatialOperator( const QString &fnName )
2185 return BINARY_SPATIAL_OPS_MAP()->contains( fnName );
2188 static QString tagNameForSpatialOperator(
const QString &fnName )
2190 return BINARY_SPATIAL_OPS_MAP()->value( fnName );
2200 return fd->
name() == QLatin1String(
"$geometry" );
2212 if ( fnDef->
name() == QLatin1String(
"geom_from_wkt" ) )
2214 const QList<QgsExpressionNode *> &args = fnNode->
args()->
list();
2230 if ( fd->
name() == QLatin1String(
"intersects_bbox" ) )
2232 QList<QgsExpressionNode *> argNodes = node->
args()->
list();
2233 Q_ASSERT( argNodes.count() == 2 );
2235 QgsGeometry geom = geometryFromConstExpr( argNodes[1] );
2236 if ( !geom.
isNull() && isGeometryColumn( argNodes[0] ) )
2246 QDomElement geomProperty = mDoc.createElement( mFilterPrefix +
":" + mPropertyName );
2247 geomProperty.appendChild( mDoc.createTextNode( mGeometryName ) );
2249 QDomElement funcElem = mDoc.createElement( mFilterPrefix +
":BBOX" );
2250 funcElem.appendChild( geomProperty );
2251 funcElem.appendChild( elemBox );
2256 mErrorMessage = QObject::tr(
"<BBOX> is currently supported only in form: bbox($geometry, geomFromWKT('…'))" );
2257 return QDomElement();
2261 if ( isBinarySpatialOperator( fd->
name() ) )
2263 QList<QgsExpressionNode *> argNodes = node->
args()->
list();
2264 Q_ASSERT( argNodes.count() == 2 );
2267 if ( isGeometryColumn( argNodes[0] ) )
2268 otherNode = argNodes[1];
2269 else if ( isGeometryColumn( argNodes[1] ) )
2270 otherNode = argNodes[0];
2273 mErrorMessage = QObject::tr(
"Unable to translate spatial operator: at least one must refer to geometry." );
2274 return QDomElement();
2277 QDomElement otherGeomElem;
2282 mErrorMessage = QObject::tr(
"spatial operator: the other operator must be a geometry constructor function" );
2283 return QDomElement();
2288 if ( otherFnDef->
name() == QLatin1String(
"geom_from_wkt" ) )
2293 mErrorMessage = QObject::tr(
"geom_from_wkt: argument must be string literal" );
2294 return QDomElement();
2299 QStringLiteral(
"qgis_id_geom_%1" ).arg( mGeomId ) );
2302 else if ( otherFnDef->
name() == QLatin1String(
"geom_from_gml" ) )
2307 mErrorMessage = QObject::tr(
"geom_from_gml: argument must be string literal" );
2308 return QDomElement();
2311 QDomDocument geomDoc;
2313 if ( !geomDoc.setContent( gml,
true ) )
2315 mErrorMessage = QObject::tr(
"geom_from_gml: unable to parse XML" );
2316 return QDomElement();
2319 QDomNode geomNode = mDoc.importNode( geomDoc.documentElement(),
true );
2320 otherGeomElem = geomNode.toElement();
2324 mErrorMessage = QObject::tr(
"spatial operator: unknown geometry constructor function" );
2325 return QDomElement();
2330 QDomElement funcElem = mDoc.createElement( mFilterPrefix +
":" + tagNameForSpatialOperator( fd->
name() ) );
2331 QDomElement geomProperty = mDoc.createElement( mFilterPrefix +
":" + mPropertyName );
2332 geomProperty.appendChild( mDoc.createTextNode( mGeometryName ) );
2333 funcElem.appendChild( geomProperty );
2334 funcElem.appendChild( otherGeomElem );
2338 if ( fd->
isStatic( node, expression, context ) )
2340 QVariant result = fd->
run( node->
args(), context, expression, node );
2342 return expressionLiteralToOgcFilter( &literal, expression, context );
2347 mErrorMessage = QObject::tr(
"Special columns/constants are not supported." );
2348 return QDomElement();
2352 QDomElement funcElem = mDoc.createElement( mFilterPrefix +
":Function" );
2353 funcElem.setAttribute( QStringLiteral(
"name" ), fd->
name() );
2354 const auto constList = node->
args()->
list();
2358 if ( !mErrorMessage.isEmpty() )
2359 return QDomElement();
2361 funcElem.appendChild( childElem );
2372 const QList<QgsOgcUtils::LayerProperties> &layerProperties,
2373 bool honourAxisOrientation,
2374 bool invertAxisOrientation,
2375 const QMap< QString, QString> &mapUnprefixedTypenameToPrefixedTypename )
2378 , mGMLVersion( gmlVersion )
2379 , mFilterVersion( filterVersion )
2380 , mLayerProperties( layerProperties )
2381 , mHonourAxisOrientation( honourAxisOrientation )
2382 , mInvertAxisOrientation( invertAxisOrientation )
2383 , mFilterPrefix( ( filterVersion ==
QgsOgcUtils::FILTER_FES_2_0 ) ?
"fes" :
"ogc" )
2384 , mPropertyName( ( filterVersion ==
QgsOgcUtils::FILTER_FES_2_0 ) ?
"ValueReference" :
"PropertyName" )
2386 , mMapUnprefixedTypenameToPrefixedTypename( mapUnprefixedTypenameToPrefixedTypename )
2412 mErrorMessage = QObject::tr(
"Node type not supported: %1" ).arg( node->
nodeType() );
2413 return QDomElement();
2422 if ( !mErrorMessage.isEmpty() )
2423 return QDomElement();
2426 switch ( node->
op() )
2429 uoElem = mDoc.createElement( mFilterPrefix +
":Literal" );
2434 uoElem.appendChild( mDoc.createTextNode(
"-" + operandElem.text() ) );
2435 mDoc.removeChild( operandElem );
2439 mErrorMessage = QObject::tr(
"This use of unary operator not implemented yet" );
2440 return QDomElement();
2444 uoElem = mDoc.createElement( mFilterPrefix +
":Not" );
2445 uoElem.appendChild( operandElem );
2450 return QDomElement();
2460 if ( !mErrorMessage.isEmpty() )
2461 return QDomElement();
2471 if ( rightLit->
value().isNull() )
2474 QDomElement elem = mDoc.createElement( mFilterPrefix +
":PropertyIsNull" );
2475 elem.appendChild( leftElem );
2479 QDomElement notElem = mDoc.createElement( mFilterPrefix +
":Not" );
2480 notElem.appendChild( elem );
2494 if ( !mErrorMessage.isEmpty() )
2495 return QDomElement();
2500 opText = QStringLiteral(
"Or" );
2502 opText = QStringLiteral(
"And" );
2504 opText = QStringLiteral(
"PropertyIsEqualTo" );
2506 opText = QStringLiteral(
"PropertyIsNotEqualTo" );
2508 opText = QStringLiteral(
"PropertyIsLessThanOrEqualTo" );
2510 opText = QStringLiteral(
"PropertyIsGreaterThanOrEqualTo" );
2512 opText = QStringLiteral(
"PropertyIsLessThan" );
2514 opText = QStringLiteral(
"PropertyIsGreaterThan" );
2516 opText = QStringLiteral(
"PropertyIsLike" );
2518 opText = QStringLiteral(
"PropertyIsLike" );
2520 if ( opText.isEmpty() )
2524 return QDomElement();
2527 QDomElement boElem = mDoc.createElement( mFilterPrefix +
":" + opText );
2532 boElem.setAttribute( QStringLiteral(
"matchCase" ), QStringLiteral(
"false" ) );
2535 boElem.setAttribute( QStringLiteral(
"wildCard" ), QStringLiteral(
"%" ) );
2536 boElem.setAttribute( QStringLiteral(
"singleChar" ), QStringLiteral(
"_" ) );
2538 boElem.setAttribute( QStringLiteral(
"escape" ), QStringLiteral(
"\\" ) );
2540 boElem.setAttribute( QStringLiteral(
"escapeChar" ), QStringLiteral(
"\\" ) );
2543 boElem.appendChild( leftElem );
2544 boElem.appendChild( rightElem );
2552 switch ( node->
value().type() )
2555 value = QString::number( node->
value().toInt() );
2557 case QVariant::LongLong:
2558 value = QString::number( node->
value().toLongLong() );
2560 case QVariant::Double:
2563 case QVariant::String:
2564 value = node->
value().toString();
2568 mErrorMessage = QObject::tr(
"Literal type not supported: %1" ).arg( node->
value().type() );
2569 return QDomElement();
2572 QDomElement litElem = mDoc.createElement( mFilterPrefix +
":Literal" );
2573 litElem.appendChild( mDoc.createTextNode( value ) );
2580 QDomElement propElem = mDoc.createElement( mFilterPrefix +
":" + mPropertyName );
2581 if ( node->
tableName().isEmpty() || mLayerProperties.size() == 1 )
2582 propElem.appendChild( mDoc.createTextNode( node->
name() ) );
2585 QString tableName( mMapTableAliasToNames[node->
tableName()] );
2586 if ( mMapUnprefixedTypenameToPrefixedTypename.contains( tableName ) )
2587 tableName = mMapUnprefixedTypenameToPrefixedTypename[tableName];
2588 propElem.appendChild( mDoc.createTextNode( tableName +
"/" + node->
name() ) );
2595 if ( node->
list()->
list().size() == 1 )
2598 QDomElement orElem = mDoc.createElement( mFilterPrefix +
":Or" );
2601 const auto constList = node->
list()->
list();
2605 if ( !mErrorMessage.isEmpty() )
2606 return QDomElement();
2608 QDomElement eqElem = mDoc.createElement( mFilterPrefix +
":PropertyIsEqualTo" );
2609 eqElem.appendChild( leftNode.cloneNode() );
2610 eqElem.appendChild( listNode );
2612 orElem.appendChild( eqElem );
2617 QDomElement notElem = mDoc.createElement( mFilterPrefix +
":Not" );
2618 notElem.appendChild( orElem );
2627 QDomElement elem = mDoc.createElement( mFilterPrefix +
":PropertyIsBetween" );
2629 QDomElement lowerBoundary = mDoc.createElement( mFilterPrefix +
":LowerBoundary" );
2631 elem.appendChild( lowerBoundary );
2632 QDomElement upperBoundary = mDoc.createElement( mFilterPrefix +
":UpperBoundary" );
2634 elem.appendChild( upperBoundary );
2638 QDomElement notElem = mDoc.createElement( mFilterPrefix +
":Not" );
2639 notElem.appendChild( elem );
2646 static QString mapBinarySpatialToOgc(
const QString &name )
2648 QString nameCompare( name );
2649 if ( name.size() > 3 && name.midRef( 0, 3 ).compare( QLatin1String(
"ST_" ), Qt::CaseInsensitive ) == 0 )
2650 nameCompare = name.mid( 3 );
2651 QStringList spatialOps;
2652 spatialOps << QStringLiteral(
"BBOX" ) << QStringLiteral(
"Intersects" ) << QStringLiteral(
"Contains" ) << QStringLiteral(
"Crosses" ) << QStringLiteral(
"Equals" )
2653 << QStringLiteral(
"Disjoint" ) << QStringLiteral(
"Overlaps" ) << QStringLiteral(
"Touches" ) << QStringLiteral(
"Within" );
2654 const auto constSpatialOps = spatialOps;
2655 for ( QString op : constSpatialOps )
2657 if ( nameCompare.compare( op, Qt::CaseInsensitive ) == 0 )
2663 static QString mapTernarySpatialToOgc(
const QString &name )
2665 QString nameCompare( name );
2666 if ( name.size() > 3 && name.midRef( 0, 3 ).compare( QLatin1String(
"ST_" ), Qt::CaseInsensitive ) == 0 )
2667 nameCompare = name.mid( 3 );
2668 if ( nameCompare.compare( QLatin1String(
"DWithin" ), Qt::CaseInsensitive ) == 0 )
2669 return QStringLiteral(
"DWithin" );
2670 if ( nameCompare.compare( QLatin1String(
"Beyond" ), Qt::CaseInsensitive ) == 0 )
2671 return QStringLiteral(
"Beyond" );
2675 QString QgsOgcUtilsSQLStatementToFilter::getGeometryColumnSRSName(
const QgsSQLStatement::Node *node )
2683 const auto constMLayerProperties = mLayerProperties;
2686 if ( prop.mName.compare( mMapTableAliasToNames[col->
tableName()], Qt::CaseInsensitive ) == 0 &&
2687 prop.mGeometryAttribute.compare( col->
name(), Qt::CaseInsensitive ) == 0 )
2689 return prop.mSRSName;
2693 if ( !mLayerProperties.empty() &&
2694 mLayerProperties.at( 0 ).mGeometryAttribute.compare( col->
name(), Qt::CaseInsensitive ) == 0 )
2696 return mLayerProperties.at( 0 ).mSRSName;
2702 QList<QgsSQLStatement::Node *> args,
2703 bool lastArgIsSRSName,
2705 bool &axisInversion )
2707 srsName = mCurrentSRSName;
2708 axisInversion = mInvertAxisOrientation;
2710 if ( lastArgIsSRSName )
2715 mErrorMessage = QObject::tr(
"%1: Last argument must be string or integer literal" ).arg( mainNode->
name() );
2719 if ( lit->
value().type() == QVariant::Int )
2723 srsName =
"EPSG:" + QString::number( lit->
value().toInt() );
2727 srsName =
"urn:ogc:def:crs:EPSG::" + QString::number( lit->
value().toInt() );
2732 srsName = lit->
value().toString();
2733 if ( srsName.startsWith( QLatin1String(
"EPSG:" ), Qt::CaseInsensitive ) )
2739 if ( !srsName.isEmpty() )
2745 axisInversion = !axisInversion;
2755 if ( node->
name().compare( QLatin1String(
"ST_GeometryFromText" ), Qt::CaseInsensitive ) == 0 )
2757 QList<QgsSQLStatement::Node *> args = node->
args()->
list();
2758 if ( args.size() != 1 && args.size() != 2 )
2760 mErrorMessage = QObject::tr(
"Function %1 should have 1 or 2 arguments" ).arg( node->
name() );
2761 return QDomElement();
2767 mErrorMessage = QObject::tr(
"%1: First argument must be string literal" ).arg( node->
name() );
2768 return QDomElement();
2773 if ( ! processSRSName( node, args, args.size() == 2, srsName, axisInversion ) )
2775 return QDomElement();
2781 QStringLiteral(
"qgis_id_geom_%1" ).arg( mGeomId ) );
2783 if ( geomElem.isNull() )
2785 mErrorMessage = QObject::tr(
"%1: invalid WKT" ).arg( node->
name() );
2786 return QDomElement();
2793 if ( node->
name().compare( QLatin1String(
"ST_MakeEnvelope" ), Qt::CaseInsensitive ) == 0 )
2795 QList<QgsSQLStatement::Node *> args = node->
args()->
list();
2796 if ( args.size() != 4 && args.size() != 5 )
2798 mErrorMessage = QObject::tr(
"Function %1 should have 4 or 5 arguments" ).arg( node->
name() );
2799 return QDomElement();
2804 for (
int i = 0; i < 4; i++ )
2809 mErrorMessage = QObject::tr(
"%1: Argument %2 must be numeric literal" ).arg( node->
name() ).arg( i + 1 );
2810 return QDomElement();
2814 if ( lit->
value().type() == QVariant::Int )
2815 val = lit->
value().toInt();
2816 else if ( lit->
value().type() == QVariant::LongLong )
2817 val = lit->
value().toLongLong();
2818 else if ( lit->
value().type() == QVariant::Double )
2819 val = lit->
value().toDouble();
2822 mErrorMessage = QObject::tr(
"%1 Argument %2 must be numeric literal" ).arg( node->
name() ).arg( i + 1 );
2823 return QDomElement();
2837 if ( ! processSRSName( node, args, args.size() == 5, srsName, axisInversion ) )
2839 return QDomElement();
2846 QgsOgcUtils::rectangleToGMLEnvelope( &rect, mDoc, srsName, axisInversion, 15 );
2850 if ( node->
name().compare( QLatin1String(
"ST_GeomFromGML" ), Qt::CaseInsensitive ) == 0 )
2852 QList<QgsSQLStatement::Node *> args = node->
args()->
list();
2853 if ( args.size() != 1 )
2855 mErrorMessage = QObject::tr(
"Function %1 should have 1 argument" ).arg( node->
name() );
2856 return QDomElement();
2862 mErrorMessage = QObject::tr(
"%1: Argument must be string literal" ).arg( node->
name() );
2863 return QDomElement();
2866 QDomDocument geomDoc;
2868 if ( !geomDoc.setContent( gml,
true ) )
2870 mErrorMessage = QObject::tr(
"ST_GeomFromGML: unable to parse XML" );
2871 return QDomElement();
2874 QDomNode geomNode = mDoc.importNode( geomDoc.documentElement(),
true );
2876 return geomNode.toElement();
2880 QString ogcName( mapBinarySpatialToOgc( node->
name() ) );
2881 if ( !ogcName.isEmpty() )
2883 QList<QgsSQLStatement::Node *> args = node->
args()->
list();
2884 if ( args.size() != 2 )
2886 mErrorMessage = QObject::tr(
"Function %1 should have 2 arguments" ).arg( node->
name() );
2887 return QDomElement();
2890 for (
int i = 0; i < 2; i ++ )
2893 (
static_cast<const QgsSQLStatement::NodeFunction *
>( args[i] )->name().compare( QLatin1String(
"ST_GeometryFromText" ), Qt::CaseInsensitive ) == 0 ||
2894 static_cast<const QgsSQLStatement::NodeFunction *
>( args[i] )->name().compare( QLatin1String(
"ST_MakeEnvelope" ), Qt::CaseInsensitive ) == 0 ) )
2896 mCurrentSRSName = getGeometryColumnSRSName( args[1 - i] );
2903 QDomElement funcElem = mDoc.createElement( mFilterPrefix +
":" + ogcName );
2904 const auto constArgs = args;
2908 if ( !mErrorMessage.isEmpty() )
2910 mCurrentSRSName.clear();
2911 return QDomElement();
2914 funcElem.appendChild( childElem );
2917 mCurrentSRSName.clear();
2921 ogcName = mapTernarySpatialToOgc( node->
name() );
2922 if ( !ogcName.isEmpty() )
2924 QList<QgsSQLStatement::Node *> args = node->
args()->
list();
2925 if ( args.size() != 3 )
2927 mErrorMessage = QObject::tr(
"Function %1 should have 3 arguments" ).arg( node->
name() );
2928 return QDomElement();
2931 for (
int i = 0; i < 2; i ++ )
2934 (
static_cast<const QgsSQLStatement::NodeFunction *
>( args[i] )->name().compare( QLatin1String(
"ST_GeometryFromText" ), Qt::CaseInsensitive ) == 0 ||
2935 static_cast<const QgsSQLStatement::NodeFunction *
>( args[i] )->name().compare( QLatin1String(
"ST_MakeEnvelope" ), Qt::CaseInsensitive ) == 0 ) )
2937 mCurrentSRSName = getGeometryColumnSRSName( args[1 - i] );
2942 QDomElement funcElem = mDoc.createElement( mFilterPrefix +
":" + node->
name().mid( 3 ) );
2943 for (
int i = 0; i < 2; i++ )
2946 if ( !mErrorMessage.isEmpty() )
2948 mCurrentSRSName.clear();
2949 return QDomElement();
2952 funcElem.appendChild( childElem );
2954 mCurrentSRSName.clear();
2959 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() );
2960 return QDomElement();
2963 if ( lit->
value().isNull() )
2965 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() );
2966 return QDomElement();
2969 QString unit( QStringLiteral(
"m" ) );
2970 switch ( lit->
value().type() )
2973 distance = QString::number( lit->
value().toInt() );
2975 case QVariant::LongLong:
2976 distance = QString::number( lit->
value().toLongLong() );
2978 case QVariant::Double:
2981 case QVariant::String:
2983 distance = lit->
value().toString();
2984 for (
int i = 0; i < distance.size(); i++ )
2986 if ( !( ( distance[i] >=
'0' && distance[i] <=
'9' ) || distance[i] ==
'-' || distance[i] ==
'.' || distance[i] ==
'e' || distance[i] ==
'E' ) )
2988 unit = distance.mid( i ).trimmed();
2989 distance = distance.mid( 0, i );
2997 mErrorMessage = QObject::tr(
"Literal type not supported: %1" ).arg( lit->
value().type() );
2998 return QDomElement();
3001 QDomElement distanceElem = mDoc.createElement( mFilterPrefix +
":Distance" );
3003 distanceElem.setAttribute( QStringLiteral(
"uom" ), unit );
3005 distanceElem.setAttribute( QStringLiteral(
"unit" ), unit );
3006 distanceElem.appendChild( mDoc.createTextNode( distance ) );
3007 funcElem.appendChild( distanceElem );
3012 QDomElement funcElem = mDoc.createElement( mFilterPrefix +
":Function" );
3013 funcElem.setAttribute( QStringLiteral(
"name" ), node->
name() );
3014 const auto constList = node->
args()->
list();
3018 if ( !mErrorMessage.isEmpty() )
3019 return QDomElement();
3021 funcElem.appendChild( childElem );
3027 const QString &leftTable )
3035 QList<QDomElement> listElem;
3037 for (
const QString &columnName : constUsingColumns )
3039 QDomElement eqElem = mDoc.createElement( mFilterPrefix +
":PropertyIsEqualTo" );
3040 QDomElement propElem1 = mDoc.createElement( mFilterPrefix +
":" + mPropertyName );
3041 propElem1.appendChild( mDoc.createTextNode( leftTable +
"/" + columnName ) );
3042 eqElem.appendChild( propElem1 );
3043 QDomElement propElem2 = mDoc.createElement( mFilterPrefix +
":" + mPropertyName );
3044 propElem2.appendChild( mDoc.createTextNode( node->
tableDef()->
name() +
"/" + columnName ) );
3045 eqElem.appendChild( propElem2 );
3046 listElem.append( eqElem );
3049 if ( listElem.size() == 1 )
3053 else if ( listElem.size() > 1 )
3055 QDomElement andElem = mDoc.createElement( mFilterPrefix +
":And" );
3056 const auto constListElem = listElem;
3057 for (
const QDomElement &elem : constListElem )
3059 andElem.appendChild( elem );
3064 return QDomElement();
3069 if ( node->
alias().isEmpty() )
3071 mMapTableAliasToNames[ node->
name()] = node->
name();
3075 mMapTableAliasToNames[ node->
alias()] = node->
name();
3081 QList<QDomElement> listElem;
3084 ( node->
tables().size() != 1 || !node->
joins().empty() ) )
3086 mErrorMessage = QObject::tr(
"Joins are only supported with WFS 2.0" );
3087 return QDomElement();
3091 const auto constTables = node->
tables();
3096 const auto constJoins = node->
joins();
3099 visit( join->tableDef() );
3103 QList< QgsSQLStatement::NodeTableDef *> nodeTables = node->
tables();
3104 QString leftTable = nodeTables.at( nodeTables.length() - 1 )->name();
3107 QDomElement joinElem =
toOgcFilter( join, leftTable );
3108 if ( !mErrorMessage.isEmpty() )
3109 return QDomElement();
3110 listElem.append( joinElem );
3111 leftTable = join->tableDef()->name();
3115 if ( node->
where() )
3118 if ( !mErrorMessage.isEmpty() )
3119 return QDomElement();
3120 listElem.append( whereElem );
3124 if ( listElem.size() == 1 )
3128 else if ( listElem.size() > 1 )
3130 QDomElement andElem = mDoc.createElement( mFilterPrefix +
":And" );
3131 const auto constListElem = listElem;
3132 for (
const QDomElement &elem : constListElem )
3134 andElem.appendChild( elem );
3139 return QDomElement();
3145 mPropertyName = QStringLiteral(
"PropertyName" );
3146 mPrefix = QStringLiteral(
"ogc" );
3150 mPropertyName = QStringLiteral(
"ValueReference" );
3151 mPrefix = QStringLiteral(
"fes" );
3157 if ( element.isNull() )
3161 if ( isBinaryOperator( element.tagName() ) )
3167 if ( isSpatialOperator( element.tagName() ) )
3173 if ( element.tagName() == QLatin1String(
"Not" ) )
3177 else if ( element.tagName() == QLatin1String(
"PropertyIsNull" ) )
3181 else if ( element.tagName() == QLatin1String(
"Literal" ) )
3185 else if ( element.tagName() == QLatin1String(
"Function" ) )
3189 else if ( element.tagName() == mPropertyName )
3193 else if ( element.tagName() == QLatin1String(
"PropertyIsBetween" ) )
3198 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() );
3204 if ( element.isNull() )
3207 int op = binaryOperatorFromTagName( element.tagName() );
3210 mErrorMessage = QObject::tr(
"'%1' binary operator not supported." ).arg( element.tagName() );
3214 if ( op ==
QgsExpressionNodeBinaryOperator::boLike && element.hasAttribute( QStringLiteral(
"matchCase" ) ) && element.attribute( QStringLiteral(
"matchCase" ) ) == QLatin1String(
"false" ) )
3219 QDomElement operandElem = element.firstChildElement();
3224 mErrorMessage = QObject::tr(
"invalid left operand for '%1' binary operator" ).arg( element.tagName() );
3228 std::unique_ptr<QgsExpressionNode> leftOp( expr->clone() );
3229 for ( operandElem = operandElem.nextSiblingElement(); !operandElem.isNull(); operandElem = operandElem.nextSiblingElement() )
3234 mErrorMessage = QObject::tr(
"invalid right operand for '%1' binary operator" ).arg( element.tagName() );
3241 if ( element.hasAttribute( QStringLiteral(
"wildCard" ) ) )
3243 wildCard = element.attribute( QStringLiteral(
"wildCard" ) );
3246 if ( element.hasAttribute( QStringLiteral(
"singleChar" ) ) )
3248 singleChar = element.attribute( QStringLiteral(
"singleChar" ) );
3250 QString escape = QStringLiteral(
"\\" );
3251 if ( element.hasAttribute( QStringLiteral(
"escape" ) ) )
3253 escape = element.attribute( QStringLiteral(
"escape" ) );
3255 if ( element.hasAttribute( QStringLiteral(
"escapeChar" ) ) )
3257 escape = element.attribute( QStringLiteral(
"escapeChar" ) );
3261 if ( !wildCard.isEmpty() && wildCard != QLatin1String(
"%" ) )
3263 oprValue.replace(
'%', QLatin1String(
"\\%" ) );
3264 if ( oprValue.startsWith( wildCard ) )
3266 oprValue.replace( 0, 1, QStringLiteral(
"%" ) );
3268 QRegExp rx(
"[^" + QRegExp::escape( escape ) +
"](" + QRegExp::escape( wildCard ) +
")" );
3270 while ( ( pos = rx.indexIn( oprValue, pos ) ) != -1 )
3272 oprValue.replace( pos + 1, 1, QStringLiteral(
"%" ) );
3275 oprValue.replace( escape + wildCard, wildCard );
3277 if ( !singleChar.isEmpty() && singleChar != QLatin1String(
"_" ) )
3279 oprValue.replace(
'_', QLatin1String(
"\\_" ) );
3280 if ( oprValue.startsWith( singleChar ) )
3282 oprValue.replace( 0, 1, QStringLiteral(
"_" ) );
3284 QRegExp rx(
"[^" + QRegExp::escape( escape ) +
"](" + QRegExp::escape( singleChar ) +
")" );
3286 while ( ( pos = rx.indexIn( oprValue, pos ) ) != -1 )
3288 oprValue.replace( pos + 1, 1, QStringLiteral(
"_" ) );
3291 oprValue.replace( escape + singleChar, singleChar );
3293 if ( !escape.isEmpty() && escape != QLatin1String(
"\\" ) )
3295 oprValue.replace( escape + escape, escape );
3303 if ( expr == leftOp )
3305 mErrorMessage = QObject::tr(
"only one operand for '%1' binary operator" ).arg( element.tagName() );
3319 QDomElement childElem = element.firstChildElement();
3321 while ( !childElem.isNull() && gml2Str.isEmpty() )
3323 if ( childElem.tagName() != mPropertyName )
3325 QTextStream gml2Stream( &gml2Str );
3326 childElem.save( gml2Stream, 0 );
3328 childElem = childElem.nextSiblingElement();
3330 if ( !gml2Str.isEmpty() )
3336 mErrorMessage = QObject::tr(
"No OGC Geometry found" );
3349 if ( element.isNull() || element.tagName() != mPropertyName )
3351 mErrorMessage = QObject::tr(
"%1:PropertyName expected, got %2" ).arg( mPrefix, element.tagName() );
3360 if ( element.isNull() || element.tagName() != QLatin1String(
"Literal" ) )
3362 mErrorMessage = QObject::tr(
"%1:Literal expected, got %2" ).arg( mPrefix, element.tagName() );
3366 std::unique_ptr<QgsExpressionNode> root;
3369 QDomNode childNode = element.firstChild();
3370 while ( !childNode.isNull() )
3372 std::unique_ptr<QgsExpressionNode> operand;
3374 if ( childNode.nodeType() == QDomNode::ElementNode )
3377 const QDomElement operandElem = childNode.toElement();
3381 mErrorMessage = QObject::tr(
"'%1' is an invalid or not supported content for %2:Literal" ).arg( operandElem.tagName(), mPrefix );
3388 QVariant value = childNode.nodeValue();
3390 bool converted =
false;
3395 QDomElement propertyNameElement = element.previousSiblingElement( mPropertyName );
3396 if ( propertyNameElement.isNull() || propertyNameElement.tagName() != mPropertyName )
3398 propertyNameElement = element.nextSiblingElement( mPropertyName );
3400 if ( !propertyNameElement.isNull() || propertyNameElement.tagName() == mPropertyName )
3402 const int fieldIndex = mLayer->
fields().
indexOf( propertyNameElement.firstChild().nodeValue() );
3403 if ( fieldIndex != -1 )
3416 const double d = value.toDouble( &ok );
3429 root = std::move( operand );
3436 childNode = childNode.nextSibling();
3440 return root.release();
3447 if ( element.tagName() != QLatin1String(
"Not" ) )
3450 const QDomElement operandElem = element.firstChildElement();
3454 mErrorMessage = QObject::tr(
"invalid operand for '%1' unary operator" ).arg( element.tagName() );
3464 if ( element.tagName() != QLatin1String(
"PropertyIsNull" ) )
3469 const QDomElement operandElem = element.firstChildElement();
3480 if ( element.isNull() || element.tagName() != QLatin1String(
"Function" ) )
3482 mErrorMessage = QObject::tr(
"%1:Function expected, got %2" ).arg( mPrefix, element.tagName() );
3490 if ( element.attribute( QStringLiteral(
"name" ) ) != funcDef->
name() )
3495 QDomElement operandElem = element.firstChildElement();
3496 while ( !operandElem.isNull() )
3503 args->append( op.release() );
3505 operandElem = operandElem.nextSiblingElement();
3517 std::unique_ptr<QgsExpressionNode> operand;
3518 std::unique_ptr<QgsExpressionNode> lowerBound;
3519 std::unique_ptr<QgsExpressionNode> upperBound;
3521 QDomElement operandElem = element.firstChildElement();
3522 while ( !operandElem.isNull() )
3524 if ( operandElem.tagName() == QLatin1String(
"LowerBoundary" ) )
3526 QDomElement lowerBoundElem = operandElem.firstChildElement();
3529 else if ( operandElem.tagName() == QLatin1String(
"UpperBoundary" ) )
3531 QDomElement upperBoundElem = operandElem.firstChildElement();
3540 if ( operand && lowerBound && upperBound )
3543 operandElem = operandElem.nextSiblingElement();
3546 if ( !operand || !lowerBound || !upperBound )
3548 mErrorMessage = QObject::tr(
"missing some required sub-elements in %1:PropertyIsBetween" ).arg( mPrefix );
3559 return mErrorMessage;