示例#1
0
        public List <PopulationDataEntry> ThesabanList()
        {
            List <PopulationDataEntry> lResult   = new List <PopulationDataEntry>();
            List <PopulationDataEntry> lThesaban = this.FlatList(EntityTypeHelper.Thesaban);

            lThesaban.Sort(delegate(PopulationDataEntry p1, PopulationDataEntry p2)
            {
                return(p2.Geocode.CompareTo(p1.Geocode));
            });
            foreach (PopulationDataEntry lEntry in lThesaban)
            {
                PopulationDataEntry lResultEntry = lResult.Find(delegate(PopulationDataEntry p)
                {
                    return(p.Geocode == lEntry.Geocode);
                });
                if (lResultEntry == null)
                {
                    lResult.Add(new PopulationDataEntry(lEntry));
                }
                else
                {
                    lResultEntry.AddNumbers(lEntry);
                }
            }
            return(lResult);
        }
        private TreeNode PopulationDataEntryToTreeNode(PopulationDataEntry iData)
        {
            // ToDo: cleanup this CodeCopy from PopulationDataView.cs
            TreeNode retval = null;

            if (iData != null)
            {
                String lName = String.Empty;
                if (!String.IsNullOrEmpty(iData.English))
                {
                    lName = iData.English;
                }
                else
                {
                    lName = "(" + iData.Name + ")";
                }
                retval     = new TreeNode(lName);
                retval.Tag = iData;
                foreach (PopulationDataEntry lEntity in iData.SubEntities)
                {
                    if (!EntityTypeHelper.Thesaban.Contains(lEntity.Type))
                    {
                        retval.Nodes.Add(PopulationDataEntryToTreeNode(lEntity));
                    }
                }
            }
            return(retval);
        }
示例#3
0
        static public FrequencyCounter Statistics(PopulationDataEntry changwat)
        {
            FrequencyCounter counter = new FrequencyCounter();

            Statistics(changwat, counter);
            return(counter);
        }
示例#4
0
        protected void ParseList(StreamReader iReader)
        {
            String lCurrentLine = String.Empty;
            PopulationDataEntry lCurrentEntry = null;

            while ((lCurrentLine = iReader.ReadLine()) != null)
            {
                if (lCurrentLine.Length > 8)
                {
                    Int32 lPos = 8;
                    if (lCurrentLine.Contains('|'))
                    {
                        lPos         = lCurrentLine.IndexOf('|');
                        lCurrentLine = lCurrentLine.Replace("|", "");
                    }
                    String lGeocodeString = lCurrentLine.Substring(0, lPos);
                    Int32  lGeocode       = Convert.ToInt32(lGeocodeString);
                    while (lGeocode % 100 == 0)
                    {
                        lGeocode = lGeocode / 100;
                    }
                    String lName = lCurrentLine.Substring(8, lCurrentLine.Length - 8);
                    lName                 = lName.Replace("|", "").Trim();
                    lCurrentEntry         = new PopulationDataEntry();
                    lCurrentEntry.Geocode = lGeocode;
                    lCurrentEntry.ParseName(lName);
                    if (lGeocode < 100)
                    {
                        lCurrentEntry.Type = EntityType.Changwat;
                    }
                    mGeocodes.SubEntities.Add(lCurrentEntry);
                }
            }
        }
 public ConstituencyStatisticsViewer(PopulationDataEntry iData)
 {
     mData = iData;
     InitializeComponent();
     FillTreeView(TrvData);
     txtStatistics.Text = CalculateData(0);
 }
示例#6
0
 private void AddNumbers(PopulationDataEntry iEntry)
 {
     this.Households += iEntry.Households;
     this.Female     += iEntry.Female;
     this.Male       += iEntry.Male;
     this.Total      += iEntry.Total;
 }
示例#7
0
 public void CopyStaticDataFrom(PopulationDataEntry iValue)
 {
     Geocode = iValue.Geocode;
     GeocodeOfCorrespondingTambon = iValue.GeocodeOfCorrespondingTambon;
     if (!String.IsNullOrEmpty(iValue.English))
     {
         English = iValue.English;
     }
     foreach (Int32 i in iValue.GeocodeParent)
     {
         GeocodeParent.Add(i);
     }
     if (EntityTypeHelper.IsCompatibleEntityType(Type, EntityType.Tambon))
     {
         var lVillageList = new List <EntityType>()
         {
             EntityType.Muban
         };
         if ((iValue.NrOfEntities(lVillageList) > 0) &
             (this.NrOfEntities(lVillageList) == 0))
         {
             foreach (PopulationDataEntry lVillage in iValue.SubEntities)
             {
                 if (EntityTypeHelper.IsCompatibleEntityType(lVillage.Type, EntityType.Muban))
                 {
                     this.SubEntities.Add((PopulationDataEntry)lVillage.Clone());
                 }
             }
         }
     }
 }
示例#8
0
        private static void GetPopulationData(PopulationDataEntry iPopulation, List <PopulationDataEntry> iData)
        {
            if (iPopulation == null)
            {
                throw new ArgumentNullException("iPopulation", "No source for population data");
            }
            List <PopulationDataEntry> lNewEntityList = new List <PopulationDataEntry>();
            List <PopulationDataEntry> lThesabanList  = iPopulation.ThesabanList();

            foreach (PopulationDataEntry lConstituencyEntry in iData)
            {
                PopulationDataEntry lPopulationdataEntry = null;
                foreach (PopulationDataEntry lThesaban in lThesabanList)
                {
                    if (lThesaban.Geocode == lConstituencyEntry.Geocode)
                    {
                        lPopulationdataEntry = lThesaban;
                        break;
                    }
                }
                if (lPopulationdataEntry == null)
                {
                    lPopulationdataEntry = iPopulation.FindByCode(lConstituencyEntry.Geocode);
                }
                Debug.Assert(lPopulationdataEntry != null, "Entity with code " + lConstituencyEntry.Geocode.ToString() + " not found");
                if (lPopulationdataEntry != null)
                {
                    lNewEntityList.Add(lPopulationdataEntry);
                }
            }
            iData.Clear();
            iData.AddRange(lNewEntityList);
        }
示例#9
0
        public PopulationDataEntry FindByCode(Int32 code)
        {
            PopulationDataEntry retval = null;

            if (this.Geocode == code)
            {
                retval = this;
            }
            else
            {
                retval = SubEntities.FirstOrDefault(entry => (entry != null) && (entry.Geocode == code));
                if (retval == null)
                {
                    SubEntities.ForEach(delegate(PopulationDataEntry entity)
                    {
                        if ((retval == null) && (entity != null))
                        {
                            if (TambonHelper.IsBaseGeocode(entity.Geocode, code))
                            {
                                retval = entity.FindByCode(code);
                            }
                        }
                    });
                }
            }
            return(retval);
        }
示例#10
0
 public void CopyPopulationDataFrom(PopulationDataEntry iValue)
 {
     Total      = iValue.Total;
     Households = iValue.Households;
     Male       = iValue.Male;
     Female     = iValue.Female;
 }
示例#11
0
        public Dictionary<PopulationDataEntry, PopulationDataEntry> DifferentMubanNames(PopulationDataEntry changwat)
        {
            var RetVal = new Dictionary<PopulationDataEntry, PopulationDataEntry>();

            PopulationData geocodes = TambonHelper.GetGeocodeList(changwat.Geocode);
            PopulationDataEntry currentChangwat = geocodes.Data;
            foreach ( PopulationDataEntry currentAmphoe in currentChangwat.SubEntities )
            {
                foreach ( PopulationDataEntry currentTambon in currentAmphoe.SubEntities )
                {
                    foreach ( PopulationDataEntry currentMuban in currentTambon.SubEntities )
                    {
                        if ( currentMuban.Type == EntityType.Muban )
                        {
                            PopulationDataEntry mubanDopa = changwat.FindByCode(currentMuban.Geocode);
                            if ( mubanDopa != null )
                            {
                                if ( !TambonHelper.IsSameMubanName(mubanDopa.Name, currentMuban.Name) )
                                {
                                    RetVal.Add(currentMuban, mubanDopa);
                                }
                            }
                        }
                    }

                }
            }
            return RetVal;
        }
