Add pipe risk probability
This commit is contained in:
@@ -42,6 +42,7 @@ sql_create = [
|
||||
"script/sql/create/38.scada_info.sql",
|
||||
"script/sql/create/39.users.sql",
|
||||
"script/sql/create/40.scheme_list.sql",
|
||||
"script/sql/create/41.pipe_risk_probability.sql",
|
||||
"script/sql/create/extension_data.sql",
|
||||
"script/sql/create/operation.sql"
|
||||
]
|
||||
@@ -49,6 +50,7 @@ sql_create = [
|
||||
sql_drop = [
|
||||
"script/sql/drop/operation.sql",
|
||||
"script/sql/drop/extension_data.sql",
|
||||
"script/sql/drop/41.pipe_risk_probability.sql",
|
||||
"script/sql/drop/40.scheme_list.sql",
|
||||
"script/sql/drop/39.users.sql",
|
||||
"script/sql/drop/38.scada_info.sql",
|
||||
|
||||
@@ -18,6 +18,7 @@ def install():
|
||||
packages = [
|
||||
'"psycopg[binary]"',
|
||||
'pytest',
|
||||
'ast',
|
||||
'influxdb_client',
|
||||
'numpy',
|
||||
'fastapi',
|
||||
|
||||
@@ -16,6 +16,7 @@ import chardet
|
||||
import simulation
|
||||
import geopandas as gpd
|
||||
from sqlalchemy import create_engine
|
||||
import ast
|
||||
|
||||
|
||||
############################################################
|
||||
@@ -985,6 +986,63 @@ def upload_shp_to_pg(name: str, table_name: str, role: str, shp_file_path: str):
|
||||
print(f"上传 Shapefile 到 PostgreSQL 时出错:{e}")
|
||||
|
||||
|
||||
def submit_risk_probability_result(name: str, result_file_path: str) -> None:
|
||||
"""
|
||||
将管网风险评估结果导入pg数据库
|
||||
:param name: 项目名称(数据库名称)
|
||||
:param result_file_path: 结果文件路径
|
||||
:return:
|
||||
"""
|
||||
# 自动检测文件编码
|
||||
# with open({result_file_path}, 'rb') as file:
|
||||
# raw_data = file.read()
|
||||
# detected = chardet.detect(raw_data)
|
||||
# file_encoding = detected['encoding']
|
||||
# print(f"检测到的文件编码:{file_encoding}")
|
||||
|
||||
try:
|
||||
# 动态替换数据库名称
|
||||
conn_string = f"dbname={name} host=127.0.0.1"
|
||||
|
||||
# 连接到 PostgreSQL 数据库
|
||||
with psycopg.connect(conn_string) as conn:
|
||||
with conn.cursor() as cur:
|
||||
# 检查 scada_info 表是否为空
|
||||
cur.execute("SELECT COUNT(*) FROM pipe_risk_probability;")
|
||||
count = cur.fetchone()[0]
|
||||
|
||||
if count > 0:
|
||||
print("pipe_risk_probability表中已有数据,正在清空记录...")
|
||||
cur.execute("DELETE FROM pipe_risk_probability;")
|
||||
print("表记录已清空。")
|
||||
|
||||
# 读取Excel并转换x/y列为列表
|
||||
df = pd.read_excel(result_file_path, sheet_name='Sheet1')
|
||||
df['x'] = df['x'].apply(ast.literal_eval)
|
||||
df['y'] = df['y'].apply(ast.literal_eval)
|
||||
|
||||
# 批量插入数据
|
||||
for index, row in df.iterrows():
|
||||
insert_query = """
|
||||
INSERT INTO pipe_risk_probability
|
||||
(pipeID, pipeage, risk_probability_now, x, y)
|
||||
VALUES (%s, %s, %s, %s, %s)
|
||||
"""
|
||||
cur.execute(insert_query, (
|
||||
row['pipeID'],
|
||||
row['pipeage'],
|
||||
row['risk_probability_now'],
|
||||
row['x'], # 直接传递列表
|
||||
row['y'] # 同上
|
||||
))
|
||||
|
||||
conn.commit()
|
||||
print("风险评估结果导入成功")
|
||||
|
||||
except Exception as e:
|
||||
print(f"导入时出错:{e}")
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# contaminant_simulation('bb_model','2024-06-24T00:00:00Z','ZBBDTZDP009034',30,1800)
|
||||
@@ -1019,7 +1077,7 @@ if __name__ == '__main__':
|
||||
# burst_ID='ZBBGXSZK001105', burst_size=25, modify_total_duration=1800, scheme_Name='burst0330')
|
||||
|
||||
# 示例:create_user
|
||||
create_user(name='bb', username='admin_test', password='123456')
|
||||
create_user(name='bb', username='admin', password='123456')
|
||||
|
||||
# 示例:delete_user
|
||||
# delete_user(name='bb', username='admin_test')
|
||||
@@ -1033,3 +1091,7 @@ if __name__ == '__main__':
|
||||
|
||||
# 示例:upload_shp_to_pg
|
||||
upload_shp_to_pg(name='bb', table_name='GIS_pipe', role='86158', shp_file_path='市政管线.shp')
|
||||
|
||||
# 示例:submit_risk_probability_result
|
||||
submit_risk_probability_result(name='bb', result_file_path='./北碚市政管线风险评价结果.xlsx')
|
||||
|
||||
|
||||
6
script/sql/create/41.pipe_risk_probability.sql
Normal file
6
script/sql/create/41.pipe_risk_probability.sql
Normal file
@@ -0,0 +1,6 @@
|
||||
-- [PIPE_RISK_PROBABILITY]
|
||||
-- 王名豪
|
||||
-- 2025/04/16
|
||||
-- 删除pipe_risk_probability这张表
|
||||
|
||||
drop table if exists pipe_risk_probability;
|
||||
6
script/sql/drop/41.pipe_risk_probability.sql
Normal file
6
script/sql/drop/41.pipe_risk_probability.sql
Normal file
@@ -0,0 +1,6 @@
|
||||
-- [PIPE_RISK_PROBABILITY]
|
||||
-- 王名豪
|
||||
-- 2025/04/16
|
||||
-- 删除pipe_risk_probability这张表
|
||||
|
||||
drop table if exists pipe_risk_probability;
|
||||
BIN
北碚市政管线风险评价结果.xlsx
Normal file
BIN
北碚市政管线风险评价结果.xlsx
Normal file
Binary file not shown.
Reference in New Issue
Block a user