I am using ArcObjects C# tool in ArcGIS that identifies map coordinates when a user clicks on a location within a map. Here's the code that I have used:
protected override void OnMouseDown(MouseEventArgs arg)
{
Fr1.Show();
IMxDocument pMxDoc =(IMxDocument)ArcMap.Application.Document;
IPoint pPoint = pMxDoc.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(arg.X, arg.Y);
MessageBox.Show("Map X: " + pPoint.X + "\nMap Y: " + pPoint.Y);
}
My map use units of Meters so the tool displays the Meters coordinates. How can I have this tool return UTM ?
-
By UTM, do you mean MGRS or USNG or aother alphanumeric version? For instance, 11SAB23445678 or 11S 345678 4349584?mkennedy– mkennedy2017年05月03日 20:22:55 +00:00Commented May 3, 2017 at 20:22
-
i mean WGS_1984_UTM_Zone_32NSaif Othmani– Saif Othmani2017年05月04日 10:10:45 +00:00Commented May 4, 2017 at 10:10
2 Answers 2
Once you have your IPoint reference then use the Project method to project it to whatever coordinate system you like.
IGeometry.Project help from Esri
Here's a code example:
Type t = Type.GetTypeFromProgID("esriGeometry.SpatialReferenceEnvironment");
System.Object obj = Activator.CreateInstance(t);
ISpatialReferenceFactory spatialRefFactory= obj as ISpatialReferenceFactory;
ISpatialReference spatialReference = spatialRefFactory.CreateProjectedCoordinateSystem(<ZoneNumber>);
pPoint.Project(spatialReference);
I'm a little confused by your question since UTM is also meters, but the easiest way to change from your current coordinate system to another is to project it.
//get your desired spatial reference
ISpatialReferenceFactory3 spatialReferenceFactory = new SpatialReferenceEnvironment() as ISpatialReferenceFactory3;
ISpatialReference spatialReference = spatialReferenceFactory.CreateSpatialReference((int)esriSRProjCSType.esriSRProjCS_WGS1984UTM_54N);
pPoint.Project(spatialReference);
In this case it will project to UTM 54N (1984). You can switch that out in the CreateSpatialReference parameter with any of the enumerations in:
- esriSRProjCSType
- esriSRProjCS2Type
- esriSRProjCS3Type
- esriSRProjCS4Type
There are also:
esriSRGeoCSType
esriSRGeoCS2Type
esriSRGeoCS3Type
if you want to go to a geographic coordinate system.
The pPoint.Project() method depends on pPoint already having a SpatialReference assigned to it. I'm not sure if the ToMapPoint() method returns a point with a SpatialReference. If pPoint doesn't have one simply assign it using the
pPoint.SpatialReference = pMxDoc.FocusMap.SpatialReference;
Explore related questions
See similar questions with these tags.