|
| 1 | +import requests |
| 2 | +import sys |
| 3 | + |
| 4 | +# If you want your own private_token please refer the README file for this script. |
| 5 | +private_token = "<Your Private Token>" |
| 6 | + |
| 7 | +while True: |
| 8 | + choice = input( |
| 9 | + "Type G for geocoding , type R for reverse geocoding and type Q to quit the script:") |
| 10 | + print() |
| 11 | + |
| 12 | + # If the user choose to perform Geocoding |
| 13 | + if choice.upper() == 'G': |
| 14 | + # Base Url for geocoding |
| 15 | + url = "https://us1.locationiq.com/v1/search.php" |
| 16 | + |
| 17 | + address = input("Input the address: ") |
| 18 | + |
| 19 | + # Parameters for the geocoding url |
| 20 | + data = { |
| 21 | + 'key': private_token, |
| 22 | + 'q': address, |
| 23 | + 'format': 'json' |
| 24 | + } |
| 25 | + |
| 26 | + response = requests.get(url, params=data) |
| 27 | + |
| 28 | + # To run only if we get success response |
| 29 | + if response.status_code == 200: |
| 30 | + latitude = response.json()[0]['lat'] |
| 31 | + longitude = response.json()[0]['lon'] |
| 32 | + |
| 33 | + print(f"The latitude of the given address is: {latitude}") |
| 34 | + print(f"The longitude of the given address is: {longitude}") |
| 35 | + print() |
| 36 | + else: |
| 37 | + sys.exit("Cant find what you where looking for") |
| 38 | + |
| 39 | + # If the user choose to perform Reverse Geocoding |
| 40 | + elif choice.upper() == 'R': |
| 41 | + # Base Url for reverse geocoding |
| 42 | + url = "https://us1.locationiq.com/v1/reverse.php" |
| 43 | + |
| 44 | + latitude_reverse = input("Enter the latitude: ") |
| 45 | + longitude_reverse = input("Enter the longitude: ") |
| 46 | + |
| 47 | + # Parameters for the reverse geocoding url |
| 48 | + data = { |
| 49 | + 'key': private_token, |
| 50 | + 'lat': latitude_reverse, |
| 51 | + 'lon': longitude_reverse, |
| 52 | + 'format': 'json' |
| 53 | + } |
| 54 | + |
| 55 | + response = requests.get(url, params=data) |
| 56 | + |
| 57 | + # To run only if we get success response |
| 58 | + if response.status_code == 200: |
| 59 | + address = response.json()['display_name'] |
| 60 | + print(f"The address is: {address}") |
| 61 | + print() |
| 62 | + else: |
| 63 | + print("Cant find what you where looking for.") |
| 64 | + |
| 65 | + # If the user choose to Quit the program |
| 66 | + elif choice.upper() == 'Q': |
| 67 | + sys.exit("Thanks for using the script") |
| 68 | + |
| 69 | + # To make sure only valid input is provided |
| 70 | + else: |
| 71 | + print("Please make a valid choice") |
| 72 | + print() |
| 73 | + |
| 74 | +#Sample Input - Output: |
| 75 | + |
| 76 | +#If you choose Geocoding: |
| 77 | + |
| 78 | +#Address(Input): Rashtrapati Bhavan |
| 79 | +#Latitude(Output): 28.614458 |
| 80 | +#Longitude(Output): 77.199594 |
| 81 | + |
| 82 | +#If you choose Reverse-Geocoding: |
| 83 | + |
| 84 | +#Latitude(Input): 28.614458 |
| 85 | +#Longitude(Input): 77.199594 |
| 86 | +#Address(Output): Rashtrapati Bhavan, Rajpath, Presidential Estate, Chanakya Puri Tehsil, New Delhi, Delhi, 110004, India |
0 commit comments