|
@@ -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
|
|
|
#
|