| | |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.IO; |
| | | using System.Linq; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | | |
| | | namespace SimuTools.Tools |
| | | { |
| | |
| | | Gdal.AllRegister(); |
| | | } |
| | | |
| | | /** |
| | | * 获取Dataset的最小点 |
| | | */ |
| | | /// <summary> |
| | | /// 获取Dataset的最小点 |
| | | /// </summary> |
| | | public static Geometry GetMinPoint(Dataset ds) |
| | | { |
| | | double[] transform = new double[6]; |
| | |
| | | return Transform(ds, point); |
| | | } |
| | | |
| | | /** |
| | | * 获取Dataset的最大点 |
| | | */ |
| | | /// <summary> |
| | | /// 获取Dataset的最大点 |
| | | /// </summary> |
| | | public static Geometry GetMaxPoint(Dataset ds) |
| | | { |
| | | /* |
| | |
| | | return Transform(ds, point); |
| | | } |
| | | |
| | | /** |
| | | * 坐标转换 |
| | | */ |
| | | /// <summary> |
| | | /// 坐标转换 |
| | | /// </summary> |
| | | public static Geometry Transform(Dataset ds, Geometry point) |
| | | { |
| | | point.AssignSpatialReference(ds.GetSpatialRef()); |
| | |
| | | return point; |
| | | } |
| | | |
| | | /** |
| | | * 创建金字塔 |
| | | */ |
| | | /// <summary> |
| | | /// 创建金字塔 |
| | | /// </summary> |
| | | public static void CreatePyramid(String file) |
| | | { |
| | | Dataset ds = null; |
| | |
| | | ds = Gdal.Open(file, Access.GA_ReadOnly); |
| | | if (null == ds) return; |
| | | |
| | | Band band = ds.GetRasterBand(1); |
| | | OSGeo.GDAL.Band band = ds.GetRasterBand(1); |
| | | if (0 == band.GetOverviewCount()) |
| | | { |
| | | ds.BuildOverviews("nearest", new int[] { 2, 4, 6, 8, 16 }); |
| | |
| | | if (null != ds) ds.Dispose(); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 创建PNG |
| | | /// </summary> |
| | | public void CreatePng(string filePath, int width, int height, int bands = 3) |
| | | { |
| | | // 创建内存驱动 |
| | | OSGeo.GDAL.Driver memDriver = Gdal.GetDriverByName("MEM"); |
| | | // 创建内存数据集 |
| | | Dataset ds = memDriver.Create("mem::", width, height, bands, DataType.GDT_Byte, null); |
| | | |
| | | // 设置图像信息 |
| | | for (int i = 1; i <= bands; i++) |
| | | { |
| | | Band band = ds.GetRasterBand(i); |
| | | band.SetRasterColorInterpretation((ColorInterp)i); |
| | | } |
| | | |
| | | // 填充内存图像 |
| | | byte[] buffer = new byte[width * height * bands]; |
| | | for (int i = 0; i < buffer.Length; i++) |
| | | { |
| | | buffer[i] = (byte)(i % 256); |
| | | } |
| | | |
| | | // 写入内存图像 |
| | | for (int i = 1; i <= bands; i++) |
| | | { |
| | | Band band = ds.GetRasterBand(i); |
| | | band.WriteRaster(0, 0, width, height, buffer, width, height, 0, 0); |
| | | } |
| | | |
| | | // 创建PNG驱动 |
| | | OSGeo.GDAL.Driver pngDriver = Gdal.GetDriverByName("PNG"); |
| | | |
| | | // 保存为PNG文件 |
| | | //ds.SetProjection(Gdal.GetProjectionRef("")); |
| | | //ds.SetGeoTransform(Gdal.GetGeoTransform("")); |
| | | pngDriver.CreateCopy(filePath, ds, 0, null, null, null); |
| | | pngDriver.Dispose(); |
| | | |
| | | // 关闭数据集 |
| | | ds.Dispose(); |
| | | memDriver.Dispose(); |
| | | } |
| | | } |
| | | } |