Clean indentation of services

This commit is contained in:
Alireza Adli 2024-02-28 15:04:14 -05:00
parent ca9b3c3910
commit ec647b88f4
15 changed files with 217 additions and 67 deletions

View File

@ -0,0 +1,26 @@
from qgis.core import QgsProject, QgsExpression, QgsExpressionContext, edit, QgsField, QgsExpressionContextUtils
# Import necessary modules
from qgis.core import QgsProject, QgsVectorLayer, QgsField
# Define the layer name
layer_name = "your_layer_name"
# Get the layer by name
layer = QgsProject.instance().mapLayersByName(layer_name)[0]
# Start editing
layer.startEditing()
# Add a new field to store the area
field_name = 'Area'
field_index = layer.fields().indexFromName(field_name)
if field_index == -1:
layer.dataProvider().addAttributes([QgsField(field_name, QgsField.QVariant.Double)])
layer.updateFields()
# Commit changes
layer.commitChanges()

View File

@ -1,4 +1,6 @@
import os, glob
import os
import glob
import time
def create_folders(directory, num_folders):
@ -25,8 +27,26 @@ def create_folders(directory, num_folders):
# .shp files from each subfolder
def find_shp_files(root_folder):
shp_files = []
for foldername, _, _ in sorted(os.walk(root_folder)): # Sort folders alphabetically
# Sort folders alphabetically
for foldername, _, _ in sorted(os.walk(root_folder)):
for filename in sorted(glob.glob(os.path.join(foldername, '*.shp'))):
new_file_name = filename.replace('\\', r'/')
shp_files.append(new_file_name)
return shp_files
# Creates a progress bar for a for loop
def progress_bar(iterable, length=20):
total = len(iterable)
progress = 0
start_time = time.time()
for item in iterable:
yield item
progress += 1
percent = progress / total
filled_length = int(length * percent)
bar = '=' * filled_length + '-' * (length - filled_length)
elapsed_time = time.time() - start_time
print(f'\r[{bar}] {progress}/{total} ({percent:.0%}) - '
f'Elapsed time: {elapsed_time:.2f}s', end='', flush=True)
print()

View File

@ -1,4 +1,4 @@
from qgis.core import QgsApplication, QgsVectorLayer, QgsProcessingFeedback
from qgis.core import QgsApplication
from qgis.analysis import QgsNativeAlgorithms
import processing
@ -12,13 +12,26 @@ qgs.initQgis()
# Add native algorithms provider
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
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_10/clipped_nrcan_10.shp'
input_layer = \
'C:/Users/a_adli/PycharmProjects/' \
'hydroquebec_archetype_gispy/data/' \
'output_data/python_fixed_05/py_fixes_05.shp'
clipping_layer = \
'C:/Users/a_adli/PycharmProjects/' \
'hydroquebec_archetype_gispy/data/' \
'input_data/montreal_boundary|layername=Montreal_boundary'
clipped_layer = \
'C:/Users/a_adli/PycharmProjects/' \
'hydroquebec_archetype_gispy/data/' \
'output_data/clipped_nrcan_10/clipped_nrcan_10.shp'
params = {'INPUT': input_layer, 'OVERLAY': clipping_layer, 'FILTER_EXPRESSION': '', 'FILTER_EXTENT': None, 'OUTPUT': clipped_layer}
params = {'INPUT': input_layer,
'OVERLAY': clipping_layer,
'FILTER_EXPRESSION': '',
'FILTER_EXTENT': None,
'OUTPUT': clipped_layer}
processing.run("native:clip", params)
print("Clipping is completed.")
qgs.exitQgis()
qgs.exitQgis()

View File

