file_util.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import os
  2. import rasterio
  3. from shapely.geometry import box
  4. def getAllFiles(filePath):
  5. fileList = []
  6. # 设置要遍历的文件夹路径
  7. folder_path = os.path.join(os.path.dirname(__file__), filePath)
  8. # 遍历文件夹
  9. for root, dirs, files in os.walk(folder_path):
  10. for file in files:
  11. file_path = os.path.join(root, file)
  12. if file_path.endswith('.tif'):
  13. fileList.append(file_path)
  14. return fileList
  15. # allPath = getAllFiles('img/DDOM')
  16. # 获取当前目录下的文件夹
  17. def getDirList(filePath,startName):
  18. dirList = []
  19. # 设置要遍历的文件夹路径
  20. folder_path = os.path.join(os.path.dirname(__file__), filePath)
  21. # 遍历文件夹
  22. for root, dirs, files in os.walk(folder_path):
  23. for dir in dirs:
  24. if dir.startswith(startName):
  25. dirList.append(filePath+'/'+dir)
  26. return dirList
  27. # getDirList('img')
  28. def getTifEwkt(filePath):
  29. # 读取 tif 文件
  30. with rasterio.open(filePath) as src:
  31. # 获取 tif 文件的边界框 (bounds)
  32. bounds = src.bounds
  33. # 使用 shapely 的 box 函数创建矩形几何对象
  34. bbox = box(bounds.left, bounds.bottom, bounds.right, bounds.top)
  35. # 将矩形几何对象转换为 WKT 格式
  36. wkt = bbox.wkt
  37. ewkt='SRID=4490;'+wkt
  38. print('矢量范围:'+wkt)
  39. return ewkt