Bladeren bron

250415-1代码。

baoyubo 1 maand geleden
bovenliggende
commit
f74097fca5
2 gewijzigde bestanden met toevoegingen van 29 en 14 verwijderingen
  1. 5 5
      routers/api/spatialAnalysis/__init__.py
  2. 24 9
      utils/spatial/__init__.py

+ 5 - 5
routers/api/spatialAnalysis/__init__.py

@@ -44,7 +44,7 @@ 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)
         print(time.time())
-        town_village_data,town_count,village_count = get_town_village_list(body,db) #[],0,0#
+        town_village_data,town_count,village_count,populationSize,areaSize,GDP = get_town_village_list(body,db) #[],0,0#
         print(time.time())
         # emergency_expert_count = count_emergency_expert(from_data.location,db)
         # emergency_management_count = count_emergency_management(from_data.location,db)
@@ -135,10 +135,10 @@ async def mine(request: Request,body = Depends(remove_xss_json),db: Session = De
                 "townData":town_village_data,
                 "townCount":town_count,
                 "villageCount":village_count,
-                # "villageCount":village_count,
-                "populationSize":0,
-                "areaSize":0,
-                "GDP":0,
+                # "villageCount":village_count,,populationSize,areaSize,GDP
+                "populationSize":f'{populationSize/10000:.2f}万人',
+                "areaSize":f'{areaSize:.2f}平方公里',
+                "GDP":f'{GDP:.2f}亿元',
                 "list":list_1
 
             }

+ 24 - 9
utils/spatial/__init__.py

@@ -22,21 +22,31 @@ def convert_to_polygon(points):
 
 
 def get_town_list2(location_list:list,db):
-    resutl = []
+    # resutl = []
+    if len(location_list) ==0:
+        return []
+    query = []
     for location in location_list:
         location = convert_to_polygon(location) #,geometry
-        sql = text(f"""SELECT DISTINCT `name`,properties,pac FROM tp_geojson_data_zj WHERE ST_Intersects(geometry,ST_PolygonFromText( '{location}', 4326 ))""")
-        # print(sql)
-        resutl+=db.execute(sql).all()
+        query.append(f"ST_Intersects(geometry,ST_PolygonFromText( '{location}', 4326 ))")
+    query=' or '.join(query)
+    sql = text(f"""SELECT DISTINCT `name`,properties,pac FROM tp_geojson_data_zj WHERE {query}""")
+    # print(sql)
+    resutl=db.execute(sql).all()
     return resutl
 
 def get_village_list(location_list:list,db,pac=''):
+    if len(location_list) ==0:
+        return []
     resutl = []
+    query = []
     for location in location_list:
         location = convert_to_polygon(location) #geometry,
-        sql = text(f"""SELECT DISTINCT `name`,properties,pac,populationSize,GDP FROM (select * from tp_geojson_data_cj_sq  {pac})A WHERE ST_Intersects(geometry,ST_PolygonFromText( '{location}', 4326 )) """)
-        # print(sql)
-        resutl+=db.execute(sql).all()
+        query.append(f"ST_Intersects(geometry,ST_PolygonFromText( '{location}', 4326 ))")
+    query=' or '.join(query)
+    sql = text(f"""SELECT DISTINCT `name`,properties,pac,populationSize,GDP FROM (select * from tp_geojson_data_cj_sq  {pac})A WHERE {query} """)
+    # print(sql)
+    resutl=db.execute(sql).all()
     return resutl
 
 def get_town_list(locations,):
@@ -109,6 +119,9 @@ def get_town_village_list(locations,db):
     intersected_names_and_pacs = []
     town_count = len(intersected_towns)
     village_count = 0
+    populationSize = 0
+    areaSize = 0
+    GDP = 0
     for town in intersected_towns:
         # town_count+=1
         town_pac = town.pac
@@ -121,7 +134,9 @@ def get_town_village_list(locations,db):
             "areaSize": f"{quyu_data['areaSize']}{quyu_data['areaSize_unit']}",
             "GDP": f"{quyu_data['GDP']}{quyu_data['GDP_unit']}"  # 假设值,需要从数据中获取
         }
-
+        populationSize += quyu_data['populationSize']
+        areaSize += quyu_data['areaSize']
+        GDP += quyu_data['GDP']
         # intersected_villages = db.query(TpCjSqGeoJSONData).filter(
         #     func.ST_Intersects(TpCjSqGeoJSONData.geometry, multi_polygon) == True
         # ).filter(TpCjSqGeoJSONData.pac.like(f'{town_pac}%')).all()
@@ -147,7 +162,7 @@ def get_town_village_list(locations,db):
             village_count += villageCount
         intersected_names_and_pacs.append(town_data)
 
-    return intersected_names_and_pacs, town_count,village_count
+    return intersected_names_and_pacs, town_count,village_count,populationSize,areaSize,GDP
 # import geopandas as gpd
 # from shapely.geometry import Polygon
 #