@ -1,9 +1,12 @@
from qgis.core import QgsApplication, QgsVectorLayer, QgsProcessingFeedback
from qgis.core import QgsApplication
from qgis.analysis import QgsNativeAlgorithms
import processing
from services_scripts.basic_functions import create_folders
clipped_layers_folder = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/aEndeavor/clip_all'
clipped_layers_folder = \
'C:/Users/a_adli/PycharmProjects/' \
'hydroquebec_archetype_gispy/' \
'data/aEndeavor/clip_all'
num_clipped_layers = 21
create_folders(clipped_layers_folder, num_clipped_layers)
@ -17,13 +20,26 @@ qgs.initQgis()
# Add native algorithms provider
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
input_layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/uniteevaluationfonciere/uniteevaluationfonciere.shp'
input_layer = \
'C:/Users/a_adli/PycharmProjects/' \
'hydroquebec_archetype_gispy/data/' \
'input_data/uniteevaluationfonciere/uniteevaluationfonciere.shp'
for each in range(num_clipped_layers):
clipping_layer = f'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/aEndeavor/divided_all/layer_{each}/layer_{each}.shp'
clipped_layer = f'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/aEndeavor/clip_all/layer_{each}/layer_{each}.shp'
clipping_layer = \
f'C:/Users/a_adli/PycharmProjects/' \
f'hydroquebec_archetype_gispy/data/' \
f'output_data/divided_all/layer_{each}/layer_{each}.shp'
clipped_layer = \
f'C:/Users/a_adli/PycharmProjects/' \
f'hydroquebec_archetype_gispy/data/' \
f'output_data/clip_all/layer_{each}/layer_{each}.shp'
params = {'INPUT': input_layer, 'OVERLAY': clipping_layer, 'FILTER_EXPRESSION': '', 'FILTER_EXTENT': None, 'OUTPUT': clipped_layer}
params = {'INPUT': input_layer,
'OVERLAY': clipping_layer,
'FILTER_EXPRESSION': '',
'FILTER_EXTENT': None,
'OUTPUT': clipped_layer}
processing.run("native:clip", params)
# Creating spatian index for each new layer
@ -32,4 +48,4 @@ for each in range(num_clipped_layers):
print("Clipping is completed.")
qgs.exitQgis()
qgs.exitQgis()

View File

@ -1,4 +1,4 @@
from qgis.core import QgsApplication, QgsVectorLayer, QgsProcessingFeedback
from qgis.core import QgsApplication
from qgis.analysis import QgsNativeAlgorithms
from load_layer import load_layer
@ -13,9 +13,12 @@ qgs.initQgis()
# Add native algorithms provider
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/tests/clipped_nrcan_10/clipped_nrcan_10.shp'
layer = \
'C:/Users/a_adli/PycharmProjects/' \
'hydroquebec_archetype_gispy/data/' \
'output_data/clipped_nrcan_10/clipped_nrcan_10.shp'
layer, layer_name = load_layer(layer, 'NRCan')
print(f'{layer_name} data count: {layer.featureCount()}')
qgs.exitQgis()
qgs.exitQgis()

View File

@ -1,4 +1,4 @@
from qgis.core import QgsApplication, QgsVectorLayer, QgsProcessingFeedback
from qgis.core import QgsApplication
from qgis.analysis import QgsNativeAlgorithms
import processing
@ -11,12 +11,16 @@ qgs.initQgis()
# Add native algorithms provider
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/tests/fixed_geoindex_3/fix_geo.shp'
layer = \
'C:/Users/a_adli/PycharmProjects/' \
'hydroquebec_archetype_gispy/data/' \
'output_data/merged_all_delete_columns/merge_divisions.shp'
params = {'INPUT': layer, 'OUTPUT': 'Output'}
params = {'INPUT': layer,
'OUTPUT': 'Output'}
processing.run("native:createspatialindex", params)
print("Creating spatial index is completed.")
qgs.exitQgis()
qgs.exitQgis()

View File

