-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patht4_export_png_basemap_v2_final_qgis3.py
61 lines (47 loc) · 1.97 KB
/
t4_export_png_basemap_v2_final_qgis3.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
from PyQt5.QtCore import QTimer
#Lưu ý là vùng tấm ảnh xuất bằng với vùng map đang mở trên QGIS
#Do vậy chỉnh vùng hiện bản đồ trên QGIS cho phù hợp rồi hãy xuất
#Cấu hình thư mục chứa ảnh
filePath = 'C:/Users/soiqu/Desktop/test_pyQGIS/png/'
#prefixes = ['t_216_TVDI_200003.tif', 't_216_TVDI_200012.tif', 't_216_TVDI_200011.tif']
prefixes = []
count = 0
#scale = 640
layers = []
for layer in QgsProject.instance().mapLayers().values():
#layers.append(layer)
#print(layer.id());
print(layer.name());
#Chỉ lấy những lớp bắt đầu bằng chữ t_
if layer.name().startswith("t_"):
prefixes.append(layer.name())
# print(layers[0].extent())
# map.setExtent(layers[0].extent())
# map.setRect(20, 20, 20, 20)
# Lên danh sách layer
def prepareMap():
layers = []
#layers = QgsProject.instance().mapLayers()
for layer in QgsProject.instance().mapLayers().values():
#Chỉ lấy những lớp bắt đầu bằng chữ t_
if layer.name().startswith("t_"):
layers.append(layer)
# Tắt hết layer
for layer in layers:
QgsProject.instance().layerTreeRoot().findLayer(layer).setItemVisibilityChecked(False)
# Chọn lớp cần xuất
exportLayers = []
for layer in QgsProject.instance().mapLayers().values():
if layer.name().startswith(prefixes[count]):
QgsProject.instance().layerTreeRoot().findLayer(layer).setItemVisibilityChecked(True)
# iface.actionZoomToSelected().trigger()
# qgis.utils.iface.mapCanvas().zoomScale(scale)
QTimer.singleShot(1000, exportMap) # Chờ 1 giây đẻ export
def exportMap(): # Save as a PNG
global count
iface.mapCanvas().saveAsImage( filePath + prefixes[count] + ".png" )
print("Layer",prefixes[count],"Đã xuất!")
if count < len(prefixes)-1:
QTimer.singleShot(1000, prepareMap) # Chờ 1s để xuất layer tiếp
count += 1
prepareMap()