Пример #1
0
        private void FixupWronglyPlacedAmphoe()
        {
            var invalidTambon = new List <Entity>();

            foreach (var amphoe in Data.entity.Where(x => x.type.IsCompatibleEntityType(EntityType.Amphoe)))
            {
                foreach (var tambon in amphoe.entity.Where(x => !GeocodeHelper.IsBaseGeocode(amphoe.geocode, x.geocode)).ToList())
                {
                    invalidTambon.Add(tambon);
                    amphoe.entity.Remove(tambon);
                }
            }
            foreach (var tambon in invalidTambon)
            {
                var mainTambon = Data.FlatList().FirstOrDefault(x => GeocodeHelper.IsSameGeocode(x.geocode, tambon.geocode, false));
                if (mainTambon != null)
                {
                    foreach (var dataPoint in tambon.population.First().data)
                    {
                        mainTambon.population.First().AddDataPoint(dataPoint);
                    }
                }
            }
            var emptyAmphoe = Data.entity.Where(x => x.type.IsCompatibleEntityType(EntityType.Amphoe) && !x.entity.Any()).ToList();

            foreach (var toRemove in emptyAmphoe)
            {
                Data.entity.Remove(toRemove);
            }
        }
Пример #2
0
        public IEnumerable <Entity> InvalidGeocodeEntries()
        {
            var result = new List <Entity>();

            foreach (var subEntity in entity)
            {
                if (!GeocodeHelper.IsBaseGeocode(this.geocode, subEntity.geocode))
                {
                    result.Add(subEntity);
                }

                Int32 entitiesWithSameCode = 0;
                foreach (var subEntityForCount in entity)
                {
                    if (subEntityForCount.geocode == subEntity.geocode)
                    {
                        entitiesWithSameCode++;
                    }
                }
                if (entitiesWithSameCode > 1)
                {
                    result.Add(subEntity);
                }

                result.AddRange(subEntity.InvalidGeocodeEntries());
            }
            return(result);
        }
Пример #3
0
        /// <summary>
        /// Gets an enumeration of all geocodes which are not set correctly.
        /// </summary>
        /// <returns></returns>
        public IEnumerable <UInt32> WrongGeocodes()
        {
            var result      = new List <UInt32>();
            var subGeocodes = entity.Select(x => x.geocode);

            result.AddRange(subGeocodes.Where(x => !GeocodeHelper.IsBaseGeocode(this.geocode, x)));
            var duplicates = subGeocodes.GroupBy(s => s).SelectMany(grp => grp.Skip(1));

            result.AddRange(duplicates);
            foreach (var subentity in entity)
            {
                result.AddRange(subentity.WrongGeocodes());
            }
            return(result);
        }
Пример #4
0
        private List <Entity> LoadGeocodeLists()
        {
            var result = new List <Entity>();

            foreach (var entry in GlobalData.Provinces)
            {
                if (GeocodeHelper.IsBaseGeocode(BaseGeocode, entry.geocode))
                {
                    var entities           = GlobalData.GetGeocodeList(entry.geocode);
                    var allEntities        = entities.FlatList();
                    var allFittingEntities = allEntities.Where(x => _entityTypes.Contains(x.type));
                    result.AddRange(allFittingEntities);
                }
            }
            return(result);
        }
Пример #5
0
        private List <String> NormalizeNameList(IEnumerable <Entity> entities)
        {
            var result = new List <String>();

            foreach (var entry in entities)
            {
                var name = entry.name;
                if (entry.type == EntityType.Muban)
                {
                    if (!String.IsNullOrEmpty(entry.name))
                    {
                        name = name.StripBanOrChumchon();
                    }
                }
                if ((!entry.IsObsolete) & (GeocodeHelper.IsBaseGeocode(BaseGeocode, entry.geocode)))
                {
                    result.Add(name);
                }
            }
            return(result);
        }
