Update online_Analysis.py from WMH

This commit is contained in:
DingZQ
2025-04-15 22:07:34 +08:00
parent 2d5b500bf7
commit 0169c962d0
6 changed files with 44 additions and 3 deletions

View File

@@ -34,7 +34,9 @@ def install():
'chardet', 'chardet',
'py-linq', 'py-linq',
'python-multipart', 'python-multipart',
'Cython'] 'Cython',
'geopandas',
'sqlalchemy']
if minor == 4: if minor == 4:
packages.append('script/package/PyMetis-2018.1-cp34-cp34m-win_amd64.whl') packages.append('script/package/PyMetis-2018.1-cp34-cp34m-win_amd64.whl')

View File

@@ -14,6 +14,8 @@ import pandas as pd
import csv import csv
import chardet import chardet
import simulation import simulation
import geopandas as gpd
from sqlalchemy import create_engine
############################################################ ############################################################
@@ -56,6 +58,7 @@ def burst_analysis(name: str, modify_pattern_start_time: str, burst_ID: list | s
['operation', 'current_operation', 'restore_operation', 'batch_operation', 'operation_table']) ['operation', 'current_operation', 'restore_operation', 'batch_operation', 'operation_table'])
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Start Opening Database.") print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Start Opening Database.")
open_project(new_name) open_project(new_name)
simulation.run_simulation(name=new_name, simulation_type='manually_temporary', modify_pattern_start_time=modify_pattern_start_time)
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Database Loading OK.") print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Database Loading OK.")
##step 1 set the emitter coefficient of end node of busrt pipe ##step 1 set the emitter coefficient of end node of busrt pipe
if isinstance(burst_ID, list): if isinstance(burst_ID, list):
@@ -950,6 +953,38 @@ def query_scheme_list(name: str) -> list:
print(f"查询错误:{e}") print(f"查询错误:{e}")
# 2025/03/23
def upload_shp_to_pg(name: str, table_name: str, role: str, shp_file_path: str):
"""
将 Shapefile 文件上传到 PostgreSQL 数据库
:param name: 项目名称(数据库名称)
:param table_name: 创建表的名字
:param role: 数据库角色名位于c盘user中查看
:param shp_file_path: shp文件的路径
:return:
"""
try:
# 动态连接到指定的数据库
conn_string = f"dbname={name} host=127.0.0.1"
with psycopg.connect(conn_string) as conn:
# 读取 Shapefile 文件
gdf = gpd.read_file(shp_file_path)
# 检查投影坐标系CRS并确保是 EPSG:4326
if gdf.crs.to_string() != 'EPSG:4490':
gdf = gdf.to_crs(epsg=4490)
# 使用 GeoDataFrame 的 .to_postgis 方法将数据写入 PostgreSQL
# 需要在数据库中提前安装 PostGIS 扩展
engine = create_engine(f"postgresql+psycopg2://{role}:@127.0.0.1/{name}")
gdf.to_postgis(table_name, engine, if_exists='replace', index=True, index_label='id')
print(f"Shapefile 文件成功上传到 PostgreSQL 数据库 '{name}' 的表 '{table_name}'.")
except Exception as e:
print(f"上传 Shapefile 到 PostgreSQL 时出错:{e}")
if __name__ == '__main__': if __name__ == '__main__':
# contaminant_simulation('bb_model','2024-06-24T00:00:00Z','ZBBDTZDP009034',30,1800) # contaminant_simulation('bb_model','2024-06-24T00:00:00Z','ZBBDTZDP009034',30,1800)
@@ -980,8 +1015,8 @@ if __name__ == '__main__':
# print(f"方案名不存在,可以使用。") # print(f"方案名不存在,可以使用。")
# 示例1burst_analysis # 示例1burst_analysis
# burst_analysis(name='bb', modify_pattern_start_time='2025-03-10T12:00:00+08:00', # burst_analysis(name='bb', modify_pattern_start_time='2025-03-30T12:00:00+08:00',
# burst_ID='ZBBGXSZW000002', burst_size=50, modify_total_duration=1800, scheme_Name='burst_scheme') # burst_ID='ZBBGXSZK001105', burst_size=25, modify_total_duration=1800, scheme_Name='burst0330')
# 示例create_user # 示例create_user
create_user(name='bb', username='admin_test', password='123456') create_user(name='bb', username='admin_test', password='123456')
@@ -995,3 +1030,6 @@ if __name__ == '__main__':
# 示例delete_scheme_info # 示例delete_scheme_info
# delete_scheme_info(name='bb', scheme_name='burst_scheme') # delete_scheme_info(name='bb', scheme_name='burst_scheme')
# 示例upload_shp_to_pg
upload_shp_to_pg(name='bb', table_name='GIS_pipe', role='86158', shp_file_path='市政管线.shp')

BIN
市政管线.dbf Normal file

Binary file not shown.

1
市政管线.prj Normal file
View File

@@ -0,0 +1 @@
GEOGCS["GCS_China_Geodetic_Coordinate_System_2000",DATUM["D_China_2000",SPHEROID["CGCS2000",6378137,298.257222101004]],PRIMEM["Greenwich",0],UNIT["Degree",0.0174532925199433]]

BIN
市政管线.shp Normal file

Binary file not shown.

BIN
市政管线.shx Normal file

Binary file not shown.