Exemplo n.º 1
0
        public int Write(SegmentDescription description)
        {
            EnsureWrite(writeStage.Descriptions);
            int i = descriptionCount++;

            descriptions[description] = i;

            writer.WriteStartObject();
            writer.WritePropertyName(Constants.TAG_SEGMENT_DESCRIPTION_LANES);
            writer.WriteStartArray();
            foreach (var lane in description.Lanes)
            {
                serializer.Serialize(writer, lane);
            }
            writer.WriteEndArray();
            writer.WriteEndObject();
            return(i);
        }
Exemplo n.º 2
0
        private IEnumerable <SegmentDescription> readDescriptions(JsonTextReader reader, JsonSerializer serializer, List <SegmentDescription> descriptions)
        {
            // use single buffer for the lanes, they are usually the same size and clear doesn't reset the capacity?
            var lanes = new List <LaneDescription>();

            if (reader.Read())
            {
                // start array
                if (reader.TokenType == JsonToken.StartArray)
                {
                    do
                    {
                        if (reader.Read()) // inside node
                        {
                            if (reader.TokenType == JsonToken.StartObject)
                            {
                                var segmentDescription = new SegmentDescription();
                                int depth = reader.Depth;
                                do
                                {
                                    if (reader.Read())
                                    {
                                        if (reader.TokenType == JsonToken.PropertyName)
                                        {
                                            switch ((string)reader.Value)
                                            {
                                            case Constants.TAG_SEGMENT_DESCRIPTION_LANES:
                                                segmentDescription.Lanes = readLanes(reader, serializer, lanes);
                                                break;
                                            }
                                        }
                                    }
                                } while (reader.Depth > depth);
                                descriptions.Add(segmentDescription);
                                yield return(segmentDescription);
                            }
                        }
                    } while (reader.TokenType != JsonToken.EndArray);
                }
            }
        }
Exemplo n.º 3
0
        private IEnumerable <Segment> readSegments(JsonTextReader reader, RoadManager roads, JsonSerializer serializer, List <Node> nodes, List <SegmentDescription> descriptions)
        {
            if (reader.Read())
            {
                // start array
                if (reader.TokenType == JsonToken.StartArray)
                {
                    do
                    {
                        if (reader.Read()) // inside node
                        {
                            if (reader.TokenType == JsonToken.StartObject)
                            {
                                Node    start = null, end = null;
                                Vector3 startTangent = Vector3.zero, endTangent = Vector3.zero;
                                Vector3 startOffset = Vector3.zero, endOffset = Vector3.zero;

                                SegmentDescription description = null;
                                int depth = reader.Depth;
                                do
                                {
                                    if (reader.Read())
                                    {
                                        if (reader.TokenType == JsonToken.PropertyName)
                                        {
                                            switch ((string)reader.Value)
                                            {
                                            case Constants.TAG_SEGMENT_START:
                                                readConnection(reader, serializer, roads, nodes, out start, out startTangent, out startOffset);
                                                break;

                                            case Constants.TAG_SEGMENT_END:
                                                readConnection(reader, serializer, roads, nodes, out end, out endTangent, out endOffset);
                                                break;

                                            case Constants.TAG_SEGMENT_DESCRIPTION:
                                                description = descriptions[reader.ReadAsInt32() ?? -1];
                                                break;
                                            }
                                        }
                                    }
                                } while (reader.Depth > depth);

                                if (start != null && end != null)
                                {
                                    var seg = roads.CreateSegment(start, end, description);
                                    seg.Start.Tangent = startTangent;
                                    seg.End.Tangent   = endTangent;
                                    seg.Start.Offset  = startOffset;
                                    seg.End.Offset    = endOffset;
                                    yield return(seg);
                                }
                                else
                                {
                                    throw new FormatException("expected start AND end connections for segment");
                                }
                            }
                        }
                    } while (reader.TokenType != JsonToken.EndArray);
                }
            }
        }
