/// <summary> /// Returns the closest full postcode to a set of coordinates. /// </summary> /// <param name="lat"></param> /// <param name="lon"></param> /// <returns></returns> public async Task <LocationLatLng> GetClosestPostcodeToCoordinate(double lat, double lon) { if (_lookupData == null || !_lookupData.Any()) { throw new InvalidOperationException("Must initialize the data first"); } LocationLatLng closest = null; double closestTotalDifference = 1000000000; foreach (var row in _lookupData) { var latDifference = Math.Abs(lat - row.Latitude); var lonDifference = Math.Abs(lon - row.Longitude); var totalDifference = latDifference + lonDifference; if (totalDifference < closestTotalDifference) { closestTotalDifference = totalDifference; closest = row; if (closestTotalDifference == 0) { break; // We can't get any closer, so stop checking. } } } return(closest); }
public async Task ConvertOSEastingsNorthingsDataToLatLongUK() { var filesToProcess = Directory.GetFiles(ukRawDataFolder); Stopwatch st = new Stopwatch(); st.Start(); foreach (var file in filesToProcess) { try { var contentRows = _csvParser.ReadCsvFromFile(file); List <LocationLatLng> latLongRows = new List <LocationLatLng>(); foreach (var row in contentRows) { if (String.IsNullOrWhiteSpace(row)) { continue; } var split = row.Split(','); var postcode = split[0]; Console.WriteLine(st.ElapsedMilliseconds + ":" + postcode); var easting = double.Parse(split[2]); // No Try here. We want it to bomb out if there's an error. var northing = double.Parse(split[3]); // A postcode covers multiple houses. // As it covers a fairly wide area, we don't need to go ultra-fine in the conversion. LatitudeLongitude latLong = _geoUKHelper.ConvertEastNorthToLatLong_LowerAccuracyFast(easting, northing); var gmaps = string.Format("https://www.google.co.uk/maps/@{0},{1},19z", Math.Round(latLong.Latitude, 6), Math.Round(latLong.Longitude, 6)); // We don't need more than 6 decimal places (11.1cm accuracy) // https://gis.stackexchange.com/questions/8650/measuring-accuracy-of-latitude-and-longitude?newreg=5f6bf4ba81534a7ea34606ceceb00b35 var latRounded = Math.Round(latLong.Latitude, 6); var longRounded = Math.Round(latLong.Longitude, 6); LocationLatLng postcodeZipLatLong = new LocationLatLng(postcode, latRounded, longRounded); latLongRows.Add(postcodeZipLatLong); } FileInfo fi = new FileInfo(file); var newFilePath = Path.Combine(ukLatLongDataFolder, fi.Name); await _postcodeZipLatLongMethods.SavePostcodeZipLatLongToFile(newFilePath, latLongRows); // Clear the data for quicker garbage disposal. latLongRows.Clear(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); Console.ReadLine(); } } }
public async Task <LocationLatLng> CondensePostcodes(string shortPostcode, List <LocationLatLng> postcodes) { LocationLatLng returnValue = new LocationLatLng(); returnValue.Identifier = shortPostcode; returnValue.Latitude = Math.Round(postcodes.Average(_ => _.Latitude), 6); returnValue.Longitude = Math.Round(postcodes.Average(_ => _.Longitude), 6); return(returnValue); }
public async void GetVendorsCustomerCanVisit_FromHavantWith15kRadius_Expect_PortsmouthOnly() { // Arrange // NOTE: No radius here, because the vendors are coming to the customer (vendors set their own radius) LocationLatLng visitingCustomer = new LocationLatLng("Havant Customer", 50.866133, -1.012903, 15000); // Act var vendorsToVisit = await _cut.GetVendorsCustomerCanVisit(visitingCustomer); var vendorsNames = vendorsToVisit.Select(_ => _.Identifier).ToList(); Assert.Equal(1, vendorsNames.Count); Assert.Contains("Portsmouth Vendor", vendorsNames); }
public async void GetVendorsWhoCanVisitCustomer_FromHavant_Expect_PortsmouthEastSotonDave() { // Arrange // NOTE: No radius here, because the vendors are coming to the customer (vendors set their own radius) LocationLatLng customerToVisit = new LocationLatLng("Havant Customer", 50.866133, -1.012903); // Act var vendors = await _cut.GetVendorsWhoCanVisitCustomer(customerToVisit); var vendorsNames = vendors.Select(_ => _.Identifier).ToList(); Assert.Equal(2, vendorsNames.Count); Assert.Contains("Portsmouth Vendor", vendorsNames); Assert.Contains("East Soton Vendor", vendorsNames); }
public async Task GetGlobalVendorsForCustomer() { // NOTE: No radius here, because the vendors are coming to PETE (vendors set their own radius) LocationLatLng customerToVisit = new LocationLatLng("Hungry Pete (@B&Q Havant)", 50.866133, -1.012903); var vendors = await _globalVendorRadiusCheck.GetVendorsWhoCanVisitCustomer(customerToVisit); var vendorsNames = vendors.Select(_ => _.Identifier).ToList(); var vendorNamesString = String.Join(",", vendorsNames); Console.WriteLine($"Vendors who can visit customer: {vendorNamesString}"); // Pete wants to find vendors in an 'x' meter radius (he goes to them, so he sets the distance) LocationLatLng visitingCustomer = new LocationLatLng("Hungry Pete (@B&Q Havant)", 50.866133, -1.012903, 15000); var vendorsToVisit = await _globalVendorRadiusCheck.GetVendorsCustomerCanVisit(visitingCustomer); var vendorsNamesV = vendorsToVisit.Select(_ => _.Identifier).ToList(); var vendorNamesVString = String.Join(",", vendorsNamesV); Console.WriteLine($"Vendors customer can visit: {vendorNamesVString}"); }