From b97fd60fff96b54cecd7ec0313e70edea1a07408 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期日, 27 八月 2023 20:57:34 +0800
Subject: [PATCH] 改进切图的坐标系设定

---
 ExportMap/Sources/xyz2.py |   29 +++++++++++++++++++++++------
 1 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/ExportMap/Sources/xyz2.py b/ExportMap/Sources/xyz2.py
index f69e57e..bb3be56 100644
--- a/ExportMap/Sources/xyz2.py
+++ b/ExportMap/Sources/xyz2.py
@@ -7,6 +7,7 @@
 import time
 
 sys.path.append(r"C:\Program Files\QGIS 3.16\apps\qgis-ltr\python\plugins")
+
 import math
 import argparse
 from qgis.core import *
@@ -54,6 +55,7 @@
     for layer in prj.mapLayers().values():
         prj.removeMapLayer(layer)
 
+    args.crs = None
     lines = read_txt(args.file)
     for i in range(0, len(lines)):
         if len(lines[i]) == 0:
@@ -62,11 +64,16 @@
         strs = lines[i].split(",")
         prefix = "__" if "1" == strs[1] else "_"
         print("layer" + prefix + str(i) + ": " + strs[0])
-
         layer = QgsRasterLayer(strs[0], "layer" + prefix + str(i))
+
         if not layer.isValid() or layer.crs() is None:
             print("layer" + prefix + str(i) + ": failed to load!")
             continue
+
+        if args.crs is None and len(layer.crs().authid()) > 0:
+            args.crs = layer.crs()
+            prj.setCrs(layer.crs())
+            print("prj.authid: " + args.crs.authid())
 
         for j in range(1, layer.bandCount() + 1):
             try:
@@ -113,15 +120,25 @@
 def create_xyz(prj, args):
     import processing
 
+    authid = args.crs.authid()
     for layer in prj.mapLayers().values():
         if "layer__" in layer.id():
             continue
 
-        e = layer.extent()
-        prj.setCrs(layer.crs())
-        epsg = '' if layer.crs() is None else ' [' + layer.crs().authid() + ']'
-        args.ext = str(e.xMinimum()) + "," + str(e.xMaximum()) + "," + str(e.yMinimum()) + "," + str(
-            e.yMaximum()) + epsg
+        rect = layer.extent()
+        if authid != layer.crs().authid():
+            transform = QgsCoordinateTransform(layer.crs(), args.crs, prj)
+            min = QgsPoint(rect.xMinimum(), rect.yMinimum())
+            max = QgsPoint(rect.xMaximum(), rect.yMaximum())
+            min.transform(transform)
+            max.transform(transform)
+
+            args.ext = str(min.x()) + "," + str(max.x()) + "," + \
+                       str(min.y()) + "," + str(max.y()) + ' [' + authid + ']'
+        else:
+            args.ext = str(rect.xMinimum()) + "," + str(rect.xMaximum()) + "," + \
+                       str(rect.yMinimum()) + "," + str(rect.yMaximum()) + ' [' + authid + ']'
+
         ops = get_xyz_ops(args)
         processing.run("qgis:tilesxyzdirectory", ops)
 

--
Gitblit v1.9.3