Update online_Analysis.py from WMH
This commit is contained in:
@@ -14,6 +14,8 @@ import pandas as pd
|
||||
import csv
|
||||
import chardet
|
||||
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'])
|
||||
print(datetime.now(pytz.timezone('Asia/Shanghai')).strftime("%Y-%m-%d %H:%M:%S") + " -- Start Opening Database.")
|
||||
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.")
|
||||
##step 1 set the emitter coefficient of end node of busrt pipe
|
||||
if isinstance(burst_ID, list):
|
||||
@@ -950,6 +953,38 @@ def query_scheme_list(name: str) -> list:
|
||||
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__':
|
||||
# contaminant_simulation('bb_model','2024-06-24T00:00:00Z','ZBBDTZDP009034',30,1800)
|
||||
@@ -980,8 +1015,8 @@ if __name__ == '__main__':
|
||||
# print(f"方案名不存在,可以使用。")
|
||||
|
||||
# 示例1:burst_analysis
|
||||
# burst_analysis(name='bb', modify_pattern_start_time='2025-03-10T12:00:00+08:00',
|
||||
# burst_ID='ZBBGXSZW000002', burst_size=50, modify_total_duration=1800, scheme_Name='burst_scheme')
|
||||
# burst_analysis(name='bb', modify_pattern_start_time='2025-03-30T12:00:00+08:00',
|
||||
# burst_ID='ZBBGXSZK001105', burst_size=25, modify_total_duration=1800, scheme_Name='burst0330')
|
||||
|
||||
# 示例:create_user
|
||||
create_user(name='bb', username='admin_test', password='123456')
|
||||
@@ -995,3 +1030,6 @@ if __name__ == '__main__':
|
||||
|
||||
# 示例:delete_scheme_info
|
||||
# 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')
|
||||
|
||||
Reference in New Issue
Block a user