__init__.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from common.BigDataCenterAPI import *
  4. from models import *
  5. from sqlalchemy import text
  6. from sqlalchemy import func
  7. from shapely.geometry import Polygon, MultiPolygon
  8. from shapely.ops import unary_union
  9. import json
  10. def convert_to_polygon(points):
  11. # 将点的列表转换为POLYGON格式的字符串
  12. polygon_str = "POLYGON(("
  13. for point in points:
  14. # 假设点的顺序是经度(x),纬度(y)
  15. polygon_str += f"{point['y']} {point['x']}, "
  16. # 移除最后一个逗号和空格,然后添加闭合点和结束括号
  17. polygon_str = polygon_str.rstrip(", ") + f", {points[0]['y']} {points[0]['x']}))"
  18. return polygon_str
  19. def get_town_list2(location_list:list,db):
  20. # resutl = []
  21. # print(len(location_list))
  22. if len(location_list) ==0:
  23. return []
  24. query = []
  25. for location in location_list:
  26. if isinstance(location,list):
  27. pass
  28. else:
  29. return []
  30. location = convert_to_polygon(location) #,geometry
  31. query.append(f"ST_Intersects(geometry,ST_PolygonFromText( '{location}', 4326 ))")
  32. query=' or '.join(query)
  33. sql = text(f"""SELECT DISTINCT `name`,properties,pac FROM tp_geojson_data_zj WHERE {query}""")
  34. # print(sql.text)
  35. resutl=db.execute(sql).all()
  36. return resutl
  37. def get_village_list(location_list:list,db,pac=''):
  38. if len(location_list) ==0:
  39. return []
  40. resutl = []
  41. query = []
  42. for location in location_list:
  43. if isinstance(location,list):
  44. pass
  45. else:
  46. return []
  47. location = convert_to_polygon(location) #geometry,
  48. query.append(f"ST_Intersects(geometry,ST_PolygonFromText( '{location}', 4326 ))")
  49. query=' or '.join(query)
  50. sql = text(f"""SELECT DISTINCT `name`,properties,pac,populationSize,GDP FROM (select * from tp_geojson_data_cj_sq {pac})A WHERE {query} """)
  51. # print(sql)
  52. resutl=db.execute(sql).all()
  53. return resutl
  54. def get_town_list(locations,):
  55. # 初始化一个空的MultiPolygon来容纳所有多边形
  56. multi_polygon = MultiPolygon()
  57. # 遍历每个位置,创建多边形并添加到multi_polygon中
  58. for location in locations:
  59. # 将边界列表转换为Polygon
  60. polygon = Polygon([(item['x'], item['y']) for item in location])
  61. multi_polygon = multi_polygon.union(polygon)
  62. # 将GeoJSON数据转换为字典
  63. with open('/home/python3/zj_geojson.json', 'r', encoding='utf-8') as file:
  64. geojson = json.load(file)
  65. # 假设GeoJSON数据是一个FeatureCollection
  66. features = geojson.get('features', [])
  67. # 初始化一个空列表来存储结果
  68. intersected_names_and_pacs = []
  69. # 遍历GeoJSON中的每个Feature,计算交集
  70. for feature in features:
  71. geom = feature['geometry']
  72. if 'coordinates' in geom:
  73. # 将GeoJSON Polygon转换为shapely Polygon
  74. if geom['type'] == 'Polygon':
  75. polygon = Polygon(geom['coordinates'][0])
  76. intersection = polygon.intersection(multi_polygon)
  77. elif geom['type'] == 'MultiPolygon':
  78. multi_polygon_feature = MultiPolygon([Polygon(coords[0]) for coords in geom['coordinates']])
  79. intersection = multi_polygon_feature.intersection(multi_polygon)
  80. else:
  81. continue # 跳过非Polygon和非MultiPolygon类型的几何对象
  82. if not intersection.is_empty:
  83. properties = feature['properties']
  84. intersected_names_and_pacs.append({
  85. "townName": properties.get('NAME', ''),
  86. "code": properties.get('PAC', ''),
  87. "populationSize": 0, # 假设值,需要从数据中获取
  88. "areaSize": round(intersection.area, 2), # 交集区域的面积
  89. "GDP": 0 # 假设值,需要从数据中获取
  90. })
  91. return intersected_names_and_pacs, len(intersected_names_and_pacs)
  92. def get_bqw_yj_quyu_data(area_code,db):
  93. sql = text(
  94. """SELECT ifnull(populationSize,0) as populationSize,
  95. ifnull(populationSize_unit,'') as populationSize_unit,
  96. ifnull(areaSize,0) as areaSize,
  97. ifnull(areaSize_unit,'')as areaSize_unit,ifnull(GDP,0) as GDP,ifnull(GDP_unit,'') as GDP_unit FROM sharedb.`bqw_yj_quyu_data` where `area_code`like :code order by area_code,year desc """).bindparams(
  98. code=f'%{area_code}%')
  99. # 执行查询
  100. result = db.execute(sql).fetchone()
  101. # 处理结果
  102. if result:
  103. return dict(result)
  104. else:
  105. return {"populationSize":0,"populationSize_unit":"","areaSize":0,"areaSize_unit":"","GDP":0,"GDP_unit":""}
  106. def get_town_village_list(locations,db):
  107. # 初始化一个空的MultiPolygon来容纳所有多边形
  108. intersected_towns = get_town_list2(locations,db)
  109. print(intersected_towns)
  110. # 初始化一个空列表来存储结果
  111. intersected_names_and_pacs = []
  112. town_count = len(intersected_towns)
  113. village_count = 0
  114. populationSize = 0
  115. areaSize = 0
  116. GDP = 0
  117. for town in intersected_towns:
  118. # town_count+=1
  119. town_pac = town.pac
  120. properties = json.loads(town.properties)
  121. quyu_data = get_bqw_yj_quyu_data(town.pac,db)
  122. town_data = {
  123. "townName": town.name,
  124. "code": town.pac,
  125. "populationSize": f"{quyu_data['populationSize']}{quyu_data['populationSize_unit']}",
  126. "areaSize": f"{quyu_data['areaSize']}{quyu_data['areaSize_unit']}",
  127. "GDP": f"{quyu_data['GDP']}{quyu_data['GDP_unit']}" # 假设值,需要从数据中获取
  128. }
  129. populationSize += quyu_data['populationSize']
  130. areaSize += quyu_data['areaSize']
  131. GDP += quyu_data['GDP']
  132. # intersected_villages = db.query(TpCjSqGeoJSONData).filter(
  133. # func.ST_Intersects(TpCjSqGeoJSONData.geometry, multi_polygon) == True
  134. # ).filter(TpCjSqGeoJSONData.pac.like(f'{town_pac}%')).all()
  135. intersected_villages = get_village_list(locations,db,pac=f""" where pac like '{town_pac}%'""")
  136. intersected_villages_names_and_pacs = []
  137. for village in intersected_villages:
  138. quyu_data = get_bqw_yj_quyu_data(village.pac, db)
  139. # town_data['populationSize']+=village.populationSize
  140. # town_data['GDP']+=village.GDP
  141. # properties = json.loads(village.properties)
  142. village_data = {
  143. "villageName": village.name,
  144. "code": village.pac,
  145. "populationSize": f"{quyu_data['populationSize']}{quyu_data['populationSize_unit']}",#village.populationSize, # 假设值,需要从数据中获取
  146. "areaSize": f"{quyu_data['areaSize']}{quyu_data['areaSize_unit']}",#properties['GEO_AREA'], # 交集区域的面积 0,#
  147. "GDP": f"{quyu_data['GDP']}{quyu_data['GDP_unit']}"#village.GDP # 假设值,需要从数据中获取
  148. }
  149. intersected_villages_names_and_pacs.append(village_data)
  150. villageCount= len(intersected_villages_names_and_pacs)
  151. if villageCount>0:
  152. town_data['children']=intersected_villages_names_and_pacs
  153. town_data['villageCount'] =villageCount
  154. village_count += villageCount
  155. intersected_names_and_pacs.append(town_data)
  156. return intersected_names_and_pacs, town_count,village_count,populationSize,areaSize,GDP
  157. # import geopandas as gpd
  158. # from shapely.geometry import Polygon
  159. #
  160. #
  161. #
  162. # def get_town_list(locations):
  163. # # 读取GeoJSON文件为GeoDataFrame
  164. # gdf = gpd.read_file('zj_geojson.json')
  165. # gdf = gdf.set_crs("EPSG:4326", allow_override=True)
  166. #
  167. # # 初始化一个空的GeoDataFrame来容纳所有多边形
  168. # multi_polygon_gdf = gpd.GeoDataFrame(crs=gdf.crs)
  169. #
  170. # # 遍历每个位置,创建多边形并添加到multi_polygon_gdf中
  171. # for location in locations:
  172. # # 将边界列表转换为Polygon
  173. # polygon = Polygon([(item['x'], item['y']) for item in location])
  174. # # 将多边形添加到multi_polygon_gdf中
  175. # multi_polygon_gdf = multi_polygon_gdf.append(gpd.GeoDataFrame([1], geometry=[polygon], crs=gdf.crs))
  176. #
  177. # # 使用overlay函数来找出相交的区域
  178. # intersected = gpd.overlay(gdf, multi_polygon_gdf, how='intersection')
  179. #
  180. # # 获取相交区域的名称和PAC
  181. # intersected_names_and_pacs = [{"name": row['NAME'], "pac": row['PAC'],"populationSize":0,"areaSize":0,"GDP":0} for index, row in intersected.iterrows() if 'NAME' in row and 'PAC' in row]
  182. #
  183. # return intersected_names_and_pacs,len(intersected_names_and_pacs)
  184. def count_town_village(location_list:list,db):
  185. town_count = 0
  186. town_list = []
  187. village_count = 0
  188. village_list = []
  189. result = []
  190. url = 'https://19.15.75.180:8581/GatewayMsg/http/api/proxy/invoke'
  191. service_code= 'YZT1685418808667'
  192. service_info = db.query(OneShareApiEntity).filter(OneShareApiEntity.servercode == service_code).first()
  193. signTime = str(GetTime() // 1000)
  194. nonce = GetNonce(5)
  195. sign = GetSign(signTime, nonce, service_info.passtoken)
  196. headers = {
  197. # 'Content-Type': 'application/json',
  198. 'x-tif-signature': sign,
  199. 'x-tif-timestamp': signTime,
  200. 'x-tif-nonce': nonce,
  201. 'x-tif-paasid': service_info.passid,
  202. 'x-tif-serviceId': service_code
  203. }
  204. response = requests.post(url=url, headers=headers, json=location_list, verify=False)
  205. if response.status_code==200:
  206. data_list = response.json()['data']
  207. for data in data_list:
  208. township = data['townshipCode']
  209. if township not in town_list:
  210. town_count+=1
  211. town_list.append(township)
  212. # result.append({'township':data['township'],"townshipCode":data['townshipCode'],"villages":[]})
  213. result.append({'township':data['township'],"townshipCode":data['townshipCode'],"village":'-',"villageCode":'-',"populationSize":0,"areaSize":0,"GDP":0})
  214. village = data['villageCode']
  215. if village not in village_list:
  216. village_count+=1
  217. village_list.append(village)
  218. # for town in result:
  219. # if town['townshipCode']==data['townshipCode']:
  220. # town["villages"].append({'village': data['village'], "villageCode": data['villageCode']})
  221. result.append({'township':data['township'],"townshipCode":data['townshipCode'],'village': data['village'], "villageCode": data['villageCode'],"populationSize":0,"areaSize":0,"GDP":0})
  222. return result,town_count,village_count
  223. def count_emergency_expert(location_list:list,db):
  224. if isinstance(location_list, dict):
  225. pass
  226. else:
  227. return []
  228. location = convert_to_polygon(location_list)
  229. sql = text(f"""SELECT * FROM emergency_expert WHERE ST_Contains(ST_PolygonFromText( '{location}', 4326 ),ST_PointFromText(CONCAT('POINT(', latitude, ' ', longitude, ')'), 4326))""")
  230. return len(db.execute(sql).all())
  231. def count_emergency_management(location_list: list, db):
  232. if isinstance(location_list, dict):
  233. pass
  234. else:
  235. return []
  236. location = convert_to_polygon(location_list)
  237. sql = text(f"""SELECT DISTINCT management_unit FROM `rescue_materia` WHERE ST_Contains(ST_PolygonFromText( '{location}', 4326 ),ST_PointFromText(CONCAT('POINT(', latitude, ' ', longitude, ')'), 4326))""")
  238. return len(db.execute(sql).all())
  239. def get_hospital_list(location_list:list,db):
  240. resutl = []
  241. for location in location_list:
  242. if isinstance(location,list):
  243. pass
  244. else:
  245. return []
  246. location = convert_to_polygon(location)
  247. sql = text(f"""SELECT hospital_name as `name`,longitude,latitude,6 AS `dataType` FROM mid_hospital WHERE ST_Contains(ST_PolygonFromText( '{location}', 4326 ),ST_PointFromText(CONCAT('POINT(', latitude, ' ', longitude, ')'), 4326))""")
  248. resutl+=db.execute(sql).all()
  249. return resutl
  250. def get_emergency_shelter_list(location_list:list,db):
  251. resutl = []
  252. for location in location_list:
  253. if isinstance(location,list):
  254. pass
  255. else:
  256. return []
  257. location = convert_to_polygon(location)
  258. sql = text(f"""SELECT shelter_name as `name`,lng as longitude,lat as latitude,3 AS `dataType` FROM mid_emergency_shelter WHERE ST_Contains(ST_PolygonFromText( '{location}', 4326 ),ST_PointFromText(CONCAT('POINT(', lat, ' ', lng, ')'), 4326))""")
  259. resutl+=db.execute(sql).all()
  260. return resutl
  261. def get_waterlogged_roads_list(location_list:list,db):
  262. resutl = []
  263. for location in location_list:
  264. if isinstance(location,list):
  265. pass
  266. else:
  267. return []
  268. location = convert_to_polygon(location)
  269. sql = text(f"""SELECT flood_name as `name`,lng as longitude,lat as latitude,4 AS `dataType` FROM mid_waterlogged_roads WHERE ST_Contains(ST_PolygonFromText( '{location}', 4326 ),ST_PointFromText(CONCAT('POINT(', lat, ' ', lng, ')'), 4326))""")
  270. resutl+=db.execute(sql).all()
  271. return resutl
  272. def get_point_list(location_list:list,db):
  273. if len(location_list) == 0:
  274. return []
  275. query = []
  276. for location in location_list:
  277. if isinstance(location, list):
  278. pass
  279. else:
  280. return []
  281. location = convert_to_polygon(location) # ,geometry
  282. query.append(f"ST_Contains(ST_PolygonFromText( '{location}', 4326 ),ST_PointFromText(CONCAT('POINT(', latitude, ' ', longitude, ')'), 4326))")
  283. query = ' or '.join(query)
  284. sql = text(f"""SELECT `id` ,`name`, longitude, latitude, `dataType` FROM point_data WHERE {query}""")
  285. # print(sql.text)
  286. resutl = db.execute(sql).all()
  287. return resutl
  288. # resutl = []
  289. # for location in location_list:
  290. # if isinstance(location,list):
  291. # pass
  292. # else:
  293. # return []
  294. # location = convert_to_polygon(location)
  295. #
  296. # sql = text(f"""SELECT `id` ,`name`, longitude, latitude, `dataType` FROM point_data WHERE ST_Contains(ST_PolygonFromText( '{location}', 4326 ),ST_PointFromText(CONCAT('POINT(', latitude, ' ', longitude, ')'), 4326))""")
  297. #
  298. # resutl+=db.execute(sql).all()
  299. # return resutl