public async Task <RasterInfo> LoadMetadata(string path) { var info = await RasterInfo.CreateFromMapFileAsync(path); if (!PotentialTiles.Contains(info)) { PotentialTiles.Add(info); } return(info); }
/// <summary> /// Reads the metadata from a file. NOTE: this is a synchronous call. /// </summary> /// <param name="fullPath">the file to read the metadata from</param> /// <returns>the RasterInfo for that file</returns> public static RasterInfo CreateFromMapFile(string fullPath) { var rasterInfo = new RasterInfo(fullPath); using (var dataset = Gdal.OpenShared(fullPath, Access.GA_ReadOnly)) { rasterInfo.ImageSize = new Size(dataset.RasterXSize, dataset.RasterYSize); rasterInfo.NumberOfBands = dataset.RasterCount; rasterInfo.Projection = dataset.GetProjection(); rasterInfo.MapArea = CalculateMapArea(dataset); } Preprocessor pre = new Preprocessor(); foreach (int zoomLevel in Enumerable.Range(0, 5)) { pre.ProjectAndTile(rasterInfo, zoomLevel, null); } return(rasterInfo); }
public void ProjectAndTile(RasterInfo originFile, int zoomLevel, IProgress <Status> progress) { var status = new Status { Total = SupportedProjections.Length, Current = 0 }; foreach (var projection in SupportedProjections) { Size fullMapSize = projection.FullMapSizeFor(zoomLevel); var tilesWide = fullMapSize.Width / projection.TileSize.Width; var tilesTall = fullMapSize.Height / projection.TileSize.Height; status.Current++; status.Total += tilesWide * tilesTall; status.Message = $"Projecting {projection.Name} at Zoom Level {zoomLevel}"; progress?.Report(status); string baseDir = Path.Combine(projection.GetType().Name, $"{zoomLevel}"); if (!Directory.Exists(baseDir)) { Directory.CreateDirectory(baseDir); } using (var source = Gdal.OpenShared(originFile.FullPath, Access.GA_ReadOnly)) using (var destination = Gdal.AutoCreateWarpedVRT(source, source.GetProjection(), projection.Wkt, ResampleAlg.GRA_NearestNeighbour, .0125)) { var sourceInfo = DetermineRenderInformation(destination); var ratioX = destination.RasterXSize / fullMapSize.Width; var ratioY = destination.RasterYSize / fullMapSize.Height; var destWidth = (int)projection.TileSize.Width; var destHeight = (int)projection.TileSize.Height; var sourceWidth = (int)(destWidth * ratioX); var sourceHeight = (int)(destHeight * ratioY); var buffer = new byte[destWidth * destHeight * sourceInfo.ChannelCount]; var stride = destWidth * sourceInfo.ChannelCount; for (int row = 0; row < tilesWide; row++) { for (int column = 0; column < tilesTall; column++) { int x = column * sourceWidth; int y = row * sourceHeight; destination.ReadRaster(x, y, sourceWidth, sourceHeight, buffer, destWidth, destHeight, sourceInfo.ChannelCount, sourceInfo.BandMap, sourceInfo.PixelSpace, stride, 1); var bitmap = BitmapSource.Create(destWidth, destHeight, 96, 96, sourceInfo.PixelFormat, sourceInfo.Colors, buffer, stride); using (var stream = File.Create(Path.Combine(baseDir, $"{row}-{column}.png"))) { var encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bitmap)); encoder.Save(stream); } status.Current++; progress?.Report(status); } } } } status.Current = status.Total; status.Message = string.Empty; progress?.Report(status); }