From 6bb0902faaf77f8e2d163ed3f19376660145c344 Mon Sep 17 00:00:00 2001 From: zyphlar Date: Wed, 25 Feb 2026 10:25:48 -0800 Subject: [PATCH] claude fixes to too-few matches --- compare-addresses.py | 52 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/compare-addresses.py b/compare-addresses.py index 6e9ae63..09a2926 100644 --- a/compare-addresses.py +++ b/compare-addresses.py @@ -386,13 +386,29 @@ out geom;""" osm_row = osm_gdf.iloc[osm_idx] osm_point = osm_row.geometry distance = local_point.distance(osm_point) - - # Additional verification: check if house numbers match (handle type differences) + + # Verify house number, street, and unit (if present) match local_house_num = str(local_row.get('addr:housenumber', '')) osm_house_num = str(osm_row.get('addr:housenumber', '')) - - # Only consider as potential match if house numbers match - if local_house_num == osm_house_num and distance < min_distance: + + # Check street name match (required) + local_street = str(local_row.get('addr:street', '')).strip().lower() + osm_street = str(osm_row.get('addr:street', '')).strip().lower() + street_match = (local_street == osm_street and local_street != '' and local_street != 'nan') + + # Check unit match (only if both have units specified) + local_unit = local_row.get('addr:unit') + osm_unit = osm_row.get('addr:unit') + # Unit matches if: (1) both are None/null, OR (2) both have values and they're equal + unit_match = True + if local_unit is not None and pd.notna(local_unit) and osm_unit is not None and pd.notna(osm_unit): + # Both have units - they must match + unit_match = (str(local_unit).strip().lower() == str(osm_unit).strip().lower()) + + # Only consider as potential match if house numbers, street, and unit all match + if (local_house_num == osm_house_num and + street_match and unit_match and + distance < min_distance): min_distance = distance best_match = osm_idx @@ -430,18 +446,32 @@ out geom;""" for idx, local_row in local_gdf.iterrows(): local_point = local_row.geometry nearby_indices = osm_index.query(local_point.buffer(self.tolerance_deg)) - + for osm_idx in nearby_indices: osm_row = osm_gdf.iloc[osm_idx] osm_point = osm_row.geometry distance_meters = local_point.distance(osm_point) * 111000.0 - - # Check if house numbers match (handle type differences) + + # Verify house number, street, and unit (if present) match local_house_num = str(local_row.get('addr:housenumber', '')) osm_house_num = str(osm_row.get('addr:housenumber', '')) - - if (distance_meters <= self.tolerance_meters and - local_house_num == osm_house_num): + + # Check street name match (required) + local_street = str(local_row.get('addr:street', '')).strip().lower() + osm_street = str(osm_row.get('addr:street', '')).strip().lower() + street_match = (local_street == osm_street and local_street != '' and local_street != 'nan') + + # Check unit match (only if both have units specified) + local_unit = local_row.get('addr:unit') + osm_unit = osm_row.get('addr:unit') + unit_match = True + if local_unit is not None and pd.notna(local_unit) and osm_unit is not None and pd.notna(osm_unit): + # Both have units - they must match + unit_match = (str(local_unit).strip().lower() == str(osm_unit).strip().lower()) + + if (distance_meters <= self.tolerance_meters and + local_house_num == osm_house_num and + street_match and unit_match): matched_osm_indices.add(osm_idx) break # Only match to first OSM address found