ArcGIS USNG Coordinate Tool


/// Updated Aug 1,2009. ArcGIS 9.3.1 Web ADF

using ESRI.ArcGIS.ADF.ArcGISServer;

using ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer;

using ESRI.ArcGIS.ADF.Web.UI.WebControls;

using ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools;

using ESRI.ArcGIS.Geometry;


/* Add this custom tool to a toolbar associated with a

map control on a ArcGIS Server (ags) website. It can be used to add a usng coordinate (of correct precision based on map scale)

* and a label at any location on the map. */


public class CustomUSNGTool : IMapServerToolAction


public void ServerAction(ToolEventArgs args)


Map map = (Map)args.Control;


//For placing text element at Click location, Get Map functionality from Map Control and then get map description from map functionality

MapFunctionality mf = (MapFunctionality)map.GetFunctionality(0);

MapDescription mapDescription = mf.MapDescription;

PointEventArgs pargs = (PointEventArgs)args;

IConversionMGRS convertPoint;

IGeographicCoordinateSystem m_GCS;

IProjectedCoordinateSystem m_PCS;

ISpatialReferenceFactory2 pSpatRefFact;

string mUSNG = ” “;


// Screen to web adf


/// Get transformation parameters from the Map control

ESRI.ArcGIS.ADF.Web.Geometry.TransformationParams transParams = map.GetTransformationParams(ESRI.ArcGIS.ADF.Web.Geometry.TransformationDirection.ToMap);

ESRI.ArcGIS.ADF.Web.Geometry.Point pPoint = ESRI.ArcGIS.ADF.Web.Geometry.Point.ToMapPoint(pargs.ScreenPoint, transParams);


/// Convert Web ADF point to USNG point using iconversionmgrs interface

pSpatRefFact = new ESRI.ArcGIS.Geometry.SpatialReferenceEnvironmentClass();

m_GCS = pSpatRefFact.CreateGeographicCoordinateSystem(4269); // Nad 1983

m_PCS = pSpatRefFact.CreateProjectedCoordinateSystem(102008); // North America Albers

/// Change the projection constants based on your geographic or projected coordinate system

ESRI.ArcGIS.Geometry.Point selPoint = new ESRI.ArcGIS.Geometry.Point();

selPoint.SpatialReference = m_GCS;

// selPoint.SpatialReference = m_PCS; // Uncomment for projected cs.

selPoint.PutCoords(pPoint.X, pPoint.Y);

convertPoint = (ESRI.ArcGIS.Geometry.IConversionMGRS)selPoint;

/// Get Current Map scale and set USNG precision (Number of digits)

int sc;

if (map.Scale >= 250000)


sc = 2;


else if (map.Scale >= 100000)


sc = 3;


else if (map.Scale >= 25000)


sc = 4;




sc = 5;


string currentScale = map.Scale.ToString();

// SendBackJS(map, string.Format(“alert(‘Map scale = {0}’);”, currentScale));

mUSNG = convertPoint.CreateMGRS(sc, false, ESRI.ArcGIS.Geometry.esriMGRSModeEnum.esriMGRSMode_USNG);


Text element will be added at click location at the server tier using ArcGIS Server SOAP API

Since we’re going to add custom graphics to the map generated by ArcGIS Server, we need to get the Web ADF functionality associated with drawing map images.

When a Map control consumes a resource, it generates a MapFunctionality, one for each resource. As a result, a Map control can have an array of MapFunctionalities.In this case, there is only one resource, and ArcGIS Server resource. To get the MapFunctionality associated with the first (and in this case only) resource, use the following code.

Since we know it is a type of ArcGIS Server resource, we can cast the functionality to the ArcGIS Server specific MapFunctionality. We’ll need to do this to work with ArcGIS Server specific implementation classes to draw graphics and work with server context.



/// ESRI.ArcGIS.ADF.ArcGISServer namespace contains classes for working with ArcObjects on the GIS server

/// Web ADF to ArcGIS Server SOAP


ESRI.ArcGIS.ADF.ArcGISServer.PointN ags_map_point;

ags_map_point = ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.FromAdfPoint(pPoint);

ESRI.ArcGIS.ADF.ArcGISServer.TextElement text = new ESRI.ArcGIS.ADF.ArcGISServer.TextElement();

text.TextGeometry = ags_map_point;

ESRI.ArcGIS.ADF.ArcGISServer.TextSymbol textSymbol = new ESRI.ArcGIS.ADF.ArcGISServer.TextSymbol();

ESRI.ArcGIS.ADF.ArcGISServer.RgbColor rgb = new ESRI.ArcGIS.ADF.ArcGISServer.RgbColor();

rgb.Red = 255;

rgb.Green = 0;

rgb.Blue = 0;

rgb.AlphaValue = 255;

textSymbol.Color = rgb;

textSymbol.Size = 10;

textSymbol.FontName = “Tahoma”;

text.Symbol = textSymbol;

text.Scale = true;

System.Web.UI.WebControls.TextBox tbox = (System.Web.UI.WebControls.TextBox)args.Control.Page.FindControl(“ClickedCoord_Textbox”); /// find USNG label textbox

text.Text = mUSNG + “\n” + tbox.Text;

///text.Text = mUSNG;

/// Flip the comments on the above two lines if you do not have/need a usng label

/* Add text, marker and label at same location

The CustomGraphics property on the MapDescription Value object accepts an array of GraphicElement Value objects. Both MarkerElement and PolygonElement derive from the GraphicElement base class. So all we need to do is create a new GraphicElement array and initialize it with the number of elements. Add the MarkerElement and PolygonElement to the array and assign it to the MapDescription.CustomGraphics property.


ESRI.ArcGIS.ADF.ArcGISServer.RgbColor rgb1;

rgb1 = new ESRI.ArcGIS.ADF.ArcGISServer.RgbColor();

rgb1.Red = 255;

rgb1.Green = 0;

rgb1.Blue = 0;

rgb1.AlphaValue = 255;

ESRI.ArcGIS.ADF.ArcGISServer.SimpleMarkerSymbol sms;

sms = new ESRI.ArcGIS.ADF.ArcGISServer.SimpleMarkerSymbol();

sms.Style = ESRI.ArcGIS.ADF.ArcGISServer.esriSimpleMarkerStyle.esriSMSCross;

sms.Color = rgb1;

sms.Size = 5.0;

ESRI.ArcGIS.ADF.ArcGISServer.MarkerElement marker;

marker = new ESRI.ArcGIS.ADF.ArcGISServer.MarkerElement();

marker.Symbol = sms;

marker.Point = ags_map_point;

// Add point and text element at the same time

if (mapDescription.CustomGraphics != null)


ESRI.ArcGIS.ADF.ArcGISServer.GraphicElement[] oldges = mapDescription.CustomGraphics;

int cnt = oldges.Length;

ESRI.ArcGIS.ADF.ArcGISServer.GraphicElement[] newges = new ESRI.ArcGIS.ADF.ArcGISServer.GraphicElement[cnt + 2];

oldges.CopyTo(newges, 0);

newges[cnt] = text;

newges[cnt + 1] = marker;

mapDescription.CustomGraphics = newges;




ESRI.ArcGIS.ADF.ArcGISServer.GraphicElement[] ges = new ESRI.ArcGIS.ADF.ArcGISServer.GraphicElement[2];

ges[0] = text;

ges[1] = marker;

mapDescription.CustomGraphics = ges;


// Refresh map control





private void SendBackJS(Map pMap, string strJS)


CallbackResult pCr = new CallbackResult(null, null, “javascript”, strJS);