Transfer_csv_to_geotif.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. """
  2. Author: Wanxue Zhu
  3. Time: 2025 - April - 18
  4. Description: 这个代码用于将csv的文件转化为geotif;
  5. csv的文件格式为,第一列为long经度;
  6. 第二列为lat纬度,第三列为数值。
  7. """
  8. import pandas as pd
  9. import geopandas as gpd
  10. from shapely.geometry import Point
  11. import rasterio
  12. from rasterio.features import rasterize
  13. from rasterio.transform import from_origin
  14. import numpy as np
  15. import os
  16. os.chdir(r"D:\土壤-大创\Irrigation_Water\Data")
  17. #------------------1. 将csv转为点shape矢量数据,保存----------------------------
  18. def csv_to_shapefile(csv_file, shapefile_output, directory):
  19. """
  20. 将CSV文件转换为Shapefile文件,并保存到指定目录。
  21. csv_file (str): CSV文件的路径。
  22. shapefile_output (str): 输出Shapefile文件的路径。
  23. directory (str): 设置工作目录路径。
  24. """
  25. df = pd.read_csv(csv_file)
  26. lon = df.iloc[:, 0]
  27. lat = df.iloc[:, 1]
  28. val = df.iloc[:, 2]
  29. geometry = [Point(xy) for xy in zip(lon, lat)]
  30. gdf = gpd.GeoDataFrame(df, geometry=geometry, crs="EPSG:4326")
  31. gdf.to_file(shapefile_output, driver="ESRI Shapefile")
  32. # 调用函数
  33. csv_file = 'D:\土壤-大创\\Water\Data\matched_data.csv' # 请替换为你的CSV文件路径
  34. shapefile_output = r'..\Raster\旱地.shp' # 输出Shapefile路径
  35. directory = r"D:\土壤-大创\Project_20250419\Data" # 工作目录路径
  36. csv_to_shapefile(csv_file, shapefile_output, directory)
  37. # -----------------2. 将点shapefile转化为Geotif ----------------------------------
  38. def vector_to_raster(input_shapefile, template_tif, output_tif, field):
  39. """
  40. 将点矢量数据转换为栅格数据,输出与模板tif文件一致的GeoTIFF文件。
  41. input_shapefile (str): 输入点矢量数据的Shapefile文件路径。
  42. template_tif (str): 用作模板的GeoTIFF文件路径。
  43. output_tif (str): 输出栅格化后的GeoTIFF文件路径。
  44. field (str): 用于栅格化的属性字段名。
  45. """
  46. gdf = gpd.read_file(input_shapefile)
  47. with rasterio.open(template_tif) as src:# 打开模板栅格,获取元信息
  48. template_meta = src.meta.copy()
  49. transform = src.transform
  50. width = src.width
  51. height = src.height
  52. crs = src.crs
  53. # 将矢量数据投影到模板的坐标系
  54. if gdf.crs != crs:
  55. gdf = gdf.to_crs(crs)
  56. # 栅格化
  57. shapes = ((geom, value) for geom, value in zip(gdf.geometry, gdf[field]))
  58. raster = rasterize(
  59. shapes=shapes,
  60. out_shape=(height, width),
  61. transform=transform,
  62. fill=np.nan,
  63. dtype='float32'
  64. )
  65. # 创建输出目录(若不存在)
  66. os.makedirs(os.path.dirname(output_tif), exist_ok=True)
  67. # 写入 GeoTIFF,与模板完全一致
  68. template_meta.update({
  69. "count": 1,
  70. "dtype": 'float32',
  71. "nodata": np.nan
  72. })
  73. with rasterio.open(output_tif, 'w', **template_meta) as dst:
  74. dst.write(raster, 1)
  75. print("Save successfully:", output_tif)
  76. # 调用函数
  77. input_shapefile = r"..\Raster\旱地.shp"
  78. template_tif = r"..\Raster\meanTemp.tif" # 用作模板的 tif
  79. output_tif = r"..\Raster\旱地.tif"
  80. field = 'Prediction'
  81. vector_to_raster(input_shapefile, template_tif, output_tif, field)