Calculate.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import os
  2. import geopandas as gpd
  3. import pandas as pd
  4. from pyproj import Transformer
  5. from shapely.geometry import Point
  6. import numpy as np
  7. # 设置工作目录
  8. os.chdir(r"D:\土壤-大创\Irrigation_Water\Raster")
  9. # 读取 Shp 文件
  10. shp_file = r"..\Raster\四县三种用电.shp"
  11. gdf_shp = gpd.read_file(shp_file)
  12. str = "旱地"
  13. # 筛选出字段 DLMC 为指定类型的面要素
  14. gdf_shp = gdf_shp[gdf_shp['DLMC'] == str]
  15. # 定义坐标系转换对象(从原始坐标系转换为WGS84)
  16. transformer = Transformer.from_crs(gdf_shp.crs, "EPSG:4326", always_xy=True)
  17. # 读取 xls 文件
  18. xls_file = r"..\Data\Irrigation_water_SamplingPoint.xlsx"
  19. df_xls = pd.read_excel(xls_file)
  20. # 新建重金属含量字段,指定数据类型为双精度浮点数
  21. gdf_shp['Cd'] = pd.Series(dtype=np.float64)
  22. gdf_shp['Hg'] = pd.Series(dtype=np.float64)
  23. gdf_shp['Cr'] = pd.Series(dtype=np.float64)
  24. gdf_shp['Pb'] = pd.Series(dtype=np.float64)
  25. gdf_shp['As'] = pd.Series(dtype=np.float64)
  26. # 根据面要素类型设置系数
  27. A = 711 * 0.524
  28. B = 427 * 0.599
  29. C = 200 * 0.7
  30. Num = 0.0
  31. if str == "水浇地":
  32. Num = B
  33. elif str == "水田":
  34. Num = A
  35. else:
  36. Num = C
  37. # 创建一个空列表来存储数据行
  38. data_rows = []
  39. # 遍历面要素
  40. for index, row in gdf_shp.iterrows():
  41. # 获取面要素的中心并转换坐标
  42. center_original = row['geometry'].centroid
  43. center_lon, center_lat = transformer.transform(center_original.x, center_original.y)
  44. center_transformed = Point(center_lon, center_lat)
  45. # 计算面要素中心与采样点的距离
  46. distances = df_xls.apply(lambda x: center_transformed.distance(Point(x['经度'], x['纬度'])), axis=1)
  47. # 找到距离最近的采样点
  48. min_distance_index = distances.idxmin()
  49. nearest_sample = df_xls.loc[min_distance_index]
  50. # 将最近采样点的值赋给面要素,并乘以系数Num
  51. cd_value = nearest_sample['Cd (ug/L)'] * Num
  52. hg_value = nearest_sample['Hg (ug/L)'] * Num
  53. cr_value = nearest_sample['Cr (ug/L)'] * Num
  54. pb_value = nearest_sample['Pb (ug/L)'] * Num
  55. as_value = nearest_sample['As (ug/L)'] * Num
  56. # 更新面要素的重金属含量
  57. gdf_shp.at[index, 'Cd'] = cd_value
  58. gdf_shp.at[index, 'Hg'] = hg_value
  59. gdf_shp.at[index, 'Cr'] = cr_value
  60. gdf_shp.at[index, 'Pb'] = pb_value
  61. gdf_shp.at[index, 'As'] = as_value
  62. # 将数据行添加到列表中
  63. data_rows.append({
  64. '面要素ID': index,
  65. 'DLMC': row['DLMC'],
  66. '中心点经度': center_lon,
  67. '中心点纬度': center_lat,
  68. '最近采样点ID': nearest_sample.name,
  69. '采样点经度': nearest_sample['经度'],
  70. '采样点纬度': nearest_sample['纬度'],
  71. 'Cd (ug/L)': cd_value,
  72. 'Hg (ug/L)': hg_value,
  73. 'Cr (ug/L)': cr_value,
  74. 'Pb (ug/L)': pb_value,
  75. 'As (ug/L)': as_value
  76. })
  77. # 从列表创建DataFrame
  78. csv_data = pd.DataFrame(data_rows)
  79. # 保存结果为CSV文件 - 修复路径中的转义字符问题
  80. csv_file_path = os.path.join('D:\土壤-大创\Water\Data\四县三用地&旱地.csv')
  81. csv_data.to_csv(csv_file_path, index=False, encoding='utf-8-sig')
  82. print(f"CSV文件已保存至: {csv_file_path}")