irrigationWater.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. <template>
  2. <div class="page-container">
  3. <!-- 上半部分:地图 + 柱状图 -->
  4. <div class="top-content">
  5. <!-- 灌溉水输入通量计算模块 -->
  6. <div class="irrigation-form">
  7. <div class="form-header">
  8. <h3>灌溉水输入通量计算</h3>
  9. </div>
  10. <div class="form-body">
  11. <div class="land-form-row">
  12. <!-- 水田 -->
  13. <div class="land-form">
  14. <h4>水田</h4>
  15. <el-form :model="irrigationData.paddy" label-position="top">
  16. <el-form-item label="灌溉水用量 (吨/公顷)">
  17. <el-input-number
  18. v-model="irrigationData.paddy.usage"
  19. :min="0"
  20. :step="100"
  21. ></el-input-number>
  22. </el-form-item>
  23. <el-form-item label="灌溉水有效利用率 (%)">
  24. <el-input-number
  25. v-model="irrigationData.paddy.efficiency"
  26. :min="0"
  27. :max="100"
  28. :step="5"
  29. ></el-input-number>
  30. </el-form-item>
  31. <el-form-item label="计算通量(吨/公顷·年)">
  32. <el-input
  33. v-model="irrigationData.paddy.flux"
  34. readonly
  35. class="flux-input"
  36. >
  37. </el-input>
  38. </el-form-item>
  39. <el-button
  40. type="primary"
  41. @click="calculateFlux('paddy')"
  42. class="calculate-btn"
  43. >计算水田通量</el-button>
  44. </el-form>
  45. </div>
  46. <!-- 水浇地 -->
  47. <div class="land-form">
  48. <h4>水浇地</h4>
  49. <el-form :model="irrigationData.irrigated" label-position="top">
  50. <el-form-item label="灌溉水用量 (吨/公顷)">
  51. <el-input-number
  52. v-model="irrigationData.irrigated.usage"
  53. :min="0"
  54. :step="100"
  55. ></el-input-number>
  56. </el-form-item>
  57. <el-form-item label="灌溉水有效利用率 (%)">
  58. <el-input-number
  59. v-model="irrigationData.irrigated.efficiency"
  60. :min="0"
  61. :max="100"
  62. :step="5"
  63. ></el-input-number>
  64. </el-form-item>
  65. <el-form-item label="计算通量(吨/公顷·年)">
  66. <el-input
  67. v-model="irrigationData.irrigated.flux"
  68. readonly
  69. class="flux-input"
  70. >
  71. </el-input>
  72. </el-form-item>
  73. <el-button
  74. type="primary"
  75. @click="calculateFlux('irrigated')"
  76. class="calculate-btn"
  77. >计算水浇地通量</el-button>
  78. </el-form>
  79. </div>
  80. <!-- 旱地 -->
  81. <div class="land-form">
  82. <h4>旱地</h4>
  83. <el-form :model="irrigationData.dryland" label-position="top">
  84. <el-form-item label="灌溉水用量 (吨/公顷)">
  85. <el-input-number
  86. v-model="irrigationData.dryland.usage"
  87. :min="0"
  88. :step="100"
  89. ></el-input-number>
  90. </el-form-item>
  91. <el-form-item label="灌溉水有效利用率 (%)">
  92. <el-input-number
  93. v-model="irrigationData.dryland.efficiency"
  94. :min="0"
  95. :max="100"
  96. :step="5"
  97. ></el-input-number>
  98. </el-form-item>
  99. <el-form-item label="计算通量(吨/公顷·年)">
  100. <el-input
  101. v-model="irrigationData.dryland.flux"
  102. readonly
  103. class="flux-input"
  104. >
  105. </el-input>
  106. </el-form-item>
  107. <el-button
  108. type="primary"
  109. @click="calculateFlux('dryland')"
  110. class="calculate-btn"
  111. >计算旱地通量</el-button>
  112. </el-form>
  113. </div>
  114. </div>
  115. </div>
  116. </div>
  117. <!-- 右侧图表区域 -->
  118. <div class="graphics-container">
  119. <!-- 下拉选择栏 -->
  120. <div class="land-type-selector">
  121. <el-select v-model="selectedLandType" placeholder="选择土地类型" @change="handleLandTypeChange">
  122. <el-option label="水田" value="paddy"></el-option>
  123. <el-option label="水浇地" value="irrigated"></el-option>
  124. <el-option label="旱地" value="dryland"></el-option>
  125. </el-select>
  126. </div>
  127. <!-- 图表容器 -->
  128. <div class="graphics-row">
  129. <!-- 地图模块 -->
  130. <div class="map-module">
  131. <div v-if="rasterMapImage" class="map-box">
  132. <img :src="rasterMapImage" alt="土地类型Cd分布图" class="raster-image">
  133. </div>
  134. <div v-else-if="mapLoading" class="map-box">地图加载中...</div>
  135. <div v-else class="map-box error-message">
  136. 地图加载失败: {{ mapError }}
  137. <el-button type="primary" size="small" @click="retryMap">重试</el-button>
  138. </div>
  139. </div>
  140. <!-- 图表模块 -->
  141. <div class="chart-module">
  142. <div v-if="histogramImage" class="chart-box">
  143. <img :src="histogramImage" alt="数据分布直方图" class="histogram-image">
  144. </div>
  145. <div v-else-if="histogramLoading" class="chart-box">直方图加载中...</div>
  146. <div v-else class="chart-box error-message">
  147. 直方图加载失败: {{ histogramError }}
  148. <el-button type="primary" size="small" @click="retryHistogram">重试</el-button>
  149. </div>
  150. </div>
  151. </div>
  152. </div>
  153. </div>
  154. <!-- 中间间距 -->
  155. <div class="middle-gap"></div>
  156. <!-- 下半部分:表格 -->
  157. <div class="table-module">
  158. <div class="table-header">
  159. <h3>计算结果汇总</h3>
  160. <el-button type="primary" @click="exportData">导出数据</el-button>
  161. </div>
  162. <el-table
  163. :data="tableData"
  164. style="width: 100%"
  165. border
  166. stripe
  167. class="compact-table"
  168. >
  169. <el-table-column prop="name" label="土地类型" width="120" />
  170. <el-table-column prop="area" label="面积 (公顷)" width="100" />
  171. <el-table-column prop="quality" label="质量等级" width="100" />
  172. <el-table-column prop="productivity" label="生产力指数" width="120" />
  173. <el-table-column prop="waterUsage" label="灌溉水用量" width="120" />
  174. <el-table-column prop="waterEfficiency" label="利用率" width="100" />
  175. <el-table-column prop="waterFlux" label="通量" width="120" />
  176. <el-table-column label="操作" width="80">
  177. <template #default="{ row }">
  178. <el-button type="primary" size="small" @click="editItem(row)">修改</el-button>
  179. </template>
  180. </el-table-column>
  181. </el-table>
  182. </div>
  183. </div>
  184. </template>
  185. <script setup>
  186. import { ref, onMounted, reactive, watch } from 'vue';
  187. import axios from 'axios';
  188. // 配置API基础URL
  189. const BASE_URL = 'http://localhost:8000/api/water';
  190. // 创建自定义axios实例
  191. const http = axios.create({
  192. timeout: 60000, // 60秒超时
  193. });
  194. // 土地类型数据映射
  195. const landTypeData = {
  196. paddy: {
  197. shp_base_name: 'lechang',
  198. tif_type: '水田',
  199. properties: {
  200. area: 1200,
  201. quality: '优',
  202. productivity: 850
  203. }
  204. },
  205. dryland: {
  206. shp_base_name: 'lechang',
  207. tif_type: '旱地',
  208. properties: {
  209. area: 800,
  210. quality: '良',
  211. productivity: 600
  212. }
  213. },
  214. irrigated: {
  215. shp_base_name: 'lechang',
  216. tif_type: '水浇地',
  217. properties: {
  218. area: 950,
  219. quality: '中',
  220. productivity: 720
  221. }
  222. }
  223. };
  224. // 灌溉水计算数据
  225. const irrigationData = reactive({
  226. paddy: {
  227. usage: 8000,
  228. efficiency: 60,
  229. flux: 4800
  230. },
  231. irrigated: {
  232. usage: 6000,
  233. efficiency: 70,
  234. flux: 4200
  235. },
  236. dryland: {
  237. usage: 4000,
  238. efficiency: 50,
  239. flux: 2000
  240. }
  241. });
  242. // 响应式数据
  243. const selectedLandType = ref('paddy');
  244. const rasterMapImage = ref(null);
  245. const histogramImage = ref(null);
  246. const tableData = ref([]);
  247. // 加载状态和错误信息
  248. const mapLoading = ref(false);
  249. const histogramLoading = ref(false);
  250. const mapError = ref('');
  251. const histogramError = ref('');
  252. // 初始化数据和表格
  253. const initTableData = () => {
  254. tableData.value = [
  255. {
  256. id: 1,
  257. name: '水田',
  258. ...landTypeData.paddy.properties,
  259. waterUsage: irrigationData.paddy.usage,
  260. waterEfficiency: irrigationData.paddy.efficiency + '%',
  261. waterFlux: irrigationData.paddy.flux
  262. },
  263. {
  264. id: 2,
  265. name: '水浇地',
  266. ...landTypeData.irrigated.properties,
  267. waterUsage: irrigationData.irrigated.usage,
  268. waterEfficiency: irrigationData.irrigated.efficiency + '%',
  269. waterFlux: irrigationData.irrigated.flux
  270. },
  271. {
  272. id: 3,
  273. name: '旱地',
  274. ...landTypeData.dryland.properties,
  275. waterUsage: irrigationData.dryland.usage,
  276. waterEfficiency: irrigationData.dryland.efficiency + '%',
  277. waterFlux: irrigationData.dryland.flux
  278. }
  279. ];
  280. };
  281. // 计算灌溉水通量
  282. const calculateFlux = (landType) => {
  283. const data = irrigationData[landType];
  284. data.flux = Math.round(data.usage * data.efficiency / 100);
  285. // 更新表格数据
  286. const row = tableData.value.find(item =>
  287. landType === 'paddy' ? item.name === '水田' :
  288. landType === 'irrigated' ? item.name === '水浇地' :
  289. item.name === '旱地'
  290. );
  291. if (row) {
  292. row.waterUsage = data.usage;
  293. row.waterEfficiency = data.efficiency + '%';
  294. row.waterFlux = data.flux;
  295. }
  296. };
  297. // 导出数据功能
  298. const exportData = () => {
  299. alert('导出数据功能已触发,数据已准备好下载');
  300. // 实际应用中这里会生成并下载CSV文件
  301. };
  302. // 修改条目功能
  303. const editItem = (row) => {
  304. alert(`正在修改 ${row.name} 的数据`);
  305. // 实际应用中这里会打开编辑模态框
  306. };
  307. onMounted(() => {
  308. initTableData();
  309. handleLandTypeChange();
  310. });
  311. // 监听灌溉数据变化更新表格
  312. watch(irrigationData, (newVal) => {
  313. tableData.value = tableData.value.map(item => {
  314. if (item.name === '水田') {
  315. return {...item,
  316. waterUsage: newVal.paddy.usage,
  317. waterEfficiency: newVal.paddy.efficiency + '%',
  318. waterFlux: newVal.paddy.flux};
  319. } else if (item.name === '水浇地') {
  320. return {...item,
  321. waterUsage: newVal.irrigated.usage,
  322. waterEfficiency: newVal.irrigated.efficiency + '%',
  323. waterFlux: newVal.irrigated.flux};
  324. } else {
  325. return {...item,
  326. waterUsage: newVal.dryland.usage,
  327. waterEfficiency: newVal.dryland.efficiency + '%',
  328. waterFlux: newVal.dryland.flux};
  329. }
  330. });
  331. }, { deep: true });
  332. const handleLandTypeChange = async () => {
  333. const landType = selectedLandType.value;
  334. const data = landTypeData[landType];
  335. // 重置状态
  336. rasterMapImage.value = null;
  337. histogramImage.value = null;
  338. mapError.value = '';
  339. histogramError.value = '';
  340. try {
  341. // 设置加载状态
  342. mapLoading.value = true;
  343. histogramLoading.value = true;
  344. // 顺序请求,避免同时处理大文件导致问题
  345. await generateRasterMap(data.shp_base_name, data.tif_type);
  346. await generateHistogram(data.tif_type);
  347. } catch (error) {
  348. console.error('数据加载失败:', error);
  349. } finally {
  350. // 重置加载状态
  351. mapLoading.value = false;
  352. histogramLoading.value = false;
  353. }
  354. };
  355. const generateRasterMap = async (shp_base_name, tif_type) => {
  356. try {
  357. const formData = new FormData();
  358. formData.append('shp_base_name', shp_base_name);
  359. formData.append('tif_type', tif_type);
  360. formData.append('color_map_name', 'colormap6');
  361. formData.append('title_name', `${getLandTypeName(selectedLandType.value)}Cd分布图`);
  362. formData.append('output_size', '8'); // 减小输出尺寸
  363. const response = await http.post(`${BASE_URL}/generate-raster-map`, formData, {
  364. headers: { 'Content-Type': 'multipart/form-data' },
  365. responseType: 'blob'
  366. });
  367. if (response.data.size === 0) {
  368. throw new Error('后端返回空响应');
  369. }
  370. const blob = new Blob([response.data], { type: 'image/jpeg' });
  371. rasterMapImage.value = URL.createObjectURL(blob);
  372. mapError.value = '';
  373. } catch (error) {
  374. console.error('栅格地图生成失败:', error);
  375. mapError.value = error.response?.data?.message || error.message || '未知错误';
  376. throw error;
  377. }
  378. };
  379. const generateHistogram = async (tif_type) => {
  380. try {
  381. const formData = new FormData();
  382. formData.append('tif_type', tif_type);
  383. formData.append('figsize_width', '8'); // 减小宽度
  384. formData.append('figsize_height', '6'); // 减小高度
  385. formData.append('xlabel', '像元值');
  386. formData.append('ylabel', '频率密度');
  387. formData.append('title', `${getLandTypeName(selectedLandType.value)}数据分布`);
  388. const response = await http.post(`${BASE_URL}/generate-tif-histogram`, formData, {
  389. headers: { 'Content-Type': 'multipart/form-data' },
  390. responseType: 'blob'
  391. });
  392. if (response.data.size === 0) {
  393. throw new Error('后端返回空响应');
  394. }
  395. const blob = new Blob([response.data], { type: 'image/jpeg' });
  396. histogramImage.value = URL.createObjectURL(blob);
  397. histogramError.value = '';
  398. } catch (error) {
  399. console.error('直方图生成失败:', error);
  400. histogramError.value = error.response?.data?.message || error.message || '未知错误';
  401. throw error;
  402. }
  403. };
  404. const getLandTypeName = (value) => {
  405. switch (value) {
  406. case 'paddy': return '水田';
  407. case 'dryland': return '旱地';
  408. case 'irrigated': return '水浇地';
  409. default: return '';
  410. }
  411. };
  412. // 重试功能
  413. const retryMap = async () => {
  414. const landType = selectedLandType.value;
  415. const data = landTypeData[landType];
  416. mapLoading.value = true;
  417. mapError.value = '';
  418. try {
  419. await generateRasterMap(data.shp_base_name, data.tif_type);
  420. } catch (error) {
  421. console.error('重试栅格地图失败:', error);
  422. } finally {
  423. mapLoading.value = false;
  424. }
  425. };
  426. const retryHistogram = async () => {
  427. const landType = selectedLandType.value;
  428. const data = landTypeData[landType];
  429. histogramLoading.value = true;
  430. histogramError.value = '';
  431. try {
  432. await generateHistogram(data.tif_type);
  433. } catch (error) {
  434. console.error('重试直方图失败:', error);
  435. } finally {
  436. histogramLoading.value = false;
  437. }
  438. };
  439. </script>
  440. <style scoped>
  441. .page-container {
  442. display: flex;
  443. flex-direction: column;
  444. height: 100vh;
  445. padding: 20px;
  446. box-sizing: border-box;
  447. background-color: #f5f7fa;
  448. }
  449. .top-content {
  450. display: flex;
  451. height: 45%;
  452. gap: 20px; /* 增加间距 */
  453. }
  454. .irrigation-form {
  455. flex: 0 0 45%;
  456. background: white;
  457. border-radius: 12px;
  458. box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  459. display: flex;
  460. flex-direction: column;
  461. }
  462. .form-header {
  463. background-color: #2c3e50;
  464. color: white;
  465. padding: 10px 15px;
  466. text-align: center;
  467. font-size: 16px;
  468. font-weight: bold;
  469. }
  470. .form-body {
  471. padding: 15px;
  472. flex: 1;
  473. display: flex;
  474. flex-direction: column;
  475. }
  476. .land-form-row {
  477. display: flex;
  478. gap: 20px; /* 增加间距 */
  479. height: 100%;
  480. }
  481. .land-form {
  482. flex: 1;
  483. border: 1px solid #e1e4e8;
  484. border-radius: 8px;
  485. padding: 15px; /* 增加内边距 */
  486. box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
  487. }
  488. .land-form h4 {
  489. margin-top: 0;
  490. margin-bottom: 15px; /* 增加下边距 */
  491. color: #1a73e8;
  492. border-bottom: 1px solid #e1e4e8;
  493. padding-bottom: 10px; /* 增加下边距 */
  494. font-size: 14px;
  495. }
  496. /* 修复通量数字显示不全 */
  497. .flux-input {
  498. width: 100%; /* 确保输入框宽度填满 */
  499. }
  500. .flux-input >>> .el-input__inner {
  501. text-align: left; /* 数字右对齐 */
  502. padding-right: 10px; /* 增加右边距 */
  503. font-weight: bold; /* 加粗显示 */
  504. }
  505. .calculate-btn {
  506. margin-top: 15px; /* 增加按钮上边距 */
  507. }
  508. .graphics-container {
  509. flex: 1;
  510. display: flex;
  511. flex-direction: column;
  512. gap: 15px; /* 增加间距 */
  513. }
  514. .land-type-selector {
  515. margin-bottom: 15px; /* 增加下边距 */
  516. }
  517. .graphics-row {
  518. display: flex;
  519. gap: 15px; /* 增加间距 */
  520. height: calc(100% - 45px); /* 减去选择器高度 */
  521. }
  522. .map-module, .chart-module {
  523. flex: 1;
  524. background: white;
  525. border-radius: 8px;
  526. box-shadow: 0 0 8px rgba(0, 0, 0, 0.1);
  527. display: flex;
  528. justify-content: center;
  529. align-items: center;
  530. overflow: hidden;
  531. height: 100%; /* 固定高度 */
  532. }
  533. .map-box, .chart-box {
  534. width: 100%;
  535. height: 100%;
  536. display: flex;
  537. justify-content: center;
  538. align-items: center;
  539. position: relative;
  540. padding: 10px; /* 增加内边距 */
  541. }
  542. .raster-image, .histogram-image {
  543. width: 100%; /* 宽度固定为容器宽度 */
  544. height: 100%; /* 高度固定为容器高度 */
  545. object-fit: contain; /* 保持比例 */
  546. }
  547. .error-message {
  548. color: #f56c6c;
  549. font-size: 13px;
  550. padding: 10px; /* 增加内边距 */
  551. text-align: center;
  552. display: flex;
  553. flex-direction: column;
  554. align-items: center;
  555. gap: 10px; /* 增加间距 */
  556. }
  557. .middle-gap {
  558. height: 20px; /* 增加中间间距高度 */
  559. }
  560. .table-module {
  561. height: 50%;
  562. background: white;
  563. border-radius: 12px;
  564. box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  565. display: flex;
  566. flex-direction: column;
  567. margin-top: 50px; /* 增加上边距 */
  568. }
  569. .table-header {
  570. display: flex;
  571. justify-content: space-between;
  572. align-items: center;
  573. padding: 15px 20px; /* 增加内边距 */
  574. background-color: #f8f9fa;
  575. border-bottom: 1px solid #e1e4e8;
  576. }
  577. .table-header h3 {
  578. margin: 0;
  579. font-size: 16px; /* 增加字体大小 */
  580. color: #2c3e50;
  581. }
  582. .compact-table {
  583. width: 100%;
  584. height: 100%;
  585. font-size: 13px;
  586. }
  587. .compact-table >>> .el-table__cell {
  588. padding: 10px 0; /* 增加单元格内边距 */
  589. }
  590. /* 修复表格中数字显示 */
  591. .compact-table >>> .el-table__cell .cell {
  592. padding: 0 10px; /* 增加单元格内边距 */
  593. text-align: center; /* 居中显示 */
  594. white-space: nowrap; /* 防止换行 */
  595. overflow: hidden; /* 隐藏溢出 */
  596. text-overflow: ellipsis; /* 显示省略号 */
  597. }
  598. </style>