File size: 2,314 Bytes
2d144e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import googlemaps
from datetime import datetime
from pprint import pprint


class AddressLocator:
    def __init__(self):
        self.maps = googlemaps.Client(key='AIzaSyAFUBwCyykt-8nfOYqGvUZbXV0dMnQYTJ4')

    # Takes the name of a place and returns the exact address
    def get_location(self, sentence):
        sentence = sentence.lower()
        commands = ["take picture", "go to", "land", "take off"]
        for command in commands:
            if command in sentence:
                address_start_index = sentence.index(command) + len(command) + 1
                address = sentence[address_start_index:].strip()
                try:
                    response = self.maps.places(query=address)
                    results = response.get('results')
                    if results:
                        return results[0]['formatted_address']
                    else:
                        return None
                except Exception as e:
                    print(e)
                    return None
        return None

    # Takes the name of a place and returns the route from one location to a single destination
    def compute_route(self, uav_location, destination):
        now = datetime.now()
        directions_result = self.maps.directions(uav_location, destination,
                                                 mode="driving",
                                                 optimize_waypoints=True,
                                                 departure_time=now
                                                 )

        directions = directions_result[0]['legs'][0]['steps']
        instructions = [step['html_instructions'] for step in directions]
        text_instructions = [self.strip_html_tags(instruction) for instruction in instructions]
        return text_instructions

    @staticmethod
    def strip_html_tags(text):
        import re
        clean = re.compile('<.*?>')
        return re.sub(clean, '', text)


# Testing
address_locator = AddressLocator()

# Placeholder
# uav_location = "Current location"

loc = address_locator.get_location("Go to Chick-fil-a on W Broad St")
print(loc)

loc2 = address_locator.get_location("Go to Roots on W Grace St")
print(loc2)

res = address_locator.compute_route(loc, loc2)

for direction in res:
    print(direction)