Exemplo n.º 4
0
        public async Task LoadAsync(string file)
        {
            var bounds         = new coordinateSet();
            var bounds2        = new coordinateSet();
            int failCount      = 0;
            var nodeDictionary = new Dictionary <long, Simulation.Traffic.Node>();

            using (var stream = File.OpenRead(file))
            {
                OsmStreamSource source;
                if (file.EndsWith(".pbf"))
                {
                    source = new PBFOsmStreamSource(stream);
                }
                else
                {
                    source = new XmlOsmStreamSource(stream);
                }

                using (source)
                {
                    var nodes = source.AsParallel().OfType <OsmSharp.Node>().ToDictionary(k => k.Id);
                    var ways  = source.AsParallel().Where(s => s.Type == OsmSharp.OsmGeoType.Way).OfType <Way>();

                    var highWays = ways.Where(w => (w.Tags.ContainsKey("highway") || w.Tags.ContainsKey("railway")) && !w.Tags.ContainsKey("fixme")).ToArray();

                    //foreach (var way in highWays)

                    highWays.AsParallel().ForAll(way =>
                    {
                        foreach (var n in way.Nodes)
                        {
                            OsmSharp.Node node;
                            if (nodes.TryGetValue(n, out node))
                            {
                                var lat = (node.Latitude ?? 0);
                                var lon = (node.Longitude ?? 0);
                                lock (bounds)
                                    bounds.fit(lat, lon);
                            }
                        }
                    });

                    var srtmFolder = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "SRTM");
                    System.IO.Directory.CreateDirectory(srtmFolder);

                    var b = new OsmSharp.API.Bounds
                    {
                        MinLatitude  = bounds.minLat,
                        MaxLatitude  = bounds.maxLat,
                        MinLongitude = bounds.minLon,
                        MaxLongitude = bounds.maxLon
                    };
                    await SRTM.SRTMDownloader.DownloadAsync(b, srtmFolder);

                    var data = new SrtmData(srtmFolder);
                    manager = new CanvasRoadManager(canvas, toX(bounds, bounds.maxLon, bounds.maxLat), toY(bounds, bounds.maxLon, bounds.maxLat));
                    foreach (var way in highWays)
                    {
                        string tagValue;
                        if (way.Tags.TryGetValue("area", out tagValue))
                        {
                            if (tagValue == "yes")
                            {
                                continue;
                            }
                        }

                        Simulation.Traffic.Node lastTrafficNode = null;
                        SegmentDescription      description     = getDescription(way);
                        if (description == null)
                        {
                            continue;
                        }
                        foreach (var n in way.Nodes)
                        {
                            OsmSharp.Node node;
                            if (nodes.TryGetValue(n, out node))
                            {
                                Simulation.Traffic.Node trafficNode = null;
                                if (!nodeDictionary.TryGetValue(n, out trafficNode))
                                {
                                    var lat = node.Latitude ?? 0;
                                    var lon = node.Longitude ?? 0;
                                    var ele = GetHeight(data, lat, lon);

                                    nodeDictionary.Add(n, trafficNode    = manager.CreateNode(new Vector3(toX(bounds, lon, lat), ele, toY(bounds, lon, lat))));
                                    (trafficNode as TrafficNode).OSMNode = node;
                                }

                                if (lastTrafficNode != null)
                                {
                                    manager.CreateSegment(lastTrafficNode, trafficNode, description);
                                }
                                lastTrafficNode = trafficNode;
                            }
                            else
                            {
                                failCount++;
                            }
                        }
                    }

                    int nodeCountBefore = this.manager.Nodes.Count();
                    mergeCloseNodes();

                    cleanupStraights();

                    cleanupCorners();
                    int nodeCountAfter = this.manager.Nodes.Count();

                    System.Diagnostics.Debug.WriteLine($"Before:  {nodeCountBefore}, after: {nodeCountAfter}");

                    foreach (var node in manager.Nodes)
                    {
                        node.UpdateOffsets();
                    }

                    manager.Update();
                }
            }

            detailsGrid.DataContext = manager;
        }
Exemplo n.º 5
0
 public TrafficSegment(SegmentDescription description, CanvasRoadManager manager) : base(description, manager)
 {
 }
        /// <summary>
        /// Convert the SegmentDescription struct defined in adapter to stack 
        /// </summary>
        /// <param name="segmentDescription">The segments field is composed of a number cSegments
        ///  of SegmentDescription fields. Each SegmentDescription field corresponds to a content segment
        ///  in the order in which they appear in the original content.
        ///  </param>
        /// <returns>Return the SegmentDescription type defined in stack</returns>
        private static TestTools.StackSdk.BranchCache.Pccrc.SegmentDescription[] ConvertToStackForSegDescription(
            SegmentDescription[] segmentDescription)
        {
            TestTools.StackSdk.BranchCache.Pccrc.SegmentDescription[] segmentDescriptionStack
                = new TestTools.StackSdk.BranchCache.Pccrc.SegmentDescription[segmentDescription.Length];
            for (int i = 0; i < segmentDescriptionStack.Length; i++)
            {
                segmentDescriptionStack[i].cbBlockSize = segmentDescription[i].CbBlockSize;
                segmentDescriptionStack[i].cbSegment = segmentDescription[i].CbSegment;
                segmentDescriptionStack[i].SegmentHashOfData = segmentDescription[i].SegmentHashOfData;
                segmentDescriptionStack[i].SegmentSecret = segmentDescription[i].SegmentSecret;
                segmentDescriptionStack[i].ullOffsetInContent = segmentDescription[i].UllOffsetInContent;
            }

            return segmentDescriptionStack;
        }
        /// <summary>
        /// Convert the SegmentDescription struct defined in stack to adapter 
        /// </summary>
        /// <param name="segmentDescriptionStack">The segments field is composed of a number cSegments
        ///  of SegmentDescription fields. Each SegmentDescription field corresponds to a content segment
        ///  in the order in which they appear in the original content.
        ///  </param>
        /// <returns>Return the SegmentDescription type defined in adapter</returns>
        private static SegmentDescription[] ConvertFromStackForSegDescription(
            TestTools.StackSdk.BranchCache.Pccrc.SegmentDescription[] segmentDescriptionStack)
        {
            Microsoft.Protocols.TestSuites.Pchc.SegmentDescription[] segmentDescription
                = new SegmentDescription[segmentDescriptionStack.Length];
            for (int i = 0; i < segmentDescription.Length; i++)
            {
                segmentDescription[i].CbBlockSize = segmentDescriptionStack[i].cbBlockSize;
                segmentDescription[i].CbSegment = segmentDescriptionStack[i].cbSegment;
                segmentDescription[i].SegmentHashOfData = segmentDescriptionStack[i].SegmentHashOfData;
                segmentDescription[i].SegmentSecret = segmentDescriptionStack[i].SegmentSecret;
                segmentDescription[i].UllOffsetInContent = segmentDescriptionStack[i].ullOffsetInContent;
            }

            return segmentDescription;
        }