diff --git a/mtl_buildings_workflow.py b/mtl_buildings_workflow.py new file mode 100644 index 0000000..ae58e38 --- /dev/null +++ b/mtl_buildings_workflow.py @@ -0,0 +1,39 @@ +from qgis.core import QgsApplication, QgsVectorLayer, QgsProcessingFeedback +from qgis.analysis import QgsNativeAlgorithms +from services_scripts.load_layer import load_layer +import processing + +# Set the path to QGIS installation +QgsApplication.setPrefixPath("C:/Program Files/QGIS 3.34.1/apps/qgis", True) + +# Initialize QGIS application +qgs = QgsApplication([], False) +qgs.initQgis() + + +# Add native algorithms provider +QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms()) + +nrcan = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/nrcan/Autobuilding_QC_VILLE_MONTREAL.shp' +nrcan, nrcan_name = load_layer(nrcan, 'NRCan') + +print(f'Loading {nrcan_name}') +print(f'{nrcan_name} data count: {nrcan.featureCount()}') + +print(f'Fixing {nrcan_name} geometries') + +fixed_nrcan = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/tests/python_fixed_04/py_fixes_04.shp' + +params_fixing_nrcan = {'INPUT': nrcan, 'METHOD': 0, 'OUTPUT': fixed_nrcan} +fixed_layer = processing.run("native:fixgeometries", params_fixing_nrcan)['OUTPUT'] + +fixed_nrcan, fixed_nrcan_name = load_layer(fixed_layer, 'Fixed NRCan') +print(f'{fixed_nrcan_name} data count: {fixed_nrcan.featureCount()}') + +params_create_index_nrcan = {'INPUT': fixed_layer, 'OUTPUT': 'Output'} +indexed_layer = processing.run("native:createspatialindex", params_create_index_nrcan) + + +print("Creating spatial index is completed.") + +qgs.exitQgis() \ No newline at end of file diff --git a/services_scripts/count_records.py b/services_scripts/count_records.py new file mode 100644 index 0000000..74fe7cf --- /dev/null +++ b/services_scripts/count_records.py @@ -0,0 +1,21 @@ +from qgis.core import QgsApplication, QgsVectorLayer, QgsProcessingFeedback +from qgis.analysis import QgsNativeAlgorithms +from load_layer import load_layer + + +# Set the path to QGIS installation +QgsApplication.setPrefixPath("C:/Program Files/QGIS 3.34.1/apps/qgis", True) + +# Initialize QGIS application +qgs = QgsApplication([], False) +qgs.initQgis() + +# Add native algorithms provider +QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms()) + +layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/tests/python_fixed_02/py_fixes_02.shp' +layer, layer_name = load_layer(layer, 'NRCan Fixed') + +print(f'{layer_name} data count: {layer.featureCount()}') + +qgs.exitQgis() \ No newline at end of file diff --git a/services_scripts/create_spatial_index.py b/services_scripts/create_spatial_index.py new file mode 100644 index 0000000..91c8c7b --- /dev/null +++ b/services_scripts/create_spatial_index.py @@ -0,0 +1,24 @@ +from qgis.core import QgsApplication, QgsVectorLayer, QgsProcessingFeedback +from qgis.analysis import QgsNativeAlgorithms +import processing + +# Set the path to QGIS installation +QgsApplication.setPrefixPath("C:/Program Files/QGIS 3.34.1/apps/qgis", True) + +# Initialize QGIS application +qgs = QgsApplication([], False) +qgs.initQgis() + +# Add native algorithms provider +QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms()) +layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/tests/python_fixed_04/py_fixes_04.shp' + +indexed_layer_path = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/tests/python_fixed_04/py_fixes_04.qix' + +params = {'INPUT': layer, 'OUTPUT': 'Output'} +indexed_layer = processing.run("native:createspatialindex", params) + + +print("Creating spatial index is completed.") + +qgs.exitQgis() \ No newline at end of file diff --git a/services_scripts/fix_geometries.py b/services_scripts/fix_geometries.py new file mode 100644 index 0000000..e3a7c50 --- /dev/null +++ b/services_scripts/fix_geometries.py @@ -0,0 +1,23 @@ +from qgis.core import QgsApplication, QgsVectorLayer, QgsProcessingFeedback +from qgis.analysis import QgsNativeAlgorithms +import processing + +# Set the path to QGIS installation +QgsApplication.setPrefixPath("C:/Program Files/QGIS 3.34.1/apps/qgis", True) + +# Initialize QGIS application +qgs = QgsApplication([], False) +qgs.initQgis() + +# Add native algorithms provider +QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms()) +layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/nrcan/Autobuilding_QC_VILLE_MONTREAL.shp' +fixed_layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/tests/python_fixed_03/py_fixes_03.shp' + +params = {'INPUT': layer, 'METHOD': 0, 'OUTPUT': fixed_layer} +fix_layer = processing.run("native:fixgeometries", params)['OUTPUT'] + +print("Fixed Geometries is completed.") + +# Exit QGIS application +qgs.exitQgis() \ No newline at end of file diff --git a/services_scripts/load_layer.py b/services_scripts/load_layer.py new file mode 100644 index 0000000..4d5e6b7 --- /dev/null +++ b/services_scripts/load_layer.py @@ -0,0 +1,18 @@ +from qgis.core import * + +# Supply the path to the qgis install location +QgsApplication.setPrefixPath(prefixPath='C:/Program Files/QGIS 3.34.1/apps/qgis', useDefaultPaths=True) + +qgs = QgsApplication([], False) + +# load providers +qgs.initQgis() + + +def load_layer(path, layer_name): + the_layer = QgsVectorLayer(path, layer_name, "ogr") + if not the_layer.isValid(): + print(f'{layer_name} failed to load!') + else: + QgsProject.instance().addMapLayer(the_layer) + return the_layer, layer_name \ No newline at end of file