We refer to the Optimize Roadtype sample(samples.thinkgeo.com/RoutingEdition/HowDoISamples/) online.
In our project, we have 3 types of road and 3 types of vehicles. It is one-one relation. We used the “Algorithm_FindingPath” in the example, but met some problems.
1.Good(Red) 1Car(Good)
2.Average (Orange) 2.Bus(Average)
3.Poor(Green) 3.Truck( Poor)
Q1: If I choose “Truck” button, it means that the route should be green. In the algorithm, the Good and Average set are removed if Poor.Count>0.
But sometimes the route shows that the majority of the roads are green. Some parts are red and orange. Why?
Q2: We used a listbox to show the Poor set and a textbox to show the count of Poor. I choose the startpoint and endpoint on a green road.
If these two points are very near, it shows that the “Algorithm_FindingPath” algorithm is not executed because the listbox and textbox are null( I also verified by debugging the code step by step).
If these two points are far away from each other, everything is correct.
Q3:If I choose the startpoint and endpoint on a “Red” road and click “Truck” button, it should be no route exists. But the listbox and textbox told me some green roads have been found. It seems that this “Algorithm_FindingPath” algorithm finds the routes according to the adjacent points of start/end points.(Is there any Green roads around point A and B? We hope the algorithm is that is there any Green roads which links A and B? )
// Truck button
private void button6_Click(object sender, EventArgs e)
{
i = 1;
featureSource = new ShapeFileFeatureSource(str1);
featureSource.Open();
RoutingSource routingSource = new RtgRoutingSource(str2);
routingEngine = new RoutingEngine(routingSource, featureSource);
routingEngine.RoutingAlgorithm.FindingRoute += new EventHandler<FindingRouteRoutingAlgorithmEventArgs>(Algorithm_FindingPath);
Route1();
}
private void Route1()
{
RoutingLayer routingLayer = (RoutingLayer)((LayerOverlay)mapControl.Overlays[“RoutingOverlay”]).Layers[“RoutingLayer”];
RoutingResult routingResult = routingEngine.GetRoute(routingLayer.StartPoint, routingLayer.EndPoint);
routingLayer.Routes.Clear();
routingLayer.Routes.Add(routingResult.Route);
mapControl.CurrentExtent = new RectangleShape(-97.7632741244507, 30.2776084974365, -97.7315167697143, 30.2549491956787);
mapControl.Refresh(mapControl.Overlays[“RoutingOverlay”]);
}
void Algorithm_FindingPath(object sender, FindingRouteRoutingAlgorithmEventArgs e)
{
Collection<string> Good = new Collection<string>();
Collection<string> Average = new Collection<string>();
Collection<string> Poor = new Collection<string>();
foreach (string item in e.RouteSegment.StartPointAdjacentIds)
{
RouteSegment road = routingEngine.RoutingSource.GetRouteSegmentByFeatureId(item);
if (road.Distance == 2)
{
Good.Add(item);
}
if (road.Distance == 1)
{
Average.Add(item);
}
if (road.Distance == 0)
{
Poor.Add(item);
}
}
foreach (string item in e.RouteSegment.EndPointAdjacentIds)
{
RoutingLayer routingLayer = (RoutingLayer)((LayerOverlay)mapControl.Overlays[“RoutingOverlay”]).Layers[“RoutingLayer”];
RouteSegment road = routingEngine.RoutingSource.GetRouteSegmentByFeatureId(item);
if (road.Distance == 2)
{
Good.Add(item);
}
if (road.Distance == 1)
{
Average.Add(item);
}
if (road.Distance == 0)
{
Poor.Add(item);
}
}
int a = Poor.Count;
textBox1.Text = a.ToString();
listBox1.Items.Add(Poor);
if (i == 1)
{
if (Poor.Count > 0)
{
foreach (string item in Good)
{
e.RouteSegment.StartPointAdjacentIds.Remove(item);
e.RouteSegment.EndPointAdjacentIds.Remove(item);
}
foreach (string item in Average)
{
e.RouteSegment.StartPointAdjacentIds.Remove(item);
e.RouteSegment.EndPointAdjacentIds.Remove(item);
}
i = 0;
}
else
{
MessageBox.Show(“No Route Exists!”);
i = 0;
routingLayer.Routes.Clear();
mapControl.Refresh(routingOverlay);
return;
}
return;
}
}