Select to view content in your preferred language

Go to x,y in BNG (wkid:27700)

2402
5
Jump to solution
01-03-2013 05:13 AM
KarlBarker-Ottley
New Contributor
Hi,

I'm trying to make a way of being able to enter BNG (wkid:27700) co-ordinates and have a point rendered on a graphics layer?

I have found the following bit of code which works for WGS84 (wkid:4326), but I'm having a few problems getting it adapted to work with BNG co-ordinates?:

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" xmlns:esri="http://www.esri.com/2008/ags">

<fx:Script>
<![CDATA[
import com.esri.ags.Graphic;
import com.esri.ags.SpatialReference;
import com.esri.ags.geometry.MapPoint;
import com.esri.ags.layers.GraphicsLayer;
import com.esri.ags.utils.WebMercatorUtil;
protected function btnSubmit_clickHandler(event:MouseEvent):void
{
var gLayer:GraphicsLayer = new GraphicsLayer();
myMap.addLayer(gLayer);
if (lat.text!="" && lon.text != ""){
var latitude:Number = Number(lat.text);
var longitude:Number = Number(lon.text);
var graPoint:MapPoint = new MapPoint(longitude,latitude,new SpatialReference(4326));
var wmPoint:MapPoint = WebMercatorUtil.geographicToWebMercator(graPoint) as MapPoint;
var gra:Graphic = new Graphic(wmPoint);
gLayer.clear();
gLayer.add(gra);
myMap.centerAt(wmPoint);
}
}
]]>
</fx:Script>

<esri:Map id="myMap">
<esri:ArcGISTiledMapServiceLayer url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/>
</esri:Map>
<s:Label text="Latitude" left="10" top="10" height="21"/>
<s:TextInput id="lat" top="10" left="60"/>
<s:Label text="Longitude" left="190" top="10" height="21"/>
<s:TextInput id="lon" top="10" left="250"/>
<s:Button id="btnSubmit" label="Plot" click="btnSubmit_clickHandler(event)" left="380" top="10"/>
</s:Application>

I am using BNG services and have altered the wkid in the code (and am obviously consuming an ArcGISTiledMapServiceLayer in BNG too, not the World_Street_Map as in this code, which is not BNG).

Appreciate any assistance,
Karl
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Karl,

   Try this.

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                xmlns:s="library://ns.adobe.com/flex/spark"
                xmlns:mx="library://ns.adobe.com/flex/mx"
                xmlns:esri="http://www.esri.com/2008/ags"
                creationComplete="application1_creationCompleteHandler(event)">
          <fx:Script>
          <![CDATA[
             import com.esri.ags.Graphic;
             import com.esri.ags.SpatialReference;
             import com.esri.ags.events.GeometryServiceEvent;
             import com.esri.ags.geometry.MapPoint;
             import com.esri.ags.layers.GraphicsLayer;
             import com.esri.ags.tasks.supportClasses.ProjectParameters;
             import mx.controls.Alert;
             import mx.events.FlexEvent;
             import mx.rpc.events.FaultEvent;
             
             private var gLayer:GraphicsLayer;
             
             protected function btnSubmit_clickHandler(event:MouseEvent):void
             {
                 if (lat.text!="" && lon.text != ""){
                     var latitude:Number = Number(lat.text);
                     var longitude:Number = Number(lon.text);
                     var graPoint:MapPoint = new MapPoint(longitude,latitude,new SpatialReference(27700));
                     if(myMap.spatialReference.wkid != graPoint.spatialReference.wkid){
                         var outSR:SpatialReference = myMap.spatialReference;
                         const projectParameters:ProjectParameters = new ProjectParameters;
                         projectParameters.geometries = [graPoint];
                         projectParameters.outSpatialReference = outSR;
                         geometryService.project(projectParameters);
                     }else{
                         var gra:Graphic = new Graphic(graPoint);
                         gLayer.clear();
                         gLayer.add(gra);
                         myMap.centerAt(graPoint);
                     }
                 }
             }
              
            protected function projectCompleteHandler(event:GeometryServiceEvent):void
            {
                 var pt:MapPoint = (event.result as Array)[0]as MapPoint;
                 var gra:Graphic = new Graphic(pt);
                 gLayer.clear();
                 gLayer.add(gra);
                 myMap.centerAt(pt);
             }
             
            protected function application1_creationCompleteHandler(event:FlexEvent):void
            {                 
         gLayer = new GraphicsLayer();
                 myMap.addLayer(gLayer);
            }
            
            protected function geometryService_faultHandler(event:FaultEvent):void
            {
                 Alert.show(event.fault.faultString + "\n\n" + event.fault.faultDetail, "Geometry Service Error " + event.fault.faultCode);
            }
            
     ]]>
     </fx:Script>
     <fx:Declarations>
         <esri:GeometryService id="geometryService"
                               concurrency="last"
                               fault="geometryService_faultHandler(event)"
                               projectComplete="projectCompleteHandler(event)"
                               showBusyCursor="true"
                               url="http://sampleserver6.arcgisonline.com/arcgis/rest/services/Utilities/Geometry/GeometryServer"/>
     </fx:Declarations>
     <esri:Map id="myMap">
         <esri:ArcGISTiledMapServiceLayer url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/>
     </esri:Map>
     <s:Label text="Latitude" left="10" top="10" height="21"/>
     <s:TextInput id="lat" top="10" left="60"/>
     <s:Label text="Longitude" left="190" top="10" height="21"/>
     <s:TextInput id="lon" top="10" left="250"/>
     <s:Button id="btnSubmit" label="Plot" click="btnSubmit_clickHandler(event)" left="380" top="10"/>
 </s:Application>

