/// <summary> /// Creates a new instance of PopulationData. /// </summary> public PopulationData() { this.referenceField = new List <object>(); this.registerField = new RegisterData(); this.dataField = new List <HouseholdDataPoint>(); this.sourceField = PopulationDataSourceType.Unknown; }
public static Entity LoadPopulationDataUnprocessed(PopulationDataSourceType source, Int16 year) { Entity result = null; if (!GlobalData.CountryEntity.population.Any(x => x.Year == year && x.source == source)) { String filename = String.Empty; switch (source) { case PopulationDataSourceType.Census: filename = BaseXMLDirectory + "\\population\\census{0}.xml"; break; case PopulationDataSourceType.DOPA: filename = BaseXMLDirectory + "\\population\\DOPA{0}.xml"; break; } filename = String.Format(CultureInfo.InvariantCulture, filename, year); if (!string.IsNullOrWhiteSpace(filename)) { result = LoadPopulationData(filename); } } return(result); }
/// <summary> /// Calculates the population data by summing up the data of the subentities. /// </summary> /// <param name="year">Year of data.</param> /// <param name="dataSource">Data source.</param> public void CalculatePopulationFromSubEntities(Int32 year, PopulationDataSourceType dataSource) { foreach (var subEntity in entity) { foreach (var dataPoint in subEntity.population.First(x => x.Year == year && x.source == dataSource).data) { this.population.First(x => x.Year == year && x.source == dataSource).AddDataPoint(dataPoint); } } }
private void SetPopulationData(IEnumerable<Entity> entities, StringBuilder collisionInfo, Boolean overrideData, PopulationDataSourceType dataSource, Int16 year) { if ( entities == null ) { throw new ArgumentNullException("entities"); } ClearRunInfo(); GlobalData.LoadPopulationData(dataSource, year); foreach ( var entity in entities.Where(x => x.population.Any(y => y.source == dataSource && y.Year == year)) ) { var item = _helper.GetWikiDataItemForEntity(entity); if ( item == null ) { _runInfo[WikiDataState.ItemNotFound]++; collisionInfo.AppendFormat("{0}: {1} was deleted!", entity.wiki.wikidata, entity.english); } else { var data = entity.population.First(y => y.source == dataSource && y.Year == year); var state = _helper.PopulationDataCorrect(item, data); _runInfo[state]++; if ( state != WikiDataState.Valid ) { var statement = _helper.SetPopulationData(item, data, overrideData); if ( statement != null ) { statement.save(_helper.GetClaimSaveEditSummary(statement)); _helper.AddPopulationDataReferences(statement, data, entity); foreach ( var reference in statement.References ) { reference.Save(_helper.GetReferenceSaveEditSummary(reference)); } _helper.AddPopulationDataQualifiers(statement, data); foreach ( var qualifier in statement.Qualifiers ) { qualifier.Save(_helper.GetQualifierSaveEditSummary(qualifier)); } } } } } }
public static void LoadPopulationData(PopulationDataSourceType source, Int16 year) { if ( !GlobalData.CountryEntity.population.Any(x => x.Year == year && x.source == source) ) { String filename = String.Empty; switch ( source ) { case PopulationDataSourceType.Census: filename = BaseXMLDirectory + "\\population\\census{0}.xml"; break; case PopulationDataSourceType.DOPA: filename = BaseXMLDirectory + "\\population\\DOPA{0}.xml"; break; } filename = String.Format(CultureInfo.InvariantCulture, filename, year); if ( !string.IsNullOrWhiteSpace(filename) ) { LoadPopulationData(filename); } } var geocodeToRecalculate = new List<UInt32>(); var allEntities = GlobalData.CompleteGeocodeList().FlatList(); foreach ( var item in allEntities.Where(x => x.newgeocode.Any() && x.population.Any(y => y.Year == year && y.source == source)).ToList() ) { foreach ( var newGeocode in item.newgeocode ) { var newItem = allEntities.FirstOrDefault(x => x.geocode == newGeocode); if ( newItem != null ) { if ( !newItem.IsObsolete ) { newItem.population.Add(item.population.First(y => y.Year == year && y.source == source)); geocodeToRecalculate.AddRange(GeocodeHelper.ParentGeocodes(newItem.geocode)); } } } geocodeToRecalculate.AddRange(GeocodeHelper.ParentGeocodes(item.geocode)); } foreach ( var recalculate in geocodeToRecalculate.Distinct() ) { var entityToRecalculate = allEntities.FirstOrDefault(x => x.geocode == recalculate); if ( entityToRecalculate != null ) { var data = entityToRecalculate.population.FirstOrDefault(y => y.Year == year && y.source == source); if ( data != null ) { data.data.Clear(); foreach ( var subentity in entityToRecalculate.entity.Where(x => !x.IsObsolete) ) { var subData = subentity.population.FirstOrDefault(y => y.Year == year && y.source == source); if ( subData != null ) { foreach ( var subDataPoint in subData.data ) { data.AddDataPoint(subDataPoint); } } } data.CalculateTotal(); } } } }
/// <summary> /// Calculates the population data by summing up the data of the subentities. /// </summary> /// <param name="year">Year of data.</param> /// <param name="dataSource">Data source.</param> public void CalculatePopulationFromSubEntities(Int32 year, PopulationDataSourceType dataSource) { foreach ( var subEntity in entity ) { foreach ( var dataPoint in subEntity.population.First(x => x.Year == year && x.source == dataSource).data ) { this.population.First(x => x.Year == year && x.source == dataSource).AddDataPoint(dataPoint); } } }
/// <summary> /// Creates a new instance of PopulationData. /// </summary> public PopulationData() { this.referenceField = new List<object>(); this.dataField = new List<HouseholdDataPoint>(); this.sourceField = PopulationDataSourceType.Unknown; }
public static void LoadPopulationData(PopulationDataSourceType source, Int16 year) { var populationData = LoadPopulationDataUnprocessed(source, year); if (populationData != null) { MergePopulationData(populationData); } var geocodeToRecalculate = new List <UInt32>(); var allEntities = GlobalData.CompleteGeocodeList().FlatList(); foreach (var item in allEntities.Where(x => x.newgeocode.Any() && x.population.Any(y => y.Year == year && y.source == source)).ToList()) { foreach (var newGeocode in item.newgeocode) { var newItem = allEntities.FirstOrDefault(x => x.geocode == newGeocode); if (newItem != null) { if (!newItem.IsObsolete) { newItem.population.Add(item.population.First(y => y.Year == year && y.source == source)); geocodeToRecalculate.AddRange(GeocodeHelper.ParentGeocodes(newItem.geocode)); } } } geocodeToRecalculate.AddRange(GeocodeHelper.ParentGeocodes(item.geocode)); } if (source == PopulationDataSourceType.Census) { // For DOPA need to be done with CalculateLocalGovernmentPopulation Entity.FillExplicitLocalGovernmentPopulation(allEntities.Where(x => x.type.IsLocalGovernment()).ToList(), allEntities, source, year); } foreach (var recalculate in geocodeToRecalculate.Distinct()) { var entityToRecalculate = allEntities.FirstOrDefault(x => x.geocode == recalculate); if (entityToRecalculate != null) { var data = entityToRecalculate.population.FirstOrDefault(y => y.Year == year && y.source == source); if (data != null) { data.data.Clear(); foreach (var subentity in entityToRecalculate.entity.Where(x => !x.IsObsolete)) { var subData = subentity.population.FirstOrDefault(y => y.Year == year && y.source == source); if (subData != null) { foreach (var subDataPoint in subData.data) { data.AddDataPoint(subDataPoint); } } } data.CalculateTotal(); } } } }
public static void LoadPopulationData(PopulationDataSourceType source, Int16 year) { if (!GlobalData.CountryEntity.population.Any(x => x.Year == year && x.source == source)) { String filename = String.Empty; switch (source) { case PopulationDataSourceType.Census: filename = BaseXMLDirectory + "\\population\\census{0}.xml"; break; case PopulationDataSourceType.DOPA: filename = BaseXMLDirectory + "\\population\\DOPA{0}.xml"; break; } filename = String.Format(CultureInfo.InvariantCulture, filename, year); if (!string.IsNullOrWhiteSpace(filename)) { LoadPopulationData(filename); } } var geocodeToRecalculate = new List <UInt32>(); var allEntities = GlobalData.CompleteGeocodeList().FlatList(); foreach (var item in allEntities.Where(x => x.newgeocode.Any() && x.population.Any(y => y.Year == year && y.source == source)).ToList()) { foreach (var newGeocode in item.newgeocode) { var newItem = allEntities.FirstOrDefault(x => x.geocode == newGeocode); if (newItem != null) { if (!newItem.IsObsolete) { newItem.population.Add(item.population.First(y => y.Year == year && y.source == source)); geocodeToRecalculate.AddRange(GeocodeHelper.ParentGeocodes(newItem.geocode)); } } } geocodeToRecalculate.AddRange(GeocodeHelper.ParentGeocodes(item.geocode)); } foreach (var recalculate in geocodeToRecalculate.Distinct()) { var entityToRecalculate = allEntities.FirstOrDefault(x => x.geocode == recalculate); if (entityToRecalculate != null) { var data = entityToRecalculate.population.FirstOrDefault(y => y.Year == year && y.source == source); if (data != null) { data.data.Clear(); foreach (var subentity in entityToRecalculate.entity.Where(x => !x.IsObsolete)) { var subData = subentity.population.FirstOrDefault(y => y.Year == year && y.source == source); if (subData != null) { foreach (var subDataPoint in subData.data) { data.AddDataPoint(subDataPoint); } } } data.CalculateTotal(); } } } }
/// <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 SetPopulationData(IEnumerable <Entity> entities, StringBuilder collisionInfo, Boolean overrideData, PopulationDataSourceType dataSource, Int16 year) { if (entities == null) { throw new ArgumentNullException("entities"); } ClearRunInfo(); GlobalData.LoadPopulationData(dataSource, year); foreach (var entity in entities.Where(x => x.population.Any(y => y.source == dataSource && y.Year == year))) { var item = _helper.GetWikiDataItemForEntity(entity); if (item == null) { _runInfo[WikiDataState.ItemNotFound]++; collisionInfo.AppendFormat("{0}: {1} was deleted!", entity.wiki.wikidata, entity.english); } else { var data = entity.population.First(y => y.source == dataSource && y.Year == year); var state = _helper.PopulationDataCorrect(item, data); _runInfo[state]++; if (state != WikiDataState.Valid) { var statement = _helper.SetPopulationData(item, data, overrideData); if (statement != null) { statement.save(_helper.GetClaimSaveEditSummary(statement)); _helper.AddPopulationDataReferences(statement, data, entity); foreach (var reference in statement.References) { reference.Save(_helper.GetReferenceSaveEditSummary(reference)); } _helper.AddPopulationDataQualifiers(statement, data); foreach (var qualifier in statement.Qualifiers) { qualifier.Save(_helper.GetQualifierSaveEditSummary(qualifier)); } } } } } }