Beispiel #1
0
        public void Then_Uri_Is_As_Expected_For_A_Single_Point_With_Standard_Encoding_And_No_Slash_In_Base_Uri()
        {
            var origin = new Location
            {
                Latitude  = 52.400997M,
                Longitude = -1.295653M
            };

            var destinations = new List <Location>
            {
                new Location
                {
                    Latitude  = 51.742141M,
                    Longitude = -1.508122M
                }
            };

            var apiClient = new GoogleDistanceMatrixApiClient(new HttpClient(),
                                                              new ApiConfiguration
            {
                //Note: no slash at end
                GoogleMapsApiBaseUrl = "https://maps.googleapis.com/maps/api",
                GoogleMapsApiKey     = "YOUR_API_KEY"
            });

            var result = apiClient.BuildUri(origin, destinations, "driving", false);

            result.Should().Be("https://maps.googleapis.com/maps/api/distancematrix/json?origins=52.400997,-1.295653&mode=driving&destinations=51.742141%2C-1.508122&key=YOUR_API_KEY");

            //https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=40.6655101,-73.89188969999998&destinations=40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.659569%2C-73.933783%7C40.729029%2C-73.851524%7C40.6860072%2C-73.6334271%7C40.598566%2C-73.7527626%7C40.659569%2C-73.933783%7C40.729029%2C-73.851524%7C40.6860072%2C-73.6334271%7C40.598566%2C-73.7527626&key=YOUR_API_KEY
            //vs polyline:
            //https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=40.6655101,-73.89188969999998&destinations=enc:_kjwFjtsbMt%60EgnKcqLcaOzkGari%40naPxhVg%7CJjjb%40cqLcaOzkGari%40naPxhV:&key=YOUR_API_KEY
        }
Beispiel #2
0
        public void Then_Uri_Is_As_Expected_For_A_Single_Point_With_Standard_Encoding_And_Slash_In_Base_Uri()
        {
            var origin = new Location
            {
                Latitude  = 52.400997M,
                Longitude = -1.295653M
            };

            var destinations = new List <Location>
            {
                new Location
                {
                    Latitude  = 51.742141M,
                    Longitude = -1.508122M
                }
            };

            var apiClient = new GoogleDistanceMatrixApiClient(new HttpClient(),
                                                              new ApiConfiguration
            {
                //Note: slash at end
                GoogleMapsApiBaseUrl = "https://maps.googleapis.com/maps/api/",
                GoogleMapsApiKey     = "YOUR_API_KEY"
            });

            var result = apiClient.BuildUri(origin, destinations, "driving", false);

            result.Should().Be("https://maps.googleapis.com/maps/api/distancematrix/json?origins=52.400997,-1.295653&mode=driving&destinations=51.742141%2C-1.508122&key=YOUR_API_KEY");
        }
Beispiel #3
0
        public void Then_Encoded_Polyline_Is_As_Expected_For_Multiple_Points()
        {
            var points = new List <Location>
            {
                new Location {
                    Latitude = 38.5M, Longitude = -120.2M
                },
                new Location {
                    Latitude = 40.7M, Longitude = -120.95M
                },
                new Location {
                    Latitude = 43.252M, Longitude = -126.453M
                }
            };

            var apiClient = new GoogleDistanceMatrixApiClient(new HttpClient(),
                                                              new ApiConfiguration
            {
                GoogleMapsApiBaseUrl = "https://maps.googleapis.com/maps/api/"
            });

            var result = apiClient.EncodePolyline(points);

            result.Should().Be("_p~iF~ps|U_ulLnnqC_mqNvxq`@");
        }
