Select to view content in your preferred language

Get Map Coordinates while in Layout View

1533
4
04-12-2012 10:56 AM
LucieBoucher1
Deactivated User
Hi,

I have built a tool in C# that performs the following : when the user clicks on the map, the tool gets the coordinates, and then adds a new point feature to a feature class with several attributes being edited according to the location of the point.  Of course, the point is added exactly where the user clicked, on the map.

The tool works fine when I use it in DataView.  However, the users might use it also in Layout View (in fact they will mostly use it in Layout View).  When they do, a point is created, but does not appear at the proper location because the coordinates used are the paper coordinates in centimeters or inches instead of the map coordinates in meters or degrees.  I was therefore wondering how to get the dataframe's coordinates while working in the Layout View, with an OnMouseDown function.  A particular aspect of the problem is that on the Layout View, I display more than one dataframes, with the same coordinate system, but with different scales . 

Here is the code I use for the tool (everything is within a protected override void OnMouseDown function) :
//Get the coordinates
IPoint newSite = new Point();
IActiveView activeView = ArcMap.Document.ActiveView;
IScreenDisplay screenDisplay = activeView.ScreenDisplay;
IPoint clickedPoint = screenDisplay.DisplayTransformation.ToMapPoint(arg.X, arg.Y);
newSite.PutCoords(clickedPoint.X, clickedPoint.Y);
newSite.SpatialReference = clickedPoint.SpatialReference;

//Reproject if required
if (newSite.SpatialReference.Name != "GCS_North_American_1983")
{
    //Reprojeter
    ISpatialReferenceFactory srFactory = new SpatialReferenceEnvironment();
    ISpatialReference sr;
    IGeographicCoordinateSystem gcs = srFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_NAD1983);
    sr = gcs;
    newSite.Project(sr);
}

//...
//Skip a long and boring part where I determine strings to edit attributes...
//...

//Get the Layer
ILayer layerPlants = FindLayer("Plants Lucie"); //Note : the FindLayer function loops through the datasets, and then through the layers, to return an ILayer
//Get the Feature Class
IFeatureLayer featurelayerPlants = layerPlants as IFeatureLayer;
IFeatureClass featureclassPlants = featurelayerPlants.FeatureClass;

//Make sure the FeatureClass has a proper geometry (point)
if (featureclassPlants.ShapeType == esriGeometryType.esriGeometryPoint)
{
    //Create the point
    IFeature plant = featureclassPlants.CreateFeature();
    plant.Shape = newSite;

    //...
    //Boring part where I edit the point attributes
    //...

    //Commit du nouveau point
    plant.Store();

    //Refresh the active view to see the new point
    activeView.Refresh();
}


I suppose there could be an if/else condition such as "if we are working in dataview, get these coordinates using method A, else get the coordinates using method B", and that the key to those methods is probably linked to the screenDisplay.DisplayTransformation.ToMapPoint, but I don't know how to perform the switch, and how to extract the proper coordinates.

Thank you very much for your help, once again!
0 Kudos
4 Replies
NeilClemmons
Honored Contributor
The active view of a document can be either the map or the page layout depending on which view the document is in.  If you want your tool to only work on the map then get your active view reference from the document's focus map reference instead of its active view reference.

IActiveView activeView = ArcMap.Document.ActiveView;

Change the line above to get the focus map instead of the active view.  When the document is in layout view then your tool will only respond to clicks on the map and will ignore clicks on the layout.
0 Kudos
LucieBoucher1
Deactivated User
The active view of a document can be either the map or the page layout depending on which view the document is in.  If you want your tool to only work on the map then get your active view reference from the document's focus map reference instead of its active view reference.

IActiveView activeView = ArcMap.Document.ActiveView;

Change the line above to get the focus map instead of the active view.  When the document is in layout view then your tool will only respond to clicks on the map and will ignore clicks on the layout.



Thanks Neil for your answer!

I switched the
IActiveView activeView = ArcMap.Document.ActiveView;

for a
IMap focusMap = ArcMap.Document.FocusMap;

but then I was using
IScreenDisplay screenDisplay = activeView.ScreenDisplay;
IPoint clickedPoint = screenDisplay.DisplayTransformation.ToMapPoint(arg.X, arg.Y);

to get the coordinates.  Is there an equivalent for this operation with the focusMap?

Thank you very much for your help, once again!
0 Kudos
NeilClemmons
Honored Contributor
You should have changed this line:

IActiveView activeView = ArcMap.Document.ActiveView;

To this:

IActiveView activeView = ArcMap.Document.FocusMap As IActiveView;
0 Kudos
LucieBoucher1
Deactivated User
You should have changed this line:
IActiveView activeView = ArcMap.Document.ActiveView;
To this:
IActiveView activeView = ArcMap.Document.FocusMap As IActiveView;


That works great, thank you so much for your help!

My MXD has two dataframes.  Is there a way for the tool to determine on wich dataframe the user clicks?  Right now, the feature class being edited is in the first dataframe.  If the user clicks in the second dataframe, the tool "extends" the first one to get the coordinates, instead of using coordinates from the second dataset.  Is there a way for me to change this situation?  I don't know how to explain it better...
0 Kudos