Don't forget to click the Mark as answer check on this post and to click the top arrow (promote).
Follow these steps as shown in the below graphic:

View solution in original post

0 Kudos
5 Replies
RobertScheitlin__GISP
MVP Emeritus

Karl,

   Try this.

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                xmlns:s="library://ns.adobe.com/flex/spark"
                xmlns:mx="library://ns.adobe.com/flex/mx"
                xmlns:esri="http://www.esri.com/2008/ags"
                creationComplete="application1_creationCompleteHandler(event)">
          <fx:Script>
          <![CDATA[
             import com.esri.ags.Graphic;
             import com.esri.ags.SpatialReference;
             import com.esri.ags.events.GeometryServiceEvent;
             import com.esri.ags.geometry.MapPoint;
             import com.esri.ags.layers.GraphicsLayer;
             import com.esri.ags.tasks.supportClasses.ProjectParameters;
             import mx.controls.Alert;
             import mx.events.FlexEvent;
             import mx.rpc.events.FaultEvent;
             
             private var gLayer:GraphicsLayer;
             
             protected function btnSubmit_clickHandler(event:MouseEvent):void
             {
                 if (lat.text!="" && lon.text != ""){
                     var latitude:Number = Number(lat.text);
                     var longitude:Number = Number(lon.text);
                     var graPoint:MapPoint = new MapPoint(longitude,latitude,new SpatialReference(27700));
                     if(myMap.spatialReference.wkid != graPoint.spatialReference.wkid){
                         var outSR:SpatialReference = myMap.spatialReference;
                         const projectParameters:ProjectParameters = new ProjectParameters;
                         projectParameters.geometries = [graPoint];
                         projectParameters.outSpatialReference = outSR;
                         geometryService.project(projectParameters);
                     }else{
                         var gra:Graphic = new Graphic(graPoint);
                         gLayer.clear();
                         gLayer.add(gra);
                         myMap.centerAt(graPoint);
                     }
                 }
             }
              
            protected function projectCompleteHandler(event:GeometryServiceEvent):void
            {
                 var pt:MapPoint = (event.result as Array)[0]as MapPoint;
                 var gra:Graphic = new Graphic(pt);
                 gLayer.clear();
                 gLayer.add(gra);
                 myMap.centerAt(pt);
             }
             
            protected function application1_creationCompleteHandler(event:FlexEvent):void
            {                 
         gLayer = new GraphicsLayer();
                 myMap.addLayer(gLayer);
            }
            
            protected function geometryService_faultHandler(event:FaultEvent):void
            {
                 Alert.show(event.fault.faultString + "\n\n" + event.fault.faultDetail, "Geometry Service Error " + event.fault.faultCode);
            }
            
     ]]>
     </fx:Script>
     <fx:Declarations>
         <esri:GeometryService id="geometryService"
                               concurrency="last"
                               fault="geometryService_faultHandler(event)"
                               projectComplete="projectCompleteHandler(event)"
                               showBusyCursor="true"
                               url="http://sampleserver6.arcgisonline.com/arcgis/rest/services/Utilities/Geometry/GeometryServer"/>
     </fx:Declarations>
     <esri:Map id="myMap">
         <esri:ArcGISTiledMapServiceLayer url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/>
     </esri:Map>
     <s:Label text="Latitude" left="10" top="10" height="21"/>
     <s:TextInput id="lat" top="10" left="60"/>
     <s:Label text="Longitude" left="190" top="10" height="21"/>
     <s:TextInput id="lon" top="10" left="250"/>
     <s:Button id="btnSubmit" label="Plot" click="btnSubmit_clickHandler(event)" left="380" top="10"/>
 </s:Application>

Don't forget to click the Mark as answer check on this post and to click the top arrow (promote).
Follow these steps as shown in the below graphic:

0 Kudos
AnthonyGiles
Frequent Contributor II
Karl,

I created a BNG widget  a while ago the code may help you out:

http://www.arcgis.com/home/item.html?id=84e905c062404284976e8b469e1b8bfb

Regards

Anthony
0 Kudos
KarlBarker-Ottley
New Contributor
Thanks for you help guys - great solution for plotting a co-ordinate over BNG services!

I am curious as to whether I can enter a x,y derived from a map in BNG (27700) and then have it re-projected into the Web-Mercator (102100). A lot of the ESRI inc basemap services use this for their tiled map services and it may be useful to know if this is possible?

I tried the offered code over an ESRI WTMS in 102100, and it does seem to reproject the point but about 100m away from the correct position (which looks like a BNG to WGS84 (4326) reprojection)?

Thanks,
Karl
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Karl,

   The code I sent does the reprojecting from 27700 to whatever WKID your map is in (like 102100). If there is a difference in the place where the reprojection places the point and what you know it to be then the reprojection may need a datum transformation.  I am not familar at all with the BNG projection so I am not real sure.
0 Kudos
KenBuja
MVP Esteemed Contributor
This thread went into some discussion about the datum transformation between BNG and WGS84 with a link to a blog about going from BNG to Web Mercator.
0 Kudos