24#include <QNetworkRequest> 
   25#include <QNetworkReply> 
   26#include <QProgressDialog> 
   34#define 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" );
 
   61QString 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  const QDomElement docElem = dom.documentElement();
 
   89  const QList<QDomElement> elementElements = domElements( docElem, QStringLiteral( 
"element" ) );
 
   93  const auto constElementElements = elementElements;
 
   94  for ( 
const QDomElement &elementElement : constElementElements )
 
   96    const QString name = elementElement.attribute( QStringLiteral( 
"name" ) );
 
   97    const QString type = elementElement.attribute( QStringLiteral( 
"type" ) );
 
   99    const QString gmlBaseType = xsdComplexTypeGmlBaseType( docElem, stripNS( type ) );
 
  106    if ( gmlBaseType == QLatin1String( 
"AbstractFeatureType" ) )
 
  110      xsdFeatureClass( docElem, stripNS( type ), featureClass );
 
  111      mFeatureClassMap.insert( name, featureClass );
 
  122  const 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  const 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  const QList<QDomElement> sequenceElements = domElements( extrest, QStringLiteral( 
"sequence.element" ) );
 
  162  const auto constSequenceElements = sequenceElements;
 
  163  for ( 
const QDomElement &sequenceElement : constSequenceElements )
 
  165    const QString fieldName = sequenceElement.attribute( QStringLiteral( 
"name" ) );
 
  166    QString fieldTypeName = stripNS( sequenceElement.attribute( QStringLiteral( 
"type" ) ) );
 
  167    const QString ref = sequenceElement.attribute( QStringLiteral( 
"ref" ) );
 
  170    if ( !ref.isEmpty() )
 
  172      if ( ref.startsWith( QLatin1String( 
"gml:" ) ) )
 
  174        if ( geometryAliases.contains( stripNS( ref ) ) )
 
  180          QgsDebugError( QStringLiteral( 
"Unknown referenced GML element: %1" ).arg( ref ) );
 
  186        QgsDebugError( QStringLiteral( 
"field %1.%2 is referencing %3 - not supported" ).arg( 
typeName, fieldName ) );
 
  191    if ( fieldName.isEmpty() )
 
  198    if ( fieldTypeName.isEmpty() )
 
  201      const 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;
 
  236QString QgsGmlSchema::xsdComplexTypeGmlBaseType( 
const QDomElement &element, 
const QString &name )
 
  239  const 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  const QString extrestName = extrest.attribute( QStringLiteral( 
"base" ) );
 
  250  if ( extrestName.startsWith( QLatin1String( 
"gml:" ) ) )
 
  253    return stripNS( extrestName );
 
  256  return xsdComplexTypeGmlBaseType( element, stripNS( extrestName ) );
 
  259QString QgsGmlSchema::stripNS( 
const QString &name )
 
  261  return name.contains( 
':' ) ? name.section( 
':', 1 ) : name;
 
  264QList<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  const QString name = names.value( 0 );
 
  273  QDomNode n1 = element.firstChild();
 
  274  while ( !n1.isNull() )
 
  276    const QDomElement el = n1.toElement();
 
  279      const QString tagName = stripNS( el.tagName() );
 
  280      if ( tagName == name )
 
  282        if ( names.isEmpty() )
 
  288          list.append( domElements( el, names.join( QLatin1Char( 
'.' ) ) ) );
 
  292    n1 = n1.nextSibling();
 
  298QDomElement QgsGmlSchema::domElement( 
const QDomElement &element, 
const QString &path )
 
  300  return domElements( element, path ).value( 0 );
 
  303QList<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 )
 
  317QDomElement 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  const int res = XML_Parse( p, data.constData(), data.size(), atEnd );
 
  336    const QString err = QString( XML_ErrorString( XML_GetErrorCode( p ) ) );
 
  337    QgsDebugError( 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" ) );
 
  345void QgsGmlSchema::startElement( 
const XML_Char *el, 
const XML_Char **attr )
 
  350  const 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  const QString path = mParsePathStack.join( QLatin1Char( 
'.' ) );
 
  362  QStringList splitName = elementName.split( 
NS_SEPARATOR );
 
  363  const QString localName = splitName.last();
 
  364  const QString ns = splitName.size() > 1 ? splitName.first() : QString();
 
  367  const 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();
 
  418    mSkipLevel = mLevel + 1; 
 
  420  else if ( parseMode == QgsGmlSchema::Feature )
 
  429    const QString name = readAttribute( QStringLiteral( 
"name" ), attr );
 
  431    if ( localName.compare( QLatin1String( 
"attribute" ), Qt::CaseInsensitive ) == 0
 
  434      const QString value = readAttribute( QStringLiteral( 
"value" ), attr );
 
  436      addAttribute( name, value );
 
  440      mAttributeName = localName;
 
  441      mParseModeStack.push( QgsGmlSchema::Attribute );
 
  447void QgsGmlSchema::endElement( 
const XML_Char *el )
 
  449  const 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  const QString localName = splitName.last();
 
  466  const QString ns = splitName.size() > 1 ? splitName.first() : QString();
 
  468  const 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();
 
  497void QgsGmlSchema::characters( 
const XML_Char *chars, 
int len )
 
  500  if ( mLevel >= mSkipLevel )
 
  507  if ( modeStackTop() == QgsGmlSchema::Attribute )
 
  509    mStringCash.append( QString::fromUtf8( chars, len ) );
 
  513void QgsGmlSchema::addAttribute( 
const QString &name, 
const QString &value )
 
  517  ( void ) value.toInt( &ok );
 
  518  QVariant::Type type = QVariant::String;
 
  521    type = QVariant::Int;
 
  525    ( void ) value.toDouble( &ok );
 
  528      type = QVariant::Double;
 
  533  QList<QgsField> &
fields = mFeatureClassMap[mCurrentFeatureName].fields();
 
  534  const int fieldIndex = mFeatureClassMap[mCurrentFeatureName].fieldIndex( name );
 
  535  if ( fieldIndex == -1 )
 
  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();
 
QgsError is container for error messages (report).
 
void append(const QString &message, const QString &tag)
Append new error message.
 
Encapsulate a field in an attribute table or data source.
 
void setType(QVariant::Type type)
Set variant type.
 
Description of feature class in GML.
 
int fieldIndex(const QString &name)
 
QStringList & geometryAttributes()
 
QList< QgsField > & fields()
 
QgsGmlFeatureClass()=default
Constructor for QgsGmlFeatureClass.
 
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)