管道基础大数据平台系统开发-【CS】-ExportMap
1
13693261870
2024-07-23 379a98780e346a793c5dae062efae417ade0117d
SimuTools/Tools/Handle.cs
@@ -9,18 +9,21 @@
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SimuTools.Tools
{
    public class Handle
    {
        #region 成员变量
        public static readonly int MAX = 256 * 256;
        public static readonly string GdalPath = ConfigurationManager.AppSettings["gdalPath"];
        public static readonly double WaterHeightOffset = 1.0;
        public static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory;
        public static readonly string GdalPath = ConfigurationManager.AppSettings["gdalPath"];
        #endregion
        /// <summary>
        /// 运行
@@ -71,12 +74,28 @@
        {
            Geometry minPoint = GdalHelper.GetMinPoint(ds);
            Geometry maxPoint = GdalHelper.GetMaxPoint(ds);
            layer.extension = new Extension(minPoint.GetX(0), minPoint.GetY(0), maxPoint.GetX(0), maxPoint.GetY(0));
            layer.extension = new Extension(minPoint.GetX(0), minPoint.GetY(0), maxPoint.GetX(0), maxPoint.GetY(0), double.MaxValue, double.MinValue);
            OSGeo.GDAL.Band band = ds.GetRasterBand(1);
            Band band = ds.GetRasterBand(1);
            double[] mm = new double[2];
            band.ComputeRasterMinMax(mm, 0);
            layer.extension.SetHeight(mm[0], mm[1]);
            layer.terrain.SetHeight(GetMinVal(mm[0]), GetMaxVal(mm[1]));
        }
        /// <summary>
        /// 获取最小值
        /// </summary>
        private static double GetMinVal(double val)
        {
            return Convert.ToInt32(Math.Floor(val * 1000)) / 1000.0;
        }
        /// <summary>
        /// 获取最大值
        /// </summary>
        private static double GetMaxVal(double val)
        {
            return Convert.ToInt32(Math.Ceiling(val * 1000)) / 1000.0;
        }
        /// <summary>
@@ -93,21 +112,18 @@
            {
                string tif = Path.Combine(tempPath, DateTime.Now.Ticks.ToString() + ".tif");
                //Resample(ds.GetDescription(), tif, sizes[0], sizes[1]);
                Resample(ds, tif, sizes[0], sizes[1]);
                if (!File.Exists(tif))
                {
                    continue;
                }
                Resample(ds, tif, sizes[0], sizes[1], layer);
                if (!File.Exists(tif)) continue;
                string png = Path.Combine(terrainPath, sizes[0] + "_" + sizes[1] + ".png");
                Tif2Png(layer, tif, png, sizes[0], sizes[1]);
                Terrain2Png(layer, tif, png, sizes[0], sizes[1]);
            }
        }
        /// <summary>
        /// 重采样
        /// </summary>
        private static void Resample(Dataset ds, string dest, int width, int height)
        private static void Resample(Dataset ds, string dest, int width, int height, Domain.Layer layer)
        {
            // https://blog.51cto.com/u_16099346/6691820
            //string args = string.Format("-t_srs {0} -ts {1} {2} -r {3} -of {4}", "EPSG:4326", width, height, "bilinear", "GTiff");
@@ -119,6 +135,11 @@
                "-ts",
                width.ToString(),
                height.ToString(),
                "-te",
                layer.extension.minx.ToString(),
                layer.extension.miny.ToString(),
                layer.extension.maxx.ToString(),
                layer.extension.maxy.ToString(),
                "-r",
                "bilinear",
                "-of",
@@ -130,6 +151,410 @@
            destDs.Dispose();
        }
        /// <summary>
        /// 地形转PNG
        /// </summary>
        private static void Terrain2Png(Domain.Layer layer, string tif, string png, int width, int height)
        {
            Dataset ds = null;
            try
            {
                ds = Gdal.Open(tif, Access.GA_ReadOnly);
                if (null == ds || 0 == ds.RasterCount) return;
                OSGeo.GDAL.Band band = ds.GetRasterBand(1);
                float[] buffer = new float[width * height];
                band.ReadRaster(0, 0, width, height, buffer, width, height, 0, 0);
                Bitmap image = new Bitmap(width, height);
                Graphics graphic = Graphics.FromImage(image);
                graphic.Clear(Color.Transparent); // 填充透明色
                double perHeight = (layer.terrain.maxHeight - layer.terrain.minHeight) * 100 / 65536;
                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        int offset = x + y * width;
                        if (float.IsNaN(buffer[offset]) || buffer[offset] < -999) continue;
                        //int val = Convert.ToInt32(buffer[offset] * 100);
                        int val = Convert.ToInt32((buffer[offset] - layer.terrain.minHeight) * 100 / perHeight);
                        int r = val / 65536;
                        int g = (val - r * 65536) / 256;
                        int b = val % 256;
                        Color color = Color.FromArgb(127, r, g, b);
                        image.SetPixel(x, y, color);
                    }
                }
                image.Save(png, System.Drawing.Imaging.ImageFormat.Png);
            }
            finally
            {
                if (null != ds) ds.Dispose();
                if (File.Exists(tif)) File.Delete(tif);
            }
        }
        #endregion
        #region 水面
        /// <summary>
        /// 处理水面
        /// </summary>
        private static void CopeWater(string waterPath, string outPath, Domain.Layer layer)
        {
            List<string> files = GetFiles(waterPath);
            if (null == files || files.Count == 0) return;
            SetWaterData(layer, files);
            SetWaterHeight(layer, files);
            if (files.Count != layer.waters.data.Count) return;
            ProcessWaters(files, outPath, layer);
        }
        /// <summary>
        /// 获取文件
        /// </summary>
        private static List<String> GetFiles(string path, string prefix = "")
        {
            string[] files = Directory.GetFiles(path, prefix + "*.tif?", SearchOption.TopDirectoryOnly);
            if (null == files || files.Length == 0) return null;
            List<string> list = files.ToList();
            list.Sort();
            return list;
        }
        /// <summary>
        /// 设置水面数据
        /// </summary>
        private static void SetWaterData(Domain.Layer layer, List<string> files)
        {
            DateTime now = DateTime.Now;
            long startTicks = new DateTime(1970, 1, 1, 8, 0, 0).Ticks;
            foreach (string file in files)
            {
                string fileName = Path.GetFileNameWithoutExtension(file);
                int hour = Convert.ToInt32(fileName.Substring(0, 2));
                int minute = Convert.ToInt32(fileName.Substring(2, 2));
                int second = Convert.ToInt32(fileName.Substring(4, 2));
                DateTime d = new DateTime(now.Year, now.Month, now.Day, hour, minute, second);
                layer.waters.data.Add((d.Ticks - startTicks) / 10000);
            }
        }
        /// <summary>
        /// 设置水面高度
        /// </summary>
        private static void SetWaterHeight(Domain.Layer layer, List<string> files)
        {
            Parallel.For(0, files.Count, i =>
            {
                Dataset ds = null;
                try
                {
                    ds = Gdal.Open(files[i], Access.GA_ReadOnly);
                    if (null == ds || 0 == ds.RasterCount || null == ds.GetSpatialRef()) return;
                    double[] mm = new double[2];
                    ds.GetRasterBand(1).ComputeRasterMinMax(mm, 0);
                    layer.extension.SetHeight(mm[0], mm[1]);
                }
                finally
                {
                    if (null != ds) ds.Dispose();
                }
            });
            layer.extension.minHeight = GetMinVal(layer.extension.minHeight);
            layer.extension.maxHeight = GetMaxVal(layer.extension.maxHeight + WaterHeightOffset);
        }
        /// <summary>
        /// 处理水面数据
        /// </summary>
        private static void ProcessWaters(List<string> files, string outPath, Domain.Layer layer)
        {
            Parallel.For(0, files.Count, i =>
            //for (int i = 0; i < files.Count; i++)
            {
                Dataset ds = null;
                try
                {
                    ds = Gdal.Open(files[i], Access.GA_ReadOnly);
                    if (null == ds || 0 == ds.RasterCount || null == ds.GetSpatialRef()) return;
                    CreateWaterPng(ds, layer, outPath, layer.waters.data[i]);
                }
                finally
                {
                    if (null != ds) ds.Dispose();
                }
            });
        }
        /// <summary>
        /// 创建水面PNG
        /// </summary>
        private static void CreateWaterPng(Dataset ds, Domain.Layer layer, string outPath, long ticks)
        {
            string tempPath = Path.Combine(outPath, "temp");
            if (!Directory.Exists(tempPath)) Directory.CreateDirectory(tempPath);
            string waterPath = Path.Combine(outPath, "waters", ticks.ToString());
            if (!Directory.Exists(waterPath)) Directory.CreateDirectory(waterPath);
            foreach (int[] sizes in layer.terrain.size)
            {
                string fileName = Path.GetFileNameWithoutExtension(ds.GetDescription()) + "_" + sizes[0] + "_" + sizes[1];
                string tif = Path.Combine(tempPath, fileName + ".tif");
                Resample(ds, tif, sizes[0], sizes[1], layer);
                if (!File.Exists(tif)) continue;
                string png = Path.Combine(waterPath, sizes[0] + "_" + sizes[1] + ".png");
                Water2Png(layer, tif, png, sizes[0], sizes[1]);
            }
        }
        /// <summary>
        /// 水面转PNG
        /// </summary>
        private static void Water2Png(Domain.Layer layer, string tif, string png, int width, int height)
        {
            Dataset ds = null;
            try
            {
                ds = Gdal.Open(tif, Access.GA_ReadOnly);
                if (null == ds || 0 == ds.RasterCount) return;
                OSGeo.GDAL.Band band = ds.GetRasterBand(1);
                float[] buffer = new float[width * height];
                band.ReadRaster(0, 0, width, height, buffer, width, height, 0, 0);
                Bitmap image = new Bitmap(width, height);
                Graphics graphic = Graphics.FromImage(image);
                graphic.Clear(Color.Transparent); // 填充透明色
                double perHeight = (layer.extension.maxHeight - layer.extension.minHeight) * 100 / 65536;
                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        int offset = x + y * width;
                        if (float.IsNaN(buffer[offset]) || buffer[offset] < -999) continue;
                        //int val = Convert.ToInt32(buffer[offset] * 100);
                        int val = Convert.ToInt32((buffer[offset] - layer.extension.minHeight + WaterHeightOffset) * 100 / perHeight);
                        int r = val / 65536;
                        int g = (val - r * 65536) / 256;
                        int b = val % 256;
                        Color color = Color.FromArgb(127, r, g, b);
                        image.SetPixel(x, y, color);
                    }
                }
                image.Save(png, System.Drawing.Imaging.ImageFormat.Png);
            }
            finally
            {
                if (null != ds) ds.Dispose();
                if (File.Exists(tif)) File.Delete(tif);
            }
        }
        #endregion
        #region 流速流向
        /// <summary>
        /// 处理流速流向
        /// </summary>
        private static void CopeFlow(string flowPath, string outPath, Domain.Layer layer)
        {
            List<string> vxFiles = GetFiles(flowPath, "vx");
            List<string> vyFiles = GetFiles(flowPath, "vy");
            if (null == vxFiles || null == vyFiles || vxFiles.Count != vyFiles.Count || vxFiles.Count != layer.waters.data.Count) return;
            Parallel.For(0, vxFiles.Count, i =>
            //for (int i = 0; i < vxFiles.Count; i++)
            {
                Dataset vxDs = null, vyDs = null;
                try
                {
                    vxDs = Gdal.Open(vxFiles[i], Access.GA_ReadOnly);
                    vyDs = Gdal.Open(vyFiles[i], Access.GA_ReadOnly);
                    if (null == vxDs || 0 == vxDs.RasterCount || null == vxDs.GetSpatialRef() ||
                        null == vyDs || 0 == vyDs.RasterCount || null == vyDs.GetSpatialRef()) return;
                    CreateFlowPng(vxDs, vyDs, layer, outPath, layer.waters.data[i]);
                }
                finally
                {
                    if (null != vxDs) vxDs.Dispose();
                    if (null != vyDs) vyDs.Dispose();
                }
            });
        }
        /// <summary>
        /// 创建流速流向PNG
        /// </summary>
        private static void CreateFlowPng(Dataset vxDs, Dataset vyDs, Domain.Layer layer, string outPath, long ticks)
        {
            string tempPath = Path.Combine(outPath, "temp");
            if (!Directory.Exists(tempPath)) Directory.CreateDirectory(tempPath);
            string flowPath = Path.Combine(outPath, "flows", ticks.ToString());
            if (!Directory.Exists(flowPath)) Directory.CreateDirectory(flowPath);
            foreach (int[] sizes in layer.terrain.size)
            {
                string vxName = Path.GetFileNameWithoutExtension(vxDs.GetDescription()) + "_" + sizes[0] + "_" + sizes[1];
                string vxTif = Path.Combine(tempPath, vxName + ".tif");
                Resample(vxDs, vxTif, sizes[0], sizes[1], layer);
                string vyName = Path.GetFileNameWithoutExtension(vyDs.GetDescription()) + "_" + sizes[0] + "_" + sizes[1];
                string vyTif = Path.Combine(tempPath, vyName + ".tif");
                Resample(vyDs, vyTif, sizes[0], sizes[1], layer);
                if (!File.Exists(vxTif) || !File.Exists(vyTif)) continue;
                string png = Path.Combine(flowPath, sizes[0] + "_" + sizes[1] + ".png");
                VxyTif2Png(layer, vxTif, vyTif, png, sizes[0], sizes[1]);
            }
        }
        /// <summary>
        /// 流速流向Tif值转存至Png
        /// </summary>
        private static void VxyTif2Png(Domain.Layer layer, string vxTif, string vyTif, string png, int width, int height)
        {
            Dataset vxDs = null, vyDs = null;
            try
            {
                vxDs = Gdal.Open(vxTif, Access.GA_ReadOnly);
                vyDs = Gdal.Open(vyTif, Access.GA_ReadOnly);
                if (null == vxDs || 0 == vxDs.RasterCount || null == vyDs || 0 == vyDs.RasterCount) return;
                float[] vxBuffer = new float[width * height], vyBuffer = new float[width * height];
                vxDs.GetRasterBand(1).ReadRaster(0, 0, width, height, vxBuffer, width, height, 0, 0);
                vyDs.GetRasterBand(1).ReadRaster(0, 0, width, height, vyBuffer, width, height, 0, 0);
                double[] vxMm = new double[2];
                vxDs.GetRasterBand(1).ComputeRasterMinMax(vxMm, 0);
                double[] vyMm = new double[2];
                vyDs.GetRasterBand(1).ComputeRasterMinMax(vyMm, 0);
                double min = vxMm[0] < vyMm[0] ? vxMm[0] : vyMm[0];
                double max = vxMm[1] > vyMm[1] ? vxMm[1] : vyMm[1];
                double perHeight = (max - min) / 255;
                CreateFlowPng(vxBuffer, vyBuffer, min, perHeight, png, width, height);
            }
            finally
            {
                if (null != vxDs) vxDs.Dispose();
                if (null != vyDs) vyDs.Dispose();
                if (File.Exists(vxTif)) File.Delete(vxTif);
                if (File.Exists(vyTif)) File.Delete(vyTif);
            }
        }
        /// <summary>
        /// 创建流速流向PNG
        /// </summary>
        private static void CreateFlowPng(float[] vxBuffer, float[] vyBuffer, double minHeight, double perHeight, string png, int width, int height)
        {
            Bitmap image = new Bitmap(width, height);
            Graphics graphic = Graphics.FromImage(image);
            graphic.Clear(Color.Transparent); // 填充透明色
            // 用 R通道表示,流向为归一化的二维向量(x,y),G通道表示为 x *255 , B通道表示为 y * 255
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    int offset = x + y * width;
                    float fx = GetFloatValue(vxBuffer[offset]);
                    float fy = GetFloatValue(vyBuffer[offset]);
                    double rv = GetRVal(fx, fy);
                    if (double.IsNaN(rv) || rv == 0) continue;
                    double dv = GetXyValue(fx, fy, rv);
                    int r = Convert.ToInt32(dv == 0 ? 0 : (dv * 0.5 + 0.5) * 255);
                    int g = float.IsNaN(fx) ? 0 : Convert.ToInt32((fx - minHeight) / perHeight);
                    int b = float.IsNaN(fy) ? 0 : Convert.ToInt32((fy - minHeight) / perHeight);
                    if (r < 0 || r > 255) r = 255;
                    if (g < 0 || g > 255) g = 255;
                    if (b < 0 || b > 255) b = 255;
                    Color color = Color.FromArgb(127, r, g, b);
                    image.SetPixel(x, y, color);
                }
            }
            image.Save(png, System.Drawing.Imaging.ImageFormat.Png);
        }
        /// <summary>
        /// 获取Float值
        /// </summary>
        private static float GetFloatValue(float val)
        {
            return (float.IsNaN(val) || val < -999) ? float.NaN : val;
        }
        /// <summary>
        /// 获取R值
        /// </summary>
        private static double GetRVal(float fx, float fy)
        {
            if (float.IsNaN(fx) && float.IsNaN(fy)) return double.NaN;
            if (float.IsNaN(fx)) return Math.Abs(fy);
            if (float.IsNaN(fy)) return Math.Abs(fx);
            return Math.Sqrt(Math.Pow(fx, 2) + Math.Pow(fy, 2));
        }
        /// <summary>
        /// 获取XY值
        /// </summary>
        private static double GetXyValue(float fx, float fy, double rv)
        {
            if (float.IsNaN(fx))
            {
                return fy / rv;
            }
            if (float.IsNaN(fy))
            {
                return fx / rv;
            }
            double dv = fx / rv + fy / rv;
            return dv < 0 ? -Math.Sqrt(-dv) : Math.Sqrt(dv);
        }
        #endregion
        #region 元数据
        /// <summary>
        /// 处理元数据
        /// </summary>
        private static void CopeLayerJson(string outPath, Domain.Layer layer)
        {
            if (null == layer) return;
            String json = JsonConvert.SerializeObject(layer);
            string filePath = Path.Combine(outPath, "layer.json");
            using (StreamWriter sw = new StreamWriter(filePath))
            {
                sw.Write(json);
            }
        }
        #endregion
        #region 暂时不用
        /// <summary>
        /// 重采样 *
        /// </summary>
