/// <summary> /// Downloads, extracts and builds the nwb router db. /// </summary> public static RouterDb DownloadExtractAndBuildRouterDb() { if (!File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "nwb.routerdb"))) { // download test data and extract to 'temp' directory relative to application base directory. Download.DownloadAndExtractShape("http://files.itinero.tech/data/open-data/NWB/WGS84_2016-09-01.zip", "WGS84_2016-09-01.zip"); // create a new router db and load the shapefile. var vehicle = DynamicVehicle.LoadFromEmbeddedResource(System.Reflection.Assembly.GetExecutingAssembly(), "OpenLR.Test.Functional.NWB.car.lua"); // load data for the car profile. var routerDb = new RouterDb(EdgeDataSerializer.MAX_DISTANCE); routerDb.LoadFromShape(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "temp"), "wegvakken.shp", "JTE_ID_BEG", "JTE_ID_END", vehicle); // write the router db to disk for later use. using (var ouputStream = File.OpenWrite(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "nwb.routerdb"))) { routerDb.Serialize(ouputStream); } return(routerDb); } else { return(RouterDb.Deserialize(File.OpenRead(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "nwb.routerdb")))); } }
public static void Main(string[] args) { Itinero.Logging.Logger.LogAction = (o, level, message, parameters) => { Console.WriteLine(string.Format("[{0}] {1} - {2}", o, level, message)); }; // download test data and extract to 'temp' directory relative to application base directory. Download.DownloadAndExtractShape("http://files.itinero.tech/data/open-data/NWB/WGS84_2016-09-01.zip", "WGS84_2016-09-01.zip"); // create a new router db and load the shapefile. var vehicle = DynamicVehicle.LoadFromStream(File.OpenRead( Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "car.lua"))); var routerDb = new RouterDb(EdgeDataSerializer.MAX_DISTANCE); routerDb.LoadFromShape(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "temp"), "wegvakken.shp", vehicle); // OPTIONAL: build a contracted version of the routing graph. // routerDb.AddContracted(vehicle.Fastest()); // write the router db to disk for later use. routerDb.Serialize(File.OpenWrite(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "nwb.routerdb"))); // calculate a test route. var router = new Router(routerDb); var route = router.Calculate(vehicle.Fastest(), new Coordinate(52.954718f, 6.338811f), new Coordinate(52.95359f, 6.337916f)); route = router.Calculate(vehicle.Fastest(), new Coordinate(51.57060821506861f, 5.46792984008789f), new Coordinate(51.58711643524425f, 5.4957228899002075f)); // generate instructions based on lua profile. var instructions = route.GenerateInstructions(); }
public static void Build(Options opts) { string executingPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)); string profileFile = Path.Combine(executingPath, ProfileFileName); var vehicle = DynamicVehicle.LoadFromStream(File.OpenRead(profileFile)); var routerDb = new RouterDb(); using (var stream = new FileInfo(opts.OsmFilePath).OpenRead()) { // read data which is relevant for car navigation only routerDb.LoadOsmData(stream, vehicle); } routerDb.AddContracted(Itinero.Osm.Vehicles.Vehicle.Car.Shortest()); routerDb.AddContracted(Itinero.Osm.Vehicles.Vehicle.Car.Fastest()); // write the routerdb to disk. Directory.CreateDirectory(opts.Outputdirectory); using (var stream = new FileInfo(Path.Combine(opts.Outputdirectory, "file.routerdb")).Open(FileMode.Create)) { routerDb.Serialize(stream); } }
/// <summary> /// Creates a new vehicle relation tag processor. /// </summary> public DynamicVehicleRelationTagProcessor(DynamicVehicle vehicle) { _vehicle = vehicle; _relationTagProcessor = vehicle.Script.Globals["relation_tag_processor"]; if (_relationTagProcessor != null) { _attributesTable = new Table(vehicle.Script); _resultsTable = new Table(vehicle.Script); } }
private readonly Func <Node, uint> _markCore; // marks the node as core. /// <summary> /// Creates a new processor. /// </summary> public DynamicVehicleNodeTagProcessor(RouterDb routerDb, DynamicVehicle vehicle, Func <Node, uint> markCore) { _routerDb = routerDb; _vehicle = vehicle; _nodeTagProcessor = vehicle.Script.Globals["node_tag_processor"]; _markCore = markCore; if (_nodeTagProcessor != null) { _attributesTable = new Table(vehicle.Script); _resultsTable = new Table(vehicle.Script); } }
/// <summary> /// Creates a new processor. /// </summary> public DynamicVehicleNodeRestrictionProcessor(RouterDb routerDb, DynamicVehicle vehicle, Func <Node, uint> markCore, Action <string, List <uint> > foundRestriction) { _foundRestriction = foundRestriction; _routerDb = routerDb; _vehicle = vehicle; _markCore = markCore; _nodeRestrictionFunc = vehicle.Script.Globals["node_restriction"]; _markCore = markCore; if (_nodeRestrictionFunc != null) { _attributesTable = new Table(vehicle.Script); _resultsTable = new Table(vehicle.Script); } }
public void TestOSMCar() { var vehicle = DynamicVehicle.LoadFromEmbeddedResource(typeof(DynamicVehicleTests).Assembly, "Itinero.Test.test_data.profiles.osm.car.lua").Fastest(); // invalid highway types. this.TestFactorAndSpeed(vehicle, 0, 0, 0, "highwey", "road"); // default highway types. this.TestFactorAndSpeed(vehicle, 0, 0, 0, "highway", "footway"); this.TestFactorAndSpeed(vehicle, 0, 0, 0, "highway", "pedestrian"); this.TestFactorAndSpeed(vehicle, 0, 0, 0, "highway", "path"); this.TestFactorAndSpeed(vehicle, 0, 0, 0, "highway", "cycleway"); this.TestFactorAndSpeed(vehicle, 0, null, 05, "highway", "living_street"); this.TestFactorAndSpeed(vehicle, 0, null, 30, "highway", "road"); this.TestFactorAndSpeed(vehicle, 0, null, 30, "highway", "track"); this.TestFactorAndSpeed(vehicle, 0, null, 50, "highway", "unclassified"); this.TestFactorAndSpeed(vehicle, 0, null, 50, "highway", "residential"); this.TestFactorAndSpeed(vehicle, 0, null, 70, "highway", "tertiary"); this.TestFactorAndSpeed(vehicle, 0, null, 70, "highway", "tertiary_link"); this.TestFactorAndSpeed(vehicle, 0, null, 70, "highway", "secondary"); this.TestFactorAndSpeed(vehicle, 0, null, 70, "highway", "secondary_link"); this.TestFactorAndSpeed(vehicle, 0, null, 90, "highway", "primary"); this.TestFactorAndSpeed(vehicle, 0, null, 90, "highway", "primary_link"); this.TestFactorAndSpeed(vehicle, 0, null, 90, "highway", "trunk"); this.TestFactorAndSpeed(vehicle, 0, null, 90, "highway", "trunk_link"); this.TestFactorAndSpeed(vehicle, 3, null, 120, "highway", "motorway"); this.TestFactorAndSpeed(vehicle, 3, null, 120, "highway", "motorway_link"); // designated roads. this.TestFactorAndSpeed(vehicle, 0, null, 50, "highway", "unclassified", "foot", "designated"); this.TestFactorAndSpeed(vehicle, 0, null, 50, "highway", "unclassified", "foot", "yes"); this.TestFactorAndSpeed(vehicle, 0, null, 50, "highway", "unclassified", "foot", "no"); this.TestFactorAndSpeed(vehicle, 0, null, 50, "highway", "unclassified", "bicycle", "designated"); this.TestFactorAndSpeed(vehicle, 0, null, 50, "highway", "unclassified", "bicycle", "yes"); this.TestFactorAndSpeed(vehicle, 0, null, 50, "highway", "unclassified", "bicycle", "no"); this.TestFactorAndSpeed(vehicle, 0, null, 50, "highway", "unclassified", "motorcar", "yes"); this.TestFactorAndSpeed(vehicle, 0, 0, 0, "highway", "unclassified", "motorcar", "no"); // test maxspeed tags. this.TestFactorAndSpeed(vehicle, 0, null, 30 * .75f, "highway", "primary", "maxspeed", "30"); this.TestFactorAndSpeed(vehicle, 0, null, 20 * 1.60934f * .75f, "highway", "primary", "maxspeed", "20 mph"); }
static void DownloadExtractAndBuildRouterDb() { // download test data and extract to 'temp' directory relative to application base directory. if (!File.Exists("WGS84_2016-09-01.zip")) { Download.DownloadAndExtractShape("http://files.itinero.tech/data/open-data/NWB/WGS84_2016-09-01.zip", "WGS84_2016-09-01.zip"); } // create a new router db and load the shapefile. var vehicle = DynamicVehicle.LoadFromStream(File.OpenRead("car.lua")); // load data for the car profile. var routerDb = new RouterDb(EdgeDataSerializer.MAX_DISTANCE); routerDb.LoadFromShape(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "temp"), "wegvakken.shp", "JTE_ID_BEG", "JTE_ID_END", vehicle); // write the router db to disk for later use. using (var ouputStream = File.OpenWrite(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "nwb.routerdb"))) { routerDb.Serialize(ouputStream); } }
public void TestShapeCar() { var vehicle = DynamicVehicle.LoadFromEmbeddedResource(typeof(DynamicVehicleTests).Assembly, "Itinero.Test.test_data.profiles.shape.car.lua"); var profile = vehicle.Fastest(); // default types. this.TestFactorAndSpeed(profile, 0, null, 50, "BST_CODE", "BVD"); this.TestFactorAndSpeed(profile, 0, null, 70, "BST_CODE", "AF"); this.TestFactorAndSpeed(profile, 0, null, 70, "BST_CODE", "OP"); this.TestFactorAndSpeed(profile, 0, null, 120, "BST_CODE", "HR"); this.TestFactorAndSpeed(profile, 1, null, 30, "BST_CODE", "MRB"); this.TestFactorAndSpeed(profile, 1, null, 30, "BST_CODE", "NRB"); // test parameters. var value = string.Empty; Assert.IsTrue(vehicle.Parameters.TryGetValue("source_vertex", out value)); Assert.AreEqual("JTE_ID_BEG", value); Assert.IsTrue(vehicle.Parameters.TryGetValue("target_vertex", out value)); Assert.AreEqual("JTE_ID_END", value); }
static void Main(string[] args) { // enable logging. OsmSharp.Logging.Logger.LogAction = (o, level, message, parameters) => { Console.WriteLine($"[{o}] {level} - {message}"); }; Itinero.Logging.Logger.LogAction = (o, level, message, parameters) => { Console.WriteLine($"[{o}] {level} - {message}"); }; Download.ToFile("http://files.itinero.tech/data/OSM/planet/europe/luxembourg-latest.osm.pbf", "luxembourg-latest.osm.pbf").Wait(); // load some routing data and create a router. var routerDb = new RouterDb(); var customCar = DynamicVehicle.Load(File.ReadAllText("custom-car.lua")); using (var stream = File.OpenRead("luxembourg-latest.osm.pbf")) { routerDb.LoadOsmData(stream, customCar); } // add custom profiles. var speed10 = routerDb.EdgeProfiles.Add(new AttributeCollection( new Attribute("highway", "residential"), new Attribute("custom-speed", "10"))); var speed20 = routerDb.EdgeProfiles.Add(new AttributeCollection( new Attribute("highway", "residential"), new Attribute("custom-speed", "20"))); var speed30 = routerDb.EdgeProfiles.Add(new AttributeCollection( new Attribute("highway", "residential"), new Attribute("custom-speed", "30"))); var speed40 = routerDb.EdgeProfiles.Add(new AttributeCollection( new Attribute("highway", "residential"), new Attribute("custom-speed", "40"))); // define locations, profile and router. var location1 = new Coordinate(49.88826851632804f, 5.815232992172241f); var location2 = new Coordinate(49.88775699771737f, 5.8133286237716675f); var router = new Router(routerDb); // calculate route before. var routeBefore = router.Calculate(customCar.Fastest(), location1, location2); var routeBeforeGeoJson = routeBefore.ToGeoJson(); // resolve an edge. var edgeLocation = new Coordinate(49.888040407347006f, 5.8142513036727905f); var resolved = router.Resolve(customCar.Fastest(), edgeLocation); // update the speed profile of this edge. var edgeData = routerDb.Network.GetEdge(resolved.EdgeId).Data; edgeData.Profile = (ushort)speed10; routerDb.Network.UpdateEdgeData(resolved.EdgeId, edgeData); // calculate route. var routeAfter = router.Calculate(customCar.Fastest(), location1, location2); var routeAfterGeoJson = routeAfter.ToGeoJson(); // calculate route to middle of edge. var location3 = new Coordinate(49.888035223039466f, 5.814205706119537f); var routeAfter13 = router.Calculate(customCar.Fastest(), location1, location3); var routeAfter13GeoJson = routeAfter13.ToGeoJson(); }
/// <summary> /// Parses this command into a processor given the arguments for this switch. Consumes the previous processors and returns how many it consumes. /// </summary> public override int Parse(List <Processor> previous, out Processor processor) { var vehicles = new List <Vehicle>(new Vehicle[] { Itinero.Osm.Vehicles.Vehicle.Car }); var allCore = false; var keepWayIds = false; for (var i = 0; i < this.Arguments.Length; i++) { string key, value; if (SwitchParsers.SplitKeyValue(this.Arguments[i], out key, out value)) { switch (key.ToLower()) { case "vehicles": case "vehicle": string[] vehicleValues; if (SwitchParsers.SplitValuesArray(value.ToLower(), out vehicleValues)) { // split the values array. vehicles = new List <Vehicle>(vehicleValues.Length); for (int v = 0; v < vehicleValues.Length; v++) { Vehicle vehicle; if (!Vehicle.TryGet(vehicleValues[v], out vehicle)) { if (vehicleValues[v] == "all") { // all vehicles. vehicles.Add(Itinero.Osm.Vehicles.Vehicle.Bicycle); vehicles.Add(Itinero.Osm.Vehicles.Vehicle.BigTruck); vehicles.Add(Itinero.Osm.Vehicles.Vehicle.Bus); vehicles.Add(Itinero.Osm.Vehicles.Vehicle.Car); vehicles.Add(Itinero.Osm.Vehicles.Vehicle.Moped); vehicles.Add(Itinero.Osm.Vehicles.Vehicle.MotorCycle); vehicles.Add(Itinero.Osm.Vehicles.Vehicle.Pedestrian); vehicles.Add(Itinero.Osm.Vehicles.Vehicle.SmallTruck); } else if (vehicleValues[v] == "motorvehicle" || vehicleValues[v] == "motorvehicles") { // all motor vehicles. vehicles.Add(Itinero.Osm.Vehicles.Vehicle.BigTruck); vehicles.Add(Itinero.Osm.Vehicles.Vehicle.Bus); vehicles.Add(Itinero.Osm.Vehicles.Vehicle.Car); vehicles.Add(Itinero.Osm.Vehicles.Vehicle.MotorCycle); vehicles.Add(Itinero.Osm.Vehicles.Vehicle.SmallTruck); } else { // assume a filename. var vehicleFile = new FileInfo(vehicleValues[v]); if (!vehicleFile.Exists) { throw new SwitchParserException("--create-routerdb", string.Format("Invalid parameter value for command --create-routerdb: Vehicle profile '{0}' not found.", vehicleValues[v])); } using (var stream = vehicleFile.OpenRead()) { vehicle = DynamicVehicle.LoadFromStream(stream); vehicle.Register(); vehicles.Add(vehicle); } } } else { vehicles.Add(vehicle); } } } break; case "allcore": if (SwitchParsers.IsTrue(value)) { allCore = true;; } break; case "keepwayids": if (SwitchParsers.IsTrue(value)) { keepWayIds = true;; } break; default: throw new SwitchParserException("--create-routerdb", string.Format("Invalid parameter for command --create-routerdb: {0} not recognized.", key)); } } } if (!(previous[previous.Count - 1] is Processors.Osm.IProcessorOsmStreamSource)) { throw new Exception("Expected an OSM stream source."); } var source = (previous[previous.Count - 1] as Processors.Osm.IProcessorOsmStreamSource).Source; Func <Itinero.RouterDb> getRouterDb = () => { var routerDb = new Itinero.RouterDb(); // load the data. var target = new Itinero.IO.Osm.Streams.RouterDbStreamTarget(routerDb, vehicles.ToArray(), allCore, processRestrictions: true); if (keepWayIds) { // add way id's. var eventsFilter = new OsmSharp.Streams.Filters.OsmStreamFilterDelegate(); eventsFilter.MoveToNextEvent += EventsFilter_AddWayId; eventsFilter.RegisterSource(source); target.RegisterSource(eventsFilter, false); } else { // use the source as-is. target.RegisterSource(source); } target.Pull(); // sort the network. routerDb.Sort(); return(routerDb); }; processor = new Processors.RouterDb.ProcessorRouterDbSource(getRouterDb); return(1); }