예제 #1
0
        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 ConvertSpecificPostcodesToGeneralised()
        {
            var slnPath             = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location).Replace(@"Demo\bin\Debug\netcoreapp3.1", "");
            var processedDataFolder = Path.Combine(slnPath, @"CoreLookup\Data\UK\CodePointOpen2020\Data_Processed_LatLong\Merged");
            var startingFile        = Path.Combine(processedDataFolder, "uk_postcodes_lat_long_full.csv");
            var finalFile           = Path.Combine(processedDataFolder, "uk_postcodes_lat_long_area_district_only.csv");

            // 1. Read in the merged data
            List <LocationLatLng> fullPostcodesList = await _postcodeZipLatLongMethods.GetPostcodeZipLatLongFromFile(startingFile);

            // 2. Loop throug the data
            string currentEvalPostcode = "";
            List <LocationLatLng> currentEvalPostcodeLookup = new List <LocationLatLng>();
            List <LocationLatLng> finalShortPostcodes       = new List <LocationLatLng>();

            for (int i = 0; i < fullPostcodesList.Count; i++)
            {
                var pCode         = fullPostcodesList[i];
                var postcodeStart = "";
                if (pCode.Identifier.Contains(" "))
                {
                    // e.g. PO4 8ZW
                    postcodeStart = pCode.Identifier.Split(' ')[0];
                }
                else
                {
                    // e.g. PO142PY
                    postcodeStart = pCode.Identifier.Substring(0, 4);
                }

                if ((String.IsNullOrWhiteSpace(currentEvalPostcode) || postcodeStart == currentEvalPostcode) &&
                    i != fullPostcodesList.Count - 1)
                {
                    currentEvalPostcode = postcodeStart;
                    currentEvalPostcodeLookup.Add(pCode);
                }
                else
                {
                    // The list has moved on. So we need to process the last batch of data and start afresh.
                    var previousSetCondensed = await CondensePostcodes(currentEvalPostcode, currentEvalPostcodeLookup);

                    finalShortPostcodes.Add(previousSetCondensed);
                    Console.WriteLine($"Processed Postcodes: {currentEvalPostcode}");

                    // Set up the new data
                    currentEvalPostcodeLookup = new List <LocationLatLng>();
                    currentEvalPostcode       = postcodeStart;
                    currentEvalPostcodeLookup.Add(pCode);                     // This is the next postcode along. Make sure to include it :)
                }
            }


            await _postcodeZipLatLongMethods.SavePostcodeZipLatLongToFile(finalFile, finalShortPostcodes);
        }