2

I am a newbie to QGIS Python. I have a shape-file which I want to load to QGIS 2.8.1 and manipulate them by zooming to the layer of the preferred shape-file.

Once I have my canvas ready, I want to use that canvas in the composer manager and export it to a png file.

Below is my script:

import ogr,os
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from qgis.core import *
import qgis.utils
import glob
import time
import math
import processing
from processing.core.Processing import Processing
from PyQt4.QtCore import QTimer
import sys

Processing.initialize()
Processing.updateAlgsList()
OutputFileName = "ABC"    # Temprory global placeholder for filename
canvas = iface.mapCanvas()

#Add List of address below for heatmap shapefile
heatmapfile = ["C:/Users/prashant.kumar/Desktop/QGIS python/Shapefiles/Map_00721.shp"]

# Add array of address below for  company shapefile
cspFile = ["C:/Users/prashant.kumar/Desktop/QGIS python/Shapefiles/HM_13_June_19.shp"]

for lop in range(filesLen):
    flag = 0
    currentShpFile = heatmapfile[lop]
    currentCompShpFile = cspFile[lop]

    #enter the common path for each shapefile here
    currentShpFileName = currentShpFile.strip("C:/Users/prashant.kumar/Desktop/QGIS python/Shapefiles/")
    #enter the common path for each shapefile here
    currentCompShpFileName = currentCompShpFile.strip("C:/Users/prashant.kumar/Desktop/QGIS python/Shapefiles/")

    # enter the output path here
    OutputFileName = "C:/Users/prashant.kumar/Desktop/QGIS python/ImageOut/" + currentShpFileName + ".png"

    # adding the shapefiles, changing the orders and zoom to layer
    wbc = QgsVectorLayer(currentCompShpFile, currentCompShpFileName, 'ogr')
    wb = QgsVectorLayer(currentShpFile, currentShpFileName, 'ogr')
    wbTex = QgsVectorLayer(TexLayer, 'CountyGrid', 'ogr')
    QgsMapLayerRegistry.instance().addMapLayer(wbc)    # Add the shapefile
    QgsMapLayerRegistry.instance().addMapLayer(wb)    # Add the shapefile
    QgsMapLayerRegistry.instance().addMapLayer(wbTex)    # Add the county shapefile
    qgis.utils.iface.setActiveLayer(wbc)   # Makes wb as active shapefile
    qgis.utils.iface.zoomToActiveLayer()    #Zoom to Layer
    print "Ready for composer manager"

    #Starting the canvas manager
    comp = iface.createNewComposer('currentcomposer')

Now I need help so I can use the current state of my canvas to be in the composer manager and export it as a PNG file.

TomazicM
  • 25,601
  • 22
  • 29
  • 39

1 Answers1

0

I found the answer here. The script worked perfectly. Please follow the link to check the proper method