From 421dec37b53c6c8584a867f8a8e14fd72f610d7a Mon Sep 17 00:00:00 2001 From: Nitzan P <9297302+nitzpo@users.noreply.github.com> Date: Thu, 27 Nov 2025 00:07:15 +0200 Subject: [PATCH] Fix duplicate polygon queries causing redundant API calls MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug: When the Govmap API returns the same polygon_id multiple times (which can happen for addresses with multiple associated polygons), the code was processing each duplicate separately, leading to: - Duplicate API calls for street/neighborhood deals - Wasted API quota and slower performance - Confusing logs showing same polygon queried multiple times Example from logs: INFO: Querying polygon 53283601 (distance: 0m from search point) INFO: Getting street deals for polygon: 53283601 INFO: Querying polygon 53283601 (distance: 0m from search point) INFO: Getting street deals for polygon: 53283601 Root cause: - nearby_polygons API response can contain duplicate polygon_ids - Code was appending all polygons without deduplication - Log message claimed "unique polygon IDs" but didn't enforce it Solution: - Use dict to deduplicate polygons by polygon_id (lines 612-643) - When duplicates exist, keep the one with shortest distance - Convert dict to list after deduplication - Now truly have unique polygon IDs as the log claims Impact: - Reduces API calls (fewer duplicates = less load on Govmap API) - Faster queries (less redundant processing) - Clearer logs (no confusing duplicate entries) - Better performance for addresses with many associated polygons 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- nadlan_mcp/govmap/client.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/nadlan_mcp/govmap/client.py b/nadlan_mcp/govmap/client.py index f719fbe..55dbc2c 100644 --- a/nadlan_mcp/govmap/client.py +++ b/nadlan_mcp/govmap/client.py @@ -608,12 +608,15 @@ class GovmapClient: logger.info(f"Using coordinates: {point}") # Extract polygon metadata with coordinates for distance calculation - polygon_metadata_list = [] + # Use dict to deduplicate by polygon_id (keep closest occurrence) + polygon_dict = {} for metadata in nearby_polygons: polygon_id = metadata.get("polygon_id") if not polygon_id: continue + polygon_id_str = str(polygon_id) + # Extract polygon center coordinates (use shape if available, fallback to search point) # Most polygon metadata includes shape which can be parsed for center # For now, use metadata coordinates if available, otherwise estimate @@ -628,9 +631,19 @@ class GovmapClient: if poly_coords: distance = utils.calculate_distance(point, poly_coords) - polygon_metadata_list.append( - {"polygon_id": str(polygon_id), "distance": distance, "metadata": metadata} - ) + # Deduplicate: keep the polygon with shortest distance if duplicate IDs exist + if ( + polygon_id_str not in polygon_dict + or distance < polygon_dict[polygon_id_str]["distance"] + ): + polygon_dict[polygon_id_str] = { + "polygon_id": polygon_id_str, + "distance": distance, + "metadata": metadata, + } + + # Convert dict to list + polygon_metadata_list = list(polygon_dict.values()) # Sort polygons by distance (closest first) polygon_metadata_list.sort(key=lambda x: x["distance"])