ThinkGeo.com    |     Documentation    |     Premium Support

GetRoute results in ArgumentOutOfRangeException

Hi Thinkgeo (and Community),



I am currently trying to find any route based on tab-Files. In order to do this, I first converted my .tab files to .shp files using ogr2ogr. Afterwards I managed to display certain start and end points using the id’s as proposed in the Getting-Started-Example.



When I call GetRoute, I get an ArgumentOutOfRange Exception. I’m pretty sure there’s something wrong with my code (it’s almost the same es in the examples):




winformsMap1.MapUnit = GeographyUnit.Meter;
winformsMap1.ThreadingMode = MapThreadingMode.SingleThreaded;
 
WorldMapKitWmsDesktopOverlay worldMapKitDesktopOverlay = new WorldMapKitWmsDesktopOverlay();
winformsMap1.Overlays.Add(worldMapKitDesktopOverlay);
 
 winformsMap1.BackgroundOverlay.BackgroundBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean);
 
ShapeFileFeatureLayer routableStreetsLayer = new ShapeFileFeatureLayer(@“Roads1Full.shp”);
routableStreetsLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle = LineStyles.LocalRoad4;
routableStreetsLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
LayerOverlay routableStreetOverlay = new LayerOverlay();
routableStreetOverlay.Layers.Add(“routableStreets”, routableStreetsLayer);
winformsMap1.Overlays.Add(“routingStreetOverlay”, routableStreetOverlay);
 
FeatureSource featureSource;
featureSource = new ShapeFileFeatureSource(@“Roads1Full.shp”);
 
RtgRoutingSource.GenerateRoutingData(@“Roads1Full_routable.rtg”, featureSource, BuildRoutingDataMode.Rebuild, GeographyUnit.Meter, DistanceUnit.Meter);   
 
RoutingLayer routingLayer = new RoutingLayer();
routableStreetsLayer.Open();
 
routingLayer.StartPoint = routableStreetsLayer.FeatureSource.GetFeatureById(“1”, ReturningColumnsType.NoColumns).GetShape().GetCenterPoint();
routingLayer.EndPoint = routableStreetsLayer.FeatureSource.GetFeatureById(“500”, ReturningColumnsType.NoColumns).GetShape().GetCenterPoint();
routableStreetsLayer.Close();
 
LayerOverlay routingOverlay = new LayerOverlay();
routingOverlay.Layers.Add(“RoutingLayer”, routingLayer);
winformsMap1.Overlays.Add(“RoutingOverlay”, routingOverlay);
 
RtgRoutingSource routingSource = new RtgRoutingSource(@“Roads1Full_routable.rtg”);
RoutingEngine routingEngine = new RoutingEngine(routingSource, featureSource);
routingLayer = (RoutingLayer)((LayerOverlay)winformsMap1.Overlays[“RoutingOverlay”]).Layers[“RoutingLayer”];
winformsMap1.Overlays[“RoutingOverlay”].Lock.EnterWriteLock();
try
{
    routingLayer.Routes.Clear();
    routingLayer.Routes.Add(routingEngine.GetRoute(“1”, “500”).Route);
     
}
finally
{
    winformsMap1.Overlays[“RoutingOverlay”].Lock.ExitWriteLock();
}
 
winformsMap1.Refresh();



When I load the .shp and its corresponding .rtg file into the MapSuite Routing Explorer, some routes can be calculated, some can’t. In case the calculation is successful, there are sometimes really weird routes, leaving the streets or taking strange detours. Maybe there’s anything wrong with my .rtg files - or maybe my streets are not really associated? I don’t get it.



Thanks in advance for your help!

Roads1Full.zip (1.34 MB)

Hi Hanna, 
  
 Thanks for your post, I think the problem is because the rotableShapefile is not generated, following code should solve the problem: 
  
 RtgRoutingSource.GenerateRoutableShapeFile(@"…\SampleData\Roads1Full.shp",  
                                            @"…\SampleData\Roads1Full_routable.shp",  
                                             OverwriteMode.Overwrite); 
  
 RtgRoutingSource.GenerateRoutingData(@"…\SampleData\Roads1Full_routable.rtg",  
                                      @"…\SampleData\Roads1Full.shp",  
                                      @"…\SampleData\Roads1Full_routable.shp", 
                                      BuildRoutingDataMode.Rebuild,  
                                      GeographyUnit.Meter,  
                                      DistanceUnit.Meter); 
  
 if you have any more question , please feel free to let us know. 
  
 Best Regards 
  
 Summer

