Beispiel #1
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());
                 }
             }
         }
     }
 }
Beispiel #2
0
        /// <summary>
        /// Gets the geocode of a entity by its Thai name, administrative type and the province.
        /// </summary>
        /// <param name="changwatName">Name of the province in Thai.</param>
        /// <param name="subdivisionName">Thai name of administrative entity to find.</param>
        /// <param name="subdivisionType">Type of administrative entity to find.</param>
        /// <returns>Code of the entity found; zero if none was found.</returns>
        internal static Int32 GetGeocode(String changwatName, String subdivisionName, EntityType subdivisionType)
        {
            Int32 provinceId = GetGeocode(changwatName);
            Int32 geocode    = 0;

            if (provinceId != 0)
            {
                String searchName = subdivisionName;
                if (searchName.Contains(" "))
                {
                    searchName = searchName.Substring(0, searchName.IndexOf(" "));
                }
                XElement changwatXml      = XElement.Load(GeocodeSourceFile(provinceId));
                var      subdivisionQuery = from c in changwatXml.Descendants(TambonHelper.TambonNameSpace + "entity")
                                            where (
                    ((String)c.Attribute("name") == searchName) &&
                    EntityTypeHelper.IsCompatibleEntityType(
                        (EntityType)Enum.Parse(typeof(EntityType), (String)c.Attribute("type"))
                        , subdivisionType))
                                            select(Int32) c.Attribute("geocode");

                geocode = subdivisionQuery.FirstOrDefault();
            }
            return(geocode);
        }
        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);
        }
Beispiel #4
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;
 }
Beispiel #5
0
        public Boolean CanWriteForWikipedia()
        {
            Boolean lResult = EntityTypeHelper.IsCompatibleEntityType(Type, EntityType.Amphoe);

            if (Type == EntityType.Tambon)
            {
                lResult = SubEntities.Find(
                    delegate(PopulationDataEntry lEntry)
                {
                    return(lEntry.Type == EntityType.Muban);
                }) != null;
            }
            return(lResult);
        }
Beispiel #6
0
        private PopulationDataEntry FindByNameAndType(String iValue, EntityType iEntityType, Boolean iAllowOldNames, Boolean iAllowObsolete, Int32 iPosition)
        {
            PopulationDataEntry retval = null;

            SubEntities.Sort(
                delegate(PopulationDataEntry a, PopulationDataEntry b)
            {
                return(a.Geocode.CompareTo(b.Geocode));
            }
                );

            foreach (PopulationDataEntry lEntity in SubEntities)
            {
                if (lEntity.SameNameAndType(iValue, iEntityType))
                {
                    if ((!lEntity.IsObsolete()) | iAllowObsolete)
                    {
                        iPosition--;
                        if (iPosition < 0)
                        {
                            retval = lEntity;
                            break;
                        }
                    }
                }
                if (iAllowOldNames & (lEntity.OldNames.Contains(iValue)) & (EntityTypeHelper.IsCompatibleEntityType(lEntity.Type, iEntityType)))
                {
                    if ((!lEntity.IsObsolete()) | iAllowObsolete)
                    {
                        iPosition--;
                        if (iPosition < 0)
                        {
                            retval = lEntity;
                            break;
                        }
                    }
                }
            }
            return(retval);
        }
Beispiel #7
0
 internal void ParseName(string iValue)
 {
     if (!String.IsNullOrEmpty(iValue))
     {
         iValue = iValue.Replace("ทต.", EntityTypeHelper.EntityNames[EntityType.ThesabanTambon]);
         Type   = EntityTypeHelper.ParseEntityType(iValue);
         if ((Type == EntityType.Unknown) | (Type == EntityType.Bangkok))
         {
             Name = iValue;
         }
         else
         {
             Name = iValue.Replace(EntityTypeHelper.EntityNames[Type], "");
         }
         if (EntityTypeHelper.Sakha.Contains(Type))
         {
             // Some pages have the syntax "Name AmphoeName" with the word อำเภอ, others without
             //Int32 pos = Name.IndexOf(Helper.EntityNames[EntityType.Amphoe]);
             //if (pos > 0)
             //{
             //    mName = mName.Remove(pos - 1);
             //}
             Int32 pos = Name.IndexOf(" ");
             if (pos > 0)
             {
                 Name = Name.Remove(pos);
             }
         }
         Obsolete = Name.Contains("*");
         Name     = Name.Replace("*", "");
         if (Name.StartsWith("."))
         {
             // Mistake in DOPA population statistic for Buriram 2005, a leading "."
             Name = Name.Substring(1, Name.Length - 1);
         }
         Name = Name.Trim().Replace("  ", " ");  // "ปอภาร (ปอพาน)" may have a double blank in middle
     }
 }
