internal static void AddTileFor(this TiledBarrierGraph graph, int vertex, Func <uint, IEnumerable <OsmGeo> > getTile, Func <TagsCollectionBase, bool> isBarrier) { var vLocation = graph.GetVertex(vertex); var t = TileStatic.WorldTileLocalId(vLocation.longitude, vLocation.latitude, graph.Zoom); graph.AddTiles(new[] { t }, getTile, isBarrier); }
static void Main(string[] args) { var logFile = Path.Combine("logs", "log-{Date}.txt"); Log.Logger = new LoggerConfiguration() #if DEBUG .MinimumLevel.Debug() #else .MinimumLevel.Information() #endif .Enrich.FromLogContext() .WriteTo.RollingFile(new JsonFormatter(), logFile) .WriteTo.Console() .CreateLogger(); var cacheFolder = "/media/xivk/2T-SSD-EXT/temp"; var tileUrl = "https://data1.anyways.eu/tiles/full/20200628-150902/14/{x}/{y}.osm"; var osmTileSource = new OsmTileSource(tileUrl, cacheFolder); bool IsBarrier(TagsCollectionBase?tags) { if (tags == null) { return(false); } return(DefaultMergeFactorCalculator.Barriers.TryCalculateValue(tags, out _)); } var wechelderzande1 = (4.801913201808929, 51.26797859372288); var wechelderzande2 = (4.774868488311768, 51.267366046233136); var wechelderzande3 = (4.774868488311768, 51.267366046233136); var wechelderzande4 = (4.774868488311768, 51.267366046233136); var staden = (3.0198, 50.9743); var leyton = (-0.00303, 51.56436); var lille = (4.82594, 51.24203); var lilleLinksIndustrie = (4.803589582443237, 51.2536864893987); var lilleIndustrie = (4.815917015075683, 51.248807861598635); var lilleZagerijstraat = (4.8164963722229, 51.233426555935694); var vorselaarSassenhout = (4.807709455490112, 51.21146402264062); var vorselaarBeek = (4.7949743270874015, 51.204624839889235); var tile1 = TileStatic.WorldTileLocalId(wechelderzande1, 14); var tile2 = TileStatic.WorldTileLocalId(wechelderzande2, 14); var tile = TileStatic.WorldTileLocalId(vorselaarBeek, 14); var graph = LoadForTileTest.Default.RunPerformance((tile, osmTileSource, IsBarrier), 1); var result = AssignFaceTest.Default.RunPerformance((graph, tile)); File.WriteAllText("barriers.geojson", graph.ToFeatures().ToFeatureCollection().ToGeoJson()); return; while (!result.success) { // extra tiles need loading. AddTilesTest.Default.RunPerformance((graph, result.missingTiles, osmTileSource, IsBarrier)); // try again. result = AssignFaceTest.Default.RunPerformance((graph, tile)); } // assign landuse. // // var landuseFeatures = NTSExtensions.FromGeoJson(File.ReadAllText("test.geojson")); // // IEnumerable<(Polygon polygon, string type)> GetLanduse(((double longitude, double latitude) topLeft, (double longitude, double latitude) bottomRight) box) // { // return new (Polygon polygon, string type)[] { (landuseFeatures.First().Geometry as Polygon, "residential") }; // } IEnumerable <(Polygon polygon, string type)> GetLanduse( ((double longitude, double latitude)topLeft, (double longitude, double latitude)bottomRight) box) { return(LandusePolygons.GetLandusePolygons(box, graph.Zoom, osmTileSource.GetTile, t => { if (DefaultMergeFactorCalculator.Landuses.TryCalculateValue(t, out var type)) { return type; } return null; })); } graph.AssignLanduse(tile, GetLanduse); File.WriteAllText("barriers.geojson", graph.ToFeatures().ToFeatureCollection().ToGeoJson()); var outerBox = graph.OuterBox(tile); // get all landuse polygon in the larger box. var landuse = GetLanduse(outerBox).ToList(); File.WriteAllText("landuse.geojson", landuse.Select(x => new Feature(x.polygon, new AttributesTable { { "type", x.type } })).ToFeatureCollection().ToGeoJson()); // var tile = TileStatic.ToLocalId(8411,5466, 14); // // // load data for tile. // var graph = new TiledBarrierGraph(); // graph.LoadForTile(tile, osmTileSource.GetTile, IsBarrier); // // // run face assignment for the tile. // var result = graph.AssignFaces(tile); // while (!result.success) // { // // extra tiles need loading.- // graph.AddTiles(result.missingTiles, osmTileSource.GetTile, IsBarrier); // // // try again. // result = graph.AssignFaces(tile); // } // // File.WriteAllText("barriers.geojson", graph.ToFeatures().ToFeatureCollection().ToGeoJson()); // // var landuse = NTSExtensions.FromGeoJson(File.ReadAllText("test.geojson")); // // IEnumerable<(Polygon polygon, string type)> GetLanduse(((double longitude, double latitude) topLeft, (double longitude, double latitude) bottomRight) box) // { // return new (Polygon polygon, string type)[] { (landuse.First().Geometry as Polygon, "residential") }; // } // graph.AssignLanduse(tile, GetLanduse); // // File.WriteAllText("barriers.geojson", graph.ToFeatures().ToFeatureCollection().ToGeoJson()); // // TiledBarrierGraphBuilder.BuildForTile(tile, cacheFolder, osmTileSource.GetTile, IsBarrier); // //TiledBarrierGraphBuilder.BuildForTile(tile2, "cache", GetTile, IsBarrier); // // var polygonGraph = new TiledPolygonGraph(); // polygonGraph.AddTileFromStream(tile, // new GZipStream(File.OpenRead(Path.Combine(cacheFolder, $"{tile1}.tile.graph.zip")), // CompressionMode.Decompress)); // // polygonGraph.AddTileFromStream(tile2, // // new GZipStream(File.OpenRead(Path.Combine("cache", $"{tile2}.tile.graph.zip")), // // CompressionMode.Decompress)); // // File.WriteAllText("barriers.geojson", polygonGraph.ToFeatures().ToFeatureCollection().ToGeoJson()); }
private static IEnumerable <uint> Add(this TiledBarrierGraph graph, IEnumerable <OsmGeo> osmGeos, Func <TagsCollectionBase, bool> isBarrier) { var uncoveredTiles = new HashSet <uint>(); // collect all nodes with more than one barrier way. var nodes = new Dictionary <long, (double longitude, double latitude)?>(); var vertexNodes = new Dictionary <long, int>(); foreach (var osmGeo in osmGeos) { if (!(osmGeo is Way way)) { continue; } if (way.Nodes == null) { continue; } if (!isBarrier(way.Tags)) { continue; } for (var n = 0; n < way.Nodes.Length; n++) { var nodeId = way.Nodes[n]; if (graph.TryGetVertex(nodeId, out var vertex)) { // node already there as a vertex. vertexNodes[nodeId] = vertex; } else { // not yet a vertex. // keep first, last and reused nodes are intersections. if (n == 0 || n == way.Nodes.Length - 1 || nodes.ContainsKey(nodeId)) { vertexNodes[nodeId] = int.MaxValue; } } nodes[nodeId] = null; } } // add all vertices new vertices and store node locations. using var enumerator = osmGeos.GetEnumerator(); var hasNext = true; while (hasNext) { hasNext = enumerator.MoveNext(); if (!hasNext) { break; } if (!(enumerator.Current is Node node)) { break; } if (node.Id == null || node.Latitude == null || node.Longitude == null) { continue; } if (graph.TryGetVertex(node.Id.Value, out _)) { continue; } if (!nodes.ContainsKey(node.Id.Value)) { continue; // not part of a barrier way. } nodes[node.Id.Value] = (node.Longitude.Value, node.Latitude.Value); var tile = TileStatic.WorldTileLocalId(node.Longitude.Value, node.Latitude.Value, graph.Zoom); if (!vertexNodes.ContainsKey(node.Id.Value) && graph.HasTile(tile)) { continue; // node is not a vertex and inside a loaded tile. } var vertex = graph.AddVertex(node.Longitude.Value, node.Latitude.Value, node.Id.Value); vertexNodes[node.Id.Value] = vertex; if (!graph.HasTile(tile)) { uncoveredTiles.Add(tile); } } // add all edges. var shape = new List <(double longitude, double latitude)>(); while (hasNext) { if (!hasNext) { break; } if (!(enumerator.Current is Way way)) { break; } if (way.Nodes == null || way.Tags == null || way.Id == null) { hasNext = enumerator.MoveNext(); continue; } if (!isBarrier(way.Tags)) { hasNext = enumerator.MoveNext(); continue; } if (graph.HasWay(way.Id.Value)) { hasNext = enumerator.MoveNext(); continue; } // way is a barrier, add it as one or more edges. shape.Clear(); var vertex1 = int.MaxValue; foreach (var node in way.Nodes) { if (!vertexNodes.TryGetValue(node, out var vertex)) { if (!nodes.TryGetValue(node, out var nodeLocation)) { throw new InvalidDataException( $"Node {node} in way {way.Id} not found!"); } if (nodeLocation == null) { OsmSharp.Logging.Logger.Log(nameof(TiledBarrierGraphBuilder), TraceEventType.Warning, $"Node location for node {node} in way {way.Id} not found!"); } else { shape.Add(nodeLocation.Value); } continue; } else if (vertex == int.MaxValue) { OsmSharp.Logging.Logger.Log(nameof(TiledBarrierGraphBuilder), TraceEventType.Warning, $"Node {node} in way {way.Id} not found in tile!"); continue; } if (vertex1 == int.MaxValue) { vertex1 = vertex; continue; } graph.AddEdgeFlattened(vertex1, vertex, shape, way.Tags, way.Id.Value); vertex1 = vertex; shape.Clear(); } hasNext = enumerator.MoveNext(); } return(uncoveredTiles); }
internal static uint TileFor(this TiledBarrierGraph graph, int vertex) { var l = graph.GetVertex(vertex); return(TileStatic.WorldTileLocalId(l.longitude, l.latitude, graph.Zoom)); }