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.


# 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):

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

    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

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


             rect = QgsRectangle(render.fullExtent())            


    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)
       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:
        QgsProject.instance().writeEntry('WMSCrsList', '', crsList[0])
        QgsProject.instance().writeEntry('WMSCrsList', '', crsList[0])

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




    <custom-order enabled="0">
<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"/>
    <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"/>
  • 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.