diff --git a/.gitignore b/.gitignore index 7d6bb3f..f67afeb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,19 @@ # Folders data/ ignored_data/ +drafts/ # Python Modules standalone_charm.py +*_test.py + +# Other Files +setting_up_standalone_pyqgis.docx + + +.idea/.name +__pycache__/ +services_scripts/__init__.py +services_scripts/__pycache__/ + + diff --git a/.idea/hydroquebec_archetype_gispy.iml b/.idea/hydroquebec_archetype_gispy.iml index 0a0e670..597c96a 100644 --- a/.idea/hydroquebec_archetype_gispy.iml +++ b/.idea/hydroquebec_archetype_gispy.iml @@ -2,7 +2,7 @@ - + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 9d2cd65..4156335 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/mtl_buildings_workflow.py b/mtl_buildings_workflow.py index 451a783..6442b8d 100644 --- a/mtl_buildings_workflow.py +++ b/mtl_buildings_workflow.py @@ -1,8 +1,17 @@ -from qgis.core import QgsApplication, QgsVectorLayer, QgsProcessingFeedback +from qgis.core import QgsApplication, QgsVectorLayer, QgsProject, QgsProcessingFeedback from qgis.analysis import QgsNativeAlgorithms -from services_scripts.load_layer import load_layer import processing + +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 + + # Set the path to QGIS installation QgsApplication.setPrefixPath('C:/Program Files/QGIS 3.34.1/apps/qgis', True) @@ -14,32 +23,32 @@ 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') +nrcan_0 = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/nrcan/Autobuilding_QC_VILLE_MONTREAL.shp' +nrcan, nrcan_name = load_layer(nrcan_0, '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' +fixed_nrcan_0 = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/new_tests/python_fixed_05/py_fixes_05.shp' -params_fixing_nrcan = {'INPUT': nrcan, 'METHOD': 0, 'OUTPUT': fixed_nrcan} +params_fixing_nrcan = {'INPUT': nrcan_0, 'METHOD': 0, 'OUTPUT': fixed_nrcan_0} fixed_layer_nrcan = processing.run('native:fixgeometries', params_fixing_nrcan)['OUTPUT'] fixed_nrcan, fixed_nrcan_name = load_layer(fixed_layer_nrcan, 'Fixed NRCan') print(f'{fixed_nrcan_name} data count: {fixed_nrcan.featureCount()}') -montreal_boundary_layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/montreal_boundary|layername=Montreal_boundary' -clipped_nrcan_layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/tests/clipped_nrcan/clipped_nrcan.shp' +clipping_montreal_boundary_layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/montreal_boundary|layername=Montreal_boundary' +clipped_nrcan_layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/new_tests/clipped_nrcan_1/clipped_nrcan_1.shp' -params_clipping_nrcan = {'INPUT': fixed_nrcan, 'OVERLAY': montreal_boundary_layer, 'FILTER_EXPRESSION': '', 'FILTER_EXTENT': None, 'OUTPUT': clipped_layer} -processing.run("native:clip", params_clipping_nrcan) +params_clipping_nrcan = {'INPUT': fixed_nrcan_0, 'OVERLAY': clipping_montreal_boundary_layer, 'FILTER_EXPRESSION': '', 'FILTER_EXTENT': None, 'OUTPUT': clipped_nrcan_layer} +clipped_layer_nrcan = processing.run("native:clip", params_clipping_nrcan)['OUTPUT'] print(f'Clipping of {fixed_nrcan_name} is completed.') -clipped_nrcan, clipped_nrcan_name = load_layer(clipped_nrcan_layer, 'Clipped NRCan') +clipped_nrcan, clipped_nrcan_name = load_layer(clipped_layer_nrcan, 'Clipped NRCan') print(f'{clipped_nrcan_name} data count: {clipped_nrcan.featureCount()}') @@ -48,9 +57,35 @@ indexed_layer = processing.run("native:createspatialindex", params_create_index_ print(f'Creating spatial index for {clipped_nrcan_name} is completed.') -geoindex_unclipped_layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/Geoindex_81670/mamh_usage_predo_2022_s_poly.shp' -geoindex_unclipped, geoindex_unclipped_name = load_layer(geoindex_unclipped_layer, 'GeoIndex Unclipped') +geoindex_layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/Geoindex_81670/mamh_usage_predo_2022_s_poly.shp' +geoindex, geoindex_name = load_layer(geoindex_layer, 'GeoIndex') -print(f'{geoindex_unclipped_name} data count: {geoindex_unclipped.featureCount()}') +print(f'{geoindex_name} data count: {geoindex.featureCount()}') + +print(f'Fixing {geoindex_name} geometries') + +fixed_geoindex_0 = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/new_tests/fixed_geoindex_9/fix_geo_9.shp' + +params_fixing_geoindex = {'INPUT': geoindex, 'METHOD': 0, 'OUTPUT': fixed_geoindex_0} +fixed_layer_geoindex = processing.run("native:fixgeometries", params_fixing_geoindex)['OUTPUT'] + +fixed_geoindex, fixed_geoindex_name = load_layer(fixed_layer_geoindex, 'Fixed NRCan') +print(f'{fixed_geoindex_name} data count: {fixed_geoindex.featureCount()}') + + +clipped_geoindex_layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/new_tests/clipped_geo_3/clipped_geo_3.shp' + +params = {'INPUT': fixed_geoindex, 'OVERLAY': clipping_montreal_boundary_layer, 'FILTER_EXPRESSION': '', 'FILTER_EXTENT': None, 'OUTPUT': clipped_geoindex_layer} +processing.run("native:clip", params) + +print(f'Clipping {fixed_geoindex_name} is completed.') + +clipped_geoindex, clipped_geoindex_name = load_layer(clipped_nrcan_layer, 'Clipped GeoIndex') +print(f'{clipped_geoindex_name} data count: {clipped_geoindex.featureCount()}') + +params_create_index_geoindex = {'INPUT': clipped_geoindex, 'OUTPUT': 'Output'} +processing.run("native:createspatialindex", params_create_index_geoindex) + +print(f'Creating spatial index for {clipped_geoindex_name} is completed.') qgs.exitQgis() \ No newline at end of file diff --git a/services_scripts/clip_layer.py b/services_scripts/clip_layer.py index 83152ac..1a7a44a 100644 --- a/services_scripts/clip_layer.py +++ b/services_scripts/clip_layer.py @@ -12,9 +12,9 @@ qgs.initQgis() # Add native algorithms provider QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms()) -input_layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/tests/python_fixed_04/py_fixes_04.shp' +input_layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/tests/python_fixed_05/py_fixes_05.shp' clipping_layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/montreal_boundary|layername=Montreal_boundary' -clipped_layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/tests/clipped_nrcan/clipped_nrcan.shp' +clipped_layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/tests/clipped_nrcan_10/clipped_nrcan_10.shp' params = {'INPUT': input_layer, 'OVERLAY': clipping_layer, 'FILTER_EXPRESSION': '', 'FILTER_EXTENT': None, 'OUTPUT': clipped_layer} processing.run("native:clip", params) diff --git a/services_scripts/count_records.py b/services_scripts/count_records.py index 74fe7cf..f293992 100644 --- a/services_scripts/count_records.py +++ b/services_scripts/count_records.py @@ -13,8 +13,8 @@ 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') +layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/tests/clipped_nrcan_10/clipped_nrcan_10.shp' +layer, layer_name = load_layer(layer, 'NRCan') print(f'{layer_name} data count: {layer.featureCount()}') diff --git a/services_scripts/create_spatial_index.py b/services_scripts/create_spatial_index.py index 417b230..f12b033 100644 --- a/services_scripts/create_spatial_index.py +++ b/services_scripts/create_spatial_index.py @@ -11,7 +11,7 @@ qgs.initQgis() # Add native algorithms provider QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms()) -layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/tests/fixed_geoindex/fix_geo.shp' +layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/tests/fixed_geoindex_3/fix_geo.shp' params = {'INPUT': layer, 'OUTPUT': 'Output'} processing.run("native:createspatialindex", params) diff --git a/services_scripts/fix_geometries.py b/services_scripts/fix_geometries.py index e3a7c50..3018817 100644 --- a/services_scripts/fix_geometries.py +++ b/services_scripts/fix_geometries.py @@ -11,8 +11,8 @@ 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' +layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/Geoindex_81670/mamh_usage_predo_2022_s_poly.shp' +fixed_layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/tests/fixed_geoindex_7/py_fixes_07.shp' params = {'INPUT': layer, 'METHOD': 0, 'OUTPUT': fixed_layer} fix_layer = processing.run("native:fixgeometries", params)['OUTPUT']