Select to view content in your preferred language

Geometry Server Question

1221
12
10-19-2010 03:47 PM
FrankRoberts
Frequent Contributor


In a different post I figured out how to use the Geometry Server to project a point, see:

http://forums.arcgis.com/threads/14793-UTM-or-State-Plane-to-Decimal-Degrees

However, I'm still left with a question. 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 ActionScript function)??

This is what I'm currently adding:





<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>



Thanks




Frank


Tags (2)
0 Kudos
12 Replies
RobertScheitlin__GISP
MVP Emeritus
Frank,

Here is how you do it in action script.

   
var geomServ:GeometryService = new GeometryService("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
                    geomServ.addEventListener(GeometryServiceEvent.PROJECT_COMPLETE,pResult);
function pResult(event:GeometryServiceEvent):void{
          var pt:MapPoint = (event.result as Array)[0]as MapPoint;
          map.centerAt(pt);
          map.cursorManager.removeBusyCursor();
         }
0 Kudos
JamesFaron
Frequent Contributor
Resurrecting this old post, due to my desire to use a geometry service in action script. The problem is that the listener event will not dispatch the geometry service project_complete function, i.e. "pResult". This post describes the problem of dispatching an event in action script:http://blogs.adobe.com/steampowered/2011/05/dispatching-events-from-an-actionscript-class.html

Just wondering if anyone has succeeded to use a geometry service in action script. I'm trying to modify a function to project to State Plane that currently uses the WebMercatorUtil.

Also found this for Silverlight that auto-reprojects grahic layers, something that relates to my issue, in that I am stuck with trying to use a State Plane projected map with a graphics layer that is produced by a complex process: http://blogs2.esri.com/esri/arcgis/2011/02/13/auto-reprojecting-graphics-layers/

Thanks,

Jim Faron
Austin Independent School District
0 Kudos
FrankRoberts
Frequent Contributor
Thanks to Robert's help above I did get my code working in Action Script  to call the Geometry services.  The following is what I did:


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
                {
                    //Project using geometry service
                    var outSR:SpatialReference = new SpatialReference(4326);
                    // var inSR:SpatialReference = new SpatialReference(102100);
                    //geometryService.project([ frmapPoint as Geometry ], outSR);
                    // Changed for 3.0 API
                    const projectParameters:ProjectParameters = new ProjectParameters;
                    projectParameters.geometries = [ frmapPoint as Geometry ];
                    projectParameters.outSpatialReference = outSR;
                    geometryService.project(projectParameters);
                }
                
                var graphic:Graphic = new Graphic();    
                var mapPoint2:MapPoint = frmapPoint;
                mapPoint2.spatialReference = map.spatialReference;                
                graphic.geometry = mapPoint2;                
                graphicsLayer.clear();                
                graphicsLayer.add(graphic);
            }
                        
            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());
                }
            }


Not sure if it helps or not, but maybe it provides you a little more then what you started with!
0 Kudos
JamesFaron
Frequent Contributor
I have used something similar in mxml, but I am trying to do a reprojection in an action script class, and the event listener will not fire within the function. Here is the orginal code:
public function convertToPoint(x:Number, y:Number, bool:Boolean):Point
  {   
   var _pointObject:MapPoint = new MapPoint();
   _pointObject.x = x;
   _pointObject.y = y;
   
   if(bool)
   {
    _pointObject = WebMercatorUtil.geographicToWebMercator(_pointObject) as MapPoint;
   }
   else
   {
    _pointObject = WebMercatorUtil.webMercatorToGeographic(_pointObject) as MapPoint;
   }   
   
   return new Point(_pointObject.x, _pointObject.y);
  }


