|
@@ -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()
|