10 #include <netinet/in.h>
20 QDomElement geometryTypeElement = geometryNode.toElement();
21 QString geomType = geometryTypeElement.tagName();
23 if ( !( geomType ==
"Point" || geomType ==
"LineString" || geomType ==
"Polygon" ||
24 geomType ==
"MultiPoint" || geomType ==
"MultiLineString" || geomType ==
"MultiPolygon" ||
25 geomType ==
"Box" || geomType ==
"Envelope" ) )
27 QDomNode geometryChild = geometryNode.firstChild();
28 if ( geometryChild.isNull() )
32 geometryTypeElement = geometryChild.toElement();
33 geomType = geometryTypeElement.tagName();
36 if ( !( geomType ==
"Point" || geomType ==
"LineString" || geomType ==
"Polygon" ||
37 geomType ==
"MultiPoint" || geomType ==
"MultiLineString" || geomType ==
"MultiPolygon" ||
38 geomType ==
"Box" || geomType ==
"Envelope" ) )
41 if ( geomType ==
"Point" )
45 else if ( geomType ==
"LineString" )
49 else if ( geomType ==
"Polygon" )
53 else if ( geomType ==
"MultiPoint" )
57 else if ( geomType ==
"MultiLineString" )
61 else if ( geomType ==
"MultiPolygon" )
65 else if ( geomType ==
"Box" )
69 else if ( geomType ==
"Envelope" )
82 QString xml = QString(
"<tmp xmlns=\"%1\" xmlns:gml=\"%1\">%2</tmp>" ).arg(
GML_NAMESPACE ).arg( xmlString );
84 if ( !doc.setContent( xml,
true ) )
95 QDomNodeList coordList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE,
"coordinates" );
96 if ( coordList.size() > 0 )
98 QDomElement coordElement = coordList.at( 0 ).toElement();
106 QDomNodeList posList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE,
"pos" );
107 if ( posList.size() < 1 )
111 QDomElement posElement = posList.at( 0 ).toElement();
118 if ( pointCoordinate.size() < 1 )
123 QgsPolyline::const_iterator point_it = pointCoordinate.begin();
124 char e = htonl( 1 ) != 1 ;
125 double x = point_it->x();
126 double y = point_it->y();
127 int size = 1 +
sizeof( int ) + 2 *
sizeof(
double );
130 unsigned char* wkb =
new unsigned char[
size];
133 memcpy( &( wkb )[wkbPosition], &e, 1 );
135 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
136 wkbPosition +=
sizeof( int );
137 memcpy( &( wkb )[wkbPosition], &x,
sizeof(
double ) );
138 wkbPosition +=
sizeof( double );
139 memcpy( &( wkb )[wkbPosition], &y,
sizeof(
double ) );
150 QDomNodeList coordList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE,
"coordinates" );
151 if ( coordList.size() > 0 )
153 QDomElement coordElement = coordList.at( 0 ).toElement();
161 QDomNodeList posList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE,
"posList" );
162 if ( posList.size() < 1 )
166 QDomElement posElement = posList.at( 0 ).toElement();
173 char e = htonl( 1 ) != 1 ;
174 int size = 1 + 2 *
sizeof( int ) + lineCoordinates.size() * 2 *
sizeof( double );
177 unsigned char* wkb =
new unsigned char[
size];
181 int nPoints = lineCoordinates.size();
184 memcpy( &( wkb )[wkbPosition], &e, 1 );
186 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
187 wkbPosition +=
sizeof( int );
188 memcpy( &( wkb )[wkbPosition], &nPoints,
sizeof(
int ) );
189 wkbPosition +=
sizeof( int );
191 QgsPolyline::const_iterator iter;
192 for ( iter = lineCoordinates.begin(); iter != lineCoordinates.end(); ++iter )
196 memcpy( &( wkb )[wkbPosition], &x,
sizeof(
double ) );
197 wkbPosition +=
sizeof( double );
198 memcpy( &( wkb )[wkbPosition], &y,
sizeof(
double ) );
199 wkbPosition +=
sizeof( double );
214 QDomNodeList outerBoundaryList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE,
"outerBoundaryIs" );
215 if ( outerBoundaryList.size() > 0 )
217 QDomElement coordinatesElement = outerBoundaryList.at( 0 ).firstChild().firstChild().toElement();
218 if ( coordinatesElement.isNull() )
226 ringCoordinates.push_back( exteriorPointList );
229 QDomNodeList innerBoundaryList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE,
"innerBoundaryIs" );
230 for (
int i = 0; i < innerBoundaryList.size(); ++i )
233 coordinatesElement = innerBoundaryList.at( i ).firstChild().firstChild().toElement();
234 if ( coordinatesElement.isNull() )
242 ringCoordinates.push_back( interiorPointList );
248 QDomNodeList exteriorList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE,
"exterior" );
249 if ( exteriorList.size() < 1 )
253 QDomElement posElement = exteriorList.at( 0 ).firstChild().firstChild().toElement();
254 if ( posElement.isNull() )
262 ringCoordinates.push_back( exteriorPointList );
265 QDomNodeList interiorList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE,
"interior" );
266 for (
int i = 0; i < interiorList.size(); ++i )
269 QDomElement posElement = interiorList.at( i ).firstChild().firstChild().toElement();
270 if ( posElement.isNull() )
278 ringCoordinates.push_back( interiorPointList );
283 int nrings = ringCoordinates.size();
288 for ( QgsMultiPolyline::const_iterator it = ringCoordinates.begin(); it != ringCoordinates.end(); ++it )
290 npoints += it->size();
292 int size = 1 + 2 *
sizeof( int ) + nrings *
sizeof(
int ) + 2 * npoints *
sizeof( double );
295 unsigned char* wkb =
new unsigned char[
size];
298 char e = htonl( 1 ) != 1 ;
300 int nPointsInRing = 0;
304 memcpy( &( wkb )[wkbPosition], &e, 1 );
306 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
307 wkbPosition +=
sizeof( int );
308 memcpy( &( wkb )[wkbPosition], &nrings,
sizeof(
int ) );
309 wkbPosition +=
sizeof( int );
310 for ( QgsMultiPolyline::const_iterator it = ringCoordinates.begin(); it != ringCoordinates.end(); ++it )
312 nPointsInRing = it->size();
313 memcpy( &( wkb )[wkbPosition], &nPointsInRing,
sizeof(
int ) );
314 wkbPosition +=
sizeof( int );
316 QgsPolyline::const_iterator iter;
317 for ( iter = it->begin(); iter != it->end(); ++iter )
322 memcpy( &( wkb )[wkbPosition], &x,
sizeof(
double ) );
323 wkbPosition +=
sizeof( double );
324 memcpy( &( wkb )[wkbPosition], &y,
sizeof(
double ) );
325 wkbPosition +=
sizeof( double );
338 QDomNodeList pointMemberList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE,
"pointMember" );
339 if ( pointMemberList.size() < 1 )
343 QDomNodeList pointNodeList;
345 QDomNodeList coordinatesList;
346 QDomNodeList posList;
347 for (
int i = 0; i < pointMemberList.size(); ++i )
350 pointNodeList = pointMemberList.at( i ).toElement().elementsByTagNameNS(
GML_NAMESPACE,
"Point" );
351 if ( pointNodeList.size() < 1 )
356 coordinatesList = pointNodeList.at( 0 ).toElement().elementsByTagNameNS(
GML_NAMESPACE,
"coordinates" );
357 if ( coordinatesList.size() > 0 )
359 currentPoint.clear();
364 if ( currentPoint.size() < 1 )
368 pointList.push_back(( *currentPoint.begin() ) );
374 posList = pointNodeList.at( 0 ).toElement().elementsByTagNameNS(
GML_NAMESPACE,
"pos" );
375 if ( posList.size() < 1 )
379 currentPoint.clear();
384 if ( currentPoint.size() < 1 )
388 pointList.push_back(( *currentPoint.begin() ) );
392 int nPoints = pointList.size();
397 int size = 1 + 2 *
sizeof( int ) + pointList.size() * ( 2 *
sizeof( double ) + 1 +
sizeof(
int ) );
400 unsigned char* wkb =
new unsigned char[
size];
403 char e = htonl( 1 ) != 1 ;
406 memcpy( &( wkb )[wkbPosition], &e, 1 );
408 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
409 wkbPosition +=
sizeof( int );
410 memcpy( &( wkb )[wkbPosition], &nPoints,
sizeof(
int ) );
411 wkbPosition +=
sizeof( int );
413 for ( QgsPolyline::const_iterator it = pointList.begin(); it != pointList.end(); ++it )
415 memcpy( &( wkb )[wkbPosition], &e, 1 );
417 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
418 wkbPosition +=
sizeof( int );
420 memcpy( &( wkb )[wkbPosition], &x,
sizeof(
double ) );
421 wkbPosition +=
sizeof( double );
423 memcpy( &( wkb )[wkbPosition], &y,
sizeof(
double ) );
424 wkbPosition +=
sizeof( double );
443 QList< QgsPolyline > lineCoordinates;
444 QDomElement currentLineStringElement;
445 QDomNodeList currentCoordList;
446 QDomNodeList currentPosList;
448 QDomNodeList lineStringMemberList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE,
"lineStringMember" );
449 if ( lineStringMemberList.size() > 0 )
451 for (
int i = 0; i < lineStringMemberList.size(); ++i )
453 QDomNodeList lineStringNodeList = lineStringMemberList.at( i ).toElement().elementsByTagNameNS(
GML_NAMESPACE,
"LineString" );
454 if ( lineStringNodeList.size() < 1 )
458 currentLineStringElement = lineStringNodeList.at( 0 ).toElement();
459 currentCoordList = currentLineStringElement.elementsByTagNameNS(
GML_NAMESPACE,
"coordinates" );
460 if ( currentCoordList.size() > 0 )
463 if (
readGMLCoordinates( currentPointList, currentCoordList.at( 0 ).toElement() ) != 0 )
467 lineCoordinates.push_back( currentPointList );
471 currentPosList = currentLineStringElement.elementsByTagNameNS(
GML_NAMESPACE,
"posList" );
472 if ( currentPosList.size() < 1 )
477 if (
readGMLPositions( currentPointList, currentPosList.at( 0 ).toElement() ) != 0 )
481 lineCoordinates.push_back( currentPointList );
487 QDomNodeList lineStringList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE,
"LineString" );
488 if ( lineStringList.size() > 0 )
490 for (
int i = 0; i < lineStringList.size(); ++i )
492 currentLineStringElement = lineStringList.at( i ).toElement();
493 currentCoordList = currentLineStringElement.elementsByTagNameNS(
GML_NAMESPACE,
"coordinates" );
494 if ( currentCoordList.size() > 0 )
497 if (
readGMLCoordinates( currentPointList, currentCoordList.at( 0 ).toElement() ) != 0 )
501 lineCoordinates.push_back( currentPointList );
506 currentPosList = currentLineStringElement.elementsByTagNameNS(
GML_NAMESPACE,
"posList" );
507 if ( currentPosList.size() < 1 )
512 if (
readGMLPositions( currentPointList, currentPosList.at( 0 ).toElement() ) != 0 )
516 lineCoordinates.push_back( currentPointList );
526 int nLines = lineCoordinates.size();
531 int size = ( lineCoordinates.size() + 1 ) * ( 1 + 2 *
sizeof(
int ) );
532 for ( QList< QgsPolyline >::const_iterator it = lineCoordinates.begin(); it != lineCoordinates.end(); ++it )
534 size += it->size() * 2 *
sizeof( double );
538 unsigned char* wkb =
new unsigned char[
size];
541 char e = htonl( 1 ) != 1 ;
545 memcpy( &( wkb )[wkbPosition], &e, 1 );
547 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
548 wkbPosition +=
sizeof( int );
549 memcpy( &( wkb )[wkbPosition], &nLines,
sizeof(
int ) );
550 wkbPosition +=
sizeof( int );
552 for ( QList< QgsPolyline >::const_iterator it = lineCoordinates.begin(); it != lineCoordinates.end(); ++it )
554 memcpy( &( wkb )[wkbPosition], &e, 1 );
556 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
557 wkbPosition +=
sizeof( int );
558 nPoints = it->size();
559 memcpy( &( wkb )[wkbPosition], &nPoints,
sizeof(
int ) );
560 wkbPosition +=
sizeof( int );
561 for ( QgsPolyline::const_iterator iter = it->begin(); iter != it->end(); ++iter )
566 memcpy( &( wkb )[wkbPosition], &x,
sizeof(
double ) );
567 wkbPosition +=
sizeof( double );
568 memcpy( &( wkb )[wkbPosition], &y,
sizeof(
double ) );
569 wkbPosition +=
sizeof( double );
582 QDomElement currentPolygonMemberElement;
583 QDomNodeList polygonList;
584 QDomElement currentPolygonElement;
586 QDomNodeList outerBoundaryList;
587 QDomElement currentOuterBoundaryElement;
588 QDomNodeList innerBoundaryList;
589 QDomElement currentInnerBoundaryElement;
591 QDomNodeList exteriorList;
592 QDomElement currentExteriorElement;
593 QDomElement currentInteriorElement;
594 QDomNodeList interiorList;
596 QDomNodeList linearRingNodeList;
597 QDomElement currentLinearRingElement;
599 QDomNodeList currentCoordinateList;
600 QDomNodeList currentPosList;
602 QDomNodeList polygonMemberList = geometryElement.elementsByTagNameNS(
GML_NAMESPACE,
"polygonMember" );
603 for (
int i = 0; i < polygonMemberList.size(); ++i )
606 currentPolygonMemberElement = polygonMemberList.at( i ).toElement();
607 polygonList = currentPolygonMemberElement.elementsByTagNameNS(
GML_NAMESPACE,
"Polygon" );
608 if ( polygonList.size() < 1 )
612 currentPolygonElement = polygonList.at( 0 ).toElement();
615 outerBoundaryList = currentPolygonElement.elementsByTagNameNS(
GML_NAMESPACE,
"outerBoundaryIs" );
616 if ( outerBoundaryList.size() > 0 )
618 currentOuterBoundaryElement = outerBoundaryList.at( 0 ).toElement();
621 linearRingNodeList = currentOuterBoundaryElement.elementsByTagNameNS(
GML_NAMESPACE,
"LinearRing" );
622 if ( linearRingNodeList.size() < 1 )
626 currentLinearRingElement = linearRingNodeList.at( 0 ).toElement();
627 currentCoordinateList = currentLinearRingElement.elementsByTagNameNS(
GML_NAMESPACE,
"coordinates" );
628 if ( currentCoordinateList.size() < 1 )
632 if (
readGMLCoordinates( ringCoordinates, currentCoordinateList.at( 0 ).toElement() ) != 0 )
636 currentPolygonList.push_back( ringCoordinates );
639 QDomNodeList innerBoundaryList = currentPolygonElement.elementsByTagNameNS(
GML_NAMESPACE,
"innerBoundaryIs" );
640 for (
int j = 0; j < innerBoundaryList.size(); ++j )
643 currentInnerBoundaryElement = innerBoundaryList.at( j ).toElement();
644 linearRingNodeList = currentInnerBoundaryElement.elementsByTagNameNS(
GML_NAMESPACE,
"LinearRing" );
645 if ( linearRingNodeList.size() < 1 )
649 currentLinearRingElement = linearRingNodeList.at( 0 ).toElement();
650 currentCoordinateList = currentLinearRingElement.elementsByTagNameNS(
GML_NAMESPACE,
"coordinates" );
651 if ( currentCoordinateList.size() < 1 )
655 if (
readGMLCoordinates( ringCoordinates, currentCoordinateList.at( 0 ).toElement() ) != 0 )
659 currentPolygonList.push_back( ringCoordinates );
665 exteriorList = currentPolygonElement.elementsByTagNameNS(
GML_NAMESPACE,
"exterior" );
666 if ( exteriorList.size() < 1 )
671 currentExteriorElement = exteriorList.at( 0 ).toElement();
674 linearRingNodeList = currentExteriorElement.elementsByTagNameNS(
GML_NAMESPACE,
"LinearRing" );
675 if ( linearRingNodeList.size() < 1 )
679 currentLinearRingElement = linearRingNodeList.at( 0 ).toElement();
680 currentPosList = currentLinearRingElement.elementsByTagNameNS(
GML_NAMESPACE,
"posList" );
681 if ( currentPosList.size() < 1 )
685 if (
readGMLPositions( ringPositions, currentPosList.at( 0 ).toElement() ) != 0 )
689 currentPolygonList.push_back( ringPositions );
692 QDomNodeList interiorList = currentPolygonElement.elementsByTagNameNS(
GML_NAMESPACE,
"interior" );
693 for (
int j = 0; j < interiorList.size(); ++j )
696 currentInteriorElement = interiorList.at( j ).toElement();
697 linearRingNodeList = currentInteriorElement.elementsByTagNameNS(
GML_NAMESPACE,
"LinearRing" );
698 if ( linearRingNodeList.size() < 1 )
702 currentLinearRingElement = linearRingNodeList.at( 0 ).toElement();
703 currentPosList = currentLinearRingElement.elementsByTagNameNS(
GML_NAMESPACE,
"posList" );
704 if ( currentPosList.size() < 1 )
708 if (
readGMLPositions( ringPositions, currentPosList.at( 0 ).toElement() ) != 0 )
712 currentPolygonList.push_back( ringPositions );
715 multiPolygonPoints.push_back( currentPolygonList );
718 int nPolygons = multiPolygonPoints.size();
722 int size = 1 + 2 *
sizeof( int );
724 for ( QgsMultiPolygon::const_iterator it = multiPolygonPoints.begin(); it != multiPolygonPoints.end(); ++it )
726 size += 1 + 2 *
sizeof( int );
727 for ( QgsPolygon::const_iterator iter = it->begin(); iter != it->end(); ++iter )
729 size +=
sizeof( int ) + 2 * iter->size() *
sizeof( double );
734 unsigned char* wkb =
new unsigned char[
size];
736 char e = htonl( 1 ) != 1 ;
743 memcpy( &( wkb )[wkbPosition], &e, 1 );
745 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
746 wkbPosition +=
sizeof( int );
747 memcpy( &( wkb )[wkbPosition], &nPolygons,
sizeof(
int ) );
748 wkbPosition +=
sizeof( int );
752 for ( QgsMultiPolygon::const_iterator it = multiPolygonPoints.begin(); it != multiPolygonPoints.end(); ++it )
754 memcpy( &( wkb )[wkbPosition], &e, 1 );
756 memcpy( &( wkb )[wkbPosition], &type,
sizeof(
int ) );
757 wkbPosition +=
sizeof( int );
759 memcpy( &( wkb )[wkbPosition], &nRings,
sizeof(
int ) );
760 wkbPosition +=
sizeof( int );
761 for ( QgsPolygon::const_iterator iter = it->begin(); iter != it->end(); ++iter )
763 nPointsInRing = iter->size();
764 memcpy( &( wkb )[wkbPosition], &nPointsInRing,
sizeof(
int ) );
765 wkbPosition +=
sizeof( int );
766 for ( QgsPolyline::const_iterator iterator = iter->begin(); iterator != iter->end(); ++iterator )
770 memcpy( &( wkb )[wkbPosition], &x,
sizeof(
double ) );
771 wkbPosition +=
sizeof( double );
772 memcpy( &( wkb )[wkbPosition], &y,
sizeof(
double ) );
773 wkbPosition +=
sizeof( double );
785 QString coordSeparator =
",";
786 QString tupelSeparator =
" ";
791 if ( elem.hasAttribute(
"cs" ) )
793 coordSeparator = elem.attribute(
"cs" );
795 if ( elem.hasAttribute(
"ts" ) )
797 tupelSeparator = elem.attribute(
"ts" );
800 QStringList tupels = elem.text().split( tupelSeparator, QString::SkipEmptyParts );
801 QStringList tupel_coords;
803 bool conversionSuccess;
805 QStringList::const_iterator it;
806 for ( it = tupels.constBegin(); it != tupels.constEnd(); ++it )
808 tupel_coords = ( *it ).split( coordSeparator, QString::SkipEmptyParts );
809 if ( tupel_coords.size() < 2 )
813 x = tupel_coords.at( 0 ).toDouble( &conversionSuccess );
814 if ( !conversionSuccess )
818 y = tupel_coords.at( 1 ).toDouble( &conversionSuccess );
819 if ( !conversionSuccess )
823 coords.push_back(
QgsPoint( x, y ) );
832 QDomElement boxElem = boxNode.toElement();
833 if ( boxElem.tagName() !=
"Box" )
836 QDomElement bElem = boxElem.firstChild().toElement();
837 QString coordSeparator =
",";
838 QString tupelSeparator =
" ";
839 if ( bElem.hasAttribute(
"cs" ) )
841 coordSeparator = bElem.attribute(
"cs" );
843 if ( bElem.hasAttribute(
"ts" ) )
845 tupelSeparator = bElem.attribute(
"ts" );
848 QString bString = bElem.text();
849 bool ok1, ok2, ok3, ok4;
850 double xmin = bString.section( tupelSeparator, 0, 0 ).section( coordSeparator, 0, 0 ).toDouble( &ok1 );
851 double ymin = bString.section( tupelSeparator, 0, 0 ).section( coordSeparator, 1, 1 ).toDouble( &ok2 );
852 double xmax = bString.section( tupelSeparator, 1, 1 ).section( coordSeparator, 0, 0 ).toDouble( &ok3 );
853 double ymax = bString.section( tupelSeparator, 1, 1 ).section( coordSeparator, 1, 1 ).toDouble( &ok4 );
855 if ( ok1 && ok2 && ok3 && ok4 )
867 QString coordSeparator =
" ";
868 QString tupelSeparator =
" ";
874 QStringList pos = elem.text().split(
" ", QString::SkipEmptyParts );
876 bool conversionSuccess;
877 int posSize = pos.size();
879 int srsDimension = 2;
880 if ( elem.hasAttribute(
"srsDimension" ) )
882 srsDimension = elem.attribute(
"srsDimension" ).toInt( &conversionSuccess );
883 if ( !conversionSuccess )
888 else if ( elem.hasAttribute(
"dimension" ) )
890 srsDimension = elem.attribute(
"dimension" ).toInt( &conversionSuccess );
891 if ( !conversionSuccess )
897 for (
int i = 0; i < posSize / srsDimension; i++ )
899 x = pos.at( i * srsDimension ).toDouble( &conversionSuccess );
900 if ( !conversionSuccess )
904 y = pos.at( i * srsDimension + 1 ).toDouble( &conversionSuccess );
905 if ( !conversionSuccess )
909 coords.push_back(
QgsPoint( x, y ) );
919 QDomElement envelopeElem = envelopeNode.toElement();
920 if ( envelopeElem.tagName() !=
"Envelope" )
923 QDomNodeList lowerCornerList = envelopeElem.elementsByTagNameNS(
GML_NAMESPACE,
"lowerCorner" );
924 if ( lowerCornerList.size() < 1 )
927 QDomNodeList upperCornerList = envelopeElem.elementsByTagNameNS(
GML_NAMESPACE,
"upperCorner" );
928 if ( upperCornerList.size() < 1 )
931 bool conversionSuccess;
932 int srsDimension = 2;
934 QDomElement elem = lowerCornerList.at( 0 ).toElement();
935 if ( elem.hasAttribute(
"srsDimension" ) )
937 srsDimension = elem.attribute(
"srsDimension" ).toInt( &conversionSuccess );
938 if ( !conversionSuccess )
943 else if ( elem.hasAttribute(
"dimension" ) )
945 srsDimension = elem.attribute(
"dimension" ).toInt( &conversionSuccess );
946 if ( !conversionSuccess )
951 QString bString = elem.text();
953 double xmin = bString.section(
" ", 0, 0 ).toDouble( &conversionSuccess );
954 if ( !conversionSuccess )
956 double ymin = bString.section(
" ", 1, 1 ).toDouble( &conversionSuccess );
957 if ( !conversionSuccess )
960 elem = upperCornerList.at( 0 ).toElement();
961 if ( elem.hasAttribute(
"srsDimension" ) )
963 srsDimension = elem.attribute(
"srsDimension" ).toInt( &conversionSuccess );
964 if ( !conversionSuccess )
969 else if ( elem.hasAttribute(
"dimension" ) )
971 srsDimension = elem.attribute(
"dimension" ).toInt( &conversionSuccess );
972 if ( !conversionSuccess )
978 Q_UNUSED( srsDimension );
980 bString = elem.text();
981 double xmax = bString.section(
" ", 0, 0 ).toDouble( &conversionSuccess );
982 if ( !conversionSuccess )
984 double ymax = bString.section(
" ", 1, 1 ).toDouble( &conversionSuccess );
985 if ( !conversionSuccess )
998 return QDomElement();
1001 QDomElement boxElem = doc.createElement(
"gml:Box" );
1002 QDomElement coordElem = doc.createElement(
"gml:coordinates" );
1003 coordElem.setAttribute(
"cs",
"," );
1004 coordElem.setAttribute(
"ts",
" " );
1006 QString coordString;
1015 QDomText coordText = doc.createTextNode( coordString );
1016 coordElem.appendChild( coordText );
1017 boxElem.appendChild( coordElem );
1026 return QDomElement();
1029 QDomElement envElem = doc.createElement(
"gml:Envelope" );
1032 QDomElement lowerCornerElem = doc.createElement(
"gml:lowerCorner" );
1036 QDomText lowerCornerText = doc.createTextNode( posList );
1037 lowerCornerElem.appendChild( lowerCornerText );
1038 envElem.appendChild( lowerCornerElem );
1040 QDomElement upperCornerElem = doc.createElement(
"gml:upperCorner" );
1044 QDomText upperCornerText = doc.createTextNode( posList );
1045 upperCornerElem.appendChild( upperCornerText );
1046 envElem.appendChild( upperCornerElem );
1053 if ( !geometry || !geometry->
asWkb() )
1054 return QDomElement();
1061 QDomElement baseCoordElem;
1063 bool hasZValue =
false;
1065 const unsigned char* wkb = geometry->
asWkb();
1067 if ( format ==
"GML3" )
1069 switch ( geometry->
wkbType() )
1075 baseCoordElem = doc.createElement(
"gml:pos" );;
1078 baseCoordElem = doc.createElement(
"gml:posList" );;
1081 baseCoordElem.setAttribute(
"srsDimension",
"2" );
1086 baseCoordElem = doc.createElement(
"gml:coordinates" );;
1087 baseCoordElem.setAttribute(
"cs", cs );
1088 baseCoordElem.setAttribute(
"ts", ts );
1091 switch ( geometry->
wkbType() )
1096 QDomElement pointElem = doc.createElement(
"gml:Point" );
1097 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1098 QString coordString;
1099 x = (
double * )( wkb + 5 );
1102 y = (
double * )( wkb + 5 +
sizeof(
double ) );
1104 QDomText coordText = doc.createTextNode( coordString );
1105 coordElem.appendChild( coordText );
1106 pointElem.appendChild( coordElem );
1113 const unsigned char *ptr;
1117 QDomElement multiPointElem = doc.createElement(
"gml:MultiPoint" );
1118 nPoints = (
int* )( wkb + 5 );
1119 ptr = wkb + 5 +
sizeof( int );
1120 for ( idx = 0; idx < *nPoints; ++idx )
1122 ptr += ( 1 +
sizeof( int ) );
1123 QDomElement pointMemberElem = doc.createElement(
"gml:pointMember" );
1124 QDomElement pointElem = doc.createElement(
"gml:Point" );
1125 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1126 QString coordString;
1127 x = (
double * )( ptr );
1130 ptr +=
sizeof( double );
1131 y = (
double * )( ptr );
1133 QDomText coordText = doc.createTextNode( coordString );
1134 coordElem.appendChild( coordText );
1135 pointElem.appendChild( coordElem );
1137 ptr +=
sizeof( double );
1140 ptr +=
sizeof( double );
1142 pointMemberElem.appendChild( pointElem );
1143 multiPointElem.appendChild( pointMemberElem );
1145 return multiPointElem;
1151 const unsigned char *ptr;
1155 QDomElement lineStringElem = doc.createElement(
"gml:LineString" );
1158 nPoints = (
int * ) ptr;
1159 ptr = wkb + 1 + 2 *
sizeof( int );
1160 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1161 QString coordString;
1162 for ( idx = 0; idx < *nPoints; ++idx )
1168 x = (
double * ) ptr;
1171 ptr +=
sizeof( double );
1172 y = (
double * ) ptr;
1174 ptr +=
sizeof( double );
1177 ptr +=
sizeof( double );
1180 QDomText coordText = doc.createTextNode( coordString );
1181 coordElem.appendChild( coordText );
1182 lineStringElem.appendChild( coordElem );
1183 return lineStringElem;
1189 const unsigned char *ptr;
1190 int idx, jdx, numLineStrings;
1193 QDomElement multiLineStringElem = doc.createElement(
"gml:MultiLineString" );
1194 numLineStrings = ( int )( wkb[5] );
1196 for ( jdx = 0; jdx < numLineStrings; jdx++ )
1198 QDomElement lineStringMemberElem = doc.createElement(
"gml:lineStringMember" );
1199 QDomElement lineStringElem = doc.createElement(
"gml:LineString" );
1201 nPoints = (
int * ) ptr;
1202 ptr +=
sizeof( int );
1203 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1204 QString coordString;
1205 for ( idx = 0; idx < *nPoints; idx++ )
1211 x = (
double * ) ptr;
1213 ptr +=
sizeof( double );
1215 y = (
double * ) ptr;
1217 ptr +=
sizeof( double );
1220 ptr +=
sizeof( double );
1223 QDomText coordText = doc.createTextNode( coordString );
1224 coordElem.appendChild( coordText );
1225 lineStringElem.appendChild( coordElem );
1226 lineStringMemberElem.appendChild( lineStringElem );
1227 multiLineStringElem.appendChild( lineStringMemberElem );
1229 return multiLineStringElem;
1235 const unsigned char *ptr;
1237 int *numRings, *nPoints;
1239 QDomElement polygonElem = doc.createElement(
"gml:Polygon" );
1241 numRings = (
int * )( wkb + 1 +
sizeof(
int ) );
1242 if ( !( *numRings ) )
1244 return QDomElement();
1248 ringStart =
new int[*numRings];
1249 ringNumPoints =
new int[*numRings];
1250 ptr = wkb + 1 + 2 *
sizeof( int );
1251 for ( idx = 0; idx < *numRings; idx++ )
1253 QString boundaryName =
"gml:outerBoundaryIs";
1256 boundaryName =
"gml:innerBoundaryIs";
1258 QDomElement boundaryElem = doc.createElement( boundaryName );
1259 QDomElement ringElem = doc.createElement(
"gml:LinearRing" );
1261 nPoints = (
int * ) ptr;
1262 ringNumPoints[idx] = *nPoints;
1264 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1265 QString coordString;
1266 for ( jdx = 0; jdx < *nPoints; jdx++ )
1272 x = (
double * ) ptr;
1275 ptr +=
sizeof( double );
1276 y = (
double * ) ptr;
1278 ptr +=
sizeof( double );
1281 ptr +=
sizeof( double );
1284 QDomText coordText = doc.createTextNode( coordString );
1285 coordElem.appendChild( coordText );
1286 ringElem.appendChild( coordElem );
1287 boundaryElem.appendChild( ringElem );
1288 polygonElem.appendChild( boundaryElem );
1290 delete [] ringStart;
1291 delete [] ringNumPoints;
1298 const unsigned char *ptr;
1300 int *numPolygons, *numRings, *nPoints;
1302 QDomElement multiPolygonElem = doc.createElement(
"gml:MultiPolygon" );
1304 numPolygons = (
int * ) ptr;
1306 for ( kdx = 0; kdx < *numPolygons; kdx++ )
1308 QDomElement polygonMemberElem = doc.createElement(
"gml:polygonMember" );
1309 QDomElement polygonElem = doc.createElement(
"gml:Polygon" );
1311 numRings = (
int * ) ptr;
1313 for ( idx = 0; idx < *numRings; idx++ )
1315 QString boundaryName =
"gml:outerBoundaryIs";
1318 boundaryName =
"gml:innerBoundaryIs";
1320 QDomElement boundaryElem = doc.createElement( boundaryName );
1321 QDomElement ringElem = doc.createElement(
"gml:LinearRing" );
1322 nPoints = (
int * ) ptr;
1324 QDomElement coordElem = baseCoordElem.cloneNode().toElement();
1325 QString coordString;
1326 for ( jdx = 0; jdx < *nPoints; jdx++ )
1332 x = (
double * ) ptr;
1334 ptr +=
sizeof( double );
1336 y = (
double * ) ptr;
1338 ptr +=
sizeof( double );
1341 ptr +=
sizeof( double );
1344 QDomText coordText = doc.createTextNode( coordString );
1345 coordElem.appendChild( coordText );
1346 ringElem.appendChild( coordElem );
1347 boundaryElem.appendChild( ringElem );
1348 polygonElem.appendChild( boundaryElem );
1349 polygonMemberElem.appendChild( polygonElem );
1350 multiPolygonElem.appendChild( polygonMemberElem );
1353 return multiPolygonElem;
1356 return QDomElement();
1367 QDomElement coordElem = doc.createElement(
"gml:coordinates" );
1368 coordElem.setAttribute(
"cs",
"," );
1369 coordElem.setAttribute(
"ts",
" " );
1371 QString coordString;
1372 QVector<QgsPoint>::const_iterator pointIt = points.constBegin();
1373 for ( ; pointIt != points.constEnd(); ++pointIt )
1375 if ( pointIt != points.constBegin() )
1384 QDomText coordText = doc.createTextNode( coordString );
1385 coordElem.appendChild( coordText );
1391 QDomElement posElem = doc.createElement(
"gml:pos" );
1392 if ( points.size() > 1 )
1393 posElem = doc.createElement(
"gml:posList" );
1394 posElem.setAttribute(
"srsDimension",
"2" );
1396 QString coordString;
1397 QVector<QgsPoint>::const_iterator pointIt = points.constBegin();
1398 for ( ; pointIt != points.constEnd(); ++pointIt )
1400 if ( pointIt != points.constBegin() )
1409 QDomText coordText = doc.createTextNode( coordString );
1410 posElem.appendChild( coordText );
1421 if ( element.isNull() || !element.hasChildNodes() )
1426 QDomElement childElem = element.firstChildElement();
1427 while ( !childElem.isNull() )
1448 childElem = childElem.nextSiblingElement();
1457 static QMap<QString, int> binOps;
1458 if ( binOps.isEmpty() )
1499 static QStringList spatialOps;
1500 if ( spatialOps.isEmpty() )
1502 spatialOps <<
"BBOX" <<
"Intersects" <<
"Contians" <<
"Crosses" <<
"Equals"
1503 <<
"Disjoint" <<
"Overlaps" <<
"Touches" <<
"Within";
1506 return spatialOps.contains( tagName );
1513 if ( element.isNull() )
1530 if ( element.tagName() ==
"Not" )
1534 else if ( element.tagName() ==
"PropertyIsNull" )
1538 else if ( element.tagName() ==
"Literal" )
1542 else if ( element.tagName() ==
"Function" )
1546 else if ( element.tagName() ==
"PropertyName" )
1550 else if ( element.tagName() ==
"PropertyIsBetween" )
1555 errorMessage += QString(
"unable to convert '%1' element to a valid expression: it is not supported yet or it has invalid arguments" ).arg( element.tagName() );
1563 if ( element.isNull() )
1569 if ( errorMessage.isEmpty() )
1570 errorMessage = QString(
"'%1' binary operator not supported." ).arg( element.tagName() );
1574 QDomElement operandElem = element.firstChildElement();
1578 if ( errorMessage.isEmpty() )
1579 errorMessage = QString(
"invalid left operand for '%1' binary operator" ).arg( element.tagName() );
1583 operandElem = operandElem.nextSiblingElement();
1587 if ( errorMessage.isEmpty() )
1588 errorMessage = QString(
"invalid right operand for '%1' binary operator" ).arg( element.tagName() );
1603 QDomElement childElem = element.firstChildElement();
1605 while ( !childElem.isNull() && gml2Str.isEmpty() )
1607 if ( childElem.tagName() !=
"PropertyName" )
1609 QTextStream gml2Stream( &gml2Str );
1610 childElem.save( gml2Stream, 0 );
1612 childElem = childElem.nextSiblingElement();
1614 if ( !gml2Str.isEmpty() )
1620 errorMessage = QString(
"No OGC Geometry found" );
1634 if ( element.tagName() !=
"Not" )
1637 QDomElement operandElem = element.firstChildElement();
1641 if ( errorMessage.isEmpty() )
1642 errorMessage = QString(
"invalid operand for '%1' unary operator" ).arg( element.tagName() );
1652 if ( element.isNull() || element.tagName() !=
"Function" )
1654 errorMessage = QString(
"ogc:Function expected, got %1" ).arg( element.tagName() );
1662 if ( element.attribute(
"name" ) != funcDef->
name() )
1667 QDomElement operandElem = element.firstChildElement();
1668 while ( !operandElem.isNull() )
1678 operandElem = operandElem.nextSiblingElement();
1691 if ( element.isNull() || element.tagName() !=
"Literal" )
1693 errorMessage = QString(
"ogc:Literal expected, got %1" ).arg( element.tagName() );
1700 QDomNode childNode = element.firstChild();
1701 while ( !childNode.isNull() )
1705 if ( childNode.nodeType() == QDomNode::ElementNode )
1708 QDomElement operandElem = childNode.toElement();
1715 errorMessage = QString(
"'%1' is an invalid or not supported content for ogc:Literal" ).arg( operandElem.tagName() );
1722 QVariant value = childNode.nodeValue();
1727 double d = value.toDouble( &ok );
1746 childNode = childNode.nextSibling();
1758 if ( element.isNull() || element.tagName() !=
"PropertyName" )
1760 errorMessage = QString(
"ogc:PropertyName expected, got %1" ).arg( element.tagName() );
1774 QDomElement operandElem = element.firstChildElement();
1775 while ( !operandElem.isNull() )
1777 if ( operandElem.tagName() ==
"LowerBoundary" )
1779 QDomElement lowerBoundElem = operandElem.firstChildElement();
1782 else if ( operandElem.tagName() ==
"UpperBoundary" )
1784 QDomElement upperBoundElem = operandElem.firstChildElement();
1796 if ( operand && lowerBound && operand2 && upperBound )
1799 operandElem = operandElem.nextSiblingElement();
1802 if ( !operand || !lowerBound || !operand2 || !upperBound )
1813 errorMessage =
"missing some required sub-elements in ogc:PropertyIsBetween";
1827 if ( element.tagName() !=
"PropertyIsNull" )
1832 QDomElement operandElem = element.firstChildElement();
1850 return QDomElement();
1852 QString localErrorMessage;
1853 QString& refErrorMessage = ( errorMessage ? *errorMessage : localErrorMessage );
1854 refErrorMessage.clear();
1857 if ( exprRootElem.isNull() )
1858 return QDomElement();
1860 QDomElement filterElem = doc.createElement(
"ogc:Filter" );
1861 filterElem.appendChild( exprRootElem );
1884 errorMessage = QString(
"Node type not supported: %1" ).arg( node->
nodeType() );
1885 return QDomElement();
1893 switch ( node->
op() )
1896 uoElem = doc.createElement(
"ogc:Literal" );
1897 uoElem.appendChild( doc.createTextNode(
"-" ) );
1900 uoElem = doc.createElement(
"ogc:Not" );
1905 return QDomElement();
1909 if ( !errorMessage.isEmpty() )
1910 return QDomElement();
1912 uoElem.appendChild( operandElem );
1920 if ( !errorMessage.isEmpty() )
1921 return QDomElement();
1931 if ( rightLit->
value().isNull() )
1934 QDomElement elem = doc.createElement(
"ogc:PropertyIsNull" );
1935 elem.appendChild( leftElem );
1939 QDomElement notElem = doc.createElement(
"ogc:Not" );
1940 notElem.appendChild( elem );
1954 if ( !errorMessage.isEmpty() )
1955 return QDomElement();
1959 if ( opText.isEmpty() )
1964 return QDomElement();
1967 QDomElement boElem = doc.createElement(
"ogc:" + opText );
1972 boElem.setAttribute(
"matchCase",
"false" );
1975 boElem.setAttribute(
"wildCard",
"%" );
1976 boElem.setAttribute(
"singleChar",
"?" );
1977 boElem.setAttribute(
"escapeChar",
"!" );
1980 boElem.appendChild( leftElem );
1981 boElem.appendChild( rightElem );
1989 switch ( node->
value().type() )
1992 value = QString::number( node->
value().toInt() );
1994 case QVariant::Double:
1995 value = QString::number( node->
value().toDouble() );
1997 case QVariant::String:
1998 value = node->
value().toString();
2002 errorMessage = QString(
"Literal type not supported: %1" ).arg( node->
value().type() );
2003 return QDomElement();
2006 QDomElement litElem = doc.createElement(
"ogc:Literal" );
2007 litElem.appendChild( doc.createTextNode( value ) );
2014 QDomElement propElem = doc.createElement(
"ogc:PropertyName" );
2015 propElem.appendChild( doc.createTextNode( node->
name() ) );
2023 if ( node->
list()->
list().size() == 1 )
2026 QDomElement orElem = doc.createElement(
"ogc:Or" );
2032 if ( !errorMessage.isEmpty() )
2033 return QDomElement();
2035 QDomElement eqElem = doc.createElement(
"ogc:PropertyIsEqualTo" );
2036 eqElem.appendChild( leftNode.cloneNode() );
2037 eqElem.appendChild( listNode );
2039 orElem.appendChild( eqElem );
2046 static QMap<QString, QString> binSpatialOps;
2047 if ( binSpatialOps.isEmpty() )
2049 binSpatialOps.insert(
"disjoint",
"Disjoint" );
2050 binSpatialOps.insert(
"intersects",
"Intersects" );
2051 binSpatialOps.insert(
"touches",
"Touches" );
2052 binSpatialOps.insert(
"crosses",
"Crosses" );
2053 binSpatialOps.insert(
"contains",
"Contains" );
2054 binSpatialOps.insert(
"overlaps",
"Overlaps" );
2055 binSpatialOps.insert(
"within",
"Within" );
2057 return binSpatialOps;
2077 return fd->
name() ==
"$geometry";
2085 if ( fd->
name() ==
"bbox" )
2087 errorMessage = QString(
"<BBOX> is currently not supported." );
2088 return QDomElement();
2093 QList<QgsExpression::Node*> argNodes = node->
args()->
list();
2094 Q_ASSERT( argNodes.count() == 2 );
2098 otherNode = argNodes[1];
2100 otherNode = argNodes[0];
2103 errorMessage = QString(
"Unable to translate spatial operator: at least one must refer to geometry." );
2104 return QDomElement();
2107 QDomElement otherGeomElem;
2112 errorMessage =
"spatial operator: the other operator must be a geometry constructor function";
2113 return QDomElement();
2118 if ( otherFnDef->
name() ==
"geomFromWKT" )
2123 errorMessage =
"geomFromWKT: argument must be string literal";
2124 return QDomElement();
2131 else if ( otherFnDef->
name() ==
"geomFromGML" )
2136 errorMessage =
"geomFromGML: argument must be string literal";
2137 return QDomElement();
2140 QDomDocument geomDoc;
2142 if ( !geomDoc.setContent( gml,
true ) )
2144 errorMessage =
"geomFromGML: unable to parse XML";
2145 return QDomElement();
2148 QDomNode geomNode = doc.importNode( geomDoc.documentElement(), true );
2149 otherGeomElem = geomNode.toElement();
2153 errorMessage =
"spatial operator: unknown geometry constructor function";
2154 return QDomElement();
2158 QDomElement geomProperty = doc.createElement(
"ogc:PropertyName" );
2159 geomProperty.appendChild( doc.createTextNode(
"geometry" ) );
2160 funcElem.appendChild( geomProperty );
2161 funcElem.appendChild( otherGeomElem );
2167 errorMessage = QString(
"Special columns / constants are not supported." );
2168 return QDomElement();
2172 QDomElement funcElem = doc.createElement(
"ogc:Function" );
2173 funcElem.setAttribute(
"name", fd->
name() );
2177 if ( !errorMessage.isEmpty() )
2178 return QDomElement();
2180 funcElem.appendChild( childElem );