@@ -179,300 +604,6 @@
                if (p != null) p.Close();
            }
            return str;
        }
        /// <summary>
        /// Tif值转存至Png
        /// </summary>
        private static void Tif2Png(Domain.Layer layer, string tif, string png, int width, int height, int heightOffset = 0)
        {
            Dataset ds = null;
            try
            {
                ds = Gdal.Open(tif, Access.GA_ReadOnly);
                if (null == ds || 0 == ds.RasterCount) return;
                OSGeo.GDAL.Band band = ds.GetRasterBand(1);
                float[] buffer = new float[width * height];
                band.ReadRaster(0, 0, width, height, buffer, width, height, 0, 0);
                Bitmap image = new Bitmap(width, height);
                Graphics graphic = Graphics.FromImage(image);
                graphic.Clear(Color.Transparent); // 填充透明色
                double perHeight = (layer.extension.maxHeight - layer.extension.minHeight + heightOffset) * 100 / 65536;
                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        int offset = x + y * width;
                        if (float.IsNaN(buffer[offset]) || buffer[offset] < -999) continue;
                        //int val = Convert.ToInt32(buffer[offset] * 100);
                        int val = Convert.ToInt32((buffer[offset] - layer.extension.minHeight) * 100 / perHeight);
                        int r = val / 65536;
                        int g = (val - r * 65536) / 256;
                        int b = val % 256;
                        Color color = Color.FromArgb(127, r, g, b);
                        image.SetPixel(x, y, color);
                    }
                }
                image.Save(png, System.Drawing.Imaging.ImageFormat.Png);
            }
            finally
            {
                if (null != ds) ds.Dispose();
                if (File.Exists(tif)) File.Delete(tif);
            }
        }
        #endregion
        #region 水面
        /// <summary>
        /// 处理水面
        /// </summary>
        private static void CopeWater(string waterPath, string outPath, Domain.Layer layer)
        {
            List<string> files = GetFiles(waterPath);
            if (null == files || files.Count == 0) return;
            SetWaterData(layer, files);
            if (files.Count != layer.waters.data.Count) return;
            ProcessWaters(files, outPath, layer);
        }
        /// <summary>
        /// 处理水面数据
        /// </summary>
        private static void ProcessWaters(List<string> files, string outPath, Domain.Layer layer)
        {
            /*for (int i = 0, c = files.Count; i < c; i++)
            {
                Dataset ds = null;
                try
                {
                    ds = Gdal.Open(files[i], Access.GA_ReadOnly);
                    if (null == ds || 0 == ds.RasterCount || null == ds.GetSpatialRef()) continue;
                    CreateWaterPng(ds, layer, outPath, layer.waters.data[i]);
                }
                finally
                {
                    if (null != ds) ds.Dispose();
                }
            }*/
            Parallel.For(0, files.Count, i =>
            {
                Dataset ds = null;
                try
                {
                    ds = Gdal.Open(files[i], Access.GA_ReadOnly);
                    if (null == ds || 0 == ds.RasterCount || null == ds.GetSpatialRef()) return;
                    CreateWaterPng(ds, layer, outPath, layer.waters.data[i]);
                }
                finally
                {
                    if (null != ds) ds.Dispose();
                }
            });
        }
        /// <summary>
        /// 创建水面PNG
        /// </summary>
        private static void CreateWaterPng(Dataset ds, Domain.Layer layer, string outPath, long ticks)
        {
            string tempPath = Path.Combine(outPath, "temp");
            if (!Directory.Exists(tempPath)) Directory.CreateDirectory(tempPath);
            string waterPath = Path.Combine(outPath, "waters", ticks.ToString());
            if (!Directory.Exists(waterPath)) Directory.CreateDirectory(waterPath);
            foreach (int[] sizes in layer.terrain.size)
            {
                string fileName = Path.GetFileNameWithoutExtension(ds.GetDescription()) + "_" + sizes[0] + "_" + sizes[1];
                string tif = Path.Combine(tempPath, fileName + ".tif");
                Resample(ds.GetDescription(), tif, sizes[0], sizes[1]);
                if (!File.Exists(tif))
                {
                    continue;
                }
                string png = Path.Combine(waterPath, sizes[0] + "_" + sizes[1] + ".png");
                Tif2Png(layer, tif, png, sizes[0], sizes[1], 1);
            }
        }
        /// <summary>
        /// 获取文件
        /// </summary>
        private static List<String> GetFiles(string path, string prefix = "")
        {
            string[] files = Directory.GetFiles(path, prefix + "*.tif?", SearchOption.TopDirectoryOnly);
            if (null == files || files.Length == 0) return null;
            List<string> list = files.ToList();
            list.Sort();
            return list;
        }
        /// <summary>
        /// 设置水面数据
        /// </summary>
        private static void SetWaterData(Domain.Layer layer, List<string> files)
        {
            DateTime now = DateTime.Now;
            long startTicks = new DateTime(1970, 1, 1, 8, 0, 0).Ticks;
            foreach (string file in files)
            {
                string fileName = Path.GetFileNameWithoutExtension(file);
                int hour = Convert.ToInt32(fileName.Substring(0, 2));
                int minute = Convert.ToInt32(fileName.Substring(2, 2));
                int second = Convert.ToInt32(fileName.Substring(4, 2));
                DateTime d = new DateTime(now.Year, now.Month, now.Day, hour, minute, second);
                layer.waters.data.Add((d.Ticks - startTicks) / 10000);
            }
        }
        #endregion
        #region 流速流向
        /// <summary>
        /// 处理流速流向
        /// </summary>
        private static void CopeFlow(string flowPath, string outPath, Domain.Layer layer)
        {
            List<string> vxFiles = GetFiles(flowPath, "vx");
            List<string> vyFiles = GetFiles(flowPath, "vy");
            if (null == vxFiles || null == vyFiles || vxFiles.Count != vyFiles.Count || vxFiles.Count != layer.waters.data.Count) return;
            Parallel.For(0, vxFiles.Count, i =>
            {
                Dataset vxDs = null, vyDs = null;
                try
                {
                    vxDs = Gdal.Open(vxFiles[i], Access.GA_ReadOnly);
                    if (null == vxDs || 0 == vxDs.RasterCount || null == vxDs.GetSpatialRef() ||
                        null == vyDs || 0 == vyDs.RasterCount || null == vyDs.GetSpatialRef()) return;
                    CreateFlowPng(vxDs, vyDs, layer, outPath, layer.waters.data[i]);
                }
                finally
                {
                    if (null != vxDs) vxDs.Dispose();
                    if (null != vyDs) vyDs.Dispose();
                }
            });
        }
        /// <summary>
        /// 创建流速流向PNG
        /// </summary>
        private static void CreateFlowPng(Dataset vxDs, Dataset vyDs, Domain.Layer layer, string outPath, long ticks)
        {
            string tempPath = Path.Combine(outPath, "temp");
            if (!Directory.Exists(tempPath)) Directory.CreateDirectory(tempPath);
            string flowPath = Path.Combine(outPath, "flows", ticks.ToString());
            if (!Directory.Exists(flowPath)) Directory.CreateDirectory(flowPath);
            foreach (int[] sizes in layer.terrain.size)
            {
                string vxName = Path.GetFileNameWithoutExtension(vxDs.GetDescription()) + "_" + sizes[0] + "_" + sizes[1];
                string vxTif = Path.Combine(tempPath, vxName + ".tif");
                Resample(vxDs.GetDescription(), vxTif, sizes[0], sizes[1]);
                string vyName = Path.GetFileNameWithoutExtension(vyDs.GetDescription()) + "_" + sizes[0] + "_" + sizes[1];
                string vyTif = Path.Combine(tempPath, vyName + ".tif");
                Resample(vyDs.GetDescription(), vyTif, sizes[0], sizes[1]);
                if (!File.Exists(vxTif) || !File.Exists(vyTif)) continue;
                string png = Path.Combine(flowPath, sizes[0] + "_" + sizes[1] + ".png");
                VxyTif2Png(layer, vxTif, vyTif, png, sizes[0], sizes[1]);
            }
        }
        /// <summary>
        /// 流速流向Tif值转存至Png
        /// </summary>
        private static void VxyTif2Png(Domain.Layer layer, string vxTif, string vyTif, string png, int width, int height)
        {
            Dataset vxDs = null, vyDs = null;
            try
            {
                vxDs = Gdal.Open(vxTif, Access.GA_ReadOnly);
                vyDs = Gdal.Open(vyTif, Access.GA_ReadOnly);
                if (null == vxDs || 0 == vxDs.RasterCount || null == vyDs || 0 == vyDs.RasterCount) return;
                float[] vxBuffer = new float[width * height], vyBuffer = new float[width * height];
                vxDs.GetRasterBand(1).ReadRaster(0, 0, width, height, vxBuffer, width, height, 0, 0);
                vyDs.GetRasterBand(1).ReadRaster(0, 0, width, height, vyBuffer, width, height, 0, 0);
                Bitmap image = new Bitmap(width, height);
                Graphics graphic = Graphics.FromImage(image);
                graphic.Clear(Color.Transparent); // 填充透明色
                // 用 R通道表示,流向为归一化的二维向量(x,y),G通道表示为 x *255 , B通道表示为 y * 255
                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        int offset = x + y * width;
                        float fx = GetFloatValue(vxBuffer[offset]);
                        float fy = GetFloatValue(vyBuffer[offset]);
                        double dx = fx * 0.5 + 0.5;
                        double dy = fy * 0.5 + 0.5;
                        double dv = Math.Sqrt(Math.Pow(fx, 2) + Math.Pow(fy, 2));
                        int r = Convert.ToInt32(dv * 255);
                        int g = Convert.ToInt32(dx * 255);
                        int b = Convert.ToInt32(dy * 255);
                        Color color = Color.FromArgb(127, r, g, b);
                        image.SetPixel(x, y, color);
                    }
                }
                image.Save(png, System.Drawing.Imaging.ImageFormat.Png);
            }
            finally
            {
                if (null != vxDs) vxDs.Dispose();
                if (null != vyDs) vyDs.Dispose();
                if (File.Exists(vxTif)) File.Delete(vxTif);
                if (File.Exists(vyTif)) File.Delete(vyTif);
            }
        }
        /// <summary>
        /// 获取Float值
        /// </summary>
        private static float GetFloatValue(float val)
        {
            return (float.IsNaN(val) || val < -999) ? 0 : val;
        }
        #endregion
        #region 元数据
        /// <summary>
        /// 处理元数据
        /// </summary>
        private static void CopeLayerJson(string outPath, Domain.Layer layer)
        {
            if (null == layer) return;
            String json = JsonConvert.SerializeObject(layer);
            string filePath = Path.Combine(outPath, "layer.json");
            using (StreamWriter sw = new StreamWriter(filePath))
            {
                sw.Write(json);
            }
        }
        #endregion
    }