Beispiel #4
0
        public static async Task GetGoogleResult(string path)
        {
            var venues = await GetVenuesFromCsv(path);

            Console.WriteLine($"Have {venues.Count} venues");
            //foreach (var venue in venues)
            //{
            //    Console.WriteLine($"Venue: {venue.Postcode}, {venue.Latitude}, {venue.Longitude}");
            //}

            var client = new GoogleDistanceMatrixApiClient(new HttpClient(), Configuration);

            var startPoint = new Location
            {
                Postcode  = "CV1 2WT",
                Latitude  = 52.400997M,
                Longitude = -1.508122M
            };

            var stopwatch = Stopwatch.StartNew();

            var searchResults = await client.Search(startPoint, venues);

            stopwatch.Stop();

            var reachableResults = searchResults.Where(r => r.TravelTime <= 60 * 60).ToList();

            Console.WriteLine();
            Console.WriteLine($"Retrieved {searchResults.Count} search results from {venues.Count} venues ({reachableResults.Count} reachable) in {stopwatch.ElapsedMilliseconds:#,###}ms");

            //Console.WriteLine("Results that could not be determined:");
            //foreach (var searchResult in searchResults.Where(x => x.Distance < 0 || x.TravelTime < 0))
            //{
            //    Console.WriteLine($"{searchResult.Address} at {searchResult.Distance}{searchResult.DistanceUnits}, travel time {searchResult.TravelTimeString}");
            //}

            //foreach (var searchResult in searchResults.OrderByDescending(x => x.TravelTime))
            //{
            //    var distanceInKm = searchResult.Distance / 1000;
            //    var distanceInMiles = searchResult.Distance / 1609.34;
            //    Console.WriteLine($"{searchResult.Address} at {distanceInMiles:#.0}mi ({distanceInKm:#.0}km), travel time {searchResult.TravelTimeString}");
            //}
        }
Beispiel #5
0
        public void Then_Encoded_Polyline_Is_As_Expected_For_A_Single_Point()
        {
            var points = new List <Location>
            {
                new Location {
                    Latitude = 52.400997M, Longitude = -1.508122M
                }
            };

            var apiClient = new GoogleDistanceMatrixApiClient(new HttpClient(),
                                                              new ApiConfiguration
            {
                GoogleMapsApiBaseUrl = "https://maps.googleapis.com/maps/api/"
            });

            var result = apiClient.EncodePolyline(points);

            result.Should().Be("gqy~HvpeH");
        }
Beispiel #6
0
        private static async Task GetDistancesForOpportunitySpreadsheet(string path, string travelMode)
        {
            var journeys = await GetOpportunityJourneysFromCsv(path);

            //Hack the postcodes out
            foreach (var journey in journeys)
            {
                var workplaces = journey.Workplace.Split();
                var n          = workplaces.Length;
                journey.Workplace     = ExtractPostcode(journey.Workplace);
                journey.ProviderVenue = ExtractPostcode(journey.ProviderVenue);
            }

            //var regex = new Regex(
            //    @"([^ ]+\s+[^ ]+$)");
            //var postcode = regex.Match(journey.Workplace).Groups[0].Captures[0];
            //var destination = regex.Match(journey.ProviderVenue).Groups[0].Captures[0];

            var client = new GoogleDistanceMatrixApiClient(new HttpClient(), Configuration);

            var resultsList  = new List <Journey>();
            var destinations = new List <Journey>();
            //var travelMode = GoogleTravelMode.Driving;
            //var travelMode = GoogleTravelMode.Transit;

            //Uncomment to filter
            //journeys = journeys
            //    .Where(j => j.Workplace == "WA14 1PH" &&
            //                (j.ProviderVenue == "SK11 8LF" ||
            //                 j.ProviderVenue == "SK17 6RY"))
            //    .ToList();

            var currentWorkplace = journeys.FirstOrDefault()?.Workplace;
            var last             = journeys.Last();

            foreach (var journey in journeys)
            {
                if (journey.Workplace == currentWorkplace)
                {
                    destinations.Add(journey);
                }

                if (journey.Workplace != currentWorkplace || journey == last)
                {
                    Console.WriteLine($"Workplace {currentWorkplace}");

                    var searchResults = await client.SearchJourney(currentWorkplace, destinations, travelMode);

                    foreach (var item in searchResults)
                    {
                        Console.WriteLine($"    driving time to {item.Address} {item.TravelTimeString}");
                        resultsList.Add(new Journey
                        {
                            Workplace     = currentWorkplace,
                            ProviderVenue = item.Address,
                            TravelTime    = item.TravelTimeString
                        });
                    }

                    currentWorkplace = journey.Workplace;
                    destinations.Clear();
                    destinations.Add(journey);
                }
            }

            Console.WriteLine($"Processed {journeys.Count} journeys, have {resultsList.Count} results");
            var outputPath = $".\\Data\\journey_results_{travelMode}.csv";

            using (var writer = File.CreateText($".\\Data\\journey_results_{travelMode}.csv"))
            {
                await writer.WriteLineAsync("Workplace,Provider Venue,Travel Time");

                await writer.WriteLineAsync("");

                foreach (var result in resultsList)
                {
                    await writer.WriteLineAsync($"{result.Workplace.Replace(",", "")},{result.ProviderVenue.Replace(",", "")},{result.TravelTime}");
                }

                Console.WriteLine($"Saved results to {outputPath}");
            }
        }
