24 #include <QNetworkRequest> 25 #include <QNetworkReply> 26 #include <QProgressDialog> 34 const QString
GML_NAMESPACE = QStringLiteral(
"http://www.opengis.net/gml" );
45 for (
int i = 0; i < mFields.size(); i++ )
47 if ( mFields[i].name() == name )
return i;
54 : mSkipLevel( std::numeric_limits<int>::max() )
56 mGeometryTypes << QStringLiteral(
"Point" ) << QStringLiteral(
"MultiPoint" )
57 << QStringLiteral(
"LineString" ) << QStringLiteral(
"MultiLineString" )
58 << QStringLiteral(
"Polygon" ) << QStringLiteral(
"MultiPolygon" );
61 QString QgsGmlSchema::readAttribute(
const QString &attributeName,
const XML_Char **attr )
const 66 if ( attributeName.compare( attr[i] ) == 0 )
68 return QString( attr[i + 1] );
81 if ( !dom.setContent( xml,
false, &errorMsg, &errorLine, &errorColumn ) )
87 QDomElement docElem = dom.documentElement();
89 QList<QDomElement> elementElements = domElements( docElem, QStringLiteral(
"element" ) );
93 const auto constElementElements = elementElements;
94 for (
const QDomElement &elementElement : constElementElements )
96 QString name = elementElement.attribute( QStringLiteral(
"name" ) );
97 QString type = elementElement.attribute( QStringLiteral(
"type" ) );
99 QString gmlBaseType = xsdComplexTypeGmlBaseType( docElem, stripNS( type ) );
106 if ( gmlBaseType == QLatin1String(
"AbstractFeatureType" ) )
110 xsdFeatureClass( docElem, stripNS( type ), featureClass );
111 mFeatureClassMap.insert( name, featureClass );
122 QDomElement complexTypeElement = domElement( element, QStringLiteral(
"complexType" ), QStringLiteral(
"name" ),
typeName );
123 if ( complexTypeElement.isNull() )
return false;
126 QDomElement extrest = domElement( complexTypeElement, QStringLiteral(
"complexContent.extension" ) );
127 if ( extrest.isNull() )
129 extrest = domElement( complexTypeElement, QStringLiteral(
"complexContent.restriction" ) );
131 if ( extrest.isNull() )
return false;
133 QString extrestName = extrest.attribute( QStringLiteral(
"base" ) );
134 if ( extrestName == QLatin1String(
"gml:AbstractFeatureType" ) )
143 if ( !xsdFeatureClass( element, stripNS( extrestName ), featureClass ) )
return false;
147 QStringList geometryPropertyTypes;
148 const auto constMGeometryTypes = mGeometryTypes;
149 for (
const QString &geom : constMGeometryTypes )
151 geometryPropertyTypes << geom +
"PropertyType";
154 QStringList geometryAliases;
155 geometryAliases << QStringLiteral(
"location" ) << QStringLiteral(
"centerOf" ) << QStringLiteral(
"position" ) << QStringLiteral(
"extentOf" )
156 << QStringLiteral(
"coverage" ) << QStringLiteral(
"edgeOf" ) << QStringLiteral(
"centerLineOf" ) << QStringLiteral(
"multiLocation" )
157 << QStringLiteral(
"multiCenterOf" ) << QStringLiteral(
"multiPosition" ) << QStringLiteral(
"multiCenterLineOf" )
158 << QStringLiteral(
"multiEdgeOf" ) << QStringLiteral(
"multiCoverage" ) << QStringLiteral(
"multiExtentOf" );
161 QList<QDomElement> sequenceElements = domElements( extrest, QStringLiteral(
"sequence.element" ) );
162 const auto constSequenceElements = sequenceElements;
163 for (
const QDomElement &sequenceElement : constSequenceElements )
165 QString fieldName = sequenceElement.attribute( QStringLiteral(
"name" ) );
166 QString fieldTypeName = stripNS( sequenceElement.attribute( QStringLiteral(
"type" ) ) );
167 QString ref = sequenceElement.attribute( QStringLiteral(
"ref" ) );
170 if ( !ref.isEmpty() )
172 if ( ref.startsWith( QLatin1String(
"gml:" ) ) )
174 if ( geometryAliases.contains( stripNS( ref ) ) )
180 QgsDebugMsg( QStringLiteral(
"Unknown referenced GML element: %1" ).arg( ref ) );
186 QgsDebugMsg( QStringLiteral(
"field %1.%2 is referencing %3 - not supported" ).arg(
typeName, fieldName ) );
191 if ( fieldName.isEmpty() )
198 if ( fieldTypeName.isEmpty() )
201 QDomElement sequenceElementRestriction = domElement( sequenceElement, QStringLiteral(
"simpleType.restriction" ) );
202 fieldTypeName = stripNS( sequenceElementRestriction.attribute( QStringLiteral(
"base" ) ) );
205 QVariant::Type fieldType = QVariant::String;
206 if ( fieldTypeName.isEmpty() )
212 if ( geometryPropertyTypes.contains( fieldTypeName ) )
219 if ( fieldTypeName == QLatin1String(
"decimal" ) )
221 fieldType = QVariant::Double;
223 else if ( fieldTypeName == QLatin1String(
"integer" ) )
225 fieldType = QVariant::Int;
229 QgsField field( fieldName, fieldType, fieldTypeName );
230 featureClass.
fields().append( field );
236 QString QgsGmlSchema::xsdComplexTypeGmlBaseType(
const QDomElement &element,
const QString &name )
239 QDomElement complexTypeElement = domElement( element, QStringLiteral(
"complexType" ), QStringLiteral(
"name" ), name );
240 if ( complexTypeElement.isNull() )
return QString();
242 QDomElement extrest = domElement( complexTypeElement, QStringLiteral(
"complexContent.extension" ) );
243 if ( extrest.isNull() )
245 extrest = domElement( complexTypeElement, QStringLiteral(
"complexContent.restriction" ) );
247 if ( extrest.isNull() )
return QString();
249 QString extrestName = extrest.attribute( QStringLiteral(
"base" ) );
250 if ( extrestName.startsWith( QLatin1String(
"gml:" ) ) )
253 return stripNS( extrestName );
256 return xsdComplexTypeGmlBaseType( element, stripNS( extrestName ) );
259 QString QgsGmlSchema::stripNS(
const QString &name )
261 return name.contains(
':' ) ? name.section(
':', 1 ) : name;
264 QList<QDomElement> QgsGmlSchema::domElements(
const QDomElement &element,
const QString &path )
266 QList<QDomElement> list;
268 QStringList names = path.split(
'.' );
269 if ( names.isEmpty() )
return list;
270 QString name = names.value( 0 );
273 QDomNode n1 = element.firstChild();
274 while ( !n1.isNull() )
276 QDomElement el = n1.toElement();
279 QString tagName = stripNS( el.tagName() );
280 if ( tagName == name )
282 if ( names.isEmpty() )
288 list.append( domElements( el, names.join( QStringLiteral(
"." ) ) ) );
292 n1 = n1.nextSibling();
298 QDomElement QgsGmlSchema::domElement(
const QDomElement &element,
const QString &path )
300 return domElements( element, path ).value( 0 );
303 QList<QDomElement> QgsGmlSchema::domElements( QList<QDomElement> &elements,
const QString &attr,
const QString &attrVal )
305 QList<QDomElement> list;
306 const auto constElements = elements;
307 for (
const QDomElement &el : constElements )
309 if ( el.attribute( attr ) == attrVal )
317 QDomElement QgsGmlSchema::domElement(
const QDomElement &element,
const QString &path,
const QString &attr,
const QString &attrVal )
319 QList<QDomElement> list = domElements( element, path );
320 return domElements( list, attr, attrVal ).value( 0 );
326 mSkipLevel = std::numeric_limits<int>::max();
327 XML_Parser p = XML_ParserCreateNS(
nullptr,
NS_SEPARATOR );
328 XML_SetUserData( p,
this );
329 XML_SetElementHandler( p, QgsGmlSchema::start, QgsGmlSchema::end );
330 XML_SetCharacterDataHandler( p, QgsGmlSchema::chars );
332 int res = XML_Parse( p, data.constData(), data.size(), atEnd );
336 QString err = QString( XML_ErrorString( XML_GetErrorCode( p ) ) );
337 QgsDebugMsg( QStringLiteral(
"XML_Parse returned %1 error %2" ).arg( res ).arg( err ) );
338 mError =
QgsError( err, QStringLiteral(
"GML schema" ) );
339 mError.
append( tr(
"Cannot guess schema" ) );
345 void QgsGmlSchema::startElement(
const XML_Char *el,
const XML_Char **attr )
350 QString elementName = QString::fromUtf8( el );
351 QgsDebugMsgLevel( QStringLiteral(
"-> %1 %2 %3" ).arg( mLevel ).arg( elementName, mLevel >= mSkipLevel ?
"skip" :
"" ), 5 );
353 if ( mLevel >= mSkipLevel )
359 mParsePathStack.append( elementName );
360 QString path = mParsePathStack.join( QStringLiteral(
"." ) );
362 QStringList splitName = elementName.split(
NS_SEPARATOR );
363 QString localName = splitName.last();
364 QString ns = splitName.size() > 1 ? splitName.first() : QString();
367 ParseMode parseMode = modeStackTop();
370 if ( ns ==
GML_NAMESPACE && localName == QLatin1String(
"boundedBy" ) )
373 mSkipLevel = mLevel + 1;
375 else if ( localName.compare( QLatin1String(
"featureMembers" ), Qt::CaseInsensitive ) == 0 )
377 mParseModeStack.push( QgsGmlSchema::FeatureMembers );
384 else if ( localName.endsWith( QLatin1String(
"member" ), Qt::CaseInsensitive ) )
386 mParseModeStack.push( QgsGmlSchema::FeatureMember );
389 else if ( elementName.endsWith( QLatin1String(
"_layer" ) ) )
397 else if ( elementName.endsWith( QLatin1String(
"_feature" ) )
398 || parseMode == QgsGmlSchema::FeatureMember
399 || parseMode == QgsGmlSchema::FeatureMembers
400 || localName.compare( QLatin1String(
"feature" ), Qt::CaseInsensitive ) == 0 )
403 if ( mFeatureClassMap.count( localName ) == 0 )
407 mCurrentFeatureName = localName;
408 mParseModeStack.push( QgsGmlSchema::Feature );
410 else if ( parseMode == QgsGmlSchema::Attribute && ns ==
GML_NAMESPACE && mGeometryTypes.indexOf( localName ) >= 0 )
413 QStringList &
geometryAttributes = mFeatureClassMap[mCurrentFeatureName].geometryAttributes();
414 if ( geometryAttributes.count( mAttributeName ) == 0 )
416 geometryAttributes.append( mAttributeName );
418 mSkipLevel = mLevel + 1;
420 else if ( parseMode == QgsGmlSchema::Feature )
429 QString name = readAttribute( QStringLiteral(
"name" ), attr );
431 if ( localName.compare( QLatin1String(
"attribute" ), Qt::CaseInsensitive ) == 0
434 QString value = readAttribute( QStringLiteral(
"value" ), attr );
436 addAttribute( name, value );
440 mAttributeName = localName;
441 mParseModeStack.push( QgsGmlSchema::Attribute );
447 void QgsGmlSchema::endElement(
const XML_Char *el )
449 QString elementName = QString::fromUtf8( el );
450 QgsDebugMsgLevel( QStringLiteral(
"<- %1 %2" ).arg( mLevel ).arg( elementName ), 5 );
452 if ( mLevel >= mSkipLevel )
461 mSkipLevel = std::numeric_limits<int>::max();
464 QStringList splitName = elementName.split(
NS_SEPARATOR );
465 QString localName = splitName.last();
466 QString ns = splitName.size() > 1 ? splitName.first() : QString();
468 QgsGmlSchema::ParseMode parseMode = modeStackTop();
470 if ( parseMode == QgsGmlSchema::FeatureMembers )
474 else if ( parseMode == QgsGmlSchema::Attribute && localName == mAttributeName )
480 if ( mFeatureClassMap[mCurrentFeatureName].
geometryAttributes().count( mAttributeName ) == 0 )
482 addAttribute( mAttributeName, mStringCash );
485 else if ( ns ==
GML_NAMESPACE && localName == QLatin1String(
"boundedBy" ) )
489 else if ( localName.endsWith( QLatin1String(
"member" ), Qt::CaseInsensitive ) )
493 mParsePathStack.removeLast();
497 void QgsGmlSchema::characters(
const XML_Char *chars,
int len )
500 if ( mLevel >= mSkipLevel )
507 if ( modeStackTop() == QgsGmlSchema::Attribute )
509 mStringCash.append( QString::fromUtf8( chars, len ) );
513 void QgsGmlSchema::addAttribute(
const QString &name,
const QString &value )
518 QVariant::Type type = QVariant::String;
521 type = QVariant::Int;
525 value.toDouble( &ok );
528 type = QVariant::Double;
533 QList<QgsField> &
fields = mFeatureClassMap[mCurrentFeatureName].fields();
534 int fieldIndex = mFeatureClassMap[mCurrentFeatureName].fieldIndex( name );
535 if ( fieldIndex == -1 )
538 fields.append( field );
542 QgsField &field = fields[fieldIndex];
544 if ( ( field.
type() == QVariant::Int && ( type == QVariant::String || type == QVariant::Double ) ) ||
545 ( field.
type() == QVariant::Double && type == QVariant::String ) )
554 return mFeatureClassMap.keys();
559 if ( mFeatureClassMap.count( typeName ) == 0 )
return QList<QgsField>();
560 return mFeatureClassMap[
typeName].fields();
565 if ( mFeatureClassMap.count( typeName ) == 0 )
return QStringList();
566 return mFeatureClassMap[
typeName].geometryAttributes();
bool guessSchema(const QByteArray &data)
Guess GML schema from data if XSD does not exist.
const QString GML_NAMESPACE
QList< QgsField > & fields()
#define QgsDebugMsgLevel(str, level)
QList< QgsField > fields(const QString &typeName)
Gets fields for type/class name parsed from GML or XSD.
void append(const QString &message, const QString &tag)
Append new error message.
Encapsulate a field in an attribute table or data source.
Description of feature class in GML.
void setType(QVariant::Type type)
Set variant type.
QgsError is container for error messages (report).
QStringList & geometryAttributes()
QgsGmlFeatureClass()=default
Constructor for QgsGmlFeatureClass.
QStringList typeNames() const
Gets list of dot separated paths to feature classes parsed from GML or XSD.
int fieldIndex(const QString &name)
bool parseXSD(const QByteArray &xml)
Gets fields info from XSD.
QStringList geometryAttributes(const QString &typeName)
Gets list of geometry attributes for type/class name.