Hi Summer, 
  
 thanks for your reply. 
 Did you test my code together with your completion? I still have the same exception. My code now is the following: 
  
              winformsMap1.MapUnit = GeographyUnit.Meter;
            winformsMap1.ThreadingMode = MapThreadingMode.SingleThreaded;

            WorldMapKitWmsDesktopOverlay worldMapKitDesktopOverlay = new WorldMapKitWmsDesktopOverlay();
            winformsMap1.Overlays.Add(worldMapKitDesktopOverlay);

            winformsMap1.BackgroundOverlay.BackgroundBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean);

            RtgRoutingSource.GenerateRoutableShapeFile(@"Roads1Full.shp", @"Roads1Full_routable.shp", OverwriteMode.Overwrite);
            RtgRoutingSource.GenerateRoutingData(@"Roads1Full_routable.rtg", @"Roads1Full.shp", @"Roads1Full_routable.shp", BuildRoutingDataMode.Rebuild, GeographyUnit.Meter, DistanceUnit.Meter);

            ShapeFileFeatureLayer routableStreetsLayer = new ShapeFileFeatureLayer(@"Roads1Full.shp");
            routableStreetsLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle = LineStyles.LocalRoad4;
            routableStreetsLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
            LayerOverlay routableStreetOverlay = new LayerOverlay();
            routableStreetOverlay.Layers.Add("routableStreets", routableStreetsLayer);
            winformsMap1.Overlays.Add("routingStreetOverlay", routableStreetOverlay);

            FeatureSource featureSource;
            featureSource = new ShapeFileFeatureSource(@"Roads1Full.shp");

            RoutingLayer routingLayer = new RoutingLayer();
            routableStreetsLayer.Open();

            routingLayer.StartPoint = routableStreetsLayer.FeatureSource.GetFeatureById("1", ReturningColumnsType.NoColumns).GetShape().GetCenterPoint();
            routingLayer.EndPoint = routableStreetsLayer.FeatureSource.GetFeatureById("500", ReturningColumnsType.NoColumns).GetShape().GetCenterPoint();
            routableStreetsLayer.Close();

            LayerOverlay routingOverlay = new LayerOverlay();
            routingOverlay.Layers.Add("RoutingLayer", routingLayer);
            winformsMap1.Overlays.Add("RoutingOverlay", routingOverlay);

            RtgRoutingSource routingSource = new RtgRoutingSource(@"Roads1Full_routable.rtg");
            RoutingEngine routingEngine = new RoutingEngine(routingSource, featureSource);
            routingLayer = (RoutingLayer)((LayerOverlay)winformsMap1.Overlays["RoutingOverlay"]).Layers["RoutingLayer"];
            winformsMap1.Overlays["RoutingOverlay"].Lock.EnterWriteLock();
            try
            {
                routingLayer.Routes.Clear();
                routingLayer.Routes.Add(routingEngine.GetRoute("1", "500").Route);

            }
            finally
            {
                winformsMap1.Overlays["RoutingOverlay"].Lock.ExitWriteLock();
            }

            winformsMap1.Refresh();
 
  
 What do I do wrong?