Beispiel #8
0
        private Boolean NameAndTypeUnique(String iValue, EntityType iEntityType, Boolean iAllowOldNames, Boolean iAllowObsolete)
        {
            Int32 lCount = 0;

            foreach (PopulationDataEntry lEntity in SubEntities)
            {
                if (lEntity.SameNameAndType(iValue, iEntityType))
                {
                    if ((!lEntity.IsObsolete()) | iAllowObsolete)
                    {
                        lCount++;
                    }
                }
                if (iAllowOldNames & (lEntity.OldNames.Contains(iValue)) & (EntityTypeHelper.IsCompatibleEntityType(lEntity.Type, iEntityType)))
                {
                    if ((!lEntity.IsObsolete()) | iAllowObsolete)
                    {
                        lCount++;
                    }
                }
            }
            return(lCount == 1);
        }
Beispiel #9
0
        private String WritePopulationForWikipedia(String iPopulationReference)
        {
            Int32 lVillageNumberTotal = NrOfEntities(new List <EntityType>()
            {
                EntityType.Muban
            });
            Boolean      lShowVillages = (lVillageNumberTotal > 0) & (EntityTypeHelper.IsCompatibleEntityType(this.Type, EntityType.Amphoe));
            StringWriter lWriter       = new StringWriter();

            lWriter.WriteLine("{|");

            lWriter.WriteLine("! No.");
            lWriter.WriteLine("! Name");
            lWriter.WriteLine("! Thai");
            if (lShowVillages)
            {
                lWriter.WriteLine("! Villages");
            }
            lWriter.WriteLine("! [[Population|Inh.]]" + iPopulationReference);
            Int32 lMaxGeocode      = 0;
            Int32 lMaxVillageCount = 0;
            Int32 lMaxPopulation   = 0;

            foreach (PopulationDataEntry lEntity in this.SubEntities)
            {
                lMaxGeocode      = Math.Max(lMaxGeocode, lEntity.Geocode % 100);
                lMaxPopulation   = Math.Max(lMaxPopulation, lEntity.Total);
                lMaxVillageCount = Math.Max(lMaxVillageCount, lEntity.NrOfEntities(new List <EntityType>()
                {
                    EntityType.Muban
                }));
            }
            foreach (PopulationDataEntry lEntity in this.SubEntities)
            {
                lWriter.WriteLine("|-");
                Int32 lGeocode = lEntity.Geocode % 100;
                lWriter.Write("||");
                String lGeocodeString = lGeocode.ToString() + ".";
                if ((lGeocode < 10) & (lMaxGeocode >= 10))
                {
                    lGeocodeString = "{{0}}" + lGeocodeString;
                }
                lWriter.Write(lGeocodeString);
                lWriter.Write("||");
                lWriter.Write(lEntity.English);
                lWriter.Write("||");
                lWriter.Write(lEntity.Name);
                lWriter.Write("||");
                if (lShowVillages)
                {
                    Int32 lVillageCount = lEntity.NrOfEntities(new List <EntityType>()
                    {
                        EntityType.Muban
                    });
                    if (lVillageCount == 0)
                    {
                        lWriter.Write("-");
                    }
                    else
                    {
                        String lVillages = lVillageCount.ToString();
                        if (lVillageCount < 10)
                        {
                            lVillages = "{{0}}" + lVillages;
                        }
                        lWriter.Write(lVillages);
                    }
                    lWriter.Write("||");
                }
                String lPopulation = lEntity.Total.ToString("###,##0", TambonHelper.CultureInfoUS);
                for (int i = lPopulation.Length; i < lMaxPopulation.ToString("###,##0").Length; i++)
                {
                    lPopulation = "{{0}}" + lPopulation;
                }
                lWriter.Write(lPopulation);
                lWriter.WriteLine();
            }
            lWriter.WriteLine("|}");
            String lResult = lWriter.ToString();

            return(lResult);
        }
Beispiel #10
0
 private bool SameNameAndType(String iName, EntityType iFindType)
 {
     return((Name == iName) & (EntityTypeHelper.IsCompatibleEntityType(Type, iFindType)));
 }
Beispiel #11
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();
            }
        }