@ -1,4 +1,4 @@
from qgis.core import QgsApplication, QgsVectorLayer, QgsProcessingFeedback
from qgis.core import QgsApplication
from qgis.analysis import QgsNativeAlgorithms
import processing
@ -12,10 +12,17 @@ qgs.initQgis()
# Add native algorithms provider
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/aEndeavor/merged_all_delete_columns/merge_divisions.shp'
input_layer = \
'C:/Users/a_adli/PycharmProjects/' \
'hydroquebec_archetype_gispy/data/' \
'output_data/qgis_spatial_pariwiseunitNrcan_geo|layername=spatial_with_geo'
output_layer = \
'C:/Users/a_adli/PycharmProjects/' \
'hydroquebec_archetype_gispy/data/' \
'output_data/empty/delete_duplicates_02.shp'
params = {'INPUT':'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/history/aEndeavor/qgis_spatial_pariwiseunitNrcan_geo|layername=spatial_with_geo',
'OUTPUT':'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/history/aEndeavor/empty/delete_duplicates_02.shp'}
params = {'INPUT': input_layer,
'OUTPUT': output_layer}
processing.run("native:deleteduplicategeometries", params)
qgs.exitQgis()
qgs.exitQgis()

View File

@ -21,21 +21,24 @@ qgs.initQgis()
# Add native algorithms provider
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
layer_path = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/aEndeavor/merged_all_delete_columns/merge_divisions.shp'
the_layer = load_layer(layer_path, 'Merged Pairwise Clipped')[0]
layer_path = \
'C:/Users/a_adli/PycharmProjects/' \
'hydroquebec_archetype_gispy/data/' \
'output_data/merged_all_delete_columns/merge_divisions.shp'
output_layer = load_layer(layer_path, 'Merged Pairwise Clipped')[0]
column_name = "layer" # and 'path'
column_name = 'path' # and 'layer'
# Start editing
with edit(the_layer):
with edit(output_layer):
# Get the index of the column to delete
idx = the_layer.fields().indexFromName(column_name)
idx = output_layer.fields().indexFromName(column_name)
# Delete the field
the_layer.deleteAttribute(idx)
output_layer.deleteAttribute(idx)
# Update layer fields
the_layer.updateFields()
output_layer.updateFields()
qgs.exitQgis()
qgs.exitQgis()

View File

@ -1,4 +1,4 @@
from qgis.core import QgsApplication, QgsVectorLayer, QgsProcessingFeedback
from qgis.core import QgsApplication
from qgis.analysis import QgsNativeAlgorithms
import processing
@ -11,8 +11,14 @@ qgs.initQgis()
# Add native algorithms provider
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
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'
layer = \
'C:/Users/a_adli/PycharmProjects/' \
'hydroquebec_archetype_gispy/data/' \
'input_data/Geoindex_81670/mamh_usage_predo_2022_s_poly.shp'
fixed_layer = \
'C:/Users/a_adli/PycharmProjects/' \
'hydroquebec_archetype_gispy/data/' \
'output_data/fixed_geoindex_7/py_fixes_07.shp'
params = {'INPUT': layer, 'METHOD': 0, 'OUTPUT': fixed_layer}
fix_layer = processing.run("native:fixgeometries", params)['OUTPUT']

View File

@ -1,6 +1,5 @@
# Join attributes by location in QGIS is the same as Spatial Join in ArcGIS
from qgis.core import QgsApplication, QgsVectorLayer, QgsProject, QgsProcessingFeedback
# This services is not used in the main workflow
from qgis.core import QgsApplication
from qgis.analysis import QgsNativeAlgorithms
import processing
@ -15,15 +14,27 @@ qgs.initQgis()
# Add native algorithms provider
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
layer_1_path = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/new_tests/python_fixed_05/py_fixes_05.shp'
layer_2_path = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/property_assessment/uniteevaluationfonciere.shp'
output_layer_path = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/tests/intersection_nrcan_property/intersection_nrcan_property.shp'
layer_1_path = \
'C:/Users/a_adli/PycharmProjects/' \
'hydroquebec_archetype_gispy/data/' \
'output_data/clipped_nrcan_1/clipped_nrcan_1.shp'
layer_2_path = \
'C:/Users/a_adli/PycharmProjects/' \
'hydroquebec_archetype_gispy/data/' \
'input_data/property_assessment/uniteevaluationfonciere.shp'
output_layer_path = \
'C:/Users/a_adli/PycharmProjects/' \
'hydroquebec_archetype_gispy/data/' \
'output_data/intersection_nrcan_property/intersection_nrcan_property_02.shp'
params = {'INPUT': layer_1_path,
'OVERLAY': layer_2_path,
'INPUT_FIELDS': [], 'OVERLAY_FIELDS': [], 'OVERLAY_FIELDS_PREFIX': '',
'OUTPUT': output_layer_path, 'GRID_SIZE': None}
'INPUT_FIELDS': [],
'OVERLAY_FIELDS': [],
'OVERLAY_FIELDS_PREFIX': '',
'OUTPUT': output_layer_path,
'GRID_SIZE': None}
processing.run("native:intersection", params)

