Select to view content in your preferred language

UTM or State Plane to Decimal Degrees

2627
3
10-11-2010 03:08 PM
FrankRoberts
Occasional Contributor III
FlexAPI 2.1 Question:

Does anyone have some code that they could share to convert coordinates (for example a single mappoint) from State Plane/and or UTM to decimal degrees (i.e. wkid 4326)?  The Web Mercator utilities are great for doing this on the client side, but unfortunately not all of my services are in Web Mercator.

I imagine this require a trip back and forth between the server.  I just  haven't found a sample of that yet.

Thanks for your help!

Frank


Code currently looks as follows:

const mapPoint:MapPoint = map.toMapFromStage(event.stageX, event.stageY);
var latlong:MapPoint;
var wkidString:String = mapPoint.spatialReference.wkid.toString();
//Alert.show(mapPoint.spatialReference.wkid.toString());
if ( wkidString == "102100")
{
latlong = WebMercatorUtil.webMercatorToGeographic(mapPoint) as MapPoint;
}
else if ( wkidString == "3857")
{
latlong = WebMercatorUtil.webMercatorToGeographic(mapPoint) as MapPoint;
}
else if ( wkidString == "4326")
{
latlong = map.toMapFromStage(event.stageX, event.stageY);
}
else
{
Alert.show("This widget will not work with data in this projection");
}

frLat = latlong.y.toFixed(6);
frLong = latlong.x.toFixed(6);
Tags (2)
0 Kudos
3 Replies
RobertScheitlin__GISP
MVP Emeritus
0 Kudos
JacksonTrappett
Occasional Contributor II
If you're looking to avoid the trip to and from the server you can always do the math in the client, check out this thread for a sample function:

http://forums.arcgis.com/threads/1786-Flex-Display-latitude-and-longitue

It's a little more complicated but works better for things where you have a lot of calls to the function, like showing the mouse location in lat long as the user moves the mouse around.  If you're just doing a single point at a time though, its easier to have the server do the reproject like in Robert's example.
0 Kudos
FrankRoberts
Occasional Contributor III
Thanks to both of you, the ESRI sample Robert pointed me to was useful.  My question is there anyway to avoid adding the Declarations section at the end (i.e. just define the geometryService info in the body of the Action Script function)??

Hope this helps someone!
Frank


The code below is what I ended up doing, please note the goal hear was to pass lat and long to a function called "displayForm" :

private function mouseClickHandler(event:MouseEvent):void
{
const frmapPoint:MapPoint = map.toMapFromStage(event.stageX, event.stageY);
var latlong:MapPoint;
var wkidString:String = frmapPoint.spatialReference.wkid.toString();
//Alert.show("Spatial Ref for map: " + frmapPoint.spatialReference.wkid.toString());
if ( wkidString == "102100")
{
  latlong = WebMercatorUtil.webMercatorToGeographic(frmapPoint) as MapPoint;
  frLat = latlong.y.toFixed(6);
  frLong = latlong.x.toFixed(6);
  displayForm(frLat, frLong);
}
else if ( wkidString == "3857")
{
  latlong = WebMercatorUtil.webMercatorToGeographic(frmapPoint) as MapPoint;
  frLat = latlong.y.toFixed(6);
  frLong = latlong.x.toFixed(6);
  displayForm(frLat, frLong);
}
else if ( wkidString == "4326")
{
  latlong = map.toMapFromStage(event.stageX, event.stageY);
  frLat = latlong.y.toFixed(6);
  frLong = latlong.x.toFixed(6);
  displayForm(frLat, frLong);
}
else
{
  //Alert.show("Non client side projection");
  var outSR:SpatialReference = new SpatialReference(4326);
  geometryService.project([frmapPoint as Geometry], outSR);
}

var graphic:Graphic = new Graphic();
var mapPoint2:MapPoint = frmapPoint;
mapPoint2.spatialReference = map.spatialReference;   
graphic.geometry = mapPoint2;   
graphicsLayer.clear();   
graphicsLayer.add(graphic);
}

//private var infoWindowText:String;
private function projectCompleteHandler(event:GeometryServiceEvent):void
{
try
{
  // Note: As of version 2.0, GeometryService returns geometries (instead of graphics)
  var pt:MapPoint = (event.result as Array)[0]as MapPoint;    
  //Alert.show(pt.x + ", " + pt.y);
  displayForm(pt.y.toString(),pt.x.toString());
}
catch (error:Error)
{
  Alert.show(error.toString());
}
}

I then had to add this after the Script Tags, as to identify the server and projectComplete event:


<fx:Declarations>
<esri:GeometryService id="geometryService"
   concurrency="last"
   fault="onFault(event)"
   projectComplete="projectCompleteHandler(event)"
   url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer"/>
</fx:Declarations>
0 Kudos