/// <summary>
        /// Validates the request to create a new trail
        /// </summary>
        public ValidationServiceResponse <TopoTrailInfo> ValidateCreate(ITopoTrailUpdateRequest request)
        {
            // load existing trail
            var response = new ValidationServiceResponse <TopoTrailInfo>(null);

            // do basic validation
            if (String.IsNullOrWhiteSpace(request.Name))
            {
                response.AddError("Name", "Name is required!");
            }

            // TODO: REFACTOR: use GeoPolitical
            var timezone = GeoTimezoneInfo.Find(request.Timezone);

            if (timezone == null)
            {
                response.AddError("Timezone", "Timezone is missing or invalid!");
            }

            var country = GeoCountryInfo.Find(request.Country);

            if (country == null)
            {
                response.AddError("Country", "Country is missing or invalid!");
            }

            var region = GeoRegionInfo.Find(request.Region);

            if (region == null && !String.IsNullOrWhiteSpace(request.Region))
            {
                response.AddError("Region", "Region is invalid!");
            }

            return(response);
        }
        public TopoTrailInfo(GpxFileData data)
        {
            Name        = data.Name;
            Description = data.Description;

            Keywords = data.Keywords;
            UrlText  = data.UrlText;
            UrlLink  = data.UrlLink;

            Timezone = GeoTimezoneInfo.Find(data.Extensions.Timezone);
            if (Timezone == null)
            {
                Timezone = GeoTimezoneInfo.UTC;
            }

            Country  = GeoCountryInfo.Find(data.Extensions.Country);
            Region   = GeoRegionInfo.Find(data.Extensions.Region);
            Location = data.Extensions.Location;

            foreach (var track in data.Tracks)
            {
                var t = new TopoTrackInfo(this, track);
                _tracks.Add(t);
            }
        }
        public TopoTrailInfo(ITopoTrailUpdateRequest data, List <GpxTrackData> tracks)
        {
            Name        = data.Name;
            Description = data.Description;

            Keywords = data.Keywords;
            UrlText  = data.UrlText;
            UrlLink  = data.UrlLink;

            Timezone = GeoTimezoneInfo.Find(data.Timezone);
            if (Timezone == null)
            {
                Timezone = GeoTimezoneInfo.UTC;
            }

            Country  = GeoCountryInfo.Find(data.Country);
            Region   = GeoRegionInfo.Find(data.Region);
            Location = data.Location;

            if (tracks != null)
            {
                foreach (var track in tracks)
                {
                    var t = new TopoTrackInfo(this, track);
                    _tracks.Add(t);
                }
            }
        }
        private dynamic MapTimezone(GeoTimezoneInfo t)
        {
            dynamic d = new ExpandoObject();

            d.tzid = t.TZID;
            d.name = t.Name;
            return(d);
        }
 public void GeoTimezoneInfo_CheckSystemTimezones()
 {
     foreach (var tz in TimeZoneInfo.GetSystemTimeZones())
     {
         var g = GeoTimezoneInfo.BySystem(tz.Id.ToLowerInvariant());
         Assert.IsNotNull(g);
         Assert.AreEqual(tz.Id, g.WinTZ);
     }
 }
 public void GeoTimezoneInfo_ByTZID()
 {
     // given a TZ ID, find the right GeoTimezone object and check its properties
     foreach (var tz in GeoTimezoneInfo.Windows)
     {
         var g = GeoTimezoneInfo.ByTZID(tz.TZID.ToLowerInvariant());
         Assert.IsNotNull(g);
         Assert.AreEqual(tz.TZID, g.TZID);
     }
 }
        private IHttpActionResult GetTimezone(string id)
        {
            var t = GeoTimezoneInfo.Find(id);

            if (t == null)
            {
                t = Graffiti.Geo.FindTimezone(id);
            }
            if (t == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            var d = MapTimezone(t);

            return(Ok(d));
        }
        public GeoPolitical(IGeoPoliticalData data)
        {
            _data = data;

            Timezone = GeoTimezoneInfo.Find(_data.Timezone);
            Country  = GeoCountryInfo.Find(_data.Country);
            Region   = GeoRegionInfo.Find(_data.Region);

            // attempt to backfill timezone
            if (Timezone == null && Region != null)
            {
                Timezone = Graffiti.Geo.GuessTimezone(Region);
            }
            if (Timezone == null && Country != null)
            {
                Timezone = Graffiti.Geo.GuessTimezone(Country);
            }
        }
        public GeoTimezoneInfo GuessTimezone(GeoCountryInfo country)
        {
            if (country == null)
            {
                return(null);
            }

            switch (country.ISO2)
            {
            case "AR": return(GeoTimezoneInfo.ByTZID("America/Buenos_Aires"));

            case "CL": return(GeoTimezoneInfo.ByTZID("America/Santiago"));

            case "JP": return(GeoTimezoneInfo.ByTZID("Asia/Tokyo"));

            case "NZ": return(GeoTimezoneInfo.ByTZID("Pacific/Auckland"));

            case "CH": return(GeoTimezoneInfo.ByTZID("Europe/Zurich"));

            default: return(null);
            }
        }
        /// <summary>
        /// Updates the trail metadata and synchronizes file and cache
        /// </summary>
        public ValidationServiceResponse <TopoTrailInfo> UpdateTrail(ITopoTrailUpdateRequest request)
        {
            // validate update request
            var response = ValidateUpdate(request);

            if (response.HasErrors)
            {
                return(response);
            }

            // process update request
            var trail = response.Data;

            // save current filename for later
            var existing = GetFilename(trail);

            // check file system
            if (!Directory.Exists(_rootUri))
            {
                throw new Exception($"Directory not initalized: {_rootUri}");
            }
            var folder = Path.Combine(_rootUri, trail.Country.Name);

            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }

            // update trail properties
            trail.Timezone = GeoTimezoneInfo.Find(request.Timezone);
            trail.Country  = GeoCountryInfo.Find(request.Country);
            trail.Region   = GeoRegionInfo.Find(request.Region);

            trail.Name        = TextMutate.TrimSafe(request.Name);
            trail.Description = TextMutate.TrimSafe(request.Description);
            trail.Location    = TextMutate.TrimSafe(request.Location);
            trail.Keywords    = TextMutate.FixKeywords(request.Keywords);

            // TODO: BUG: fix url writing for v1.1 files!
            //trail.UrlLink = TextMutate.FixUrl(request.UrlLink);
            //trail.UrlText = TextMutate.TrimSafe(request.UrlText);

            // generate new and rename/remove existing files
            var filename = GetFilename(trail);

            // check if overwrite file
            var renamed = String.Compare(existing, filename, true) != 0;

            if (!renamed)
            {
                // temp rename current file
                File.Move(existing, existing + "~temp");
                existing = existing + "~temp";
            }

            // write new file
            var contents = BuildGpx(trail);

            File.WriteAllText(filename, contents);

            // delete old file
            File.Delete(existing);

            // refresh key and cache data
            if (renamed)
            {
                trail.Key = Path.GetFileNameWithoutExtension(filename).ToUpperInvariant();
                _trails.Remove(request.Key.ToUpperInvariant());
                _trails.Add(trail);
            }

            // return successful response
            return(response);
        }
