baoyubo 5 dagen geleden
bovenliggende
commit
52f83b276b
1 gewijzigde bestanden met toevoegingen van 41 en 14 verwijderingen
  1. 41 14
      routers/api/spatialAnalysis/point.py

+ 41 - 14
routers/api/spatialAnalysis/point.py

@@ -29,10 +29,24 @@ from multiprocessing import Pool, cpu_count
 import json
 import time
 import math
+from shapely import wkb
+from shapely.geometry import Point
 
 
 router = APIRouter()
 
+def get_geom(db,iszjcj,pac):
+    if iszjcj=='zj':
+        pac = pac[:6]
+        zjcjtable = 'tp_geojson_data_qx'
+    elif iszjcj=='cj':
+        pac = pac[:9]
+        zjcjtable = 'tp_geojson_data_zj'
+    sql = f"""select name, ST_AsBinary(geometry)as geometry from {zjcjtable} where pac like '{pac}%'"""
+    result = db.execute(sql)
+    infos = result.fetchall()
+    return  infos
+
 @router.post("/get_info")
 @router.get("/get_info")
 async def get_infos(
@@ -84,7 +98,12 @@ async def get_infos(
         videos = get_videos(db,dict_value,latitude_min,latitude_max,longitude_min,longitude_max,iszjcj)
         infos = get_points(db,option,latitude_min,latitude_max,longitude_min,longitude_max,iszjcj,pac)
         # 动态分组逻辑
-        groups = group_points(videos+infos, distance_threshold)
+        if 'iszjcj' in body:
+            iszjcj = body['iszjcj']
+            pac = body['pac']
+            groups = group_points(videos + infos, distance_threshold,db,iszjcj,pac)
+        else:
+            groups = group_points(videos+infos, distance_threshold)
 
         print("4",time.time())
 
@@ -175,7 +194,7 @@ def calculate_distance(point1, point2):
     c = 2 * atan2(sqrt(a), sqrt(1 - a))
     return R * c
 
-def group_points(points, distance_threshold):
+def group_points(points, distance_threshold,db=get_db(),iszjcj='',pac=''):
     grid_size = calculate_grid_size(distance_threshold)
     grid = defaultdict(lambda:{"count":0}) #,"list":[]
     groups = []
@@ -210,7 +229,15 @@ def group_points(points, distance_threshold):
             grid['%s-%s'%grid_key]['longitude'] = float(point.longitude)
 
     groups = list(grid.values())
-
+    if iszjcj!='':
+        geom = get_geom(db, iszjcj, pac)
+        for group in groups:
+            for name, wkb_bytes in geom:
+                lon, lat = group['longitude'],group['latitude']
+                if wkb.loads(wkb_bytes).contains(Point(lon, lat)):
+                    break
+            else:
+                del group
     return groups
 
 def get_videos(db:Session,dict_value,latitude_min,latitude_max,longitude_min,longitude_max,iszjcj=''):
@@ -262,17 +289,17 @@ def get_points(db:Session,option,latitude_min,latitude_max,longitude_min,longitu
     # 使用参数化查询避免 SQL 注入
     if isinstance(option, list):
         option = tuple(option)
-    if iszjcj=='zj':
-        zd = ',T2.name as pacname,T2.pac,T2.parent_pac'
-        pac = pac[:6]
-        zjcjtable = f"""select * from tp_geojson_data_qx where pac like '{pac}%'"""
-        que = f' JOIN ({zjcjtable}) T2 on ST_Intersects(T2.geometry, ST_SRID(POINT(A.longitude, A.latitude), 4326))'
-    elif iszjcj=='cj':
-        pac = pac[:9]
-        zjcjtable = f"""select * from tp_geojson_data_zj where pac like '{pac}%'"""
-        que = f'LEFT JOIN ({zjcjtable}) T2 on ST_Intersects(T2.geometry, ST_SRID(POINT(A.longitude, A.latitude), 4326))'
-    else:
-        que=''
+    # if iszjcj=='zj':
+    #     zd = ',T2.name as pacname,T2.pac,T2.parent_pac'
+    #     pac = pac[:6]
+    #     zjcjtable = f"""select * from tp_geojson_data_qx where pac like '{pac}%'"""
+    #     que = f' JOIN ({zjcjtable}) T2 on ST_Intersects(T2.geometry, ST_SRID(POINT(A.longitude, A.latitude), 4326))'
+    # elif iszjcj=='cj':
+    #     pac = pac[:9]
+    #     zjcjtable = f"""select * from tp_geojson_data_zj where pac like '{pac}%'"""
+    #     que = f'LEFT JOIN ({zjcjtable}) T2 on ST_Intersects(T2.geometry, ST_SRID(POINT(A.longitude, A.latitude), 4326))'
+    # else:
+    que=''
     query = text(f"""
         SELECT 
             A.`name`,A.`id`,A.dataType,A.longitude,A.latitude,A.infoType