How to draw a polyline displaying the shortest distance between two features

4690
7
04-19-2011 09:32 AM
tandrews
New Contributor II
Hi All,
I have a situation where I need to select 2 features on the map, get the shortest distance between these 2 features, and then programmatically draw a polyline showing the shortest path between the features. The features can be of any type.  I'm currently able to select the features and then get the shortest distance value using the geometry task.  I cannot seem to find a way to draw the polyline connecting the features however; anyone know how to do this?   Any help would be appreciated.

TIA,
Todd
0 Kudos
7 Replies
JenniferNery
Esri Regular Contributor
Have you looked at these SDK samples? http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#Routing (under Network)
0 Kudos
ChristopherHill
Occasional Contributor
Well if you already have the polyline then you have some of this done already. this is an example of what it would take to build a polyline from nothing and add it to the map.

// What my symbol looks like
Symbols.SimpleLineSymbol symbol = new ESRI.ArcGIS.Client.Symbols.SimpleLineSymbol()
{
              Color = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Blue),
 Style = Symbols.SimpleLineSymbol.LineStyle.Solid,    
};

// the points that make up my shape
PointCollection pc = new PointCollection()
{
 new MapPoint(0,0, new SpatialReference(4326)), // first point in line
 new MapPoint(1,1, new SpatialReference(4326)), // second point in line
};

Polyline polyline = new Polyline()
{        
 // don't for get to let the map know what spatial reference this is
 SpatialReference = new SpatialReference(4326) 
};
polyline.Paths.Add(pc);
    
Graphic g = new Graphic()
{
 Symbol = symbol, // add my symbol to my graphic
 Geometry = polyline // add my geometry to my graphic
};

GraphicsLayer graphicLayer = new GraphicsLayer();
graphicLayer.Graphics.Add(g);
0 Kudos
YamunadeviRathinasamy
New Contributor II
Craete a stored procedure and handle this in SQL server and display the result in the Silverlight UI graphics layer.

declare @g1 geometry,@g2 geometry,@dist float,@gp1 geometry,@gp2 geometry
begin
select @g1=shape from table1 OBJECTID=3;
select @g2=shape from table2 where OBJECTID=2;
SELECT @dist=@g1.STDistance(@g2);
select @gp1=@g1.STBuffer(@dist).STIntersection(@g2);
select @gp2=@g2.STBuffer(@dist).STIntersection(@g1);
print 'Point1:'+convert(varchar(max),@gp1)+' Point2:'+convert(varchar(max),@gp1)+'Distance:'+convert(varchar(max),@dist)
end;
0 Kudos
tandrews
New Contributor II
Yamuna, Thank you, currently pursuing that possibility, looks promising 😉

Jennifer,
I have reviewed those samples but thus far I've had no success employing this in my scenario.  I'm hoping that someone can help me to understand why the following simple example returns no results.  To summarize I have 1 graphics layer, I'm adding 2 points to it, and then trying to use the route task to trace the shortest distance between these features.  My routeTask_SolveCompleted event handler fires on completion without error but e.Results is empty.  What am I missing here?  Is it plausible to use this route task to accomplish this or am I barking up the wrong tree?

//Clear graphics layer:
glMeasureFeatureDistance.ClearGraphics();
//Create map points:
MapPoint mpBegin = new MapPoint(3231948.89, 1740328.161);
MapPoint mpEnd = new MapPoint(3231928.299, 1736971.88);
//Create first graphic, set geometry, set symbol, add to graphics layer:
Graphic graphic = new Graphic();
graphic.Geometry = mpBegin;
graphic.Symbol = Measurehelp.SetSymbolSettings("Point");
glMeasureFeatureDistance.Graphics.Add(graphic);
//Create second graphic, set geometry, set symbol, add to graphics layer:
graphic = new Graphic();
graphic.Geometry = mpEnd;
graphic.Symbol = Measurehelp.SetSymbolSettings("Point");
glMeasureFeatureDistance.Graphics.Add(graphic);
//Create route task, set url, set event handlers:
RouteTask routeTask = new RouteTask();
routeTask.Url = "http://insert_IP/ArcGIS/rest/services/Base_Map/MapServer";
routeTask.SolveCompleted += new EventHandler<RouteEventArgs>(routeTask_SolveCompleted);
routeTask.Failed += new EventHandler<TaskFailedEventArgs>(routeTask_Failed);
//Create routeparameters, set properties:
RouteParameters routeParameters = new RouteParameters();
routeParameters.ReturnRoutes = true;
routeParameters.Stops = glMeasureFeatureDistance;
//Call route task:
routeTask.SolveAsync(routeParameters);



Thanks
0 Kudos
DominiqueBroux
Esri Frequent Contributor

What am I missing here?

You have to set the spatial reference of your 2 points (mpBegin and mpEnd) since, obviously, it's not geographical coordinates.

Is it plausible to use this route task to accomplish this or am I barking up the wrong tree?

I am not sure of what you have to do exactly, but using the route task you will find the shortest path between 2 points using existing roads.  Has your shortest path to take care of the roads?
0 Kudos
tandrews
New Contributor II
Hi dbroux,
Yes, the SR is set correctly.  I have no roads and I do not have Network Analyst.  I'm very confident at this point that I am indeed looking at the route task in a wishful manner.  Putting the route task aside I am back to the original query:

Does anyone know how to draw a polyline displaying the shortest distance between two features???

I'm using the geometry task 'DistanceAsync' method to GET the distance but I cannot determine where the start and end points are as the method does not return these values.


Thanks again,
Todd
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Putting the route task aside I am back to the original query:

Does anyone know how to draw a polyline displaying the shortest distance between two features???


I don't know and it doesn't look simple to me.

Yamuna idea (use SQL server to do it) looks promising but I never tested that.

Another options might be to write geometry code at the client side (not that easy), or write a web service doing it with ArcObjects or perhaps a GP tools could do it.

Unfortunately I can't help you more. Sorry.
Hopefully somebody else will have better ideas....
0 Kudos