Skip to main content
deleted 52 characters in body
Source Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

My code is below, if anyone could help me it would be much appreciated,.

I'm not very familiar with QGIS and PyQGIS

My code is below, if anyone could help me it would be much appreciated, I'm not very familiar with QGIS and PyQGIS

My code is below.

I'm not very familiar with QGIS and PyQGIS

edited tags
Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345
Source Link
Carson
  • 365
  • 2
  • 11

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...enter image description here

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, if anyone could help me it would be much appreciated, 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 = []

def loadGeoJSON():
    i = 570
    while i <= NUMBER_OF_GEOJSON_FILES:
        #Load geojson
        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

loadGeoJSON()
exportImages()