__init__.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. point_type_to_list = {
  39. 1: "专家",
  40. 2: "救援物资仓库",
  41. 3: "应急避难场所",
  42. 4: "易涝点",
  43. 5: "学校",
  44. 6: "医院",
  45. 7: "加油站",
  46. 8: "非煤矿山企业",
  47. 9: "危化企业",
  48. 10: "船舶动态",
  49. 11: "危险化学品经营企业",
  50. 12: "危险化学品生产企业",
  51. 13: "危险化学品使用企业",
  52. 14: "化工企业",
  53. 15: "无人机",
  54. 16: "雨窝点",
  55. 17: "地质灾害隐患点",
  56. 18: "矿山施工",
  57. # 19: "工矿商贸",
  58. 20: "气象灾害防御重点单位",
  59. 21: "建筑工程",
  60. 22: "储罐",
  61. 23: "重大危险源",
  62. 24: "客运站",
  63. 25: "堆场",
  64. 26: "旅游景点",
  65. 27: "在建工地",
  66. 28: "运输资源",
  67. 29: "灾害信息员", #govdata_disaster_info_officer
  68. 30: "路网视频",
  69. 31: "江河湖库视频",
  70. 32: "防溺水视频",
  71. 33: "森林火灾视频",
  72. 34: "防灾救援视频",
  73. 35: "救援人员单位",
  74. # 36: "灾害信息员", #govdata_disaster_info_officer
  75. 37: "水利工程",
  76. 38: "人防工程",
  77. 39: "救助站"
  78. }
  79. # 初始化所有列表
  80. lists = {key: [] for key in point_type_to_list}
  81. # 填充列表
  82. for point in get_point_list(body, db):
  83. if point.dataType in lists:
  84. lists[point.dataType].append({
  85. "id": point.id,
  86. "dataType": point.dataType,
  87. "name": point.name,
  88. "longitude": point.longitude,
  89. "latitude": point.latitude,
  90. })
  91. # 构建最终的 list_1,仅包含非空列表
  92. list_1 = [{"name": point_type_to_list[dataType], "num": len(lst), "list": lst}
  93. for dataType, lst in lists.items() if lst]
  94. # list_1 = []
  95. # point_list = get_point_list(body,db)
  96. # emergency_expert_list = [] #专家 1
  97. # rescue_materia_warehouse_list = [] #救援物资-仓库 2
  98. # emergency_shelter_list = [] # 应急避难场所 3
  99. # waterlogged_roads_list = [] # 易涝点 4
  100. # school_list = [] # 学校 5
  101. # hospital_list = [] #医院 6
  102. # gasoline_station_list = [] #加油站 7
  103. # mining_company_list = []# 非煤矿山企业 8
  104. # chemical_company_list = [] #危化企业 9
  105. # ship_realtilme_positioninfo_list = []# 船舶动态 10
  106. # for point in point_list:
  107. # if point.dataType == 1:
  108. # emergency_expert_list.append({"id": point.id, "dataType": point.dataType, "name": point.name, "longitude": point.longitude, "latitude": point.latitude})
  109. # elif point.dataType == 2:
  110. # rescue_materia_warehouse_list.append({"id": point.id, "dataType": point.dataType, "name": point.name, "longitude": point.longitude,
  111. # "latitude": point.latitude})
  112. # elif point.dataType == 3:
  113. # emergency_shelter_list.append(
  114. # {"id": point.id, "dataType": point.dataType, "name": point.name, "longitude": point.longitude,
  115. # "latitude": point.latitude})
  116. # elif point.dataType == 4:
  117. # waterlogged_roads_list.append(
  118. # {"id": point.id, "dataType": point.dataType, "name": point.name, "longitude": point.longitude,
  119. # "latitude": point.latitude})
  120. # elif point.dataType == 5:
  121. # school_list.append(
  122. # {"id": point.id, "dataType": point.dataType, "name": point.name, "longitude": point.longitude,
  123. # "latitude": point.latitude})
  124. # elif point.dataType == 6:
  125. # hospital_list.append(
  126. # {"id": point.id, "dataType": point.dataType, "name": point.name, "longitude": point.longitude,
  127. # "latitude": point.latitude})
  128. # elif point.dataType == 7:
  129. # gasoline_station_list.append(
  130. # {"id": point.id, "dataType": point.dataType, "name": point.name, "longitude": point.longitude,
  131. # "latitude": point.latitude})
  132. # elif point.dataType == 8:
  133. # mining_company_list.append(
  134. # {"id": point.id, "dataType": point.dataType, "name": point.name, "longitude": point.longitude,
  135. # "latitude": point.latitude})
  136. # elif point.dataType == 9:
  137. # chemical_company_list.append(
  138. # {"id": point.id, "dataType": point.dataType, "name": point.name, "longitude": point.longitude,
  139. # "latitude": point.latitude})
  140. # elif point.dataType == 10:
  141. # ship_realtilme_positioninfo_list.append(
  142. # {"id": point.id, "dataType": point.dataType, "name": point.name, "longitude": point.longitude,
  143. # "latitude": point.latitude})
  144. # num = len(emergency_expert_list)
  145. # if num>0:
  146. # list_1.append({"name":"专家","num":num,"list":emergency_expert_list})
  147. # num = len(rescue_materia_warehouse_list)
  148. # if num>0:
  149. # list_1.append({"name":"救援物资仓库","num":num,"list":rescue_materia_warehouse_list})
  150. # num = len(emergency_shelter_list)
  151. # if num>0:
  152. # list_1.append({"name":"应急避难场所","num":num,"list":emergency_shelter_list})
  153. # num = len(waterlogged_roads_list)
  154. # if num>0:
  155. # list_1.append({"name":"易涝点","num":num,"list":waterlogged_roads_list})
  156. # num = len(school_list)
  157. # if num>0:
  158. # list_1.append({"name":"学校","num":num,"list":school_list})
  159. # num = len(hospital_list)
  160. # if num>0:
  161. # list_1.append({"name":"医院","num":num,"list":hospital_list})
  162. # num = len(gasoline_station_list)
  163. # if num>0:
  164. # list_1.append({"name":"加油站","num":num,"list":gasoline_station_list})
  165. # num = len(mining_company_list)
  166. # if num>0:
  167. # list_1.append({"name":"非煤矿山企业","num":num,"list":mining_company_list})
  168. # num = len(chemical_company_list)
  169. # if num>0:
  170. # list_1.append({"name":"危化企业","num":num,"list":chemical_company_list})
  171. # num = len(ship_realtilme_positioninfo_list)
  172. # if num>0:
  173. # list_1.append({"name":"船舶动态","num":num,"list":ship_realtilme_positioninfo_list})
  174. # hospital_list = get_hospital_list(body,db)
  175. # emergency_shelter_list = get_emergency_shelter_list(body,db)
  176. # waterlogged_roads_list = get_waterlogged_roads_list(body,db)
  177. return {
  178. "code": 200,
  179. "msg": "成功",
  180. "data": {
  181. "townData":town_village_data,
  182. "townCount":town_count,
  183. "villageCount":village_count,
  184. # "villageCount":village_count,
  185. "populationSize":0,
  186. "areaSize":0,
  187. "GDP":0,
  188. "list":list_1
  189. # [{
  190. # "name":"应急避难场所","num":len(emergency_shelter_list),"list":[{"name":shelter.name,"longitude":shelter.longitude,"latitude":shelter.latitude} for shelter in emergency_shelter_list]
  191. # },{
  192. # "name":"易涝点","num":len(waterlogged_roads_list),"list":[{"name":waterlogged.name,"longitude":waterlogged.longitude,"latitude":waterlogged.latitude} for waterlogged in waterlogged_roads_list]
  193. # },{
  194. # "name":"医院","num":len(hospital_list),"list":[{"name":hospital.name,"longitude":hospital.longitude,"latitude":hospital.latitude} for hospital in hospital_list]
  195. # }
  196. # ]
  197. # "emergencyExpertCount":emergency_expert_count,
  198. # "emergencyManagementCount":emergency_management_count
  199. }
  200. }
  201. except Exception as e:
  202. db.rollback()
  203. traceback.print_exc()
  204. raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
  205. @router.get('/get_mms_area_info')
  206. async def mine(request: Request,body = Depends(remove_xss_json),db: Session = Depends(get_db)):
  207. try:
  208. with open('/home/python3/xh_twapi01/routers/api/spatialAnalysis/茂名市.json','r', encoding='utf-8') as f:
  209. data = json.load(f)
  210. features = data['features']
  211. result = [{'name': '高','color': '#ff2f3c',"points":[]},
  212. {'name': '较高', 'color': '#ffaf00',"points":[]},
  213. {'name': '较低', 'color': '#ffd800',"points":[]},
  214. {'name': '低', 'color': '#40c75f',"points":[]},]
  215. adcode_dit = {'高':['440902','440983'],"较高":['440904'],"较低":['440981'],"低":['440982']}
  216. # "440902":{'name': '高','color': '#ff2f3c'},
  217. # "440904": {'name': '较高', 'color': '#ffaf00'},
  218. # "440981": {'name': '较低', 'color': '#ffd800'},
  219. # "440982": {'name': '低', 'color': '#40c75f'},
  220. # "440983": {'name': '高', 'color': '#ff2f3c'},
  221. # }
  222. area_data = {}
  223. for feature in features:
  224. properties = feature['properties']
  225. area_code = properties['adcode']
  226. geometry = feature['geometry']
  227. coordinates = geometry['coordinates']
  228. area_data[str(area_code)]=[]
  229. for i in coordinates:
  230. area_data[str(area_code)] += i
  231. # result.append({"name":adcode_dit[str(area_code)]['name'],
  232. # "color":adcode_dit[str(area_code)]['color'],
  233. # "area_name":area_name,
  234. # "area_code":area_code,
  235. # "points":i})
  236. for i in result:
  237. for x in adcode_dit[i['name']]:
  238. i['points']+=area_data[x]
  239. return {
  240. "code": 200,
  241. "msg": "成功",
  242. "data": result}
  243. except Exception as e:
  244. traceback.print_exc()
  245. raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")