2

In Python console from within QGIS, we use

qgis.utils.iface.legendInterface().moveLayer( layer1, 1 )

to make modifications to the legend. What is the equivalent in a standalone python script ?In the .qjs file generated by below code some tags are missing.

Code

# Import system modules
from xml.dom.minidom import Document
import string
import os
import sys
from qgis.core import *
from qgis.gui import *
from PyQt4.QtCore import *
from PyQt4.QtGui import QApplication
from PyQt4.QtXml import *
from qgis.utils import iface


#Read input parameters from GP dialog
strProjetName = "C:/OSGeo4W/apache/htdocs/QGIS-Web-Client-master/projects/myworld33.qgs"

if os.path.isfile(strProjetName):
   os.remove(strProjetName)


def add_Layers():
    QGISAPP = QgsApplication(sys.argv, True)     
    QgsApplication.setPrefixPath(r"C:\OSGeo4W\apps\qgis", True)
    QgsApplication.initQgis()

    QgsProject.instance().setFileName(strProjetName)
    print QgsProject.instance().fileName()
    render = QgsMapRenderer()
    canvas = QgsMapCanvas()



    QgsProject.instance().writeEntry('WFSLayers', "/", [])
    pWfsLayer =[]
    for file1 in os.listdir(r"C:\myprojects\world"):
         if file1.endswith('.shp'):
             layer = QgsVectorLayer(r"C:\myprojects\world"+r"\\"+file1, file1, "ogr")
             print file1
             print layer.isValid()
             # Add layer to the registry
             QgsMapLayerRegistry.instance().addMapLayer(layer)
             #qgis.utils.iface.legendInterface.setLayerVisible(layer,True)

             # set layer set
             lst = [ layer.id() ] # add ID of every layer
             pWfsLayer.append(u'%s' % layer.id())

             render.setLayerSet(lst)

             rect = QgsRectangle(render.fullExtent())            
             rect.scale(1.1)
             render.setExtent(rect)


    canvas.setExtent(rect)

    QgsProject.instance().writeEntry('WFSLayers', '', pWfsLayer)


    QgsProject.instance().writeEntry('WMSServiceCapabilities', "/", "True")    
    QgsProject.instance().writeEntry('WMSServiceTitle', '', u'DEM Viewer')
    QgsProject.instance().writeEntry('WMSContactMail', '', u'Durga.Dhulipudi@Honeywell.com')

    # check if a bbox has been given in the project OWS tab configuration
    pWmsExtentLe = QgsProject.instance().readListEntry('WMSExtent','')
    pWmsExtent = pWmsExtentLe[0]

    if len(pWmsExtent) < 1 :
       pWmsExtent.append(u'%s' % rect.xMinimum())
       pWmsExtent.append(u'%s' % rect.yMinimum())
       pWmsExtent.append(u'%s' % rect.xMaximum())
       pWmsExtent.append(u'%s' % rect.yMaximum())
       QgsProject.instance().writeEntry('WMSExtent', '', pWmsExtent)
    else:
       if not pWmsExtent[0] or not pWmsExtent[1] or not pWmsExtent[2] or not pWmsExtent[3]:
           pWmsExtent[0] = u'%s' % rect.xMinimum()
           pWmsExtent[1] = u'%s' % rect.yMinimum()
           pWmsExtent[2] = u'%s' % rect.xMaximum()
           pWmsExtent[3] = u'%s' % rect.yMaximum()
           QgsProject.instance().writeEntry('WMSExtent', '', pWmsExtent)



    crsList = QgsProject.instance().readListEntry('WMSCrsList','')
    pmFound = False
    for i in crsList[0]:
        if i == 'EPSG:3857':
            pmFound = True
    if not pmFound:
        crsList[0].append('EPSG:3857')
        QgsProject.instance().writeEntry('WMSCrsList', '', crsList[0])
        crsList[0].append('EPSG:4326')
        QgsProject.instance().writeEntry('WMSCrsList', '', crsList[0])

    QgsProject.instance().writeEntry('WMSAddWktGeometry', "/", "True")





    QgsProject.instance().write()


    QgsApplication.exitQgis()


add_Layers()

<visibility-presets/>
<layer-tree-canvas>
    <custom-order enabled="0">
        <item>world_countries_shp20150116155540387</item>
        <item>Grid1_shp20150116155540363</item>
    </custom-order>
</layer-tree-canvas>
<legend updateDrawingOrder="true">
    <legendlayer drawingOrder="-1" open="true" checked="Qt::Checked" name="world_countries.shp" showFeatureCount="0">
        <filegroup open="true" hidden="false">
            <legendlayerfile isInOverview="0" layerid="world_countries_shp20150116155540387" visible="1"/>
        </filegroup>
    </legendlayer>
    <legendlayer drawingOrder="-1" open="true" checked="Qt::Checked" name="Grid1.shp" showFeatureCount="0">
        <filegroup open="true" hidden="false">
            <legendlayerfile isInOverview="0" layerid="Grid1_shp20150116155540363" visible="1"/>
        </filegroup>
    </legendlayer>
</legend>
  • You don't have a legned in a standalone script. That interface is for the legend tree. – Nathan W Jan 17 '15 at 8:20
  • I just want to know how to generate .qjs file.I have generated and able to view in QGIS Desktop.But not able view in Web Client.I found that the few tags are missing and not sure how to generate them...appreciate if you could help. – addcolor Jan 17 '15 at 9:11
  • 1
    @NathanW (For other users reading this post) You don't have it out of the box, but you can build one: geotux.tuxfamily.org/index.php/en/component/k2/item/… – Germán Carrillo Jan 17 '15 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.