管道基础大数据平台系统开发-【CS】-ExportMap
13693261870
2024-07-18 0a70f99e51b0ea0c83288213e3b1f7904d78ede0
SimuTools/Tools/GdalHelper.cs
@@ -4,9 +4,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SimuTools.Tools
{
@@ -21,6 +18,8 @@
        public static SpatialReference sr4326 { set; get; }
        public static SpatialReference sr4490 { set; get; }
        public static readonly String CGCS2000 = "CGCS2000";
        /// <summary>
        /// 构造函数
@@ -80,5 +79,147 @@
            Ogr.RegisterAll();
            Gdal.AllRegister();
        }
        /// <summary>
        /// 获取Dataset的最小点
        /// </summary>
        public static Geometry GetMinPoint(Dataset ds)
        {
            double[] transform = new double[6];
            ds.GetGeoTransform(transform);
            double xMin = transform[0];
            double yMin = transform[3] - ds.RasterYSize * transform[1];
            Geometry point = new Geometry(wkbGeometryType.wkbPoint);
            point.AddPoint(xMin, yMin, 0);
            return Transform(ds, point);
        }
        /// <summary>
        /// 获取Dataset的最大点
        /// </summary>
        public static Geometry GetMaxPoint(Dataset ds)
        {
            /*
             * transform[0] 左上角x坐标
             * transform[1] 东西方向分辨率
             * transform[2] 旋转角度, 0表示图像 "北方朝上"
             *
             * transform[3] 左上角y坐标
             * transform[4] 旋转角度, 0表示图像 "北方朝上"
             * transform[5] 南北方向分辨率
             */
            double[] transform = new double[6];
            ds.GetGeoTransform(transform);
            double xMax = transform[0] + (ds.RasterYSize * transform[1]);
            double yMax = transform[3];
            Geometry point = new Geometry(wkbGeometryType.wkbPoint);
            point.AddPoint(xMax, yMax, 0);
            return Transform(ds, point);
        }
        /// <summary>
        /// 坐标转换
        /// </summary>
        public static Geometry Transform(Dataset ds, Geometry point)
        {
            point.AssignSpatialReference(ds.GetSpatialRef());
            if (ds.GetSpatialRef().IsGeographic() > 0)
            {
                return point;
            }
            String srsName = ds.GetSpatialRef().GetName();
            if (srsName.Contains(CGCS2000))
            {
                point.TransformTo(sr4490);
            }
            else
            {
                point.TransformTo(sr4326);
            }
            point.SwapXY();
            return point;
        }
        /// <summary>
        /// 创建金字塔
        /// </summary>
        public static void CreatePyramid(String file)
        {
            Dataset ds = null;
            try
            {
                if (!File.Exists(file)) return;
                ds = Gdal.Open(file, Access.GA_ReadOnly);
                if (null == ds) return;
                OSGeo.GDAL.Band band = ds.GetRasterBand(1);
                if (0 == band.GetOverviewCount())
                {
                    ds.BuildOverviews("nearest", new int[] { 2, 4, 6, 8, 16 });
                }
            }
            catch (Exception ex)
            {
                LogOut.Error(ex.Message);
            }
            finally
            {
                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();
        }
    }
}