11 #include <netinet/in.h>
22 QDomElement geometryTypeElement = geometryNode.toElement();
23 QString geomType = geometryTypeElement.tagName();
25 if ( !( geomType ==
"Point" || geomType ==
"LineString" || geomType ==
"Polygon" ||
26 geomType ==
"MultiPoint" || geomType ==
"MultiLineString" || geomType ==
"MultiPolygon" ||
27 geomType ==
"Box" || geomType ==
"Envelope" ) )
29 QDomNode geometryChild = geometryNode.firstChild();
30 if ( geometryChild.isNull() )
34 geometryTypeElement = geometryChild.toElement();
35 geomType = geometryTypeElement.tagName();
38 if ( !( geomType ==
"Point" || geomType ==
"LineString" || geomType ==
"Polygon" ||
39 geomType ==
"MultiPoint" || geomType ==
"MultiLineString" || geomType ==
"MultiPolygon" ||
40 geomType ==
"Box" || geomType ==
"Envelope" ) )
43 if ( geomType ==
"Point" )
47 else if ( geomType ==
"LineString" )
51 else if ( geomType ==
"Polygon" )
55 else if ( geomType ==
"MultiPoint" )
59 else if ( geomType ==
"MultiLineString" )
63 else if ( geomType ==
"MultiPolygon" )
67 else if ( geomType ==
"Box" )
71 else if ( geomType ==
"Envelope" )
84 QString xml = QString(
"<tmp xmlns=\"%1\" xmlns:gml=\"%1\">%2</tmp>" ).arg(
GML_NAMESPACE ).arg( xmlString );
86 if ( !doc.setContent( xml,
true ) )
97 QDomNodeList coordList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE,
"coordinates" );
98 if ( coordList.size() > 0 )
100 QDomElement coordElement = coordList.at( 0 ).toElement();
108 QDomNodeList posList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE,
"pos" );
109 if ( posList.size() < 1 )
113 QDomElement posElement = posList.at( 0 ).toElement();
120 if ( pointCoordinate.size() < 1 )
125 QgsPolyline::const_iterator point_it = pointCoordinate.begin();
126 char e = htonl( 1 ) != 1 ;
127 double x = point_it->x();
128 double y = point_it->y();
129 int size = 1 +
sizeof( int ) + 2 *
sizeof(
double );
132 unsigned char* wkb =
new unsigned char[
size];
135 memcpy( &( wkb )[wkbPosition], &e, 1 );
137 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
138 wkbPosition +=
sizeof( int );
139 memcpy( &( wkb )[wkbPosition], &x,
sizeof(
double ) );
140 wkbPosition +=
sizeof( double );
141 memcpy( &( wkb )[wkbPosition], &y,
sizeof(
double ) );
152 QDomNodeList coordList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE,
"coordinates" );
153 if ( coordList.size() > 0 )
155 QDomElement coordElement = coordList.at( 0 ).toElement();
163 QDomNodeList posList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE,
"posList" );
164 if ( posList.size() < 1 )
168 QDomElement posElement = posList.at( 0 ).toElement();
175 char e = htonl( 1 ) != 1 ;
176 int size = 1 + 2 *
sizeof( int ) + lineCoordinates.size() * 2 *
sizeof( double );
179 unsigned char* wkb =
new unsigned char[
size];
183 int nPoints = lineCoordinates.size();
186 memcpy( &( wkb )[wkbPosition], &e, 1 );
188 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
189 wkbPosition +=
sizeof( int );
190 memcpy( &( wkb )[wkbPosition], &nPoints,
sizeof(
int ) );
191 wkbPosition +=
sizeof( int );
193 QgsPolyline::const_iterator iter;
194 for ( iter = lineCoordinates.begin(); iter != lineCoordinates.end(); ++iter )
198 memcpy( &( wkb )[wkbPosition], &x,
sizeof(
double ) );
199 wkbPosition +=
sizeof( double );
200 memcpy( &( wkb )[wkbPosition], &y,
sizeof(
double ) );
201 wkbPosition +=
sizeof( double );
216 QDomNodeList outerBoundaryList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE,
"outerBoundaryIs" );
217 if ( outerBoundaryList.size() > 0 )
219 QDomElement coordinatesElement = outerBoundaryList.at( 0 ).firstChild().firstChild().toElement();
220 if ( coordinatesElement.isNull() )
228 ringCoordinates.push_back( exteriorPointList );
231 QDomNodeList innerBoundaryList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE,
"innerBoundaryIs" );
232 for (
int i = 0; i < innerBoundaryList.size(); ++i )
235 coordinatesElement = innerBoundaryList.at( i ).firstChild().firstChild().toElement();
236 if ( coordinatesElement.isNull() )
244 ringCoordinates.push_back( interiorPointList );
250 QDomNodeList exteriorList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE,
"exterior" );
251 if ( exteriorList.size() < 1 )
255 QDomElement posElement = exteriorList.at( 0 ).firstChild().firstChild().toElement();
256 if ( posElement.isNull() )
264 ringCoordinates.push_back( exteriorPointList );
267 QDomNodeList interiorList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE,
"interior" );
268 for (
int i = 0; i < interiorList.size(); ++i )
271 QDomElement posElement = interiorList.at( i ).firstChild().firstChild().toElement();
272 if ( posElement.isNull() )
280 ringCoordinates.push_back( interiorPointList );
285 int nrings = ringCoordinates.size();
290 for ( QgsMultiPolyline::const_iterator it = ringCoordinates.begin(); it != ringCoordinates.end(); ++it )
292 npoints += it->size();
294 int size = 1 + 2 *
sizeof( int ) + nrings *
sizeof(
int ) + 2 * npoints *
sizeof( double );
297 unsigned char* wkb =
new unsigned char[
size];
300 char e = htonl( 1 ) != 1 ;
302 int nPointsInRing = 0;
306 memcpy( &( wkb )[wkbPosition], &e, 1 );
308 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
309 wkbPosition +=
sizeof( int );
310 memcpy( &( wkb )[wkbPosition], &nrings,
sizeof(
int ) );
311 wkbPosition +=
sizeof( int );
312 for ( QgsMultiPolyline::const_iterator it = ringCoordinates.begin(); it != ringCoordinates.end(); ++it )
314 nPointsInRing = it->size();
315 memcpy( &( wkb )[wkbPosition], &nPointsInRing,
sizeof(
int ) );
316 wkbPosition +=
sizeof( int );
318 QgsPolyline::const_iterator iter;
319 for ( iter = it->begin(); iter != it->end(); ++iter )
324 memcpy( &( wkb )[wkbPosition], &x,
sizeof(
double ) );
325 wkbPosition +=
sizeof( double );
326 memcpy( &( wkb )[wkbPosition], &y,
sizeof(
double ) );
327 wkbPosition +=
sizeof( double );
340 QDomNodeList pointMemberList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE,
"pointMember" );
341 if ( pointMemberList.size() < 1 )
345 QDomNodeList pointNodeList;
347 QDomNodeList coordinatesList;
348 QDomNodeList posList;
349 for (
int i = 0; i < pointMemberList.size(); ++i )
352 pointNodeList = pointMemberList.at( i ).toElement().elementsByTagNameNS(
GML_NAMESPACE,
"Point" );
353 if ( pointNodeList.size() < 1 )
358 coordinatesList = pointNodeList.at( 0 ).toElement().elementsByTagNameNS(
GML_NAMESPACE,
"coordinates" );
359 if ( coordinatesList.size() > 0 )
361 currentPoint.clear();
366 if ( currentPoint.size() < 1 )
370 pointList.push_back(( *currentPoint.begin() ) );
376 posList = pointNodeList.at( 0 ).toElement().elementsByTagNameNS(
GML_NAMESPACE,
"pos" );
377 if ( posList.size() < 1 )
381 currentPoint.clear();
386 if ( currentPoint.size() < 1 )
390 pointList.push_back(( *currentPoint.begin() ) );
394 int nPoints = pointList.size();
399 int size = 1 + 2 *
sizeof( int ) + pointList.size() * ( 2 *
sizeof( double ) + 1 +
sizeof(
int ) );
402 unsigned char* wkb =
new unsigned char[
size];
405 char e = htonl( 1 ) != 1 ;
408 memcpy( &( wkb )[wkbPosition], &e, 1 );
410 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
411 wkbPosition +=
sizeof( int );
412 memcpy( &( wkb )[wkbPosition], &nPoints,
sizeof(
int ) );
413 wkbPosition +=
sizeof( int );
415 for ( QgsPolyline::const_iterator it = pointList.begin(); it != pointList.end(); ++it )
417 memcpy( &( wkb )[wkbPosition], &e, 1 );
419 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
420 wkbPosition +=
sizeof( int );
422 memcpy( &( wkb )[wkbPosition], &x,
sizeof(
double ) );
423 wkbPosition +=
sizeof( double );
425 memcpy( &( wkb )[wkbPosition], &y,
sizeof(
double ) );
426 wkbPosition +=
sizeof( double );
445 QList< QgsPolyline > lineCoordinates;
446 QDomElement currentLineStringElement;
447 QDomNodeList currentCoordList;
448 QDomNodeList currentPosList;
450 QDomNodeList lineStringMemberList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE,
"lineStringMember" );
451 if ( lineStringMemberList.size() > 0 )
453 for (
int i = 0; i < lineStringMemberList.size(); ++i )
455 QDomNodeList lineStringNodeList = lineStringMemberList.at( i ).toElement().elementsByTagNameNS(
GML_NAMESPACE,
"LineString" );
456 if ( lineStringNodeList.size() < 1 )
460 currentLineStringElement = lineStringNodeList.at( 0 ).toElement();
461 currentCoordList = currentLineStringElement.elementsByTagNameNS(
GML_NAMESPACE,
"coordinates" );
462 if ( currentCoordList.size() > 0 )
465 if (
readGMLCoordinates( currentPointList, currentCoordList.at( 0 ).toElement() ) != 0 )
469 lineCoordinates.push_back( currentPointList );
473 currentPosList = currentLineStringElement.elementsByTagNameNS(
GML_NAMESPACE,
"posList" );
474 if ( currentPosList.size() < 1 )
479 if (
readGMLPositions( currentPointList, currentPosList.at( 0 ).toElement() ) != 0 )
483 lineCoordinates.push_back( currentPointList );
489 QDomNodeList lineStringList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE,
"LineString" );
490 if ( lineStringList.size() > 0 )
492 for (
int i = 0; i < lineStringList.size(); ++i )
494 currentLineStringElement = lineStringList.at( i ).toElement();
495 currentCoordList = currentLineStringElement.elementsByTagNameNS(
GML_NAMESPACE,
"coordinates" );
496 if ( currentCoordList.size() > 0 )
499 if (
readGMLCoordinates( currentPointList, currentCoordList.at( 0 ).toElement() ) != 0 )
503 lineCoordinates.push_back( currentPointList );
508 currentPosList = currentLineStringElement.elementsByTagNameNS(
GML_NAMESPACE,
"posList" );
509 if ( currentPosList.size() < 1 )
514 if (
readGMLPositions( currentPointList, currentPosList.at( 0 ).toElement() ) != 0 )
518 lineCoordinates.push_back( currentPointList );
528 int nLines = lineCoordinates.size();
533 int size = ( lineCoordinates.size() + 1 ) * ( 1 + 2 *
sizeof(
int ) );
534 for ( QList< QgsPolyline >::const_iterator it = lineCoordinates.begin(); it != lineCoordinates.end(); ++it )
536 size += it->size() * 2 *
sizeof( double );
540 unsigned char* wkb =
new unsigned char[
size];
543 char e = htonl( 1 ) != 1 ;
547 memcpy( &( wkb )[wkbPosition], &e, 1 );
549 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
550 wkbPosition +=
sizeof( int );
551 memcpy( &( wkb )[wkbPosition], &nLines,
sizeof(
int ) );
552 wkbPosition +=
sizeof( int );
554 for ( QList< QgsPolyline >::const_iterator it = lineCoordinates.begin(); it != lineCoordinates.end(); ++it )
556 memcpy( &( wkb )[wkbPosition], &e, 1 );
558 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
559 wkbPosition +=
sizeof( int );
560 nPoints = it->size();
561 memcpy( &( wkb )[wkbPosition], &nPoints,
sizeof(
int ) );
562 wkbPosition +=
sizeof( int );
563 for ( QgsPolyline::const_iterator iter = it->begin(); iter != it->end(); ++iter )
568 memcpy( &( wkb )[wkbPosition], &x,
sizeof(
double ) );
569 wkbPosition +=
sizeof( double );
570 memcpy( &( wkb )[wkbPosition], &y,
sizeof(
double ) );
571 wkbPosition +=
sizeof( double );
584 QDomElement currentPolygonMemberElement;
585 QDomNodeList polygonList;
586 QDomElement currentPolygonElement;
588 QDomNodeList outerBoundaryList;
589 QDomElement currentOuterBoundaryElement;
590 QDomNodeList innerBoundaryList;
591 QDomElement currentInnerBoundaryElement;
593 QDomNodeList exteriorList;
594 QDomElement currentExteriorElement;
595 QDomElement currentInteriorElement;
596 QDomNodeList interiorList;
598 QDomNodeList linearRingNodeList;
599 QDomElement currentLinearRingElement;
601 QDomNodeList currentCoordinateList;
602 QDomNodeList currentPosList;
604 QDomNodeList polygonMemberList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE,
"polygonMember" );
605 for (
int i = 0; i < polygonMemberList.size(); ++i )
608 currentPolygonMemberElement = polygonMemberList.at( i ).toElement();
609 polygonList = currentPolygonMemberElement.elementsByTagNameNS(
GML_NAMESPACE,
"Polygon" );
610 if ( polygonList.size() < 1 )
614 currentPolygonElement = polygonList.at( 0 ).toElement();
617 outerBoundaryList = currentPolygonElement.elementsByTagNameNS(
GML_NAMESPACE,
"outerBoundaryIs" );
618 if ( outerBoundaryList.size() > 0 )
620 currentOuterBoundaryElement = outerBoundaryList.at( 0 ).toElement();
623 linearRingNodeList = currentOuterBoundaryElement.elementsByTagNameNS(
GML_NAMESPACE,
"LinearRing" );
624 if ( linearRingNodeList.size() < 1 )
628 currentLinearRingElement = linearRingNodeList.at( 0 ).toElement();
629 currentCoordinateList = currentLinearRingElement.elementsByTagNameNS(
GML_NAMESPACE,
"coordinates" );
630 if ( currentCoordinateList.size() < 1 )
634 if (
readGMLCoordinates( ringCoordinates, currentCoordinateList.at( 0 ).toElement() ) != 0 )
638 currentPolygonList.push_back( ringCoordinates );
641 QDomNodeList innerBoundaryList = currentPolygonElement.elementsByTagNameNS(
GML_NAMESPACE,
"innerBoundaryIs" );
642 for (
int j = 0; j < innerBoundaryList.size(); ++j )
645 currentInnerBoundaryElement = innerBoundaryList.at( j ).toElement();
646 linearRingNodeList = currentInnerBoundaryElement.elementsByTagNameNS(
GML_NAMESPACE,
"LinearRing" );
647 if ( linearRingNodeList.size() < 1 )
651 currentLinearRingElement = linearRingNodeList.at( 0 ).toElement();
652 currentCoordinateList = currentLinearRingElement.elementsByTagNameNS(
GML_NAMESPACE,
"coordinates" );
653 if ( currentCoordinateList.size() < 1 )
657 if (
readGMLCoordinates( ringCoordinates, currentCoordinateList.at( 0 ).toElement() ) != 0 )
661 currentPolygonList.push_back( ringCoordinates );
667 exteriorList = currentPolygonElement.elementsByTagNameNS(
GML_NAMESPACE,
"exterior" );
668 if ( exteriorList.size() < 1 )
673 currentExteriorElement = exteriorList.at( 0 ).toElement();
676 linearRingNodeList = currentExteriorElement.elementsByTagNameNS(
GML_NAMESPACE,
"LinearRing" );
677 if ( linearRingNodeList.size() < 1 )
681 currentLinearRingElement = linearRingNodeList.at( 0 ).toElement();
682 currentPosList = currentLinearRingElement.elementsByTagNameNS(
GML_NAMESPACE,
"posList" );
683 if ( currentPosList.size() < 1 )
687 if (
readGMLPositions( ringPositions, currentPosList.at( 0 ).toElement() ) != 0 )
691 currentPolygonList.push_back( ringPositions );
694 QDomNodeList interiorList = currentPolygonElement.elementsByTagNameNS(
GML_NAMESPACE,
"interior" );
695 for (
int j = 0; j < interiorList.size(); ++j )
698 currentInteriorElement = interiorList.at( j ).toElement();
699 linearRingNodeList = currentInteriorElement.elementsByTagNameNS(
GML_NAMESPACE,
"LinearRing" );
700 if ( linearRingNodeList.size() < 1 )
704 currentLinearRingElement = linearRingNodeList.at( 0 ).toElement();
705 currentPosList = currentLinearRingElement.elementsByTagNameNS(
GML_NAMESPACE,
"posList" );
706 if ( currentPosList.size() < 1 )
710 if (
readGMLPositions( ringPositions, currentPosList.at( 0 ).toElement() ) != 0 )
714 currentPolygonList.push_back( ringPositions );
717 multiPolygonPoints.push_back( currentPolygonList );
720 int nPolygons = multiPolygonPoints.size();
724 int size = 1 + 2 *
sizeof( int );
726 for ( QgsMultiPolygon::const_iterator it = multiPolygonPoints.begin(); it != multiPolygonPoints.end(); ++it )
728 size += 1 + 2 *
sizeof( int );
729 for ( QgsPolygon::const_iterator iter = it->begin(); iter != it->end(); ++iter )
731 size +=
sizeof( int ) + 2 * iter->size() *
sizeof( double );
736 unsigned char* wkb =
new unsigned char[
size];
738 char e = htonl( 1 ) != 1 ;
745 memcpy( &( wkb )[wkbPosition], &e, 1 );
747 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
748 wkbPosition +=
sizeof( int );
749 memcpy( &( wkb )[wkbPosition], &nPolygons,
sizeof(
int ) );
750 wkbPosition +=
sizeof( int );
754 for ( QgsMultiPolygon::const_iterator it = multiPolygonPoints.begin(); it != multiPolygonPoints.end(); ++it )
756 memcpy( &( wkb )[wkbPosition], &e, 1 );
758 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
759 wkbPosition +=
sizeof( int );
761 memcpy( &( wkb )[wkbPosition], &nRings,
sizeof(
int ) );
762 wkbPosition +=
sizeof( int );
763 for ( QgsPolygon::const_iterator iter = it->begin(); iter != it->end(); ++iter )
765 nPointsInRing = iter->size();
766 memcpy( &( wkb )[wkbPosition], &nPointsInRing,
sizeof(
int ) );
767 wkbPosition +=
sizeof( int );
768 for ( QgsPolyline::const_iterator iterator = iter->begin(); iterator != iter->end(); ++iterator )
772 memcpy( &( wkb )[wkbPosition], &x,
sizeof(
double ) );
773 wkbPosition +=
sizeof( double );
774 memcpy( &( wkb )[wkbPosition], &y,
sizeof(
double ) );
775 wkbPosition +=
sizeof( double );
787 QString coordSeparator =
",";
788 QString tupelSeparator =
" ";
793 if ( elem.hasAttribute(
"cs" ) )
795 coordSeparator = elem.attribute(
"cs" );
797 if ( elem.hasAttribute(
"ts" ) )
799 tupelSeparator = elem.attribute(
"ts" );
802 QStringList tupels = elem.text().split( tupelSeparator, QString::SkipEmptyParts );
803 QStringList tupel_coords;
805 bool conversionSuccess;
807 QStringList::const_iterator it;
808 for ( it = tupels.constBegin(); it != tupels.constEnd(); ++it )
810 tupel_coords = ( *it ).split( coordSeparator, QString::SkipEmptyParts );
811 if ( tupel_coords.size() < 2 )
815 x = tupel_coords.at( 0 ).toDouble( &conversionSuccess );
816 if ( !conversionSuccess )
820 y = tupel_coords.at( 1 ).toDouble( &conversionSuccess );
821 if ( !conversionSuccess )
825 coords.push_back(
QgsPoint( x, y ) );
834 QDomElement boxElem = boxNode.toElement();
835 if ( boxElem.tagName() !=
"Box" )
838 QDomElement bElem = boxElem.firstChild().toElement();
839 QString coordSeparator =
",";
840 QString tupelSeparator =
" ";
841 if ( bElem.hasAttribute(
"cs" ) )
843 coordSeparator = bElem.attribute(
"cs" );
845 if ( bElem.hasAttribute(
"ts" ) )
847 tupelSeparator = bElem.attribute(
"ts" );
850 QString bString = bElem.text();
851 bool ok1, ok2, ok3, ok4;
852 double xmin = bString.section( tupelSeparator, 0, 0 ).section( coordSeparator, 0, 0 ).toDouble( &ok1 );
853 double ymin = bString.section( tupelSeparator, 0, 0 ).section( coordSeparator, 1, 1 ).toDouble( &ok2 );
854 double xmax = bString.section( tupelSeparator, 1, 1 ).section( coordSeparator, 0, 0 ).toDouble( &ok3 );
855 double ymax = bString.section( tupelSeparator, 1, 1 ).section( coordSeparator, 1, 1 ).toDouble( &ok4 );
857 if ( ok1 && ok2 && ok3 && ok4 )
869 QString coordSeparator =
" ";
870 QString tupelSeparator =
" ";
876 QStringList pos = elem.text().split(
" ", QString::SkipEmptyParts );
878 bool conversionSuccess;
879 int posSize = pos.size();
881 int srsDimension = 2;
882 if ( elem.hasAttribute(
"srsDimension" ) )
884 srsDimension = elem.attribute(
"srsDimension" ).toInt( &conversionSuccess );
885 if ( !conversionSuccess )
890 else if ( elem.hasAttribute(
"dimension" ) )
892 srsDimension = elem.attribute(
"dimension" ).toInt( &conversionSuccess );
893 if ( !conversionSuccess )
899 for (
int i = 0; i < posSize / srsDimension; i++ )
901 x = pos.at( i * srsDimension ).toDouble( &conversionSuccess );
902 if ( !conversionSuccess )
906 y = pos.at( i * srsDimension + 1 ).toDouble( &conversionSuccess );
907 if ( !conversionSuccess )
911 coords.push_back(
QgsPoint( x, y ) );
921 QDomElement envelopeElem = envelopeNode.toElement();
922 if ( envelopeElem.tagName() !=
"Envelope" )
925 QDomNodeList lowerCornerList = envelopeElem.elementsByTagNameNS(
GML_NAMESPACE,
"lowerCorner" );
926 if ( lowerCornerList.size() < 1 )
929 QDomNodeList upperCornerList = envelopeElem.elementsByTagNameNS(
GML_NAMESPACE,
"upperCorner" );
930 if ( upperCornerList.size() < 1 )
933 bool conversionSuccess;
934 int srsDimension = 2;
936 QDomElement elem = lowerCornerList.at( 0 ).toElement();
937 if ( elem.hasAttribute(
"srsDimension" ) )
939 srsDimension = elem.attribute(
"srsDimension" ).toInt( &conversionSuccess );
940 if ( !conversionSuccess )
945 else if ( elem.hasAttribute(
"dimension" ) )
947 srsDimension = elem.attribute(
"dimension" ).toInt( &conversionSuccess );
948 if ( !conversionSuccess )
953 QString bString = elem.text();
955 double xmin = bString.section(
" ", 0, 0 ).toDouble( &conversionSuccess );
956 if ( !conversionSuccess )
958 double ymin = bString.section(
" ", 1, 1 ).toDouble( &conversionSuccess );
959 if ( !conversionSuccess )
962 elem = upperCornerList.at( 0 ).toElement();
963 if ( elem.hasAttribute(
"srsDimension" ) )
965 srsDimension = elem.attribute(
"srsDimension" ).toInt( &conversionSuccess );
966 if ( !conversionSuccess )
971 else if ( elem.hasAttribute(
"dimension" ) )
973 srsDimension = elem.attribute(
"dimension" ).toInt( &conversionSuccess );
974 if ( !conversionSuccess )
980 Q_UNUSED( srsDimension );
982 bString = elem.text();
983 double xmax = bString.section(
" ", 0, 0 ).toDouble( &conversionSuccess );
984 if ( !conversionSuccess )
986 double ymax = bString.section(
" ", 1, 1 ).toDouble( &conversionSuccess );
987 if ( !conversionSuccess )
1000 return QDomElement();
1003 QDomElement boxElem = doc.createElement(
"gml:Box" );
1004 QDomElement coordElem = doc.createElement(
"gml:coordinates" );
1005 coordElem.setAttribute(
"cs",
"," );
1006 coordElem.setAttribute(
"ts",
" " );
1008 QString coordString;
1017 QDomText coordText = doc.createTextNode( coordString );
1018 coordElem.appendChild( coordText );
1019 boxElem.appendChild( coordElem );
1028 return QDomElement();
1031 QDomElement envElem = doc.createElement(
"gml:Envelope" );
1034 QDomElement lowerCornerElem = doc.createElement(
"gml:lowerCorner" );
1038 QDomText lowerCornerText = doc.createTextNode( posList );
1039 lowerCornerElem.appendChild( lowerCornerText );
1040 envElem.appendChild( lowerCornerElem );
1042 QDomElement upperCornerElem = doc.createElement(
"gml:upperCorner" );
1046 QDomText upperCornerText = doc.createTextNode( posList );
1047 upperCornerElem.appendChild( upperCornerText );
1048 envElem.appendChild( upperCornerElem );
1055 if ( !geometry || !geometry->
asWkb() )
1056 return QDomElement();
1063 QDomElement baseCoordElem;
1065 bool hasZValue =
false;
1069 if ( format ==
"GML3" )
1071 switch ( geometry->
wkbType() )
1077 baseCoordElem = doc.createElement(
"gml:pos" );;
1080 baseCoordElem = doc.createElement(
"gml:posList" );;
1083 baseCoordElem.setAttribute(
"srsDimension",
"2" );
1088 baseCoordElem = doc.createElement(
"gml:coordinates" );;
1089 baseCoordElem.setAttribute(
"cs", cs );
1090 baseCoordElem.setAttribute(
"ts", ts );
1093 switch ( geometry->
wkbType() )
1098 QDomElement pointElem = doc.createElement(
"gml:Point" );
1099 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1105 coordElem.appendChild( coordText );
1106 pointElem.appendChild( coordElem );
1113 QDomElement multiPointElem = doc.createElement(
"gml:MultiPoint" );
1118 for (
int idx = 0; idx < nPoints; ++idx )
1120 wkbPtr += 1 +
sizeof( int );
1121 QDomElement pointMemberElem = doc.createElement(
"gml:pointMember" );
1122 QDomElement pointElem = doc.createElement(
"gml:Point" );
1123 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1129 coordElem.appendChild( coordText );
1130 pointElem.appendChild( coordElem );
1134 wkbPtr +=
sizeof( double );
1136 pointMemberElem.appendChild( pointElem );
1137 multiPointElem.appendChild( pointMemberElem );
1139 return multiPointElem;
1145 QDomElement lineStringElem = doc.createElement(
"gml:LineString" );
1151 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1152 QString coordString;
1153 for (
int idx = 0; idx < nPoints; ++idx )
1166 wkbPtr +=
sizeof( double );
1169 QDomText coordText = doc.createTextNode( coordString );
1170 coordElem.appendChild( coordText );
1171 lineStringElem.appendChild( coordElem );
1172 return lineStringElem;
1178 QDomElement multiLineStringElem = doc.createElement(
"gml:MultiLineString" );
1183 for (
int jdx = 0; jdx < nLines; jdx++ )
1185 QDomElement lineStringMemberElem = doc.createElement(
"gml:lineStringMember" );
1186 QDomElement lineStringElem = doc.createElement(
"gml:LineString" );
1187 wkbPtr += 1 +
sizeof( int );
1192 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1193 QString coordString;
1194 for (
int idx = 0; idx < nPoints; idx++ )
1208 wkbPtr +=
sizeof( double );
1211 QDomText coordText = doc.createTextNode( coordString );
1212 coordElem.appendChild( coordText );
1213 lineStringElem.appendChild( coordElem );
1214 lineStringMemberElem.appendChild( lineStringElem );
1215 multiLineStringElem.appendChild( lineStringMemberElem );
1217 return multiLineStringElem;
1223 QDomElement polygonElem = doc.createElement(
"gml:Polygon" );
1229 if ( numRings == 0 )
1230 return QDomElement();
1232 int *ringNumPoints =
new int[numRings];
1234 for (
int idx = 0; idx < numRings; idx++ )
1236 QString boundaryName =
"gml:outerBoundaryIs";
1239 boundaryName =
"gml:innerBoundaryIs";
1241 QDomElement boundaryElem = doc.createElement( boundaryName );
1242 QDomElement ringElem = doc.createElement(
"gml:LinearRing" );
1246 ringNumPoints[idx] = nPoints;
1248 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1249 QString coordString;
1250 for (
int jdx = 0; jdx < nPoints; jdx++ )
1263 wkbPtr +=
sizeof( double );
1266 QDomText coordText = doc.createTextNode( coordString );
1267 coordElem.appendChild( coordText );
1268 ringElem.appendChild( coordElem );
1269 boundaryElem.appendChild( ringElem );
1270 polygonElem.appendChild( boundaryElem );
1272 delete [] ringNumPoints;
1279 QDomElement multiPolygonElem = doc.createElement(
"gml:MultiPolygon" );
1282 wkbPtr >> numPolygons;
1284 for (
int kdx = 0; kdx < numPolygons; kdx++ )
1286 QDomElement polygonMemberElem = doc.createElement(
"gml:polygonMember" );
1287 QDomElement polygonElem = doc.createElement(
"gml:Polygon" );
1289 wkbPtr += 1 +
sizeof( int );
1294 for (
int idx = 0; idx < numRings; idx++ )
1296 QString boundaryName =
"gml:outerBoundaryIs";
1299 boundaryName =
"gml:innerBoundaryIs";
1301 QDomElement boundaryElem = doc.createElement( boundaryName );
1302 QDomElement ringElem = doc.createElement(
"gml:LinearRing" );
1307 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1308 QString coordString;
1309 for (
int jdx = 0; jdx < nPoints; jdx++ )
1323 wkbPtr +=
sizeof( double );
1326 QDomText coordText = doc.createTextNode( coordString );
1327 coordElem.appendChild( coordText );
1328 ringElem.appendChild( coordElem );
1329 boundaryElem.appendChild( ringElem );
1330 polygonElem.appendChild( boundaryElem );
1331 polygonMemberElem.appendChild( polygonElem );
1332 multiPolygonElem.appendChild( polygonMemberElem );
1335 return multiPolygonElem;
1338 return QDomElement();
1349 QDomElement coordElem = doc.createElement(
"gml:coordinates" );
1350 coordElem.setAttribute(
"cs",
"," );
1351 coordElem.setAttribute(
"ts",
" " );
1353 QString coordString;
1354 QVector<QgsPoint>::const_iterator pointIt = points.constBegin();
1355 for ( ; pointIt != points.constEnd(); ++pointIt )
1357 if ( pointIt != points.constBegin() )
1366 QDomText coordText = doc.createTextNode( coordString );
1367 coordElem.appendChild( coordText );
1373 QDomElement posElem = doc.createElement(
"gml:pos" );
1374 if ( points.size() > 1 )
1375 posElem = doc.createElement(
"gml:posList" );
1376 posElem.setAttribute(
"srsDimension",
"2" );
1378 QString coordString;
1379 QVector<QgsPoint>::const_iterator pointIt = points.constBegin();
1380 for ( ; pointIt != points.constEnd(); ++pointIt )
1382 if ( pointIt != points.constBegin() )
1391 QDomText coordText = doc.createTextNode( coordString );
1392 posElem.appendChild( coordText );
1402 if ( fillElement.isNull() || !fillElement.hasChildNodes() )
1410 QDomElement cssElem = fillElement.firstChildElement(
"CssParameter" );
1411 while ( !cssElem.isNull() )
1413 cssName = cssElem.attribute(
"name",
"not_found" );
1414 if ( cssName !=
"not_found" )
1416 elemText = cssElem.text();
1417 if ( cssName ==
"fill" )
1419 color.setNamedColor( elemText );
1421 else if ( cssName ==
"fill-opacity" )
1424 double opacity = elemText.toDouble( &ok );
1427 color.setAlphaF( opacity );
1432 cssElem = cssElem.nextSiblingElement(
"CssParameter" );
1441 if ( element.isNull() || !element.hasChildNodes() )
1446 QDomElement childElem = element.firstChildElement();
1447 while ( !childElem.isNull() )
1468 childElem = childElem.nextSiblingElement();
1480 static QMap<QString, int> binOps;
1481 if ( binOps.isEmpty() )
1522 static QStringList spatialOps;
1523 if ( spatialOps.isEmpty() )
1525 spatialOps <<
"BBOX" <<
"Intersects" <<
"Contians" <<
"Crosses" <<
"Equals"
1526 <<
"Disjoint" <<
"Overlaps" <<
"Touches" <<
"Within";
1529 return spatialOps.contains( tagName );
1536 if ( element.isNull() )
1553 if ( element.tagName() ==
"Not" )
1557 else if ( element.tagName() ==
"PropertyIsNull" )
1561 else if ( element.tagName() ==
"Literal" )
1565 else if ( element.tagName() ==
"Function" )
1569 else if ( element.tagName() ==
"PropertyName" )
1573 else if ( element.tagName() ==
"PropertyIsBetween" )
1578 errorMessage += QString(
"unable to convert '%1' element to a valid expression: it is not supported yet or it has invalid arguments" ).arg( element.tagName() );
1586 if ( element.isNull() )
1592 if ( errorMessage.isEmpty() )
1593 errorMessage = QString(
"'%1' binary operator not supported." ).arg( element.tagName() );
1597 QDomElement operandElem = element.firstChildElement();
1601 if ( errorMessage.isEmpty() )
1602 errorMessage = QString(
"invalid left operand for '%1' binary operator" ).arg( element.tagName() );
1606 operandElem = operandElem.nextSiblingElement();
1610 if ( errorMessage.isEmpty() )
1611 errorMessage = QString(
"invalid right operand for '%1' binary operator" ).arg( element.tagName() );
1626 QDomElement childElem = element.firstChildElement();
1628 while ( !childElem.isNull() && gml2Str.isEmpty() )
1630 if ( childElem.tagName() !=
"PropertyName" )
1632 QTextStream gml2Stream( &gml2Str );
1633 childElem.save( gml2Stream, 0 );
1635 childElem = childElem.nextSiblingElement();
1637 if ( !gml2Str.isEmpty() )
1643 errorMessage = QString(
"No OGC Geometry found" );
1657 if ( element.tagName() !=
"Not" )
1660 QDomElement operandElem = element.firstChildElement();
1664 if ( errorMessage.isEmpty() )
1665 errorMessage = QString(
"invalid operand for '%1' unary operator" ).arg( element.tagName() );
1675 if ( element.isNull() || element.tagName() !=
"Function" )
1677 errorMessage = QString(
"ogc:Function expected, got %1" ).arg( element.tagName() );
1685 if ( element.attribute(
"name" ) != funcDef->
name() )
1690 QDomElement operandElem = element.firstChildElement();
1691 while ( !operandElem.isNull() )
1701 operandElem = operandElem.nextSiblingElement();
1714 if ( element.isNull() || element.tagName() !=
"Literal" )
1716 errorMessage = QString(
"ogc:Literal expected, got %1" ).arg( element.tagName() );
1723 QDomNode childNode = element.firstChild();
1724 while ( !childNode.isNull() )
1728 if ( childNode.nodeType() == QDomNode::ElementNode )
1731 QDomElement operandElem = childNode.toElement();
1738 errorMessage = QString(
"'%1' is an invalid or not supported content for ogc:Literal" ).arg( operandElem.tagName() );
1745 QVariant value = childNode.nodeValue();
1750 double d = value.toDouble( &ok );
1769 childNode = childNode.nextSibling();
1781 if ( element.isNull() || element.tagName() !=
"PropertyName" )
1783 errorMessage = QString(
"ogc:PropertyName expected, got %1" ).arg( element.tagName() );
1797 QDomElement operandElem = element.firstChildElement();
1798 while ( !operandElem.isNull() )
1800 if ( operandElem.tagName() ==
"LowerBoundary" )
1802 QDomElement lowerBoundElem = operandElem.firstChildElement();
1805 else if ( operandElem.tagName() ==
"UpperBoundary" )
1807 QDomElement upperBoundElem = operandElem.firstChildElement();
1819 if ( operand && lowerBound && operand2 && upperBound )
1822 operandElem = operandElem.nextSiblingElement();
1825 if ( !operand || !lowerBound || !operand2 || !upperBound )
1836 errorMessage =
"missing some required sub-elements in ogc:PropertyIsBetween";
1850 if ( element.tagName() !=
"PropertyIsNull" )
1855 QDomElement operandElem = element.firstChildElement();
1873 return QDomElement();
1875 QString localErrorMessage;
1876 QString& refErrorMessage = ( errorMessage ? *errorMessage : localErrorMessage );
1877 refErrorMessage.clear();
1880 if ( exprRootElem.isNull() )
1881 return QDomElement();
1883 QDomElement filterElem = doc.createElementNS(
OGC_NAMESPACE,
"ogc:Filter" );
1884 filterElem.appendChild( exprRootElem );
1907 errorMessage = QString(
"Node type not supported: %1" ).arg( node->
nodeType() );
1908 return QDomElement();
1916 switch ( node->
op() )
1919 uoElem = doc.createElement(
"ogc:Literal" );
1920 uoElem.appendChild( doc.createTextNode(
"-" ) );
1923 uoElem = doc.createElement(
"ogc:Not" );
1928 return QDomElement();
1932 if ( !errorMessage.isEmpty() )
1933 return QDomElement();
1935 uoElem.appendChild( operandElem );
1943 if ( !errorMessage.isEmpty() )
1944 return QDomElement();
1954 if ( rightLit->
value().isNull() )
1957 QDomElement elem = doc.createElement(
"ogc:PropertyIsNull" );
1958 elem.appendChild( leftElem );
1962 QDomElement notElem = doc.createElement(
"ogc:Not" );
1963 notElem.appendChild( elem );
1977 if ( !errorMessage.isEmpty() )
1978 return QDomElement();
1982 if ( opText.isEmpty() )
1987 return QDomElement();
1990 QDomElement boElem = doc.createElement(
"ogc:" + opText );
1995 boElem.setAttribute(
"matchCase",
"false" );
1998 boElem.setAttribute(
"wildCard",
"%" );
1999 boElem.setAttribute(
"singleChar",
"?" );
2000 boElem.setAttribute(
"escapeChar",
"!" );
2003 boElem.appendChild( leftElem );
2004 boElem.appendChild( rightElem );
2012 switch ( node->
value().type() )
2015 value = QString::number( node->
value().toInt() );
2017 case QVariant::Double:
2018 value = QString::number( node->
value().toDouble() );
2020 case QVariant::String:
2021 value = node->
value().toString();
2025 errorMessage = QString(
"Literal type not supported: %1" ).arg( node->
value().type() );
2026 return QDomElement();
2029 QDomElement litElem = doc.createElement(
"ogc:Literal" );
2030 litElem.appendChild( doc.createTextNode( value ) );
2037 QDomElement propElem = doc.createElement(
"ogc:PropertyName" );
2038 propElem.appendChild( doc.createTextNode( node->
name() ) );
2046 if ( node->
list()->
list().size() == 1 )
2049 QDomElement orElem = doc.createElement(
"ogc:Or" );
2055 if ( !errorMessage.isEmpty() )
2056 return QDomElement();
2058 QDomElement eqElem = doc.createElement(
"ogc:PropertyIsEqualTo" );
2059 eqElem.appendChild( leftNode.cloneNode() );
2060 eqElem.appendChild( listNode );
2062 orElem.appendChild( eqElem );
2069 static QMap<QString, QString> binSpatialOps;
2070 if ( binSpatialOps.isEmpty() )
2072 binSpatialOps.insert(
"disjoint",
"Disjoint" );
2073 binSpatialOps.insert(
"intersects",
"Intersects" );
2074 binSpatialOps.insert(
"touches",
"Touches" );
2075 binSpatialOps.insert(
"crosses",
"Crosses" );
2076 binSpatialOps.insert(
"contains",
"Contains" );
2077 binSpatialOps.insert(
"overlaps",
"Overlaps" );
2078 binSpatialOps.insert(
"within",
"Within" );
2080 return binSpatialOps;
2100 return fd->
name() ==
"$geometry";
2112 if ( fnDef->
name() ==
"geomFromWKT" )
2114 const QList<QgsExpression::Node*>& args = fnNode->
args()->
list();
2130 if ( fd->
name() ==
"bbox" )
2132 QList<QgsExpression::Node*> argNodes = node->
args()->
list();
2133 Q_ASSERT( argNodes.count() == 2 );
2143 QDomElement geomProperty = doc.createElement(
"ogc:PropertyName" );
2144 geomProperty.appendChild( doc.createTextNode(
"geometry" ) );
2146 QDomElement funcElem = doc.createElement(
"ogr:BBOX" );
2147 funcElem.appendChild( geomProperty );
2148 funcElem.appendChild( elemBox );
2155 errorMessage = QString(
"<BBOX> is currently supported only in form: bbox($geometry, geomFromWKT('...'))" );
2156 return QDomElement();
2162 QList<QgsExpression::Node*> argNodes = node->
args()->
list();
2163 Q_ASSERT( argNodes.count() == 2 );
2167 otherNode = argNodes[1];
2169 otherNode = argNodes[0];
2172 errorMessage = QString(
"Unable to translate spatial operator: at least one must refer to geometry." );
2173 return QDomElement();
2176 QDomElement otherGeomElem;
2181 errorMessage =
"spatial operator: the other operator must be a geometry constructor function";
2182 return QDomElement();
2187 if ( otherFnDef->
name() ==
"geomFromWKT" )
2192 errorMessage =
"geomFromWKT: argument must be string literal";
2193 return QDomElement();
2200 else if ( otherFnDef->
name() ==
"geomFromGML" )
2205 errorMessage =
"geomFromGML: argument must be string literal";
2206 return QDomElement();
2209 QDomDocument geomDoc;
2211 if ( !geomDoc.setContent( gml,
true ) )
2213 errorMessage =
"geomFromGML: unable to parse XML";
2214 return QDomElement();
2217 QDomNode geomNode = doc.importNode( geomDoc.documentElement(), true );
2218 otherGeomElem = geomNode.toElement();
2222 errorMessage =
"spatial operator: unknown geometry constructor function";
2223 return QDomElement();
2227 QDomElement geomProperty = doc.createElement(
"ogc:PropertyName" );
2228 geomProperty.appendChild( doc.createTextNode(
"geometry" ) );
2229 funcElem.appendChild( geomProperty );
2230 funcElem.appendChild( otherGeomElem );
2236 errorMessage = QString(
"Special columns / constants are not supported." );
2237 return QDomElement();
2241 QDomElement funcElem = doc.createElement(
"ogc:Function" );
2242 funcElem.setAttribute(
"name", fd->
name() );
2246 if ( !errorMessage.isEmpty() )
2247 return QDomElement();
2249 funcElem.appendChild( childElem );