示例#12
0
        static public String StatisticsText(PopulationDataEntry changwat)
        {
            FrequencyCounter statistics = Statistics(changwat);
            String           result     = StatisticsText(statistics);

            return(result);
        }
示例#13
0
        private static void ModifyPopulationDataForBuengKan(PopulationDataEntry data)
        {
            PopulationDataEntry buengKan = data.FindByCode(38);

            if (buengKan == null)
            {
                buengKan         = new PopulationDataEntry();
                buengKan.English = "Bueng Kan";
                buengKan.Geocode = 38;
                List <Int32> buengKanAmphoeCodes = new List <int>()
                {
                    4313, 4311, 4309, 4312, 4303, 4306, 4310, 4304
                };
                data.SubEntities.RemoveAll(p => p == null);
                PopulationDataEntry nongKhai = data.FindByCode(43);
                foreach (Int32 code in buengKanAmphoeCodes)
                {
                    PopulationDataEntry entry = nongKhai.FindByCode(code);
                    buengKan.SubEntities.Add(entry);
                    nongKhai.SubEntities.Remove(entry);
                }
                nongKhai.CalculateNumbersFromSubEntities();
                buengKan.CalculateNumbersFromSubEntities();
                data.SubEntities.Add(buengKan);
                data.CalculateNumbersFromSubEntities();
            }
        }
示例#14
0
        public Dictionary <PopulationDataEntry, PopulationDataEntry> DifferentMubanNames(PopulationDataEntry changwat)
        {
            var RetVal = new Dictionary <PopulationDataEntry, PopulationDataEntry>();

            PopulationData      geocodes        = TambonHelper.GetGeocodeList(changwat.Geocode);
            PopulationDataEntry currentChangwat = geocodes.Data;

            foreach (PopulationDataEntry currentAmphoe in currentChangwat.SubEntities)
            {
                foreach (PopulationDataEntry currentTambon in currentAmphoe.SubEntities)
                {
                    foreach (PopulationDataEntry currentMuban in currentTambon.SubEntities)
                    {
                        if (currentMuban.Type == EntityType.Muban)
                        {
                            PopulationDataEntry mubanDopa = changwat.FindByCode(currentMuban.Geocode);
                            if (mubanDopa != null)
                            {
                                if (!TambonHelper.IsSameMubanName(mubanDopa.Name, currentMuban.Name))
                                {
                                    RetVal.Add(currentMuban, mubanDopa);
                                }
                            }
                        }
                    }
                }
            }
            return(RetVal);
        }
 public ConstituencyStatisticsViewer(PopulationDataEntry iData)
 {
     mData = iData;
     InitializeComponent();
     FillTreeView(TrvData);
     txtStatistics.Text = CalculateData(0);
 }
        private void TrvData_AfterSelect(object sender, TreeViewEventArgs e)
        {
            PopulationDataEntry lEntry = (PopulationDataEntry)e.Node.Tag;

            if (lEntry != null)
            {
                txtStatistics.Text = CalculateData(lEntry.Geocode);
            }
        }
示例#17
0
        internal static ConstituencyEntry Load(XmlNode iNode)
        {
            ConstituencyEntry RetVal = null;

            if (iNode != null && iNode.Name.Equals("constituency"))
            {
                RetVal               = new ConstituencyEntry();
                RetVal.Index         = TambonHelper.GetAttributeOptionalInt(iNode, "index", 0);
                RetVal.NumberOfSeats = TambonHelper.GetAttributeOptionalInt(iNode, "numberofseats", 1);

                if (iNode.HasChildNodes)
                {
                    foreach (XmlNode lChildNode in iNode.ChildNodes)
                    {
                        if (lChildNode.Name == "include")
                        {
                            PopulationDataEntry lEntity = new PopulationDataEntry();
                            lEntity.Geocode = TambonHelper.GetAttributeOptionalInt(lChildNode, "geocode", 0);
                            foreach (XmlNode lSubChildNode in lChildNode.ChildNodes)
                            {
                                if (lSubChildNode.Name == "exclude")
                                {
                                    PopulationDataEntry lExcludedEntity = new PopulationDataEntry();
                                    lExcludedEntity.Geocode = TambonHelper.GetAttributeOptionalInt(lSubChildNode, "geocode", 0);
                                    if (!RetVal.ExcludedAdministrativeEntities.ContainsKey(lEntity))
                                    {
                                        RetVal.ExcludedAdministrativeEntities[lEntity] = new List <PopulationDataEntry>();
                                    }
                                    RetVal.ExcludedAdministrativeEntities[lEntity].Add(lExcludedEntity);
                                }
                            }
                            RetVal.AdministrativeEntities.Add(lEntity);
                        }
                        if (lChildNode.Name == "includesub")
                        {
                            PopulationDataEntry lEntity = new PopulationDataEntry();
                            lEntity.Geocode = TambonHelper.GetAttributeOptionalInt(lChildNode, "geocode", 0);
                            foreach (XmlNode lSubChildNode in lChildNode.ChildNodes)
                            {
                                if (lSubChildNode.Name == "include")
                                {
                                    PopulationDataEntry lIncludedEntity = new PopulationDataEntry();
                                    lIncludedEntity.Geocode = TambonHelper.GetAttributeOptionalInt(lSubChildNode, "geocode", 0);
                                    if (!RetVal.SubIncludedAdministrativeEntities.ContainsKey(lEntity))
                                    {
                                        RetVal.SubIncludedAdministrativeEntities[lEntity] = new List <PopulationDataEntry>();
                                    }
                                    RetVal.SubIncludedAdministrativeEntities[lEntity].Add(lIncludedEntity);
                                }
                            }
                        }
                    }
                }
            }
            return(RetVal);
        }
示例#18
0
文件: Form1.cs 项目: zenwalk/tambon
        private void btn_PopulationAllProvinces_Click(object sender, EventArgs e)
        {
            Int32 year = Convert.ToInt32(edtYear.Value);
            PopulationDataEntry country = TambonHelper.GetCountryPopulationData(year);
            // CheckPopulationData(downloader);
            var dataForm = new PopulationByEntityTypeViewer();

            dataForm.Country = country;
            dataForm.Show();
        }
示例#19
0
 private void LoadXmlThesaban(XmlNode node)
 {
     foreach (XmlNode childNode in node.ChildNodes)
     {
         if (childNode.Name == "entity")
         {
             _thesaban.Add(PopulationDataEntry.Load(childNode));
         }
     }
 }
        private PopulationDataEntry FindCompare()
        {
            PopulationDataEntry result = null;

            if (chkCompare.Checked)
            {
                Int32 year = Convert.ToInt32(edtCompareYear.Value);
                result = TambonHelper.GetCountryPopulationData(year);
            }
            return(result);
        }
        private string GetEntityConstituencyName(Int32 iEntry)
        {
            Int32 lGeocode             = iEntry / 100;
            Int32 lConstituency        = iEntry % 100;
            PopulationDataEntry lEntry = mData.FindByCode(lGeocode);

            Debug.Assert(lEntry != null, "Code " + lGeocode.ToString() + " not found");
            String lResult = lEntry.English + " Constituency " + lConstituency.ToString();

            return(lResult);
        }
示例#22
0
        private PopulationDataEntry FindByName(string iValue)
        {
            PopulationDataEntry retval = null;

            retval = FindByName(iValue, false);
            if (retval == null)
            {
                retval = FindByName(iValue, true);
            }
            return(retval);
        }
示例#23
0
 public static void Statistics(PopulationDataEntry changwat, FrequencyCounter counter)
 {
     foreach ( PopulationDataEntry amphoe in changwat.SubEntities )
     {
         foreach ( PopulationDataEntry tambon in amphoe.SubEntities )
         {
             Int32 lNumberOfMuban = tambon.SubEntities.Count;
             counter.IncrementForCount(lNumberOfMuban, tambon.Geocode);
         }
     }
 }
