123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- from fastapi import APIRouter, Request, Depends, Query, HTTPException, status
- from common.security import valid_access_token
- from sqlalchemy.orm import Session
- from sqlalchemy.sql import func
- from common.auth_user import *
- from sqlalchemy import text
- from pydantic import BaseModel
- from common.BigDataCenterAPI import *
- from database import get_db
- from typing import List
- from models import *
- from utils import *
- from utils.spatial import *
- import json
- import traceback
- from .point import router as pointrouter
- router = APIRouter()
- router.include_router(pointrouter, prefix="/point")
- class location_c(BaseModel):
- x:float
- y:float
- class mine(BaseModel):
- location : List=[]
- @router.post('/get_info')
- async def mine(request: Request,body = Depends(remove_xss_json),db: Session = Depends(get_db)):
- try:
- # 验证必需的字段
- # required_fields = ['location','location_c']
- # missing_fields = [field for field in required_fields if field not in body]
- # if missing_fields:
- # raise HTTPException(status_code=401, detail=f"Missing required fields: {', '.join(missing_fields)}")
- # 行政镇、行政村数据
- # town_village_data,town_count,village_count = count_town_village(from_data.location,db)
- # town_village_data,town_count = get_town_list(body)
- print(time.time())
- town_village_data,town_count,village_count,populationSize,areaSize,GDP = get_town_village_list(body,db) #[],0,0#
- print(time.time(),town_village_data,town_count,village_count,populationSize,areaSize,GDP)
- # emergency_expert_count = count_emergency_expert(from_data.location,db)
- # emergency_management_count = count_emergency_management(from_data.location,db)
- point_type_to_list = {
- 1: "专家",
- 2: "救援物资仓库",
- 3: "应急避难场所",
- 4: "易涝点",
- 5: "学校",
- 6: "医院",
- 7: "加油站",
- 8: "非煤矿山企业",
- 9: "危化企业",
- 10: "船舶动态",
- 11: "危险化学品经营企业",
- 12: "危险化学品生产企业",
- 13: "危险化学品使用企业",
- 14: "化工企业",
- 15: "无人机",
- 16: "雨窝点",
- 17: "地质灾害隐患点",
- 18: "矿山施工",
- # 19: "工矿商贸",
- 20: "气象灾害防御重点单位",
- 21: "建筑工程",
- 22: "储罐",
- 23: "重大危险源",
- 24: "客运站",
- 25: "堆场",
- 26: "旅游景点",
- 27: "在建工地",
- 28: "运输资源",
- 29: "灾害信息员", #govdata_disaster_info_officer
- 30: "路网视频",
- 31: "江河湖库视频",
- 32: "防溺水视频",
- 33: "森林火灾视频",
- 34: "防灾救援视频",
- 35: "救援人员单位",
- # 36: "灾害信息员", #govdata_disaster_info_officer
- 37: "水利工程",
- 38: "人防工程",
- 39: "救助站",
- 40: "救援物资",
- 41: "救援队伍",
- 43: "重点车辆",
- 44: "儿童福利机构",
- 45: "养老机构",
- 46: "大中型水库",
- 47: "小(1)型水库",
- 48: "小(2)型水库",
- 49: "屋顶山塘",
- 50: "高坝山塘",
- 51: "水库降等山塘",
- 52: "万亩以上江堤",
- 53: "万亩以上海堤",
- 54: "大型水闸",
- 55: "中型水闸",
- 56: "防汛任务电站"
- }
- # 初始化所有列表
- lists = {key: [] for key in point_type_to_list}
- # 填充列表
- for point in get_point_list(body, db):
- if point.dataType in lists:
- lists[point.dataType].append({
- "id": point.id,
- "dataType": point.dataType,
- "name": point.name,
- "longitude": point.longitude,
- "latitude": point.latitude,
- })
- # 构建最终的 list_1,仅包含非空列表
- list_1 = [{"name": point_type_to_list[dataType], "num": len(lst), "list": lst}
- for dataType, lst in lists.items() if lst]
- print(time.time())
- return {
- "code": 200,
- "msg": "成功",
- "data": {
- "townData":town_village_data,
- "townCount":town_count,
- "villageCount":village_count,
- # "villageCount":village_count,,populationSize,areaSize,GDP
- "populationSize":f'{populationSize/10000:.2f}万人',
- "areaSize":f'{areaSize:.2f}平方公里',
- "GDP":f'{GDP:.2f}亿元',
- "list":list_1
- }
- }
- except Exception as e:
- db.rollback()
- traceback.print_exc()
- raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
- @router.post('/get_area_count')
- async def mine(request: Request,body = Depends(remove_xss_json),db: Session = Depends(get_db)):
- try:
- town_count = 0
- village_count = 0
- populationSize = 0
- areaSize = 0
- GDP= 0
- if body:
- query = []
- print(time.time())
- for location in body:
- location = convert_to_polygon(location) # ,geometry
- query.append(f" ST_Intersects(geometry,ST_PolygonFromText( '{location}', 4326 )) ")
- querystr = 'or'.join(query)
- print(time.time())
- townsql = text(
- f"""SELECT DISTINCT pac FROM tp_geojson_data_zj WHERE {querystr} """)
- # print(sql)
- townresutl = db.execute(townsql).all()
- town_count = len(townresutl)
- town_list = [i['pac'] for i in townresutl]
- print(time.time())
- villagesql = text(
- f"""SELECT count(1)as cn FROM tp_geojson_data_cj_sq WHERE {querystr} """)
- print(villagesql)
- villageresutl = db.execute(villagesql).first()
- village_count = villageresutl['cn']
- print(time.time())
- return {
- "code": 200,
- "msg": "成功",
- "data": {
- "townCount": town_count,
- "villageCount": village_count,
- "populationSize": populationSize,
- "areaSize": areaSize,
- "GDP": GDP,
- "town_list":town_list
- }
- }
- except Exception as e:
- traceback.print_exc()
- raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
- @router.get('/get_mms_area_info')
- async def mine(request: Request,body = Depends(remove_xss_json),db: Session = Depends(get_db)):
- try:
- with open('/home/python3/xh_twapi01/routers/api/spatialAnalysis/茂名市.json','r', encoding='utf-8') as f:
- data = json.load(f)
- features = data['features']
- result = [{'name': '高','color': '#ff2f3c',"points":[]},
- {'name': '较高', 'color': '#ffaf00',"points":[]},
- {'name': '较低', 'color': '#ffd800',"points":[]},
- {'name': '低', 'color': '#40c75f',"points":[]},]
- adcode_dit = {'高':['440902','440983'],"较高":['440904'],"较低":['440981'],"低":['440982']}
- # "440902":{'name': '高','color': '#ff2f3c'},
- # "440904": {'name': '较高', 'color': '#ffaf00'},
- # "440981": {'name': '较低', 'color': '#ffd800'},
- # "440982": {'name': '低', 'color': '#40c75f'},
- # "440983": {'name': '高', 'color': '#ff2f3c'},
- # }
- area_data = {}
- for feature in features:
- properties = feature['properties']
- area_code = properties['adcode']
- geometry = feature['geometry']
- coordinates = geometry['coordinates']
- area_data[str(area_code)]=[]
- for i in coordinates:
- area_data[str(area_code)] += i
- # result.append({"name":adcode_dit[str(area_code)]['name'],
- # "color":adcode_dit[str(area_code)]['color'],
- # "area_name":area_name,
- # "area_code":area_code,
- # "points":i})
- for i in result:
- for x in adcode_dit[i['name']]:
- i['points']+=area_data[x]
- return {
- "code": 200,
- "msg": "成功",
- "data": result}
- except Exception as e:
- traceback.print_exc()
- raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
|