16#include "moc_qgsgmlschema.cpp"
25#include <QNetworkRequest>
26#include <QNetworkReply>
27#include <QProgressDialog>
35#define GML_NAMESPACE QStringLiteral( "http://www.opengis.net/gml" )
46 for (
int i = 0; i < mFields.size(); i++ )
48 if ( mFields[i].name() == name )
return i;
55 : mSkipLevel( std::numeric_limits<int>::max() )
57 mGeometryTypes << QStringLiteral(
"Point" ) << QStringLiteral(
"MultiPoint" )
58 << QStringLiteral(
"LineString" ) << QStringLiteral(
"MultiLineString" )
59 << QStringLiteral(
"Polygon" ) << QStringLiteral(
"MultiPolygon" );
62QString QgsGmlSchema::readAttribute(
const QString &attributeName,
const XML_Char **attr )
const
67 if ( attributeName.compare( attr[i] ) == 0 )
69 return QString( attr[i + 1] );
82 if ( !dom.setContent( xml,
false, &errorMsg, &errorLine, &errorColumn ) )
88 const QDomElement docElem = dom.documentElement();
90 const QList<QDomElement> elementElements = domElements( docElem, QStringLiteral(
"element" ) );
94 const auto constElementElements = elementElements;
95 for (
const QDomElement &elementElement : constElementElements )
97 const QString name = elementElement.attribute( QStringLiteral(
"name" ) );
98 const QString type = elementElement.attribute( QStringLiteral(
"type" ) );
100 const QString gmlBaseType = xsdComplexTypeGmlBaseType( docElem, stripNS( type ) );
107 if ( gmlBaseType == QLatin1String(
"AbstractFeatureType" ) )
111 xsdFeatureClass( docElem, stripNS( type ), featureClass );
112 mFeatureClassMap.insert( name, featureClass );
123 const QDomElement complexTypeElement = domElement( element, QStringLiteral(
"complexType" ), QStringLiteral(
"name" ),
typeName );
124 if ( complexTypeElement.isNull() )
return false;
127 QDomElement extrest = domElement( complexTypeElement, QStringLiteral(
"complexContent.extension" ) );
128 if ( extrest.isNull() )
130 extrest = domElement( complexTypeElement, QStringLiteral(
"complexContent.restriction" ) );
132 if ( extrest.isNull() )
return false;
134 const QString extrestName = extrest.attribute( QStringLiteral(
"base" ) );
135 if ( extrestName == QLatin1String(
"gml:AbstractFeatureType" ) )
144 if ( !xsdFeatureClass( element, stripNS( extrestName ), featureClass ) )
return false;
148 QStringList geometryPropertyTypes;
149 const auto constMGeometryTypes = mGeometryTypes;
150 for (
const QString &geom : constMGeometryTypes )
152 geometryPropertyTypes << geom +
"PropertyType";
155 QStringList geometryAliases;
156 geometryAliases << QStringLiteral(
"location" ) << QStringLiteral(
"centerOf" ) << QStringLiteral(
"position" ) << QStringLiteral(
"extentOf" )
157 << QStringLiteral(
"coverage" ) << QStringLiteral(
"edgeOf" ) << QStringLiteral(
"centerLineOf" ) << QStringLiteral(
"multiLocation" )
158 << QStringLiteral(
"multiCenterOf" ) << QStringLiteral(
"multiPosition" ) << QStringLiteral(
"multiCenterLineOf" )
159 << QStringLiteral(
"multiEdgeOf" ) << QStringLiteral(
"multiCoverage" ) << QStringLiteral(
"multiExtentOf" );
162 const QList<QDomElement> sequenceElements = domElements( extrest, QStringLiteral(
"sequence.element" ) );
163 const auto constSequenceElements = sequenceElements;
164 for (
const QDomElement &sequenceElement : constSequenceElements )
166 const QString fieldName = sequenceElement.attribute( QStringLiteral(
"name" ) );
167 QString fieldTypeName = stripNS( sequenceElement.attribute( QStringLiteral(
"type" ) ) );
168 const QString ref = sequenceElement.attribute( QStringLiteral(
"ref" ) );
171 if ( !ref.isEmpty() )
173 if ( ref.startsWith( QLatin1String(
"gml:" ) ) )
175 if ( geometryAliases.contains( stripNS( ref ) ) )
181 QgsDebugError( QStringLiteral(
"Unknown referenced GML element: %1" ).arg( ref ) );
187 QgsDebugError( QStringLiteral(
"field %1.%2 is referencing %3 - not supported" ).arg(
typeName, fieldName ) );
192 if ( fieldName.isEmpty() )
199 if ( fieldTypeName.isEmpty() )
202 const QDomElement sequenceElementRestriction = domElement( sequenceElement, QStringLiteral(
"simpleType.restriction" ) );
203 fieldTypeName = stripNS( sequenceElementRestriction.attribute( QStringLiteral(
"base" ) ) );
206 QMetaType::Type fieldType = QMetaType::Type::QString;
207 if ( fieldTypeName.isEmpty() )
213 if ( geometryPropertyTypes.contains( fieldTypeName ) )
220 if ( fieldTypeName == QLatin1String(
"decimal" ) )
222 fieldType = QMetaType::Type::Double;
224 else if ( fieldTypeName == QLatin1String(
"integer" ) )
226 fieldType = QMetaType::Type::Int;
230 const QgsField field( fieldName, fieldType, fieldTypeName );
231 featureClass.
fields().append( field );
237QString QgsGmlSchema::xsdComplexTypeGmlBaseType(
const QDomElement &element,
const QString &name )
240 const QDomElement complexTypeElement = domElement( element, QStringLiteral(
"complexType" ), QStringLiteral(
"name" ), name );
241 if ( complexTypeElement.isNull() )
return QString();
243 QDomElement extrest = domElement( complexTypeElement, QStringLiteral(
"complexContent.extension" ) );
244 if ( extrest.isNull() )
246 extrest = domElement( complexTypeElement, QStringLiteral(
"complexContent.restriction" ) );
248 if ( extrest.isNull() )
return QString();
250 const QString extrestName = extrest.attribute( QStringLiteral(
"base" ) );
251 if ( extrestName.startsWith( QLatin1String(
"gml:" ) ) )
254 return stripNS( extrestName );
257 return xsdComplexTypeGmlBaseType( element, stripNS( extrestName ) );
260QString QgsGmlSchema::stripNS(
const QString &name )
262 return name.contains(
':' ) ? name.section(
':', 1 ) : name;
265QList<QDomElement> QgsGmlSchema::domElements(
const QDomElement &element,
const QString &path )
267 QList<QDomElement> list;
269 QStringList names = path.split(
'.' );
270 if ( names.isEmpty() )
return list;
271 const QString name = names.value( 0 );
274 QDomNode n1 = element.firstChild();
275 while ( !n1.isNull() )
277 const QDomElement el = n1.toElement();
280 const QString tagName = stripNS( el.tagName() );
281 if ( tagName == name )
283 if ( names.isEmpty() )
289 list.append( domElements( el, names.join( QLatin1Char(
'.' ) ) ) );
293 n1 = n1.nextSibling();
299QDomElement QgsGmlSchema::domElement(
const QDomElement &element,
const QString &path )
301 return domElements( element, path ).value( 0 );
304QList<QDomElement> QgsGmlSchema::domElements( QList<QDomElement> &elements,
const QString &attr,
const QString &attrVal )
306 QList<QDomElement> list;
307 const auto constElements = elements;
308 for (
const QDomElement &el : constElements )
310 if ( el.attribute( attr ) == attrVal )
318QDomElement QgsGmlSchema::domElement(
const QDomElement &element,
const QString &path,
const QString &attr,
const QString &attrVal )
320 QList<QDomElement> list = domElements( element, path );
321 return domElements( list, attr, attrVal ).value( 0 );
327 mSkipLevel = std::numeric_limits<int>::max();
328 XML_Parser p = XML_ParserCreateNS(
nullptr,
NS_SEPARATOR );
329 XML_SetUserData( p,
this );
330 XML_SetElementHandler( p, QgsGmlSchema::start, QgsGmlSchema::end );
331 XML_SetCharacterDataHandler( p, QgsGmlSchema::chars );
333 const int res = XML_Parse( p, data.constData(), data.size(), atEnd );
337 const QString err = QString( XML_ErrorString( XML_GetErrorCode( p ) ) );
338 QgsDebugError( QStringLiteral(
"XML_Parse returned %1 error %2" ).arg( res ).arg( err ) );
339 mError =
QgsError( err, QStringLiteral(
"GML schema" ) );
340 mError.
append( tr(
"Cannot guess schema" ) );
346void QgsGmlSchema::startElement(
const XML_Char *el,
const XML_Char **attr )
351 const QString elementName = QString::fromUtf8( el );
352 QgsDebugMsgLevel( QStringLiteral(
"-> %1 %2 %3" ).arg( mLevel ).arg( elementName, mLevel >= mSkipLevel ?
"skip" :
"" ), 5 );
354 if ( mLevel >= mSkipLevel )
360 mParsePathStack.append( elementName );
361 const QString path = mParsePathStack.join( QLatin1Char(
'.' ) );
363 QStringList splitName = elementName.split( NS_SEPARATOR );
364 const QString localName = splitName.last();
365 const QString ns = splitName.size() > 1 ? splitName.first() : QString();
368 const ParseMode parseMode = modeStackTop();
371 if ( ns ==
GML_NAMESPACE && localName == QLatin1String(
"boundedBy" ) )
374 mSkipLevel = mLevel + 1;
376 else if ( localName.compare( QLatin1String(
"featureMembers" ), Qt::CaseInsensitive ) == 0 )
378 mParseModeStack.push( QgsGmlSchema::FeatureMembers );
385 else if ( localName.endsWith( QLatin1String(
"member" ), Qt::CaseInsensitive ) )
387 mParseModeStack.push( QgsGmlSchema::FeatureMember );
390 else if ( elementName.endsWith( QLatin1String(
"_layer" ) ) )
398 else if ( elementName.endsWith( QLatin1String(
"_feature" ) )
399 || parseMode == QgsGmlSchema::FeatureMember
400 || parseMode == QgsGmlSchema::FeatureMembers
401 || localName.compare( QLatin1String(
"feature" ), Qt::CaseInsensitive ) == 0 )
404 if ( mFeatureClassMap.count( localName ) == 0 )
408 mCurrentFeatureName = localName;
409 mParseModeStack.push( QgsGmlSchema::Feature );
411 else if ( parseMode == QgsGmlSchema::Attribute && ns ==
GML_NAMESPACE && mGeometryTypes.indexOf( localName ) >= 0 )
414 QStringList &
geometryAttributes = mFeatureClassMap[mCurrentFeatureName].geometryAttributes();
419 mSkipLevel = mLevel + 1;
421 else if ( parseMode == QgsGmlSchema::Feature )
430 const QString name = readAttribute( QStringLiteral(
"name" ), attr );
432 if ( localName.compare( QLatin1String(
"attribute" ), Qt::CaseInsensitive ) == 0
435 const QString value = readAttribute( QStringLiteral(
"value" ), attr );
437 addAttribute( name, value );
441 mAttributeName = localName;
442 mParseModeStack.push( QgsGmlSchema::Attribute );
448void QgsGmlSchema::endElement(
const XML_Char *el )
450 const QString elementName = QString::fromUtf8( el );
451 QgsDebugMsgLevel( QStringLiteral(
"<- %1 %2" ).arg( mLevel ).arg( elementName ), 5 );
453 if ( mLevel >= mSkipLevel )
462 mSkipLevel = std::numeric_limits<int>::max();
465 QStringList splitName = elementName.split( NS_SEPARATOR );
466 const QString localName = splitName.last();
467 const QString ns = splitName.size() > 1 ? splitName.first() : QString();
469 const QgsGmlSchema::ParseMode parseMode = modeStackTop();
471 if ( parseMode == QgsGmlSchema::FeatureMembers )
475 else if ( parseMode == QgsGmlSchema::Attribute && localName == mAttributeName )
481 if ( mFeatureClassMap[mCurrentFeatureName].
geometryAttributes().count( mAttributeName ) == 0 )
483 addAttribute( mAttributeName, mStringCash );
486 else if ( ns ==
GML_NAMESPACE && localName == QLatin1String(
"boundedBy" ) )
490 else if ( localName.endsWith( QLatin1String(
"member" ), Qt::CaseInsensitive ) )
494 mParsePathStack.removeLast();
498void QgsGmlSchema::characters(
const XML_Char *chars,
int len )
501 if ( mLevel >= mSkipLevel )
508 if ( modeStackTop() == QgsGmlSchema::Attribute )
510 mStringCash.append( QString::fromUtf8( chars, len ) );
514void QgsGmlSchema::addAttribute(
const QString &name,
const QString &value )
518 ( void ) value.toInt( &ok );
519 QMetaType::Type type = QMetaType::Type::QString;
522 type = QMetaType::Type::Int;
526 ( void ) value.toDouble( &ok );
529 type = QMetaType::Type::Double;
534 QList<QgsField> &
fields = mFeatureClassMap[mCurrentFeatureName].fields();
535 const int fieldIndex = mFeatureClassMap[mCurrentFeatureName].fieldIndex( name );
536 if ( fieldIndex == -1 )
545 if ( ( field.
type() == QMetaType::Type::Int && ( type == QMetaType::Type::QString || type == QMetaType::Type::Double ) ) ||
546 ( field.
type() == QMetaType::Type::Double && type == QMetaType::Type::QString ) )
555 return mFeatureClassMap.keys();
560 if ( mFeatureClassMap.count(
typeName ) == 0 )
return QList<QgsField>();
561 return mFeatureClassMap[
typeName].fields();
566 if ( mFeatureClassMap.count(
typeName ) == 0 )
return QStringList();
567 return mFeatureClassMap[
typeName].geometryAttributes();
A container for error messages.
void append(const QString &message, const QString &tag)
Append new error message.
Encapsulate a field in an attribute table or data source.
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)