27 #include <QNetworkRequest> 28 #include <QNetworkReply> 29 #include <QProgressDialog> 41 const QString& geometryAttribute,
44 , mTypeName( typeName )
45 , mGeometryAttribute( geometryAttribute )
48 , mCurrentFeature( nullptr )
50 , mCurrentWKB( nullptr, 0 )
52 , mCoorMode(
QgsGml::coordinate )
55 mThematicAttributes.
clear();
56 for (
int i = 0; i < fields.
size(); i++ )
58 mThematicAttributes.
insert( fields[i].
name(), qMakePair( i, fields[i] ) );
64 if ( index != -1 && index < mTypeName.
length() )
66 mTypeName = mTypeName.
mid( index + 1 );
79 XML_Parser p = XML_ParserCreateNS(
nullptr,
NS_SEPARATOR );
80 XML_SetUserData( p,
this );
81 XML_SetElementHandler( p, QgsGml::start, QgsGml::end );
82 XML_SetCharacterDataHandler( p, QgsGml::chars );
93 tr(
"GML Getfeature network request update failed for authcfg %1" ).arg( authcfg ),
102 request.
setRawHeader(
"Authorization",
"Basic " +
QString(
"%1:%2" ).arg( userName, password ).toAscii().toBase64() );
106 connect( reply, SIGNAL( finished() ),
this, SLOT( setFinished() ) );
107 connect( reply, SIGNAL( downloadProgress( qint64, qint64 ) ),
this, SLOT( handleProgressEvent( qint64, qint64 ) ) );
112 QWidgetList topLevelWidgets = qApp->topLevelWidgets();
113 for ( QWidgetList::const_iterator it = topLevelWidgets.constBegin(); it != topLevelWidgets.constEnd(); ++it )
115 if (( *it )->objectName() ==
"QgisApp" )
123 progressDialog =
new QProgressDialog(
tr(
"Loading GML data\n%1" ).arg( mTypeName ),
tr(
"Abort" ), 0, 0, mainWindow );
127 connect( progressDialog, SIGNAL( canceled() ),
this, SLOT( setFinished() ) );
128 progressDialog->
show();
141 if ( XML_Parse( p, readData.
constData(), readData.
size(), atEnd ) == 0 )
143 XML_Error errorCode = XML_GetErrorCode( p );
144 QString errorString =
tr(
"Error: %1 on line %2, column %3" )
145 .
arg( XML_ErrorString( errorCode ) )
146 .
arg( XML_GetCurrentLineNumber( p ) )
147 .
arg( XML_GetCurrentColumnNumber( p ) );
154 QNetworkReply::NetworkError replyError = reply->
error();
158 delete progressDialog;
163 tr(
"GML Getfeature network request failed with error: %1" ).arg( replyErrorString ),
175 calculateExtentFromFeatures();
192 XML_Parser p = XML_ParserCreateNS(
nullptr,
NS_SEPARATOR );
193 XML_SetUserData( p,
this );
194 XML_SetElementHandler( p, QgsGml::start, QgsGml::end );
195 XML_SetCharacterDataHandler( p, QgsGml::chars );
205 void QgsGml::setFinished()
210 void QgsGml::handleProgressEvent( qint64 progress, qint64 totalSteps )
212 if ( totalSteps < 0 )
222 void QgsGml::startElement(
const XML_Char* el,
const XML_Char** attr )
225 ParseMode theParseMode( mParseModeStack.isEmpty() ? none : mParseModeStack.
top() );
227 QString localName = splitName.last();
228 QString ns = splitName.
size() > 1 ? splitName.first() :
"";
230 if ( elementName == GML_NAMESPACE +
NS_SEPARATOR +
"coordinates" )
232 mParseModeStack.
push( QgsGml::coordinate );
233 mCoorMode = QgsGml::coordinate;
235 mCoordinateSeparator = readAttribute(
"cs", attr );
236 if ( mCoordinateSeparator.
isEmpty() )
238 mCoordinateSeparator =
',';
240 mTupleSeparator = readAttribute(
"ts", attr );
241 if ( mTupleSeparator.
isEmpty() )
243 mTupleSeparator =
' ';
246 if ( elementName == GML_NAMESPACE +
NS_SEPARATOR +
"pos" 247 || elementName == GML_NAMESPACE +
NS_SEPARATOR +
"posList" )
249 mParseModeStack.
push( QgsGml::posList );
250 mCoorMode = QgsGml::posList;
252 QString dimension = readAttribute(
"srsDimension", attr );
254 mDimension = dimension.
toInt( &ok );
255 if ( dimension.
isEmpty() || !ok )
260 else if ( localName == mGeometryAttribute )
262 mParseModeStack.
push( QgsGml::geometry );
265 else if ( elementName == GML_NAMESPACE +
NS_SEPARATOR +
"boundedBy" )
267 mParseModeStack.
push( QgsGml::boundingBox );
269 else if ( theParseMode == none && localName == mTypeName )
271 Q_ASSERT( !mCurrentFeature );
272 mCurrentFeature =
new QgsFeature( mFeatureCount );
275 mParseModeStack.
push( QgsGml::feature );
276 mCurrentFeatureId = readAttribute(
"fid", attr );
279 else if ( theParseMode == boundingBox && elementName == GML_NAMESPACE +
NS_SEPARATOR +
"Box" )
283 if ( readEpsgFromAttribute( epsgNr, attr ) != 0 )
288 else if ( elementName == GML_NAMESPACE +
NS_SEPARATOR +
"Polygon" )
292 else if ( elementName == GML_NAMESPACE +
NS_SEPARATOR +
"MultiPoint" )
294 mParseModeStack.
push( QgsGml::multiPoint );
298 else if ( elementName == GML_NAMESPACE +
NS_SEPARATOR +
"MultiLineString" )
300 mParseModeStack.
push( QgsGml::multiLine );
304 else if ( elementName == GML_NAMESPACE +
NS_SEPARATOR +
"MultiPolygon" )
306 mParseModeStack.
push( QgsGml::multiPolygon );
308 else if ( theParseMode == feature && mThematicAttributes.
contains( localName ) )
310 mParseModeStack.
push( QgsGml::attribute );
311 mAttributeName = localName;
316 else if ( theParseMode == feature
317 && localName.
compare(
"attribute", Qt::CaseInsensitive ) == 0 )
320 if ( mThematicAttributes.
contains( name ) )
322 QString value = readAttribute(
"value", attr );
323 setAttribute( name, value );
327 if ( mEpsg == 0 && ( localName ==
"Point" || localName ==
"MultiPoint" ||
328 localName ==
"LineString" || localName ==
"MultiLineString" ||
329 localName ==
"Polygon" || localName ==
"MultiPolygon" ) )
331 if ( readEpsgFromAttribute( mEpsg, attr ) != 0 )
342 void QgsGml::endElement(
const XML_Char* el )
345 ParseMode theParseMode( mParseModeStack.isEmpty() ? none : mParseModeStack.
top() );
347 QString localName = splitName.last();
348 QString ns = splitName.
size() > 1 ? splitName.first() :
"";
350 if (( theParseMode == coordinate && elementName == GML_NAMESPACE +
NS_SEPARATOR +
"coordinates" )
351 || ( theParseMode == posList && (
353 || elementName == GML_NAMESPACE +
NS_SEPARATOR +
"posList" ) ) )
355 mParseModeStack.
pop();
357 else if ( theParseMode == attribute && localName == mAttributeName )
359 mParseModeStack.
pop();
361 setAttribute( mAttributeName, mStringCash );
363 else if ( theParseMode == geometry && localName == mGeometryAttribute )
365 mParseModeStack.
pop();
367 else if ( theParseMode == boundingBox && elementName == GML_NAMESPACE +
NS_SEPARATOR +
"boundedBy" )
370 if ( createBBoxFromCoordinateString( mCurrentExtent, mStringCash ) != 0 )
375 mParseModeStack.
pop();
377 else if ( theParseMode == feature && localName == mTypeName )
379 Q_ASSERT( mCurrentFeature );
380 if ( mCurrentWKB.
size() > 0 )
387 else if ( !mCurrentExtent.
isEmpty() )
397 mFeatures.
insert( mCurrentFeature->
id(), mCurrentFeature );
398 if ( !mCurrentFeatureId.
isEmpty() )
400 mIdMap.
insert( mCurrentFeature->
id(), mCurrentFeatureId );
402 mCurrentFeature =
nullptr;
404 mParseModeStack.
pop();
406 else if ( elementName == GML_NAMESPACE +
NS_SEPARATOR +
"Point" )
409 if ( pointsFromString( pointList, mStringCash ) != 0 )
417 if ( theParseMode == QgsGml::geometry )
420 if ( getPointWKB( mCurrentWKB, *( pointList.
constBegin() ) ) != 0 )
433 if ( getPointWKB( wkbPtr, *( pointList.
constBegin() ) ) != 0 )
437 if ( !mCurrentWKBFragments.
isEmpty() )
439 mCurrentWKBFragments.
last().push_back( wkbPtr );
448 else if ( elementName == GML_NAMESPACE +
NS_SEPARATOR +
"LineString" )
453 if ( pointsFromString( pointList, mStringCash ) != 0 )
457 if ( theParseMode == QgsGml::geometry )
459 if ( getLineWKB( mCurrentWKB, pointList ) != 0 )
472 if ( getLineWKB( wkbPtr, pointList ) != 0 )
476 if ( !mCurrentWKBFragments.
isEmpty() )
478 mCurrentWKBFragments.
last().push_back( wkbPtr );
487 else if (( theParseMode == geometry || theParseMode == multiPolygon ) && elementName == GML_NAMESPACE +
NS_SEPARATOR +
"LinearRing" )
490 if ( pointsFromString( pointList, mStringCash ) != 0 )
496 if ( getRingWKB( wkbPtr, pointList ) != 0 )
501 if ( !mCurrentWKBFragments.
isEmpty() )
503 mCurrentWKBFragments.
last().push_back( wkbPtr );
511 else if (( theParseMode == geometry || theParseMode == multiPolygon ) && elementName == GML_NAMESPACE +
NS_SEPARATOR +
"Polygon" )
518 if ( theParseMode == geometry )
520 createPolygonFromFragments();
523 else if ( theParseMode == multiPoint && elementName == GML_NAMESPACE +
NS_SEPARATOR +
"MultiPoint" )
526 mParseModeStack.
pop();
527 createMultiPointFromFragments();
529 else if ( theParseMode == multiLine && elementName == GML_NAMESPACE +
NS_SEPARATOR +
"MultiLineString" )
532 mParseModeStack.
pop();
533 createMultiLineFromFragments();
535 else if ( theParseMode == multiPolygon && elementName == GML_NAMESPACE +
NS_SEPARATOR +
"MultiPolygon" )
538 mParseModeStack.
pop();
539 createMultiPolygonFromFragments();
543 void QgsGml::characters(
const XML_Char* chars,
int len )
546 if ( mParseModeStack.isEmpty() )
551 QgsGml::ParseMode theParseMode = mParseModeStack.
top();
552 if ( theParseMode == QgsGml::attribute || theParseMode == QgsGml::coordinate || theParseMode == QgsGml::posList )
562 if ( att_it != mThematicAttributes.
constEnd() )
565 switch ( att_it.
value().second.type() )
567 case QVariant::Double:
573 case QVariant::LongLong:
580 Q_ASSERT( mCurrentFeature );
585 int QgsGml::readEpsgFromAttribute(
int& epsgNr,
const XML_Char** attr )
const 590 if ( strcmp( attr[i],
"srsName" ) == 0 )
592 QString epsgString( attr[i+1] );
596 epsgNrString = epsgString.
section(
'#', 1, 1 );
600 epsgNrString = epsgString.
section(
':', 1, 1 );
603 int eNr = epsgNrString.
toInt( &conversionOk );
616 QString QgsGml::readAttribute(
const QString& attributeName,
const XML_Char** attr )
const 621 if ( attributeName.
compare( attr[i] ) == 0 )
630 int QgsGml::createBBoxFromCoordinateString(
QgsRectangle &r,
const QString& coordString )
const 633 if ( pointsFromCoordinateString( points, coordString ) != 0 )
638 if ( points.
size() < 2 )
643 r.
set( points[0], points[1] );
651 QStringList tuples = coordString.
split( mTupleSeparator, QString::SkipEmptyParts );
654 bool conversionSuccess;
657 for ( tupleIterator = tuples.
constBegin(); tupleIterator != tuples.
constEnd(); ++tupleIterator )
659 tuples_coordinates = tupleIterator->split( mCoordinateSeparator, QString::SkipEmptyParts );
660 if ( tuples_coordinates.
size() < 2 )
664 x = tuples_coordinates.
at( 0 ).toDouble( &conversionSuccess );
665 if ( !conversionSuccess )
669 y = tuples_coordinates.
at( 1 ).toDouble( &conversionSuccess );
670 if ( !conversionSuccess )
679 int QgsGml::pointsFromPosListString(
QList<QgsPoint>& points,
const QString& coordString,
int dimension )
const 682 QStringList coordinates = coordString.
split(
' ', QString::SkipEmptyParts );
684 if ( coordinates.
size() % dimension != 0 )
689 int ncoor = coordinates.
size() / dimension;
690 for (
int i = 0; i < ncoor; i++ )
692 bool conversionSuccess;
693 double x = coordinates.
value( i * dimension ).toDouble( &conversionSuccess );
694 if ( !conversionSuccess )
698 double y = coordinates.
value( i * dimension + 1 ).toDouble( &conversionSuccess );
699 if ( !conversionSuccess )
710 if ( mCoorMode == QgsGml::coordinate )
712 return pointsFromCoordinateString( points, coordString );
714 else if ( mCoorMode == QgsGml::posList )
716 return pointsFromPosListString( points, coordString, mDimension );
723 int wkbSize = 1 +
sizeof( int ) + 2 *
sizeof(
double );
724 wkbPtr =
QgsWkbPtr(
new unsigned char[wkbSize], wkbSize );
734 int wkbSize = 1 + 2 *
sizeof( int ) + lineCoordinates.
size() * 2 *
sizeof( double );
735 wkbPtr =
QgsWkbPtr(
new unsigned char[wkbSize], wkbSize );
742 for ( iter = lineCoordinates.
constBegin(); iter != lineCoordinates.
constEnd(); ++iter )
744 fillPtr << iter->x() << iter->y();
752 int wkbSize =
sizeof( int ) + ringCoordinates.
size() * 2 *
sizeof( double );
753 wkbPtr =
QgsWkbPtr(
new unsigned char[wkbSize], wkbSize );
757 fillPtr << ringCoordinates.
size();
760 for ( iter = ringCoordinates.
constBegin(); iter != ringCoordinates.
constEnd(); ++iter )
762 fillPtr << iter->x() << iter->y();
768 int QgsGml::createMultiLineFromFragments()
770 int size = 1 + 2 *
sizeof( int ) + totalWKBFragmentSize();
771 mCurrentWKB =
QgsWkbPtr(
new unsigned char[size], size );
779 for ( ; wkbIt != mCurrentWKBFragments.
constBegin()->constEnd(); ++wkbIt )
781 memcpy( wkbPtr, *wkbIt, wkbIt->size() );
782 wkbPtr += wkbIt->size();
786 mCurrentWKBFragments.
clear();
791 int QgsGml::createMultiPointFromFragments()
793 int size = 1 + 2 *
sizeof( int ) + totalWKBFragmentSize();
794 mCurrentWKB =
QgsWkbPtr(
new unsigned char[size], size );
800 for ( ; wkbIt != mCurrentWKBFragments.
constBegin()->constEnd(); ++wkbIt )
802 memcpy( wkbPtr, *wkbIt, wkbIt->size() );
803 wkbPtr += wkbIt->size();
807 mCurrentWKBFragments.
clear();
813 int QgsGml::createPolygonFromFragments()
815 int size = 1 + 2 *
sizeof( int ) + totalWKBFragmentSize();
816 mCurrentWKB =
QgsWkbPtr(
new unsigned char[size], size );
822 for ( ; wkbIt != mCurrentWKBFragments.
constBegin()->constEnd(); ++wkbIt )
824 memcpy( wkbPtr, *wkbIt, wkbIt->size() );
825 wkbPtr += wkbIt->size();
829 mCurrentWKBFragments.
clear();
834 int QgsGml::createMultiPolygonFromFragments()
837 size += 1 + 2 *
sizeof( int );
838 size += totalWKBFragmentSize();
839 size += mCurrentWKBFragments.
size() * ( 1 + 2 *
sizeof( int ) );
841 mCurrentWKB =
QgsWkbPtr(
new unsigned char[size], size );
849 for ( ; outerWkbIt != mCurrentWKBFragments.
constEnd(); ++outerWkbIt )
855 for ( ; innerWkbIt != outerWkbIt->
constEnd(); ++innerWkbIt )
857 memcpy( wkbPtr, *innerWkbIt, innerWkbIt->size() );
858 wkbPtr += innerWkbIt->size();
859 delete[] *innerWkbIt;
863 mCurrentWKBFragments.
clear();
868 int QgsGml::totalWKBFragmentSize()
const 881 void QgsGml::calculateExtentFromFeatures()
883 if ( mFeatures.
size() < 1 )
890 bool bboxInitialised =
false;
892 for (
int i = 0; i < mFeatures.
size(); ++i )
894 currentFeature = mFeatures[i];
895 if ( !currentFeature )
900 if ( currentGeometry )
902 if ( !bboxInitialised )
905 bboxInitialised =
true;
QgsFeatureId id() const
Get the feature ID for this feature.
void unionRect(const QgsRectangle &rect)
Updates rectangle to include passed argument.
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
A rectangle specified with double values.
QString & append(QChar ch)
bool isEmpty() const
test if rectangle is empty.
void setMinimal()
Set a rectangle so that min corner is at max and max corner is at min.
bool contains(const Key &key) const
static QgsAuthManager * instance()
Enforce singleton pattern.
void push_back(const T &value)
void fromWkb(unsigned char *wkb, int length)
Set the geometry, feeding in the buffer containing OGC Well-Known Binary and the buffer's length...
QString errorString() const
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
const T & at(int i) const
QgsRectangle boundingBox() const
Returns the bounding box of this feature.
Container of fields for a vector layer.
A geometry is the spatial representation of a feature.
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
bool setAttribute(int field, const QVariant &attr)
Set an attribute's value by field index.
WkbType
Used for symbology operations.
void dataReadProgress(int progress)
const_iterator constFind(const Key &key) const
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
double toDouble(bool *ok) const
static endian_t endian()
Returns whether this machine uses big or little endian.
QString tr(const char *sourceText, const char *disambiguation, int n)
double x() const
Get the x value of the point.
void totalStepsUpdate(int totalSteps)
void set(const QgsPoint &p1, const QgsPoint &p2)
Set the rectangle from two QgsPoints.
bool createFromOgcWmsCrs(QString theCrs)
Set up this CRS from the given OGC CRS.
const char * name() const
void setGeometry(const QgsGeometry &geom)
Set this feature's geometry from another QgsGeometry object.
void processEvents(QFlags< QEventLoop::ProcessEventsFlag > flags)
void append(const T &value)
QString fromUtf8(const char *str, int size)
int getFeatures(const QString &uri, QGis::WkbType *wkbType, QgsRectangle *extent=nullptr, const QString &userName=QString(), const QString &password=QString(), const QString &authcfg=QString())
Does the Http GET request to the wfs server Supports only UTF-8, UTF-16, ISO-8859-1, ISO-8859-1 XML encodings.
void dataProgressAndSteps(int progress, int totalSteps)
This class reads data from a WFS server or alternatively from a GML file.
QgsGml(const QString &typeName, const QString &geometryAttribute, const QgsFields &fields)
int toInt(bool *ok, int base) const
const_iterator constEnd() const
const char * constData() const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
static void logMessage(const QString &message, const QString &tag=QString::null, MessageLevel level=WARNING)
add a message to the instance (and create it if necessary)
A class to represent a point.
const QString GML_NAMESPACE
void setValid(bool validity)
Sets the validity of the feature.
QString mid(int position, int n) const
bool updateNetworkRequest(QNetworkRequest &request, const QString &authcfg, const QString &dataprovider=QString())
Provider call to update a QNetworkRequest with an authentication config.
static QgsNetworkAccessManager * instance()
returns a pointer to the single instance
Class for storing a coordinate reference system (CRS)
int size() const
Return number of items.
const QgsGeometry * constGeometry() const
Gets a const pointer to the geometry object associated with this feature.
static QgsGeometry * fromRect(const QgsRectangle &rect)
Creates a new geometry from a QgsRectangle.
QString section(QChar sep, int start, int end, QFlags< QString::SectionFlag > flags) const
NetworkError error() const
double y() const
Get the y value of the point.
iterator insert(const Key &key, const T &value)
QNetworkReply * get(const QNetworkRequest &request)
const_iterator constEnd() const
const_iterator constBegin() const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
int compare(const QString &other) const
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
qlonglong toLongLong(bool *ok, int base) const
QgsCoordinateReferenceSystem crs() const
Returns features spatial reference system.
const T value(const Key &key) const