View File

@ -1,6 +1,6 @@
# Join attributes by location in QGIS is the same as Spatial Join in ArcGIS
from qgis.core import QgsApplication, QgsVectorLayer, QgsProject, QgsProcessingFeedback
from qgis.core import QgsApplication, QgsProcessingFeedback
from qgis.analysis import QgsNativeAlgorithms
import processing
@ -15,9 +15,18 @@ qgs.initQgis()
# Add native algorithms provider
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
layer_1_path = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/tests/python_fixed_05/py_fixes_05.shp'
layer_2_path = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/property_assessment/uniteevaluationfonciere.shp'
output_layer_path = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/tests/python_joined_12/py_joined_12.shp'
layer_1_path = \
'C:/Users/a_adli/PycharmProjects/' \
'hydroquebec_archetype_gispy/data/' \
'output_data/clipped_nrcan_1/clipped_nrcan_1.shp'
layer_2_path = \
'C:/Users/a_adli/PycharmProjects/' \
'hydroquebec_archetype_gispy/data/' \
'output_data/intersection_nrcan_property/intersection_nrcan_property_02.shp'
output_layer_path = \
'C:/Users/a_adli/PycharmProjects/' \
'hydroquebec_archetype_gispy/data/' \
'output_data/join_nrcan_intersected_nrcan/join_nrcan_intersected_nrcan.shp'
params = {'INPUT': layer_1_path,

View File

@ -1,7 +1,7 @@
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)
QgsApplication.setPrefixPath('C:/Program Files/QGIS 3.34.1/apps/qgis', True)
qgs = QgsApplication([], False)
@ -15,4 +15,4 @@ def load_layer(path, layer_name):
print(f'{layer_name} failed to load!')
else:
QgsProject.instance().addMapLayer(the_layer)
return the_layer, layer_name
return the_layer, layer_name

View File