Hi Hanna, 
  
 Please use generated _routable file as your feature source. 
  
 As below is the code work correct here: 
  
 

 winformsMap1.MapUnit = GeographyUnit.Meter;
            winformsMap1.ThreadingMode = MapThreadingMode.SingleThreaded;

            WorldMapKitWmsDesktopOverlay worldMapKitDesktopOverlay = new WorldMapKitWmsDesktopOverlay();
            winformsMap1.Overlays.Add(worldMapKitDesktopOverlay);

            winformsMap1.BackgroundOverlay.BackgroundBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean);

            RtgRoutingSource.GenerateRoutableShapeFile(@"…\SampleData\Roads1Full.shp", @"…\SampleData\Roads1Full_routable.shp", OverwriteMode.Overwrite);
            RtgRoutingSource.GenerateRoutingData(@"…\SampleData\Roads1Full_routable.rtg", @"…\SampleData\Roads1Full.shp", @"…\SampleData\Roads1Full_routable.shp", BuildRoutingDataMode.Rebuild, GeographyUnit.Meter, DistanceUnit.Meter);

            ShapeFileFeatureLayer routableStreetsLayer = new ShapeFileFeatureLayer(@"…\SampleData\Roads1Full_routable.shp");
            routableStreetsLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle = LineStyles.LocalRoad4;
            routableStreetsLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
            LayerOverlay routableStreetOverlay = new LayerOverlay();
            routableStreetOverlay.Layers.Add("routableStreets", routableStreetsLayer);
            winformsMap1.Overlays.Add("routingStreetOverlay", routableStreetOverlay);

            FeatureSource featureSource;
            featureSource = new ShapeFileFeatureSource(@"…\SampleData\Roads1Full_routable.shp");
            featureSource.Open();
            winformsMap1.CurrentExtent = featureSource.GetBoundingBox();
            //featureSource.Close();

            RoutingLayer routingLayer = new RoutingLayer();
            routableStreetsLayer.Open();

            routingLayer.StartPoint = routableStreetsLayer.FeatureSource.GetFeatureById("1", ReturningColumnsType.NoColumns).GetShape().GetCenterPoint();
            routingLayer.EndPoint = routableStreetsLayer.FeatureSource.GetFeatureById("500", ReturningColumnsType.NoColumns).GetShape().GetCenterPoint();
            //routableStreetsLayer.Close();

            LayerOverlay routingOverlay = new LayerOverlay();
            routingOverlay.Layers.Add("RoutingLayer", routingLayer);
            winformsMap1.Overlays.Add("RoutingOverlay", routingOverlay);

            RtgRoutingSource routingSource = new RtgRoutingSource(@"…\SampleData\Roads1Full_routable.rtg");
            RoutingEngine routingEngine = new RoutingEngine(routingSource, featureSource);
            routingEngine.GeographyUnit = GeographyUnit.Meter;
            routingLayer = (RoutingLayer)((LayerOverlay)winformsMap1.Overlays["RoutingOverlay"]).Layers["RoutingLayer"];
            winformsMap1.Overlays["RoutingOverlay"].Lock.EnterWriteLock();
            try
            {
                routingLayer.Routes.Clear();
                routingLayer.Routes.Add(routingEngine.GetRoute("1", "500").Route);

            }
            finally
            {
                winformsMap1.Overlays["RoutingOverlay"].Lock.ExitWriteLock();
            }

            winformsMap1.Refresh();

 
 
  
 Regards, 
  
 Don

Hi Don, 
  
 thanks, the code you provided works perfectly. I’m almost sure I tried the same, but I must have missed something… 
  
 I came across some other difficulties: 
 - when I calculate my route as you pointed me to above, the directions in my routing result are “invalid”. I guess they’re not calculated and therefor appear as not valid. When I force RoutingEngine to calculate the directions calling RoutingEngine.GetDrivingDirections for each segment returned by routingEngine.GetRouteSegments(), any direction returned is “forward”, which is not very helpful in routing. 
 - I have several road layers I want to use for routing. So I merged them to one “all-roads”-shapefile. Creating routing data and routing index for this large shapefile takes extremely long. Is there a more performant way to use more than one road layer? I tried using MultipleShapeFileFeatureSource, but somehow the routing index could not be built. Maybe I should give that a try again - will this be faster than using one large shapefile? 
 - is there a possibility to find out the world coordinates of a marker? 
 - is there a possibility in MapSuite to convert .TAB to .shp files? 
  
 Thanks in advance, 
  
 Hanna 


Hi Hanna,  
  
 As below is the answers about your question: 
  
 Invalid is the default value of a RouteSegment, could you please try put your data into our sample to make sure whether you can get the result in map? 
  
 You should still choose to create a "all-roads" shapefile, MultipleShapeFileFeatureSource is not recommended. 
  
 You can add map click event to get world coordinates or open mouse coordinates, so the coordinates will shown when mouse move on map. 
  
 You can use our layer to read tab file and get all features then save them to a new shape file. 
  
 BTW, you should want to put the last two questions to other sub forums like: Map Suite Desktop Edition Support. 
  
 Regards, 
  
 Don

Hi Don, 
  
 thank you for your help. I built the routable shape file and the routing index again and the directions are calculated now correctly.  
 Of course my third and fourth question do not belong into here, but to the Dektop Support Forum. Sorry for that!

Hi Hanna, 
  
 We are glad to hear your problem have been solved.  
  
 Any question please let us know. 
  
 Regards, 
  
 Don