I have a Python script that runs in the QGIS Python console but I would like to run it outside of the console, so that a map can be created without having to be within QGIS. The script runs perfectly inside in the console but I just need to find a way how to use it from the outside, so I can then eventually use task scheduler to repeat this process at set time intervals. What are the steps I need to take in order for my script to run outside? I vaguely know that I need import modules/libraries that I have used into Python but I am unsure on how to do this and what other steps I need to take. I have looked at this hand book but I can't seem to find a way what to do - http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/intro.html. I have included an example of a small section of my script but included all imports:
import math
import json
import os
import glob
from qgis.PyQt import QtGui
#Add Base Map
urlWithParams = 'type=xyz&url=https://a.tile.openstreetmap.org/%7Bz%7D/%7Bx%7D/%7By%7D.png&zmax=19&zmin=0&crs=EPSG3857'
rlayer2 = QgsRasterLayer(urlWithParams, 'OpenStreetMap', 'wms')
if rlayer2.isValid():
QgsProject.instance().addMapLayer(rlayer2)
else:
print('invalid layer')
#Add Vector Layers
vlayer = iface.addVectorLayer(latest_file, "Earthquake Event", "ogr")
if not vlayer:
print("Layer failed to load!")
# Creating the distance buffers
layer = iface.activeLayer()
feats = [ feat for feat in layer.getFeatures() ]
epsg = layer.crs().postgisSrid()
uri = "Polygon?crs=epsg:" + str(epsg) + "&field=id:integer&field=x:real& field=y:real&field=point_id:integer""&index=yes"
#Using zonal statistics to work out affected population sum column
#######
# there is no need to look for the raster layer again since you already saved it to a variable before(line 53)
prefix = rlayer.name()
params = {'INPUT_RASTER': rlayer.name(), 'RASTER_BAND': 1, 'INPUT_VECTOR': f'MMI {intensity} Buffer', 'COLUMN_PREFIX': prefix+'_', 'STATS': 1}
processing.run("qgis:zonalstatistics", params)
#This adds a map item to the Print Layout
map = QgsLayoutItemMap(layout)
map.setRect(20, 20, 20, 20)
#Choosing export folder and exporting layout
data_dir = os.path.join(os.path.expanduser('~'), 'Path_to_output')
output_file = feat['id'] + ' Event.png'
output_path = os.path.join(data_dir, output_file)
exporter = QgsLayoutExporter(layout)
exporter.exportToImage(
output_path, QgsLayoutExporter.ImageExportSettings())
print("Finished")