internal IPoint GetPointFromString(string coordinate)
{
Type t = Type.GetTypeFromProgID("esriGeometry.SpatialReferenceEnvironment");
System.Object obj = Activator.CreateInstance(t);
ISpatialReferenceFactory srFact = obj as ISpatialReferenceFactory;
// Use the enumeration to create an instance of the predefined object.
IGeographicCoordinateSystem geographicCS =
srFact.CreateGeographicCoordinateSystem((int)
esriSRGeoCSType.esriSRGeoCS_WGS1984);
var point = new Point() as IPoint;
point.SpatialReference = geographicCS;
var cn = point as IConversionNotation;
if (cn == null)
return null;
try { cn.PutCoordsFromDD(coordinate); return point; }
catch { }
try { cn.PutCoordsFromDDM(coordinate); return point; }
catch { }
try { cn.PutCoordsFromDMS(coordinate); return point; }
catch { }
try { cn.PutCoordsFromGARS(esriGARSModeEnum.esriGARSModeCENTER, coordinate); return point; }
catch { }
try { cn.PutCoordsFromGARS(esriGARSModeEnum.esriGARSModeLL, coordinate); return point; }
catch { }
try { cn.PutCoordsFromMGRS(coordinate, esriMGRSModeEnum.esriMGRSMode_Automatic); return point; }
catch { }
try { cn.PutCoordsFromMGRS(coordinate, esriMGRSModeEnum.esriMGRSMode_NewStyle); return point; }
catch { }
try { cn.PutCoordsFromMGRS(coordinate, esriMGRSModeEnum.esriMGRSMode_NewWith180InZone01); return point; }
catch { }
try { cn.PutCoordsFromMGRS(coordinate, esriMGRSModeEnum.esriMGRSMode_OldStyle); return point; }
catch { }
try { cn.PutCoordsFromMGRS(coordinate, esriMGRSModeEnum.esriMGRSMode_OldWith180InZone01); return point; }
catch { }
try { cn.PutCoordsFromMGRS(coordinate, esriMGRSModeEnum.esriMGRSMode_USNG); return point; }
catch { }
try { cn.PutCoordsFromUSNG(coordinate); return point; }
catch { }
try { cn.PutCoordsFromUTM(esriUTMConversionOptionsEnum.esriUTMAddSpaces, coordinate); return point; }
catch { }
try { cn.PutCoordsFromUTM(esriUTMConversionOptionsEnum.esriUTMUseNS, coordinate); return point; }
catch { }
try { cn.PutCoordsFromUTM(esriUTMConversionOptionsEnum.esriUTMAddSpaces | esriUTMConversionOptionsEnum.esriUTMUseNS, coordinate); return point; }
catch { }
try { cn.PutCoordsFromUTM(esriUTMConversionOptionsEnum.esriUTMNoOptions, coordinate); return point; }
catch { }
try { cn.PutCoordsFromGeoRef(coordinate); return point; }
catch { }
// lets see if we have a PCS coordinate
// we'll assume the same units as the map units
// get spatial reference of map
if (ArcMap.Document == null || ArcMap.Document.FocusMap == null || ArcMap.Document.FocusMap.SpatialReference == null)
return null;
var map = ArcMap.Document.FocusMap;
var pcs = map.SpatialReference as IProjectedCoordinateSystem;
if (pcs == null)
return null;
point.SpatialReference = map.SpatialReference;
// get pcs coordinate from input
coordinate = coordinate.Trim();
Regex regexMercator = new Regex(@"^(?<latitude>\-?\d+\.?\d*)[+,;:\s]*(?<longitude>\-?\d+\.?\d*)");
var matchMercator = regexMercator.Match(coordinate);
if (matchMercator.Success && matchMercator.Length == coordinate.Length)
{
try
{
var Lat = Double.Parse(matchMercator.Groups["latitude"].Value);
var Lon = Double.Parse(matchMercator.Groups["longitude"].Value);
point.PutCoords(Lon, Lat);
return point;
}
catch
{
return null;
}
}
return null;
}