@ -1,10 +1,13 @@
from qgis.core import QgsApplication, QgsVectorLayer, QgsProcessingFeedback
from qgis.core import QgsApplication
from qgis.analysis import QgsNativeAlgorithms
import processing
from services_scripts.basic_functions import find_shp_files
root_folder = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/aEndeavor/clip_all'
root_folder = \
'C:/Users/a_adli/PycharmProjects/' \
'hydroquebec_archetype_gispy/data/' \
'output_data/clip_all'
layers_path = find_shp_files(root_folder)
# Set the path to QGIS installation
@ -17,7 +20,10 @@ qgs.initQgis()
# Add native algorithms provider
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
merged_layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/aEndeavor/merged_all/merge_divisions.shp'
merged_layer = \
'C:/Users/a_adli/PycharmProjects/' \
'hydroquebec_archetype_gispy/data/' \
'output_data/merged_all/merge_divisions.shp'
params = {'LAYERS': layers_path,
'CRS': None,

View File

@ -1,4 +1,4 @@
from qgis.core import QgsApplication, QgsVectorLayer, QgsProcessingFeedback
from qgis.core import QgsApplication
from qgis.analysis import QgsNativeAlgorithms
import processing
@ -13,8 +13,14 @@ qgs.initQgis()
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
input_layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/output_data/delete_dups_4_partitioned|layername=delete_dups__partitioned'
singled_parts_layer = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/output_data/multi_to_single/multi_to_single2.shp'
input_layer = \
'C:/Users/a_adli/PycharmProjects/' \
'hydroquebec_archetype_gispy/data/' \
'output_data/delete_dups_4_partitioned|layername=delete_dups__partitioned'
singled_parts_layer = \
'C:/Users/a_adli/PycharmProjects/' \
'hydroquebec_archetype_gispy/data/' \
'output_data/multi_to_single/multi_to_single2.shp'
params = {'INPUT': input_layer,
'OUTPUT': singled_parts_layer}

View File

@ -1,4 +1,4 @@
from qgis.core import QgsApplication, QgsVectorLayer, QgsProject, QgsProcessingFeedback
from qgis.core import QgsApplication, QgsVectorLayer, QgsProject
from qgis.analysis import QgsNativeAlgorithms
import processing
import os
@ -26,11 +26,17 @@ qgs.initQgis()
# Add native algorithms provider
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
output_layers_folder = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/aEndeavor/divided_all'
num_layers = 3
output_layers_folder = \
'C:/Users/a_adli/PycharmProjects/' \
'hydroquebec_archetype_gispy/data/' \
'output_data/divided_all'
num_layers = 20
create_folders(output_layers_folder, num_layers)
layer_path = 'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/aEndeavor/02_clipped_nrcan_mtl_boundary/clipped_nrcan.shp'
layer_path = \
'C:/Users/a_adli/PycharmProjects/' \
'hydroquebec_archetype_gispy/data/' \
'output_data/02_clipped_nrcan_mtl_boundary/clipped_nrcan.shp'
fixed_nrcan, fixed_nrcan_name = load_layer(layer_path, 'Clipped NRCan')
layer_length = fixed_nrcan.featureCount()
@ -39,23 +45,37 @@ print(f'{fixed_nrcan_name} data count: {layer_length}')
intervals = layer_length // num_layers
for each in range(num_layers):
output_layer_path = f'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/aEndeavor/divided_all/layer_{each}/layer_{each}.shp'
output_layer_path = \
f'C:/Users/a_adli/PycharmProjects/' \
f'hydroquebec_archetype_gispy/data/' \
f'output_data/divided_all/layer_{each}/layer_{each}.shp'
params = {'INPUT': layer_path,
'EXPRESSION': f'$id >= {each * intervals} AND $id < {(each + 1) * intervals}\r\n',
'EXPRESSION': f'$id >= {each * intervals} '
f'AND $id < {(each + 1) * intervals}\r\n',
'OUTPUT': output_layer_path}
processing.run("native:extractbyexpression", params)
# Creating spatian index for each new layer
create_spatial_indext_params = {'INPUT': output_layer_path,
'OUTPUT': 'Output'}
processing.run("native:createspatialindex", create_spatial_indext_params)
remaining_features = num_layers
os.makedirs(f'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/aEndeavor/divided_all/layer_{remaining_features}')
output_layer_path = f'C:/Users/a_adli/PycharmProjects/hydroquebec_archetype_gispy/data/aEndeavor/divided_all/layer_{remaining_features}/layer_{remaining_features}.shp'
os.makedirs(f'C:/Users/a_adli/PycharmProjects/'
f'hydroquebec_archetype_gispy/data/'
f'output_data/divided_all/layer_{remaining_features}')
output_layer_path = \
f'C:/Users/a_adli/PycharmProjects/' \
f'hydroquebec_archetype_gispy/data/' \
f'output_data/divided_all/' \
f'layer_{remaining_features}/layer_{remaining_features}.shp'
params = {'INPUT': layer_path,
'EXPRESSION': f'$id >= {num_layers * intervals}\r\n',
'OUTPUT': output_layer_path}
processing.run("native:extractbyexpression", params)
# Exit QGIS application
qgs.exitQgis()