Define self.layer

This commit is contained in:
Alireza Adli 2024-03-15 12:08:52 -04:00
parent 71270c9f6d
commit 2357703fa9

View File

@ -1,30 +1,39 @@
from qgis.core import *
from qgis.analysis import QgsNativeAlgorithms
class ScrubLayer:
def __init__(self, layer_path, layer_name):
def __init__(self, qgis_path, layer_path, layer_name):
# Set the path to QGIS installation
QgsApplication.setPrefixPath(qgis_path, True)
# Initialize QGIS application
qgs = QgsApplication([], False)
qgs.initQgis()
self.layer_path = layer_path
self.layer_name = layer_name
self.layer = self._layer()
self.layer = self.load_layer()
@property
def layer(self):
return self._layer
@layer.setter
def layer(self):
the_layer = QgsVectorLayer(self.layer_path, self.layer_name, "ogr")
def load_layer(self):
the_layer = QgsVectorLayer(self.layer_path, self.layer_name, 'ogr')
if not the_layer.isValid():
print(f'{self.layer_name} failed to load!')
raise ValueError(f'Failed to load layer {self.layer_name} from {self.layer_path}')
else:
self._layer = QgsProject.instance().addMapLayer(the_layer)
QgsProject.instance().addMapLayer(the_layer)
return the_layer
@staticmethod
def cleanup():
QgsApplication.exitQgis()
if __name__ == '__main__':
new_path = \
'C:/Users/a_adli/PycharmProjects/' \
'hydroquebec_archetype_gispy/data/' \
'test_data/mtl_north/test_north_mtl.shp'
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(new_path, 'mtl_north')
handle_layer.layer.
handle_layer = ScrubLayer(app_path, new_path, 'mtl_north')
print(handle_layer.layer.featureCount())