forest_fire_condition.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. @router.post('/forest_warrning')
  49. async def mine(request: Request,db: Session = Depends(get_db)):
  50. try:
  51. body = await request.json()
  52. max_time = get_forest_max_time(db)
  53. result = ''
  54. forest_warring = get_forest_warring(db)
  55. #print(forest_warring)
  56. # 分析数据
  57. high_risk_areas = []
  58. medium_risk_areas = []
  59. low_risk_areas = []
  60. all_low_risk = True # 假设所有区域都是低风险
  61. for item in forest_warring:
  62. if item['warning_level'] >= 2: # 只考虑等级大于等于2的
  63. all_low_risk = False
  64. level_description = get_warning_description(item['warning_level'])
  65. area_report = f"{item['area_name']}的森林火险等级为{level_description}({item['warning_level']}级)"
  66. if item['warning_level'] == 2:
  67. low_risk_areas.append(area_report)
  68. elif item['warning_level'] == 3:
  69. medium_risk_areas.append(area_report)
  70. elif item['warning_level'] >= 4:
  71. high_risk_areas.append(area_report)
  72. # 构建描述句子
  73. if all_low_risk:
  74. risk_description = "当前无风险"
  75. else:
  76. risk_description = ""
  77. if high_risk_areas:
  78. risk_description += ";".join(high_risk_areas)
  79. if medium_risk_areas:
  80. if risk_description:
  81. risk_description += ";"
  82. risk_description += ";".join(medium_risk_areas)
  83. if low_risk_areas:
  84. if risk_description:
  85. risk_description += ";"
  86. risk_description += ";".join(low_risk_areas)
  87. result = ''
  88. # 输出结果
  89. if risk_description:
  90. result = f"{max_time},{risk_description}"
  91. else:
  92. result =f"{max_time},当前无风险"
  93. #print(result)
  94. return {
  95. "code": 200,
  96. "msg": "成功",
  97. "data":
  98. {"result":result}
  99. }
  100. except Exception as e:
  101. db.rollback()
  102. traceback.print_exc()
  103. raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")