__init__.py 12 KB

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