【问题标题】:How do I turn an SQLite table into a SpatiaLite table?如何将 SQLite 表转换为 SpatiaLite 表?
【发布时间】: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 IntoAddGeometryColumn 设置为最初的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


    【解决方案1】:

    最后我猜想'n'在代码末尾检查了我使用这个花絮的方式:

    cur.execute('SELECT RecoverGeometryColumn("table_with_XY", "geometry", (?), "POINT", "XY")', (EPSG_Code,))
    

    完整代码:

    #This only has to happen once per computer (i think) so feel free to skip these 2 lines
    spatialite_path = 'C:Program Files (x86)Spatialite'
    os.environ['PATH'] = spatialite_path + ';' + os.environ['PATH']
    
    #Variables: db_name, EPSG_Code
    #Connect to the database
    con = sqlite3.connect(db_name)
    cur = con.cursor()  
    #Initialise Spatialite - this has to be done to create geometry binaries
    con.enable_load_extension(True)
    con.load_extension("mod_spatialite")
    con.execute("SELECT InitSpatialMetaData();")      
    
    #Add a blank/empty geometry column (I'm using Point data)
    #the ? is to add a variable into a python based sql statement. EPSG_Code
    cur.execute('SELECT AddGeometryColumn ("table_with_XY", "geometry",(?),"POINT",0)', (EPSG_Code,))
    
    #This is where we make our points from the X and Y column in the table
    cur.execute('UPDATE table_with_XY SET geometry =MakePoint(X,Y,?)', (EPSG_Code,))
    
    #This line I have to use even though I don't think it should be necessary but it makes my code work so...
    cur.execute('SELECT RecoverGeometryColumn("table_with_XY", "geometry", (?), "POINT", "XY")', (EPSG_Code,))
    #Spatial Index to improve performance 
    cur.execute('SELECT CreateSpatialIndex("table_with_XY","geometry")')
    #commit the changes and always close your connection to the database
    con.commit()
    con.close()
    

    这似乎是您可以从同一个表中的 X 和 Y 数据轻松创建几何图形的方式。我不确定它是最佳的。 事后看来,这可能与主键有关。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-15
      • 2011-01-20
      • 1970-01-01
      • 2019-05-28
      • 1970-01-01
      • 1970-01-01
      • 2017-05-13
      • 1970-01-01
      相关资源
      最近更新 更多