Transfer_csv_to_geotif.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #------------------1. 将csv转为点shape矢量数据,保存----------------------------
  17. def csv_to_shapefile(csv_file, shapefile_output, directory):
  18. """
  19. 将CSV文件转换为Shapefile文件,并保存到指定目录。
  20. csv_file (str): CSV文件的路径。
  21. shapefile_output (str): 输出Shapefile文件的路径。
  22. directory (str): 设置工作目录路径。
  23. """
  24. df = pd.read_csv(csv_file)
  25. lon = df.iloc[:, 0]
  26. lat = df.iloc[:, 1]
  27. val = df.iloc[:, 2]
  28. geometry = [Point(xy) for xy in zip(lon, lat)]
  29. gdf = gpd.GeoDataFrame(df, geometry=geometry, crs="EPSG:4326")
  30. gdf.to_file(shapefile_output, driver="ESRI Shapefile")
  31. # # 调用函数
  32. # csv_file = r'D:\17417\Documents\backend\Water\Data\matched_data.csv' # 请替换为你的CSV文件路径
  33. # shapefile_output = r'..\Raster\水浇地.shp' # 输出Shapefile路径
  34. # directory = r"D:\17417\Documents\backend\Water\Data" # 工作目录路径
  35. #
  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. #
  82. # vector_to_raster(input_shapefile, template_tif, output_tif, field)