28#include <QNetworkReply>
29#include <QNetworkRequest>
30#include <QProgressDialog>
36#include "moc_qgsgmlschema.cpp"
38using namespace Qt::StringLiterals;
40#ifndef NS_SEPARATOR_DEFINED
41#define NS_SEPARATOR_DEFINED
42static const char NS_SEPARATOR =
'?';
45#define GML_NAMESPACE u"http://www.opengis.net/gml"_s
55 for (
int i = 0; i < mFields.size(); i++ )
57 if ( mFields[i].name() == name )
65 : mSkipLevel( std::numeric_limits<int>::max() )
67 mGeometryTypes << u
"Point"_s << u
"MultiPoint"_s << u
"LineString"_s << u
"MultiLineString"_s << u
"Polygon"_s << u
"MultiPolygon"_s;
70QString QgsGmlSchema::readAttribute(
const QString &attributeName,
const XML_Char **attr )
const
75 if ( attributeName.compare( attr[i] ) == 0 )
77 return QString( attr[i + 1] );
90 if ( !dom.setContent( xml,
false, &errorMsg, &errorLine, &errorColumn ) )
96 const QDomElement docElem = dom.documentElement();
98 const QList<QDomElement> elementElements = domElements( docElem, u
"element"_s );
102 const auto constElementElements = elementElements;
103 for (
const QDomElement &elementElement : constElementElements )
105 const QString name = elementElement.attribute( u
"name"_s );
106 const QString type = elementElement.attribute( u
"type"_s );
108 const QString gmlBaseType = xsdComplexTypeGmlBaseType( docElem, stripNS( type ) );
115 if ( gmlBaseType ==
"AbstractFeatureType"_L1 )
119 xsdFeatureClass( docElem, stripNS( type ), featureClass );
120 mFeatureClassMap.insert( name, featureClass );
128bool QgsGmlSchema::xsdFeatureClass(
const QDomElement &element,
const QString &typeName,
QgsGmlFeatureClass &featureClass )
131 const QDomElement complexTypeElement = domElement( element, u
"complexType"_s, u
"name"_s, typeName );
132 if ( complexTypeElement.isNull() )
136 QDomElement extrest = domElement( complexTypeElement, u
"complexContent.extension"_s );
137 if ( extrest.isNull() )
139 extrest = domElement( complexTypeElement, u
"complexContent.restriction"_s );
141 if ( extrest.isNull() )
144 const QString extrestName = extrest.attribute( u
"base"_s );
145 if ( extrestName ==
"gml:AbstractFeatureType"_L1 )
154 if ( !xsdFeatureClass( element, stripNS( extrestName ), featureClass ) )
159 QStringList geometryPropertyTypes;
160 const auto constMGeometryTypes = mGeometryTypes;
161 for (
const QString &geom : constMGeometryTypes )
163 geometryPropertyTypes << geom +
"PropertyType";
166 QStringList geometryAliases;
175 << u
"multiLocation"_s
176 << u
"multiCenterOf"_s
177 << u
"multiPosition"_s
178 << u
"multiCenterLineOf"_s
180 << u
"multiCoverage"_s
181 << u
"multiExtentOf"_s;
184 const QList<QDomElement> sequenceElements = domElements( extrest, u
"sequence.element"_s );
185 const auto constSequenceElements = sequenceElements;
186 for (
const QDomElement &sequenceElement : constSequenceElements )
188 const QString fieldName = sequenceElement.attribute( u
"name"_s );
189 QString fieldTypeName = stripNS( sequenceElement.attribute( u
"type"_s ) );
190 const QString ref = sequenceElement.attribute( u
"ref"_s );
193 if ( !ref.isEmpty() )
195 if ( ref.startsWith(
"gml:"_L1 ) )
197 if ( geometryAliases.contains( stripNS( ref ) ) )
203 QgsDebugError( u
"Unknown referenced GML element: %1"_s.arg( ref ) );
209 QgsDebugError( u
"field %1.%2 is referencing %3 - not supported"_s.arg( typeName, fieldName ) );
214 if ( fieldName.isEmpty() )
216 QgsDebugError( u
"field in %1 without name"_s.arg( typeName ) );
221 if ( fieldTypeName.isEmpty() )
224 const QDomElement sequenceElementRestriction = domElement( sequenceElement, u
"simpleType.restriction"_s );
225 fieldTypeName = stripNS( sequenceElementRestriction.attribute( u
"base"_s ) );
228 QMetaType::Type fieldType = QMetaType::Type::QString;
229 if ( fieldTypeName.isEmpty() )
231 QgsDebugError( u
"Cannot get %1.%2 field type"_s.arg( typeName, fieldName ) );
235 if ( geometryPropertyTypes.contains( fieldTypeName ) )
242 if ( fieldTypeName ==
"decimal"_L1 )
244 fieldType = QMetaType::Type::Double;
246 else if ( fieldTypeName ==
"integer"_L1 )
248 fieldType = QMetaType::Type::Int;
252 const QgsField field( fieldName, fieldType, fieldTypeName );
253 featureClass.
fields().append( field );
259QString QgsGmlSchema::xsdComplexTypeGmlBaseType(
const QDomElement &element,
const QString &name )
262 const QDomElement complexTypeElement = domElement( element, u
"complexType"_s, u
"name"_s, name );
263 if ( complexTypeElement.isNull() )
266 QDomElement extrest = domElement( complexTypeElement, u
"complexContent.extension"_s );
267 if ( extrest.isNull() )
269 extrest = domElement( complexTypeElement, u
"complexContent.restriction"_s );
271 if ( extrest.isNull() )
274 const QString extrestName = extrest.attribute( u
"base"_s );
275 if ( extrestName.startsWith(
"gml:"_L1 ) )
278 return stripNS( extrestName );
281 return xsdComplexTypeGmlBaseType( element, stripNS( extrestName ) );
284QString QgsGmlSchema::stripNS(
const QString &name )
286 return name.contains(
':' ) ? name.section(
':', 1 ) : name;
289QList<QDomElement> QgsGmlSchema::domElements(
const QDomElement &element,
const QString &path )
291 QList<QDomElement> list;
293 QStringList names = path.split(
'.' );
294 if ( names.isEmpty() )
296 const QString name = names.value( 0 );
299 QDomNode n1 = element.firstChild();
300 while ( !n1.isNull() )
302 const QDomElement el = n1.toElement();
305 const QString tagName = stripNS( el.tagName() );
306 if ( tagName == name )
308 if ( names.isEmpty() )
314 list.append( domElements( el, names.join(
'.'_L1 ) ) );
318 n1 = n1.nextSibling();
324QDomElement QgsGmlSchema::domElement(
const QDomElement &element,
const QString &path )
326 return domElements( element, path ).value( 0 );
329QList<QDomElement> QgsGmlSchema::domElements( QList<QDomElement> &elements,
const QString &attr,
const QString &attrVal )
331 QList<QDomElement> list;
332 const auto constElements = elements;
333 for (
const QDomElement &el : constElements )
335 if ( el.attribute( attr ) == attrVal )
343QDomElement QgsGmlSchema::domElement(
const QDomElement &element,
const QString &path,
const QString &attr,
const QString &attrVal )
345 QList<QDomElement> list = domElements( element, path );
346 return domElements( list, attr, attrVal ).value( 0 );
352 mSkipLevel = std::numeric_limits<int>::max();
353 XML_Parser p = XML_ParserCreateNS(
nullptr, NS_SEPARATOR );
354 XML_SetUserData( p,
this );
355 XML_SetElementHandler( p, QgsGmlSchema::start, QgsGmlSchema::end );
356 XML_SetCharacterDataHandler( p, QgsGmlSchema::chars );
358 const int res = XML_Parse( p, data.constData(), data.size(), atEnd );
362 const QString err = QString( XML_ErrorString( XML_GetErrorCode( p ) ) );
363 QgsDebugError( u
"XML_Parse returned %1 error %2"_s.arg( res ).arg( err ) );
364 mError =
QgsError( err, u
"GML schema"_s );
365 mError.append( tr(
"Cannot guess schema" ) );
371void QgsGmlSchema::startElement(
const XML_Char *el,
const XML_Char **attr )
376 const QString elementName = QString::fromUtf8( el );
377 QgsDebugMsgLevel( u
"-> %1 %2 %3"_s.arg( mLevel ).arg( elementName, mLevel >= mSkipLevel ?
"skip" :
"" ), 5 );
379 if ( mLevel >= mSkipLevel )
385 mParsePathStack.append( elementName );
386 const QString path = mParsePathStack.join(
'.'_L1 );
388 QStringList splitName = elementName.split( NS_SEPARATOR );
389 const QString localName = splitName.last();
390 const QString ns = splitName.size() > 1 ? splitName.first() : QString();
393 const ParseMode parseMode = modeStackTop();
399 mSkipLevel = mLevel + 1;
401 else if ( localName.compare(
"featureMembers"_L1, Qt::CaseInsensitive ) == 0 )
403 mParseModeStack.push( QgsGmlSchema::FeatureMembers );
410 else if ( localName.endsWith(
"member"_L1, Qt::CaseInsensitive ) )
412 mParseModeStack.push( QgsGmlSchema::FeatureMember );
415 else if ( elementName.endsWith(
"_layer"_L1 ) )
423 else if ( elementName.endsWith(
"_feature"_L1 ) || parseMode == QgsGmlSchema::FeatureMember || parseMode == QgsGmlSchema::FeatureMembers || localName.compare(
"feature"_L1, Qt::CaseInsensitive ) == 0 )
426 if ( mFeatureClassMap.count( localName ) == 0 )
428 mFeatureClassMap.insert( localName, QgsGmlFeatureClass( localName, path ) );
430 mCurrentFeatureName = localName;
431 mParseModeStack.push( QgsGmlSchema::Feature );
433 else if ( parseMode == QgsGmlSchema::Attribute && ns ==
GML_NAMESPACE && mGeometryTypes.indexOf( localName ) >= 0 )
436 QStringList &
geometryAttributes = mFeatureClassMap[mCurrentFeatureName].geometryAttributes();
441 mSkipLevel = mLevel + 1;
443 else if ( parseMode == QgsGmlSchema::Feature )
452 const QString name = readAttribute( u
"name"_s, attr );
454 if ( localName.compare(
"attribute"_L1, Qt::CaseInsensitive ) == 0 && !name.isEmpty() )
456 const QString value = readAttribute( u
"value"_s, attr );
458 addAttribute( name, value );
462 mAttributeName = localName;
463 mParseModeStack.push( QgsGmlSchema::Attribute );
469void QgsGmlSchema::endElement(
const XML_Char *el )
471 const QString elementName = QString::fromUtf8( el );
474 if ( mLevel >= mSkipLevel )
483 mSkipLevel = std::numeric_limits<int>::max();
486 QStringList splitName = elementName.split( NS_SEPARATOR );
487 const QString localName = splitName.last();
488 const QString ns = splitName.size() > 1 ? splitName.first() : QString();
490 const QgsGmlSchema::ParseMode parseMode = modeStackTop();
492 if ( parseMode == QgsGmlSchema::FeatureMembers )
496 else if ( parseMode == QgsGmlSchema::Attribute && localName == mAttributeName )
502 if ( mFeatureClassMap[mCurrentFeatureName].
geometryAttributes().count( mAttributeName ) == 0 )
504 addAttribute( mAttributeName, mStringCash );
507 else if ( ns ==
GML_NAMESPACE && localName ==
"boundedBy"_L1 )
511 else if ( localName.endsWith(
"member"_L1, Qt::CaseInsensitive ) )
515 mParsePathStack.removeLast();
519void QgsGmlSchema::characters(
const XML_Char *chars,
int len )
522 if ( mLevel >= mSkipLevel )
529 if ( modeStackTop() == QgsGmlSchema::Attribute )
531 mStringCash.append( QString::fromUtf8( chars, len ) );
535void QgsGmlSchema::addAttribute(
const QString &name,
const QString &value )
539 ( void ) value.toInt( &ok );
540 QMetaType::Type type = QMetaType::Type::QString;
543 type = QMetaType::Type::Int;
547 ( void ) value.toDouble( &ok );
550 type = QMetaType::Type::Double;
555 QList<QgsField> &
fields = mFeatureClassMap[mCurrentFeatureName].fields();
556 const int fieldIndex = mFeatureClassMap[mCurrentFeatureName].fieldIndex( name );
557 if ( fieldIndex == -1 )
559 const QgsField field( name, type );
564 QgsField &field =
fields[fieldIndex];
566 if ( ( field.
type() == QMetaType::Type::Int && ( type == QMetaType::Type::QString || type == QMetaType::Type::Double ) )
567 || ( field.
type() == QMetaType::Type::Double && type == QMetaType::Type::QString ) )
576 return mFeatureClassMap.keys();
581 if ( mFeatureClassMap.count( typeName ) == 0 )
582 return QList<QgsField>();
583 return mFeatureClassMap[typeName].fields();
588 if ( mFeatureClassMap.count( typeName ) == 0 )
589 return QStringList();
590 return mFeatureClassMap[typeName].geometryAttributes();
A container for error messages.
void setType(QMetaType::Type type)
Set variant type.
Description of feature class in GML.
int fieldIndex(const QString &name)
QStringList & geometryAttributes()
QList< QgsField > & fields()
QgsGmlFeatureClass()=default
bool parseXSD(const QByteArray &xml)
Gets fields info from XSD.
QList< QgsField > fields(const QString &typeName)
Gets fields for type/class name parsed from GML or XSD.
QStringList geometryAttributes(const QString &typeName)
Gets list of geometry attributes for type/class name.
bool guessSchema(const QByteArray &data)
Guess GML schema from data if XSD does not exist.
QStringList typeNames() const
Gets list of dot separated paths to feature classes parsed from GML or XSD.
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)