public static PopulationData Load(XmlNode node) { PopulationData result = null; if (node != null) { result = new PopulationData(); foreach (XmlNode childNode in node.ChildNodes) { if (childNode.Name == "year") { result.Year = Convert.ToInt32(childNode.Attributes.GetNamedItem("value")); foreach (XmlNode llNode in childNode.ChildNodes) { result.DoLoad(llNode); } } else { result.DoLoad(childNode); } } } if (result._changwat != null) { result._geocode = result._changwat.Geocode; } return(result); }
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 List <PopulationDataEntry> ThesabanWithoutConstituencies() { List <PopulationDataEntry> lResult = new List <PopulationDataEntry>(); PopulationData lGeocodes = TambonHelper.GetGeocodeList(mGeocode); lGeocodes.ReOrderThesaban(); foreach (PopulationDataEntry lEntry in lGeocodes.Thesaban) { if (EntityTypeHelper.Thesaban.Contains(lEntry.Type) & (!lEntry.Obsolete)) { Boolean lSuccess = HasConstituencyAnnouncement(lEntry.Geocode, lEntry.Type); if ((!lSuccess) & (lEntry.GeocodeOfCorrespondingTambon != 0)) { lSuccess = HasConstituencyAnnouncement(lEntry.GeocodeOfCorrespondingTambon, lEntry.Type); } if (!lSuccess) { lResult.Add(lEntry); } } } return(lResult); }
public static PopulationData Load(String fromFile) { StreamReader reader = null; XmlDocument xmlDoc = null; PopulationData result = null; try { if (!String.IsNullOrEmpty(fromFile) && File.Exists(fromFile)) { reader = new StreamReader(fromFile); xmlDoc = new XmlDocument(); xmlDoc.LoadXml(reader.ReadToEnd()); result = PopulationData.Load(xmlDoc); } } finally { if (reader != null) { reader.Close(); } } return(result); }
private void GetProvinceData() { Data = new Entity(); var populationJsonData = GetDataFromDopa(_geocode, DopaStatisticsType.Population); Data.entity.AddRange(ParsePopulationJson(populationJsonData)); foreach (var entity in Data.entity) { var subPopulationData = GetDataFromDopa(entity.geocode, DopaStatisticsType.Population); entity.entity.AddRange(ParsePopulationJson(subPopulationData)); if (_geocode > 10 && _downloadMuban) // Bangkok has no Muban { foreach (var tambonEntity in entity.entity) { var mubanPopulationData = GetDataFromDopa(TambonGeocodeForDataRetrieval(tambonEntity.geocode, entity.geocode), DopaStatisticsType.Population); tambonEntity.entity.AddRange(ParsePopulationJson(mubanPopulationData)); } } } Data.geocode = _geocode; Data.population = new List <PopulationData>(); PopulationData populationData = CreateEmptyPopulationEntry(); Data.population.Add(populationData); AddOtherData(populationData, _geocode, DopaStatisticsType.Birth); AddOtherData(populationData, _geocode, DopaStatisticsType.Death); AddOtherData(populationData, _geocode, DopaStatisticsType.MoveIn); AddOtherData(populationData, _geocode, DopaStatisticsType.MoveOut); }
public Dictionary <String, EntityLeader> NewGovernorsList() { Dictionary <String, EntityLeader> RetVal = new Dictionary <String, EntityLeader>(); List <PopulationDataEntry> lFoundEntries = new List <PopulationDataEntry>(); foreach (PopulationDataEntry lEntry in mData) { lEntry.Geocode = TambonHelper.GetGeocode(lEntry.Name); PopulationData lData = TambonHelper.GetGeocodeList(lEntry.Geocode); lEntry.English = lData.Data.English; foreach (EntityOffice lOffice in lEntry.Offices) { foreach (EntityLeader lLeader in lOffice.OfficialsList) { if (lLeader.Position == EntityLeaderType.Governor) { if (lData.Data.LeaderAlreadyInList(lLeader)) { lFoundEntries.Add(lEntry); } else { RetVal.Add(lEntry.English, lLeader); } } } } } foreach (PopulationDataEntry lEntry in lFoundEntries) { mData.Remove(lEntry); } return(RetVal); }
private void CheckPopulationData(PopulationData data) { var entitiesWithoutGeocode = data.EntitiesWithoutGeocode(); if (entitiesWithoutGeocode.Count > 0) { StringBuilder builder = new StringBuilder(); foreach (Tuple <PopulationDataEntry, Int32> entry in entitiesWithoutGeocode) { builder.AppendLine(String.Format("{0} ({1}, Parent {2}, {3} people in {4} households)", entry.Item1.Name, entry.Item1.Type, entry.Item2, entry.Item1.Total, entry.Item1.Households)); } MessageBox.Show(entitiesWithoutGeocode.Count.ToString() + " entities without geocode in " + data.Year.ToString() + Environment.NewLine + builder.ToString()); } var entitiesWithInvalidGeocode = data.EntitiesWithInvalidGeocode(); if (entitiesWithInvalidGeocode.Count > 0) { String message = ""; foreach (PopulationDataEntry lEntry in entitiesWithInvalidGeocode) { message = message + lEntry.Geocode.ToString() + ' ' + lEntry.Name + Environment.NewLine; } MessageBox.Show(entitiesWithInvalidGeocode.Count.ToString() + " entities with invalid geocode in " + data.Year.ToString() + Environment.NewLine + message); } }
private void ShowPopulationDialog(PopulationData iData) { var lDataForm = new PopulationDataView(); lDataForm.ShowGazette += ShowGazetteDialog; lDataForm.Data = iData; lDataForm.Show(); }
public void RemoveAllKnownGeocodes() { foreach (var lEntry in TambonHelper.ProvinceGeocodes) { PopulationData lCurrentList = TambonHelper.GetGeocodeList(lEntry.Geocode); RemoveKnownGeocodes(lCurrentList.Data); } }
protected void GetGeocodes() { if (_changwat != null) { PopulationData geocodes = TambonHelper.GetGeocodeList(_changwat.Geocode); _invalidGeocodes = geocodes.Data.InvalidGeocodeEntries(); Data.GetCodes(geocodes.Data); } }
public static Dictionary <PopulationDataEntry, Int32> Calculate(Int32 parentGeocode, Int32 year, Int32 numberOfSeats) { Dictionary <PopulationDataEntry, Int32> retval = null; PopulationData downloader = new PopulationData(year, parentGeocode); downloader.Process(); retval = Calculate(downloader.Data, year, numberOfSeats); return(retval); }
private PopulationData CreateEmptyPopulationEntry() { PopulationData population = new PopulationData(); population.source = PopulationDataSourceType.DOPA; population.referencedate = new DateTime(Year, 12, 31); population.referencedateSpecified = true; population.year = Year.ToString(CultureInfo.InvariantCulture); return(population); }
/// <summary> /// Loads population data from a XML file. /// </summary> /// <param name="fromFile">File name.</param> /// <returns>Population data.</returns> public static PopulationData Load(String fromFile) { PopulationData result = null; using (var fileStream = new FileStream(fromFile, FileMode.Open, FileAccess.Read)) { result = XmlManager.XmlToEntity <PopulationData>(fileStream, new XmlSerializer(typeof(PopulationData))); } return(result); }
private void btnPopulation_Click(object sender, EventArgs e) { PopulationData downloader = new PopulationData(Convert.ToInt32(edtYear.Value), GetCurrentChangwat().Geocode); Thread downloadThread = new Thread(downloader.Process); downloader.ProcessingFinished += PopulationDataReadyCheck; downloader.ProcessingFinished += PopulationDataReadyShow; downloadThread.Name = "Worker Thread Population " + GetCurrentChangwat().English + " " + downloader.Year.ToString(); downloadThread.Start(); }
private void SetData(PopulationData value) { mData = value; if (value != null) { if (mData.Data != null) { this.Text = mData.Data.English + " " + mData.Year.ToString(); } PopulationDataToTreeView(mTreeviewData, mData); } }
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 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; }
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); }
private void btnPopulationDownloadAll_click(object sender, EventArgs e) { var lChangwat = (PopulationDataEntry)cbx_changwat.SelectedItem; Int32 lGeocode = lChangwat.Geocode; for (int lYear = TambonHelper.PopulationStatisticMinYear; lYear <= TambonHelper.PopulationStatisticMaxYear; lYear++) { PopulationData downloader = new PopulationData(lYear, lGeocode); downloader.ProcessingFinished += PopulationDataReadyCheck; Thread t = new Thread(downloader.Process); t.Name = "Worker Thread Population " + lChangwat.English + " " + downloader.Year.ToString(); t.Start(); } }
private List <PopulationDataEntry> LoadGeocodeLists() { var lList = new List <PopulationDataEntry>(); foreach (PopulationDataEntry lEntry in TambonHelper.ProvinceGeocodes) { if (TambonHelper.IsBaseGeocode(BaseGeocode, lEntry.Geocode)) { PopulationData lEntities = TambonHelper.GetGeocodeList(lEntry.Geocode); lList.AddRange(lEntities.Data.FlatList(mEntityTypes)); } } return(lList); }
private void PopulationDataToTreeView(TreeView iTreeView, PopulationData iData) { iTreeView.BeginUpdate(); iTreeView.Nodes.Clear(); if (iData != null) { TreeNode lNode = PopulationDataEntryToTreeNode(iData.Data); if (lNode != null) { iTreeView.Nodes.Add(lNode); } } iTreeView.EndUpdate(); }
private void AddOtherData(PopulationData populationData, UInt32 geocode, DopaStatisticsType dataType) { var jsonData = GetDataFromDopa(geocode, dataType); if (populationData.register == null) { populationData.register = new RegisterData(); } PopulationChangeEntry otherData = ParseAdditionalJson(jsonData); otherData.type = _populationChangeType[dataType]; populationData.register.change.Add(otherData); }
private IEnumerable <Entity> ParseJson(JsonObject data) { var result = new List <Entity>(); var actualData = data.get("aaData"); if (actualData != null) { var array = actualData.asArray(); foreach (JsonArray item in array) { var parsedData = new List <String>(); foreach (JsonValue dataPoint in item) { var strippedText = Regex.Replace(dataPoint.asString(), "<.*?>", string.Empty).Replace(",", String.Empty); if (strippedText == "-") { strippedText = "0"; } parsedData.Add(strippedText); } var firstLine = parsedData.First(); if (!String.IsNullOrWhiteSpace(firstLine) && (firstLine != "00")) { Entity entity = new Entity(); entity.ParseName(parsedData.ElementAt(1).Replace("ท้องถิ่น", String.Empty).Trim()); entity.geocode = Convert.ToUInt32(firstLine, CultureInfo.InvariantCulture); while (entity.geocode % 100 == 0) { entity.geocode = entity.geocode / 100; } PopulationData population = CreateEmptyPopulationEntry(); entity.population.Add(population); HouseholdDataPoint householdDataPoint = new HouseholdDataPoint(); householdDataPoint.male = Convert.ToInt32(parsedData.ElementAt(2), CultureInfo.InvariantCulture); householdDataPoint.female = Convert.ToInt32(parsedData.ElementAt(3), CultureInfo.InvariantCulture); householdDataPoint.total = Convert.ToInt32(parsedData.ElementAt(4), CultureInfo.InvariantCulture); householdDataPoint.households = Convert.ToInt32(parsedData.ElementAt(5), CultureInfo.InvariantCulture); population.data.Add(householdDataPoint); if ((householdDataPoint.total > 0) || (householdDataPoint.households > 0)) { // occasionally there are empty entries, e.g. for 3117 includes an empty 311102 result.Add(entity); } } } } return(result); }
protected void GetData() { Data = new Entity(); var data = GetDataFromDopa(_geocode); Data.entity.AddRange(ParseJson(data)); foreach (var entity in Data.entity) { var subData = GetDataFromDopa(entity.geocode); entity.entity.AddRange(ParseJson(subData)); } Data.geocode = _geocode; Data.population = new List <PopulationData>(); PopulationData populationData = CreateEmptyPopulationEntry(); Data.population.Add(populationData); }
/// <summary> /// Gets the data for all provinces, used if <see cref="_geocode"/> is 0. /// </summary> private void ProcessAllProvinces() { Data = new Entity(); Data.population = new List <PopulationData>(); PopulationData populationData = CreateEmptyPopulationEntry(); Data.population.Add(populationData); Data.type = EntityType.Country; Data.CopyBasicDataFrom(GlobalData.CountryEntity); foreach (var entry in GlobalData.Provinces) { var tempCalculator = new PopulationDataDownloader(Year, entry.geocode); tempCalculator.Process(); Data.entity.Add(tempCalculator.Data); } Data.CalculatePopulationFromSubEntities(Year, PopulationDataSourceType.DOPA); }
public static PopulationDataEntry GetCountryPopulationData(Int32 year) { PopulationDataEntry country = null; if (TambonHelper.PopulationDataByYear.Keys.Contains(year)) { country = TambonHelper.PopulationDataByYear[year]; } else { PopulationData downloader = new PopulationData(year, 0); downloader.Process(); downloader.ReOrderThesaban(); country = downloader.Data; TambonHelper.PopulationDataByYear[year] = country; } return(country); }
internal void AddTambonInThesabanToAmphoe(Entity tambon, Entity thesaban) { var allSubEntities = entity.SelectMany(x => x.entity).ToList(); var mainTambon = allSubEntities.SingleOrDefault(x => (GeocodeHelper.IsSameGeocode(x.geocode, tambon.geocode, false)) & (x.type == tambon.type)); var mainAmphoe = entity.FirstOrDefault(x => (x.geocode == tambon.geocode / 100)); if (mainTambon == null) { if (mainAmphoe != null) { mainTambon = XmlManager.MakeClone <Entity>(tambon); mainAmphoe.entity.Add(mainTambon); } } else { if (mainTambon.population.Any()) { mainTambon.population.First().data.AddRange(tambon.population.First().data); } else { mainTambon.population.Add(tambon.population.First()); } } if (mainAmphoe != null) { var population = tambon.population.First(); foreach (var dataPoint in population.data) { var amphoePopulation = mainAmphoe.population.FirstOrDefault(); if (amphoePopulation == null) { amphoePopulation = new PopulationData(); amphoePopulation.referencedate = population.referencedate; amphoePopulation.referencedateSpecified = population.referencedateSpecified; amphoePopulation.source = population.source; amphoePopulation.year = population.year; mainAmphoe.population.Add(amphoePopulation); } amphoePopulation.AddDataPoint(dataPoint); } } }
/// <summary> /// Returns the tree of administrative subdivisions for a given province. /// </summary> /// <param name="provinceCode">TIS1099 code of the province.</param> /// <exception cref="ArgumentOutOfRangeException">Thrown if <cref>provinceCode</cref> does not refer to a valid province.</exception> /// <returns>Tree of subdivisions.</returns> /// <remarks>Internally caches a clone of the returned value, to load the file from disc only once.</remarks> static public PopulationData GetGeocodeList(Int32 provinceCode) { PopulationData result = null; if (!ProvinceGeocodes.Any(entry => entry.Geocode == provinceCode)) { throw new ArgumentOutOfRangeException("provinceCode"); } if (_GeocodeCache.Keys.Contains(provinceCode)) { result = new PopulationData((PopulationDataEntry)(_GeocodeCache[provinceCode].Clone())); } else { String fileName = TambonHelper.GeocodeSourceFile(provinceCode); if (File.Exists(fileName)) { result = PopulationData.Load(fileName); _GeocodeCache.Add(provinceCode, (PopulationDataEntry)(result.Data.Clone())); } } return(result); }
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 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; }
public static PopulationDataEntry GetCountryPopulationData(Int32 year) { PopulationDataEntry country = null; if ( TambonHelper.PopulationDataByYear.Keys.Contains(year) ) { country = TambonHelper.PopulationDataByYear[year]; } else { PopulationData downloader = new PopulationData(year, 0); downloader.Process(); downloader.ReOrderThesaban(); country = downloader.Data; TambonHelper.PopulationDataByYear[year] = country; } return country; }
private void btnPopulationDownloadAll_click(object sender, EventArgs e) { var lChangwat = (PopulationDataEntry)cbx_changwat.SelectedItem; Int32 lGeocode = lChangwat.Geocode; for ( int lYear = TambonHelper.PopulationStatisticMinYear ; lYear <= TambonHelper.PopulationStatisticMaxYear ; lYear++ ) { PopulationData downloader = new PopulationData(lYear, lGeocode); downloader.ProcessingFinished += PopulationDataReadyCheck; Thread t = new Thread(downloader.Process); t.Name = "Worker Thread Population " + lChangwat.English + " " + downloader.Year.ToString(); t.Start(); } }
private void CheckPopulationData(PopulationData data) { var entitiesWithoutGeocode = data.EntitiesWithoutGeocode(); if ( entitiesWithoutGeocode.Count > 0 ) { StringBuilder builder = new StringBuilder(); foreach ( Tuple<PopulationDataEntry, Int32> entry in entitiesWithoutGeocode ) { builder.AppendLine(String.Format("{0} ({1}, Parent {2}, {3} people in {4} households)", entry.Item1.Name, entry.Item1.Type, entry.Item2, entry.Item1.Total, entry.Item1.Households)); } MessageBox.Show(entitiesWithoutGeocode.Count.ToString() + " entities without geocode in " + data.Year.ToString() + Environment.NewLine + builder.ToString()); } var entitiesWithInvalidGeocode = data.EntitiesWithInvalidGeocode(); if ( entitiesWithInvalidGeocode.Count > 0 ) { String message = ""; foreach ( PopulationDataEntry lEntry in entitiesWithInvalidGeocode ) { message = message + lEntry.Geocode.ToString() + ' ' + lEntry.Name + Environment.NewLine; } MessageBox.Show(entitiesWithInvalidGeocode.Count.ToString() + " entities with invalid geocode in " + data.Year.ToString() + Environment.NewLine + message); } }
public void AddPopulationDataQualifiers(Statement statement, PopulationData data) { var pointInTimeQualifier = new Qualifier(statement, SnakType.Value, new EntityId(WikiBase.PropertyIdPointInTime), TimeValue.DateValue(data.referencedate)); // statement.Qualifiers.Add(pointInTimeQualifier); // already added by the constructor var method = String.Empty; switch ( data.source ) { case PopulationDataSourceType.Census: method = WikiBase.ItemCensuses; break; case PopulationDataSourceType.DOPA: method = WikiBase.ItemRegistration; break; } if ( !String.IsNullOrEmpty(method) ) { var methodQualifier = new Qualifier(statement, SnakType.Value, new EntityId(WikiBase.PropertyIdDeterminationMethod), new EntityIdValue(new EntityId(method))); // statement.Qualifiers.Add(methodQualifier); // already added by the constructor } }
private WikiDataState PopulationData(Item item, PopulationData data, Boolean createStatement, Boolean overrideWrongData, out Statement statement) { var total = data.data.FirstOrDefault(y => y.type == PopulationDataType.total); var propertyName = WikiBase.PropertyIdPopulation; WikiDataState result = WikiDataState.Unknown; // Statement claim = item.Claims.FirstOrDefault(x => x.IsAboutProperty(WikiBase.PropertyIdCountry)) as Statement; var property = new EntityId(propertyName); var propertyPointInTime = new EntityId(WikiBase.PropertyIdPointInTime); var claimsForProperty = item.Claims.Where(x => property.Equals(x.mainSnak.PropertyId)); Statement claim = claimsForProperty.FirstOrDefault( x => x.Qualifiers.Any( y => y.PropertyId.Equals(propertyPointInTime) && y.DataValue is TimeValue && (y.DataValue as TimeValue).DateTime.Year == data.Year)) as Statement; var dataValue = new QuantityValue(total.total); var snak = new Snak(SnakType.Value, new EntityId(propertyName), dataValue); if ( claim == null ) { result = WikiDataState.NotSet; if ( createStatement ) { claim = item.createStatementForSnak(snak); } } else { Snak oldSnak = claim.mainSnak; var oldDataValue = snak.DataValue as QuantityValue; if ( oldDataValue.Equals(dataValue) ) { result = WikiDataState.Valid; } else { result = WikiDataState.WrongValue; if ( overrideWrongData ) { claim.mainSnak = snak; } } } statement = claim as Statement; return result; }
/// <summary> /// Returns the tree of administrative subdivisions for a given province. /// </summary> /// <param name="provinceCode">TIS1099 code of the province.</param> /// <exception cref="ArgumentOutOfRangeException">Thrown if <cref>provinceCode</cref> does not refer to a valid province.</exception> /// <returns>Tree of subdivisions.</returns> /// <remarks>Internally caches a clone of the returned value, to load the file from disc only once.</remarks> public static PopulationData GetGeocodeList(Int32 provinceCode) { PopulationData result = null; if ( !ProvinceGeocodes.Any(entry => entry.Geocode == provinceCode) ) { throw new ArgumentOutOfRangeException("provinceCode"); } if ( _GeocodeCache.Keys.Contains(provinceCode) ) { result = new PopulationData((PopulationDataEntry)(_GeocodeCache[provinceCode].Clone())); } else { String fileName = TambonHelper.GeocodeSourceFile(provinceCode); if ( File.Exists(fileName) ) { result = PopulationData.Load(fileName); _GeocodeCache.Add(provinceCode, (PopulationDataEntry)(result.Data.Clone())); } } return result; }
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(); } } }
private void PopulationDataToTreeView(TreeView iTreeView, PopulationData iData) { iTreeView.BeginUpdate(); iTreeView.Nodes.Clear(); if ( iData != null ) { TreeNode lNode = PopulationDataEntryToTreeNode(iData.Data); if ( lNode != null ) { iTreeView.Nodes.Add(lNode); } } iTreeView.EndUpdate(); }
private void SetData(PopulationData value) { mData = value; if ( value != null ) { if ( mData.Data != null ) { this.Text = mData.Data.English + " " + mData.Year.ToString(); } PopulationDataToTreeView(mTreeviewData, mData); } }
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(); }
/// <summary> /// Calculates the population for each of the local governments. /// </summary> /// <param name="localGovernments">Local governments to calculate.</param> /// <param name="allTambon">All tambon covered by the local governments.</param> /// <param name="populationDataSource">Data source of the population data.</param> /// <param name="populationYear">Reference year of the population data.</param> public static void CalculateLocalGovernmentPopulation(IEnumerable<Entity> localGovernments, IEnumerable<Entity> allTambon, PopulationDataSourceType populationDataSource, Int16 populationYear) { foreach ( var localEntityWithoutPopulation in localGovernments.Where(x => x.LocalGovernmentAreaCoverage.Any() && !x.population.Any( y => y.Year == populationYear && y.source == populationDataSource)) ) { var populationData = new PopulationData(); localEntityWithoutPopulation.population.Add(populationData); foreach ( var coverage in localEntityWithoutPopulation.LocalGovernmentAreaCoverage ) { var tambon = allTambon.Single(x => x.geocode == coverage.geocode); var sourcePopulationData = tambon.population.FirstOrDefault(y => y.Year == populationYear && y.source == populationDataSource); if ( sourcePopulationData != null ) { populationData.year = sourcePopulationData.year; populationData.referencedate = sourcePopulationData.referencedate; populationData.referencedateSpecified = sourcePopulationData.referencedateSpecified; populationData.source = sourcePopulationData.source; List<HouseholdDataPoint> dataPointToClone = new List<HouseholdDataPoint>(); dataPointToClone.AddRange(sourcePopulationData.data.Where(x => x.geocode == localEntityWithoutPopulation.geocode)); if ( !dataPointToClone.Any() ) { if ( coverage.coverage == CoverageType.completely ) { dataPointToClone.AddRange(sourcePopulationData.data); } else { dataPointToClone.AddRange(sourcePopulationData.data.Where(x => x.type == PopulationDataType.nonmunicipal)); } } foreach ( var dataPoint in dataPointToClone ) { var newDataPoint = new HouseholdDataPoint(); newDataPoint.male = dataPoint.male; newDataPoint.female = dataPoint.female; newDataPoint.households = dataPoint.households; newDataPoint.total = dataPoint.total; newDataPoint.geocode = coverage.geocode; newDataPoint.type = dataPoint.type; populationData.data.Add(newDataPoint); } } } if ( populationData.data.Count == 1 ) { populationData.data.First().type = PopulationDataType.total; } populationData.CalculateTotal(); } }
private void btnCheckCensus_Click(object sender, EventArgs e) { Int16 year = Convert.ToInt16(cbxCensusYears.SelectedItem as String); var builder = new StringBuilder(); var baseEntity = GlobalData.CompleteGeocodeList(); var populationData = GlobalData.LoadPopulationDataUnprocessed(PopulationDataSourceType.Census, year); var allEntities = populationData.FlatList().Where(x => x.population.Any(y => y.source == PopulationDataSourceType.Census && y.Year == year)).ToList(); foreach ( var entity in allEntities ) { var population = entity.population.First(y => y.source == PopulationDataSourceType.Census && y.Year == year); Int32 diff = 0; foreach ( var data in population.data ) { if ( data.male != 0 && data.female != 0 ) { diff = Math.Abs(data.total - data.male - data.female); if ( diff > 1 ) { builder.AppendFormat("{0} ({1}): {2} differs male/female to total by {3}", entity.english, entity.geocode, data.type, diff); builder.AppendLine(); } } foreach ( var subData in data.data ) { if ( data.male != 0 && data.female != 0 ) { diff = Math.Abs(data.total - data.male - data.female); if ( diff > 1 ) { builder.AppendFormat("{0} ({1}): {2} of {3} differs male/female to total by {4}", entity.english, entity.geocode, subData.type, data.type, diff); builder.AppendLine(); } } } } diff = population.SumError(); if ( diff > 1 ) { builder.AppendFormat("{0} ({1}): Sum of parts differs by {2}", entity.english, entity.geocode, diff); builder.AppendLine(); } var sum = new PopulationData(); foreach ( var subEntity in entity.entity.Where(x => !x.IsObsolete && x.population.Any()) ) { var populationToAdd = subEntity.population.FirstOrDefault(y => y.source == PopulationDataSourceType.Census && y.Year == year); if ( populationToAdd != null ) { foreach ( var dataPoint in populationToAdd.data ) { sum.AddDataPoint(dataPoint); } } } if ( sum.data.Any() ) { diff = sum.TotalPopulation.MaxDeviation(entity.population.First(y => y.source == PopulationDataSourceType.Census && y.Year == year).TotalPopulation); if ( diff > 1 ) { builder.AppendFormat("{0} ({1}): Sum of subentities differs by {2}", entity.english, entity.geocode, diff); builder.AppendLine(); } } } var result = builder.ToString(); var formCensusProblems = new StringDisplayForm("Census data problems", result); formCensusProblems.Show(); }
public void AddPopulationDataReferences(Statement statement, PopulationData data, Entity entity) { Reference reference = null; Snak snak; switch ( data.source ) { case PopulationDataSourceType.Census: var statedInItem = String.Empty; if ( WikiBase.ItemCensus.Keys.Contains(data.Year) ) { statedInItem = WikiBase.ItemCensus[data.Year]; } snak = new Snak(SnakType.Value, new EntityId(WikiBase.PropertyIdStatedIn), new EntityIdValue(new EntityId(statedInItem))); reference = statement.CreateReferenceForSnak(snak); foreach ( var refItem in data.reference ) { var urlReference = refItem as MyUri; if ( urlReference != null ) { reference.AddSnak(new Snak(SnakType.Value, new EntityId(WikiBase.PropertyIdReferenceUrl), new StringValue(urlReference.Value))); } } break; case PopulationDataSourceType.DOPA: Uri source = PopulationDataDownloader.GetDisplayUrl(data.Year, entity.geocode); snak = new Snak(SnakType.Value, new EntityId(WikiBase.PropertyIdReferenceUrl), new StringValue(source.AbsoluteUri)); reference = statement.CreateReferenceForSnak(snak); reference.AddSnak(new Snak(SnakType.Value, new EntityId(WikiBase.PropertyIdPublisher), new EntityIdValue(new EntityId(WikiBase.ItemDopa)))); break; } if ( reference != null ) { statement.AddReference(reference); } }
public WikiDataState PopulationDataCorrect(Item item, PopulationData data) { if ( item == null ) throw new ArgumentNullException("item"); Statement dummy; return PopulationData(item, data, false, false, out dummy); }
public Statement SetPopulationData(Item item, PopulationData data, Boolean overrideWrongData) { if ( item == null ) throw new ArgumentNullException("item"); Statement result; PopulationData(item, data, true, overrideWrongData, out result); return result; }
internal void AddTambonInThesabanToAmphoe(Entity tambon, Entity thesaban) { var allSubEntities = entity.SelectMany(x => x.entity).ToList(); var mainTambon = allSubEntities.SingleOrDefault(x => (GeocodeHelper.IsSameGeocode(x.geocode, tambon.geocode, false)) & (x.type == tambon.type)); var mainAmphoe = entity.FirstOrDefault(x => (x.geocode == tambon.geocode / 100)); if ( mainTambon == null ) { if ( mainAmphoe != null ) { mainTambon = XmlManager.MakeClone<Entity>(tambon); mainAmphoe.entity.Add(mainTambon); } } else { if ( mainTambon.population.Any() ) { mainTambon.population.First().data.AddRange(tambon.population.First().data); } else { mainTambon.population.Add(tambon.population.First()); } } if ( mainAmphoe != null ) { var population = tambon.population.First(); foreach ( var dataPoint in population.data ) { var amphoePopulation = mainAmphoe.population.FirstOrDefault(); if ( amphoePopulation == null ) { amphoePopulation = new PopulationData(); amphoePopulation.referencedate = population.referencedate; amphoePopulation.referencedateSpecified = population.referencedateSpecified; amphoePopulation.source = population.source; amphoePopulation.year = population.year; mainAmphoe.population.Add(amphoePopulation); } amphoePopulation.AddDataPoint(dataPoint); } } }
private PopulationData CreateEmptyPopulationEntry() { PopulationData population = new PopulationData(); population.source = PopulationDataSourceType.DOPA; population.referencedate = new DateTime(Year, 12, 31); population.referencedateSpecified = true; population.year = Year.ToString(CultureInfo.InvariantCulture); return population; }