English

How To: Project or unproject x,y coordinates using ArcXML

Summary

Instructions provided describe the process of adding a tool to an ArcIMS HTML Viewer to accomplish the functionality described below.

Projected data can be served while allowing a user to click on the map and display geographic coordinates. Implementing an ArcXML request into the application accomplishes this. This ArcXML GET_IMAGE request will return an envelope in geographic coordinates. These values are extracted from the response and the center point of this envelope is calculated and displayed in a Javascript pop-up window.

Note:
While this article addresses the steps for adding specific functionality into the HTML Viewer, the same principles and approach can be used to project or unproject any set of x,y values, using any of the ArcIMS connectors.

Procedure

The following steps involve editing HTML and Javascript files of an out-of-the-box ArcIMS HTML Viewer. Any text editor can be used to edit these files, however Notepad is not recommended because of formatting issues.

The HTML Viewer should reference an ArcIMS service that either 1) references projected data, or 2) uses ArcXML projection elements (FEATURECOORDSYS and FILTERCOORDSYS) to project data on the fly. Knowing the output projection id of this service is imperative, and can be found by looking in the ArcXML Programmer's Reference Guide. Alternatively, a projection string can be used instead of a projection id.

Warning:
In the upcoming steps specified code from this document will be inserted into the .htm and .js files. It is not recommended that this document be copied due to extra formatting characters that also are added to the code when copying.
  1. Edit the site's ArcIMSParam.js file.
    Open the site's ArcIMSParam.js file and find the following line of code:
    var useExtract=false;
    After this line, insert the following code:
    var useUnprojectPt=true;
    This will allow you to easily remove the tool from the application in the future. Setting the value to false will prevent the tool from displaying in the toolbar.
    Save and close the file.
    1. Add a tool to the toolbar.
      Open the site's toolbar.htm file and find the revertToolPic function near the beginning of the file. In that function, find the following line of code:
      if (parent.MapFrame.useIdentifyAll) document.identifyall.src="images/identifyall_1.gif";
      After this line and before the squiggly bracket that closes the revertToolPic function, insert the following line of code:
      if (parent.MapFrame.useUnprojectPt) document.unproject.src="images/xy_1.gif";
      In the next function, setToolPic, find the following block of code:
      } else if (functName=="Identify All") {
      	document.identifyall.src="images/identifyall_2.gif";
      }
      After this block of code (on the same line as the above block's closing squiggly bracket), and before the squiggly bracket that closes the function, insert the following line of code:
      else if (functName=="unproject") {
      	document.unproject.src="images/xy_2.gif";
      }
      Now locate the following block of code:
      if (parent.MapFrame.allowOptions) {
      	// Options. . . requiers aimsOptions.js... allowOptions is set to true in this file
      	document.write('<td align="center" valign="middle">');
      	document.write('<img src="images/wrench.gif" width=16 height=16 hspace=1 vspace=1 border=0 alt=" ' + t.buttonList[40] + '" onmousedown="parent.MapFrame.writeOptionForm();" onmouseover="window.status=\' ' + t.buttonList[40] + '\'">');
      	isSecond = !isSecond
      	document.writeln('</td>');
      	if (isSecond) document.write('</tr><tr>');
      }
      Before the next if statement, add the following block of code:
      if (parent.MapFrame.useUnprojectPt) {
      	// Unproject point. . . requiers aimsCustom.js
      	document.write('<td align="center" valign="middle">');
      	document.write('<img src="images/xy_1.gif" width=16 height=16 hspace=1 vspace=1 border=0 alt=\'Return Decimal Degrees\' name="unproject" onmousedown="parent.MapFrame.clickFunction(\'unproject\'); setToolPic(\'unproject\');" onmouseover="window.status=\'Return Decimal Degrees\'">');
      	isSecond = !isSecond
      	document.writeln('</td>');
      	if (isSecond) document.write('</tr><tr>');
      }
      Save and close the file.
  2. Download the tool icons to the site's \images folder.
    Right-click on the following image and save the image as xy_1.gif to the site's \images directory. [O-Image] Unproject Point tool icon 1
    Right-click on the following image and save the image as xy_2.gif to the site's \images directory. [O-Image] Unproject tool icon 2
    Note:
    When saving the file remember to name the file as described above, preserving case sensitivity as well.
    1. Add code to initialize the new tool.
      Open the site's aimsClick.js file and find the following block of code:
      function clickFunction (toolName) {
      	if (hasLayer("measureBox"))
      		hideLayer("measureBox");
      	switch(toolName) {
      Below this block of code insert the following block of code:
      case "unproject":
      	toolMode = 1001;
      	if (isIE) {
      		document.all.theTop.style.cursor = "crosshair";
      		theCursor = document.all.theTop.style.cursor;
      	}
      	modeBlurb = "Return decimal degrees";
      	break;
      Save and close the file.
  3. Add code that creates an ArcXML request and parses out the ArcXML response.
    Open the site's aimsCustom.js file. In the customMapTool function's "if" statement that checks if toolMode==1001, insert the following two lines of code:
    unprojectPoint(e);
    return false;
    In the useCustomFunction function's "if" statement that checks if XMLMode==1001, insert the following line of code:
    extractPtVal(theReply);
    At the very end of this file, add the following two functions:
    function unprojectPoint(e) {
    	//send a request that will return unprojected coordinates
    	getImageXY(e);
    	getMapXY(mouseX,mouseY);
    
    	var theString = '<ARCXML VERSION="1.0.1">\n<REQUEST>\n<GET_IMAGE>\n<PROPERTIES>\n';
    	theString += '<ENVELOPE minx="' + (mapX-5) + '" miny="' + (mapY-5) + '" maxx="' + (mapX+5) + '" maxy="' + (mapY+5) + '" />\n';
    	theString += '<IMAGESIZE height="1" width="1" />\n';
    	theString += '<LAYERLIST >\n';
    	for (var i=0;i<layerCount;i++) {
    	theString += '<LAYERDEF id="' + LayerID[i] + '" visible="false" />\n';
    	}
    	theString += '</LAYERLIST>\n';
    	// FILTERCOORDSYS is the id of the ArcIMS service's output coordinate system.
    	// FEATURECOORDSYS is the id of the desired coordinate system of the resulting coordinates.
    	theString += '<FILTERCOORDSYS id="54030" />\n<FEATURECOORDSYS id="4326" />\n';
    	theString += '</PROPERTIES>\n</GET_IMAGE>\n</REQUEST>\n</ARCXML>';
    	//alert(theString);
    	sendToServer(imsURL,theString,1001); //XMLmode for unproject xy is 1001
    
    	return false;
    }
    function extractPtVal(theReply) {
    	var theXYs = getEnvelopeXYs(theReply, 0)
    	var limitLeft = theXYs[0];
    	var limitBottom = theXYs[1];
    	var limitRight = theXYs[2];
    	var limitTop = theXYs[3];
    	var theX = (limitLeft + limitRight)/2;
    	var theY = (limitBottom + limitTop)/2;
    	alert("X: " + theX + "\n" + "Y: " + theY);
    }
    Note:
    The unprojectPoint function creates an ArcXML request that returns projected coordinates. Remember to change this request's FILTERCOORDSYS id to that of the ArcIMS service's output coordinate system, and the FEATURECOORDYS id to whatever coordinate system values to be returned.