/// <summary> /// Creates a CH data processing target. /// </summary> /// <param name="dynamicGraph"></param> /// <param name="interpreter"></param> /// <param name="tagsIndex"></param> /// <param name="vehicle"></param> public CHEdgeGraphOsmStreamTarget(IDynamicGraphRouterDataSource <CHEdgeData> dynamicGraph, IOsmRoutingInterpreter interpreter, ITagsIndex tagsIndex, Vehicle vehicle) : base(dynamicGraph, interpreter, new CHEdgeDataComparer(), tagsIndex) { _vehicle = vehicle; // _dynamicDataSource = dynamicGraph; }
/// <summary> /// Serializes all the tags in the given index. This serialization preserves the id's of each tag collection. /// </summary> /// <param name="stream">The target stream.</param> /// <param name="tagsIndex">The tags index to serialize.</param> public static void Serialize(Stream stream, ITagsIndex tagsIndex) { int begin = (int)stream.Position; // build a string index. ObjectTable <string> stringTable = new ObjectTable <string>(false); // convert tag collections to simpler objects. List <KeyValuePair <uint, List <KeyValuePair <uint, uint> > > > tagsIndexList = new List <KeyValuePair <uint, List <KeyValuePair <uint, uint> > > >(); for (uint tagId = 0; tagId < tagsIndex.Max; tagId++) { TagsCollection tagsCollection = tagsIndex.Get(tagId); if (tagsCollection != null) { // convert the tags collection to a list and add to the tag index. List <KeyValuePair <uint, uint> > tagsList = new List <KeyValuePair <uint, uint> >(); foreach (Tag tag in tagsCollection) { uint keyId = stringTable.Add(tag.Key); uint valueId = stringTable.Add(tag.Value); tagsList.Add(new KeyValuePair <uint, uint>( keyId, valueId)); } tagsIndexList.Add(new KeyValuePair <uint, List <KeyValuePair <uint, uint> > >(tagId, tagsList)); } } // do the serialization. TagIndexSerializer.Serialize(begin, stream, tagsIndexList, stringTable); // clear everything. tagsIndexList.Clear(); }
/// <summary> /// Creates a new osm edge data processing target. /// </summary> /// <param name="dynamicGraph"></param> /// <param name="interpreter"></param> /// <param name="tagsIndex"></param> /// <param name="idTransformations"></param> /// <param name="box"></param> public LiveGraphOsmStreamWriter(IDynamicGraphRouterDataSource <LiveEdge> dynamicGraph, IRoutingInterpreter interpreter, ITagsIndex tagsIndex, IDictionary <long, uint> idTransformations, GeoCoordinateBox box) : base(dynamicGraph, interpreter, null, tagsIndex, idTransformations, box) { _dynamicDataSource = dynamicGraph; }
/// <summary> /// Creates a new stream target. /// </summary> public CHEdgeFlatfileStreamTarget(Stream stream, ITagsIndex tagsIndex, Vehicle vehicle, MemoryMappedStream memoryMappedStream) : base(new RouterDataSource<CHEdgeData>(new DirectedGraph<CHEdgeData>(memoryMappedStream, 1000, CHEdgeData.MapFromDelegate, CHEdgeData.MapToDelegate, CHEdgeData.SizeUints), tagsIndex), new OsmRoutingInterpreter(), tagsIndex, vehicle) { _stream = stream; _memoryMappedStream = memoryMappedStream; }
/// <summary> /// Creates a new router data source. /// </summary> /// <param name="stream"></param> /// <param name="compressed"></param> /// <param name="tileMetas"></param> /// <param name="zoom"></param> /// <param name="v1RoutingDataSourceSerializer"></param> /// <param name="initialCapacity"></param> internal CHEdgeDataDataSource( Stream stream, bool compressed, CHEdgeDataDataSourceSerializer.SerializableGraphTileMetas tileMetas, int zoom, CHEdgeDataDataSourceSerializer v1RoutingDataSourceSerializer, int initialCapacity = 1000) { _tagsIndex = new SimpleTagsIndex(); _vertices = new SparseArray <Vertex>(initialCapacity); _coordinates = new SparseArray <Location>(initialCapacity); _vertexIndex = new QuadTree <GeoCoordinate, uint>(); _graphTileMetas = new Dictionary <Tile, TileStreamPosition>(); for (int tileIdx = 0; tileIdx < tileMetas.TileX.Length; tileIdx++) { _graphTileMetas.Add( new Tile(tileMetas.TileX[tileIdx], tileMetas.TileY[tileIdx], zoom), new TileStreamPosition() { Offset = tileMetas.Offset[tileIdx], Length = tileMetas.Length[tileIdx] }); } _loadedTiles = new HashSet <Tile>(); _tilesPerVertex = new Dictionary <uint, Tile>(); _zoom = zoom; _routingDataSourceSerializer = v1RoutingDataSourceSerializer; _stream = stream; _compressed = compressed; }
/// <summary> /// Creates a new osm memory router data source. /// </summary> /// <param name="graph"></param> /// <param name="tagsIndex"></param> /// <exception cref="ArgumentNullException"></exception> public DynamicGraphRouterDataSource(IDynamicGraph <TEdgeData> graph, ITagsIndex tagsIndex) { if (graph == null) { throw new ArgumentNullException("graph"); } if (tagsIndex == null) { throw new ArgumentNullException("tagsIndex"); } _graph = graph; _vertexIndex = new QuadTree <GeoCoordinate, uint>(); _tagsIndex = tagsIndex; _supportedVehicles = new HashSet <Vehicle>(); // add the current graph's vertices to the vertex index. for (uint newVertexId = 1; newVertexId < graph.VertexCount + 1; newVertexId++) { // add to the CHRegions. float latitude, longitude; graph.GetVertex(newVertexId, out latitude, out longitude); _vertexIndex.Add(new GeoCoordinate(latitude, longitude), newVertexId); } }
/// <summary> /// Preprocesses the data from the given OsmStreamReader and converts it directly to a routable data source. /// </summary> /// <param name="reader"></param> /// <param name="tagsIndex"></param> /// <param name="interpreter"></param> /// <param name="vehicle"></param> /// <param name="leaveReverseEdges"></param> /// <returns></returns> public static DynamicGraphRouterDataSource <CHEdgeData> Preprocess(OsmStreamSource reader, ITagsIndex tagsIndex, IOsmRoutingInterpreter interpreter, Vehicle vehicle, bool leaveReverseEdges) { // pull in the data. var dynamicGraphRouterDataSource = new DynamicGraphRouterDataSource <CHEdgeData>(tagsIndex); var targetData = new CHEdgeGraphOsmStreamTarget( dynamicGraphRouterDataSource, interpreter, tagsIndex, vehicle); targetData.RegisterSource(reader); targetData.Pull(); // compress the graph. INodeWitnessCalculator witnessCalculator = new DykstraWitnessCalculator(); var edgeDifference = new EdgeDifference( dynamicGraphRouterDataSource, witnessCalculator); var preProcessor = new CHPreProcessor( dynamicGraphRouterDataSource, edgeDifference, witnessCalculator); preProcessor.Start(); return(dynamicGraphRouterDataSource); }
/// <summary> /// Preprocesses the data from the given OsmStreamReader and converts it directly to a routable data source. /// </summary> /// <param name="reader"></param> /// <param name="tagsIndex"></param> /// <param name="interpreter"></param> /// <param name="vehicle"></param> /// <returns></returns> public static DynamicGraphRouterDataSource <CHEdgeData> Preprocess(OsmStreamSource reader, ITagsIndex tagsIndex, IOsmRoutingInterpreter interpreter, Vehicle vehicle) { return(CHEdgeGraphOsmStreamTarget.Preprocess(reader, tagsIndex, interpreter, vehicle, false)); }
/// <summary> /// Creates a new osm edge data processing target. /// </summary> public GraphOsmStreamTarget(RouterDataSourceBase <Edge> graph, IOsmRoutingInterpreter interpreter, ITagsIndex tagsIndex, ICoordinateIndex coordinates) : this(graph, interpreter, tagsIndex, null, true, coordinates, (g) => { return(new HilbertSortingPreprocessor <Edge>(g)); }) { }
/// <summary> /// Creates a CH data processing target. /// </summary> public CHEdgeGraphOsmStreamTarget(RouterDataSourceBase <CHEdgeData> graph, IOsmRoutingInterpreter interpreter, ITagsIndex tagsIndex, Vehicle vehicle) : this(graph, interpreter, tagsIndex, vehicle, (g) => { return(new DefaultPreprocessor(g)); }) { }
/// <summary> /// Creates a new osm edge data processing target. /// </summary> public GraphOsmStreamTarget(RouterDataSourceBase <Edge> graph, IOsmRoutingInterpreter interpreter, ITagsIndex tagsIndex, IEnumerable <Vehicle> vehicles, bool collectIntermediates) : this(graph, interpreter, tagsIndex, vehicles, collectIntermediates, new CoordinateIndex(), (g) => { return(new HilbertSortingPreprocessor <Edge>(g)); }) { }
/// <summary> /// Serialize/deserialize index. /// </summary> /// <param name="index"></param> /// <param name="from"></param> /// <param name="to"></param> /// <returns></returns> private ITagsIndexReadonly SerializeDeserialize(ITagsIndex index, uint from, uint to) { MemoryStream stream = new MemoryStream(); TagIndexSerializer.Serialize(stream, index, from, to); stream.Seek(0, SeekOrigin.Begin); return(TagIndexSerializer.Deserialize(stream)); }
/// <summary> /// Serialize/deserialize index. /// </summary> /// <param name="index"></param> /// <param name="blockSize"></param> /// <param name="position"></param> /// <returns></returns> private ITagsIndexReadonly SerializeDeserializeBlock(ITagsIndex index, uint blockSize, int position) { MemoryStream stream = new MemoryStream(); stream.Seek(position, SeekOrigin.Begin); TagIndexSerializer.SerializeBlocks(stream, index, blockSize); stream.Seek(position, SeekOrigin.Begin); return(TagIndexSerializer.DeserializeBlocks(stream)); }
/// <summary> /// Creates a CH data processing target. /// </summary> /// <param name="graph"></param> /// <param name="interpreter"></param> /// <param name="tagsIndex"></param> /// <param name="vehicle"></param> public CHEdgeGraphOsmStreamTarget(RouterDataSourceBase <CHEdgeData> graph, IOsmRoutingInterpreter interpreter, ITagsIndex tagsIndex, Vehicle vehicle) : base(graph, interpreter, tagsIndex) { if (!graph.IsDirected) { throw new ArgumentOutOfRangeException("Only directed graphs can be used for contraction hiearchies."); } _vehicle = vehicle; }
/// <summary> /// Preprocesses the data from the given OsmStreamReader and converts it directly to a routable data source. /// </summary> /// <param name="source">The source stream.</param> /// <param name="tagsIndex">The tags index.</param> /// <param name="interpreter">The routing interpreter.</param> /// <returns></returns> public static RouterDataSource <Edge> Preprocess(OsmStreamSource source, ITagsIndex tagsIndex, IOsmRoutingInterpreter interpreter) { var routerDataSource = new RouterDataSource <Edge>(new Graph <Edge>(), tagsIndex); var targetData = new GraphOsmStreamTarget(routerDataSource, interpreter, tagsIndex); targetData.RegisterSource(source); targetData.Pull(); return(routerDataSource); }
/// <summary> /// Preprocesses the data from the given OsmStreamReader and converts it directly to a routable data source. /// </summary> /// <param name="reader"></param> /// <param name="tagsIndex"></param> /// <param name="interpreter"></param> /// <returns></returns> public static DynamicGraphRouterDataSource <LiveEdge> Preprocess(OsmStreamSource reader, ITagsIndex tagsIndex, IRoutingInterpreter interpreter) { var dynamicGraphRouterDataSource = new DynamicGraphRouterDataSource <LiveEdge>(tagsIndex); var targetData = new LiveGraphOsmStreamWriter(dynamicGraphRouterDataSource, interpreter, dynamicGraphRouterDataSource.TagsIndex); targetData.RegisterSource(reader); targetData.Pull(); return(dynamicGraphRouterDataSource); }
/// <summary> /// Preprocesses the data from the given OsmStreamReader and converts it directly to a routable data source. /// </summary> /// <param name="reader"></param> /// <param name="tagsIndex"></param> /// <param name="interpreter"></param> /// <param name="vehicle"></param> /// <returns></returns> public static RouterDataSource <CHEdgeData> Preprocess(OsmStreamSource reader, ITagsIndex tagsIndex, IOsmRoutingInterpreter interpreter, Vehicle vehicle) { // pull in the data. var graph = new RouterDataSource <CHEdgeData>(new DirectedGraph <CHEdgeData>(), tagsIndex); var targetData = new CHEdgeGraphOsmStreamTarget( graph, interpreter, tagsIndex, vehicle); targetData.RegisterSource(reader); targetData.Pull(); return(graph); }
/// <summary> /// Serialize/deserialize index. /// </summary> /// <param name="index"></param> /// <param name="position"></param> /// <returns></returns> private ITagsIndexReadonly SerializeDeserializeLimitedStream(ITagsIndex index, int position) { MemoryStream memoryStream = new MemoryStream(); memoryStream.Seek(position, SeekOrigin.Begin); LimitedStream stream = new LimitedStream(memoryStream); TagIndexSerializer.Serialize(stream, index); stream.Seek(0, SeekOrigin.Begin); return(TagIndexSerializer.Deserialize(stream)); }
/// <summary> /// Creates a new osm memory router data source. /// </summary> /// <exception cref="ArgumentNullException"></exception> public DynamicGraphRouterDataSource(ITagsIndex tagsIndex) { if (tagsIndex == null) { throw new ArgumentNullException("tagsIndex"); } _graph = new MemoryDynamicGraph <TEdgeData>(); _vertexIndex = new QuadTree <GeoCoordinate, uint>(); _tagsIndex = tagsIndex; _supportedVehicles = new HashSet <Vehicle>(); }
/// <summary> /// Creates a new osm edge data processing target. /// </summary> /// <param name="graph">The graph.</param> /// <param name="interpreter">Inteprets the OSM-data.</param> /// <param name="tagsIndex">Holds all the tags.</param> /// <param name="vehicles">The vehicle profiles to build routing information for.</param> /// <param name="collectIntermediates">The collect intermediates flag.</param> /// <param name="coordinates">The coordinates index.</param> public GraphOsmStreamTarget(RouterDataSourceBase <Edge> graph, IOsmRoutingInterpreter interpreter, ITagsIndex tagsIndex, IEnumerable <Vehicle> vehicles, bool collectIntermediates, ICoordinateIndex coordinates) : base(graph, interpreter, tagsIndex, new HugeDictionary <long, uint>(), collectIntermediates, coordinates) { _vehicles = new HashSet <Vehicle>(); if (vehicles != null) { foreach (Vehicle vehicle in vehicles) { _vehicles.Add(vehicle); } } }
/// <summary> /// Creates a new osm edge data processing target. /// </summary> /// <param name="dynamicGraph"></param> /// <param name="interpreter"></param> /// <param name="tagsIndex"></param> /// <param name="idTransformations"></param> /// <param name="box"></param> /// <param name="vehicles">The vehicle profiles to build routing information for.</param> public LiveGraphOsmStreamTarget(IDynamicGraphRouterDataSource <LiveEdge> dynamicGraph, IOsmRoutingInterpreter interpreter, ITagsIndex tagsIndex, IDictionary <long, uint> idTransformations, GeoCoordinateBox box, IEnumerable <Vehicle> vehicles) : base(dynamicGraph, interpreter, null, tagsIndex, idTransformations, box) { _vehicles = new HashSet <Vehicle>(); if (vehicles != null) { foreach (Vehicle vehicle in vehicles) { _vehicles.Add(vehicle); } } }
/// <summary> /// Creates a new CH edge data source. /// </summary> public CHEdgeDataDataSource(Stream stream, CHEdgeDataDataSourceSerializer serializer, int startOfRegions, CHVertexRegionIndex regionIndex, int zoom, int startOfBlocks, CHBlockIndex blockIndex, uint blockSize) { _stream = stream; _serializer = serializer; this.InitializeRegions(startOfRegions, regionIndex, zoom); this.InitializeBlocks(startOfBlocks, blockIndex, blockSize); _blocks = new LRUCache <uint, CHBlock>(1000); _regions = new LRUCache <ulong, CHVertexRegion>(1000); _tagsIndex = new SimpleTagsIndex(); }
/// <summary> /// Calculates edge data. /// </summary> /// <param name="tagsIndex"></param> /// <param name="tags"></param> /// <param name="tagsForward"></param> /// <param name="from"></param> /// <param name="to"></param> /// <param name="edgeInterpreter"></param> /// <param name="intermediates"></param> /// <returns></returns> protected override Edge CalculateEdgeData(IEdgeInterpreter edgeInterpreter, ITagsIndex tagsIndex, TagsCollectionBase tags, bool tagsForward, GeoCoordinate from, GeoCoordinate to, List <GeoCoordinateSimple> intermediates) { if (edgeInterpreter == null) { throw new ArgumentNullException("edgeInterpreter"); } if (tagsIndex == null) { throw new ArgumentNullException("tagsIndex"); } if (tags == null) { throw new ArgumentNullException("tags"); } if (from == null) { throw new ArgumentNullException("from"); } if (to == null) { throw new ArgumentNullException("to"); } uint tagsId = tagsIndex.Add(tags); var shapeInBox = true; if (intermediates != null) { // verify shape-in-box. var box = new GeoCoordinateBox(from, to); for (int idx = 0; idx < intermediates.Count; idx++) { if (!box.Contains(intermediates[idx].Longitude, intermediates[idx].Latitude)) { // shape not in box. shapeInBox = false; break; } } } return(new Edge() { Forward = tagsForward, Tags = tagsId, Distance = (float)from.DistanceEstimate(to).Value, ShapeInBox = shapeInBox }); }
/// <summary> /// Creates a new processor target. /// </summary> /// <param name="dynamicGraph">The graph that will be filled.</param> /// <param name="interpreter">The interpreter to generate the edge data.</param> /// <param name="edgeComparer"></param> /// <param name="tagsIndex"></param> /// <param name="idTransformations"></param> /// <param name="box"></param> protected DynamicGraphOsmStreamWriter( IDynamicGraph <TEdgeData> dynamicGraph, IRoutingInterpreter interpreter, IDynamicGraphEdgeComparer <TEdgeData> edgeComparer, ITagsIndex tagsIndex, IDictionary <long, uint> idTransformations, GeoCoordinateBox box) { _dynamicGraph = dynamicGraph; _interpreter = interpreter; _edgeComparer = edgeComparer; _box = box; _tagsIndex = tagsIndex; _idTransformations = idTransformations; _preIndexMode = true; _preIndex = new HashSet <long>(); _usedTwiceOrMore = new HashSet <long>(); }
/// <summary> /// Returns true if the edge is traversable. /// </summary> /// <param name="edgeInterpreter"></param> /// <param name="tagsIndex"></param> /// <param name="tags"></param> /// <returns></returns> protected override bool CalculateIsTraversable(IEdgeInterpreter edgeInterpreter, ITagsIndex tagsIndex, TagsCollection tags) { if (_vehicles.Count > 0) { // limit only to vehicles in this list. foreach (Vehicle vehicle in _vehicles) { if (vehicle.CanTraverse(tags)) { // one of them is enough. return(true); } } return(false); } return(edgeInterpreter.IsRoutable(tags)); }
/// <summary> /// Creates a new osm memory router data source. /// </summary> /// <param name="graph"></param> /// <param name="tagsIndex"></param> /// <exception cref="ArgumentNullException"></exception> public RouterDataSource(GraphBase <TEdgeData> graph, ITagsIndex tagsIndex) { if (graph == null) { throw new ArgumentNullException("graph"); } if (tagsIndex == null) { throw new ArgumentNullException("tagsIndex"); } _graph = graph; _tagsIndex = tagsIndex; _supportedVehicles = new HashSet <Vehicle>(); }
/// <summary> /// Verifies the content of the given tags index agains the expected content list. /// </summary> /// <param name="tagsCollectionIndex"></param> /// <param name="expectedContent"></param> private void TestTagIndexContent(ITagsIndex tagsCollectionIndex, List <KeyValuePair <uint, TagsCollectionBase> > expectedContent) { // check the index. foreach (var pair in expectedContent) { var indexTags = tagsCollectionIndex.Get(pair.Key); Assert.AreEqual(pair.Value.Count, indexTags.Count); foreach (var tag in pair.Value) { Assert.IsTrue(indexTags.ContainsKeyValue(tag.Key, tag.Value)); } foreach (var tag in indexTags) { Assert.IsTrue(pair.Value.ContainsKeyValue(tag.Key, tag.Value)); } } }
/// <summary> /// Closes this target. /// </summary> public override void Close() { _coordinates.Clear(); _dataCache.Clear(); _idTransformations.Clear(); if (_nodesToCache != null) { _nodesToCache.Clear(); } if (_waysToCache != null) { _waysToCache.Clear(); } _restricedWays = null; _collapsedNodes = null; _preIndex = null; _relevantNodes = null; _tagsIndex = null; }
/// <summary> /// Creates a new processor target. /// </summary> /// <param name="graph">The graph that will be filled.</param> /// <param name="interpreter">The interpreter to generate the edge data.</param> /// <param name="tagsIndex"></param> /// <param name="idTransformations"></param> /// <param name="collectIntermediates"></param> /// <param name="coordinates"></param> protected GraphOsmStreamTargetBase( RouterDataSourceBase <TEdgeData> graph, IOsmRoutingInterpreter interpreter, ITagsIndex tagsIndex, HugeDictionary <long, uint> idTransformations, bool collectIntermediates, ICoordinateIndex coordinates) { _graph = graph; _interpreter = interpreter; _tagsIndex = tagsIndex; _idTransformations = idTransformations; _preIndexMode = true; _preIndex = new OsmSharp.Collections.LongIndex.LongIndex.LongIndex(); _relevantNodes = new OsmSharp.Collections.LongIndex.LongIndex.LongIndex(); _restricedWays = new HashSet <long>(); _collapsedNodes = new Dictionary <long, KeyValuePair <KeyValuePair <long, uint>, KeyValuePair <long, uint> > >(); _collectIntermediates = collectIntermediates; _dataCache = new OsmDataCacheMemory(); _coordinates = coordinates; }
/// <summary> /// Tests the given tags collection index. /// </summary> /// <param name="tagsCollectionIndex"></param> protected void TestTagIndex(ITagsIndex tagsCollectionIndex) { // set the seed manually. OsmSharp.Math.Random.StaticRandomGenerator.Set(116542346); var addedTags = new List <KeyValuePair <uint, TagsCollectionBase> >(); for (int i = 0; i < 100; i++) { var tagsCollection = new TagsCollection(); var tagCollectionSize = OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(3) + 1; for (int idx = 0; idx < tagCollectionSize; idx++) { var tagValue = OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(3); tagsCollection.Add( string.Format("key_{0}", tagValue), string.Format("value_{0}", tagValue)); } var addCount = OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(2) + 1; for (int idx = 0; idx < addCount; idx++) { var tagsId = tagsCollectionIndex.Add(tagsCollection); addedTags.Add(new KeyValuePair <uint, TagsCollectionBase>(tagsId, tagsCollection)); var indexTags = tagsCollectionIndex.Get(tagsId); Assert.AreEqual(tagsCollection.Count, indexTags.Count); foreach (var tag in tagsCollection) { Assert.IsTrue(indexTags.ContainsKeyValue(tag.Key, tag.Value)); } } } // test complete content. this.TestTagIndexContent(tagsCollectionIndex, addedTags); }
/// <summary> /// Creates a new stream target. /// </summary> public CHEdgeFlatfileStreamTarget(Stream stream, ITagsIndex tagsIndex, Vehicle vehicle) : base(new RouterDataSource<CHEdgeData>(new DirectedGraph<CHEdgeData>(), tagsIndex), new OsmRoutingInterpreter(), tagsIndex, vehicle) { _stream = stream; }
/// <summary> /// Serialize/deserialize index. /// </summary> /// <param name="index"></param> /// <param name="blockSize"></param> /// <param name="position"></param> /// <returns></returns> private ITagsIndexReadonly SerializeDeserializeBlock(ITagsIndex index, uint blockSize, int position) { MemoryStream stream = new MemoryStream(); stream.Seek(position, SeekOrigin.Begin); TagIndexSerializer.SerializeBlocks(stream, index, blockSize); stream.Seek(position, SeekOrigin.Begin); return TagIndexSerializer.DeserializeBlocks(stream); }
/// <summary> /// Serialize/deserialize index. /// </summary> /// <param name="index"></param> /// <param name="from"></param> /// <param name="to"></param> /// <returns></returns> private ITagsIndexReadonly SerializeDeserialize(ITagsIndex index, uint from, uint to) { MemoryStream stream = new MemoryStream(); TagIndexSerializer.Serialize(stream, index, from, to); stream.Seek(0, SeekOrigin.Begin); return TagIndexSerializer.Deserialize(stream); }
/// <summary> /// Creates a new OSM stream target. /// </summary> /// <param name="tagsIndex"></param> public OsmStreamTargetTags(ITagsIndex tagsIndex) { _tagsIndex = tagsIndex; }
/// <summary> /// Creates a new basic dykstra router. /// </summary> /// <param name="tagsIndex"></param> protected DykstraRoutingBase(ITagsIndex tagsIndex) { _tagsIndex = tagsIndex; }
/// <summary> /// Serializes all the tags in the given index. This serialization preserves the id's of each tag collection. /// </summary> /// <param name="stream">The target stream.</param> /// <param name="tagsIndex">The tags index to serialize.</param> public static void Serialize(Stream stream, ITagsIndex tagsIndex) { int begin = (int)stream.Position; // build a string index. ObjectTable<string> stringTable = new ObjectTable<string>(false); // convert tag collections to simpler objects. List<KeyValuePair<uint, List<KeyValuePair<uint, uint>>>> tagsIndexList = new List<KeyValuePair<uint,List<KeyValuePair<uint,uint>>>>(); for (uint tagId = 0; tagId < tagsIndex.Max; tagId++) { TagsCollection tagsCollection = tagsIndex.Get(tagId); if (tagsCollection != null) { // convert the tags collection to a list and add to the tag index. List<KeyValuePair<uint, uint>> tagsList = new List<KeyValuePair<uint, uint>>(); foreach (Tag tag in tagsCollection) { uint keyId = stringTable.Add(tag.Key); uint valueId = stringTable.Add(tag.Value); tagsList.Add(new KeyValuePair<uint, uint>( keyId, valueId)); } tagsIndexList.Add(new KeyValuePair<uint, List<KeyValuePair<uint, uint>>>(tagId, tagsList)); } } // do the serialization. TagIndexSerializer.Serialize(begin, stream, tagsIndexList, stringTable); // clear everything. tagsIndexList.Clear(); }
/// <summary> /// Serialize/deserialize index. /// </summary> /// <param name="index"></param> /// <param name="position"></param> /// <returns></returns> private ITagsIndexReadonly SerializeDeserializeLimitedStream(ITagsIndex index, int position) { MemoryStream memoryStream = new MemoryStream(); memoryStream.Seek(position, SeekOrigin.Begin); LimitedStream stream = new LimitedStream(memoryStream); TagIndexSerializer.Serialize(stream, index); stream.Seek(0, SeekOrigin.Begin); return TagIndexSerializer.Deserialize(stream); }
/// <summary> /// Creates a new live edge flat file. /// </summary> /// <param name="stream"></param> /// <param name="tagsIndex"></param> public LiveEdgeFlatfileStreamTarget(Stream stream, ITagsIndex tagsIndex) : base(new RouterDataSource<Edge>(new Graph<Edge>(), tagsIndex), new OsmRoutingInterpreter(), tagsIndex) { _stream = stream; }
/// <summary> /// Creates a reference stream target. /// </summary> /// <param name="graph"></param> /// <param name="interpreter"></param> /// <param name="tagsIndex"></param> /// <param name="vehicles"></param> public ReferenceGraphTarget(RouterDataSourceBase<Edge> graph, IOsmRoutingInterpreter interpreter, ITagsIndex tagsIndex, IEnumerable<Vehicle> vehicles) : base(graph, interpreter, tagsIndex, vehicles, false) { }
/// <summary> /// Verifies the content of the given tags index agains the expected content list. /// </summary> /// <param name="tagsCollectionIndex"></param> /// <param name="expectedContent"></param> private void TestTagIndexContent(ITagsIndex tagsCollectionIndex, List<KeyValuePair<uint, TagsCollectionBase>> expectedContent) { // check the index. foreach (var pair in expectedContent) { var indexTags = tagsCollectionIndex.Get(pair.Key); Assert.AreEqual(pair.Value.Count, indexTags.Count); foreach (var tag in pair.Value) { Assert.IsTrue(indexTags.ContainsKeyValue(tag.Key, tag.Value)); } foreach (var tag in indexTags) { Assert.IsTrue(pair.Value.ContainsKeyValue(tag.Key, tag.Value)); } } }
/// <summary> /// Tests the given tags collection index. /// </summary> /// <param name="tagsCollectionIndex"></param> protected void TestTagIndex(ITagsIndex tagsCollectionIndex) { // set the seed manually. OsmSharp.Math.Random.StaticRandomGenerator.Set(116542346); var addedTags = new List<KeyValuePair<uint, TagsCollectionBase>>(); for (int i = 0; i < 100; i++) { var tagsCollection = new TagsCollection(); var tagCollectionSize = OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(3) + 1; for (int idx = 0; idx < tagCollectionSize; idx++) { var tagValue = OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(3); tagsCollection.Add( string.Format("key_{0}", tagValue), string.Format("value_{0}", tagValue)); } var addCount = OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(2) + 1; for (int idx = 0; idx < addCount; idx++) { var tagsId = tagsCollectionIndex.Add(tagsCollection); addedTags.Add(new KeyValuePair<uint, TagsCollectionBase>(tagsId, tagsCollection)); var indexTags = tagsCollectionIndex.Get(tagsId); Assert.AreEqual(tagsCollection.Count, indexTags.Count); foreach (var tag in tagsCollection) { Assert.IsTrue(indexTags.ContainsKeyValue(tag.Key, tag.Value)); } } } // test complete content. this.TestTagIndexContent(tagsCollectionIndex, addedTags); }