From ba0645f798a315b7852c4e6c00925f324d2a9b22 Mon Sep 17 00:00:00 2001 From: Alireza Adli Date: Fri, 23 Aug 2024 16:50:24 -0400 Subject: [PATCH] Remove the duplicates & Finalize --- .../remove_nrcan_duplicates.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/varennes_single_processes/remove_nrcan_duplicates.py b/varennes_single_processes/remove_nrcan_duplicates.py index 95b6b0f..f0454b2 100644 --- a/varennes_single_processes/remove_nrcan_duplicates.py +++ b/varennes_single_processes/remove_nrcan_duplicates.py @@ -81,7 +81,7 @@ counter = 0 centroid_x = varennes_nrcan_centroids_df['centroid_x'].tolist() centroid_y = varennes_nrcan_centroids_df['centroid_y'].tolist() feature_ids_all = varennes_nrcan_centroids_df['feature_id'].tolist() -feature_ids = [] +duplicated_feature_ids = [] for feature_index in range(len(centroid_x)): for next_feature_index in range(feature_index + 1, len(centroid_x)): @@ -92,11 +92,18 @@ for feature_index in range(len(centroid_x)): b_y = centroid_y[next_feature_index] subtract_y = a_y - b_y if abs(subtract_x) < tolerance and abs(subtract_y) < tolerance: - feature_ids.append(feature_ids_all[next_feature_index]) + duplicated_feature_ids.append(feature_ids_all[next_feature_index]) -duplicated_features = {'dups_id': feature_ids} -duplicated_features_df = pd.DataFrame(duplicated_features) - -# Removing records based on the duplicated_features_df (dataframe) +# Removing records based on the duplicated_feature_ids list +varennes_nrcan_centroids.layer.startEditing() +features = varennes_nrcan_centroids.layer.getFeatures() +# Iterate through features in the layer +for feature in features: + if feature['feature_id'] in duplicated_feature_ids: + # Delete the feature + varennes_nrcan_centroids.layer.deleteFeature(feature.id()) + +# Save changes and stop editing +varennes_nrcan_centroids.layer.commitChanges()