| | 1 | | using Newtonsoft.Json; |
| | 2 | | using System; |
| | 3 | | using System.ComponentModel; |
| | 4 | | using System.ComponentModel.DataAnnotations; |
| | 5 | | using System.Linq; |
| | 6 | |
|
| | 7 | | namespace Morpho25.Settings |
| | 8 | | { |
| | 9 | |
|
| | 10 | | public class UTM |
| | 11 | | { |
| | 12 | | public UTM(double utmEsting, double utmNorthing, string utmZone) |
| | 13 | | { |
| | 14 | | UTMesting = utmEsting; |
| | 15 | | UTMnorthing = utmNorthing; |
| | 16 | | UTMzone = utmZone; |
| | 17 | | } |
| | 18 | | public double UTMesting { get; } |
| | 19 | | public double UTMnorthing { get; } |
| | 20 | | public string UTMzone { get; } |
| | 21 | |
|
| | 22 | | public override string ToString() |
| | 23 | | { |
| | 24 | | return String.Format("UTM coordinate::{0}", UTMzone); |
| | 25 | | } |
| | 26 | | } |
| | 27 | |
|
| | 28 | |
|
| | 29 | | public class Location : IEquatable<Location> |
| | 30 | | { |
| | 31 | | public const double TIMEZONE_LONGITUDE = 15.00000; |
| | 32 | | public const string PROJECTION_SYSTEM = " "; |
| | 33 | | public const string REALWORLD_POINT = "0.00000"; |
| | 34 | |
|
| | 35 | | private double _latitude; |
| | 36 | | private double _longitude; |
| | 37 | | private string _timeZone; |
| | 38 | | private double _modelRotation; |
| | 39 | | private double _timezoneRefence; |
| | 40 | |
|
| | 41 | | [JsonIgnore] |
| 0 | 42 | | public UTM UTM { get; set; } |
| | 43 | |
|
| | 44 | | [DisplayName("Name")] |
| | 45 | | [Description("Location name.")] |
| | 46 | | [JsonProperty("locationName")] |
| 7 | 47 | | public string LocationName { get; set; } |
| | 48 | |
|
| | 49 | | [DisplayName("Latitude")] |
| | 50 | | [Description("Latitude in deg.")] |
| | 51 | | [Range(-90, 90, ErrorMessage = "Number in range(-90, 90) is required.")] |
| | 52 | | [JsonProperty("latitude", Required = Required.Always)] |
| | 53 | | public double Latitude { |
| 9 | 54 | | get { return _latitude; } |
| | 55 | | private set |
| 4 | 56 | | { |
| 4 | 57 | | if (value > 90.0 || value < -90.0) |
| 1 | 58 | | throw new ArgumentOutOfRangeException( |
| 1 | 59 | | $"{nameof(value)} must be in range (-90, 90)."); |
| | 60 | |
|
| 3 | 61 | | _latitude = value; |
| 3 | 62 | | } |
| | 63 | | } |
| | 64 | |
|
| | 65 | | [DisplayName("Longitude")] |
| | 66 | | [Description("Longitude in deg.")] |
| | 67 | | [Range(-180, 180, ErrorMessage = "Number in range(-180, 180) is required.")] |
| | 68 | | [JsonProperty("longitude", Required = Required.Always)] |
| | 69 | | public double Longitude { |
| 9 | 70 | | get { return _longitude; } |
| | 71 | | private set |
| 3 | 72 | | { |
| 3 | 73 | | if (value > 180.0 || value < -180.0) |
| 0 | 74 | | throw new ArgumentOutOfRangeException( |
| 0 | 75 | | $"{nameof(value)} must be in range (-180, 180)."); |
| | 76 | |
|
| 3 | 77 | | _longitude = value; |
| 3 | 78 | | } |
| | 79 | | } |
| | 80 | |
|
| | 81 | | [DisplayName("Timezone reference")] |
| | 82 | | [Description("Longitude reference for time zone.")] |
| | 83 | | [Range(-180, 180, ErrorMessage = "Number in range(-180, 180) is required.")] |
| | 84 | | [JsonProperty("timezoneReference")] |
| | 85 | | public double TimezoneReference |
| | 86 | | { |
| 9 | 87 | | get { return _timezoneRefence; } |
| | 88 | | set |
| 4 | 89 | | { |
| 4 | 90 | | if (value > 180.0 || value < -180.0) |
| 0 | 91 | | throw new ArgumentOutOfRangeException( |
| 0 | 92 | | $"{nameof(value)} must be in range (-180, 180)."); |
| | 93 | |
|
| 4 | 94 | | _timezoneRefence = value; |
| 4 | 95 | | } |
| | 96 | | } |
| | 97 | |
|
| | 98 | | [DisplayName("Timezone")] |
| | 99 | | [Description("Timezone.")] |
| | 100 | | [JsonProperty("timeZone")] |
| | 101 | | public string TimeZone |
| | 102 | | { |
| 9 | 103 | | get { return _timeZone; } |
| | 104 | | set |
| 4 | 105 | | { |
| | 106 | | int val; |
| 4 | 107 | | if (value == "GMT") |
| 1 | 108 | | { |
| 1 | 109 | | val = 0; |
| 1 | 110 | | } |
| 3 | 111 | | else if (value.StartsWith("UTC")) |
| 0 | 112 | | { |
| 0 | 113 | | var num = value.Split('C').Last(); |
| 0 | 114 | | val = Convert.ToInt32(num); |
| 0 | 115 | | } else |
| 3 | 116 | | { |
| 3 | 117 | | val = Convert.ToInt32(value); |
| 3 | 118 | | } |
| | 119 | |
|
| 4 | 120 | | if (val > 14 || val < -12) |
| 0 | 121 | | throw new ArgumentOutOfRangeException($"{nameof(value)} must be in range (-12, 14)."); |
| | 122 | |
|
| 4 | 123 | | if (val > 0) |
| 0 | 124 | | _timeZone = "UTC+" + value; |
| 4 | 125 | | else if (val < 0) |
| 0 | 126 | | _timeZone = "UTC-" + value; |
| | 127 | | else |
| 4 | 128 | | _timeZone = "GMT"; |
| 4 | 129 | | } |
| | 130 | | } |
| | 131 | |
|
| | 132 | |
|
| | 133 | | [DisplayName("Rotation")] |
| | 134 | | [Description("Model rotation clockwise in deg.")] |
| | 135 | | [Range(0, 360, ErrorMessage = "Number in range(0, 360) is required.")] |
| | 136 | | [JsonProperty("modelRotation")] |
| | 137 | | public double ModelRotation { |
| 9 | 138 | | get { return _modelRotation; } |
| | 139 | | set |
| 4 | 140 | | { |
| 4 | 141 | | if (value > 360.0 || value < 0.0) |
| 0 | 142 | | throw new ArgumentOutOfRangeException( |
| 0 | 143 | | $"{nameof(value)} must be in range (0, 360) Clockwise."); |
| | 144 | |
|
| 4 | 145 | | _modelRotation = value; |
| 4 | 146 | | } |
| | 147 | | } |
| | 148 | |
|
| 4 | 149 | | public Location(double latitude, double longitude) |
| 4 | 150 | | { |
| 4 | 151 | | Latitude = latitude; |
| 3 | 152 | | Longitude = longitude; |
| 3 | 153 | | LocationName = "Envimet Location"; |
| 3 | 154 | | TimeZone = "0"; |
| 3 | 155 | | ModelRotation = 0.0; |
| 3 | 156 | | TimezoneReference = Location.TIMEZONE_LONGITUDE; |
| 3 | 157 | | } |
| | 158 | |
|
| | 159 | | [JsonConstructor] |
| | 160 | | public Location(double latitude, |
| | 161 | | double longitude, |
| | 162 | | string locationName="EnvimetLocation", |
| | 163 | | string timeZone="0", |
| | 164 | | double modelRotation=0, |
| | 165 | | double timezoneReference=Location.TIMEZONE_LONGITUDE) : |
| 2 | 166 | | this(latitude, longitude) |
| 1 | 167 | | { |
| 1 | 168 | | LocationName = locationName; |
| 1 | 169 | | TimeZone = timeZone; |
| 1 | 170 | | ModelRotation = modelRotation; |
| 1 | 171 | | TimezoneReference = timezoneReference; |
| 1 | 172 | | } |
| | 173 | |
|
| | 174 | |
|
| | 175 | | public override string ToString() |
| 0 | 176 | | { |
| 0 | 177 | | return String.Format("{0}::{1}::{2}::{3}", LocationName, |
| 0 | 178 | | Latitude, Longitude, ModelRotation); |
| 0 | 179 | | } |
| | 180 | |
|
| | 181 | | public string Serialize() |
| 1 | 182 | | { |
| 1 | 183 | | return JsonConvert.SerializeObject(this); |
| 1 | 184 | | } |
| | 185 | |
|
| | 186 | | public static Location Deserialize(string json) |
| 2 | 187 | | { |
| | 188 | | try |
| 2 | 189 | | { |
| 2 | 190 | | return JsonConvert.DeserializeObject<Location>(json); |
| | 191 | | } |
| 1 | 192 | | catch (Exception e) |
| 1 | 193 | | { |
| 1 | 194 | | throw new Exception(e.Message); |
| | 195 | | } |
| 1 | 196 | | } |
| | 197 | |
|
| | 198 | | public bool Equals(Location other) |
| 1 | 199 | | { |
| 1 | 200 | | if (other == null) |
| 0 | 201 | | return false; |
| | 202 | |
|
| 1 | 203 | | if (other != null |
| 1 | 204 | | && other.LocationName == this.LocationName |
| 1 | 205 | | && other.TimeZone == this.TimeZone |
| 1 | 206 | | && other.Latitude == this.Latitude |
| 1 | 207 | | && other.Longitude == this.Longitude |
| 1 | 208 | | && other.TimezoneReference == this.TimezoneReference |
| 1 | 209 | | && other.ModelRotation == this.ModelRotation) |
| 1 | 210 | | return true; |
| | 211 | | else |
| 0 | 212 | | return false; |
| 1 | 213 | | } |
| | 214 | |
|
| | 215 | | public override bool Equals(Object obj) |
| 0 | 216 | | { |
| 0 | 217 | | if (obj == null) |
| 0 | 218 | | return false; |
| | 219 | |
|
| 0 | 220 | | var locObj = obj as Location; |
| 0 | 221 | | if (locObj == null) |
| 0 | 222 | | return false; |
| | 223 | | else |
| 0 | 224 | | return Equals(locObj); |
| 0 | 225 | | } |
| | 226 | |
|
| | 227 | | public override int GetHashCode() |
| 0 | 228 | | { |
| | 229 | | unchecked |
| 0 | 230 | | { |
| 0 | 231 | | int hash = 17; |
| 0 | 232 | | hash = hash * 23 + LocationName.GetHashCode(); |
| 0 | 233 | | hash = hash * 23 + TimeZone.GetHashCode(); |
| 0 | 234 | | hash = hash * 23 + Latitude.GetHashCode(); |
| 0 | 235 | | hash = hash * 23 + Longitude.GetHashCode(); |
| 0 | 236 | | hash = hash * 23 + TimezoneReference.GetHashCode(); |
| 0 | 237 | | hash = hash * 23 + ModelRotation.GetHashCode(); |
| 0 | 238 | | return hash; |
| | 239 | | } |
| 0 | 240 | | } |
| | 241 | |
|
| | 242 | | public static bool operator ==(Location loc1, Location loc2) |
| 2 | 243 | | { |
| 2 | 244 | | if (((object)loc1) == null || ((object)loc2) == null) |
| 2 | 245 | | return Object.Equals(loc1, loc2); |
| | 246 | |
|
| 0 | 247 | | return loc1.Equals(loc2); |
| 2 | 248 | | } |
| | 249 | |
|
| | 250 | | public static bool operator !=(Location loc1, Location loc2) |
| 1 | 251 | | { |
| 1 | 252 | | return !(loc1 == loc2); |
| 1 | 253 | | } |
| | 254 | | } |
| | 255 | | } |