From 5cf24cb110a05a90ceba4a31d97cafb61a178859 Mon Sep 17 00:00:00 2001 From: gisfan Date: Mon, 23 Sep 2024 14:22:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- com.vgis.python.gis/setup.py | 2 +- com.vgis.python.gis/vgis_gis/gdalTools.py | 61 +++++++++++++++++++++++ com.vgis.python.gis/vgis_gis/shpTools.py | 2 +- com.vgis.python.rs/setup.py | 2 +- com.vgis.python.rs/vgis_rs/tifTools.py | 14 ++++-- 5 files changed, 73 insertions(+), 8 deletions(-) create mode 100644 com.vgis.python.gis/vgis_gis/gdalTools.py diff --git a/com.vgis.python.gis/setup.py b/com.vgis.python.gis/setup.py index c4c80c1..bb7799d 100644 --- a/com.vgis.python.gis/setup.py +++ b/com.vgis.python.gis/setup.py @@ -24,7 +24,7 @@ setup( name="vgis_gis", # Required 项目名称 - version="1.3.9", # Required 发布版本号 + version="1.4.1", # Required 发布版本号 description="A libary for gis operator", # Optional 项目简单描述 long_description=long_description, # Optional 详细描述 diff --git a/com.vgis.python.gis/vgis_gis/gdalTools.py b/com.vgis.python.gis/vgis_gis/gdalTools.py new file mode 100644 index 0000000..81725e1 --- /dev/null +++ b/com.vgis.python.gis/vgis_gis/gdalTools.py @@ -0,0 +1,61 @@ +import json +import subprocess + + +class GdalHelper: + + def __init__(self): + pass + + + def get_projection_by_gdalinfo(self,tif_path): + # 执行cmd命令 + cmd = "gdalinfo -json {}".format(tif_path) + result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, encoding="utf-8") + + # 获取标准输出和错误信息 + stdout = result.stdout + stderr = result.stderr + + # 打印输出结果 + # print(stdout) + + # 如果有错误信息,也打印它们 + if stderr: + print("错误信息:" + stderr) + info_dict = json.loads(stdout) + proj_wkt = info_dict['coordinateSystem']['wkt'] + # 如果调用gdalinfo命令报错, + # windows环境,在这里单元测试通过staticmethod方法会报这些错,但是普通方法不报错,如果打包好vgis-rs,在别的地方调用没问题 + # linux环境,不会出现这些问题 + # ERROR 1: PROJ: proj_create_from_database: C:\Program Files\gdal\bin\proj6\share\proj.db lacks DATABASE.LAYOUT.VERSION.MAJOR / DATABASE.LAYOUT.VERSION.MINOR metadata. It comes from another PROJ installation. + # ERROR 1: PROJ: proj_create_from_database: C:\Program Files\gdal\bin\proj6\share\proj.db lacks DATABASE.LAYOUT.VERSION.MAJOR / DATABASE.LAYOUT.VERSION.MINOR metadata. It comes from another PROJ installation. + # ERROR 1: PROJ: proj_get_ellipsoid: CRS has no geodetic CRS + # ERROR 1: PROJ: proj_get_ellipsoid: Object is not a CRS or GeodeticReferenceFrame + # 这个时候得到的projection是不完整的,开头:ENGCRS["WGS_1984_Web_Mercator_Auxiliary_Sphere", + print(proj_wkt) + return proj_wkt + + + # 通过gdalsrsinfo命令获取完整的epsg + def get_epsg_of_geo_file(self,geo_file_path): + # 执行cmd命令 + cmd = "gdalsrsinfo {} -o epsg".format(geo_file_path) + print(cmd) + result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, + encoding="utf-8") + + # 获取标准输出和错误信息 + stdout = result.stdout + stderr = result.stderr + + # 打印输出结果 + # print(stdout) + + # 如果有错误信息,也打印它们 + if stderr: + print("错误信息:" + stderr) + epsg = stdout.replace("\n", "").lstrip("EPSG:").lstrip("epsg:") + + print(epsg) + return epsg \ No newline at end of file diff --git a/com.vgis.python.gis/vgis_gis/shpTools.py b/com.vgis.python.gis/vgis_gis/shpTools.py index aadec2e..db89b89 100644 --- a/com.vgis.python.gis/vgis_gis/shpTools.py +++ b/com.vgis.python.gis/vgis_gis/shpTools.py @@ -194,7 +194,7 @@ def get_epsg_of_shp_v2(shp_path): # 执行cmd命令 cmd = "gdalsrsinfo {} -o epsg".format(shp_path) print(cmd) - result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, encoding="utf-8") # 获取标准输出和错误信息 stdout = result.stdout diff --git a/com.vgis.python.rs/setup.py b/com.vgis.python.rs/setup.py index 3cfd640..a9d9c47 100644 --- a/com.vgis.python.rs/setup.py +++ b/com.vgis.python.rs/setup.py @@ -24,7 +24,7 @@ setup( name="vgis_rs", # Required 项目名称 - version="1.1.6", # Required 发布版本号 + version="1.1.8", # Required 发布版本号 description="A libary for rs operator", # Optional 项目简单描述 long_description=long_description, # Optional 详细描述 long_description_content_type="text/markdown", # 内容类型 diff --git a/com.vgis.python.rs/vgis_rs/tifTools.py b/com.vgis.python.rs/vgis_rs/tifTools.py index 09c8c0e..e6645d5 100644 --- a/com.vgis.python.rs/vgis_rs/tifTools.py +++ b/com.vgis.python.rs/vgis_rs/tifTools.py @@ -13,6 +13,7 @@ from pyproj import Transformer, CRS, Proj, transform from PIL import Image from osgeo import gdal, osr +from vgis_gis.gdalTools import GdalHelper class TifFileOperator: @@ -99,7 +100,8 @@ def read_tiff(input_file): def get_projection_by_gdalinfo(tif_path): # 执行cmd命令 cmd = "gdalinfo -json {}".format(tif_path) - result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, + encoding="utf-8") # 获取标准输出和错误信息 stdout = result.stdout @@ -140,7 +142,9 @@ def get_all_meta_of_tif(tif_path): envelop = [tif_minx, tif_miny, tif_maxx, tif_maxy] # 获取投影信息 - proj_wkt = TifFileOperator.get_projection_by_gdalinfo(tif_path) + # proj_wkt = TifFileOperator.get_projection_by_gdalinfo(tif_path) + gdalOpeprator = GdalHelper() + proj_wkt = gdalOpeprator.get_projection_by_gdalinfo(tif_path) # 获取波段数 band_count = dataset.RasterCount @@ -153,7 +157,6 @@ def get_all_meta_of_tif(tif_path): data_type = band.DataType bit_depth = gdal.GetDataTypeSize(data_type) - # 地理坐标,经纬度 # 转换为米,进行面积计算和分辨率计算 resolution = None @@ -228,7 +231,8 @@ def get_epsg_of_tif(shp_path): # 执行cmd命令 cmd = "gdalsrsinfo {} -o epsg".format(shp_path) print(cmd) - result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, + encoding="utf-8") # 获取标准输出和错误信息 stdout = result.stdout @@ -254,7 +258,7 @@ def get_epsg_of_tif(shp_path): test_tif_path = "/mnt/share/data/test_images/TW2015_4326.TIF" # cmd = "gdalinfo -json {}".format(test_tif_path) - # result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + # result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, encoding="utf-8") # # # 获取标准输出和错误信息 # stdout = result.stdout