Пример #6
0
        /// <summary>
        /// Creates an Wikipedia article stub for a Tambon.
        /// </summary>
        /// <param name="entity">Entity to export.</param>
        /// <param name="language">Language to use.</param>
        /// <returns>Wikipedia text.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="entity"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException"><paramref name="entity"/> has wrong <see cref="Entity.type"/>.</exception>
        /// <exception cref="NotImplementedException"><paramref name="language"/> is not yet implemented.</exception>
        public String TambonArticle(Entity entity, Language language)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            if (!entity.type.IsCompatibleEntityType(EntityType.Tambon))
            {
                throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "Entity type {0} not compatible with Tambon", entity.type));
            }
            if (language != Language.English)
            {
                throw new NotImplementedException(String.Format(CultureInfo.InvariantCulture, "Unsupported language {0}", language));
            }

            var englishCulture  = new CultureInfo("en-US");
            var province        = _country.entity.FirstOrDefault(x => x.geocode == GeocodeHelper.ProvinceCode(entity.geocode));
            var amphoe          = province.entity.FirstOrDefault(x => GeocodeHelper.IsBaseGeocode(x.geocode, entity.geocode));
            var muban           = entity.entity.Where(x => x.type == EntityType.Muban && !x.IsObsolete);
            var lao             = _localGovernments.Where(x => x.LocalGovernmentAreaCoverage.Any(y => y.geocode == entity.geocode));
            var parentTambon    = new List <Entity>();
            var creationHistory = entity.history.Items.FirstOrDefault(x => x is HistoryCreate) as HistoryCreate;

            if (creationHistory != null)
            {
                var allTambon = _country.FlatList().Where(x => x.type.IsCompatibleEntityType(EntityType.Tambon));
                parentTambon.AddRange(creationHistory.splitfrom.Select(x => allTambon.FirstOrDefault(y => x == y.geocode)));
            }
            var tempList = new List <Entity>()
            {
                province, amphoe
            };

            tempList.AddRange(muban);
            tempList.AddRange(lao);
            tempList.AddRange(parentTambon);
            var   links          = RetrieveWikpediaLinks(tempList, language);
            var   populationData = entity.population.FirstOrDefault(x => x.Year == PopulationReferenceYear && x.source == PopulationDataSourceType.DOPA);
            Int32 population     = 0;

            if (populationData != null)
            {
                population = populationData.TotalPopulation.total;
            }

            StringBuilder builder = new StringBuilder();

            builder.AppendLine("{{Infobox settlement");
            builder.AppendLine("<!--See the Table at Infobox Settlement for all fields and descriptions of usage-->");
            builder.AppendLine("<!-- Basic info  ---------------->");
            builder.AppendFormat(englishCulture, "|official_name          = {0}", entity.english);
            builder.AppendLine();
            builder.AppendFormat(englishCulture, "|native_name            = {0}", entity.name);
            builder.AppendLine();
            builder.AppendLine("|settlement_type        = [[Tambon]]");
            builder.AppendLine("|motto                  =");
            builder.AppendLine("<!-- Location ------------------>");
            builder.AppendLine("|subdivision_type      = Country ");
            builder.AppendLine("|subdivision_name      ={{flag|Thailand}} ");
            builder.AppendLine("|subdivision_type2      = [[Provinces of Thailand|Province]]");
            builder.AppendFormat(englishCulture, "|subdivision_name2      = {0}", WikiLink(links[province], province.english));
            builder.AppendLine();
            builder.AppendLine("|subdivision_type3      = [[Amphoe]]");
            builder.AppendFormat(englishCulture, "|subdivision_name3      = {0}", WikiLink(links[amphoe], amphoe.english));
            builder.AppendLine();
            builder.AppendLine("<!-- Politics ----------------->");
            builder.AppendLine("|established_title      = ");
            builder.AppendLine("|established_date       = ");
            builder.AppendLine("<!-- Area    --------------------->");
            builder.AppendLine("|area_total_km2           = ");
            builder.AppendLine("|area_water_km2           =");
            builder.AppendLine("<!-- Population   ----------------------->");
            builder.AppendFormat(englishCulture, "|population_as_of               = {0}", PopulationReferenceYear);
            builder.AppendLine();
            builder.AppendLine("|population_footnotes           = ");
            builder.AppendLine("|population_note                = ");
            builder.AppendFormat(englishCulture, "|population_total               = {0:#,###,###}", population);
            builder.AppendLine();
            builder.AppendLine("|population_density_km2         = ");
            builder.AppendLine("<!-- General information  --------------->");
            builder.AppendLine("|timezone               = [[Thailand Standard Time|TST]]");
            builder.AppendLine("|utc_offset             = +7");
            builder.AppendLine("|latd= |latm= |lats= |latNS=N");
            builder.AppendLine("|longd= |longm= |longs= |longEW=E");
            builder.AppendLine("|elevation_footnotes    =  ");
            builder.AppendLine("|elevation_m            = ");
            builder.AppendLine("<!-- Area/postal codes & others -------->");
            builder.AppendLine("|postal_code_type       = Postal code");
            builder.AppendLine("|postal_code            = {{#property:P281}}");
            builder.AppendLine("|area_code              = ");
            builder.AppendLine("|blank_name             = [[TIS 1099]]");
            builder.AppendLine("|blank_info             = {{#property:P1067}}");
            builder.AppendLine("|website                = ");
            builder.AppendLine("|footnotes              = ");
            builder.AppendLine("}}");
            builder.AppendLine();

            String nativeName;

            if (!String.IsNullOrEmpty(entity.ipa))
            {
                nativeName = String.Format(englishCulture, "{{{{lang-th|{0}}}}}; {{{{IPA-th|{1}|IPA}}}}", entity.name, entity.ipa);
            }
            else
            {
                nativeName = String.Format(englishCulture, "{{{{lang-th|{0}}}}}", entity.name);
            }

            builder.AppendFormat(englishCulture,
                                 "'''{0}''' ({1}) is a ''[[tambon]]'' (subdistrict) of {2}, in {3}, [[Thailand]]. In {4} it had a total population of {5:#,###,###} people.{6}",
                                 entity.english,
                                 nativeName,
                                 WikiLink(links[amphoe], amphoe.english + " District"),
                                 WikiLink(links[province], province.english + " Province"),
                                 PopulationReferenceYear,
                                 population,
                                 PopulationDataDownloader.WikipediaReference(GeocodeHelper.ProvinceCode(entity.geocode), PopulationReferenceYear, language)
                                 );
            builder.AppendLine();
            builder.AppendLine();

            if (creationHistory != null)
            {
                builder.AppendLine("==History==");

                var parents = String.Join(", ", parentTambon.Select(x => links.Keys.Contains(x) ? WikiLink(links[x], x.english) : x.english));
                if (!parentTambon.Any())
                {
                    builder.AppendFormat(englishCulture, "The subdistrict was created effective {0:MMMM d, yyyy}.", creationHistory.effective);
                }
                else if (creationHistory.subdivisions > 0)
                {
                    builder.AppendFormat(englishCulture, "The subdistrict was created effective {0:MMMM d, yyyy} by splitting off {1} administrative villages from {2}.", creationHistory.effective, creationHistory.subdivisions, parents);
                }
                else
                {
                    builder.AppendFormat(englishCulture, "The subdistrict was created effective {0:MMMM d, yyyy} by splitting off from {1}.", creationHistory.effective, parents);
                }
                var gazetteRef = creationHistory.Items.FirstOrDefault(x => x is GazetteRelated) as GazetteRelated;
                if (gazetteRef != null)
                {
                    var gazette = GlobalData.AllGazetteAnnouncements.FindAnnouncement(gazetteRef);
                    if (gazette != null)
                    {
                        builder.AppendFormat("<ref>{0}</ref>", gazette.WikipediaReference(language));
                    }
                    builder.AppendLine();
                }
            }

            builder.AppendLine("==Administration==");
            builder.AppendLine();
            builder.AppendLine("===Central administration===");
            if (muban.Any())
            {
                builder.AppendFormat(englishCulture, "The ''tambon'' is subdivided into {0} administrative villages (''[[muban]]'').", muban.Count());
                builder.AppendLine();
                builder.AppendLine("{| class=\"wikitable sortable\"");
                builder.AppendLine("! No.");
                builder.AppendLine("! Name");
                builder.AppendLine("! Thai");
                foreach (var mu in muban)
                {
                    builder.AppendLine("|-");
                    var muEnglish = mu.english;
                    if (links.Keys.Contains(mu))
                    {
                        muEnglish = WikiLink(links[mu], mu.english);
                    }
                    var muNumber = mu.geocode % 100;
                    if (muNumber < 10)
                    {
                        builder.AppendFormat(englishCulture, "||{{{{0}}}}{0}.||{1}||{2}", muNumber, muEnglish, mu.name);
                    }
                    else
                    {
                        builder.AppendFormat(englishCulture, "||{0}.||{1}||{2}", muNumber, mu.english, mu.name);
                    }
                    builder.AppendLine();
                }
                builder.AppendLine("|}");
                builder.AppendLine();
            }
            else
            {
                builder.AppendLine("The ''tambon'' has no administrative villages (''[[muban]]'').");
                builder.AppendLine();
            }
            if (lao.Any())
            {
                var enWikipediaLink = new Dictionary <EntityType, String>()
                {
                    { EntityType.ThesabanNakhon, "city (''[[Thesaban#City municipality|Thesaban Nakhon]]'')" },
                    { EntityType.ThesabanMueang, "town (''[[Thesaban#Town municipality|Thesaban Mueang]]'')" },
                    { EntityType.ThesabanTambon, "subdistrict municipality (''[[Thesaban#Subdistrict municipality|Thesaban Tambon]]'')" },
                    { EntityType.TAO, "[[Subdistrict administrative organization|subdistrict administrative organization (SAO)]]" },
                };

                builder.AppendLine("===Local administration===");
                var laoTupel = new List <Tuple <String, String, String> >();
                foreach (var laoEntity in lao)
                {
                    var laoEnglish = laoEntity.english;
                    if (links.Keys.Contains(laoEntity))
                    {
                        laoEnglish = WikiLink(links[laoEntity], laoEnglish);
                    }
                    laoTupel.Add(new Tuple <String, String, String>(enWikipediaLink[laoEntity.type], laoEnglish, laoEntity.FullName));
                }

                if (lao.Count() == 1)
                {
                    var firstLao = laoTupel.First();

                    builder.AppendFormat(englishCulture, "The whole area of the subdistrict is covered by the {0} {1} ({2}).", firstLao.Item1, firstLao.Item2, firstLao.Item3);
                    builder.AppendLine();
                }
                else
                {
                    builder.AppendFormat(englishCulture, "The area of the subdistrict is shared by {0} local governments.", lao.Count());
                    builder.AppendLine();
                    foreach (var tupel in laoTupel)
                    {
                        builder.AppendFormat(englishCulture, "*the {0} {1} ({2})", tupel.Item1, tupel.Item2, tupel.Item3);
                        builder.AppendLine();
                    }
                }
                builder.AppendLine();
            }

            builder.AppendLine("==References==");
            builder.AppendLine("{{reflist}}");
            builder.AppendLine();
            builder.AppendLine("==External links==");
            builder.AppendFormat(CultureInfo.InvariantCulture, "*[http://www.thaitambon.com/tambon/{0} Thaitambon.com on {1}]", entity.geocode, entity.english);
            builder.AppendLine();
            // {{coord|19.0625|N|98.9396|E|source:wikidata-and-enwiki-cat-tree_region:TH|display=title}}
            builder.AppendLine();

            builder.AppendFormat(CultureInfo.InvariantCulture, "[[Category:Tambon of {0} Province]]", province.english);
            builder.AppendLine();
            builder.AppendFormat(CultureInfo.InvariantCulture, "[[Category:Populated places in {0} Province]]", province.english);
            builder.AppendLine();
            builder.AppendLine();
            builder.AppendFormat(CultureInfo.InvariantCulture, "{{{{{0}-geo-stub}}}}", province.english.ToCamelCase());

            return(builder.ToString());
        }