# Screenshot every layer from a geoJSON object, failing to zoom to extents of each layer correctly

What I've been trying to do is I have a ton of geoJSON files (a few thousand) and I need to load them, change the colors of the Polygons and background to white and black, then export them as an image. Like the one below...

Though I've had a lot of trouble, I now can load all the geoJSON layers (though some of the geoJSON layers have nothing in them...so it loads up as Unknown in QGIS). I add the geojson layer to a LAYERS list that I use later, add the symbols, set the extent to that layer, and repeat.

Then, to screenshot each image, I iterate through the LAYERS list, set my extent to each layer, and screenshot it.

However, the screenshots are not correctly viewing the layer.

My code is below.

I'm not very familiar with QGIS and PyQGIS

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from qgis.core import *
import os
import sys
from time import sleep

NUMBER_OF_GEOJSON_FILES = 580
GEOJSON_DIR = "C:\\path\\to\\geojson"
IMAGE_DIR = "C:\\path\\to\\destination"
FILENAME = "filename_format_img"

#Set canvas color to black
iface.mapCanvas().setCanvasColor(QColor.fromRgb(0,0,0))

print GEOJSON_DIR
print IMAGE_DIR
print FILENAME

LAYERS = []

i = 570
while i <= NUMBER_OF_GEOJSON_FILES:
print(GEOJSON_DIR + FILENAME + str(i) + ".geojson")
layer = iface.addVectorLayer(GEOJSON_DIR + FILENAME + str(i) + ".geojson", "layer", "ogr")
LAYERS.append(layer)

#Set symbol colors, if no symbols do nothing
try:
symbols = layer.rendererV2().symbols()
symbol = symbols[0]
symbol.setColor(QColor.fromRgb(255,255,255))
except AttributeError:
print "No symbols"

#Zoom to layer
iface.mapCanvas().setExtent(layer.extent())
i = i + 1

def exportImages():
i = 1
for j in LAYERS:
sleep(3)
#Zoom to layer
iface.mapCanvas().setExtent(j.extent())
# Take screenshot
print("Screen Shotting:  " + IMAGE_DIR + FILENAME + str(i) + ".png")
iface.mapCanvas().saveAsImage(IMAGE_DIR + FILENAME + str(i) + ".png")
i = i + 1

exportImages()

• try adding iface.mapCanvas().refresh() after setting the extent in exportImages. Note that the layers are not turned on/off so you may have unexpected screenshots should they overlap
– JGH
Jul 6 '17 at 16:13
• Ah awesome, that solved the problem of it getting stuck on just screenshotting one layer thanks! I do still have another problem I guess because the empty geojson objects come up as not Polygons it is not registering them as layers (see image). So it is not changing the layer to a blank black extent. Any idea on that? I guess I could just have a case for blank geojson objects and just render a black blank image but if there is a way in QGIS to get around that. Thank you, I'll accept your answer if you post it for the refresh thing :) Image Jul 6 '17 at 16:18

After setting the current layer extent, you would need to refresh the map display

iface.mapCanvas().refresh()


If you want to do a particular action when the layer is empty, you can count the features

layer.featureCount()


The function would look like:

def exportImages():
i = 1
for j in LAYERS:
sleep(3)

if j.featureCount() == 0:
#do something with empty layer... (skip, copy a blank image etc)
else:
#Zoom to layer
iface.mapCanvas().setExtent(j.extent())
iface.mapCanvas().refresh()
# Take screenshot
print("Screen Shotting:  " + IMAGE_DIR + FILENAME + str(i) + ".png")
iface.mapCanvas().saveAsImage(IMAGE_DIR + FILENAME + str(i) + ".png")
i = i + 1

• I'm still having some troubles, it seems like there is a delay between iface.mapCanvas().refresh() and iface.mapCanvas().saveAsImage(IMAGE_DIR + FILENAME + str(i) + ".png") to where they are going out of sync as it is taking the screenshot. Is there some sort of like check when rendered or when refreshed is finished? By out of sync I mean it's taking the screenshot before the extent has changed. Jul 6 '17 at 18:50
• see gis.stackexchange.com/a/194906/84870 for setting a delay
– JGH
Jul 6 '17 at 18:55