| | 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 | | { |
| 0 | 12 | | public UTM(double utmEsting, double utmNorthing, string utmZone) |
| 0 | 13 | | { |
| 0 | 14 | | UTMesting = utmEsting; |
| 0 | 15 | | UTMnorthing = utmNorthing; |
| 0 | 16 | | UTMzone = utmZone; |
| 0 | 17 | | } |
| 0 | 18 | | public double UTMesting { get; } |
| 0 | 19 | | public double UTMnorthing { get; } |
| 0 | 20 | | public string UTMzone { get; } |
| | 21 | |
|
| | 22 | | public override string ToString() |
| 0 | 23 | | { |
| 0 | 24 | | return String.Format("UTM coordinate::{0}", UTMzone); |
| 0 | 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] |
| | 42 | | public UTM UTM { get; set; } |
| | 43 | |
|
| | 44 | | [DisplayName("Name")] |
| | 45 | | [Description("Location name.")] |
| | 46 | | [JsonProperty("locationName")] |
| | 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 { |
| | 54 | | get { return _latitude; } |
| | 55 | | private set |
| | 56 | | { |
| | 57 | | if (value > 90.0 || value < -90.0) |
| | 58 | | throw new ArgumentOutOfRangeException( |
| | 59 | | $"{nameof(value)} must be in range (-90, 90)."); |
| | 60 | |
|
| | 61 | | _latitude = value; |
| | 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 { |
| | 70 | | get { return _longitude; } |
| | 71 | | private set |
| | 72 | | { |
| | 73 | | if (value > 180.0 || value < -180.0) |
| | 74 | | throw new ArgumentOutOfRangeException( |
| | 75 | | $"{nameof(value)} must be in range (-180, 180)."); |
| | 76 | |
|
| | 77 | | _longitude = value; |
| | 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 | | { |
| | 87 | | get { return _timezoneRefence; } |
| | 88 | | set |
| | 89 | | { |
| | 90 | | if (value > 180.0 || value < -180.0) |
| | 91 | | throw new ArgumentOutOfRangeException( |
| | 92 | | $"{nameof(value)} must be in range (-180, 180)."); |
| | 93 | |
|
| | 94 | | _timezoneRefence = value; |
| | 95 | | } |
| | 96 | | } |
| | 97 | |
|
| | 98 | | [DisplayName("Timezone")] |
| | 99 | | [Description("Timezone.")] |
| | 100 | | [JsonProperty("timeZone")] |
| | 101 | | public string TimeZone |
| | 102 | | { |
| | 103 | | get { return _timeZone; } |
| | 104 | | set |
| | 105 | | { |
| | 106 | | int val; |
| | 107 | | if (value == "GMT") |
| | 108 | | { |
| | 109 | | val = 0; |
| | 110 | | } |
| | 111 | | else if (value.StartsWith("UTC")) |
| | 112 | | { |
| | 113 | | var num = value.Split('C').Last(); |
| | 114 | | val = Convert.ToInt32(num); |
| | 115 | | } else |
| | 116 | | { |
| | 117 | | val = Convert.ToInt32(value); |
| | 118 | | } |
| | 119 | |
|
| | 120 | | if (val > 14 || val < -12) |
| | 121 | | throw new ArgumentOutOfRangeException($"{nameof(value)} must be in range (-12, 14)."); |
| | 122 | |
|
| | 123 | | if (val > 0) |
| | 124 | | _timeZone = "UTC+" + value; |
| | 125 | | else if (val < 0) |
| | 126 | | _timeZone = "UTC-" + value; |
| | 127 | | else |
| | 128 | | _timeZone = "GMT"; |
| | 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 { |
| | 138 | | get { return _modelRotation; } |
| | 139 | | set |
| | 140 | | { |
| | 141 | | if (value > 360.0 || value < 0.0) |
| | 142 | | throw new ArgumentOutOfRangeException( |
| | 143 | | $"{nameof(value)} must be in range (0, 360) Clockwise."); |
| | 144 | |
|
| | 145 | | _modelRotation = value; |
| | 146 | | } |
| | 147 | | } |
| | 148 | |
|
| | 149 | | public Location(double latitude, double longitude) |
| | 150 | | { |
| | 151 | | Latitude = latitude; |
| | 152 | | Longitude = longitude; |
| | 153 | | LocationName = "Envimet Location"; |
| | 154 | | TimeZone = "0"; |
| | 155 | | ModelRotation = 0.0; |
| | 156 | | TimezoneReference = Location.TIMEZONE_LONGITUDE; |
| | 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) : |
| | 166 | | this(latitude, longitude) |
| | 167 | | { |
| | 168 | | LocationName = locationName; |
| | 169 | | TimeZone = timeZone; |
| | 170 | | ModelRotation = modelRotation; |
| | 171 | | TimezoneReference = timezoneReference; |
| | 172 | | } |
| | 173 | |
|
| | 174 | |
|
| | 175 | | public override string ToString() |
| | 176 | | { |
| | 177 | | return String.Format("{0}::{1}::{2}::{3}", LocationName, |
| | 178 | | Latitude, Longitude, ModelRotation); |
| | 179 | | } |
| | 180 | |
|
| | 181 | | public string Serialize() |
| | 182 | | { |
| | 183 | | return JsonConvert.SerializeObject(this); |
| | 184 | | } |
| | 185 | |
|
| | 186 | | public static Location Deserialize(string json) |
| | 187 | | { |
| | 188 | | try |
| | 189 | | { |
| | 190 | | return JsonConvert.DeserializeObject<Location>(json); |
| | 191 | | } |
| | 192 | | catch (Exception e) |
| | 193 | | { |
| | 194 | | throw new Exception(e.Message); |
| | 195 | | } |
| | 196 | | } |
| | 197 | |
|
| | 198 | | public bool Equals(Location other) |
| | 199 | | { |
| | 200 | | if (other == null) |
| | 201 | | return false; |
| | 202 | |
|
| | 203 | | if (other != null |
| | 204 | | && other.LocationName == this.LocationName |
| | 205 | | && other.TimeZone == this.TimeZone |
| | 206 | | && other.Latitude == this.Latitude |
| | 207 | | && other.Longitude == this.Longitude |
| | 208 | | && other.TimezoneReference == this.TimezoneReference |
| | 209 | | && other.ModelRotation == this.ModelRotation) |
| | 210 | | return true; |
| | 211 | | else |
| | 212 | | return false; |
| | 213 | | } |
| | 214 | |
|
| | 215 | | public override bool Equals(Object obj) |
| | 216 | | { |
| | 217 | | if (obj == null) |
| | 218 | | return false; |
| | 219 | |
|
| | 220 | | var locObj = obj as Location; |
| | 221 | | if (locObj == null) |
| | 222 | | return false; |
| | 223 | | else |
| | 224 | | return Equals(locObj); |
| | 225 | | } |
| | 226 | |
|
| | 227 | | public override int GetHashCode() |
| | 228 | | { |
| | 229 | | unchecked |
| | 230 | | { |
| | 231 | | int hash = 17; |
| | 232 | | hash = hash * 23 + LocationName.GetHashCode(); |
| | 233 | | hash = hash * 23 + TimeZone.GetHashCode(); |
| | 234 | | hash = hash * 23 + Latitude.GetHashCode(); |
| | 235 | | hash = hash * 23 + Longitude.GetHashCode(); |
| | 236 | | hash = hash * 23 + TimezoneReference.GetHashCode(); |
| | 237 | | hash = hash * 23 + ModelRotation.GetHashCode(); |
| | 238 | | return hash; |
| | 239 | | } |
| | 240 | | } |
| | 241 | |
|
| | 242 | | public static bool operator ==(Location loc1, Location loc2) |
| | 243 | | { |
| | 244 | | if (((object)loc1) == null || ((object)loc2) == null) |
| | 245 | | return Object.Equals(loc1, loc2); |
| | 246 | |
|
| | 247 | | return loc1.Equals(loc2); |
| | 248 | | } |
| | 249 | |
|
| | 250 | | public static bool operator !=(Location loc1, Location loc2) |
| | 251 | | { |
| | 252 | | return !(loc1 == loc2); |
| | 253 | | } |
| | 254 | | } |
| | 255 | | } |