Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
gisfanmachel committed Sep 23, 2024
1 parent 14e2433 commit 5cf24cb
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 8 deletions.
2 changes: 1 addition & 1 deletion com.vgis.python.gis/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 详细描述
Expand Down
61 changes: 61 additions & 0 deletions com.vgis.python.gis/vgis_gis/gdalTools.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion com.vgis.python.gis/vgis_gis/shpTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion com.vgis.python.rs/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", # 内容类型
Expand Down
14 changes: 9 additions & 5 deletions com.vgis.python.rs/vgis_rs/tifTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -153,7 +157,6 @@ def get_all_meta_of_tif(tif_path):
data_type = band.DataType
bit_depth = gdal.GetDataTypeSize(data_type)


# 地理坐标,经纬度
# 转换为米,进行面积计算和分辨率计算
resolution = None
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 5cf24cb

Please sign in to comment.