Example #1
0
class DistanceMatrixTest(unittest.TestCase):
    def setUp(self):
        self.google_maps = GoogleMaps(api_key=TEST_API_KEY)
        self.duration_regex = r'([0-9]*)d ([0-9]*)h ([0-9]*)m ([0-9]*)s'
        self.delta_km = 25
        self.delta_m = 25000
        self.delta_miles = 25

    def test_distance_matrix(self):
        origins = ['rybnik', 'oslo']
        destinations = ['zagrzeb']

        items = self.google_maps.distance(origins, destinations).all()

        for item in items:
            if item.origin == 'Rybnik, Poland':
                self.assertEqual(item.destination, 'Zagreb, Croatia')
                self.assertAlmostEqual(Decimal(709), item.distance.kilometers, delta=self.delta_km)
                self.assertAlmostEqual(Decimal(713000), item.distance.meters, delta=self.delta_m)
                self.assertAlmostEqual(Decimal(443.0368), item.distance.miles, delta=self.delta_miles)
                self.assertRegexpMatches(str(item.duration), self.duration_regex)

            if item.origin == 'Oslo, Norway':
                self.assertEqual(item.destination, 'Zagreb, Croatia')
                self.assertAlmostEqual(Decimal(2063), item.distance.kilometers, delta=self.delta_km)
                self.assertAlmostEqual(Decimal(2063000), item.distance.meters, delta=self.delta_m)
                self.assertAlmostEqual(Decimal(1281.8863), item.distance.miles, delta=self.delta_miles)
                self.assertRegexpMatches(str(item.duration), self.duration_regex)

    def test_distance_matrix_bicycling(self):
        origins = ['rybnik']
        destinations = ['oslo']

        item = self.google_maps.distance(origins, destinations, DistanceMatrixApiClient.MODE_BICYCLING).first()

        self.assertEqual(item.origin, 'Rybnik, Poland')
        self.assertEqual(item.destination, 'Oslo, Norway')
        self.assertAlmostEqual(Decimal(1596), item.distance.kilometers, delta=self.delta_km)
        self.assertAlmostEqual(Decimal(1596000), item.distance.meters, delta=self.delta_m)
        self.assertAlmostEqual(Decimal(991.7065), item.distance.miles, delta=self.delta_miles)
        self.assertRegexpMatches(str(item.duration), self.duration_regex)

    def test_distance_matrix_walking(self):
        origins = ['rybnik']
        destinations = ['oslo']

        item = self.google_maps.distance(origins, destinations, DistanceMatrixApiClient.MODE_WALKING).first()

        self.assertEqual(item.origin, 'Rybnik, Poland')
        self.assertEqual(item.destination, 'Oslo, Norway')
        self.assertAlmostEqual(Decimal(1380), item.distance.kilometers, delta=self.delta_km)
        self.assertAlmostEqual(Decimal(1380000), item.distance.meters, delta=self.delta_m)
        self.assertAlmostEqual(Decimal(857.4906), item.distance.miles, delta=self.delta_miles)
        self.assertRegexpMatches(str(item.duration), self.duration_regex)

    def test_distance_matrix_avoid_tolls(self):
        origins = ['rybnik']
        destinations = ['oslo']

        item = self.google_maps.distance(origins, destinations, avoid=DistanceMatrixApiClient.AVOID_TOLLS).first()

        self.assertEqual(item.origin, 'Rybnik, Poland')
        self.assertEqual(item.destination, 'Oslo, Norway')
        self.assertAlmostEqual(Decimal(1542), item.distance.kilometers, delta=self.delta_km)
        self.assertAlmostEqual(Decimal(1542000), item.distance.meters, delta=self.delta_m)
        self.assertAlmostEqual(Decimal(958.1525), item.distance.miles, delta=self.delta_miles)
        self.assertRegexpMatches(str(item.duration), self.duration_regex)

    def test_distance_matrix_avoid_highways(self):
        origins = ['rybnik']
        destinations = ['oslo']

        item = self.google_maps.distance(origins, destinations, avoid=DistanceMatrixApiClient.AVOID_HIGHWAYS).first()

        self.assertEqual(item.origin, 'Rybnik, Poland')
        self.assertEqual(item.destination, 'Oslo, Norway')
        self.assertAlmostEqual(Decimal(1542), item.distance.kilometers, delta=self.delta_km)
        self.assertAlmostEqual(Decimal(1542000), item.distance.meters, delta=self.delta_m)
        self.assertAlmostEqual(Decimal(958.1525), item.distance.miles, delta=self.delta_miles)
        self.assertRegexpMatches(str(item.duration), self.duration_regex)

    def test_distance_matrix_avoid_ferries(self):
        origins = ['rybnik']
        destinations = ['oslo']

        item = self.google_maps.distance(origins, destinations, avoid=DistanceMatrixApiClient.AVOID_FERRIES).first()

        self.assertEqual(item.origin, 'Rybnik, Poland')
        self.assertEqual(item.destination, 'Oslo, Norway')
        self.assertAlmostEqual(Decimal(1851), item.distance.kilometers, delta=self.delta_km)
        self.assertAlmostEqual(Decimal(1851000), item.distance.meters, delta=self.delta_m)
        self.assertAlmostEqual(Decimal(1150.1559), item.distance.miles, delta=self.delta_miles)
        self.assertRegexpMatches(str(item.duration), self.duration_regex)
