Exemplo n.º 1
0
void QgsProjectParser::layersAndStylesCapabilities( QDomElement& parentElement, QDomDocument& doc ) const
{
  QList<QDomElement> layerElems = projectLayerElements();

  QStringList nonIdentifiableLayers = identifyDisabledLayers();

  if ( layerElems.size() < 1 )
  {
    return;
  }

  QMap<QString, QgsMapLayer *> layerMap;

  QList<QDomElement>::const_iterator layerIt = layerElems.constBegin();
  for ( ; layerIt != layerElems.constEnd(); ++layerIt )
  {
    QgsMapLayer *layer = createLayerFromElement( *layerIt );
    if ( layer )
    {
      QgsDebugMsg( QString( "add layer %1 to map" ).arg( layer->id() ) );
      layerMap.insert( layer->id(), layer );
    }
#if QGSMSDEBUG
    else
    {
      QString buf;
      QTextStream s( &buf );
      layerIt->save( s, 0 );
      QgsDebugMsg( QString( "layer %1 not found" ).arg( buf ) );
    }
#endif
  }

  //According to the WMS spec, there can be only one toplevel layer.
  //So we create an artificial one here to be in accordance with the schema
  QString projTitle = projectTitle();
  QDomElement layerParentElem = doc.createElement( "Layer" );
  layerParentElem.setAttribute( "queryable", "1" );
  QDomElement layerParentNameElem = doc.createElement( "Name" );
  QDomText layerParentNameText = doc.createTextNode( projTitle );
  layerParentNameElem.appendChild( layerParentNameText );
  layerParentElem.appendChild( layerParentNameElem );
  QDomElement layerParentTitleElem = doc.createElement( "Title" );
  QDomText layerParentTitleText = doc.createTextNode( projTitle );
  layerParentTitleElem.appendChild( layerParentTitleText );
  layerParentElem.appendChild( layerParentTitleElem );

  QDomElement legendElem = mXMLDoc->documentElement().firstChildElement( "legend" );

  addLayers( doc, layerParentElem, legendElem, layerMap, nonIdentifiableLayers );

  parentElement.appendChild( layerParentElem );
  combineExtentAndCrsOfGroupChildren( layerParentElem, doc );
}
Exemplo n.º 2
0
QList<QgsMapLayer*> QgsProjectParser::mapLayerFromStyle( const QString& lName, const QString& styleName, bool allowCaching ) const
{
  Q_UNUSED( styleName );
  Q_UNUSED( allowCaching );
  QList<QgsMapLayer*> layerList;

  //first assume lName refers to a leaf layer
  QMap< QString, QDomElement > layerElemMap = projectLayerElementsByName();
  QMap< QString, QDomElement >::const_iterator layerElemIt = layerElemMap.find( lName );
  if ( layerElemIt != layerElemMap.constEnd() )
  {
    QgsMapLayer* layer = createLayerFromElement( layerElemIt.value() );
    if ( layer )
    {
      layerList.push_back( layer );
      return layerList;
    }
  }

  //Check if layer name refers to the top level group for the project.
  if ( lName == projectTitle() )
  {
    QList<QDomElement> layerElemList = projectLayerElements();
    QList<QDomElement>::const_iterator layerElemIt = layerElemList.constBegin();
    for ( ; layerElemIt != layerElemList.constEnd(); ++layerElemIt )
    {
      layerList.push_back( createLayerFromElement( *layerElemIt ) );
    }
    return layerList;
  }

  //maybe the layer is a goup. Check if lName is contained in the group list
  QMap< QString, QDomElement > idLayerMap = projectLayerElementsById();

  QList<QDomElement> legendGroups = legendGroupElements();
  QList<QDomElement>::const_iterator groupIt = legendGroups.constBegin();
  for ( ; groupIt != legendGroups.constEnd(); ++groupIt )
  {
    if ( groupIt->attribute( "name" ) == lName )
    {
      if ( groupIt->attribute( "embedded" ) == "1" ) //requested group is embedded from another project
      {
        QString project = convertToAbsolutePath( groupIt->attribute( "project" ) );
        QgsDebugMsg( QString( "Project path: %1" ).arg( project ) );
        QgsProjectParser* p = dynamic_cast<QgsProjectParser*>( QgsConfigCache::instance()->searchConfiguration( project ) );
        if ( p )
        {
          QList<QDomElement> pGroupElems = p->legendGroupElements();
          QList<QDomElement>::const_iterator pGroupIt = pGroupElems.constBegin();
          QDomElement embeddedGroupElem;

          for ( ; pGroupIt != pGroupElems.constEnd(); ++pGroupIt )
          {
            if ( pGroupIt->attribute( "name" ) == lName )
            {
              embeddedGroupElem = *pGroupIt;
              break;
            }
          }

          if ( !embeddedGroupElem.isNull() )
          {
            //add all the layers under the group
            QMap< QString, QDomElement > pLayerElems = p->projectLayerElementsById();
            QDomNodeList pLayerNodes = embeddedGroupElem.elementsByTagName( "legendlayer" );
            for ( int i = 0; i < pLayerNodes.size(); ++i )
            {
              QString pLayerId = pLayerNodes.at( i ).toElement().firstChildElement( "filegroup" ).firstChildElement( "legendlayerfile" ).attribute( "layerid" );
              QgsMapLayer* pLayer = p->createLayerFromElement( pLayerElems[pLayerId] );
              if ( pLayer )
              {
                layerList.push_back( pLayer );
              }
            }
          }
        }
      }
      else //normal (not embedded) group
      {
        QDomNodeList layerFileList = groupIt->elementsByTagName( "legendlayerfile" );
        for ( int i = 0; i < layerFileList.size(); ++i )
        {
          QMap< QString, QDomElement >::const_iterator layerEntry = idLayerMap.find( layerFileList.at( i ).toElement().attribute( "layerid" ) );
          if ( layerEntry != idLayerMap.constEnd() )
          {
            layerList.push_back( createLayerFromElement( layerEntry.value() ) );
          }
        }
      }
      return layerList;
    }
  }

  //maybe the layer is embedded from another project
  QMap< QString, QDomElement >::const_iterator layerIt = idLayerMap.constBegin();
  for ( ; layerIt != idLayerMap.constEnd(); ++layerIt )
  {
    if ( layerIt.value().attribute( "embedded" ) == "1" )
    {
      QString id = layerIt.value().attribute( "id" );
      QString project = layerIt.value().attribute( "project" );
      QgsDebugMsg( QString( "Project path: %1" ).arg( project ) );

      //get config parser from cache
      QgsProjectParser* otherParser = dynamic_cast<QgsProjectParser*>( QgsConfigCache::instance()->searchConfiguration( project ) );
      if ( otherParser )
      {
        //get element by id
        QMap< QString, QDomElement > otherLayerElems = otherParser->projectLayerElementsById();
        QMap< QString, QDomElement >::const_iterator otherLayerIt = otherLayerElems.find( id );
        if ( otherLayerIt != otherLayerElems.constEnd() )
        {
          if ( otherLayerIt.value().firstChildElement( "layername" ).text() == lName )
          {
            layerList.push_back( otherParser->createLayerFromElement( otherLayerIt.value() ) );
            return layerList;
          }
        }
      }
    }
  }

  //layer still not found. Check if it is a single layer contained in a embedded layer group
  groupIt = legendGroups.constBegin();
  for ( ; groupIt != legendGroups.constEnd(); ++groupIt )
  {
    if ( groupIt->attribute( "embedded" ) == "1" )
    {
      QString project = convertToAbsolutePath( groupIt->attribute( "project" ) );
      QgsDebugMsg( QString( "Project path: %1" ).arg( project ) );
      QgsProjectParser* p = dynamic_cast<QgsProjectParser*>( QgsConfigCache::instance()->searchConfiguration( project ) );
      if ( p )
      {
        QMap< QString, QDomElement > pLayers = p->projectLayerElementsByName();
        QMap< QString, QDomElement >::const_iterator pLayerIt = pLayers.find( lName );
        if ( pLayerIt != pLayers.constEnd() )
        {
          QgsMapLayer* layer = p->createLayerFromElement( pLayerIt.value() );
          if ( layer )
          {
            layerList.push_back( layer );
            return layerList;
          }
        }
      }
    }
  }

  //layer not found, return empty list
  return layerList;
}
Exemplo n.º 3
0
      layerMap.insert( layer->id(), layer );
    }
#if QGSMSDEBUG
    else
    {
      QString buf;
      QTextStream s( &buf );
      elem.save( s, 0 );
      QgsDebugMsg( QString( "layer %1 not found" ).arg( buf ) );
    }
#endif
  }

  //According to the WMS spec, there can be only one toplevel layer.
  //So we create an artificial one here to be in accordance with the schema
  QString projTitle = projectTitle();
  QDomElement layerParentElem = doc.createElement( "Layer" );
  layerParentElem.setAttribute( "queryable", "1" );
  QDomElement layerParentNameElem = doc.createElement( "Name" );
  QDomText layerParentNameText = doc.createTextNode( projTitle );
  layerParentNameElem.appendChild( layerParentNameText );
  layerParentElem.appendChild( layerParentNameElem );
  QDomElement layerParentTitleElem = doc.createElement( "Title" );
  QDomText layerParentTitleText = doc.createTextNode( projTitle );
  layerParentTitleElem.appendChild( layerParentTitleText );
  layerParentElem.appendChild( layerParentTitleElem );

  QDomElement legendElem = mXMLDoc->documentElement().firstChildElement( "legend" );

  addLayers( doc, layerParentElem, legendElem, layerMap, nonIdentifiableLayers );