Beispiel #7
0
        public void Then_Uri_Is_As_Expected_For_Multiple_Points_With_Polyline_Encoding()
        {
            var origin = new Location
            {
                Latitude  = 40.6655101M,
                Longitude = -73.89188969999998M
            };

            var destinations = new List <Location>
            {
                new Location {
                    Latitude = 40.6905615M, Longitude = -73.9976592M
                },
                new Location {
                    Latitude = 40.6905615M, Longitude = -73.9976592M
                },
                new Location {
                    Latitude = 40.6905615M, Longitude = -73.9976592M
                },
                new Location {
                    Latitude = 40.6905615M, Longitude = -73.9976592M
                },
                new Location {
                    Latitude = 40.6905615M, Longitude = -73.9976592M
                },
                new Location {
                    Latitude = 40.6905615M, Longitude = -73.9976592M
                },
                new Location {
                    Latitude = 40.659569M, Longitude = -73.933783M
                },
                new Location {
                    Latitude = 40.729029M, Longitude = -73.851524M
                },
                new Location {
                    Latitude = 40.6860072M, Longitude = -73.6334271M
                },
                new Location {
                    Latitude = 40.598566M, Longitude = -73.7527626M
                },
                new Location {
                    Latitude = 40.659569M, Longitude = -73.933783M
                },
                new Location {
                    Latitude = 40.729029M, Longitude = -73.851524M
                },
                new Location {
                    Latitude = 40.6860072M, Longitude = -73.6334271M
                },
                new Location {
                    Latitude = 40.598566M, Longitude = -73.7527626M
                }
            };

            var apiClient = new GoogleDistanceMatrixApiClient(new HttpClient(),
                                                              new ApiConfiguration
            {
                GoogleMapsApiBaseUrl = "https://maps.googleapis.com/maps/api/",
                GoogleMapsApiKey     = "YOUR_API_KEY"
            });

            var result = apiClient.BuildUri(origin, destinations, "driving", true);

            result.Should().Be("https://maps.googleapis.com/maps/api/distancematrix/json?origins=40.6655101,-73.89188969999998&mode=driving&destinations=enc:_kjwFjtsbMt%60EgnKcqLcaOzkGari%40naPxhVg%7CJjjb%40cqLcaOzkGari%40naPxhV:&key=YOUR_API_KEY");
            //https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=40.6655101,-73.89188969999998&destinations=40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.659569%2C-73.933783%7C40.729029%2C-73.851524%7C40.6860072%2C-73.6334271%7C40.598566%2C-73.7527626%7C40.659569%2C-73.933783%7C40.729029%2C-73.851524%7C40.6860072%2C-73.6334271%7C40.598566%2C-73.7527626&key=YOUR_API_KEY
            //vs polyline:
            //https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=40.6655101,-73.89188969999998&destinations=enc:_kjwFjtsbMt%60EgnKcqLcaOzkGari%40naPxhVg%7CJjjb%40cqLcaOzkGari%40naPxhV:&key=YOUR_API_KEY
        }