Exemple #11
0
 public static string GetTimezoneUrl(GeoTimezoneInfo timezone)
 {
     return($"/geo/timezones/#{timezone.GeoTZID}");
 }
Exemple #12
0
        // --------------------------------------------------
        // Timezone
        public GeoTimezoneInfo LookupTimezone(IGeoLatLon point)
        {
            var response = _google.RequestTimezone(point);

            return(GeoTimezoneInfo.ByTZID(response.TimeZoneId));
        }
        // TODO: update and fix this
        private GeoTimezoneInfo GuessTimezone(IEnumerable <GeoCountryInfo> countries, IEnumerable <GeoRegionInfo> regions)
        {
            GeoCountryInfo country = countries.FirstOrDefault();

            var regionCountries = regions.Select(x => x.Country).Distinct().Count();

            if (regionCountries == 1)             // 1 country with multiple regions
            {
                country = regions.First().Country;
            }

            // now pick timezone
            if (country == null)
            {
                return(GeoTimezoneInfo.LocalTimezone);
            }

            // simple country level
            switch (country.ISO2)
            {
            case "AR": return(GeoTimezoneInfo.ByKey("Argentina"));

            case "CL": return(GeoTimezoneInfo.ByKey("Pacific SA"));

            case "NZ": return(GeoTimezoneInfo.ByKey("New Zealand"));

            case "JP": return(GeoTimezoneInfo.ByKey("Tokyo"));

            case "SG": return(GeoTimezoneInfo.ByKey("Singapore"));
            }

            // assume europe is single timezone
            if (country.Continent == GeoContinents.Europe)
            {
                return(GeoTimezoneInfo.ByKey("W. Europe"));
            }

            // break australia down into regions
            if (country.ISO2 == "AU")
            {
                if (regions.Any(x => x.RegionName == "Tasmania"))
                {
                    return(GeoTimezoneInfo.ByKey("Tasmania"));
                }
                else if (regions.Any(x => x.RegionName == "Western Australia"))
                {
                    return(GeoTimezoneInfo.ByKey("W. Australia"));
                }
                else if (regions.Any(x => x.RegionName == "New South Wales"))
                {
                    return(GeoTimezoneInfo.ByKey("AUS Eastern"));
                }
                else if (regions.Any(x => x.RegionName == "Queensland"))
                {
                    return(GeoTimezoneInfo.ByKey("AUS Eastern"));
                }
            }

            // default to UTC
            return(GeoTimezoneInfo.ByKey("UTC"));
        }