2

I have a data-set of .tif satellite images and .geoJSON labels corresponding to them. I am trying to iterate over the satellite images,plot the geoJSON labels and save them as .jpeg images.

However,only the first image is being saved correctly.The others are either completely blank or have very little of the original .tif image on them. I am very new to QGIS.

Here is my code

for el in [1,3,4,7,8,9]:
    iface.addRasterLayer("Path/RGB-PanSharpen_AOI_2_Vegas_img"+str(el)+".tif", "Image") #Adding Image
    iface.addVectorLayer('Path/buildings_AOI_2_Vegas_img'+str(el)+'.geojson','Label','ogr') #Adding labels
    eMenu =iface.viewMenu()
    eMenu.actions() [11].trigger() #Zooming in to layer
    iface.mapCanvas().saveAsImage("Desktop\Im"+str(el)+".jpeg",None,"jpeg") #Saving
    QgsMapLayerRegistry.instance().removeAllMapLayers() #Clearing canvas

I am using QGIS 2.14.20-Essen

2
1

The problem was that enough time delay was not being provided and hence I got half loaded images.

Here is the final code if it helps anyone else-

eleArr=[1,3,4,7,8,9]
ind=-1
from PyQt4.QtCore import QTimer

def AddRastLayer():
   global eleArr
   global ind
   ind+=1
   el=eleArr[ind]   
   if(ind<=4):
        iface.addRasterLayer("Path/RGB-PanSharpen_AOI_2_Vegas_img"+str(el)+".tif", "layer of "+str(el))
        QTimer.singleShot(1000, AddVectLayer)
def AddVectLayer():
   global eleArr
   global ind
   el=eleArr[ind]
   iface.addVectorLayer('Path/AOI_2_Vegas_img'+str(el)+'.geojson','Label '+str(el),'ogr')  
   QTimer.singleShot(1000, ZoomLayer)
def ZoomLayer():
   global eleArr
   global ind
   eMenu =iface.viewMenu()
   eMenu.actions()[11].trigger()
   QTimer.singleShot(1000, SaveLayer)   
def SaveLayer():
   global eleArr
   global ind
   el=eleArr[ind]
   iface.mapCanvas().saveAsImage("Path\ Im"+str(el)+".jpeg",None,"jpeg")
   QTimer.singleShot(1000, RemLayer)
def RemLayer():
   global eleArr
   global ind
   el=eleArr[ind]
   QgsMapLayerRegistry.instance().removeAllMapLayers()
   QTimer.singleShot(1000, AddRastLayer)
AddRastLayer()

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.