Browse Source

优化空间分析

baoyubo 10 months ago
parent
commit
0123e0d421

+ 2 - 1
routers/api/spatialAnalysis/__init__.py

@@ -40,7 +40,8 @@ async def mine(request: Request,body = Depends(remove_xss_json),db: Session = De
 
         # 行政镇、行政村数据
         # town_village_data,town_count,village_count = count_town_village(from_data.location,db)
-        town_village_data,town_count = get_town_list(body)
+        # town_village_data,town_count = get_town_list(body)
+        town_village_data,town_count = get_town_village_list(body,db)
         # emergency_expert_count = count_emergency_expert(from_data.location,db)
         # emergency_management_count = count_emergency_management(from_data.location,db)
         hospital_list = get_hospital_list(body,db)

+ 49 - 1
utils/spatial/__init__.py

@@ -21,7 +21,7 @@ def get_town_list(locations,):
         multi_polygon = multi_polygon.union(polygon)
 
     # 将GeoJSON数据转换为字典
-    with open('/home/python3/xh_twapi01/utils/spatial/zj_geojson.json', 'r', encoding='utf-8') as file:
+    with open('/home/python3/zj_geojson.json', 'r', encoding='utf-8') as file:
         geojson = json.load(file)
 
     # 假设GeoJSON数据是一个FeatureCollection
@@ -55,6 +55,54 @@ def get_town_list(locations,):
                 })
 
     return intersected_names_and_pacs, len(intersected_names_and_pacs)
+
+def get_town_village_list(locations,db):
+    # 初始化一个空的MultiPolygon来容纳所有多边形
+
+    multi_polygon = MultiPolygon()
+
+    # 遍历每个位置,创建多边形并添加到multi_polygon中
+    for location in locations:
+        # 将边界列表转换为Polygon
+        polygon = Polygon([(item['x'], item['y']) for item in location])
+        multi_polygon = multi_polygon.union(polygon)
+
+    intersected_towns = TpZjGeoJSONData.query.filter(
+        db.func.ST_Intersects(TpZjGeoJSONData.geometry, multi_polygon) == True
+    ).all()
+
+
+    # 初始化一个空列表来存储结果
+    intersected_names_and_pacs = []
+    for town in intersected_towns:
+        town_pac = town.pac[:-3]
+        town_data = {
+            "townName": town.name,
+            "code": town.pac,
+            "populationSize": 0,  # 假设值,需要从数据中获取
+            "areaSize": town.geometry.area,  # 交集区域的面积
+            "GDP": 0  # 假设值,需要从数据中获取
+        }
+
+        intersected_villages = TpCjSqGeoJSONData.query.filter(
+            db.func.ST_Intersects(TpCjSqGeoJSONData.geometry, multi_polygon) == True
+        ).filter(TpCjSqGeoJSONData.pac.like(f'{town_pac}%')).all()
+        intersected_villages_names_and_pacs = []
+        for village in intersected_villages:
+            village_data = {
+                "villageName": village.name,
+                "code": village.pac,
+                "populationSize": 0,  # 假设值,需要从数据中获取
+                "areaSize": village.geometry.area,  # 交集区域的面积
+                "GDP": 0  # 假设值,需要从数据中获取
+            }
+            intersected_villages_names_and_pacs.append(village_data)
+        if len(intersected_villages_names_and_pacs)>0:
+            town_data['children']=intersected_villages_names_and_pacs
+            town_data['villageCount'] = len(intersected_villages_names_and_pacs)
+        intersected_names_and_pacs.append(town_data)
+
+    return intersected_names_and_pacs, len(intersected_names_and_pacs)
 # import geopandas as gpd
 # from shapely.geometry import Polygon
 #

File diff suppressed because it is too large
+ 0 - 0
utils/spatial/zj_geojson.json


Some files were not shown because too many files changed in this diff