Created
May 28, 2017 02:04
-
-
Save erickmendonca/ec2cf2be3f77cdcaea360cddfe787272 to your computer and use it in GitHub Desktop.
Get LatLong for addresses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import requests | |
import urllib | |
from typing import Iterator, Dict, Tuple, List | |
FILENAME = 'list.txt' | |
GOOGLE_MAPS_URL = 'https://maps.googleapis.com/maps/api/geocode/json' | |
API_KEY = '' | |
def read_file(filename: str) -> Iterator[str]: | |
with open(filename, 'r') as source: | |
return source.readlines() | |
def parse_google_maps_result(response_dict: Dict) -> Tuple[float, float]: | |
try: | |
results = response_dict.get('results', []) | |
lat, lng = results[0]["geometry"]["location"].values() | |
return (lat, lng) | |
except Exception: | |
return (None, None) | |
def main(args: List[str]) -> None: | |
if args[0]: | |
FILENAME = args[0] | |
for line in read_file(FILENAME): | |
address = line.splitlines()[0] | |
response = requests.get( | |
GOOGLE_MAPS_URL, | |
params={ | |
'address': address, | |
'key': API_KEY | |
} | |
).json() | |
lat, lng = parse_google_maps_result(response) | |
print("{0} ; {1} , {2}".format( | |
address, lat, lng | |
) | |
) | |
if __name__ == "__main__": | |
main(sys.argv[1:]) # first args is always the script name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment