static void Main(string[] args) { var features = (new GeoJsonReader()).Read <FeatureCollection>(File.ReadAllText("test.geojson")); // build the vector tile tree. var tree = new VectorTileTree(); tree.Add(features, ConfigureFeature); IEnumerable <(IFeature feature, int zoom, string layerName)> ConfigureFeature(IFeature feature) { for (var z = 12; z <= 14; z++) { if (feature.Geometry is LineString) { yield return(feature, z, "cyclenetwork"); } else if (feature.Geometry is Polygon) { yield return(feature, z, "polygons"); } else { yield return(feature, z, "cyclenodes"); } } } // write the tiles to disk as mvt. Mapbox.MapboxTileWriter.Write(tree, "tiles"); // write the tiles to disk as geojson. GeoJson.GeoJsonTileWriter.Write(tree, "tiles"); }
static void Main() { var features = (new GeoJsonReader()).Read <FeatureCollection>(File.ReadAllText("test.geojson")); // build the vector tile tree. var tree = new VectorTileTree(); tree.Add(features, ConfigureFeature);
/// <summary> /// Writes the tiles in a /z/x/y.mvt folder structure. /// </summary> /// <param name="tree">The tree.</param> /// <param name="path">The path.</param> /// <param name="extent">The extent.</param> /// <remarks>Replaces the files if they are already present.</remarks> public static void Write(this VectorTileTree tree, string path, uint extent = 4096) { IEnumerable <VectorTile> GetTiles() { foreach (var tile in tree) { yield return(tree[tile]); } } GetTiles().Write(path, extent); }
/// <summary> /// Writes the tiles in a /z/x/y.mvt folder structure. /// </summary> /// <param name="tree">The tree.</param> /// <param name="path">The path.</param> /// <param name="extent">The extent.</param> /// <remarks>Replaces the files if they are already present.</remarks> public static void Write(this VectorTileTree tree, string path, uint extent = 4096) { foreach (var tileId in tree) { var tile = new Tiles.Tile(tileId); var zFolder = Path.Combine(path, tile.Zoom.ToString()); if (!Directory.Exists(zFolder)) { Directory.CreateDirectory(zFolder); } var xFolder = Path.Combine(zFolder, tile.X.ToString()); if (!Directory.Exists(xFolder)) { Directory.CreateDirectory(xFolder); } var file = Path.Combine(xFolder, $"{tile.Y.ToString()}.mvt"); using (var stream = File.Open(file, FileMode.Create)) { tree[tileId].Write(stream, extent); } } }
/// <summary> /// Writes the tiles provided in <c>VectorTileTree</c> in a /z/x/y-{layer}.geojson folder structure. /// </summary> /// <param name="tree">The tree.</param> /// <param name="path">The path.</param> /// <remarks>Replaces the files if they are already present.</remarks> public static void Write(this VectorTileTree tree, string path) { foreach (ulong tileId in tree) { var tileData = tree[tileId]; foreach (var layer in tileData.Layers) { var tile = new Tiles.Tile(tileId); string zFolder = Path.Combine(path, tile.Zoom.ToString()); if (!Directory.Exists(zFolder)) { Directory.CreateDirectory(zFolder); } string xFolder = Path.Combine(zFolder, tile.X.ToString()); if (!Directory.Exists(xFolder)) { Directory.CreateDirectory(xFolder); } string file = Path.Combine(xFolder, $"{tile.Y}-{layer.Name}.geojson"); using var stream = File.Open(file, FileMode.Create); layer.Write(stream); } } }
public void Run() { var areas = _dbContext.Areas.Where(x => x.ParentAreaId == null) .Include(x => x.AreaAttributes).ToList(); IEnumerable <(IFeature feature, int zoom, string layerName)> ConfigureFeature(IFeature feature) { if (feature.Attributes == null) { yield break; } if (!feature.Attributes.Exists("admin_level")) { yield break; } var adminLevelValue = feature.Attributes["admin_level"]; if (!(adminLevelValue is string adminLevelString)) { yield break; } if (!long.TryParse(adminLevelString, NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out var adminLevel)) { yield break; } if (adminLevel == 2) { // country level. for (var z = 0; z <= 7; z++) { yield return(feature, z, "areas"); } yield break; } if (adminLevel > 2 && adminLevel <= 4) { // regional level. for (var z = 7; z <= 10; z++) { yield return(feature, z, "areas"); } yield break; } for (var z = 10; z <= 14; z++) { yield return(feature, z, "areas"); } } var vectorTileTree = new VectorTileTree(); var queue = new Queue <int>(); while (true) { foreach (var area in areas) { var areaFeature = ToFeature(area); var features = new FeatureCollection { areaFeature }; vectorTileTree.Add(features, ConfigureFeature); queue.Enqueue(area.AreaId); } if (queue.Count == 0) { break; } var parentId = queue.Dequeue(); areas = _dbContext.Areas.Where(x => x.ParentAreaId == parentId) .Include(x => x.AreaAttributes).ToList(); } vectorTileTree.Write(_configuration.OutputPath); }
public static IEnumerable <VectorTile> Tiles(this VectorTileTree tree) { return(tree.Select(id => tree[id])); }