Selaa lähdekoodia

Merge branch 'master' of https://gogs.tjp.com.cn/maoming/python-fastapi-mm-zhcs-yj-api

libushang 3 päivää sitten
vanhempi
commit
39a9cd0c91
2 muutettua tiedostoa jossa 33 lisäystä ja 18 poistoa
  1. 30 18
      routers/api/spatialAnalysis/point.py
  2. 3 0
      routers/prod_api/system/dic/__init__.py

+ 30 - 18
routers/api/spatialAnalysis/point.py

@@ -339,31 +339,43 @@ async def get_geojson(
     try:
         # 根据缩放级别动态调整分组粒度
         pac = body['area_code']
-        if pac[-3:]=='000':
-            pac=pac.replace('000','')
         table_name = 'tp_geojson_data_zj'
         option = body['option']
         if 'cj' == option:
-            # print(1111)
             table_name = 'tp_geojson_data_cj_sq'
-        sql = f"""SELECT  name,
-       pac,
+            pac = pac[:9]
+        else:
+            pac = pac[:6]
+        sql = f"""SELECT 
        ST_AsGeoJSON(geometry) AS geometry,
        properties
 FROM {table_name}
-WHERE pac like '{pac}%';"""
-        result = db.execute(sql)
-        features = [
-            {**dict(r), "geometry": json.loads(r.geometry),"properties":json.loads(r.properties)}
-            for r in result.fetchall()
-        ]
-        # features = result.fetchall()
-        # for info in features:
-        #     info['geometry']= {**dict(info), "geometry": json.loads(info.geometry)}
-        #     pass
-        return {"code": 200,
-                "msg": "操作成功","type":"FeatureCollection",
-                "features": features}
+WHERE parent_pac = '{pac}';"""
+
+        def gen():
+            # 1. 写头
+            yield '{"type":"FeatureCollection","features":['
+            first = True
+            # 2. 逐行流式
+            for geom, prop_json in db.execute(sql):  # 迭代器,不 fetchall
+                if not first:
+                    yield ","
+                feature = {
+                    "geometry": json.loads(geom),
+                    "properties": {
+                        "PAC": json.loads(prop_json)['PAC'],
+                        "NAME": json.loads(prop_json)['NAME']
+                    }
+                }
+                yield json.dumps(feature, ensure_ascii=False)
+                first = False
+            # 3. 写尾
+            yield "]}"
+
+
+        return StreamingResponse(gen(), media_type="application/json",
+                             headers={"Content-Disposition": "attachment; filename=data.geojson"})
+
     except Exception as e:
         # 处理异常
         traceback.print_exc()

+ 3 - 0
routers/prod_api/system/dic/__init__.py

@@ -271,6 +271,7 @@ async def delete_dict_type(
 
 @router.get('/data/list')
 async def get_dict_data_by_type(
+dictLabel:str = Query(None),
     dictType: str = Query(None, max_length=100),
     pageNum: int = Query(1, gt=0),
     pageSize: int = Query(10, gt=0),
@@ -287,6 +288,8 @@ async def get_dict_data_by_type(
         # if dictType:
         query = query.filter(SysDictData.dict_type==f'{dictType}')
         query = query.filter(SysDictData.del_flag != '2')
+        if dictLabel:
+            query = query.filter(SysDictData.dict_label.like(f'%{dictLabel}%'))
 
         # 计算总记录数
         total_count = query.count()