管道基础大数据平台系统开发-【CS】-ExportMap
1
13693261870
2023-03-09 3e6920900970a94d5f6a840951963d968475d6bb
1
已重命名5个文件
已添加8个文件
已修改1个文件
133 ■■■■■ 文件已修改
ExportMap/Sources/Test.qgz 补丁 | 查看 | 原始文档 | blame | 历史
ExportMap/Sources/bak/Layout.qpt 补丁 | 查看 | 原始文档 | blame | 历史
ExportMap/Sources/bak/Lengend.png 补丁 | 查看 | 原始文档 | blame | 历史
ExportMap/Sources/bak/geo.png 补丁 | 查看 | 原始文档 | blame | 历史
ExportMap/Sources/bak/geo_render.py 补丁 | 查看 | 原始文档 | blame | 历史
ExportMap/Sources/bak/logo2.png 补丁 | 查看 | 原始文档 | blame | 历史
ExportMap/Sources/bak/main.py 补丁 | 查看 | 原始文档 | blame | 历史
ExportMap/Sources/bak/qgis_py.png 补丁 | 查看 | 原始文档 | blame | 历史
ExportMap/Sources/bak/render1.png 补丁 | 查看 | 原始文档 | blame | 历史
ExportMap/Sources/bak/render2.png 补丁 | 查看 | 原始文档 | blame | 历史
ExportMap/Sources/bak/render_bak.py 补丁 | 查看 | 原始文档 | blame | 历史
ExportMap/Sources/bak/ui.py 补丁 | 查看 | 原始文档 | blame | 历史
ExportMap/Sources/xyz.py 133 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ExportMap/Sources/xyz.qgz 补丁 | 查看 | 原始文档 | blame | 历史
ExportMap/Sources/Test.qgz
Binary files differ
ExportMap/Sources/bak/Layout.qpt
ExportMap/Sources/bak/Lengend.png
ExportMap/Sources/bak/geo.png
ExportMap/Sources/bak/geo_render.py
ExportMap/Sources/bak/logo2.png
ExportMap/Sources/bak/main.py
ExportMap/Sources/bak/qgis_py.png
ExportMap/Sources/bak/render1.png
ExportMap/Sources/bak/render2.png
ExportMap/Sources/bak/render_bak.py
ExportMap/Sources/bak/ui.py
ExportMap/Sources/xyz.py
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,133 @@
#!/usr/bin/env
# -*- coding: utf-8 -*-
import os
import sys
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 qgis.PyQt.QtXml import *
# èŽ·å–å®Œæ•´è·¯å¾„
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="Test.qgz", required=False)
    parser.add_argument("-qpt", type=str, default="Test.qpt", required=False)
    return parser.parse_args()
# è®¾ç½®å›¾å±‚可用性
def set_layers_valid(prj, show_layers):
    for layer in prj.mapLayers().values():
        flag = layer.name() in show_layers
        print(layer.name() + ": " + str(flag))
        # if not flag:
        # project.removeMapLayer(layer)
        layer.setValid(flag)
# è®¾ç½®å›¾å±‚可见性
def set_item_visibility(root_node, show_layers):
    print(root_node.name() + ': Group')
    for node in root_node.children():
        # QgsLayerTreeLayer
        if isinstance(node, QgsLayerTreeGroup):
            set_item_visibility(node, show_layers)
        # QgsRasterLayer, QgsVectorLayer
        else:
            name = node.layer().name()
            flag = name in showLayers
            print(name + ": " + str(flag))
            # if not flag:
            # root_node.removeLayer(node.layer())
            # node.layer().setValid(False)
            node.setItemVisibilityChecked(flag)
# åˆå§‹åŒ–
# QgsApplication.setPrefixPath(r"E:/terrait/TianJin/LFServer/QGIS/", True)
qgs = QgsApplication([], True)
qgs.initQgis()
# åŠ è½½å·¥ç¨‹
args = get_args()
project = QgsProject.instance()
# project.read(r'Test.qgz')
project.read(os.path.join(args.src, args.qgz))
print("FileName: " + project.fileName())
# layout = QgsPrintLayout(project)
layout = QgsLayout(project)
# layout.initializeDefaults()
# åŠ è½½æ¨¡æ¿
# with open(r'Test.qpt', 'r', encoding='utf-8') as f:
with open(os.path.join(args.src, args.qpt), 'r', encoding='utf-8') as f:
    template_content = f.read()
doc = QDomDocument()
doc.setContent(template_content)
layout.loadFromTemplate(doc, QgsReadWriteContext(), True)
# map = layout.itemById(r'地图')
map = layout.referenceMap()
map.zoomToExtent(map.extent())
scale = '1:' + str(math.ceil(map.scale() / 100) * 100)
print(scale)
# template_content = template_content.replace('1:25万', scale)
# doc.setContent(template_content)
# layout.loadFromTemplate(doc, QgsReadWriteContext(), True)
# map = layout.referenceMap()
# map.zoomToExtent(map.extent())
size = map.sizeWithUnits()
print('size: ' + str(size.width()) + " * " + str(size.height()) + " mm")
if size.width() > 280 or size.height() > 174:
    map.attemptResize(QgsLayoutSize(280, 174, QgsUnitTypes.LayoutMillimeters))
    print('size: ' + str(map.sizeWithUnits().width()) + " * " + str(map.sizeWithUnits().height()) + " mm")
# è®¾ç½®å›¾å±‚
# "阀室", "站场", "管道中心线", "矢量注记", "矢量图", "高德影像注记", "高德影像", "影像注记", "影像图"
# showLayers = ["阀室", "管道中心线", "矢量注记", "矢量图"]
showLayers = layout.customProperty("layers").split(',')
root = project.layerTreeRoot()
# set_layers_valid(project, showLayers)
set_item_visibility(root, showLayers)
# sm = layout.itemById(r'说明')
table = layout.multiFrames()[0]
contents = table.tableContents()
contents[1][1] = scale
table.refreshAttributes()
table.refresh()
# collection = layout.pageCollection()
# page = collection.page(0)
# page.refreshItemSize()
# åˆ·æ–°
map.refresh()
# layout.updateBounds()
layout.renderContext()
layout.refresh()
# å¯¼å‡º
exporter = QgsLayoutExporter(layout)
# img_path = os.path.join(r"E:/terrait/TianJin/LFServer/QGIS", "render.png")
img_path = layout.customProperty("imgPath")
exporter.exportToImage(img_path, QgsLayoutExporter.ImageExportSettings())
# é€€å‡º
qgs.exitQgis()
ExportMap/Sources/xyz.qgz
Binary files differ