ArcGIS Zoom to USNG Coordinate Tool

Zoom to USNG Coordinate tool

This is my first shot at explaining the callback framework implemented in the ESRI ArcGIS Server Web ADF. So please feel free to ask questions if you get lost. Create two input html textboxes (not the System.Web.UI.WebControls textbox) and one input html button.

The ‘Add USNG Point’ Button’s Click event triggers the following JavaScript in your Default.aspx page.

function Button1_onclick() {

var x = document.getElementById(‘TextBox_USNG’).value;

var lab = document.getElementById(‘Loc’).value;

var message = ‘ControlType=Map’;

message += ‘&’ + ‘EventArg’+ ‘=’ + ‘zoomtopoint’ + ‘&’+ ‘usngPoint’+ ‘=’ + x + ‘&’ + ‘usngLocationLabel’+ ‘=’ + lab;

var context = ‘Map1’;

<%=sCallBackFunctionInvocation%>

}

This callback raised by the client computer has to be handled and the response returned to the client by the server ie. Your default.aspx.cs page in your web mapping application.

/* Default aspx.cs – Code behind page of the Default page of your application. This following code snippets must be added to the default page to create the “Zoom to USNG tool.

1.Make sure the ICallbackEventHandler is implemented in your default class.

2. Declare the string variables for the Callback invocation function (sCallBackFunctionInvocation )and the response string (mapstring) and save the orginal scale of the map when it loads initially.

3. The raisecallback event handler parses and decodes the callback string and passes the “USNGLocationlabel” and the “USNGPoint” to the ZoomtoPointServer function.

4. ZoomtoPointServer function does the reverse of the USNG Coordinate tool – ie, it converts the typed in USNG Coordinate to a annotated point graphic on the map, changes the map scale to the correct precision to zoom to that location.

*/

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Collections.Generic;

using System.Collections.Specialized;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using ESRI.ArcGIS.ADF.Web;

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

using ESRI.ArcGIS.ADF.Web.DataSources;

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

using ESRI.ArcGIS.ADF.Connection.AGS;

using ESRI.ArcGIS.Server;

using ESRI.ArcGIS.Geometry;

 

public partial class _Default : System.Web.UI.Page, ICallbackEventHandler

 

{

double mapOriginalScale;

public string sCallBackFunctionInvocation;

private string mapstring = “”;

 

protected void Page_Load(object sender, EventArgs e)

{

sCallBackFunctionInvocation = Page.ClientScript.GetCallbackEventReference(this, “message”, “processCallbackResult”, “context”, “postBackError”, true);

mapOriginalScale = Map1.Scale;

}

#region ICallbackEventHandler Members

 

public string GetCallbackResult()

{

return mapstring;

}

 

public void RaiseCallbackEvent(string responseString)

{

// break out the responseString into a querystring

Array keyValuePairs = responseString.Split(“&”.ToCharArray());

NameValueCollection m_queryString = new NameValueCollection();

string[] keyValue;

///string response = “”;

if (keyValuePairs.Length > 0)

{

for (int i = 0; i < keyValuePairs.Length; i++)

{

keyValue = keyValuePairs.GetValue(i).ToString().Split(“=”.ToCharArray());

m_queryString.Add(keyValue[0], keyValue[1]);

}

}

else

{

keyValue = responseString.Split(“=”.ToCharArray());

if (keyValue.Length > 0)

m_queryString.Add(keyValue[0], keyValue[1]);

}

// isolate control type and mode

string controlType = m_queryString[“ControlType”];

string eventArg = m_queryString[“EventArg”];

string usngArg = m_queryString[“usngPoint”];

string usngLoc = m_queryString[“usngLocationLabel”];

string usngX = m_queryString[“usngX”];

string usngY = m_queryString[“usngY”];

 

if (controlType == null) controlType = “Map”;

switch (controlType)

{

case “Map”:

// request is for the map control

if (eventArg == “zoomtopoint”)

{

// ZoomToPointServer(usngX,usngY); // Continous callback

System.Web.UI.WebControls.TextBox tbox = (System.Web.UI.WebControls.TextBox)this.Page.FindControl(“Loc”);

ZoomToPointServer(usngArg, usngLoc);

mapstring = Map1.CallbackResults.ToString();

}

break;

 

default:

//

break;

}

/// return response;

}

 

 

public void ZoomToPointServer(string ea, string Loc)

{

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

ESRI.ArcGIS.ADF.ArcGISServer.MapDescription mapDescription = mf.MapDescription;

 

 

ESRI.ArcGIS.Geometry.Point selPoint;

ESRI.ArcGIS.Geometry.IConversionMGRS convertPoint;

ESRI.ArcGIS.Geometry.IGeographicCoordinateSystem m_GCS;

ESRI.ArcGIS.Geometry.IProjectedCoordinateSystem m_PCS;

ESRI.ArcGIS.Geometry.ISpatialReferenceFactory2 pSpatRefFact;

 

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

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

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

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

selPoint.SpatialReference = m_GCS;

// selPoint.SpatialReference = m_PCS;

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

// string USNGstring = “15SYT0982336405”;

string USNGstring = ea.ToUpper();

 

double zoomfactor = 1;

int lngth = ea.Length;

 

if (((lngth % 2) == 0) && (lngth > 5))

{

// SendBackJS(Map1, string.Format(“alert(‘Wrong format!!, Example of correct USNG format: 15SYT followed by a even number of digits – Max 10’);”));

return;

}

 

 

if (lngth == 5)

{

zoomfactor = mapOriginalScale / 1000000;

}

else if (lngth == 7)

{

zoomfactor = mapOriginalScale / 500000;

}

else if (lngth == 9)

{

zoomfactor = mapOriginalScale / 250000;

}

else if (lngth == 11)

{

zoomfactor = mapOriginalScale / 100000;

}

else if (lngth == 13)

{

zoomfactor = mapOriginalScale / 25000;

}

else if (lngth == 15)

{

zoomfactor = mapOriginalScale / 10000;

}

else

{

SendBackJS(Map1, string.Format(“alert(‘Wrong USNG Format !!!, Correct USNG format is like 15SYT followed by a even number of digits – Max 10’);”));

return;

 

}

 

convertPoint.PutCoordsFromMGRS(USNGstring, ESRI.ArcGIS.Geometry.esriMGRSModeEnum.esriMGRSMode_USNG);

 

ESRI.ArcGIS.ADF.ArcGISServer.PointN ags_map_point = new ESRI.ArcGIS.ADF.ArcGISServer.PointN();

ags_map_point.X = selPoint.X;

ags_map_point.Y = selPoint.Y;

 

// convert geometry.point to adf.web.geometry point

ESRI.ArcGIS.ADF.Web.Geometry.Point adfPoint = ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.FromIPoint(selPoint);

 

Map1.Zoom(zoomfactor, adfPoint);

 

// Use the map control’s zoom method to zoom the map to the correct scale and the map

// center being the user entered USNG Coordinate

 

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;

text.Text = USNGstring + “\n” + Loc;

 

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;

}

else

{

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

ges[0] = text;

ges[1] = marker;

mapDescription.CustomGraphics = ges;

}

Map1.Refresh();

}

#endregion