-
Notifications
You must be signed in to change notification settings - Fork 0
/
qgis_field.py
89 lines (63 loc) · 2.33 KB
/
qgis_field.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import sys
import os
sys.path.insert(0,"/usr/lib/python3/dist-packages")
from qgis.core import *
qgs = QgsApplication([], False)
# Load providers
qgs.initQgis()
# Set up the QGIS project instance
project = QgsProject.instance()
# Enable on-the-fly CRS transformation
QgsProject.instance().setCrs(QgsCoordinateReferenceSystem('EPSG:3857'))
# Get the current working directory
current_directory = os.getcwd()
# Construct the file path
file_path = os.path.join(current_directory, "meshgrid_data.csv")
# Construct the URI
uri = f"file://{file_path}?encoding=UTF-8&delimiter=,&xField=Longitude&yField=Latitude"
vlayer = QgsVectorLayer(uri, "CSV Layer", "delimitedtext")
# Configure the vector field symbol layer
vector_field_layer = QgsVectorFieldSymbolLayer()
vector_field_layer.setXAttribute('U')
vector_field_layer.setYAttribute('V')
vector_field_layer.setVectorFieldType(QgsVectorFieldSymbolLayer.Cartesian)
vector_field_layer.setScale(4)
arrow = QgsArrowSymbolLayer.create(
{
"arrow_width": "1",
"head_length": "3",
"head_thickness": "2",
"head_type": "0",
"arrow_type": "0",
"is_curved": "0",
"arrow_start_width": "1"
}
)
# Create an arrow symbol
arrow_symbol = QgsMarkerSymbol()
vector_field_layer.subSymbol().changeSymbolLayer(0, arrow)
# vector_field_layer.copyDataDefinedProperties(arrow_symbol)
arrow_symbol.changeSymbolLayer(0, vector_field_layer)
# Set the renderer to use the arrow symbol
vlayer.renderer().setSymbol(arrow_symbol)
vlayer.triggerRepaint()
# Set the CRS to EPSG:4326
crs = QgsCoordinateReferenceSystem("EPSG:4326")
vlayer.setCrs(crs)
# Check if the layer is valid
if not vlayer.isValid():
print("Layer failed to load!")
# Add the vector layer to the project
project.addMapLayer(vlayer)
# Define the raster layer (OpenStreetMap) and its CRS
urlWithParams = 'type=xyz&url=https://a.tile.openstreetmap.org/%7Bz%7D/%7Bx%7D/%7By%7D.png&zmax=19&zmin=0&crs=EPSG3857'
rlayer = QgsRasterLayer(urlWithParams, 'OpenStreetMap', 'wms')
# Check if the raster layer is valid
if not rlayer.isValid():
print("Raster layer failed to load!")
# Add the raster layer to the project
project.addMapLayer(rlayer)
# Write the project to a file
project.write('my_new_qgis_project.qgs')
# Exit QGIS
qgs.exitQgis()