Fix duplicate polygon queries causing redundant API calls

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 <noreply@anthropic.com>
This commit is contained in:
Nitzan P
2025-11-27 00:07:15 +02:00
parent bd0932abf6
commit 421dec37b5
+17 -4
View File
@@ -608,12 +608,15 @@ class GovmapClient:
logger.info(f"Using coordinates: {point}") logger.info(f"Using coordinates: {point}")
# Extract polygon metadata with coordinates for distance calculation # 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: for metadata in nearby_polygons:
polygon_id = metadata.get("polygon_id") polygon_id = metadata.get("polygon_id")
if not polygon_id: if not polygon_id:
continue continue
polygon_id_str = str(polygon_id)
# Extract polygon center coordinates (use shape if available, fallback to search point) # Extract polygon center coordinates (use shape if available, fallback to search point)
# Most polygon metadata includes shape which can be parsed for center # Most polygon metadata includes shape which can be parsed for center
# For now, use metadata coordinates if available, otherwise estimate # For now, use metadata coordinates if available, otherwise estimate
@@ -628,9 +631,19 @@ class GovmapClient:
if poly_coords: if poly_coords:
distance = utils.calculate_distance(point, poly_coords) distance = utils.calculate_distance(point, poly_coords)
polygon_metadata_list.append( # Deduplicate: keep the polygon with shortest distance if duplicate IDs exist
{"polygon_id": str(polygon_id), "distance": distance, "metadata": metadata} 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) # Sort polygons by distance (closest first)
polygon_metadata_list.sort(key=lambda x: x["distance"]) polygon_metadata_list.sort(key=lambda x: x["distance"])