public void ParseDDM() { CoordinateDDM coord; Assert.IsTrue(CoordinateDDM.TryParse("40°16.38288', -78°50.84562'", out coord)); Assert.IsTrue(CoordinateDDM.TryParse("40°16.38288', -078°50.84562'", out coord)); Assert.IsFalse(CoordinateDDM.TryParse("This is not a coordinate", out coord)); }
public virtual bool CanGetDDM(int srFactoryCode, out string coord) { CoordinateDDM ddm; if (CoordinateDDM.TryParse(InputCoordinate, out ddm, true)) { coord = ddm.ToString("", new CoordinateDDMFormatter()); return(true); } else { coord = string.Empty; return(false); } }
private CoordinateType GetCoordinateType(string input, out MapPoint point) { point = null; // DD CoordinateDD dd; if (CoordinateDD.TryParse(input, out dd)) { point = QueuedTask.Run(() => { ArcGIS.Core.Geometry.SpatialReference sptlRef = SpatialReferenceBuilder.CreateSpatialReference(4326); return(MapPointBuilder.CreateMapPoint(dd.Lon, dd.Lat, sptlRef)); }).Result; return(CoordinateType.DD); } // DDM CoordinateDDM ddm; if (CoordinateDDM.TryParse(input, out ddm)) { dd = new CoordinateDD(ddm); point = QueuedTask.Run(() => { ArcGIS.Core.Geometry.SpatialReference sptlRef = SpatialReferenceBuilder.CreateSpatialReference(4326); return(MapPointBuilder.CreateMapPoint(dd.Lon, dd.Lat, sptlRef)); }).Result; return(CoordinateType.DDM); } // DMS CoordinateDMS dms; if (CoordinateDMS.TryParse(input, out dms)) { dd = new CoordinateDD(dms); point = QueuedTask.Run(() => { ArcGIS.Core.Geometry.SpatialReference sptlRef = SpatialReferenceBuilder.CreateSpatialReference(4326); return(MapPointBuilder.CreateMapPoint(dd.Lon, dd.Lat, sptlRef)); }).Result; return(CoordinateType.DMS); } return(CoordinateType.Unknown); }
private CoordinateType GetCoordinateType(string input, out MapPoint point) { point = null; // DD CoordinateDD dd; if (CoordinateDD.TryParse(input, out dd)) { point = QueuedTask.Run(() => { ArcGIS.Core.Geometry.SpatialReference sptlRef = SpatialReferenceBuilder.CreateSpatialReference(4326); return(MapPointBuilder.CreateMapPoint(dd.Lon, dd.Lat, sptlRef)); }).Result; return(CoordinateType.DD); } // DDM CoordinateDDM ddm; if (CoordinateDDM.TryParse(input, out ddm)) { dd = new CoordinateDD(ddm); point = QueuedTask.Run(() => { ArcGIS.Core.Geometry.SpatialReference sptlRef = SpatialReferenceBuilder.CreateSpatialReference(4326); return(MapPointBuilder.CreateMapPoint(dd.Lon, dd.Lat, sptlRef)); }).Result; return(CoordinateType.DDM); } // DMS CoordinateDMS dms; if (CoordinateDMS.TryParse(input, out dms)) { dd = new CoordinateDD(dms); point = QueuedTask.Run(() => { ArcGIS.Core.Geometry.SpatialReference sptlRef = SpatialReferenceBuilder.CreateSpatialReference(4326); return(MapPointBuilder.CreateMapPoint(dd.Lon, dd.Lat, sptlRef)); }).Result; return(CoordinateType.DMS); } CoordinateGARS gars; if (CoordinateGARS.TryParse(input, out gars)) { try { point = QueuedTask.Run(() => { ArcGIS.Core.Geometry.SpatialReference sptlRef = SpatialReferenceBuilder.CreateSpatialReference(4326); var tmp = MapPointBuilder.FromGeoCoordinateString(gars.ToString("", new CoordinateGARSFormatter()), sptlRef, GeoCoordinateType.GARS, FromGeoCoordinateMode.Default); return(tmp); }).Result; return(CoordinateType.GARS); } catch { } } CoordinateMGRS mgrs; if (CoordinateMGRS.TryParse(input, out mgrs)) { try { point = QueuedTask.Run(() => { ArcGIS.Core.Geometry.SpatialReference sptlRef = SpatialReferenceBuilder.CreateSpatialReference(4326); var tmp = MapPointBuilder.FromGeoCoordinateString(mgrs.ToString("", new CoordinateMGRSFormatter()), sptlRef, GeoCoordinateType.MGRS, FromGeoCoordinateMode.Default); return(tmp); }).Result; return(CoordinateType.MGRS); } catch { } } CoordinateUSNG usng; if (CoordinateUSNG.TryParse(input, out usng)) { try { point = QueuedTask.Run(() => { ArcGIS.Core.Geometry.SpatialReference sptlRef = SpatialReferenceBuilder.CreateSpatialReference(4326); var tmp = MapPointBuilder.FromGeoCoordinateString(usng.ToString("", new CoordinateMGRSFormatter()), sptlRef, GeoCoordinateType.USNG, FromGeoCoordinateMode.Default); return(tmp); }).Result; return(CoordinateType.USNG); } catch { } } CoordinateUTM utm; if (CoordinateUTM.TryParse(input, out utm)) { try { point = QueuedTask.Run(() => { ArcGIS.Core.Geometry.SpatialReference sptlRef = SpatialReferenceBuilder.CreateSpatialReference(4326); var tmp = MapPointBuilder.FromGeoCoordinateString(utm.ToString("", new CoordinateUTMFormatter()), sptlRef, GeoCoordinateType.UTM, FromGeoCoordinateMode.Default); return(tmp); }).Result; return(CoordinateType.UTM); } catch { } } return(CoordinateType.Unknown); }
private void UpdateSample() { var type = GetCoordinateType(); switch (type) { case CoordinateType.DD: var dd = new CoordinateDD(); if (ctdict.ContainsKey(CoordinateType.DD)) { CoordinateDD.TryParse(ctdict[type], out dd); } Sample = dd.ToString(Format, new CoordinateDDFormatter()); break; case CoordinateType.DDM: var ddm = new CoordinateDDM(); if (ctdict.ContainsKey(type)) { CoordinateDDM.TryParse(ctdict[type], out ddm); } Sample = ddm.ToString(Format, new CoordinateDDMFormatter()); break; case CoordinateType.DMS: var dms = new CoordinateDMS(); if (ctdict.ContainsKey(type)) { CoordinateDMS.TryParse(ctdict[type], out dms); } Sample = dms.ToString(Format, new CoordinateDMSFormatter()); break; case CoordinateType.GARS: var gars = new CoordinateGARS(); if (ctdict.ContainsKey(type)) { CoordinateGARS.TryParse(ctdict[type], out gars); } Sample = gars.ToString(Format, new CoordinateGARSFormatter()); break; case CoordinateType.MGRS: var mgrs = new CoordinateMGRS(); if (ctdict.ContainsKey(type)) { CoordinateMGRS.TryParse(ctdict[type], out mgrs); } Sample = mgrs.ToString(Format, new CoordinateMGRSFormatter()); break; case CoordinateType.USNG: var usng = new CoordinateUSNG(); if (ctdict.ContainsKey(type)) { CoordinateUSNG.TryParse(ctdict[type], out usng); } Sample = usng.ToString(Format, new CoordinateMGRSFormatter()); break; case CoordinateType.UTM: var utm = new CoordinateUTM(); if (ctdict.ContainsKey(type)) { CoordinateUTM.TryParse(ctdict[type], out utm); } Sample = utm.ToString(Format, new CoordinateUTMFormatter()); break; default: break; } RaisePropertyChanged(() => Sample); }
private async Task <CCCoordinate> GetCoordinateType(string input) { MapPoint point = null; // DD CoordinateDD dd; if (CoordinateDD.TryParse(input, out dd, true)) { if (dd.Lat > 90 || dd.Lat < -90 || dd.Lon > 180 || dd.Lon < -180) { return new CCCoordinate() { Type = CoordinateType.Unknown, Point = null } } ; point = await QueuedTask.Run(() => { ArcGIS.Core.Geometry.SpatialReference sptlRef = SpatialReferenceBuilder.CreateSpatialReference(4326); return(MapPointBuilder.CreateMapPoint(dd.Lon, dd.Lat, sptlRef)); });//.Result; return(new CCCoordinate() { Type = CoordinateType.DD, Point = point, PointInformation = dd }); } // DDM CoordinateDDM ddm; if (CoordinateDDM.TryParse(input, out ddm, true)) { dd = new CoordinateDD(ddm); if (dd.Lat > 90 || dd.Lat < -90 || dd.Lon > 180 || dd.Lon < -180) { return new CCCoordinate() { Type = CoordinateType.Unknown, Point = null } } ; point = await QueuedTask.Run(() => { ArcGIS.Core.Geometry.SpatialReference sptlRef = SpatialReferenceBuilder.CreateSpatialReference(4326); return(MapPointBuilder.CreateMapPoint(dd.Lon, dd.Lat, sptlRef)); });//.Result; return(new CCCoordinate() { Type = CoordinateType.DDM, Point = point, PointInformation = ddm }); } // DMS CoordinateDMS dms; if (CoordinateDMS.TryParse(input, out dms, true)) { dd = new CoordinateDD(dms); if (dd.Lat > 90 || dd.Lat < -90 || dd.Lon > 180 || dd.Lon < -180) { return new CCCoordinate() { Type = CoordinateType.Unknown, Point = null } } ; point = await QueuedTask.Run(() => { ArcGIS.Core.Geometry.SpatialReference sptlRef = SpatialReferenceBuilder.CreateSpatialReference(4326); return(MapPointBuilder.CreateMapPoint(dd.Lon, dd.Lat, sptlRef)); });//.Result; return(new CCCoordinate() { Type = CoordinateType.DMS, Point = point, PointInformation = dms }); } CoordinateGARS gars; if (CoordinateGARS.TryParse(input, out gars)) { try { point = await QueuedTask.Run(() => { ArcGIS.Core.Geometry.SpatialReference sptlRef = SpatialReferenceBuilder.CreateSpatialReference(4326); var tmp = MapPointBuilder.FromGeoCoordinateString(gars.ToString("", new CoordinateGARSFormatter()), sptlRef, GeoCoordinateType.GARS, FromGeoCoordinateMode.Default); return(tmp); });//.Result; return(new CCCoordinate() { Type = CoordinateType.GARS, Point = point, PointInformation = gars }); } catch { } } CoordinateMGRS mgrs; if (CoordinateMGRS.TryParse(input, out mgrs)) { try { point = await QueuedTask.Run(() => { ArcGIS.Core.Geometry.SpatialReference sptlRef = SpatialReferenceBuilder.CreateSpatialReference(4326); var tmp = MapPointBuilder.FromGeoCoordinateString(mgrs.ToString(), sptlRef, GeoCoordinateType.MGRS, FromGeoCoordinateMode.Default); return(tmp); });//.Result; return(new CCCoordinate() { Type = CoordinateType.MGRS, Point = point, PointInformation = mgrs }); } catch { } } CoordinateUSNG usng; if (CoordinateUSNG.TryParse(input, out usng)) { try { point = await QueuedTask.Run(() => { ArcGIS.Core.Geometry.SpatialReference sptlRef = SpatialReferenceBuilder.CreateSpatialReference(4326); var tmp = MapPointBuilder.FromGeoCoordinateString(usng.ToString("", new CoordinateMGRSFormatter()), sptlRef, GeoCoordinateType.USNG, FromGeoCoordinateMode.Default); return(tmp); });//.Result; return(new CCCoordinate() { Type = CoordinateType.USNG, Point = point, PointInformation = usng }); } catch { } } CoordinateUTM utm; if (CoordinateUTM.TryParse(input, out utm)) { try { point = await QueuedTask.Run(() => { ArcGIS.Core.Geometry.SpatialReference sptlRef = SpatialReferenceBuilder.CreateSpatialReference(4326); var tmp = MapPointBuilder.FromGeoCoordinateString(utm.ToString("", new CoordinateUTMFormatter()), sptlRef, GeoCoordinateType.UTM, FromGeoCoordinateMode.Default); return(tmp); });//.Result; return(new CCCoordinate() { Type = CoordinateType.UTM, Point = point, PointInformation = utm }); } catch { } } /* * Updated RegEx to capture invalid coordinates like 00, 45, or 456987. */ Regex regexMercator = new Regex(@"^(?<latitude>\-?\d+[.,]?\d*)[+,;:\s]{1,}(?<longitude>\-?\d+[.,]?\d*)"); var matchMercator = regexMercator.Match(input); if (matchMercator.Success && matchMercator.Length == input.Length) { try { var Lat = Double.Parse(matchMercator.Groups["latitude"].Value); var Lon = Double.Parse(matchMercator.Groups["longitude"].Value); var sr = proCoordGetter.Point != null ? proCoordGetter.Point.SpatialReference : SpatialReferences.WebMercator; point = await QueuedTask.Run(() => { return(MapPointBuilder.CreateMapPoint(Lon, Lat, sr)); });//.Result; return(new CCCoordinate() { Type = CoordinateType.DD, Point = point }); } catch (Exception) { return(new CCCoordinate() { Type = CoordinateType.Unknown, Point = null }); } } return(new CCCoordinate() { Type = CoordinateType.Unknown, Point = null }); } } }
private CoordinateType GetCoordinateType(string input, out MapPoint point) { point = null; // DD CoordinateDD dd; if (CoordinateDD.TryParse(input, out dd)) { point = QueuedTask.Run(() => { ArcGIS.Core.Geometry.SpatialReference sptlRef = SpatialReferenceBuilder.CreateSpatialReference(4326); return(MapPointBuilder.CreateMapPoint(dd.Lon, dd.Lat, sptlRef)); }).Result; return(CoordinateType.DD); } // DDM CoordinateDDM ddm; if (CoordinateDDM.TryParse(input, out ddm)) { dd = new CoordinateDD(ddm); point = QueuedTask.Run(() => { ArcGIS.Core.Geometry.SpatialReference sptlRef = SpatialReferenceBuilder.CreateSpatialReference(4326); return(MapPointBuilder.CreateMapPoint(dd.Lon, dd.Lat, sptlRef)); }).Result; return(CoordinateType.DDM); } // DMS CoordinateDMS dms; if (CoordinateDMS.TryParse(input, out dms)) { dd = new CoordinateDD(dms); point = QueuedTask.Run(() => { ArcGIS.Core.Geometry.SpatialReference sptlRef = SpatialReferenceBuilder.CreateSpatialReference(4326); return(MapPointBuilder.CreateMapPoint(dd.Lon, dd.Lat, sptlRef)); }).Result; return(CoordinateType.DMS); } CoordinateGARS gars; if (CoordinateGARS.TryParse(input, out gars)) { try { point = QueuedTask.Run(() => { ArcGIS.Core.Geometry.SpatialReference sptlRef = SpatialReferenceBuilder.CreateSpatialReference(4326); var tmp = MapPointBuilder.FromGeoCoordinateString(gars.ToString("", new CoordinateGARSFormatter()), sptlRef, GeoCoordinateType.GARS, FromGeoCoordinateMode.Default); return(tmp); }).Result; return(CoordinateType.GARS); } catch { } } CoordinateMGRS mgrs; if (CoordinateMGRS.TryParse(input, out mgrs)) { try { point = QueuedTask.Run(() => { ArcGIS.Core.Geometry.SpatialReference sptlRef = SpatialReferenceBuilder.CreateSpatialReference(4326); var tmp = MapPointBuilder.FromGeoCoordinateString(mgrs.ToString("", new CoordinateMGRSFormatter()), sptlRef, GeoCoordinateType.MGRS, FromGeoCoordinateMode.Default); return(tmp); }).Result; return(CoordinateType.MGRS); } catch { } } CoordinateUSNG usng; if (CoordinateUSNG.TryParse(input, out usng)) { try { point = QueuedTask.Run(() => { ArcGIS.Core.Geometry.SpatialReference sptlRef = SpatialReferenceBuilder.CreateSpatialReference(4326); var tmp = MapPointBuilder.FromGeoCoordinateString(usng.ToString("", new CoordinateMGRSFormatter()), sptlRef, GeoCoordinateType.USNG, FromGeoCoordinateMode.Default); return(tmp); }).Result; return(CoordinateType.USNG); } catch { } } CoordinateUTM utm; if (CoordinateUTM.TryParse(input, out utm)) { try { point = QueuedTask.Run(() => { ArcGIS.Core.Geometry.SpatialReference sptlRef = SpatialReferenceBuilder.CreateSpatialReference(4326); var tmp = MapPointBuilder.FromGeoCoordinateString(utm.ToString("", new CoordinateUTMFormatter()), sptlRef, GeoCoordinateType.UTM, FromGeoCoordinateMode.Default); return(tmp); }).Result; return(CoordinateType.UTM); } catch { } } Regex regexMercator = new Regex(@"^(?<latitude>\-?\d+[.,]?\d*)[+,;:\s]*(?<longitude>\-?\d+[.,]?\d*)"); var matchMercator = regexMercator.Match(input); if (matchMercator.Success && matchMercator.Length == input.Length) { try { var Lat = Double.Parse(matchMercator.Groups["latitude"].Value); var Lon = Double.Parse(matchMercator.Groups["longitude"].Value); var sr = proCoordGetter.Point != null ? proCoordGetter.Point.SpatialReference : SpatialReferences.WebMercator; point = QueuedTask.Run(() => { return(MapPointBuilder.CreateMapPoint(Lon, Lat, sr)); }).Result; return(CoordinateType.DD); } catch (Exception ex) { return(CoordinateType.Unknown); } } return(CoordinateType.Unknown); }
private void UpdateOutputs() { foreach (var output in CoordinateConversionLibraryConfig.AddInConfig.OutputCoordinateList) { var props = new Dictionary <string, string>(); string coord = string.Empty; switch (output.CType) { case CoordinateType.DD: CoordinateDD cdd; if (coordinateGetter.CanGetDD(output.SRFactoryCode, out coord) && CoordinateDD.TryParse(coord, out cdd)) { output.OutputCoordinate = cdd.ToString(output.Format, new CoordinateDDFormatter()); var splits = output.Format.Split(new char[] { 'X' }, StringSplitOptions.RemoveEmptyEntries); if (splits.Count() == 2) { props.Add(Properties.Resources.StringLat, cdd.ToString(splits[0].Trim(), new CoordinateDDFormatter())); props.Add(Properties.Resources.StringLon, cdd.ToString("X" + splits[1].Trim(), new CoordinateDDFormatter())); } else { splits = output.Format.Split(new char[] { 'Y' }, StringSplitOptions.RemoveEmptyEntries); if (splits.Count() == 2) { props.Add(Properties.Resources.StringLon, cdd.ToString(splits[0].Trim(), new CoordinateDDFormatter())); props.Add(Properties.Resources.StringLat, cdd.ToString("Y" + splits[1].Trim(), new CoordinateDDFormatter())); } else { props.Add(Properties.Resources.StringLat, cdd.Lat.ToString()); props.Add(Properties.Resources.StringLon, cdd.Lon.ToString()); } } output.Props = props; } break; case CoordinateType.DMS: CoordinateDMS cdms; if (coordinateGetter.CanGetDMS(output.SRFactoryCode, out coord) && CoordinateDMS.TryParse(coord, out cdms)) { output.OutputCoordinate = cdms.ToString(output.Format, new CoordinateDMSFormatter()); var splits = output.Format.Split(new char[] { 'X' }, StringSplitOptions.RemoveEmptyEntries); if (splits.Count() == 2) { props.Add(Properties.Resources.StringLat, cdms.ToString(splits[0].Trim(), new CoordinateDMSFormatter())); props.Add(Properties.Resources.StringLon, cdms.ToString("X" + splits[1].Trim(), new CoordinateDMSFormatter())); } else { splits = output.Format.Split(new char[] { 'Y' }, StringSplitOptions.RemoveEmptyEntries); if (splits.Count() == 2) { props.Add(Properties.Resources.StringLon, cdms.ToString(splits[0].Trim(), new CoordinateDMSFormatter())); props.Add(Properties.Resources.StringLat, cdms.ToString("Y" + splits[1].Trim(), new CoordinateDMSFormatter())); } else { props.Add(Properties.Resources.StringLat, cdms.ToString("A0°B0'C0.0\"N", new CoordinateDMSFormatter())); props.Add(Properties.Resources.StringLon, cdms.ToString("X0°Y0'Z0.0\"E", new CoordinateDMSFormatter())); } } output.Props = props; } break; case CoordinateType.DDM: CoordinateDDM ddm; if (coordinateGetter.CanGetDDM(output.SRFactoryCode, out coord) && CoordinateDDM.TryParse(coord, out ddm)) { output.OutputCoordinate = ddm.ToString(output.Format, new CoordinateDDMFormatter()); var splits = output.Format.Split(new char[] { 'X' }, StringSplitOptions.RemoveEmptyEntries); if (splits.Count() == 2) { props.Add(Properties.Resources.StringLat, ddm.ToString(splits[0].Trim(), new CoordinateDDMFormatter())); props.Add(Properties.Resources.StringLon, ddm.ToString("X" + splits[1].Trim(), new CoordinateDDMFormatter())); } else { splits = output.Format.Split(new char[] { 'Y' }, StringSplitOptions.RemoveEmptyEntries); if (splits.Count() == 2) { props.Add(Properties.Resources.StringLon, ddm.ToString(splits[0].Trim(), new CoordinateDDMFormatter())); props.Add(Properties.Resources.StringLat, ddm.ToString("Y" + splits[1].Trim(), new CoordinateDDMFormatter())); } else { props.Add(Properties.Resources.StringLat, ddm.ToString("A0°B0.0#####'N", new CoordinateDDMFormatter())); props.Add(Properties.Resources.StringLon, ddm.ToString("X0°Y0.0#####'E", new CoordinateDDMFormatter())); } } output.Props = props; } break; case CoordinateType.GARS: CoordinateGARS gars; if (coordinateGetter.CanGetGARS(output.SRFactoryCode, out coord) && CoordinateGARS.TryParse(coord, out gars)) { output.OutputCoordinate = gars.ToString(output.Format, new CoordinateGARSFormatter()); props.Add(Properties.Resources.StringLon, gars.LonBand.ToString()); props.Add(Properties.Resources.StringLat, gars.LatBand); props.Add(Properties.Resources.StringQuadrant, gars.Quadrant.ToString()); props.Add(Properties.Resources.StringKey, gars.Key.ToString()); output.Props = props; } break; case CoordinateType.MGRS: CoordinateMGRS mgrs; if (coordinateGetter.CanGetMGRS(output.SRFactoryCode, out coord) && CoordinateMGRS.TryParse(coord, out mgrs)) { output.OutputCoordinate = mgrs.ToString(output.Format, new CoordinateMGRSFormatter()); props.Add(Properties.Resources.StringGZD, mgrs.GZD); props.Add(Properties.Resources.StringGridSq, mgrs.GS); props.Add(Properties.Resources.StringEasting, mgrs.Easting.ToString("00000")); props.Add(Properties.Resources.StringNorthing, mgrs.Northing.ToString("00000")); output.Props = props; } break; case CoordinateType.USNG: CoordinateUSNG usng; if (coordinateGetter.CanGetUSNG(output.SRFactoryCode, out coord) && CoordinateUSNG.TryParse(coord, out usng)) { output.OutputCoordinate = usng.ToString(output.Format, new CoordinateMGRSFormatter()); props.Add(Properties.Resources.StringGZD, usng.GZD); props.Add(Properties.Resources.StringGridSq, usng.GS); props.Add(Properties.Resources.StringEasting, usng.Easting.ToString("00000")); props.Add(Properties.Resources.StringNorthing, usng.Northing.ToString("00000")); output.Props = props; } break; case CoordinateType.UTM: CoordinateUTM utm; if (coordinateGetter.CanGetUTM(output.SRFactoryCode, out coord) && CoordinateUTM.TryParse(coord, out utm)) { output.OutputCoordinate = utm.ToString(output.Format, new CoordinateUTMFormatter()); props.Add(Properties.Resources.StringZone, utm.Zone.ToString() + utm.Hemi); props.Add(Properties.Resources.StringEasting, utm.Easting.ToString("000000")); props.Add(Properties.Resources.StringNorthing, utm.Northing.ToString("0000000")); output.Props = props; } break; default: break; } } }
/// <summary> /// Returns a formatted coordinate string for an input coordinate string /// IMPORTANT: if a coordinate format is not matched: /// Returns CoordinateType.Unknown, the input string as the formattedString /// </summary> /// <param name="input">Input coord string</param> /// <param name="formattedString">Formatted coord string</param> /// <returns>CoordinateType of the format that matched, Unknown if unmatched</returns> public static CoordinateType GetCoordinateString(string input, out string formattedString) { // We don't want the Ambiguous Coords Dialog to show during these calls // But don't overwrite the setting in case user has set to prompt for this bool savedAmbigCoordSetting = CoordinateConversionLibraryConfig.AddInConfig.DisplayAmbiguousCoordsDlg; CoordinateConversionLibraryConfig.AddInConfig.DisplayAmbiguousCoordsDlg = false; formattedString = input; // DD CoordinateDD dd; if (CoordinateDD.TryParse(input, out dd, true) == true) { formattedString = dd.ToString("Y0.0#####N X0.0#####E", new CoordinateDDFormatter()); return(CoordinateType.DD); } // DDM CoordinateDDM ddm; if (CoordinateDDM.TryParse(input, out ddm, true) == true) { dd = new CoordinateDD(ddm); formattedString = ddm.ToString("", new CoordinateDDMFormatter()); return(CoordinateType.DDM); } // DMS CoordinateDMS dms; if (CoordinateDMS.TryParse(input, out dms, true) == true) { dd = new CoordinateDD(dms); formattedString = dms.ToString("A0°B0'C0.0##\"N X0°Y0'Z0.0##\"E", new CoordinateDMSFormatter()); return(CoordinateType.DMS); } // restore the setting (not needed for others, only DD/DMS CoordinateConversionLibraryConfig.AddInConfig.DisplayAmbiguousCoordsDlg = savedAmbigCoordSetting; // GARS CoordinateGARS gars; if (CoordinateGARS.TryParse(input, out gars) == true) { formattedString = gars.ToString("", new CoordinateGARSFormatter()); return(CoordinateType.GARS); } // MGRS CoordinateMGRS mgrs; if (CoordinateMGRS.TryParse(input, out mgrs) == true) { formattedString = mgrs.ToString("ZSX00000Y00000", new CoordinateMGRSFormatter()); return(CoordinateType.MGRS); } // USNG CoordinateUSNG usng; if (CoordinateUSNG.TryParse(input, out usng) == true) { formattedString = usng.ToString("ZSX00000Y00000", new CoordinateMGRSFormatter()); return(CoordinateType.USNG); } // UTM CoordinateUTM utm; if (CoordinateUTM.TryParse(input, out utm) == true) { formattedString = utm.ToString("", new CoordinateUTMFormatter()); return(CoordinateType.UTM); } return(CoordinateType.Unknown); }
private CoordinateType GetCoordinateType(string input, out ESRI.ArcGIS.Geometry.IPoint point) { point = new PointClass(); var cn = (IConversionNotation)point; Type t = Type.GetTypeFromProgID("esriGeometry.SpatialReferenceEnvironment"); if (t == null) { return(CoordinateType.Unknown); } System.Object obj = Activator.CreateInstance(t); if (obj == null) { return(CoordinateType.Unknown); } ISpatialReferenceFactory srFact = obj as ISpatialReferenceFactory; if (srFact == null) { return(CoordinateType.Unknown); } // Use the enumeration to create an instance of the predefined object. IGeographicCoordinateSystem geographicCS = srFact.CreateGeographicCoordinateSystem((int) esriSRGeoCSType.esriSRGeoCS_WGS1984); point.SpatialReference = geographicCS; string numSep = System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator; input = numSep != "." ? input.Replace(numSep, ".") : input; try { CoordinateDD dd; if (CoordinateDD.TryParse(input, out dd, true)) { // Reformat the string for cases where lat/lon have been switched // PutCoords calls fail if the double uses decimal separator other than a decimal point // Added InvariantCulture option to ensure the current culture is ignored string newInput = string.Format(CultureInfo.InvariantCulture, "{0} {1}", dd.Lat, dd.Lon); cn.PutCoordsFromDD(newInput); return(CoordinateType.DD); } } catch { /* Conversion Failed */ } try { CoordinateDDM ddm; if (CoordinateDDM.TryParse(input, out ddm, true)) { // Reformat the string for cases where lat/lon have been switched // PutCoords calls fail if the double uses decimal separator other than a decimal point // Added InvariantCulture option to ensure the current culture is ignored string newInput = string.Format(CultureInfo.InvariantCulture, "{0} {1} {2} {3}", ddm.LatDegrees, ddm.LatMinutes, ddm.LonDegrees, ddm.LonMinutes); cn.PutCoordsFromDD(newInput); return(CoordinateType.DDM); } } catch { /* Conversion Failed */ } try { CoordinateDMS dms; if (CoordinateDMS.TryParse(input, out dms, true)) { // Reformat the string for cases where lat/lon have been switched // PutCoords calls fail if the double uses decimal separator other than a decimal point // Added InvariantCulture option to ensure the current culture is ignored string newInput = string.Format(CultureInfo.InvariantCulture, "{0} {1} {2} {3} {4} {5}", dms.LatDegrees, dms.LatMinutes, dms.LatSeconds, dms.LonDegrees, dms.LonMinutes, dms.LonSeconds); cn.PutCoordsFromDD(newInput); return(CoordinateType.DMS); } } catch { /* Conversion Failed */ } try { cn.PutCoordsFromGARS(esriGARSModeEnum.esriGARSModeCENTER, input); return(CoordinateType.GARS); } catch { /* Conversion Failed */ } try { cn.PutCoordsFromGARS(esriGARSModeEnum.esriGARSModeLL, input); return(CoordinateType.GARS); } catch { /* Conversion Failed */ } CoordinateGARS gars; if (CoordinateGARS.TryParse(input, out gars)) { try { cn.PutCoordsFromGARS(esriGARSModeEnum.esriGARSModeCENTER, gars.ToString("", new CoordinateGARSFormatter())); return(CoordinateType.GARS); } catch { /* Conversion Failed */ } } try { cn.PutCoordsFromMGRS(input, esriMGRSModeEnum.esriMGRSMode_Automatic); return(CoordinateType.MGRS); } catch { /* Conversion Failed */ } try { cn.PutCoordsFromMGRS(input, esriMGRSModeEnum.esriMGRSMode_NewStyle); return(CoordinateType.MGRS); } catch { /* Conversion Failed */ } try { cn.PutCoordsFromMGRS(input, esriMGRSModeEnum.esriMGRSMode_NewWith180InZone01); return(CoordinateType.MGRS); } catch { /* Conversion Failed */ } try { cn.PutCoordsFromMGRS(input, esriMGRSModeEnum.esriMGRSMode_OldStyle); return(CoordinateType.MGRS); } catch { /* Conversion Failed */ } try { cn.PutCoordsFromMGRS(input, esriMGRSModeEnum.esriMGRSMode_OldWith180InZone01); return(CoordinateType.MGRS); } catch { /* Conversion Failed */ } // mgrs try parse CoordinateMGRS mgrs; if (CoordinateMGRS.TryParse(input, out mgrs)) { try { cn.PutCoordsFromMGRS(mgrs.ToString("", new CoordinateMGRSFormatter()), esriMGRSModeEnum.esriMGRSMode_NewStyle); return(CoordinateType.MGRS); } catch { /* Conversion Failed */ } } try { cn.PutCoordsFromUSNG(input); return(CoordinateType.USNG); } catch { /* Conversion Failed */ } CoordinateUSNG usng; if (CoordinateUSNG.TryParse(input, out usng)) { try { cn.PutCoordsFromUSNG(usng.ToString("", new CoordinateMGRSFormatter())); return(CoordinateType.USNG); } catch { /* Conversion Failed */ } } try { cn.PutCoordsFromUTM(esriUTMConversionOptionsEnum.esriUTMAddSpaces, input); return(CoordinateType.UTM); } catch { /* Conversion Failed */ } try { cn.PutCoordsFromUTM(esriUTMConversionOptionsEnum.esriUTMNoOptions, input); return(CoordinateType.UTM); } catch { /* Conversion Failed */ } try { cn.PutCoordsFromUTM(esriUTMConversionOptionsEnum.esriUTMUseNS, input); return(CoordinateType.UTM); } catch { /* Conversion Failed */ } CoordinateUTM utm; if (CoordinateUTM.TryParse(input, out utm)) { try { cn.PutCoordsFromUTM(esriUTMConversionOptionsEnum.esriUTMNoOptions, utm.ToString("", new CoordinateUTMFormatter())); return(CoordinateType.UTM); } catch { /* Conversion Failed */ } } /* * Updated RegEx to capture invalid coordinates like 00, 45, or 456987. */ Regex regexMercator = new Regex(@"^(?<latitude>\-?\d+[.,]?\d*)[+,;:\s]{1,}(?<longitude>\-?\d+[.,]?\d*)"); var matchMercator = regexMercator.Match(input); if (matchMercator.Success && matchMercator.Length == input.Length) { try { var Lat = Double.Parse(matchMercator.Groups["latitude"].Value); var Lon = Double.Parse(matchMercator.Groups["longitude"].Value); IMap map = ((IMxDocument)ArcMap.Application.Document).FocusMap; var sr = map.SpatialReference != null ? map.SpatialReference : ArcMapHelpers.GetSR((int)esriSRProjCS3Type.esriSRProjCS_WGS1984WebMercatorMajorAuxSphere); point.X = Lon; point.Y = Lat; point.SpatialReference = sr; return(CoordinateType.DD); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } } return(CoordinateType.Unknown); }
public Dictionary <string, string> GetOutputFormats(AddInPoint input) { var results = new Dictionary <string, string>(); IPoint point; var inputText = input.Point.Y + " " + input.Point.X; var ctype = GetCoordinateType(inputText, out point); if (point != null) { ArcMapCoordinateGet arcMapCoordinateGetter = new ArcMapCoordinateGet(); arcMapCoordinateGetter.Point = point; CoordinateGetBase coordinateGetter = arcMapCoordinateGetter as CoordinateGetBase; results.Add(CoordinateFieldName, input.Text); CoordinateBase.IsOutputInProcess = true; foreach (var output in CoordinateConversionLibraryConfig.AddInConfig.OutputCoordinateList) { var props = new Dictionary <string, string>(); string coord = string.Empty; switch (output.CType) { case CoordinateType.DD: CoordinateDD cdd; if (coordinateGetter.CanGetDD(output.SRFactoryCode, out coord) && CoordinateDD.TryParse(coord, out cdd, true)) { results.Add(output.Name, cdd.ToString(output.Format, new CoordinateDDFormatter())); } break; case CoordinateType.DMS: CoordinateDMS cdms; if (coordinateGetter.CanGetDMS(output.SRFactoryCode, out coord) && CoordinateDMS.TryParse(coord, out cdms, true)) { results.Add(output.Name, cdms.ToString(output.Format, new CoordinateDMSFormatter())); } break; case CoordinateType.DDM: CoordinateDDM ddm; if (coordinateGetter.CanGetDDM(output.SRFactoryCode, out coord) && CoordinateDDM.TryParse(coord, out ddm, true)) { results.Add(output.Name, ddm.ToString(output.Format, new CoordinateDDMFormatter())); } break; case CoordinateType.GARS: CoordinateGARS gars; if (coordinateGetter.CanGetGARS(output.SRFactoryCode, out coord) && CoordinateGARS.TryParse(coord, out gars)) { results.Add(output.Name, gars.ToString(output.Format, new CoordinateGARSFormatter())); } break; case CoordinateType.MGRS: CoordinateMGRS mgrs; if (coordinateGetter.CanGetMGRS(output.SRFactoryCode, out coord) && CoordinateMGRS.TryParse(coord, out mgrs)) { results.Add(output.Name, mgrs.ToString(output.Format, new CoordinateMGRSFormatter())); } break; case CoordinateType.USNG: CoordinateUSNG usng; if (coordinateGetter.CanGetUSNG(output.SRFactoryCode, out coord) && CoordinateUSNG.TryParse(coord, out usng)) { results.Add(output.Name, usng.ToString(output.Format, new CoordinateMGRSFormatter())); } break; case CoordinateType.UTM: CoordinateUTM utm; if (coordinateGetter.CanGetUTM(output.SRFactoryCode, out coord) && CoordinateUTM.TryParse(coord, out utm)) { results.Add(output.Name, utm.ToString(output.Format, new CoordinateUTMFormatter())); } break; default: break; } } CoordinateBase.IsOutputInProcess = false; } return(results); }
public static string GetFormattedCoord(CoordinateType cType, string coord, string format) { if (cType == CoordinateType.DD) { CoordinateDD dd; if (CoordinateDD.TryParse(coord, out dd)) { return(dd.ToString(format, new CoordinateDDFormatter())); } } if (cType == CoordinateType.DDM) { CoordinateDDM ddm; if (CoordinateDDM.TryParse(coord, out ddm)) { return(ddm.ToString(format, new CoordinateDDMFormatter())); } } if (cType == CoordinateType.DMS) { CoordinateDMS dms; if (CoordinateDMS.TryParse(coord, out dms)) { return(dms.ToString(format, new CoordinateDMSFormatter())); } } if (cType == CoordinateType.GARS) { CoordinateGARS gars; if (CoordinateGARS.TryParse(coord, out gars)) { return(gars.ToString(format, new CoordinateGARSFormatter())); } } if (cType == CoordinateType.MGRS) { CoordinateMGRS mgrs; if (CoordinateMGRS.TryParse(coord, out mgrs)) { return(mgrs.ToString(format, new CoordinateMGRSFormatter())); } } if (cType == CoordinateType.USNG) { CoordinateUSNG usng; if (CoordinateUSNG.TryParse(coord, out usng)) { return(usng.ToString(format, new CoordinateMGRSFormatter())); } } if (cType == CoordinateType.UTM) { CoordinateUTM utm; if (CoordinateUTM.TryParse(coord, out utm)) { return(utm.ToString(format, new CoordinateUTMFormatter())); } } return(null); }