From 79bd333651fc95b051e0e1c83b33e792b8423e6d Mon Sep 17 00:00:00 2001
From: Eric Duminil <eric.duminil@gmail.com>
Date: Mon, 6 May 2024 11:19:33 +0200
Subject: [PATCH] Check if trees are duplicate.

---
 .../import_existing_trees.py                           | 10 ++++++----
 python_scripts/add_trees_to_open_street_map/tree.py    |  4 +++-
 2 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/python_scripts/add_trees_to_open_street_map/import_existing_trees.py b/python_scripts/add_trees_to_open_street_map/import_existing_trees.py
index c450783..4c5d45e 100644
--- a/python_scripts/add_trees_to_open_street_map/import_existing_trees.py
+++ b/python_scripts/add_trees_to_open_street_map/import_existing_trees.py
@@ -5,18 +5,20 @@ from tree import Tree, Forest
 def get_existing_forest(shp_input):
     print(f"Importing {shp_input}")
     df = gpd.read_file(shp_input)
-    trees = []
+    forest = Forest()
     for tree_row in df.itertuples():
         point = tree_row.geometry
-        trees.append(Tree(point.x, point.y,
+        added = forest.add_tree_if_possible(0.1, point.x, point.y,
                           description=tree_row.Bezeichnun,
                           diameter=tree_row.Kronenbrei,
                           type=tree_row.Baumart,
                           trunk_diameter=tree_row.Stammumfan,
                           height=tree_row.Baumhöhe,
                           source=Path(shp_input).name
-                          ))
-    return Forest(trees)
+                          )
+        if not added:
+            print(f"Warning, tree seems to be too close to others! Is it a duplicate?\n\t{tree_row}")
+    return forest
 
 if __name__ == "__main__":
     print(repr(get_existing_forest('existing_trees/Trees_ideal_2_20240227.shp')))
diff --git a/python_scripts/add_trees_to_open_street_map/tree.py b/python_scripts/add_trees_to_open_street_map/tree.py
index 8bc3162..3a80179 100644
--- a/python_scripts/add_trees_to_open_street_map/tree.py
+++ b/python_scripts/add_trees_to_open_street_map/tree.py
@@ -39,11 +39,13 @@ class Forest(UserList):
 
         self.data = existing_trees
 
-    def add_tree_if_possible(self, min_distance_2, x, y, **kparams):
+    def add_tree_if_possible(self, min_distance_2, x, y, **kparams) -> bool:
         _nearest_tree, distance_2 = self.kd_tree.search_nn((x, y))
         if distance_2 > min_distance_2:
             self.kd_tree.add((x, y))
             self.append(Tree(x, y, **kparams))
+            return True
+        return False
 
     @property
     def xs_ys_cs(self):
-- 
GitLab