コード例 #1
0
ファイル: HouseHoldDataPoint.cs プロジェクト: wanjarus/tambon
 /// <summary>
 /// Creates a new <see cref="HouseholdDataPoint"/> containing all values from <paramref name="dataPoint"/>.
 /// </summary>
 /// <param name="dataPoint">Data to copy.</param>
 public HouseholdDataPoint(HouseholdDataPoint dataPoint)
 {
     if (dataPoint == null)
     {
         throw new ArgumentNullException("dataPoint");
     }
     this.male       = dataPoint.male;
     this.female     = dataPoint.female;
     this.households = dataPoint.households;
     this.total      = dataPoint.total;
     this.geocode    = dataPoint.geocode;
     this.type       = dataPoint.type;
 }
コード例 #2
0
        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);
        }
コード例 #3
0
ファイル: PopulationData.cs プロジェクト: yieldret/tambon
 /// <summary>
 /// Adds the numbers of the datapoint to this.
 /// </summary>
 /// <param name="dataPoint">Data point to add.</param>
 /// <exception cref="ArgumentNullException"><paramref name="dataPoint"/> is <c>null</c>.</exception>
 public void AddDataPoint(HouseholdDataPoint dataPoint)
 {
     if ( dataPoint == null )
     {
         throw new ArgumentNullException("dataPoint");
     }
     var target = data.FirstOrDefault(x => x.type == dataPoint.type);
     if ( target == null )
     {
         target = new HouseholdDataPoint();
         target.type = dataPoint.type;
         data.Add(target);
     }
     target.total += dataPoint.total;
     target.male += dataPoint.male;
     target.female += dataPoint.female;
     target.households += dataPoint.households;
 }
コード例 #4
0
ファイル: PopulationData.cs プロジェクト: yieldret/tambon
 public void CalculateTotal()
 {
     if ( !data.Any(x => x.type == PopulationDataType.total) )
     {
         var result = new HouseholdDataPoint();
         result.type = PopulationDataType.total;
         foreach ( var element in this.data.Where(x => x.type == PopulationDataType.municipal || x.type == PopulationDataType.nonmunicipal) )
         {
             result.female += element.female;
             result.male += element.male;
             result.total += element.total;
             result.households += element.households;
         }
         if ( result.total > 0 )
         {
             data.Add(result);
         }
     }
 }
コード例 #5
0
ファイル: PopulationData.cs プロジェクト: zenwalk/tambon
 public void CalculateTotal()
 {
     if (!data.Any(x => x.type == PopulationDataType.total))
     {
         var result = new HouseholdDataPoint();
         result.type = PopulationDataType.total;
         foreach (var element in this.data.Where(x => x.type == PopulationDataType.municipal || x.type == PopulationDataType.nonmunicipal))
         {
             result.female     += element.female;
             result.male       += element.male;
             result.total      += element.total;
             result.households += element.households;
         }
         if (result.total > 0)
         {
             data.Add(result);
         }
     }
 }
コード例 #6
0
ファイル: PopulationData.cs プロジェクト: zenwalk/tambon
        /// <summary>
        /// Adds the numbers of the datapoint to this.
        /// </summary>
        /// <param name="dataPoint">Data point to add.</param>
        /// <exception cref="ArgumentNullException"><paramref name="dataPoint"/> is <c>null</c>.</exception>
        public void AddDataPoint(HouseholdDataPoint dataPoint)
        {
            if (dataPoint == null)
            {
                throw new ArgumentNullException("dataPoint");
            }
            var target = data.FirstOrDefault(x => x.type == dataPoint.type);

            if (target == null)
            {
                target      = new HouseholdDataPoint();
                target.type = dataPoint.type;
                data.Add(target);
            }
            target.total      += dataPoint.total;
            target.male       += dataPoint.male;
            target.female     += dataPoint.female;
            target.households += dataPoint.households;
        }
コード例 #7
0
        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;
        }
コード例 #8
0
ファイル: Entity.cs プロジェクト: PaulCharlton/tambon
        /// <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();
            }
        }