管道基础大数据平台系统开发-【CS】-ExportMap
1
13693261870
2023-03-10 adffca71aba3ed339dc0484900e7c866d52d5f33
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env
# -*- coding: utf-8 -*-
 
 
import os
import sys
sys.path.append(r"C:\Program Files\QGIS 3.16\apps\qgis-ltr\python\plugins")
import math
import argparse
from qgis.core import *
from qgis.gui import *
from qgis.PyQt.QtGui import *
from qgis.PyQt.QtCore import *
from qgis.PyQt.QtWidgets import *
from processing.core.Processing import Processing
 
 
# 获取完整路径
def get_full_path():
    return os.path.split(sys.argv[0])[0]
 
 
# 获取参数
def get_args():
    print("argv = ", sys.argv[1:])
    parser = argparse.ArgumentParser(description='ArgUtils')
    parser.add_argument("-src", type=str, default=get_full_path(), required=False)
    parser.add_argument("-qgz", type=str, default="xyz.qgz", required=False)
    parser.add_argument("-file", type=str, default=r"D:\xyz\tiles.txt", required=False)
    parser.add_argument("-out", type=str, default=r"D:\xyz\tiles\zy", required=False)
 
    return parser.parse_args()
 
 
# 读取文本文件
def readTxt(filePath):
    f = open(filePath, encoding="utf-8")
    lines = f.readlines()
    f.close()
 
    for i in range(0, len(lines)):
        lines[i] = lines[i].replace('\n', '')
 
    return lines
 
 
# 加载图层
def loadLayers(prj, args):
    for layer in prj.mapLayers().values():
        prj.removeMapLayer(layer)
 
    lines = readTxt(args.file)
    for i in range(0, len(lines)):
        if len(lines[i]) == 0:
            continue
 
        print("layer: " + lines[i])
        layer = QgsRasterLayer(lines[i], "layer_" + str(i))
        if not layer.isValid() or layer.crs() is None:
            print("layer_" + str(i) + ": failed to load!")
            continue
 
        if len(prj.mapLayers()) == 0:
            prj.setCrs(layer.crs())
 
        for j in range(1, layer.bandCount() + 1):
            layer.dataProvider().setNoDataValue(j, 0)
 
        prj.addMapLayer(layer)
 
    for layer in prj.mapLayers().values():
        print(layer.name() + ", " + layer.extent().toString() + ", " + layer.crs().authid())
 
 
# 初始化
def init():
    # QgsApplication.setPrefixPath("C:\Program Files\QGIS 3.16", True)
    qgs = QgsApplication([], False)
    qgs.initQgis()
    Processing.initialize()
 
    args = get_args()
    project = QgsProject.instance()
    project.read(os.path.join(args.src, args.qgz))
    print("FileName: " + project.fileName())
 
    loadLayers(project, args)
 
    import processing
    ops = {
        'BACKGROUND_COLOR': QColor(0, 0, 0, 0),
        'DPI': 96,
        'EXTENT': '38399769.358700000,38403264.373400003,3558330.210400000,3561632.889400000',
        'METATILESIZE': 4,
        'OUTPUT_DIRECTORY': args.out,
        'OUTPUT_HTML': args.out + "\\view.html",
        'QUALITY': 100,
        'TILE_FORMAT': 0,
        'TILE_HEIGHT': 256,
        'TILE_WIDTH': 256,
        'TMS_CONVENTION': True,
        'ZOOM_MAX': 12,
        'ZOOM_MIN': 12
    }
    processing.run("qgis:tilesxyzdirectory", ops)
 
    qgs.exitQgis()
 
 
# main
if __name__ == '__main__':
    init()