-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdinamic_layer_based_layout.py
76 lines (60 loc) · 2.87 KB
/
dinamic_layer_based_layout.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
from qgis.core import (
QgsProject, QgsPrintLayout, QgsLayoutItemMap, QgsLayoutPoint,
QgsLayoutSize, QgsUnitTypes, QgsRectangle, QgsLayoutItemPage
)
# Ottenere il progetto e il gestore dei layout
project = QgsProject.instance()
manager = project.layoutManager()
# Nome del layer che contiene i quadrati
layer_name = "layer_name" # Cambia con il nome esatto del tuo layer
layer = QgsProject.instance().mapLayersByName(layer_name)[0]
# Controllo che il layer esista
if not layer:
print("Errore: Il layer specificato non esiste.")
else:
# Impostiamo dimensioni fisse per la pagina (100 cm = 1000 mm)
page_width_mm = 1000 # 100 cm
page_height_mm = 1000 # 100 cm
scala_fissa = 2000 # Scala 1:2000
# Dimensioni reali della mappa in unità della CRS
width_real = 2000 # 2000 metri nel sistema di coordinate
height_real = 2000 # 2000 metri
# Itera su ogni quadrato del layer
for feature in layer.getFeatures():
fid = feature.id() # Prende il fid del quadrato
bbox = feature.geometry().boundingBox()
# Calcoliamo il centro del quadrato
center_x = (bbox.xMinimum() + bbox.xMaximum()) / 2
center_y = (bbox.yMinimum() + bbox.yMaximum()) / 2
# Calcoliamo la nuova estensione basata sulla scala
new_bbox = QgsRectangle(
center_x - (width_real / 2), # X minimo
center_y - (height_real / 2), # Y minimo
center_x + (width_real / 2), # X massimo
center_y + (height_real / 2) # Y massimo
)
# Nome del layout basato sul fid
layout_name = f"Layout_{fid}"
# Rimuove eventuali layout esistenti con lo stesso nome
existing_layout = manager.layoutByName(layout_name)
if existing_layout:
manager.removeLayout(existing_layout)
# Crea un nuovo layout
layout = QgsPrintLayout(project)
layout.setName(layout_name)
manager.addLayout(layout)
# **CREAZIONE DELLA PAGINA**
page = QgsLayoutItemPage(layout)
page.setPageSize(QgsLayoutSize(page_width_mm, page_height_mm, QgsUnitTypes.LayoutMillimeters))
layout.pageCollection().addPage(page) # Aggiunge la pagina
# **CREAZIONE DELLA MAPPA**
map_item = QgsLayoutItemMap(layout)
layout.addLayoutItem(map_item)
# Imposta la posizione e la dimensione della mappa nel layout
map_item.attemptMove(QgsLayoutPoint(0, 0, QgsUnitTypes.LayoutMillimeters)) # Allineata in alto a sinistra
map_item.attemptResize(QgsLayoutSize(page_width_mm, page_height_mm, QgsUnitTypes.LayoutMillimeters)) # Stessa dimensione della pagina
# **Imposta l'estensione calcolata**
map_item.setExtent(new_bbox)
map_item.setScale(scala_fissa)
print(f"Creato layout '{layout_name}' per FID {fid} con scala 1:{scala_fissa}")
print("Operazione completata!")