示例#24
0
 protected void DoLoad(XmlNode node)
 {
     if (node.Name == "entity")
     {
         _changwat = PopulationDataEntry.Load(node);
     }
     else if (node.Name == "thesaban")
     {
         LoadXmlThesaban(node);
     }
 }
示例#25
0
 static public void Statistics(PopulationDataEntry changwat, FrequencyCounter counter)
 {
     foreach (PopulationDataEntry amphoe in changwat.SubEntities)
     {
         foreach (PopulationDataEntry tambon in amphoe.SubEntities)
         {
             Int32 lNumberOfMuban = tambon.SubEntities.Count;
             counter.IncrementForCount(lNumberOfMuban, tambon.Geocode);
         }
     }
 }
示例#26
0
        private PopulationDataEntry GetPopulationDataFromCache(Int32 year)
        {
            PopulationDataEntry data = null;

            if (_downloadedData.Keys.Contains(year))
            {
                data = _downloadedData[year];
                data = (PopulationDataEntry)data.Clone();
            }
            return(data);
        }
示例#27
0
        private PopulationDataEntry FindByNameAndType(String iValue, EntityType iEntityType, Boolean iAllowOldNames)
        {
            PopulationDataEntry retval = null;

            retval = FindByNameAndType(iValue, iEntityType, iAllowOldNames, false, 0);
            if (retval == null)
            {
                retval = FindByNameAndType(iValue, iEntityType, iAllowOldNames, true, 0);
            }
            return(retval);
        }
示例#28
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();
                }
            }
        }
        private void UpdateList()
        {
            IEnumerable <PopulationDataEntry> list = CalculateList();
            PopulationDataEntry compare            = FindCompare();
            List <Tuple <Int32, Int32, Double> > populationChanges = null;

            if (compare != null)
            {
                populationChanges = CalcPopulationChanges(list, compare).ToList();
            }
            FillListView(list, populationChanges);

            FrequencyCounter counter = new FrequencyCounter();

            foreach (var entry in list)
            {
                counter.IncrementForCount(entry.Total, entry.Geocode);
            }

            StringBuilder builder = new StringBuilder();

            builder.AppendLine("Total population: " + counter.SumValue.ToString("##,###,##0"));
            builder.AppendLine("Number of entities: " + counter.NumberOfValues.ToString());
            builder.AppendLine("Mean population: " + counter.MeanValue.ToString("##,###,##0.0"));
            builder.AppendLine("Maximum population: " + counter.MaxValue.ToString("##,###,##0"));
            builder.AppendLine("Minimum population: " + counter.MinValue.ToString("##,###,##0"));

            if ((populationChanges != null) && (populationChanges.Any(x => x.Item2 != 0)))
            {
                builder.AppendLine();
                populationChanges.Sort((x, y) => y.Item2.CompareTo(x.Item2));
                var winner      = populationChanges.First();
                var winnerEntry = list.First(x => x.Geocode == winner.Item1);
                var looser      = populationChanges.Last();
                var looserEntry = list.First(x => x.Geocode == looser.Item1);
                builder.AppendLine("Biggest winner: " + winner.Item2.ToString("##,###,##0") + " by " + winnerEntry.English + " (" + winner.Item1 + ")");
                builder.AppendLine("Biggest looser: " + looser.Item2.ToString("##,###,##0") + " by " + looserEntry.English + " (" + looser.Item1 + ")");
            }
            if ((populationChanges != null) && (populationChanges.Any(x => x.Item2 != 0)))
            {
                builder.AppendLine();
                populationChanges.Sort((x, y) => y.Item3.CompareTo(x.Item3));
                var winner      = populationChanges.First();
                var winnerEntry = list.First(x => x.Geocode == winner.Item1);
                var looser      = populationChanges.Last();
                var looserEntry = list.First(x => x.Geocode == looser.Item1);
                builder.AppendLine("Biggest winner: " + winner.Item3.ToString("##0.00") + "% by " + winnerEntry.English + " (" + winner.Item1 + ")");
                builder.AppendLine("Biggest looser: " + looser.Item3.ToString("##0.00") + "% by " + looserEntry.English + " (" + looser.Item1 + ")");
            }

            txtStatistics.Text = builder.ToString();
        }
示例#30
0
        public static Dictionary <PopulationDataEntry, Int32> Calculate(PopulationDataEntry data, Int32 year, Int32 numberOfSeats)
        {
            Dictionary <PopulationDataEntry, Int32> result = new Dictionary <PopulationDataEntry, Int32>();

            Int32 totalPopulation = 0;

            foreach (PopulationDataEntry entry in data.SubEntities)
            {
                if (entry != null)
                {
                    result.Add(entry, 0);
                    totalPopulation += entry.Total;
                }
            }
            double divisor       = (1.0 * numberOfSeats) / (1.0 * totalPopulation);
            Int32  remainingSeat = numberOfSeats;
            Dictionary <PopulationDataEntry, double> remainder = new Dictionary <PopulationDataEntry, double>();

            foreach (PopulationDataEntry entry in data.SubEntities)
            {
                if (entry != null)
                {
                    double seats       = entry.Total * divisor;
                    Int32  actualSeats = Math.Max(1, Convert.ToInt32(Math.Truncate(seats)));
                    result[entry]  = actualSeats;
                    remainingSeat -= actualSeats;
                    double remainingValue = seats - actualSeats;
                    remainder.Add(entry, remainingValue);
                }
            }

            List <PopulationDataEntry> sortedRemainders = new List <PopulationDataEntry>();

            foreach (PopulationDataEntry entry in data.SubEntities)
            {
                if (entry != null)
                {
                    sortedRemainders.Add(entry);
                }
            }
            sortedRemainders.Sort(delegate(PopulationDataEntry p1, PopulationDataEntry p2) { return(remainder[p2].CompareTo(remainder[p1])); });

            while (remainingSeat > 0)
            {
                PopulationDataEntry first = sortedRemainders.First();
                result[first] += 1;
                remainingSeat -= 1;
                sortedRemainders.Remove(first);
            }

            return(result);
        }
示例#31
0
        private PopulationDataEntry GetPopulationData(Int32 year)
        {
            PopulationDataEntry data = GetPopulationDataFromCache(year);

            if (data == null)
            {
                PopulationData downloader = new PopulationData(year, 0);
                downloader.Process();
                data = downloader.Data;
                StorePopulationDataToCache(data, year);
            }
            return(data);
        }
示例#32
0
 /// <summary>
 /// Creates a new instance of <see cref="PopulationData"/> from a <see cref="PopulationDataEntry"/>.
 /// </summary>
 /// <param name="entry">Population data.</param>
 /// <exception cref="ArgumentNullException"><paramref name="entry"/> is <c>null</c>.</exception>
 /// <exception cref="ArgumentException"><paramref name="entry"/> is not the data of a province-like entity.</exception>
 public PopulationData(PopulationDataEntry entry)
 {
     if (entry == null)
     {
         throw new ArgumentNullException("entry");
     }
     if (!EntityTypeHelper.IsCompatibleEntityType(entry.Type, EntityType.Changwat))
     {
         throw new ArgumentException("entry", String.Format("{0} is an invalid type of base entry", entry.Type));
     }
     _geocode  = entry.Geocode;
     _changwat = entry;
 }
示例#33
0
        private void ProcessAllProvinces()
        {
            PopulationDataEntry data = new PopulationDataEntry();

            foreach (PopulationDataEntry entry in TambonHelper.ProvinceGeocodes)
            {
                PopulationData tempCalculator = new PopulationData(Year, entry.Geocode);
                tempCalculator.Process();
                data.SubEntities.Add(tempCalculator.Data);
            }
            data.CalculateNumbersFromSubEntities();
            _changwat = data;
        }
