-2

I am working with a CSV file with well over 100,000 lines of data. The data depicts various points throughout the world via a lot long. I would like to have a list returned back to me that shows the closest point from a given known point. A example line of data is here

956985,Bob,large_building,California,41.45339929,-119.6739981,4398,NA,US,US-CA,Alturas,no,A24,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

The data above 41.45339929 is the latitude and the -119.6739981 is the longitude. I would want the filteredpoints to return a list that will be in order of closest lat and longs

For example

private List<string> points = new List<string>();
private List<string> filteredpoints = new List<string>();//list of closest lat long matches

        private double MyLatitude = 39.23836;
        private double MyLongitude = -119.5550003;

            



        private void Window_Initialized(object sender, EventArgs e)
        {
            var strLines = File.ReadLines(@"C:\ProgramData\Inksnape\Data\file.csv");
            foreach (var line in strLines)
            {
                points.Add(line);
                
            }
        } ```

I am stumped on how I can proceed with this issue
 



nabpp
  • 9
  • 4
  • You need to do some research on the subject of calculating distances between geographic points for yourself first. Unless you want actually do the maths calculations yourself, which I doubt, then you're going to need a library that provides that functionality. SO is not the place to get recommendations for such a library. You need to choose the library, learn how to use it and then use it. If what you do doesn't work, then you have a question to ask here. – jmcilhinney Oct 20 '22 at 07:55
  • Thanks, I am aware I am not going to actually get the distance for this, instead I just want to get a list of objects from the csv that is in descending order from "MyLatitude" and "MyLongitude" – nabpp Oct 20 '22 at 07:58
  • If you want to get the closest then you do need to calculate the distance. The closest is, by definition, the one with the lowest distance. – jmcilhinney Oct 20 '22 at 08:01
  • or the closest number to myLatitude and myLongitude. Again I don't care about the distance. I have a separate library I am using to calculate distance but I want to do that operation with a filtered list. not the raw list with over 100,000 objects – nabpp Oct 20 '22 at 08:05
  • 1
    If you just want to do basic arithmetic then go ahead, although it won't give you accurate results unless the distances are very small. You don't need us to tell you how to do that. Work out the logic first, then try to implement that logic in code. The fact that you're basically showing that you're trying to write code without knowing what that code has to do is part of the problem. How would you do it with pen and paper? That doesn't take any programming experience. Once you have that logic, try to write code to implement that logic. If you encounter an issue at that stage, as us then. – jmcilhinney Oct 20 '22 at 08:19

1 Answers1

0

Here's how you'd approach this:

  1. Parse your CSV into a C# data structure (i.e., a list of a custom class containing the fields of your CSV file with correct data types). That way, your problem "how to solve X with CSV" becomes just "how to solve X".

  2. For each entry in your list, calculate the distance to your "given known point". Store that calculated distance in an extra field of your class.

  3. Sort your list by distance.

Heinzi
  • 167,459
  • 57
  • 363
  • 519