__init__.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 models import *
  14. from utils import *
  15. from utils.spatial import *
  16. import json
  17. import traceback
  18. router = APIRouter()
  19. class location_c(BaseModel):
  20. x:float
  21. y:float
  22. class mine(BaseModel):
  23. location : List=[]
  24. @router.post('/get_info')
  25. async def mine(request: Request,body = Depends(remove_xss_json),db: Session = Depends(get_db)):
  26. try:
  27. # 验证必需的字段
  28. # required_fields = ['location','location_c']
  29. # missing_fields = [field for field in required_fields if field not in body]
  30. # if missing_fields:
  31. # raise HTTPException(status_code=401, detail=f"Missing required fields: {', '.join(missing_fields)}")
  32. # 行政镇、行政村数据
  33. # town_village_data,town_count,village_count = count_town_village(from_data.location,db)
  34. # town_village_data,town_count = get_town_list(body)
  35. town_village_data,town_count,village_count = get_town_village_list(body,db) #[],0,0#
  36. # emergency_expert_count = count_emergency_expert(from_data.location,db)
  37. # emergency_management_count = count_emergency_management(from_data.location,db)
  38. hospital_list = get_hospital_list(body,db)
  39. emergency_shelter_list = get_emergency_shelter_list(body,db)
  40. waterlogged_roads_list = get_waterlogged_roads_list(body,db)
  41. return {
  42. "code": 200,
  43. "msg": "成功",
  44. "data": {
  45. "townData":town_village_data,
  46. "townCount":town_count,
  47. "villageCount":village_count,
  48. # "villageCount":village_count,
  49. "populationSize":0,
  50. "areaSize":0,
  51. "GDP":0,
  52. "list":[{
  53. "name":"应急避难场所","num":len(emergency_shelter_list),"list":[{"name":shelter.name,"longitude":shelter.longitude,"latitude":shelter.latitude} for shelter in emergency_shelter_list]
  54. },{
  55. "name":"易涝点","num":len(waterlogged_roads_list),"list":[{"name":waterlogged.name,"longitude":waterlogged.longitude,"latitude":waterlogged.latitude} for waterlogged in waterlogged_roads_list]
  56. },{
  57. "name":"医院","num":len(hospital_list),"list":[{"name":hospital.name,"longitude":hospital.longitude,"latitude":hospital.latitude} for hospital in hospital_list]
  58. }
  59. ]
  60. # "emergencyExpertCount":emergency_expert_count,
  61. # "emergencyManagementCount":emergency_management_count
  62. }
  63. }
  64. except Exception as e:
  65. db.rollback()
  66. traceback.print_exc()
  67. raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
  68. @router.get('/get_mms_area_info')
  69. async def mine(request: Request,body = Depends(remove_xss_json),db: Session = Depends(get_db)):
  70. try:
  71. with open('/home/python3/xh_twapi01/routers/api/spatialAnalysis/茂名市.json','r', encoding='utf-8') as f:
  72. data = json.load(f)
  73. features = data['features']
  74. result = [{'name': '高','color': '#ff2f3c',"points":[]},
  75. {'name': '较高', 'color': '#ffaf00',"points":[]},
  76. {'name': '较低', 'color': '#ffd800',"points":[]},
  77. {'name': '低', 'color': '#40c75f',"points":[]},]
  78. adcode_dit = {'高':['440902','440983'],"较高":['440904'],"较低":['440981'],"低":['440982']}
  79. # "440902":{'name': '高','color': '#ff2f3c'},
  80. # "440904": {'name': '较高', 'color': '#ffaf00'},
  81. # "440981": {'name': '较低', 'color': '#ffd800'},
  82. # "440982": {'name': '低', 'color': '#40c75f'},
  83. # "440983": {'name': '高', 'color': '#ff2f3c'},
  84. # }
  85. area_data = {}
  86. for feature in features:
  87. properties = feature['properties']
  88. area_code = properties['adcode']
  89. geometry = feature['geometry']
  90. coordinates = geometry['coordinates']
  91. area_data[str(area_code)]=[]
  92. for i in coordinates:
  93. area_data[str(area_code)] += i
  94. # result.append({"name":adcode_dit[str(area_code)]['name'],
  95. # "color":adcode_dit[str(area_code)]['color'],
  96. # "area_name":area_name,
  97. # "area_code":area_code,
  98. # "points":i})
  99. for i in result:
  100. for x in adcode_dit[i['name']]:
  101. i['points']+=area_data[x]
  102. return {
  103. "code": 200,
  104. "msg": "成功",
  105. "data": result}
  106. except Exception as e:
  107. traceback.print_exc()
  108. raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")