@@ -54,11 +54,13 @@ def handler(event, context):
5454 return response
5555
5656 postal_code = extract_postal_prefix (params_full_list .get ("q" ))
57+ postal_code_detected = False
5758
5859 if (postal_code and key_in_params ("locate" , params_full_list )):
5960 #print("Postal code detected, using forward sortation area instead")
6061 q = extract_postal_prefix (params_full_list .get ("q" ))
6162 params_full_list .update ({"q" : q }) #need to update this variable as only q and lang are used for caching
63+ postal_code_detected = True
6264 else :
6365 q = params_full_list .get ("q" )
6466
@@ -106,6 +108,40 @@ def handler(event, context):
106108 item_keys ,
107109 dev )
108110 loads .extend (items )
111+
112+ if postal_code_detected :
113+ service_id = "nominatim"
114+ service_schema = schemas .get (service_id )
115+ lat_lon = f"{ loads [0 ]['lat' ]} ,{ loads [0 ]['lng' ]} "
116+ params_full_list .update ({"q" : lat_lon })
117+ # Adjust the parameters to the service's schema
118+ url , params , code_table_urls = assemble_url (service_schema , params_full_list .copy ())
119+ service_load = url_request (url , params , service_id )
120+ items = items_from_service (service_id ,
121+ table_params ,
122+ service_schema ,
123+ output_schema_items ,
124+ service_load ,
125+ item_keys ,
126+ dev )
127+ loads .extend (items )
128+
129+ service_id = "geonames"
130+ service_schema = schemas .get (service_id )
131+ params_full_list .pop ("q" )
132+ params_full_list .update ({"lat" : f"{ loads [0 ]['lat' ]} " , "lon" : f"{ loads [0 ]['lng' ]} " })
133+ # Adjust the parameters to the service's schema
134+ url , params , code_table_urls = assemble_url (service_schema , params_full_list .copy ())
135+ service_load = url_request (url , params , service_id )
136+ items = items_from_service (service_id ,
137+ table_params ,
138+ service_schema ,
139+ output_schema_items ,
140+ service_load ,
141+ item_keys ,
142+ dev )
143+ #print("items", items)
144+ loads .extend (items )
109145
110146 # add query result to cache
111147 if q_alphanumeric (q ) and response_ok :
@@ -179,12 +215,27 @@ def q_alphanumeric(q):
179215
180216 return False
181217
218+
182219def extract_postal_prefix (postal_code ):
183220 """
184- Regular expression to match postal code with a space in between
221+ Extracts the first three characters of a valid Canadian postal code.
222+ Supports:
223+ - Standard format: "A1B 2C3"
224+ - Plus sign separator (+ is appended by API Gateway): "A1B+2C3"
225+ - No separator: "A1B2C3"
226+ - FSA-only input: "A1B"
227+ - Optional '*' at the end: "A1B2C3*"
228+ - New groups for (A1B2 or A1B2C) and (A1B2* or A1B2C*)
185229 """
186- if re .fullmatch (r'[A-Za-z]\d[A-Za-z][\s\+]?(\d[A-Za-z]\d)' , postal_code ):
187- return postal_code [:3 ]
230+
231+ match = re .match (r'^([A-Za-z]\d[A-Za-z])(?:[\s\+]?\d[A-Za-z]\d)?\*?$|^([A-Za-z]\d[A-Za-z]\d)\*?$|^([A-Za-z]\d[A-Za-z]\d[A-Za-z])\*?$' , postal_code )
232+
233+ # Extract the first three characters from the match group
234+ if match :
235+ # group(1) handles A1B2C3, A1B 2C3, A1B+2C3, A1B, and optional '*' at the end e.g., "A1B2C3*"
236+ # group(2) handles A1B2 or A1B2*
237+ # group(3) handles A1B2C or A1B2C*
238+ return match .group (1 ) or match .group (2 ) or match .group (3 )
188239 return None
189240
190241def key_in_params (key , params_full_list ):
0 commit comments