Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polygon merging incomplete #330

Open
davecraig opened this issue Jan 13, 2025 · 0 comments
Open

Polygon merging incomplete #330

davecraig opened this issue Jan 13, 2025 · 0 comments

Comments

@davecraig
Copy link
Contributor

davecraig commented Jan 13, 2025

If a feature has more than 1 duplicate, then they each just get merged with the first "original", but not with each other. I was using the golf course in testVectorToGeoJsonGrid as an example.

image

(I had added in a call to mergeAllPolygonsInFeatureCollection(featureCollection) to the test).

My attempts to fix this failed as the result of the first duplicate/original merge was failing validation attempts when going on to merge with subsequent duplicates. My changes (which could be wrong!) looked like this:

--- a/app/src/main/java/org/scottishtecharmy/soundscape/geoengine/utils/TileUtils.kt
+++ b/app/src/main/java/org/scottishtecharmy/soundscape/geoengine/utils/TileUtils.kt
@@ -1662,7 +1662,7 @@ fun mergeAllPolygonsInFeatureCollection(
         }
     }
 
-    val mergedPolygonsFeatureCollection = FeatureCollection()
+    val mergedPolygons = hashMapOf<Double, Feature>()
     val duplicateLineStringsAndPoints = FeatureCollection()
     val originalPolygonsUsedInMerge = mutableSetOf<Any>() // Track original polygons
 
@@ -1674,7 +1674,23 @@ fun mergeAllPolygonsInFeatureCollection(
 
         // Merge duplicate polygons
         if (originalFeature != null && originalFeature.geometry.type == "Polygon" && duplicate.geometry.type == "Polygon") {
-            mergedPolygonsFeatureCollection.features.add(mergePolygons(originalFeature, duplicate))
+
+            originalFeature.foreign?.get("osm_ids")?.let {
+                val osmIdArray = it as ArrayList<Double>
+                if(!mergedPolygons.containsKey(osmIdArray[0])) {
+                    mergedPolygons[osmIdArray[0]] = mergePolygons(
+                        originalFeature,
+                        duplicate
+                    )
+                } else {
+                    mergedPolygons[osmIdArray[0]] = mergePolygons(
+                        mergedPolygons[osmIdArray[0]]!!,
+                        duplicate
+                    )
+                }
+                if(osmIdArray[0] == 2191281972.0)
+                    println("Golf!")
+            }
             // Add to the set
             originalFeature.foreign?.get("osm_ids")?.let { originalPolygonsUsedInMerge.add(it) }
             // Add to the set
@@ -1690,8 +1706,9 @@ fun mergeAllPolygonsInFeatureCollection(
     val finalFeatureCollection = FeatureCollection()
 
     // Add merged Polygons
-    finalFeatureCollection.features.addAll(mergedPolygonsFeatureCollection.features)
-
+    for(feature in mergedPolygons) {
+        finalFeatureCollection.addFeature(feature.value)
+    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant