Problems creating json string

My brain is not working, and I'm trying to do something more complex than I think it really should be, and I need another set of eyes. I have the following text in a text file

|TS|170702/2300|170703/0503|42.80 -102.64 39.76 -102.64 39.44 -99.37 42.48 -99.37
|TS|170703/0505|170703/0905|40.22 -97.30 38.63 -97.30 38.19 -101.03 39.78 -101.03

what does this mean ... (| watchtype | watchstart | watchend | lat / long pairs)

The problem I am facing is that I need to take an EACH ROW (maybe 0 or maybe 100 +) and create a polygon on the map to mark the location of this storm clock. I currently have the following.

MODEL

public class WatchPolygons
{
    public string WatchType { get; set; }
    public string WatchStart { get; set; }
    public string WatchEnd { get; set; }
    public List<lat_longPairs> Lat_Long_Pairs {get; set;}
}

public class lat_longPairs
{
    public decimal latitude { get; set; }
    public decimal longitude { get; set; }
}

CONTROLLER

public JsonResult GetWatchPath()
{
    var watchFilePaths = ConfigurationManager.AppSettings["watchFilePath"];
    return Json(Directory.GetFiles(Server.MapPath(watchFilePaths), "current*.txt"), JsonRequestBehavior.AllowGet);
}

[HttpGet]
public ActionResult GetWatchData(string watchPath)
{
    var stringData = new List<string>();
    using (var reader = new StreamReader(watchPath))
    {
        while (!reader.EndOfStream)
        {
            var data = reader.ReadLine().Trim();

            if (!string.IsNullOrEmpty(data))
                stringData.Add(data);
        }

    }

    return Json((from item in stringData
                 select item.Split(new char [] { '|' }, StringSplitOptions.RemoveEmptyEntries)
                into rawData
                 select new WatchPolygons
                 {
                     WatchType = rawData[0],
                     WatchStart = rawData[1],
                     WatchEnd = rawData[2]
                 }).ToList(), JsonRequestBehavior.AllowGet);
}

I know what I am missing latlong pairs = rawData. I don’t have it in the code, because in the model it is a list, and I cannot easily convert the list to a string as I need it.

? , , , lat/long. .

+4
2

rawData[3], , lat/lon. , , :

private static List<lat_longPairs> ParseLatLon(string input)
{
    var numbers = input.Split(new [] { " " }, StringSplitOptions.RemoveEmptyEntries)
                       .Select(i => decimal.Parse(i))
                       .ToArray();

    var latLonPairs = new List<lat_longPairs>();

    for (int i = 0; i < numbers.Length; i += 2)
    {
        latLonPairs.Add(new lat_longPairs
        {
            latitude = numbers[i],
            longitude = numbers[i + 1],
        });
    }

    return latLonPairs;
}

, :

select new WatchPolygons
{
    WatchType = rawData[0],
    WatchStart = rawData[1],
    WatchEnd = rawData[2],
    Lat_Long_Pairs = ParseLatLon(rawData[3])
}

, .

+4

GroupBy, .

        var str = "|TS|170702/2300|170703/0503|42.80 -102.64 39.76 -102.64 39.44 -99.37 42.48 -99.37";  
        int itemsInGroup = 2;
        var pairs = str.Split('|')[4].Split(' ').
                    // Give each set of coordinate a group number.
                    Select((n, i) => new { GroupNumber = i / itemsInGroup, Number = n }).
                    GroupBy(n => n.GroupNumber).
                    Select(g => 
                    {
                        var coordinate = g.Select(n => n.Number).ToList();
                        return new lat_longPairs
                        {
                            latitude = decimal.Parse(coordinate[0], NumberFormatInfo.InvariantInfo),
                            longitude = decimal.Parse(coordinate[1], NumberFormatInfo.InvariantInfo),
                        };
        });
0

Source: https://habr.com/ru/post/1680666/


All Articles