示例#34
0
 private void PopulationDataEntryToListView(PopulationDataEntry iData)
 {
     mListviewData.BeginUpdate();
     mListviewData.Items.Clear();
     foreach ( PopulationDataEntry lEntity in iData.SubEntities )
     {
         ListViewItem lListViewItem = mListviewData.Items.Add(lEntity.English);
         lListViewItem.SubItems.Add(lEntity.Name);
         lListViewItem.SubItems.Add(lEntity.Geocode.ToString());
         lListViewItem.SubItems.Add(lEntity.Total.ToString());
         lListViewItem.SubItems.Add(lEntity.SubNames(EntityTypeHelper.Thesaban).ToString());
     }
     mListviewData.EndUpdate();
 }
示例#35
0
        public PopulationDataEntry(PopulationDataEntry value)
        {
            Type = value.Type;
            Total = value.Total;
            Male = value.Male;
            Female = value.Female;
            Households = value.Households;
            English = value.English;
            Geocode = value.Geocode;
            Obsolete = value.Obsolete;
            Name = value.Name;
            Comment = value.Comment;

            foreach ( Int32 lGeocode in value.mNewGeocode )
            {
                mNewGeocode.Add(lGeocode);
            }
            foreach ( Int32 lGeocode in value.mGeocodeParent )
            {
                mGeocodeParent.Add(lGeocode);
            }
            foreach ( PopulationDataEntry lSubEntity in value.SubEntities )
            {
                if ( lSubEntity != null )
                {
                    SubEntities.Add((PopulationDataEntry)lSubEntity.Clone());
                }
            }
            foreach ( EntityOffice lOffice in value.Offices )
            {
                Offices.Add((EntityOffice)lOffice.Clone());
            }
            foreach ( String lOldName in value.OldNames )
            {
                mOldNames.Add(lOldName);
            }
            foreach ( ConstituencyEntry lEntry in value.ConstituencyList )
            {
                ConstituencyList.Add((ConstituencyEntry)lEntry.Clone());
            }
        }
示例#36
0
 private static void ModifyPopulationDataForBuengKan(PopulationDataEntry data)
 {
     PopulationDataEntry buengKan = data.FindByCode(38);
     if ( buengKan == null )
     {
         buengKan = new PopulationDataEntry();
         buengKan.English = "Bueng Kan";
         buengKan.Geocode = 38;
         List<Int32> buengKanAmphoeCodes = new List<int>() { 4313, 4311, 4309, 4312, 4303, 4306, 4310, 4304 };
         data.SubEntities.RemoveAll(p => p == null);
         PopulationDataEntry nongKhai = data.FindByCode(43);
         foreach ( Int32 code in buengKanAmphoeCodes )
         {
             PopulationDataEntry entry = nongKhai.FindByCode(code);
             buengKan.SubEntities.Add(entry);
             nongKhai.SubEntities.Remove(entry);
         }
         nongKhai.CalculateNumbersFromSubEntities();
         buengKan.CalculateNumbersFromSubEntities();
         data.SubEntities.Add(buengKan);
         data.CalculateNumbersFromSubEntities();
     }
 }
示例#37
0
 public void CopyPopulationToConstituencies(PopulationDataEntry iPopulationSource)
 {
     PopulationDataEntry lSourcePopulationdataEntry = iPopulationSource.FindByCode(Geocode);
     if ( lSourcePopulationdataEntry != null )
     {
         lSourcePopulationdataEntry.CalculateNumbersFromSubEntities();
         CopyPopulationDataFrom(lSourcePopulationdataEntry);
     }
     Debug.Assert(lSourcePopulationdataEntry != null, "No source data entry with geocode " + Geocode.ToString());
     if ( lSourcePopulationdataEntry != null )
     {
         foreach ( ConstituencyEntry lConstituency in ConstituencyList )
         {
             GetPopulationData(lSourcePopulationdataEntry, lConstituency.AdministrativeEntities);
             foreach ( var lKeyValuePair in lConstituency.ExcludedAdministrativeEntities )
             {
                 PopulationDataEntry lSourceSubEntity = iPopulationSource.FindByCode(lKeyValuePair.Key.Geocode);
                 GetPopulationData(lSourceSubEntity, lKeyValuePair.Value);
             }
             foreach ( var lKeyValuePair in lConstituency.SubIncludedAdministrativeEntities )
             {
                 PopulationDataEntry lSourceSubEntity = iPopulationSource.FindByCode(lKeyValuePair.Key.Geocode);
                 GetPopulationData(lSourceSubEntity, lKeyValuePair.Value);
             }
         }
     }
     Int32 lPopulation = ConstituencyList.Population();
     Int32 lTotal = Total;
     if ( (lPopulation > 0) && (lTotal > 0) )
     {
         Int32 lPopulationDiff = lPopulation - lTotal;
         Debug.Assert(lPopulationDiff == 0, "Population for " + English + " does not sum up, off by " + lPopulationDiff.ToString());
     }
 }
示例#38
0
 public void CopyStaticDataFrom(PopulationDataEntry iValue)
 {
     Geocode = iValue.Geocode;
     GeocodeOfCorrespondingTambon = iValue.GeocodeOfCorrespondingTambon;
     if ( !String.IsNullOrEmpty(iValue.English) )
     {
         English = iValue.English;
     }
     foreach ( Int32 i in iValue.GeocodeParent )
     {
         GeocodeParent.Add(i);
     }
     if ( EntityTypeHelper.IsCompatibleEntityType(Type, EntityType.Tambon) )
     {
         var lVillageList = new List<EntityType>() { EntityType.Muban };
         if ( (iValue.NrOfEntities(lVillageList) > 0) &
             (this.NrOfEntities(lVillageList) == 0) )
         {
             foreach ( PopulationDataEntry lVillage in iValue.SubEntities )
             {
                 if ( EntityTypeHelper.IsCompatibleEntityType(lVillage.Type, EntityType.Muban) )
                 {
                     this.SubEntities.Add((PopulationDataEntry)lVillage.Clone());
                 }
             }
         }
     }
 }
示例#39
0
        internal static PopulationDataEntry Load(XmlNode iNode)
        {
            PopulationDataEntry RetVal = null;

            if ( iNode != null && iNode.Name.Equals("entity") )
            {
                RetVal = new PopulationDataEntry();
                RetVal.Name = TambonHelper.GetAttributeOptionalString(iNode, "name").Trim();
                RetVal.English = TambonHelper.GetAttributeOptionalString(iNode, "english").Trim();
                RetVal.Total = TambonHelper.GetAttributeOptionalInt(iNode, "total", 0);
                RetVal.Obsolete = TambonHelper.GetAttributeOptionalBool(iNode, "obsolete", false);
                RetVal.Male = TambonHelper.GetAttributeOptionalInt(iNode, "male", 0);
                RetVal.Female = TambonHelper.GetAttributeOptionalInt(iNode, "female", 0);
                RetVal.Households = TambonHelper.GetAttributeOptionalInt(iNode, "households", 0);
                RetVal.Geocode = TambonHelper.GetAttributeOptionalInt(iNode, "geocode", 0);
                RetVal.GeocodeOfCorrespondingTambon = TambonHelper.GetAttributeOptionalInt(iNode, "tambon", 0);
                RetVal.Comment = TambonHelper.GetAttributeOptionalString(iNode, "comment");
                string lNewGeocode = TambonHelper.GetAttributeOptionalString(iNode, "newgeocode");
                foreach ( string lSubString in lNewGeocode.Split(new Char[] { ' ' }) )
                {
                    if ( !string.IsNullOrEmpty(lSubString) )
                    {
                        RetVal.NewGeocode.Add(Convert.ToInt32(lSubString));
                    }
                }
                string s = TambonHelper.GetAttributeOptionalString(iNode, "type");
                if ( !String.IsNullOrEmpty(s) )
                {
                    RetVal.Type = (EntityType)Enum.Parse(typeof(EntityType), s);
                }
                string lGeocodeParent = TambonHelper.GetAttributeOptionalString(iNode, "parent");
                foreach ( string lSubString in lGeocodeParent.Split(new Char[] { ' ' }) )
                {
                    if ( !string.IsNullOrEmpty(lSubString) )
                    {
                        RetVal.GeocodeParent.Add(Convert.ToInt32(lSubString));
                    }
                }
                if ( iNode.HasChildNodes )
                {
                    foreach ( XmlNode lChildNode in iNode.ChildNodes )
                    {
                        if ( lChildNode.Name == "office" )
                        {
                            EntityOffice lOffice = EntityOffice.Load(lChildNode);
                            RetVal.Offices.Add(lOffice);
                        }
                        if ( lChildNode.Name == "history" )
                        {
                            RetVal.ParseHistory(lChildNode);
                        }
                        if ( lChildNode.Name == "entity" )
                        {
                            RetVal.SubEntities.Add(PopulationDataEntry.Load(lChildNode));
                        }
                        if ( lChildNode.Name == "constituencies" )
                        {
                            RetVal.ConstituencyList.ReadFromXml(lChildNode);
                        }
                    }
                }
            }
            return RetVal;
        }
