diff --git a/install.py b/install.py index 28d1dcf..551af2e 100644 --- a/install.py +++ b/install.py @@ -34,7 +34,9 @@ def install(): 'chardet', 'py-linq', 'python-multipart', - 'Cython'] + 'Cython', + 'geopandas', + 'sqlalchemy'] if minor == 4: packages.append('script/package/PyMetis-2018.1-cp34-cp34m-win_amd64.whl') diff --git a/online_Analysis.py b/online_Analysis.py index a129dcb..32e40cb 100644 --- a/online_Analysis.py +++ b/online_Analysis.py @@ -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') diff --git a/市政管线.dbf b/市政管线.dbf new file mode 100644 index 0000000..69a3e6d Binary files /dev/null and b/市政管线.dbf differ diff --git a/市政管线.prj b/市政管线.prj new file mode 100644 index 0000000..1711ae4 --- /dev/null +++ b/市政管线.prj @@ -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]] diff --git a/市政管线.shp b/市政管线.shp new file mode 100644 index 0000000..390e58a Binary files /dev/null and b/市政管线.shp differ diff --git a/市政管线.shx b/市政管线.shx new file mode 100644 index 0000000..8a0afdc Binary files /dev/null and b/市政管线.shx differ