Add create spatial index

This commit is contained in:
Alireza Adli 2024-03-15 16:43:27 -04:00
parent 2357703fa9
commit c6b7972de5

View File

@ -1,5 +1,8 @@
from qgis.core import *
from qgis.core import QgsApplication, QgsField, QgsProject, \
QgsProcessingFeedback, QgsVectorLayer, QgsVectorDataProvider, \
QgsExpressionContext, QgsExpressionContextUtils
from qgis.analysis import QgsNativeAlgorithms
import processing
class ScrubLayer:
@ -23,6 +26,14 @@ class ScrubLayer:
QgsProject.instance().addMapLayer(the_layer)
return the_layer
def create_spatial_index(self):
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
create_spatial_index_params = {
'INPUT': self.layer_path,
'OUTPUT': 'Output'
}
processing.run("native:createspatialindex", create_spatial_index_params)
@staticmethod
def cleanup():
QgsApplication.exitQgis()
@ -31,9 +42,10 @@ class ScrubLayer:
if __name__ == '__main__':
app_path = 'C:/Program Files/QGIS 3.34.1/apps/qgis'
new_path = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/' \
'data/test_data/mtl_north/test_north_mtl.shp'
data_layer_name = 'mtl_north'
handle_layer = ScrubLayer(app_path, new_path, 'mtl_north')
print(handle_layer.layer.featureCount())
'data/test_data/test_input/nrcan_north/nrcan_north.shp'
data_layer_name = 'nrcan_north'
handle_layer = ScrubLayer(app_path, new_path, data_layer_name)
# print(handle_layer.layer.featureCount())
handle_layer.create_spatial_index()