QGIS完成初步重构
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
"""
|
||||
一次性脚本:从 PostgreSQL 导出静态底图为 GeoPackage 文件。
|
||||
优先使用 ogr2ogr(QGIS 自带),回退到 geopandas。
|
||||
|
||||
运行一次即可,之后服务直接读本地 GPKG。
|
||||
|
||||
用法: python -m app.script.export_static_layers
|
||||
"""
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
@@ -13,68 +16,159 @@ project_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(_
|
||||
if project_root not in sys.path:
|
||||
sys.path.insert(0, project_root)
|
||||
|
||||
import geopandas as gpd
|
||||
from sqlalchemy import create_engine
|
||||
from config import settings
|
||||
# ============================================================
|
||||
# 配置
|
||||
# ============================================================
|
||||
|
||||
# GPKG 输出目录(相对于项目根目录)
|
||||
GPKG_DIR = os.path.join(project_root, getattr(settings, "QGIS_GPKG_DIR", "app/data/gpkg"))
|
||||
QGIS_ROOT = os.environ.get("QGIS_ROOT", "D:/QGIS")
|
||||
ogr2ogr_path = os.path.join(QGIS_ROOT, "bin", "ogr2ogr.exe")
|
||||
|
||||
# 静态图层定义: {显示名: (schema.table, geom_column, srid)}
|
||||
STATIC_LAYERS = {
|
||||
"水库": ("qgis.rivers", "Geometry", 0),
|
||||
"市州驻地": ("qgis.sx_capital", "geometry", 0),
|
||||
"河流": ("qgis.river", "Geometry", 0),
|
||||
"active_fault": ("qgis.active_fault", "Geometry", 0),
|
||||
"陕西省": ("qgis.sx", "Geometry", 0),
|
||||
"乡镇驻地": ("qgis.sx_street", "dgeom", 0),
|
||||
"区县驻地": ("qgis.sx_xa_county", "Geometry", 0),
|
||||
"县界": ("qgis.sx_xa_county_boundary", "Geometry", 0),
|
||||
"周边区县": ("qgis.sx_zb_county_boundary", "Geometry", 0),
|
||||
"周边市州": ("qgis.sx_zb_city", "Geometry", 4326),
|
||||
"周边县区": ("qgis.sx_zb_county", "Geometry", 0),
|
||||
"traffic_expressway": ("qgis.traffic_expressway", "Geometry", 0),
|
||||
"traffic_provincial": ("qgis.traffic_provincial", "Geometry", 0),
|
||||
"traffic_railway": ("qgis.traffic_railway", "Geometry", 0),
|
||||
"traffic_township": ("qgis.traffic_township", "Geometry", 0),
|
||||
"traffic_trunk_line": ("qgis.traffic_trunk_line", "Geometry", 0),
|
||||
}
|
||||
GPKG_DIR = os.path.join(project_root, "app", "data", "gpkg")
|
||||
|
||||
# 静态图层定义: {显示名: (schema.table, gpkg文件名)}
|
||||
STATIC_LAYERS = [
|
||||
("水库", "qgis.rivers", "rivers.gpkg"),
|
||||
("市州驻地", "qgis.sx_capital", "sx_capital.gpkg"),
|
||||
("河流", "qgis.river", "river.gpkg"),
|
||||
("active_fault", "qgis.active_fault", "active_fault.gpkg"),
|
||||
("陕西省", "qgis.sx", "sx.gpkg"),
|
||||
("乡镇驻地", "qgis.sx_street", "sx_street.gpkg"),
|
||||
("区县驻地", "qgis.sx_xa_county", "sx_xa_county.gpkg"),
|
||||
("县界", "qgis.sx_xa_county_boundary", "sx_xa_county_boundary.gpkg"),
|
||||
("周边区县", "qgis.sx_zb_county_boundary", "sx_zb_county_boundary.gpkg"),
|
||||
("周边市州", "qgis.sx_zb_city", "sx_zb_city.gpkg"),
|
||||
("周边县区", "qgis.sx_zb_county", "sx_zb_county.gpkg"),
|
||||
("traffic_expressway", "qgis.traffic_expressway", "traffic_expressway.gpkg"),
|
||||
("traffic_provincial", "qgis.traffic_provincial", "traffic_provincial.gpkg"),
|
||||
("traffic_railway", "qgis.traffic_railway", "traffic_railway.gpkg"),
|
||||
("traffic_township", "qgis.traffic_township", "traffic_township.gpkg"),
|
||||
("traffic_trunk_line", "qgis.traffic_trunk_line", "traffic_trunk_line.gpkg"),
|
||||
]
|
||||
|
||||
|
||||
def export_layers():
|
||||
os.makedirs(GPKG_DIR, exist_ok=True)
|
||||
# ============================================================
|
||||
# 方法一: ogr2ogr(推荐,QGIS 自带)
|
||||
# ============================================================
|
||||
|
||||
conn_str = (
|
||||
f"postgresql://{settings.DB_USER}:{settings.DB_PASSWORD}"
|
||||
f"@{settings.DB_HOST}:{settings.DB_PORT}/{settings.DB_NAME}"
|
||||
)
|
||||
def _setup_gdal_env():
|
||||
"""设置 GDAL/OGR 运行环境"""
|
||||
gdal_data = os.path.join(QGIS_ROOT, "apps", "gdal", "share", "gdal")
|
||||
gdal_lib = os.path.join(QGIS_ROOT, "apps", "gdal", "lib")
|
||||
gdal_bin = os.path.join(QGIS_ROOT, "apps", "gdal", "bin")
|
||||
|
||||
if os.path.isdir(gdal_data):
|
||||
os.environ["GDAL_DATA"] = gdal_data
|
||||
os.environ["GDAL_FILENAME_IS_UTF8"] = "YES"
|
||||
|
||||
paths_to_add = [p for p in [gdal_bin, gdal_lib] if os.path.isdir(p)]
|
||||
os.environ["PATH"] = ";".join(paths_to_add) + ";" + os.environ.get("PATH", "")
|
||||
|
||||
|
||||
def _export_with_ogr2ogr(host, port, dbname, user, password, schema, table, gpkg_path):
|
||||
"""用 ogr2ogr 导出单个图层"""
|
||||
conn = f"PG:host={host} port={port} dbname={dbname} user={user} password={password}"
|
||||
cmd = [
|
||||
ogr2ogr_path,
|
||||
"-f", "GPKG",
|
||||
gpkg_path,
|
||||
conn,
|
||||
"-sql", f'SELECT * FROM "{schema}"."{table}"',
|
||||
"-nln", table,
|
||||
"-overwrite",
|
||||
"-t_srs", "EPSG:4326",
|
||||
]
|
||||
result = subprocess.run(cmd, capture_output=True, timeout=120)
|
||||
if result.returncode != 0:
|
||||
stderr = result.stderr.decode("utf-8", errors="replace").strip()
|
||||
raise RuntimeError(stderr[:300])
|
||||
return True
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 方法二: geopandas(回退)
|
||||
# ============================================================
|
||||
|
||||
def _export_with_geopandas(host, port, dbname, user, password, schema, table, gpkg_path):
|
||||
"""用 geopandas 导出单个图层"""
|
||||
import geopandas as gpd
|
||||
from sqlalchemy import create_engine
|
||||
|
||||
conn_str = f"postgresql://{user}:{password}@{host}:{port}/{dbname}"
|
||||
engine = create_engine(conn_str)
|
||||
|
||||
for layer_name, (table_ref, geom_col, srid) in STATIC_LAYERS.items():
|
||||
t0 = time.time()
|
||||
gdf = gpd.read_postgis(
|
||||
f'SELECT * FROM "{schema}"."{table}"',
|
||||
engine,
|
||||
geom_col="Geometry",
|
||||
)
|
||||
if gdf.crs is None:
|
||||
gdf = gdf.set_crs(epsg=4326)
|
||||
|
||||
gdf.to_file(gpkg_path, driver="GPKG")
|
||||
engine.dispose()
|
||||
return len(gdf)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 主入口
|
||||
# ============================================================
|
||||
|
||||
def main():
|
||||
# 从 config.settings 读取数据库配置
|
||||
try:
|
||||
from config import settings
|
||||
host = getattr(settings, "QGIS_TEMPLATE_OVERRIDE_ACTUAL_HOST",
|
||||
getattr(settings, "DB_HOST", "47.92.216.173"))
|
||||
port = str(getattr(settings, "QGIS_TEMPLATE_OVERRIDE_ACTUAL_PORT",
|
||||
getattr(settings, "DB_PORT", 7654)))
|
||||
dbname = getattr(settings, "QGIS_TEMPLATE_OVERRIDE_ACTUAL_DB_NAME",
|
||||
getattr(settings, "DB_NAME", "xian_new"))
|
||||
user = getattr(settings, "DB_USER", "postgres")
|
||||
password = getattr(settings, "DB_PASSWORD", "zhangsan")
|
||||
except Exception:
|
||||
host, port, dbname, user, password = "47.92.216.173", "7654", "xian_new", "postgres", "zhangsan"
|
||||
|
||||
os.makedirs(GPKG_DIR, exist_ok=True)
|
||||
|
||||
# 选择导出方法
|
||||
use_ogr2ogr = os.path.isfile(ogr2ogr_path)
|
||||
method = "ogr2ogr" if use_ogr2ogr else "geopandas"
|
||||
|
||||
if use_ogr2ogr:
|
||||
_setup_gdal_env()
|
||||
|
||||
print(f"数据库: {host}:{port}/{dbname}")
|
||||
print(f"输出目录: {GPKG_DIR}")
|
||||
print(f"导出方法: {method}")
|
||||
print(f"共 {len(STATIC_LAYERS)} 个图层\n")
|
||||
|
||||
success = 0
|
||||
failed = 0
|
||||
|
||||
for name, table_ref, gpkg_file in STATIC_LAYERS:
|
||||
schema, table = table_ref.split(".", 1)
|
||||
print(f"导出 {layer_name} ({table_ref}) ...", end=" ", flush=True)
|
||||
gpkg_path = os.path.join(GPKG_DIR, gpkg_file)
|
||||
|
||||
print(f"[{success + failed + 1}/{len(STATIC_LAYERS)}] {name} ({table_ref})", end=" ... ", flush=True)
|
||||
t0 = time.time()
|
||||
|
||||
try:
|
||||
sql = f'SELECT * FROM "{schema}"."{table}"'
|
||||
gdf = gpd.read_postgis(sql, engine, geom_col=geom_col)
|
||||
|
||||
if srid and srid > 0:
|
||||
gdf = gdf.set_crs(epsg=srid, allow_override=True)
|
||||
elif gdf.crs is None:
|
||||
gdf = gdf.set_crs(epsg=4326)
|
||||
|
||||
gpkg_path = os.path.join(GPKG_DIR, f"{table}.gpkg")
|
||||
gdf.to_file(gpkg_path, driver="GPKG")
|
||||
|
||||
print(f"✓ {len(gdf)} 行, {time.time() - t0:.1f}s")
|
||||
|
||||
if use_ogr2ogr:
|
||||
_export_with_ogr2ogr(host, port, dbname, user, password, schema, table, gpkg_path)
|
||||
size_kb = os.path.getsize(gpkg_path) / 1024
|
||||
print(f"✓ {size_kb:.0f} KB, {time.time() - t0:.1f}s")
|
||||
else:
|
||||
count = _export_with_geopandas(host, port, dbname, user, password, schema, table, gpkg_path)
|
||||
print(f"✓ {count} 行, {time.time() - t0:.1f}s")
|
||||
success += 1
|
||||
except Exception as e:
|
||||
print(f"✗ 失败: {e}")
|
||||
failed += 1
|
||||
|
||||
engine.dispose()
|
||||
print(f"\n导出完成!文件目录: {GPKG_DIR}")
|
||||
print(f"\n{'='*50}")
|
||||
print(f"完成: 成功={success}, 失败={failed}, 共={len(STATIC_LAYERS)}")
|
||||
print(f"输出目录: {GPKG_DIR}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
export_layers()
|
||||
main()
|
||||
Reference in New Issue
Block a user