Calculate.py 3.1 KB

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