示例#1
0
        private float toX(coordinateSet bounds, float lon, float lat)
        {
            if (lat < bounds.minLat)
            {
                throw new InvalidOperationException();
            }
            if (lat > bounds.maxLat)
            {
                throw new InvalidOperationException();
            }
            if (lon < bounds.minLon)
            {
                throw new InvalidOperationException();
            }
            if (lon > bounds.maxLon)
            {
                throw new InvalidOperationException();
            }
            var a = new GeoCoordinate(lat, lon);
            var b = new GeoCoordinate(lat, bounds.minLon);

            var dst = (float)a.GetDistanceTo(b);

            return(dst);
            //return (float)(distance(lat, bounds.minLon, lat, lon) * Math.PI);
        }
示例#2
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;
        }