【发布时间】:2022-11-01 23:21:40
【问题描述】:
我在 SQLite 数据库中有一个 Pandas 表,其中有 40 个奇数列,所有这些列都可能需要在 QGIS 中进行查询。该表包含 XY 数据和以前的 Python 代码捕获创建点所需的 EPSG 代码。如何将我的 Pandas 或 SQLite 表转换为 SpatiaLite 表?
spatialite_path = 'C:\Program Files (x86)\Spatialite'
os.environ['PATH'] = spatialite_path + ';' + os.environ['PATH']
con.enable_load_extension(True)
con.load_extension("mod_spatialite")
con.execute("SELECT InitSpatialMetaData();")
Tables = table_clean_wGIS,pandas_table_wXY,db_table_wXY
Variables = db_name (string), EPSG_Code (int)
最初,根据the old SpatiaLite Cookbook,我使用AddGeometryColumn 创建了一个带有主键和X 和Y 的表:
con = sqlite3.connect(db_name)
cur = con.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS table_clean_wGIS (ID REAL PRIMARY KEY, UniquePointName TEXT DEFAULT 0, X DOUBLE DEFAULT 0, Y DOUBLE DEFAULT 0, Date TEXT )')
cur.execute('SELECT AddGeometryColumn("table_clean_wGIS", "geometry",(?),"POINT",0)', (EPSG_Code,))
cur.execute('SELECT CreateSpatialIndex("table_clean_wGIS","geometry")')
cur.execute('INSERT INTO table_clean_wGIS(ID, UniquePointName,X,Y, Date, geometry) SELECT ID, UniquePointName, X,Y,Date,MakePoint(X,Y,?) FROM pandas_table_wXY', (EPSG_Code,))
con.commit()
con.close()
这有效,但只有几列。我尝试了FULL OUTER JOIN(SQLite 不支持它),然后是双倍的LEFT JOIN,但我认为它忘记了它是几何图形。然后我用Insert Into 将AddGeometryColumn 设置为最初的40+ 列表,但这创建了双倍的行正确的几何形状但不是与行有关。
con = sqlite3.connect(db_name)
cur = con.cursor()
cur.execute('SELECT AddGeometryColumn ("db_table_wXY", "geometry",(?),"POINT",0)', (EPSG_Code,))
cur.execute('SELECT CreateSpatialIndex("db_table_wXY","geometry")')
cur.execute('INSERT INTO db_table_wXY(geometry) SELECT MakePoint(X,Y,?) FROM db_table_wXY', (EPSG_Code,))
con.commit()
con.close()
我尝试了 Update-Set,似乎这有效,但结果没有显示在 QGIS 中。
cur.execute('SELECT AddGeometryColumn ("db_pss", "geometry",(?),"POINT",0)', (EPSG_Code,))
cur.execute('UPDATE db_pss SET geometry =MakePoint(X,Y,?)', (EPSG_Code,))
cur.execute('SELECT CreateSpatialIndex("db_pss","geometry")')
con.commit()
con.close()
【问题讨论】:
标签: python pandas sqlite qgis spatialite