示例#40
0
 private static void GetPopulationData(PopulationDataEntry iPopulation, List<PopulationDataEntry> iData)
 {
     if ( iPopulation == null )
     {
         throw new ArgumentNullException("iPopulation", "No source for population data");
     }
     List<PopulationDataEntry> lNewEntityList = new List<PopulationDataEntry>();
     List<PopulationDataEntry> lThesabanList = iPopulation.ThesabanList();
     foreach ( PopulationDataEntry lConstituencyEntry in iData )
     {
         PopulationDataEntry lPopulationdataEntry = null;
         foreach ( PopulationDataEntry lThesaban in lThesabanList )
         {
             if ( lThesaban.Geocode == lConstituencyEntry.Geocode )
             {
                 lPopulationdataEntry = lThesaban;
                 break;
             }
         }
         if ( lPopulationdataEntry == null )
         {
             lPopulationdataEntry = iPopulation.FindByCode(lConstituencyEntry.Geocode);
         }
         Debug.Assert(lPopulationdataEntry != null, "Entity with code " + lConstituencyEntry.Geocode.ToString() + " not found");
         if ( lPopulationdataEntry != null )
         {
             lNewEntityList.Add(lPopulationdataEntry);
         }
     }
     iData.Clear();
     iData.AddRange(lNewEntityList);
 }
示例#41
0
        private TreeNode PopulationDataEntryToTreeNode(PopulationDataEntry iData)
        {
            TreeNode retval = null;
            if ( iData != null )
            {
                String lName = String.Empty;
                if ( !String.IsNullOrEmpty(iData.English) )
                {
                    lName = iData.English;
                }
                else
                {
                    lName = "(" + iData.Name + ")";
                }
                retval = new TreeNode(lName);
                retval.Tag = iData;
                foreach ( PopulationDataEntry lEntity in iData.SubEntities )
                {
                    if ( !EntityTypeHelper.Thesaban.Contains(lEntity.Type) )
                    {
                        retval.Nodes.Add(PopulationDataEntryToTreeNode(lEntity));
                    }
                }

            }
            return retval;
        }
示例#42
0
        public PopulationDataEntry Parse(StreamReader reader)
        {
            // Column 1 : is number, then use
            // Column 2 : Amphoe name
            // Column 3 : Tambon name
            // Column 4 : Code
            // Column 5 : Name
            // Column 6 : Muban number
            // Column 7 : Location source/placemark
            // Column 8 : Location UTM Easting (47N, Indian 1974)
            // Column 9 : Location UTM Northing (47N, Indian 1974)

            String currentLine = String.Empty;
            PopulationDataEntry currentChangwat = new PopulationDataEntry();
            PopulationDataEntry currentAmphoe = new PopulationDataEntry();
            PopulationDataEntry currentTambon = new PopulationDataEntry();

            while ( (currentLine = reader.ReadLine()) != null )
            {
                var subStrings = currentLine.Split(new Char[] { '\t' });
                if ( (subStrings.Length > 0) & (!String.IsNullOrEmpty(subStrings[0])) & TambonHelper.IsNumeric(subStrings[0]) )
                {
                    PopulationDataEntry currentMuban = new PopulationDataEntry();
                    String amphoe = subStrings[1].Replace('"', ' ').Trim();
                    String tambon = subStrings[2].Replace('"', ' ').Trim();
                    String geocode = subStrings[3].Replace('"', ' ').Replace(" ", "").Trim();
                    currentMuban.Geocode = Convert.ToInt32(geocode);
                    currentMuban.Name = subStrings[4].Replace('"', ' ').Trim();
                    currentMuban.Type = EntityType.Muban;
                    String comment = subStrings[6].Replace('"', ' ').Trim();
                    String easting = subStrings[7].Replace('"', ' ').Replace('E', ' ').Trim();
                    String northing = subStrings[8].Replace('"', ' ').Replace('N', ' ').Trim();
                    if ( TambonHelper.IsNumeric(easting) && TambonHelper.IsNumeric(northing) )
                    {
                        EntityOffice office = new EntityOffice();
                        office.Type = OfficeType.VillageHeadmanOffice;
                        UtmPoint utmLocation = new UtmPoint(Convert.ToInt32(easting), Convert.ToInt32(northing), 47, true);
                        office.Location = new GeoPoint(utmLocation, GeoDatum.DatumIndian1975());
                        office.Location.Datum = GeoDatum.DatumWGS84();
                        currentMuban.Offices.Add(office);
                    }
                    String mubanString = subStrings[5].Replace('"', ' ').Trim();
                    if ( TambonHelper.IsNumeric(mubanString) )
                    {
                        Int32 muban = Convert.ToInt32(mubanString);
                        if ( muban != (currentMuban.Geocode % 100) )
                        {
                            comment = comment + Environment.NewLine + "Code is " + currentMuban.Geocode.ToString() + ',';
                            comment = comment + " Muban number is " + muban.ToString();
                            currentMuban.Geocode = currentMuban.Geocode - (currentMuban.Geocode % 100) + muban;
                        }
                    }
                    if ( (currentMuban.Geocode / 10000) != currentAmphoe.Geocode )
                    {
                        currentAmphoe = new PopulationDataEntry();
                        currentAmphoe.Name = tambon;
                        currentAmphoe.Type = EntityType.Amphoe;
                        currentAmphoe.Geocode = (currentMuban.Geocode / 10000);
                        currentChangwat.SubEntities.Add(currentAmphoe);
                    }
                    if ( (currentMuban.Geocode / 100) != currentTambon.Geocode )
                    {
                        currentTambon = new PopulationDataEntry();
                        currentTambon.Name = tambon;
                        currentTambon.Type = EntityType.Tambon;
                        currentTambon.Geocode = (currentMuban.Geocode / 100);
                        currentAmphoe.SubEntities.Add(currentTambon);
                    }
                    currentMuban.Comment = comment;
                    currentTambon.SubEntities.Add(currentMuban);
                }
            }
            currentChangwat.Type = EntityType.Changwat;
            return currentChangwat;
        }
