"""QGIS screenshots for QGIS essentials part 1 (run by tools/qgis-capture/scene_shot.ps1). 1. Opens Data Source Manager > Delimited Text with the Environment Agency rainfall gauge CSV, X field = long, Y field = lat, CRS EPSG:4326, and grabs the dialogue. 2. Adds the gauges (saved to GeoPackage by csv_to_points.py) over OpenStreetMap and grabs the map. Inputs are read from C:\\TechnicalGIS\\qgis-work\\csv-to-points so no personal path appears. """ from qgis.core import (QgsCoordinateReferenceSystem, QgsCoordinateTransform, QgsMarkerSymbol, QgsProject, QgsRasterLayer, QgsRectangle, QgsVectorLayer) from qgis.PyQt.QtWidgets import QApplication, QComboBox, QGroupBox, QTableWidget, QDockWidget, QRadioButton, QWidget WORK = r"C:\TechnicalGIS\qgis-work\csv-to-points" def steps(iface, shot, log): st = {} def setup(): mw = iface.mainWindow() mw.showNormal() mw.resize(1500, 900) mw.move(40, 40) for dock in mw.findChildren(QDockWidget): if dock.objectName() != "Layers": dock.hide() QgsProject.instance().setCrs(QgsCoordinateReferenceSystem("EPSG:3857")) log("setup") def open_dsm(): iface.openDataSourceManagerPage("delimitedtext") def fill(): dsm = next(w for w in QApplication.topLevelWidgets() if w.metaObject().className() == "QgsDataSourceManagerDialog") dsm.show() dsm.raise_() dsm.resize(1100, 820) st["dsm"] = dsm page = next(w for w in dsm.findChildren(QWidget) if w.metaObject().className() == "QgsDelimitedTextSourceSelect") st["page"] = page fw = next(w for w in page.findChildren(QWidget) if w.metaObject().className() == "QgsFileWidget") fw.setFilePath(WORK + r"\rainfall-stations.csv") log("file set; radios: " + ", ".join(r.objectName() for r in page.findChildren(QRadioButton))) def fields(): page = st["page"] combos = {c.objectName(): c for c in page.findChildren(QComboBox)} log("combos: " + ", ".join(sorted(combos))) for name, value in (("cmbXField", "long"), ("cmbYField", "lat")): c = combos[name] c.setCurrentIndex(c.findText(value)) crs = next((w for w in page.findChildren(QWidget) if w.metaObject().className() == "QgsProjectionSelectionWidget"), None) if crs: crs.setCrs(QgsCoordinateReferenceSystem("EPSG:4326")) # QgsCollapsibleGroupBox comes back as a plain QGroupBox, so use its Qt property. for gb in page.findChildren(QGroupBox): if gb.metaObject().className() == "QgsCollapsibleGroupBox": gb.setProperty("collapsed", gb.objectName() not in ("geometryDefinitionGroupBox", "fileFormatGroupBox")) table = page.findChild(QTableWidget, "tblSample") if table: heads = [table.horizontalHeaderItem(i).text() for i in range(table.columnCount())] if "lat" in heads: table.scrollToItem(table.item(0, heads.index("long")), QTableWidget.PositionAtCenter) log(f"sample columns: {len(heads)}") log(f"x={combos['cmbXField'].currentText()} y={combos['cmbYField'].currentText()}") def grab_dialog(): shot(st["dsm"], "01-qgis-delimited-text-dialog.png") st["dsm"].close() def add_layers(): project = QgsProject.instance() osm = QgsRasterLayer("type=xyz&url=https://tile.openstreetmap.org/{z}/{x}/{y}.png&zmax=19&zmin=0", "OpenStreetMap", "wms") gauges = QgsVectorLayer(WORK + r"\rainfall-gauges.gpkg|layername=rainfall_gauges", "Rainfall gauges", "ogr") gauges.renderer().setSymbol(QgsMarkerSymbol.createSimple( {"name": "circle", "color": "31,120,180", "outline_color": "255,255,255", "outline_width": "0.3", "size": "2.2"})) project.addMapLayer(osm) project.addMapLayer(gauges) canvas = iface.mapCanvas() canvas.setDestinationCrs(project.crs()) tr = QgsCoordinateTransform(QgsCoordinateReferenceSystem("EPSG:4326"), project.crs(), project) canvas.setExtent(tr.transformBoundingBox(QgsRectangle(-6.2, 49.9, 2.3, 55.9))) canvas.refresh() log(f"gauges valid={gauges.isValid()} n={gauges.featureCount()}") def grab_map(): shot(iface.mainWindow(), "02-qgis-rainfall-gauges-map.png") return [ (500, setup), (1000, open_dsm), (2500, fill), (3000, fields), (2000, grab_dialog), (500, add_layers), (15000, grab_map), ]