Skip to content

Commit

Permalink
Added a classification based profile for issue #19.
Browse files Browse the repository at this point in the history
  • Loading branch information
xivk committed Sep 9, 2016
1 parent 4884b93 commit 9a6c92c
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 2 deletions.
4 changes: 2 additions & 2 deletions SharedAssemblyVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@

using System.Reflection;

[assembly: AssemblyVersion("0.23.3")] // semantic versioning Major.Minor.Patch
[assembly: AssemblyInformationalVersion("0.23.3-rc1")]
[assembly: AssemblyVersion("0.23.4")] // semantic versioning Major.Minor.Patch
[assembly: AssemblyInformationalVersion("0.23.4-rc1")]
24 changes: 24 additions & 0 deletions src/Itinero/Osm/Vehicles/MotorVehicle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// along with Itinero. If not, see <http://www.gnu.org/licenses/>.

using Itinero.Attributes;
using Itinero.Profiles;

namespace Itinero.Osm.Vehicles
{
Expand Down Expand Up @@ -137,5 +138,28 @@ public override float MinSpeed()
{
return 30;
}

/// <summary>
/// Gets a profile that aggressively follows the road classifications.
/// </summary>
/// <returns></returns>
public virtual Profile Classified()
{
return new Profiles.MotorVehicleClassifications(this);
}

/// <summary>
/// Gets all profiles for this vehicle.
/// </summary>
/// <returns></returns>
public override Profile[] GetProfiles()
{
return new Profile[]
{
this.Fastest(),
this.Shortest(),
this.Classified()
};
}
}
}
109 changes: 109 additions & 0 deletions src/Itinero/Osm/Vehicles/Profiles/MotorVehicleClassifications.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Itinero - Routing for .NET
// Copyright (C) 2016 Abelshausen Ben
//
// This file is part of Itinero.
//
// Itinero is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// Itinero is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Itinero. If not, see <http://www.gnu.org/licenses/>.

using Itinero.Attributes;
using Itinero.Osm.Vehicles;
using Itinero.Profiles;
using System;

namespace Itinero.Osm.Vehicles.Profiles
{
/// <summary>
/// A car profile that aggresively follows the road classifications. Motorway > Trunk > Primary > Secondary ... > Unclassified.
/// </summary>
internal class MotorVehicleClassifications : Profile
{
private static float CLASS_FACTOR = 4;
private static float MOTORWAY = 10;
private static float TRUNK = 9;
private static float PRIMARY = 8;
private static float SECONDARY = 7;
private static float TERTIARY = 6;
private static float RESIDENTIAL = 5;
private static float REST = 4;

internal MotorVehicleClassifications(MotorVehicle mv)
: base(mv.UniqueName + ".Classifications", mv.GetGetSpeed(), mv.GetGetMinSpeed(),
mv.GetCanStop(), mv.GetEquals(), mv.VehicleTypes, InternalGetFactor(mv))
{

}

/// <summary>
/// Gets a custom factor for the given tags.
/// </summary>
private static Func<IAttributeCollection, Factor> InternalGetFactor(MotorVehicle mv)
{
// adjusts to a hypothetical speed indicating preference.

var getFactorDefault = mv.GetGetFactor();
var getSpeedDefault = mv.GetGetSpeed();
return (tags) =>
{
var speed = getSpeedDefault(tags);
if (speed.Value == 0)
{
return new Factor()
{
Value = 0,
Direction = 0
};
}
string highwayType;
if (tags.TryGetValue("highway", out highwayType))
{
switch (highwayType)
{
case "motorway":
case "motorway_link":
speed.Value = speed.Value * CLASS_FACTOR * MOTORWAY;
break;
case "trunk":
case "trunk_link":
speed.Value = speed.Value * CLASS_FACTOR * TRUNK;
break;
case "primary":
case "primary_link":
speed.Value = speed.Value * CLASS_FACTOR * PRIMARY;
break;
case "secondary":
case "secondary_link":
speed.Value = speed.Value * CLASS_FACTOR * SECONDARY;
break;
case "tertiary":
case "tertiary_link":
speed.Value = speed.Value * CLASS_FACTOR * TERTIARY;
break;
case "residential":
speed.Value = speed.Value * CLASS_FACTOR * RESIDENTIAL;
break;
default:
speed.Value = speed.Value * CLASS_FACTOR * REST;
break;
}
}
return new Factor()
{
Value = 1.0f / speed.Value,
Direction = speed.Direction
};
};
}
}
}

0 comments on commit 9a6c92c

Please sign in to comment.