df1.info()

from geolocation.main import GoogleMaps 
from geolocation.distance_matrix.client import DistanceMatrixApiClient
origins1 = ['CHICAGO,IL'] 
destination1_1 = ['AURORA,IL']
destination1_2 = ['JOLIET,IL']
origins2 = ['INDIANAPOLIS,IN']
destination2_1 = ['BLOOMINGTON,IN']
destination2_2 = ['CARMEL,IN']
origins3 = ['SAN ANTONIO,TX']
destination3_1 = ['DALLAS,TX']

google_maps = GoogleMaps(api_key='AIzaSyDYgz9eU5Ek9kmnNNz5mFzmumqVqmBpcM')

dis1 = google_maps.distance(origins1, destination1_1).all()
dis2 = google_maps.distance(destination1_1, destination1_2).all()
dis3 = google_maps.distance(origins2, destination2_1).all()
dis4 = google_maps.distance(destination2_1, destination2_2).all()
dis5 = google_maps.distance(origins3, destination3_1).all()
for item in dis1:
    d1 = (float(item.distance.miles) * 1.58)
for item in dis2:
    d1 += (float(item.distance.miles) * 0.99)
for item in dis3:
    d2 = (float(item.distance.miles) * 1.58)
for item in dis4:
    d2 += (float(item.distance.miles) * 1.32)
for item in dis5:
    d3 = (float(item.distance.miles) * 1.38)
    
Example #3
0
# -*- coding: utf-8 -*-
from geolocation.main import GoogleMaps
from geolocation.distance_matrix.client import DistanceMatrixApiClient

if __name__ == "__main__":
    origins = ['rybnik', 'oslo']
    destinations = ['zagrzeb']

    google_maps = GoogleMaps(api_key='your_google_maps_key')

    items = google_maps.distance(origins, destinations).all()  # default mode parameter is const.MODE_DRIVING

    for item in items:
        print('origin: %s' % item.origin)
        print('destination: %s' % item.destination)
        print('km: %s' % item.distance.kilometers)
        print('m: %s' % item.distance.meters)
        print('miles: %s' % item.distance.miles)
        print('duration: %s' % item.duration)  # it returns str
        print('duration datetime: %s' % item.duration.datetime)  # it returns datetime

        # you can also get items from duration
        print('duration days: %s' % item.duration.days)
        print('duration hours: %s' % item.duration.hours)
        print('duration minutes: %s' % item.duration.minutes)
        print('duration seconds: %s' % item.duration.seconds)

    items = google_maps.distance(origins, destinations, DistanceMatrixApiClient.MODE_BICYCLING).all()

    for item in items:
        print('origin: %s' % item.origin)
Example #4
0
from geolocation.main import GoogleMaps
from geolocation.distance_matrix.client import DistanceMatrixApiClient

origins = [
    'São Luis Maranhão', 'Porto Alegre Rio Grande do Sul',
    'Belo Horizonte Minas Gerais'
]
destinations = ['São Paulo']

google_maps = GoogleMaps(api_key='AIzaSyBCksh0B58c_C6k_Epm2k1ZQb-YF6kA6SE')

items = google_maps.distance(
    origins,
    destinations).all()  # default mode parameter is const.MODE_DRIVING

for item in items:
    print('Origin: %s' % item.origin)
    print('Destination: %s' % item.destination)
    print('Km: %s' % item.distance.kilometers)
    print('Duration: %s' % item.duration)