Skip to content

Commit

Permalink
Merge pull request #100 from Resgrid/develop
Browse files Browse the repository at this point in the history
#86696cxr4 Guarding from errors in geo providers
  • Loading branch information
ucswift authored Feb 11, 2023
2 parents b84edf5 + 1bedb05 commit 6f0f7f0
Showing 1 changed file with 100 additions and 71 deletions.
171 changes: 100 additions & 71 deletions Providers/Resgrid.Providers.Geo/GeoLocationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,15 @@ public async Task<string> GetAddressFromLatLong(double lat, double lon)
{
async Task<string> getAddressFromCords()
{
IGeocoder googleGeoCoder = new GoogleGeocoder()
{
ApiKey = Config.MappingConfig.GoogleMapsApiKey
};
IGeocoder bingGeoCoder = new BingMapsGeocoder(Config.MappingConfig.BingMapsApiKey);

string address = null;

try
{
IGeocoder googleGeoCoder = new GoogleGeocoder()
{
ApiKey = Config.MappingConfig.GoogleMapsApiKey
};

var addresses = await googleGeoCoder.ReverseGeocodeAsync(lat, lon);

if (addresses != null && addresses.Any())
Expand All @@ -71,6 +70,8 @@ async Task<string> getAddressFromCords()
{
try
{
IGeocoder bingGeoCoder = new BingMapsGeocoder(Config.MappingConfig.BingMapsApiKey);

var addresses = await bingGeoCoder.ReverseGeocodeAsync(lat, lon);

if (addresses != null && addresses.Count() > 0)
Expand All @@ -88,15 +89,15 @@ public async Task<string> GetLatLonFromAddress(string address)
{
async Task<string> getCordsFromAddress()
{
IGeocoder googleGeoCoder = new GoogleGeocoder()
{
ApiKey = Config.MappingConfig.GoogleMapsApiKey
};
IGeocoder bingGeoCoder = new BingMapsGeocoder(Config.MappingConfig.BingMapsApiKey);
string coordinates = null;

try
{
IGeocoder googleGeoCoder = new GoogleGeocoder()
{
ApiKey = Config.MappingConfig.GoogleMapsApiKey
};

var addresses = await googleGeoCoder.GeocodeAsync(address);

if (addresses != null && addresses.Any())
Expand Down Expand Up @@ -124,6 +125,8 @@ async Task<string> getCordsFromAddress()
{
try
{
IGeocoder bingGeoCoder = new BingMapsGeocoder(Config.MappingConfig.BingMapsApiKey);

var addresses = await bingGeoCoder.GeocodeAsync(address);

if (addresses != null && addresses.Count() > 0)
Expand All @@ -140,31 +143,37 @@ async Task<string> getCordsFromAddress()

public async Task<RouteInformation> GetRoute(double startLat, double startLon, double endLat, double endLon)
{

DirectionsRequest directionsRequest = new DirectionsRequest()
{
Origin = string.Format("{0},{1}", startLat, startLon),
Destination = string.Format("{0},{1}", endLat, endLon),
ApiKey = Config.MappingConfig.GoogleMapsApiKey
};

async Task<RouteInformation> getRoute()
{
DirectionsResponse directions = await GoogleMapsApi.GoogleMaps.Directions.QueryAsync(directionsRequest);

var info = new RouteInformation();

if (directions != null && directions.Status == DirectionsStatusCodes.OK)
try
{
var route = directions.Routes.FirstOrDefault();
DirectionsRequest directionsRequest = new DirectionsRequest()
{
Origin = string.Format("{0},{1}", startLat, startLon),
Destination = string.Format("{0},{1}", endLat, endLon),
ApiKey = Config.MappingConfig.GoogleMapsApiKey
};
DirectionsResponse directions = await GoogleMapsApi.GoogleMaps.Directions.QueryAsync(directionsRequest);

if (route != null)
if (directions != null && directions.Status == DirectionsStatusCodes.OK)
{
info.Name = route.Summary;
info.ProcessedOn = DateTime.UtcNow;
info.Successful = true;
var route = directions.Routes.FirstOrDefault();

if (route != null)
{
info.Name = route.Summary;
info.ProcessedOn = DateTime.UtcNow;
info.Successful = true;
}
}
}
catch
{
info.ProcessedOn = DateTime.UtcNow;
info.Successful = false;
}

return info;
};
Expand All @@ -177,38 +186,46 @@ public async Task<RouteInformation> GetRoute(string start, string end)
if (String.IsNullOrWhiteSpace(start) || String.IsNullOrWhiteSpace(end))
return new RouteInformation();

DirectionsRequest directionsRequest = new DirectionsRequest()
{
Origin = start,
Destination = end,
ApiKey = Config.MappingConfig.GoogleMapsApiKey
};

async Task<RouteInformation> getRoute()
{
DirectionsResponse directions = await GoogleMapsApi.GoogleMaps.Directions.QueryAsync(directionsRequest);

var info = new RouteInformation();

if (directions != null && directions.Status == DirectionsStatusCodes.OK)
try
{
var route = directions.Routes.FirstOrDefault();
DirectionsRequest directionsRequest = new DirectionsRequest()
{
Origin = start,
Destination = end,
ApiKey = Config.MappingConfig.GoogleMapsApiKey
};

DirectionsResponse directions = await GoogleMapsApi.GoogleMaps.Directions.QueryAsync(directionsRequest);

if (route != null)
if (directions != null && directions.Status == DirectionsStatusCodes.OK)
{
var leg = route.Legs.FirstOrDefault();
var route = directions.Routes.FirstOrDefault();

if (leg != null)
if (route != null)
{
info.Name = route.Summary;
info.ProcessedOn = DateTime.UtcNow;
info.Successful = true;
info.TimeString = leg.Duration.Text;
info.Seconds = leg.Duration.Value.TotalSeconds;
info.DistanceInMeters = leg.Distance.Value;
var leg = route.Legs.FirstOrDefault();

if (leg != null)
{
info.Name = route.Summary;
info.ProcessedOn = DateTime.UtcNow;
info.Successful = true;
info.TimeString = leg.Duration.Text;
info.Seconds = leg.Duration.Value.TotalSeconds;
info.DistanceInMeters = leg.Distance.Value;
}
}
}
}
catch
{
info.ProcessedOn = DateTime.UtcNow;
info.Successful = false;
}

return info;
};
Expand All @@ -220,19 +237,23 @@ public async Task<Coordinates> GetCoordinatesFromW3W(string words)
{
Func<Task<Coordinates>> getLocationFromW3W = async () =>
{
var client = new RestClient("https://api.what3words.com");
var request = new RestRequest($"/v2/forward?key={Config.MappingConfig.What3WordsApiKey}&lang=en&addr={words}", Method.Get);
try
{
var client = new RestClient("https://api.what3words.com");
var request = new RestRequest($"/v2/forward?key={Config.MappingConfig.What3WordsApiKey}&lang=en&addr={words}", Method.Get);
var response = await client.ExecuteAsync<W3WResponse>(request);
var response = await client.ExecuteAsync<W3WResponse>(request);
if (response.Data != null && response.Data.geometry != null)
{
var coords = new Coordinates();
coords.Latitude = response.Data.geometry.lat;
coords.Longitude = response.Data.geometry.lng;
if (response.Data != null && response.Data.geometry != null)
{
var coords = new Coordinates();
coords.Latitude = response.Data.geometry.lat;
coords.Longitude = response.Data.geometry.lng;
return coords;
return coords;
}
}
catch { /* Don't report on GeoLocation failures */ }
return null;
};
Expand All @@ -244,19 +265,23 @@ public async Task<Coordinates> GetCoordinatesFromW3WAsync(string words)
{
Func<Task<Coordinates>> getLocationFromW3W = async () =>
{
var client = new RestClient("https://api.what3words.com");
var request = new RestRequest($"/v2/forward?key={Config.MappingConfig.What3WordsApiKey}&lang=en&addr={words}", Method.Get);
try
{
var client = new RestClient("https://api.what3words.com");
var request = new RestRequest($"/v2/forward?key={Config.MappingConfig.What3WordsApiKey}&lang=en&addr={words}", Method.Get);
var response = await client.ExecuteAsync<W3WResponse>(request);
var response = await client.ExecuteAsync<W3WResponse>(request);
if (response.Data != null && response.Data.geometry != null)
{
var coords = new Coordinates();
coords.Latitude = response.Data.geometry.lat;
coords.Longitude = response.Data.geometry.lng;
if (response.Data != null && response.Data.geometry != null)
{
var coords = new Coordinates();
coords.Latitude = response.Data.geometry.lat;
coords.Longitude = response.Data.geometry.lng;
return coords;
return coords;
}
}
catch { /* Don't report on GeoLocation failures */ }
return null;
};
Expand All @@ -268,15 +293,19 @@ public async Task<string> GetW3WFromCoordinates(Coordinates coordinates)
{
Func<Task<string>> getLocationFromW3W = async () =>
{
var client = new RestClient("https://api.what3words.com");
var request = new RestRequest($"/v2/reverse?key={Config.MappingConfig.What3WordsApiKey}&coords={$"{coordinates.Latitude},{coordinates.Longitude}"}", Method.Get);
try
{
var client = new RestClient("https://api.what3words.com");
var request = new RestRequest($"/v2/reverse?key={Config.MappingConfig.What3WordsApiKey}&coords={$"{coordinates.Latitude},{coordinates.Longitude}"}", Method.Get);
var response = await client.ExecuteAsync<ReverseW3WResponse>(request);
var response = await client.ExecuteAsync<ReverseW3WResponse>(request);
if (response.Data != null && !String.IsNullOrWhiteSpace(response.Data.words))
{
return response.Data.words;
if (response.Data != null && !String.IsNullOrWhiteSpace(response.Data.words))
{
return response.Data.words;
}
}
catch { /* Don't report on GeoLocation failures */ }
return null;
};
Expand Down

0 comments on commit 6f0f7f0

Please sign in to comment.