C# sample realistic routing

C# sample realistic routing

using XtremeRoute.Shared;
using XtremeRoute.Route;
using XtremeRoute.Network;
using XtremeRoute.Tasks;

public static void RealisticRoutes(Tasker tasker, Router router)
{
    // Use all available threads, note that this might not always be best, 
    // for example in multiuser environments with many concurrant users.
    router.NrThreads = tasker.MaxNrThreads;

    // Turn on dual threading if we have 2 or more cores/cpus available.
    // This is the default setting, but shown here for instructional purposes.
    if (tasker.MaxNrThreads >= 2)
        router.Settings.UseDualThreading = true;

    // Enable dynamic routing, needed for TurnCosts and LinkModificatios.
    router.Settings.UseDynamicRouting = true;

    // This needs to be time for some of the settings below (this is default setting).
    router.Settings.CostType = CostType.Time;

    // Use a normal car, change for other vehicles
    router.Settings.Vehicle = Vehicles.Car;

    // Using accurate is very importand to get good VIA and fleet routes.
    router.Settings.DistanceMatrixAccuracy = DistanceMatrixAccuracy.Accurate;

    // Set extra delays (in seconds) for crosing other roads and turning.
    // These settings are examples only, and might need modification depending
    // on where the routing takes place or if driving on left side of road.
    TurnCosts tc = new TurnCosts();
    tc.CrossBiggerRoad = 30;
    tc.CrossSimilarRoad = 20;
    tc.CrossSmallerRoad = 10;
    tc.LeftToBiggerRoad = 45;
    tc.LeftToSimilarRoad = 35;
    tc.LeftToSmallerRoad = 25;
    tc.RightToBiggerRoad = 30;
    tc.RightToSimilarRoad = 20;
    tc.RightToSmallerRoad = 10;
    tc.UTurn = 45;
    router.Settings.TurnCosts = tc;
            
    // Adjust speed depending on road types, if road types are available.
    if (router.Storage.LinksHasRoadTypes)
    {
        // Turn on the use of roadtypes
        router.Settings.UseRoadTypes = true;

        // Adjust speed on most common roads
        router.Settings.SetRoadTypeModification(BaseRoadType.Motorway, DynamicModificationType.Relative, 1.1);
        router.Settings.SetRoadTypeModification(BaseRoadType.Trunk, DynamicModificationType.Relative, 1.05);
        router.Settings.SetRoadTypeModification(BaseRoadType.Primary, DynamicModificationType.Relative, 1);
        router.Settings.SetRoadTypeModification(BaseRoadType.Secondary, DynamicModificationType.Relative, 0.9);
        router.Settings.SetRoadTypeModification(BaseRoadType.Tertiary, DynamicModificationType.Relative, 0.9);
        router.Settings.SetRoadTypeModification(BaseRoadType.Quarternary, DynamicModificationType.Relative, 0.85);

        // Set speed and avoidance for roads we probably want to stay awya from as much as possible, the settings
        // below reduces speed my 0.8 (20%), but also makes road 50% less attractive internally during calculation.
        router.Settings.SetRoadTypeModification(BaseRoadType.LivingStreet, DynamicModificationType.Relative, 0.8, 0.5, false);
        router.Settings.SetRoadTypeModification(BaseRoadType.LivingStreetNarrow, DynamicModificationType.Relative, 0.8, 0.5, false);
        router.Settings.SetRoadTypeModification(BaseRoadType.Residential, DynamicModificationType.Relative, 0.8, 0.5, false);
        router.Settings.SetRoadTypeModification(BaseRoadType.ResidentialNarrow, DynamicModificationType.Relative, 0.8, 0.5, false);
        router.Settings.SetRoadTypeModification(BaseRoadType.Unpaved, DynamicModificationType.Relative, 0.8, 0.5, false);

        // Assume "Unknown" is a road we want to stay away from.
        router.Settings.SetRoadTypeModification(BaseRoadType.Unpaved, DynamicModificationType.Relative, 0.8, 0.5, false);

        // We use a car, so close some roads completely.
        router.Settings.CloseRoadTypeModification(BaseRoadType.Track);
        router.Settings.CloseRoadTypeModification(BaseRoadType.Bikeway);
        router.Settings.CloseRoadTypeModification(BaseRoadType.Footpath);
        router.Settings.CloseRoadTypeModification(BaseRoadType.Path);
    }

    // No matter if allowed or not, people usually do these so let them :-)
    router.Settings.AllowUTurns = true;

    // Set this to guard against errors in the network, in this case 
    // its assumed KmPerHour is speed unit, and 130 km/hour is a max.
    router.Settings.MaxSpeedAllowed = 130;
}
Select all code
Close
Copyright © 2012 Xtreme Route - All Rights Reserved. Contact us: