24 #include <QNetworkRequest>
25 #include <QNetworkReply>
26 #include <QProgressDialog>
37 const QString& typeName,
38 const QString& geometryAttribute,
41 , mTypeName( typeName )
42 , mGeometryAttribute( geometryAttribute )
44 , mCurrentFeature( 0 )
46 , mCurrentWKBSize( 0 )
50 for (
int i = 0; i < fields.
size(); i++ )
58 if ( index != -1 && index <
mTypeName.length() )
74 XML_SetUserData( p,
this );
81 QNetworkRequest request(
mUri );
84 connect( reply, SIGNAL( finished() ),
this, SLOT(
setFinished() ) );
85 connect( reply, SIGNAL( downloadProgress( qint64, qint64 ) ),
this, SLOT(
handleProgressEvent( qint64, qint64 ) ) );
88 QProgressDialog* progressDialog = 0;
89 QWidget* mainWindow = 0;
90 QWidgetList topLevelWidgets = qApp->topLevelWidgets();
91 for ( QWidgetList::iterator it = topLevelWidgets.begin(); it != topLevelWidgets.end(); ++it )
93 if (( *it )->objectName() ==
"QgisApp" )
101 progressDialog =
new QProgressDialog(
tr(
"Loading GML data\n%1" ).arg(
mTypeName ),
tr(
"Abort" ), 0, 0, mainWindow );
102 progressDialog->setWindowModality( Qt::ApplicationModal );
103 connect(
this, SIGNAL(
dataReadProgress(
int ) ), progressDialog, SLOT( setValue(
int ) ) );
104 connect(
this, SIGNAL(
totalStepsUpdate(
int ) ), progressDialog, SLOT( setMaximum(
int ) ) );
105 connect( progressDialog, SIGNAL( canceled() ),
this, SLOT(
setFinished() ) );
106 progressDialog->show();
116 QByteArray readData = reply->readAll();
117 if ( readData.size() > 0 )
119 if ( XML_Parse( p, readData.constData(), readData.size(), atEnd ) == 0 )
121 XML_Error errorCode = XML_GetErrorCode( p );
122 QString errorString =
tr(
"Error: %1 on line %2, column %3" )
123 .arg( XML_ErrorString( errorCode ) )
124 .arg( XML_GetCurrentLineNumber( p ) )
125 .arg( XML_GetCurrentColumnNumber( p ) );
129 QCoreApplication::processEvents();
132 QNetworkReply::NetworkError replyError = reply->error();
133 QString replyErrorString = reply->errorString();
136 delete progressDialog;
141 tr(
"GML Getfeature network request failed with error: %1" ).arg( replyErrorString ),
170 XML_Parser p = XML_ParserCreateNS( NULL,
NS_SEPARATOR );
171 XML_SetUserData( p,
this );
175 XML_Parse( p, data.constData(), data.size(), atEnd );
190 if ( totalSteps < 0 )
202 QString elementName( QString::fromUtf8( el ) );
204 QStringList splitName = elementName.split(
NS_SEPARATOR );
205 QString localName = splitName.last();
206 QString ns = splitName.size() > 1 ? splitName.first() :
"";
233 if ( dimension.isEmpty() || !ok )
297 if (
mEpsg == 0 && ( localName ==
"Point" || localName ==
"MultiPoint" ||
298 localName ==
"LineString" || localName ==
"MultiLineString" ||
299 localName ==
"Polygon" || localName ==
"MultiPolygon" ) )
314 QString elementName( QString::fromUtf8( el ) );
316 QStringList splitName = elementName.split(
NS_SEPARATOR );
317 QString localName = splitName.last();
318 QString ns = splitName.size() > 1 ? splitName.first() :
"";
321 || ( theParseMode ==
posList && (
336 switch ( att_it.value().second.type() )
338 case QVariant::Double:
344 case QVariant::LongLong:
397 QList<QgsPoint> pointList;
418 unsigned char* wkb = 0;
420 QList<unsigned char*> wkbList;
421 QList<int> wkbSizeList;
422 if (
getPointWKB( &wkb, &wkbSize, *( pointList.begin() ) ) != 0 )
441 QList<QgsPoint> pointList;
460 unsigned char* wkb = 0;
462 QList<unsigned char*> wkbList;
463 QList<int> wkbSizeList;
464 if (
getLineWKB( &wkb, &wkbSize, pointList ) != 0 )
481 QList<QgsPoint> pointList;
486 unsigned char* wkb = 0;
488 if (
getRingWKB( &wkb, &wkbSize, pointList ) != 0 )
545 mStringCash.append( QString::fromUtf8( chars, len ) );
552 while ( attr[i] != NULL )
554 if ( strcmp( attr[i],
"srsName" ) == 0 )
556 QString epsgString( attr[i+1] );
557 QString epsgNrString;
558 if ( epsgString.startsWith(
"http" ) )
560 epsgNrString = epsgString.section(
"#", 1, 1 );
564 epsgNrString = epsgString.section(
":", 1, 1 );
567 int eNr = epsgNrString.toInt( &conversionOk );
583 while ( attr[i] != NULL )
585 if ( attributeName.compare( attr[i] ) == 0 )
587 return QString( attr[i+1] );
596 QList<QgsPoint> points;
602 if ( points.size() < 2 )
607 r.
set( points[0], points[1] );
615 QStringList tuples = coordString.split(
mTupleSeparator, QString::SkipEmptyParts );
616 QStringList tuples_coordinates;
618 bool conversionSuccess;
620 QStringList::const_iterator tupleIterator;
621 for ( tupleIterator = tuples.constBegin(); tupleIterator != tuples.constEnd(); ++tupleIterator )
624 if ( tuples_coordinates.size() < 2 )
628 x = tuples_coordinates.at( 0 ).toDouble( &conversionSuccess );
629 if ( !conversionSuccess )
633 y = tuples_coordinates.at( 1 ).toDouble( &conversionSuccess );
634 if ( !conversionSuccess )
638 points.push_back(
QgsPoint( x, y ) );
646 QStringList coordinates = coordString.split(
" ", QString::SkipEmptyParts );
648 if ( coordinates.size() % dimension != 0 )
653 int ncoor = coordinates.size() / dimension;
654 for (
int i = 0; i < ncoor; i++ )
656 bool conversionSuccess;
657 double x = coordinates.value( i * dimension ).toDouble( &conversionSuccess );
658 if ( !conversionSuccess )
662 double y = coordinates.value( i * dimension + 1 ).toDouble( &conversionSuccess );
663 if ( !conversionSuccess )
687 int wkbSize = 1 +
sizeof( int ) + 2 *
sizeof(
double );
689 *wkb =
new unsigned char[wkbSize];
691 double x = point.
x();
692 double y = point.
y();
695 memcpy( &( *wkb )[wkbPosition], &
mEndian, 1 );
697 memcpy( &( *wkb )[wkbPosition], &type,
sizeof(
int ) );
698 wkbPosition +=
sizeof( int );
699 memcpy( &( *wkb )[wkbPosition], &x,
sizeof(
double ) );
700 wkbPosition +=
sizeof( double );
701 memcpy( &( *wkb )[wkbPosition], &y,
sizeof(
double ) );
707 int wkbSize = 1 + 2 *
sizeof( int ) + lineCoordinates.size() * 2 *
sizeof( double );
709 *wkb =
new unsigned char[wkbSize];
713 int nPoints = lineCoordinates.size();
716 memcpy( &( *wkb )[wkbPosition], &
mEndian, 1 );
718 memcpy( &( *wkb )[wkbPosition], &type,
sizeof(
int ) );
719 wkbPosition +=
sizeof( int );
720 memcpy( &( *wkb )[wkbPosition], &nPoints,
sizeof(
int ) );
721 wkbPosition +=
sizeof( int );
723 QList<QgsPoint>::const_iterator iter;
724 for ( iter = lineCoordinates.begin(); iter != lineCoordinates.end(); ++iter )
728 memcpy( &( *wkb )[wkbPosition], &x,
sizeof(
double ) );
729 wkbPosition +=
sizeof( double );
730 memcpy( &( *wkb )[wkbPosition], &y,
sizeof(
double ) );
731 wkbPosition +=
sizeof( double );
738 int wkbSize =
sizeof( int ) + ringCoordinates.size() * 2 *
sizeof( double );
740 *wkb =
new unsigned char[wkbSize];
743 int nPoints = ringCoordinates.size();
744 memcpy( &( *wkb )[wkbPosition], &nPoints,
sizeof(
int ) );
745 wkbPosition +=
sizeof( int );
747 QList<QgsPoint>::const_iterator iter;
748 for ( iter = ringCoordinates.begin(); iter != ringCoordinates.end(); ++iter )
752 memcpy( &( *wkb )[wkbPosition], &x,
sizeof(
double ) );
753 wkbPosition +=
sizeof( double );
754 memcpy( &( *wkb )[wkbPosition], &y,
sizeof(
double ) );
755 wkbPosition +=
sizeof( double );
773 memcpy( &(
mCurrentWKB[pos] ), &type,
sizeof(
int ) );
774 pos +=
sizeof( int );
775 memcpy( &(
mCurrentWKB[pos] ), &numLines,
sizeof(
int ) );
776 pos +=
sizeof( int );
807 memcpy( &(
mCurrentWKB[pos] ), &type,
sizeof(
int ) );
808 pos +=
sizeof( int );
809 memcpy( &(
mCurrentWKB[pos] ), &numPoints,
sizeof(
int ) );
810 pos +=
sizeof( int );
841 memcpy( &(
mCurrentWKB[pos] ), &type,
sizeof(
int ) );
842 pos +=
sizeof( int );
843 memcpy( &(
mCurrentWKB[pos] ), &numRings,
sizeof(
int ) );
844 pos +=
sizeof( int );
876 memcpy( &(
mCurrentWKB[pos] ), &type,
sizeof(
int ) );
877 pos +=
sizeof( int );
878 memcpy( &(
mCurrentWKB[pos] ), &numPolys,
sizeof(
int ) );
879 pos +=
sizeof( int );
882 QList< QList<unsigned char*> >::iterator outerWkbIt;
883 QList< QList<int> >::iterator outerSizeIt;
884 QList< unsigned char* >::iterator innerWkbIt;
885 QList< int >::iterator innerSizeIt;
895 memcpy( &(
mCurrentWKB[pos] ), &polygonType,
sizeof(
int ) );
896 pos +=
sizeof( int );
897 numRings = outerWkbIt->size();
898 memcpy( &(
mCurrentWKB[pos] ), &numRings,
sizeof(
int ) );
899 pos +=
sizeof( int );
901 innerWkbIt = outerWkbIt->begin();
902 innerSizeIt = outerSizeIt->begin();
903 for ( ; innerWkbIt != outerWkbIt->end(); ++innerWkbIt, ++innerSizeIt )
905 memcpy( &(
mCurrentWKB[pos] ), *innerWkbIt, *innerSizeIt );
907 delete[] *innerWkbIt;
922 foreach (
int i, list )
939 bool bboxInitialised =
false;
941 for (
int i = 0; i <
mFeatures.size(); ++i )
944 if ( !currentFeature )
948 currentGeometry = currentFeature->
geometry();
949 if ( currentGeometry )
951 if ( !bboxInitialised )
954 bboxInitialised =
true;