I have tried creating a class to project the _pointObject to state plane, but first I tried something like this (this is a simplified version of what I tried, but the essence is that I can't get the event listener to dispatch the result function) I had the same issue in the class I created :
public function convertToPoint(x:Number, y:Number, bool:Boolean):Point
  {   
   var _pointObject:MapPoint = new MapPoint();
   _pointObject.x = x;
   _pointObject.y = y;
   
   if(bool)
   {
    //_pointObject = WebMercatorUtil.geographicToWebMercator(_pointObject) as MapPoint;
    var geomServ:GeometryService = new GeometryService("http://myserver/arcgis/rest/services/Utilities/Geometry/GeometryServer");
    geomServ.addEventListener(GeometryServiceEvent.PROJECT_COMPLETE,pResult);
    const projParams:ProjectParameters = new ProjectParameters();
    projParams.geometries = [_pointObject as MapPoint ];
    var outSR:SpatialReference = new SpatialReference(2277);
    projParams.outSpatialReference = outSR;
    geomServ.project(projParams);
    function pResult(event:GeometryServiceEvent):void{
     var pt:MapPoint = (event.result as Array)[0]as MapPoint;
     _pointObject.x = pt.x;
     _pointObject.y = pt.y;
   }
The code will work up to the geomServ.project(projParams) function, but the project comple function (pResult) will not fire up to complete the projection. I believe that there is an inherent issue with event listeners in action script, and I may be out of luck: I just wanted to bounce this off more experienced programmers. As I implied in my first post, it would be fantastic if we had a way to do something like this since many folks are either stuck with using state plane, or choose to for other reasons. By the time this data gets back to the mxml, it is buried in a complex graphic provider of a graphics layer, making re-projection in mxml  a very difficult or unlikely prospect. The idea is to get to this data before it gets loaded into that graphicprovider.

Thanks,

Jim Faron
Austin Independent School District
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Jim,

  Try this:

geomServ.project(projParams, 
                        new AsyncResponder(
                            function pResult(item:Object, token:Object = null):void{
                                var pt:MapPoint = (item as Array)[0] as MapPoint;
                                _pointObject.x = pt.x;
                                _pointObject.y = pt.y;
                            },
                            function pFault(fault:Fault, token:Object = null):void{
                                Alert.show("Error: " + fault.faultString, "Error code: " + fault.faultCode);
                            }
                        )
                    );
0 Kudos
JamesFaron
Frequent Contributor
Robert,

That does formulate the send request to the geometry service as expected, but it does not actually send it, at least as I can tell from monitoring it with fiddler. I stepped through this in debug and it forms the url request to the geometry server. But the item:Object never gets a value returned from the geometry server, and it basically skips through both the pResult and the pFault functions. Seems to be related to the problem regarding dispatching events that is referred to in the adobe blog post I linked in my first post.

Thanks,

Jim Faron
Austin Independent School District
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Jim,

   I got that code to work just fine. Are you setting the spatial reference of the _pointObject before you attempt to project it?
0 Kudos
JamesFaron
Frequent Contributor
Robert,

Here is the code I am using:
public function convertToPoint(x:Number, y:Number, bool:Boolean):Point
  {   
   var _pointObject:MapPoint = new MapPoint();
   _pointObject.x = x;
   _pointObject.y = y;
   
   if((bool) && (!isNaN(x)) && (!isNaN(y)))
   {
    //_pointObject = WebMercatorUtil.geographicToWebMercator(_pointObject) as MapPoint;
    var geomServ:GeometryService = new GeometryService("https://my-imap.austinisd.org/arcgis/rest/services/Utilities/Geometry/GeometryServer");
    //geomServ.addEventListener(GeometryServiceEvent.PROJECT_COMPLETE,pResult);
    const projParams:ProjectParameters = new ProjectParameters();
    projParams.geometries = [_pointObject as MapPoint ];
    var outSR:SpatialReference = new SpatialReference(2277);
    projParams.outSpatialReference = outSR;
    geomServ.project(projParams, 
     new AsyncResponder(
      function pResult(item:Object, token:Object = null):void{
       var pt:MapPoint = (item as Array)[0] as MapPoint;
       _pointObject.x = pt.x;
       _pointObject.y = pt.y;
      },
      function pFault(fault:Fault, token:Object = null):void{
       Alert.show("Error: " + fault.faultString, "Error code: " + fault.faultCode);
      }
     )
    );
    
   }
The url that forms (from debug) is
http://mapaisd/arcgis/rest/services/Utilities/Geometry/GeometryServer/project?outSR=2277&f=json&geometries=%7B%22geometryType%22%3A%22esriGeometryPoint%22%2C%22geometries%22%3A%5B%7B%22y%22%3A30%2E49213059%2C%22x%22%3A%2D97%2E64269802%7D%5D%7D

This returns {"error":{"code":400,"message":"Unable to complete operation.","details":[]}} if I input it in the browser.

Thanks,

Jim Faron
Austin Independent School District
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Jim,

   I still don't see in your code where you are specifying that the spatial reference of the MapPoint is (originally) before you attempt to reproject it. How can the geometry service know how to re-project if it does not know what spatial reference it is in to begin with?
0 Kudos