forest_fire_condition.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from fastapi import APIRouter, Request, Depends, Query, HTTPException, status
  4. from common.security import valid_access_token
  5. from sqlalchemy.orm import Session
  6. from sqlalchemy.sql import func
  7. from common.auth_user import *
  8. from sqlalchemy import text
  9. from pydantic import BaseModel
  10. from common.BigDataCenterAPI import *
  11. from database import get_db
  12. from typing import List
  13. from utils import *
  14. from utils.risk import *
  15. import json
  16. import traceback
  17. router = APIRouter()
  18. @router.post('/get_max_forest')
  19. async def mine(request: Request,db: Session = Depends(get_db)):
  20. try:
  21. body = await request.json()
  22. result = get_max_forest_level(db)
  23. #print(result)
  24. result = get_warning_description(result)
  25. return {
  26. "code": 200,
  27. "msg": "成功",
  28. "data":
  29. {"max_level":result}
  30. }
  31. except Exception as e:
  32. db.rollback()
  33. traceback.print_exc()
  34. raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
  35. # def get_warning_description(level):
  36. # if level == 1:
  37. # return "无危险"
  38. # elif level == 2:
  39. # return "较低危险"
  40. # elif level == 3:
  41. # return "中等危险"
  42. # elif level == 4:
  43. # return "高火险级"
  44. # elif level == 5:
  45. # return "最高火险级"
  46. # else:
  47. # return "未知等级"
  48. def get_warning_description(level):
  49. if level == 1:
  50. return "无危险"
  51. elif level == 2:
  52. return "较低危险"
  53. elif level == 3:
  54. return "较高危险"
  55. elif level == 4:
  56. return "高度危险"
  57. elif level == 5:
  58. return "极度危险"
  59. @router.post('/forest_warrning')
  60. async def mine(request: Request,db: Session = Depends(get_db)):
  61. try:
  62. body = await request.json()
  63. max_time = get_forest_max_time(db)
  64. result = ''
  65. forest_warring = get_forest_warring(db)
  66. #print(forest_warring)
  67. # 分析数据
  68. high_risk_areas = []
  69. medium_risk_areas = []
  70. low_risk_areas = []
  71. all_low_risk = True # 假设所有区域都是低风险
  72. for item in forest_warring:
  73. if item['warning_level'] >= 2: # 只考虑等级大于等于2的
  74. all_low_risk = False
  75. level_description = get_warning_description(item['warning_level'])
  76. area_report = f"{item['area_name']}的森林火险等级为{level_description}({item['warning_level']}级)"
  77. if item['warning_level'] == 2:
  78. low_risk_areas.append(area_report)
  79. elif item['warning_level'] == 3:
  80. medium_risk_areas.append(area_report)
  81. elif item['warning_level'] >= 4:
  82. high_risk_areas.append(area_report)
  83. # 构建描述句子
  84. if all_low_risk:
  85. risk_description = "当前无风险"
  86. else:
  87. risk_description = ""
  88. if high_risk_areas:
  89. risk_description += ";".join(high_risk_areas)
  90. if medium_risk_areas:
  91. if risk_description:
  92. risk_description += ";"
  93. risk_description += ";".join(medium_risk_areas)
  94. if low_risk_areas:
  95. if risk_description:
  96. risk_description += ";"
  97. risk_description += ";".join(low_risk_areas)
  98. result = ''
  99. # 输出结果
  100. if risk_description:
  101. result = f"{max_time},{risk_description}"
  102. else:
  103. result =f"{max_time},当前无风险"
  104. #print(result)
  105. return {
  106. "code": 200,
  107. "msg": "成功",
  108. "data":
  109. {"result":result}
  110. }
  111. except Exception as e:
  112. db.rollback()
  113. traceback.print_exc()
  114. raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")