Exemple #1
0
        private void btnLoadConstituencyXml_Click(Object sender, EventArgs e)
        {
            OpenFileDialog openDialog = new OpenFileDialog();

            openDialog.Filter = "XML Files|*.xml|All files|*.*";
            if (openDialog.ShowDialog() == DialogResult.OK)
            {
                PopulationData data   = null;
                StreamReader   reader = null;
                try
                {
                    reader = new StreamReader(openDialog.FileName);
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.LoadXml(reader.ReadToEnd());
                    foreach (XmlNode node in xmlDoc.ChildNodes)
                    {
                        if (node.Name == "electiondata")
                        {
                            data = PopulationData.Load(node);
                        }
                    }
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Dispose();
                    }
                }
                if ((data != null) && (data.Data != null))
                {
                    Int32 year = Convert.ToInt32(edtYear.Value);
                    PopulationDataEntry dataEntry = GetPopulationData(year);
                    if (rbxNational.Checked && chkBuengKan.Checked)
                    {
                        ModifyPopulationDataForBuengKan(dataEntry);
                    }
                    dataEntry.SortSubEntitiesByEnglishName();

                    var entitie = data.Data.FlatList(new List <EntityType>()
                    {
                        EntityType.Bangkok, EntityType.Changwat, EntityType.Amphoe, EntityType.KingAmphoe, EntityType.Khet
                    });
                    foreach (PopulationDataEntry entry in entitie)
                    {
                        entry.CopyPopulationToConstituencies(dataEntry);
                    }

                    ConstituencyStatisticsViewer dialog = new ConstituencyStatisticsViewer(data.Data);
                    dialog.Show();
                }
            }
        }
Exemple #2
0
        private void btnCalc_Click(Object sender, EventArgs e)
        {
            Int32 year = Convert.ToInt32(edtYear.Value);
            Int32 numberOfConstituencies = Convert.ToInt32(edtNumberOfConstituencies.Value);
            Int32 geocode            = 0;
            PopulationDataEntry data = null;

            if (rbxNational.Checked)
            {
                data = GetPopulationData(year);
            }
            if (rbxProvince.Checked)
            {
                var province = (PopulationDataEntry)cbxProvince.SelectedItem;
                geocode = province.Geocode;
                PopulationData downloader = new PopulationData(year, geocode);
                downloader.Process();

                data = downloader.Data;
            }

            if (rbxNational.Checked && chkBuengKan.Checked)
            {
                ModifyPopulationDataForBuengKan(data);
            }
            data.SortSubEntitiesByEnglishName();

            Dictionary <PopulationDataEntry, Int32> result = ConstituencyCalculator.Calculate(data, year, numberOfConstituencies);

            if (chkRegions.Checked)
            {
                List <PopulationDataEntry> regions = TambonHelper.GetRegionBySchemeName(cbxRegion.Text);
                Dictionary <PopulationDataEntry, Int32> regionResult = new Dictionary <PopulationDataEntry, Int32>();
                foreach (PopulationDataEntry region in regions)
                {
                    Int32 constituencies = 0;
                    List <PopulationDataEntry> subList = new List <PopulationDataEntry>();
                    foreach (PopulationDataEntry province in region.SubEntities)
                    {
                        PopulationDataEntry foundEntry = data.FindByCode(province.Geocode);
                        if (foundEntry != null)
                        {
                            constituencies = constituencies + result[foundEntry];
                            subList.Add(foundEntry);
                        }
                    }
                    region.SubEntities.Clear();
                    region.SubEntities.AddRange(subList);
                    region.CalculateNumbersFromSubEntities();
                    regionResult.Add(region, constituencies);
                }
                result = regionResult;
            }

            String displayResult = String.Empty;

            foreach (KeyValuePair <PopulationDataEntry, Int32> entry in result)
            {
                Int32 votersPerSeat = 0;
                if (entry.Value != 0)
                {
                    votersPerSeat = entry.Key.Total / entry.Value;
                }
                displayResult = displayResult +
                                String.Format("{0} {1} ({2} per seat)", entry.Key.English, entry.Value, votersPerSeat) + Environment.NewLine;
            }
            txtData.Text       = displayResult;
            _lastCalculation   = result;
            btnSaveCsv.Enabled = true;
        }