Skip to content

Instantly share code, notes, and snippets.

@bertt
Created June 3, 2025 11:47
Show Gist options
  • Save bertt/783ec3da815d052cd39102f9ae054436 to your computer and use it in GitHub Desktop.
Save bertt/783ec3da815d052cd39102f9ae054436 to your computer and use it in GitHub Desktop.
QGIS Promptly sample
prompt=voeg D:\gisdata\andorra\ALPSMLC30_N042E001_DSM.tif toe aan qgis
Please follow these guidelines when generating QGIS code:
1. Always include necessary QGIS imports at the top of your code (note the correct locations):
- from qgis.core import QgsProject, QgsVectorLayer, QgsCoordinateReferenceSystem, Qgis
- from qgis.utils import iface
- from PyQt5.QtCore import Qt, QVariant
- from PyQt5.QtGui import QColor
- from PyQt5.QtWidgets import QMessageBox, QInputDialog, QDialog, QComboBox
2. IMPORTANT IMPORT RULES:
- Qgis (with the enums like Qgis.Info) is imported from qgis.core, NOT from PyQt5
- Message bars are accessed through iface.messageBar(), NOT through a QgsMessageBar class
3. When your code involves modifying the map canvas or project:
- Reference the current project with QgsProject.instance()
- Use iface for user interface operations
- Always call refresh operations like iface.mapCanvas().refresh() when making visual changes
4. For adding layers to the map:
- Use QgsProject.instance().addMapLayer(layer) or addMapLayers([layers])
- Ensure layers have proper CRS settings
5. For user notifications:
- Use iface.messageBar().pushMessage("Title", "Message", level=Qgis.Info)
- Do NOT use QgsMessageBar directly
6. For USER INTERACTION and LAYER SELECTION:
- If a specific layer has been pre-selected in the dropdown, use that layer directly
- For additional layer selection needs, allow the user to select a layer using:
```python
layer_names = [layer.name() for layer in QgsProject.instance().mapLayers().values()]
layer_name, ok = QInputDialog.getItem(None, "Select Layer", "Choose a layer:", layer_names, 0, False)
if ok and layer_name:
selected_layer = next((layer for layer in QgsProject.instance().mapLayers().values() if layer.name() == layer_name), None)
if selected_layer:
# Proceed with the selected layer
```
7. USING THE SELECTED LAYER:
- If a layer has been pre-selected in the dropdown, access it directly with:
```python
# Get the selected layer by name
layer_name = "LAYER_NAME_HERE" # Replace with the name from the metadata
selected_layer = None
for lyr in QgsProject.instance().mapLayers().values():
if lyr.name() == layer_name:
selected_layer = lyr
break
if selected_layer:
# Work with the selected layer
# Example: count features, process attributes, etc.
else:
iface.messageBar().pushMessage("Error", f"Layer '{layer_name}' not found", level=Qgis.Warning)
```
8. When executing complex operations:
- Consider wrapping in try/except blocks
- Add progress messages for user feedback
Provide complete, self-contained code that will run in the QGIS Python environment.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment