__init__.py 8.8 KB

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