Prompt Details
Model
Chat - GPT-4o (gpt-4o)
Token size
67 ($0.00335 / call)
Example input
[Write a Python script using the QGIS Python API PyQGIS to add a new layer in QGIS from a GeoJSON file. The script should load the GeoJSON file, create a corresponding vector layer, and add it to the QGIS project. Ensure the layer is correctly rendered with attributes and geometry.]: Write a Python script using the QGIS Python API PyQGIS to add a new layer in QGIS from a GeoJSON file. The script should load the GeoJSON file, create a corresponding vector layer, and add it to the QGIS project. Ensure the layer is correctly rendered with attributes and geometry.
Example output
from qgis.core import (
QgsApplication,
QgsProject,
QgsVectorLayer
)
# Initialize QGIS application
QgsApplication.setPrefixPath("/path/to/qgis", True)
qgs = QgsApplication([], False)
qgs.initQgis()
# Path to the GeoJSON file
geojson_file = "/path/to/your/file.geojson"
# Load the GeoJSON file as a vector layer
layer = QgsVectorLayer(geojson_file, "My GeoJSON Layer", "ogr")
# Check if the layer is valid
if not layer.isValid():
print("Failed to load the GeoJSON file!")
else:
print("GeoJSON file loaded successfully!")
# Add the layer to the current QGIS project
QgsProject.instance().addMapLayer(layer)
print("Layer added to the QGIS project!")
# Exit QGIS application
qgs.exitQgis()
By purchasing this prompt, you agree to our terms of service
GPT-4O
This prompt guides the creation of a Python script using the QGIS Python API (PyQGIS) to seamlessly add a GeoJSON file as a new layer in QGIS. The script ensures that the vector layer is properly loaded, preserving attributes and geometry, and is automatically added to the QGIS project for visualization and analysis.
It is ideal for GIS professionals, developers, and data analysts who want to automate layer management in QGIS without manually importing files. By using this prompt, users can qui
...more
Added over 1 month ago
