__init__.py 8.7 KB

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