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(); } }
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); }
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); }
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); }
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(); } }
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()); } }
private void RemoveKnownGeocodes(PopulationDataEntry iEntry) { if (iEntry.Geocode != 0) { PopulationDataEntry lFoundEntry = mGeocodes.FindByCode(iEntry.Geocode); if (lFoundEntry != null) { // TODO: Check for spelling changes mGeocodes.SubEntities.Remove(lFoundEntry); } } if ((!EntityTypeHelper.Thesaban.Contains(iEntry.Type)) && (iEntry.Type != EntityType.Tambon) && (iEntry.Type != EntityType.Khwaeng)) { foreach (PopulationDataEntry lEntry in iEntry.SubEntities) { RemoveKnownGeocodes(lEntry); } } }
private String CalculateData(Int32 iGeocode) { String lResult = String.Empty; PopulationDataEntry lEntry = null; if (iGeocode == 0) { lEntry = mData; } else { lEntry = mData.FindByCode(iGeocode); } if (lEntry != null) { List <PopulationDataEntry> lList = lEntry.FlatList(new List <EntityType>() { EntityType.Bangkok, EntityType.Changwat, EntityType.Amphoe, EntityType.KingAmphoe, EntityType.Khet }); lList.Add(lEntry); FrequencyCounter lCounter = new FrequencyCounter(); Int32 lSeats = 0; foreach (PopulationDataEntry lSubEntry in lList) { foreach (ConstituencyEntry lConstituency in lSubEntry.ConstituencyList) { lCounter.IncrementForCount(lConstituency.Population() / lConstituency.NumberOfSeats, lSubEntry.Geocode * 100 + lConstituency.Index); lSeats += lConstituency.NumberOfSeats; } } StringBuilder lBuilder = new StringBuilder(); lBuilder.AppendLine("Number of constituencies: " + lCounter.NumberOfValues.ToString()); lBuilder.AppendLine("Number of seats: " + lSeats.ToString()); if (lCounter.NumberOfValues > 0) { lBuilder.AppendLine("Mean population per seat: " + Math.Round(lCounter.MeanValue).ToString()); lBuilder.AppendLine("Standard deviation: " + Math.Round(lCounter.StandardDeviation).ToString()); lBuilder.AppendLine("Maximum population per seat: " + lCounter.MaxValue.ToString()); foreach (var lSubEntry in lCounter.Data[lCounter.MaxValue]) { lBuilder.AppendLine(" " + GetEntityConstituencyName(lSubEntry)); } lBuilder.AppendLine("Minimum population per seat: " + lCounter.MinValue.ToString()); foreach (var lSubEntry in lCounter.Data[lCounter.MinValue]) { lBuilder.AppendLine(" " + GetEntityConstituencyName(lSubEntry)); } } lBuilder.AppendLine(); foreach (PopulationDataEntry lSubEntry in lList) { foreach (ConstituencyEntry lConstituency in lSubEntry.ConstituencyList) { lBuilder.AppendLine( GetEntityConstituencyName(lSubEntry.Geocode * 100 + lConstituency.Index) + ": " + lConstituency.Population() / lConstituency.NumberOfSeats); } } lResult = lBuilder.ToString(); } return(lResult); }
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; }
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; }
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(); } }
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; }
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); }
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(); } }
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()); } }