示例#43
0
        private void btnCreateKml_Click(object sender, EventArgs e)
        {
            var allEntities = new List<PopulationDataEntry>();
            foreach ( PopulationDataEntry provinceGeocode in TambonHelper.ProvinceGeocodes )
            {
                PopulationData provinceEntities = TambonHelper.GetGeocodeList(provinceGeocode.Geocode);
                allEntities.Add(provinceEntities.Data);
            }
            PopulationDataEntry master = new PopulationDataEntry();
            master.SubEntities.AddRange(allEntities);
            String outFilename = Path.Combine(GlobalSettings.XMLOutputDir, "offices.kml");
            master.ExportToKml(outFilename);

            var geotaggedOffices = new Dictionary<OfficeType, Int32>();
            var anyOffices = new Dictionary<OfficeType, Int32>();
            var flatList = master.FlatList(EntityTypeHelper.AllEntityTypes);
            foreach ( var entity in flatList )
            {
                foreach ( var office in entity.Offices )
                {
                    if ( office.Location != null )
                    {
                        if ( !geotaggedOffices.ContainsKey(office.Type) )
                        {
                            geotaggedOffices.Add(office.Type, 0);
                        }
                        geotaggedOffices[office.Type]++;
                    }
                    if ( !anyOffices.ContainsKey(office.Type) )
                    {
                        anyOffices.Add(office.Type, 0);
                    }
                    anyOffices[office.Type]++;
                }
            }
            String officeTypeInfo = String.Empty;
            foreach ( OfficeType officeType in System.Enum.GetValues(typeof(OfficeType)) )
            {
                if ( geotaggedOffices.ContainsKey(officeType) )
                {
                    officeTypeInfo =
                        officeTypeInfo +
                        String.Format("{0} {1} (of {2})", officeType, geotaggedOffices[officeType], anyOffices[officeType])
                        + Environment.NewLine;
                }
            }

            var provincesWithoutPAOLocation = flatList.FindAll(x => x.Offices.Any(y => (y.Type == OfficeType.PAOOffice) && (y.Location == null)));
            String provincesWithoutPAOLocationInfo = String.Format("No PAO location ({0}): ", provincesWithoutPAOLocation.Count());
            foreach ( var entity in provincesWithoutPAOLocation )
            {
                provincesWithoutPAOLocationInfo = provincesWithoutPAOLocationInfo + String.Format("{0} ({1}),", entity.English, entity.Geocode);
            }
            var districtsWithoutOffice = flatList.FindAll(x => x.Offices.Any(y => (y.Type == OfficeType.DistrictOffice) && (y.Location == null)));
            String districtsWithoutOfficeInfo = String.Format("No district office location ({0}): ", districtsWithoutOffice.Count());
            foreach ( var entity in districtsWithoutOffice )
            {
                districtsWithoutOfficeInfo = districtsWithoutOfficeInfo + String.Format("{0} ({1}),", entity.English, entity.Geocode);
            }

            var info = officeTypeInfo + Environment.NewLine + provincesWithoutPAOLocationInfo + Environment.NewLine + districtsWithoutOfficeInfo;

            var form = new StringDisplayForm("Office types", info);
            form.Show();
        }
示例#44
0
 private void AddNumbers(PopulationDataEntry iEntry)
 {
     this.Households += iEntry.Households;
     this.Female += iEntry.Female;
     this.Male += iEntry.Male;
     this.Total += iEntry.Total;
 }
