示例#1
0
        public static void WritePenguinsData(StreamWriter writer, PenguinSite site, List <List <PenguinCount> > counts, string penguinName)
        {
            writer.Write(site.SiteName);
            writer.Write(";");
            writer.Write(site.Latitude);
            writer.Write(";");
            writer.Write(site.Longitude);
            writer.Write(";");
            writer.Write(penguinName);
            writer.Write(";");

            bool first = true;

            foreach (var listCount in counts)
            {
                if (!first)
                {
                    writer.Write(";;;;");
                }

                first = false;

                foreach (var count in listCount)
                {
                    writer.Write(count.Year);
                    writer.Write(";");
                }

                writer.WriteLine();

                writer.Write(";;;;");

                foreach (var count in listCount)
                {
                    writer.Write(count.Count);
                    writer.Write(";");
                }

                writer.WriteLine();

                writer.Write(";;;;");

                foreach (var count in listCount)
                {
                    writer.Write(count.Opacity);
                    writer.Write(";");
                }

                writer.WriteLine();
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            List <string> siteName  = new List <string>();
            List <string> longitude = new List <string>();
            List <string> latitude  = new List <string>();
            List <string> penguin   = new List <string>();
            List <string> month     = new List <string>();
            List <string> year      = new List <string>();
            List <string> count     = new List <string>();

            using (var reader = new StreamReader(@"D:\test.csv"))
            {
                while (!reader.EndOfStream)
                {
                    var line   = reader.ReadLine();
                    var values = line.Split(';');

                    if (values[0].Equals("site_name"))
                    {
                        continue;
                    }

                    siteName.Add(values[0]);
                    longitude.Add(values[3]);
                    latitude.Add(values[4]);
                    penguin.Add(values[5]);
                    month.Add(values[7]);
                    year.Add(values[9]);
                    count.Add(values[10]);
                }
            }

            List <PenguinSite> sites = new List <PenguinSite>();

            string lastName = "";

            for (int i = 0; i < siteName.Count; i++)
            {
                if (!lastName.Equals(siteName.ElementAt(i)))
                {
                    PenguinSite penguinSite = new PenguinSite
                    {
                        SiteName  = siteName.ElementAt(i),
                        Latitude  = double.Parse(latitude.ElementAt(i), CultureInfo.InvariantCulture.NumberFormat),
                        Longitude = double.Parse(longitude.ElementAt(i), CultureInfo.InvariantCulture.NumberFormat)
                    };

                    penguinSite.Gentoo.Add(new List <PenguinCount>());
                    penguinSite.Adelie.Add(new List <PenguinCount>());
                    penguinSite.Chinstrap.Add(new List <PenguinCount>());
                    penguinSite.Emperor.Add(new List <PenguinCount>());

                    lastName = siteName.ElementAt(i);

                    sites.Add(penguinSite);
                }

                try
                {
                    PenguinCount penguinCount = new PenguinCount
                    {
                        Name = ConvertPenguinName(penguin.ElementAt(i)),
                        //month = Int32.Parse(month.ElementAt(i)),
                        Interpolated = false,
                        Count        = Int32.Parse(count.ElementAt(i)),
                        Year         = Int32.Parse(year.ElementAt(i)),
                        Opacity      = 0
                    };

                    switch (penguinCount.Name)
                    {
                    case PenguinName.Adelie:
                        sites.Last().Adelie.ElementAt(0).Add(penguinCount);
                        break;

                    case PenguinName.Chinstrap:
                        sites.Last().Chinstrap.ElementAt(0).Add(penguinCount);
                        break;

                    case PenguinName.Emperor:
                        sites.Last().Emperor.ElementAt(0).Add(penguinCount);
                        break;

                    case PenguinName.Gentoo:
                        sites.Last().Gentoo.ElementAt(0).Add(penguinCount);
                        break;
                    }
                }
                catch (Exception)
                {
                }
            }


            using (var writer = new StreamWriter(@"D:\result.csv"))
            {
                const int minPenguins = 1;

                foreach (var penguinSite in sites)
                {
                    OptimizePenguinsCount(penguinSite.Gentoo);
                    OptimizePenguinsCount(penguinSite.Adelie);
                    OptimizePenguinsCount(penguinSite.Chinstrap);
                    OptimizePenguinsCount(penguinSite.Emperor);

                    if (penguinSite.Gentoo.Count >= minPenguins)
                    {
                        WritePenguinsData(writer, penguinSite, penguinSite.Gentoo, "Gentoo");
                    }

                    if (penguinSite.Adelie.Count >= minPenguins)
                    {
                        WritePenguinsData(writer, penguinSite, penguinSite.Adelie, "Adelie");
                    }

                    if (penguinSite.Chinstrap.Count >= minPenguins)
                    {
                        WritePenguinsData(writer, penguinSite, penguinSite.Chinstrap, "Chinstrap");
                    }

                    if (penguinSite.Emperor.Count >= minPenguins)
                    {
                        WritePenguinsData(writer, penguinSite, penguinSite.Emperor, "Emperor");
                    }
                }
            }

            List <JsonData> _data = new List <JsonData>();

            foreach (var site in sites)
            {
                if (site.Gentoo.Count >= 1)
                {
                    List <int> data         = new List <int>();
                    List <int> transparency = new List <int>();

                    foreach (var penCount in site.Gentoo.ElementAt(0))
                    {
                        data.Add(penCount.Count);
                        transparency.Add(penCount.Opacity);
                    }

                    _data.Add(new JsonData()
                    {
                        SiteName     = site.SiteName,
                        Penguin      = "Gentoo",
                        Latitude     = site.Latitude,
                        Longitude    = site.Longitude,
                        Data         = data,
                        Transparency = transparency
                    });
                }

                if (site.Adelie.Count >= 1)
                {
                    List <int> data         = new List <int>();
                    List <int> transparency = new List <int>();

                    foreach (var penCount in site.Adelie.ElementAt(0))
                    {
                        data.Add(penCount.Count);
                        transparency.Add(penCount.Opacity);
                    }

                    _data.Add(new JsonData()
                    {
                        SiteName     = site.SiteName,
                        Penguin      = "Adélie",
                        Latitude     = site.Latitude,
                        Longitude    = site.Longitude,
                        Data         = data,
                        Transparency = transparency
                    });
                }

                if (site.Chinstrap.Count >= 1)
                {
                    List <int> data         = new List <int>();
                    List <int> transparency = new List <int>();

                    foreach (var penCount in site.Chinstrap.ElementAt(0))
                    {
                        data.Add(penCount.Count);
                        transparency.Add(penCount.Opacity);
                    }

                    _data.Add(new JsonData()
                    {
                        SiteName     = site.SiteName,
                        Penguin      = "Chinstrap",
                        Latitude     = site.Latitude,
                        Longitude    = site.Longitude,
                        Data         = data,
                        Transparency = transparency
                    });
                }

                if (site.Emperor.Count >= 1)
                {
                    List <int> data         = new List <int>();
                    List <int> transparency = new List <int>();

                    foreach (var penCount in site.Emperor.ElementAt(0))
                    {
                        data.Add(penCount.Count);
                        transparency.Add(penCount.Opacity);
                    }

                    _data.Add(new JsonData()
                    {
                        SiteName     = site.SiteName,
                        Penguin      = "Emperor",
                        Latitude     = site.Latitude,
                        Longitude    = site.Longitude,
                        Data         = data,
                        Transparency = transparency
                    });
                }
            }

            string json = JsonConvert.SerializeObject(_data.ToArray());

            //write string to file
            System.IO.File.WriteAllText(@"D:\penguins_heatmap.json", json);
        }