· 5 years ago · Aug 04, 2020, 10:10 AM
1# Enter your API key here
2# Available from dev section of w3w site
3my_api='XXXXXXX'
4
5# let's go
6import what3words
7import re
8
9geocoder = what3words.Geocoder(my_api)
10done=False
11
12regex = "^/*[^0-9`~!@#$%^&*()+\-_=[{\]}\\|'<,.>?/\";:£§º©®\s]{1,}[・.][^\s]{1,}[・.][^\s]{1,}$"
13while not done:
14 w3w=input('What 3 Words? : ')
15 if (re.search(regex, w3w, flags=re.UNICODE)):
16 done=True
17 else:
18 print(w3w + " is NOT a three word address")
19
20res = geocoder.convert_to_coordinates(w3w)
21
22if 'error' in res: #there is a problem
23 code = res['error']['code']
24 message = res['error']['message']
25 print(code,message,sep=': ')
26
27else: #no problem
28 country=res['country']
29 nearest=res['nearestPlace']
30 coords=res['coordinates']
31
32 print(coords['lng'],coords['lat'],sep=',')
33 print(nearest, country)
34