示例#45
0
        internal void GetCodes(PopulationDataEntry geocodeSource)
        {
            List<PopulationDataEntry> missedEntities = new List<PopulationDataEntry>();
            if ( geocodeSource != null )
            {
                // this == geocodeSource => copy directly from source
                if ( ((Name == geocodeSource.Name) | (geocodeSource.OldNames.Contains(Name))) & (EntityTypeHelper.IsCompatibleEntityType(Type, geocodeSource.Type)) )
                {
                    CopyStaticDataFrom(geocodeSource);
                }

                foreach ( PopulationDataEntry subEntity in SubEntities )
                {
                    // find number of sub entities with same name and type
                    Int32 position = 0;
                    if ( subEntity.Type != EntityType.Muban )
                    {
                        foreach ( PopulationDataEntry find in SubEntities )
                        {
                            if ( find == subEntity )
                            {
                                break;
                            }
                            if ( find.SameNameAndType(subEntity.Name, subEntity.Type) )
                            {
                                position++;
                            }
                        }
                    }

                    PopulationDataEntry sourceEntity = null;
                    if ( subEntity.Type == EntityType.Muban )
                    {
                        sourceEntity = geocodeSource.FindByCode(subEntity.Geocode);
                    }
                    else
                    {
                        sourceEntity = geocodeSource.FindByNameAndType(subEntity.Name, subEntity.Type, false, position);
                        if ( sourceEntity == null )
                        {
                            sourceEntity = geocodeSource.FindByNameAndType(subEntity.Name, subEntity.Type, true, position);
                        }
                    }
                    if ( sourceEntity != null )
                    {
                        subEntity.GetCodes(sourceEntity);
                    }
                    else
                    {
                        // Problem!
                    }

                    if ( EntityTypeHelper.Thesaban.Contains(subEntity.Type) | (EntityTypeHelper.Sakha.Contains(subEntity.Type)) )
                    {
                        foreach ( Int32 parentCode in subEntity.GeocodeParent )
                        {
                            PopulationDataEntry parentEntity = geocodeSource.FindByCode(parentCode);
                            if ( parentEntity != null )
                            {
                                subEntity.GetCodes(parentEntity);
                                PopulationDataEntry sourceValue = this.FindByCode(parentCode);
                                if ( sourceValue == null )
                                {
                                    PopulationDataEntry newEntry = (PopulationDataEntry)parentEntity.Clone();
                                    newEntry.SubEntities.Clear();
                                    Boolean found = false;
                                    foreach ( PopulationDataEntry compare in missedEntities )
                                    {
                                        found = found | (compare.Geocode == newEntry.Geocode);
                                    }
                                    if ( !found )
                                    {
                                        missedEntities.Add(newEntry);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            foreach ( PopulationDataEntry newEntry in missedEntities )
            {
                PopulationDataEntry parent = this.FindByCode(newEntry.Geocode / 100);
                if ( parent != null )
                {
                    parent.SubEntities.Add(newEntry);
                }
                parent.SortSubEntitiesByGeocode();
            }
        }
示例#46
0
 public void CopyPopulationDataFrom(PopulationDataEntry iValue)
 {
     Total = iValue.Total;
     Households = iValue.Households;
     Male = iValue.Male;
     Female = iValue.Female;
 }
示例#47
0
 public static FrequencyCounter Statistics(PopulationDataEntry changwat)
 {
     FrequencyCounter counter = new FrequencyCounter();
     Statistics(changwat, counter);
     return counter;
 }
示例#48
0
 private void StorePopulationDataToCache(PopulationDataEntry data, Int32 year)
 {
     _downloadedData[year] = (PopulationDataEntry)data.Clone();
 }
示例#49
0
 public static String StatisticsText(PopulationDataEntry changwat)
 {
     FrequencyCounter statistics = Statistics(changwat);
     String result = StatisticsText(statistics);
     return result;
 }
 private IEnumerable<Tuple<Int32, Int32, Double>> CalcPopulationChanges(IEnumerable<PopulationDataEntry> entityList, PopulationDataEntry compare)
 {
     List<Tuple<Int32, Int32, Double>> result = new List<Tuple<Int32, Int32, Double>>();
     if ( entityList.Any() && (compare != null) )
     {
         IEnumerable<PopulationDataEntry> thesabanList = null;
         if ( (EntityTypeHelper.IsCompatibleEntityType(EntityType.Thesaban, entityList.First().Type)) )
         {
             thesabanList = compare.ThesabanList();
         }
         foreach ( PopulationDataEntry entity in entityList )
         {
             if ( entity.Geocode != 0 )
             {
                 PopulationDataEntry compareEntry;
                 if ( EntityTypeHelper.IsCompatibleEntityType(EntityType.Thesaban, entity.Type) )
                 {
                     compareEntry = thesabanList.FirstOrDefault(x => x.Geocode == entity.Geocode);
                 }
                 else
                 {
                     compareEntry = compare.FindByCode(entity.Geocode);
                 }
                 if ( (compareEntry != null) && (compareEntry.Total > 0) )
                 {
                     Int32 populationChange = entity.Total - compareEntry.Total;
                     Double changePercent = 100.0 * populationChange / compareEntry.Total;
                     result.Add(Tuple.Create(entity.Geocode, populationChange, changePercent));
                 }
             }
         }
     }
     return result;
 }
示例#51
0
 internal void IterateEntitiesWithoutGeocode(PopulationDataEntryEvent iCallback, PopulationDataEntry parent)
 {
     if ( this.Geocode == 0 )
     {
         iCallback(this, parent);
     }
     if ( SubEntities != null )
     {
         foreach ( PopulationDataEntry lSubEntity in SubEntities )
         {
             if ( lSubEntity != null )
             {
                 lSubEntity.IterateEntitiesWithoutGeocode(iCallback, this);
             }
         }
     }
 }
示例#52
0
        internal static ConstituencyEntry Load(XmlNode iNode)
        {
            ConstituencyEntry RetVal = null;

            if ( iNode != null && iNode.Name.Equals("constituency") )
            {
                RetVal = new ConstituencyEntry();
                RetVal.Index = TambonHelper.GetAttributeOptionalInt(iNode, "index", 0);
                RetVal.NumberOfSeats = TambonHelper.GetAttributeOptionalInt(iNode, "numberofseats", 1);

                if ( iNode.HasChildNodes )
                {
                    foreach ( XmlNode lChildNode in iNode.ChildNodes )
                    {
                        if ( lChildNode.Name == "include" )
                        {
                            PopulationDataEntry lEntity = new PopulationDataEntry();
                            lEntity.Geocode = TambonHelper.GetAttributeOptionalInt(lChildNode, "geocode", 0);
                            foreach ( XmlNode lSubChildNode in lChildNode.ChildNodes )
                            {
                                if ( lSubChildNode.Name == "exclude" )
                                {
                                    PopulationDataEntry lExcludedEntity = new PopulationDataEntry();
                                    lExcludedEntity.Geocode = TambonHelper.GetAttributeOptionalInt(lSubChildNode, "geocode", 0);
                                    if ( !RetVal.ExcludedAdministrativeEntities.ContainsKey(lEntity) )
                                    {
                                        RetVal.ExcludedAdministrativeEntities[lEntity] = new List<PopulationDataEntry>();
                                    }
                                    RetVal.ExcludedAdministrativeEntities[lEntity].Add(lExcludedEntity);
                                }
                            }
                            RetVal.AdministrativeEntities.Add(lEntity);
                        }
                        if ( lChildNode.Name == "includesub" )
                        {
                            PopulationDataEntry lEntity = new PopulationDataEntry();
                            lEntity.Geocode = TambonHelper.GetAttributeOptionalInt(lChildNode, "geocode", 0);
                            foreach ( XmlNode lSubChildNode in lChildNode.ChildNodes )
                            {
                                if ( lSubChildNode.Name == "include" )
                                {
                                    PopulationDataEntry lIncludedEntity = new PopulationDataEntry();
                                    lIncludedEntity.Geocode = TambonHelper.GetAttributeOptionalInt(lSubChildNode, "geocode", 0);
                                    if ( !RetVal.SubIncludedAdministrativeEntities.ContainsKey(lEntity) )
                                    {
                                        RetVal.SubIncludedAdministrativeEntities[lEntity] = new List<PopulationDataEntry>();
                                    }
                                    RetVal.SubIncludedAdministrativeEntities[lEntity].Add(lIncludedEntity);
                                }
                            }
                        }
                    }
                }
            }
            return RetVal;
        }
示例#53
0
 internal void AddTambonInThesabanToAmphoe(PopulationDataEntry iTambon, PopulationDataEntry iThesaban)
 {
     PopulationDataEntry lMainTambon = FindByCodeAndType(iTambon.Geocode, EntityType.Tambon);
     PopulationDataEntry lMainAmphoe = FindByCode(iTambon.Geocode / 100);
     if ( lMainTambon == null )
     {
         if ( lMainAmphoe != null )
         {
             lMainTambon = (PopulationDataEntry)iTambon.Clone();
             lMainAmphoe.SubEntities.Add(lMainTambon);
         }
     }
     else
     {
         lMainTambon.Total = lMainTambon.Total + iTambon.Total;
         lMainTambon.Male = lMainTambon.Male + iTambon.Male;
         lMainTambon.Female = lMainTambon.Female + iTambon.Female;
         lMainTambon.Households = lMainTambon.Households + iTambon.Households;
     }
     if ( lMainAmphoe != null )
     {
         lMainAmphoe.Total = lMainAmphoe.Total + iTambon.Total;
         lMainAmphoe.Male = lMainAmphoe.Male + iTambon.Male;
         lMainAmphoe.Female = lMainAmphoe.Female + iTambon.Female;
         lMainAmphoe.Households = lMainAmphoe.Households + iTambon.Households;
     }
     if ( lMainTambon != null )
     {
         PopulationDataEntry lNewEntity = new PopulationDataEntry()
         {
             Geocode = iThesaban.Geocode,
             Name = iThesaban.Name,
             English = iThesaban.English,
             Type = iThesaban.Type,
             Total = iTambon.Total,
             Male = iTambon.Male,
             Female = iTambon.Female,
             Households = iTambon.Households
         };
         lMainTambon.SubEntities.Add(lNewEntity);
     }
 }
示例#54
0
        public string Information(PopulationDataEntry changwat)
        {
            StringBuilder builder = new StringBuilder();

            builder.AppendLine(StatisticsText(changwat));
            builder.AppendLine();

            foreach ( KeyValuePair<PopulationDataEntry, PopulationDataEntry> keyValuePair in DifferentMubanNames(changwat) )
            {
                builder.Append(keyValuePair.Key.Geocode.ToString());
                builder.Append(' ');
                builder.Append(TambonHelper.StripBanOrChumchon(keyValuePair.Key.Name));
                builder.Append(" instead of ");
                builder.AppendLine(TambonHelper.StripBanOrChumchon(keyValuePair.Value.Name));
            }

            String result = builder.ToString();
            return result;
        }
示例#55
0
        private void btnCheckNames_Click(object sender, EventArgs e)
        {
            PopulationDataEntry masterDataEntry = new PopulationDataEntry();
            Dictionary<String, String> romanizations = new Dictionary<String, String>();
            var romanizationMistakes = new List<Tuple<Int32, String, String, String>>();
            var romanizationSuggestions = new List<Tuple<Int32, String, String>>();
            var romanizationMissing = new List<Tuple<Int32, String>>();

            var provinceList = new List<PopulationDataEntry>();
            foreach ( PopulationDataEntry province in TambonHelper.ProvinceGeocodes )
            {
                PopulationData entities = TambonHelper.GetGeocodeList(province.Geocode);
                provinceList.Add(entities.Data);
            }
            masterDataEntry.SubEntities.AddRange(provinceList);
            foreach ( var entityToCheck in masterDataEntry.FlatList(EntityTypeHelper.AllEntityTypes) )
            {
                if ( !String.IsNullOrEmpty(entityToCheck.English) )
                {
                    String name = entityToCheck.Name;
                    String english = entityToCheck.English;
                    if ( (entityToCheck.Type == EntityType.Muban) | (entityToCheck.Type == EntityType.Chumchon) )
                    {
                        name = TambonHelper.StripBanOrChumchon(name);

                        if ( english.StartsWith("Ban ") )
                        {
                            english = english.Remove(0, "Ban ".Length).Trim();
                        }
                        if ( english.StartsWith("Chumchon ") )
                        {
                            english = english.Remove(0, "Chumchon ".Length).Trim();
                        }

                        if ( entityToCheck.Type == EntityType.Chumchon )
                        {
                            // Chumchon may have the name "Chumchon Ban ..."
                            name = TambonHelper.StripBanOrChumchon(name);

                            if ( english.StartsWith("Ban ") )
                            {
                                english = english.Remove(0, "Ban ".Length).Trim();
                            }
                        }
                    }
                    if ( romanizations.Keys.Contains(name) )
                    {
                        if ( romanizations[name] != english )
                        {
                            romanizationMistakes.Add(Tuple.Create(entityToCheck.Geocode, name, english, romanizations[name]));
                        }
                    }
                    else
                    {
                        romanizations[name] = english;
                    }
                }
            }

            foreach ( var entityToCheck in masterDataEntry.FlatList(EntityTypeHelper.AllEntityTypes) )
            {
                if ( String.IsNullOrEmpty(entityToCheck.English) )
                {
                    String foundEnglishName = String.Empty;
                    if ( romanizations.Keys.Contains(entityToCheck.Name) )
                    {
                        foundEnglishName = entityToCheck.Name;
                    }
                    else
                    {
                        var searchName = TambonHelper.StripBanOrChumchon(entityToCheck.Name);

                        if ( romanizations.Keys.Contains(searchName) )
                        {
                            foundEnglishName = searchName;
                        }
                        else
                        {
                            // Chumchon may have the name "Chumchon Ban ..."
                            searchName = TambonHelper.StripBanOrChumchon(searchName);
                            if ( romanizations.Keys.Contains(searchName) )
                            {
                                foundEnglishName = searchName;
                            }
                        }
                    }

                    if ( !String.IsNullOrEmpty(foundEnglishName) )
                    {
                        romanizationSuggestions.Add(Tuple.Create(entityToCheck.Geocode, entityToCheck.Name, romanizations[foundEnglishName]));
                    }
                    else
                    {
                        Boolean found = false;
                        String name = TambonHelper.StripBanOrChumchon(entityToCheck.Name);
                        name = TambonHelper.ReplaceThaiNumerals(name);
                        List<Char> numerals = new List<Char>() { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
                        foreach ( Char c in numerals )
                        {
                            name = name.Replace(c, ' ');
                        }
                        name = name.Trim();
                        foreach ( var keyValuePair in TambonHelper.NameSuffixRomanizations )
                        {
                            if ( entityToCheck.Name.EndsWith(keyValuePair.Key) )
                            {
                                String searchString = TambonHelper.StripBanOrChumchon(name.Substring(0, name.Length - keyValuePair.Key.Length));
                                if ( String.IsNullOrEmpty(searchString) )
                                {
                                    romanizationSuggestions.Add(Tuple.Create(entityToCheck.Geocode, entityToCheck.Name, keyValuePair.Value));
                                    found = true;
                                }
                                else if ( romanizations.Keys.Contains(searchString) )
                                {
                                    romanizationSuggestions.Add(Tuple.Create(entityToCheck.Geocode, entityToCheck.Name, romanizations[searchString] + " " + keyValuePair.Value));
                                    found = true;
                                }
                            }
                        }
                        if ( !found )
                        {
                            var prefixes = TambonHelper.NamePrefixRomanizations.Union(TambonHelper.NameSuffixRomanizations);
                            foreach ( var keyValuePair in prefixes )
                            {
                                if ( name.StartsWith(keyValuePair.Key) )
                                {
                                    String searchString = name.Substring(keyValuePair.Key.Length);
                                    if ( String.IsNullOrEmpty(searchString) )
                                    {
                                        romanizationSuggestions.Add(Tuple.Create(entityToCheck.Geocode, entityToCheck.Name, keyValuePair.Value));
                                        found = true;
                                    }
                                    else if ( romanizations.Keys.Contains(searchString) )
                                    {
                                        romanizationSuggestions.Add(Tuple.Create(entityToCheck.Geocode, entityToCheck.Name, keyValuePair.Value + " " + romanizations[searchString]));
                                        found = true;
                                    }
                                }
                            }
                        }
                        if ( !found )
                        {
                            romanizationMissing.Add(Tuple.Create(entityToCheck.Geocode, entityToCheck.Name));
                        }
                    }
                }
            }

            Int32 provinceFilter = 0;
            if ( cbxCheckNamesFiltered.Checked )
            {
                provinceFilter = GetCurrentChangwat().Geocode;
            }

            StringBuilder romanizationMistakesBuilder = new StringBuilder();
            Int32 romanizationMistakeCount = 0;
            foreach ( var entry in romanizationMistakes )
            {
                if ( TambonHelper.IsBaseGeocode(provinceFilter, entry.Item1) )
                {
                    romanizationMistakesBuilder.AppendLine(String.Format("{0} {1}: {2} vs. {3}", entry.Item1, entry.Item2, entry.Item3, entry.Item4));
                    romanizationMistakeCount++;
                }
            }

            if ( romanizationMistakeCount > 0 )
            {
                var lForm = new StringDisplayForm("Romanization problems (" + romanizationMistakeCount.ToString() + ")", romanizationMistakesBuilder.ToString());
                lForm.Show();
            }

            StringBuilder romanizationSuggestionBuilder = new StringBuilder();
            Int32 romanizationSuggestionCount = 0;
            foreach ( var entry in romanizationSuggestions )
            {
                if ( TambonHelper.IsBaseGeocode(provinceFilter, entry.Item1) )
                {
                    romanizationSuggestionBuilder.AppendLine(String.Format("{0} {1}: {2}", entry.Item1, entry.Item2, entry.Item3));
                    romanizationSuggestionCount++;
                }
            }
            if ( romanizationSuggestionCount > 0 )
            {
                var lForm = new StringDisplayForm("Romanization suggestions (" + romanizationSuggestionCount.ToString() + ")", romanizationSuggestionBuilder.ToString());
                lForm.Show();

                List<Tuple<String, String, Int32>> counter = new List<Tuple<String, String, Int32>>();
                foreach ( var entry in romanizationSuggestions )
                {
                    var found = counter.FirstOrDefault(x => x.Item1 == entry.Item2);
                    if ( found == null )
                    {
                        counter.Add(Tuple.Create(entry.Item2, entry.Item3, 1));
                    }
                    else
                    {
                        counter.Remove(found);
                        counter.Add(Tuple.Create(entry.Item2, entry.Item3, found.Item3 + 1));
                    }
                }
                counter.RemoveAll(x => x.Item3 < 2);
                if ( counter.Any() )
                {
                    counter.Sort(delegate(Tuple<String, String, Int32> p1, Tuple<String, String, Int32> p2)
                    {
                        return (p2.Item3.CompareTo(p1.Item3));
                    });

                    Int32 suggestionCounter = 0;
                    StringBuilder sortedBuilder = new StringBuilder();
                    foreach ( var entry in counter )
                    {
                        sortedBuilder.AppendLine(String.Format("{0}: {1} ({2})", entry.Item1, entry.Item2, entry.Item3));
                        suggestionCounter += entry.Item3;
                    }
                    var lForm2 = new StringDisplayForm(
                        String.Format("Romanization suggestions ({0} of {1})", suggestionCounter, romanizationSuggestionCount),
                        sortedBuilder.ToString());
                    lForm2.Show();
                }
            }

            if ( romanizationMissing.Any() )
            {
                List<Tuple<String, Int32>> counter = new List<Tuple<String, Int32>>();
                foreach ( var entry in romanizationMissing )
                {
                    var found = counter.FirstOrDefault(x => x.Item1 == entry.Item2);
                    if ( found == null )
                    {
                        counter.Add(Tuple.Create(entry.Item2, 1));
                    }
                    else
                    {
                        counter.Remove(found);
                        counter.Add(Tuple.Create(entry.Item2, found.Item2 + 1));
                    }
                }
                // counter.RemoveAll(x => x.Item2 < 2);
                if ( counter.Any() )
                {
                    counter.Sort(delegate(Tuple<String, Int32> p1, Tuple<String, Int32> p2)
                    {
                        var result = p2.Item2.CompareTo(p1.Item2);
                        if ( result == 0 )
                        {
                            result = p2.Item1.CompareTo(p1.Item1);
                        }
                        return result;
                    });

                    Int32 missingCounter = 0;
                    StringBuilder sortedBuilder = new StringBuilder();
                    foreach ( var entry in counter )
                    {
                        sortedBuilder.AppendLine(String.Format("{0}: {1}", entry.Item1, entry.Item2));
                        missingCounter += entry.Item2;
                    }
                    var lForm2 = new StringDisplayForm(
                        String.Format("Romanization missing ({0} of {1})", missingCounter, romanizationMissing.Count),
                        sortedBuilder.ToString());
                    lForm2.Show();
                }
            }
        }