使用 pyQGIS 在 PostGIS 中创建模式的 shapefile 的空间索引
Create a spatial Index of the shapefiles of a schema in PostGIS using pyQGIS
我正在为我拥有的每个 shapefile 创建一个空间索引,然后将它们导入模式,但空间索引丢失了。如何在模式中再次创建空间索引?
layers_fimport = QgsProject.instance().mapLayers().values()
for a in layers_fimport:
a.setCrs(QgsCoordinateReferenceSystem(2056))
a.dataProvider().createSpatialIndex()
for layer in layers_fimport:
mytable=layer.name()
con_string = "dbname='xxxx' host='xxxxx' port='xxx' user='xxxx' password='xxxxx' key=id type=POLYLINE table='"+project_name+"'." + mytable + " (geom)"
err = QgsVectorLayerExporter.exportLayer(layer, con_string, 'postgres', QgsCoordinateReferenceSystem(2056), False)
Spatial_Index_missing
所以经过一番研究,我找到了一种通过 pyQGIS 使用 SQL 的方法,并创建了空间索引。
import psycopg2
# Create a Spatial Index for the tables in the schema
#-----------------------------------------------------------------------
connection = psycopg2.connect (dbname = "xxxx",
user = "xxxxx",
password = "xxxxxx",
host = "xxxxxxxx",
)
cursor = connection.cursor()
#--No capital letters allowed (schema + tables)
#--------------------------------------------------------------
cursor.execute("CREATE INDEX sidx_l_abluft_geom ON test.l_abluft(geom);")
connection.commit()
print("Query successful")
我正在为我拥有的每个 shapefile 创建一个空间索引,然后将它们导入模式,但空间索引丢失了。如何在模式中再次创建空间索引?
layers_fimport = QgsProject.instance().mapLayers().values()
for a in layers_fimport:
a.setCrs(QgsCoordinateReferenceSystem(2056))
a.dataProvider().createSpatialIndex()
for layer in layers_fimport:
mytable=layer.name()
con_string = "dbname='xxxx' host='xxxxx' port='xxx' user='xxxx' password='xxxxx' key=id type=POLYLINE table='"+project_name+"'." + mytable + " (geom)"
err = QgsVectorLayerExporter.exportLayer(layer, con_string, 'postgres', QgsCoordinateReferenceSystem(2056), False)
Spatial_Index_missing
所以经过一番研究,我找到了一种通过 pyQGIS 使用 SQL 的方法,并创建了空间索引。
import psycopg2
# Create a Spatial Index for the tables in the schema
#-----------------------------------------------------------------------
connection = psycopg2.connect (dbname = "xxxx",
user = "xxxxx",
password = "xxxxxx",
host = "xxxxxxxx",
)
cursor = connection.cursor()
#--No capital letters allowed (schema + tables)
#--------------------------------------------------------------
cursor.execute("CREATE INDEX sidx_l_abluft_geom ON test.l_abluft(geom);")
connection.commit()
print("Query successful")