From bd2a523f4634e6bc24150849535fe7cdb0f66e45 Mon Sep 17 00:00:00 2001 From: Alireza Adli Date: Tue, 27 Aug 2024 13:30:33 -0400 Subject: [PATCH] Complete the workflow --- .../add_height_to_nrcan.py | 36 +++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/varennes_single_processes/add_height_to_nrcan.py b/varennes_single_processes/add_height_to_nrcan.py index 6834136..f72c5eb 100644 --- a/varennes_single_processes/add_height_to_nrcan.py +++ b/varennes_single_processes/add_height_to_nrcan.py @@ -37,11 +37,35 @@ varennes_nrcan.delete_field('min_x') varennes_nrcan.delete_field('min_y') varennes_nrcan.delete_field('max_x') varennes_nrcan.delete_field('max_y') +varennes_nrcan.delete_field('comment') +varennes_nrcan.layer.startEditing() -# varennes_nrcan_duplicate.delete_field('heightmin') -# varennes_nrcan_duplicate.delete_field('heightmax') -# varennes_nrcan_duplicate.delete_field('elevmin') -# varennes_nrcan_duplicate.delete_field('elevmax') -# varennes_nrcan_duplicate.delete_field('bldgarea') -# varennes_nrcan_duplicate.delete_field('comment') \ No newline at end of file +# Add a new field for the height +weighted_height = QgsField('building_height', QVariant.Double) +varennes_nrcan.layer.dataProvider().addAttributes([weighted_height]) +varennes_nrcan.layer.updateFields() + +height_min_idx = varennes_nrcan.layer.fields().indexOf('heightmin') +height_max_idx = varennes_nrcan.layer.fields().indexOf('heightmax') +weighted_height_idx = varennes_nrcan.layer.fields().indexOf('ave_height') + +# Iterate through each feature and calculate the average weighted height +for feature in varennes_nrcan.layer.getFeatures(): + height_min = feature[height_min_idx] + height_max = feature[height_max_idx] + + if height_min is not None and height_max is not None: + average_value = (2/3 * height_min + 1/3 * height_max) + else: + average_value = None + + # Update the new field with the calculated average + varennes_nrcan.layer.changeAttributeValue( + feature.id(), weighted_height_idx, average_value) + +# Commit the changes +varennes_nrcan.layer.commitChanges() + +print('Average weighted height calculation (ave_height)' + ' for buildings completed successfully!')