管道基础大数据平台系统开发-【CS】-ExportMap
1
13693261870
2024-07-20 ecfe110922f3c542df7aaef9ac9e26cdb0b73e55
SimuTools/Tools/Handle.cs
@@ -36,13 +36,14 @@
            if (!Directory.Exists(temp)) Directory.CreateDirectory(temp);
            CopeTerrain(terrainFile, outPath, layer);
            CopeWater(waterPath, outPath, layer);
            //CopeWater(waterPath, outPath, layer);
            CopeFlow(flowPath, outPath, layer);
            CopeLayerJson(outPath, layer);
            if (Directory.Exists(temp)) Directory.Delete(temp, true);
        }
        #region 地形
        /// <summary>
        /// 处理地形
        /// </summary>
@@ -55,7 +56,7 @@
                if (null == ds || 0 == ds.RasterCount || null == ds.GetSpatialRef()) return;
                SetTerrainInfo(ds, layer);
                CreateTerrainPng(ds, layer, outPath);
                //CreateTerrainPng(ds, layer, outPath);
            }
            finally
            {
@@ -79,7 +80,7 @@
        }
        /// <summary>
        /// 创建地形图层
        /// 创建地形PNG
        /// </summary>
        private static void CreateTerrainPng(Dataset ds, Domain.Layer layer, string outPath)
        {
@@ -91,19 +92,46 @@
            foreach (int[] sizes in layer.terrain.size)
            {
                string tif = Path.Combine(tempPath, DateTime.Now.Ticks.ToString() + ".tif");
                Resample(ds.GetDescription(), tif, sizes[0], sizes[1]);
                //Resample(ds.GetDescription(), tif, sizes[0], sizes[1]);
                Resample(ds, tif, sizes[0], sizes[1]);
                if (!File.Exists(tif))
                {
                    continue;
                }
                string png = Path.Combine(terrainPath, sizes[0] + "_" + sizes[1] + ".png");
                Terrain2Png(layer, tif, png, sizes[0], sizes[1]);
                Tif2Png(layer, tif, png, sizes[0], sizes[1]);
            }
        }
        /// <summary>
        /// 重采样
        /// </summary>
        private static void Resample(Dataset ds, string dest, int width, int height)
        {
            // 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");
            //string[] options = Gdal.ParseCommandLine(args);
            string[] options = new string[]
            {
                "-t_srs",
                "EPSG:4326",
                "-ts",
                width.ToString(),
                height.ToString(),
                "-r",
                "bilinear",
                "-of",
                "GTiff"
            };
            GDALWarpAppOptions warpAppOptions = new GDALWarpAppOptions(options);
            Dataset destDs = Gdal.Warp(dest, new Dataset[] { ds }, warpAppOptions, null, null);
            destDs.Dispose();
        }
        /// <summary>
        /// 重采样 *
        /// </summary>
        private static void Resample(string source, string dest, int width, int height)
        {
@@ -156,7 +184,7 @@
        /// <summary>
        /// Tif值转存至Png
        /// </summary>
        private static void Terrain2Png(Domain.Layer layer, string tif, string png, int width, int height)
        private static void Tif2Png(Domain.Layer layer, string tif, string png, int width, int height)
        {
            Dataset ds = null;
            try
@@ -180,8 +208,8 @@
                {
                    for (int y = 0; y < height; y++)
                    {
                        int offset = x + y * height;
                        if (float.IsNaN(buffer[offset]) || buffer[offset] == -9999)
                        int offset = x + y * width;
                        if (float.IsNaN(buffer[offset]) || buffer[offset] < -999)
                        {
                            //image.SetPixel(x, y, Color.Transparent);
                            continue;
@@ -205,7 +233,9 @@
                if (File.Exists(tif)) File.Delete(tif);
            }
        }
        #endregion
        #region 水面
        /// <summary>
        /// 处理水面
        /// </summary>
@@ -215,6 +245,7 @@
            if (null == files || files.Count == 0) return;
            SetWaterData(layer, files);
            if (files.Count != layer.waters.data.Count) return;
            /*for (int i = 0, c = files.Count; i < c; i++)
            {
@@ -249,7 +280,7 @@
        }
        /// <summary>
        /// 创建地形图层
        /// 创建水面PNG
        /// </summary>
        private static void CreateWaterPng(Dataset ds, Domain.Layer layer, string outPath, long ticks)
        {
@@ -269,16 +300,16 @@
                }
                string png = Path.Combine(waterPath, sizes[0] + "_" + sizes[1] + ".png");
                Terrain2Png(layer, tif, png, sizes[0], sizes[1]);
                Tif2Png(layer, tif, png, sizes[0], sizes[1]);
            }
        }
        /// <summary>
        /// 获取文件
        /// </summary>
        private static List<String> GetFiles(string path)
        private static List<String> GetFiles(string path, string prefix = "")
        {
            string[] files = Directory.GetFiles(path, "*.tif?", SearchOption.TopDirectoryOnly);
            string[] files = Directory.GetFiles(path, prefix + "*.tif?", SearchOption.TopDirectoryOnly);
            if (null == files || files.Length == 0) return null;
            List<string> list = files.ToList();
@@ -306,15 +337,120 @@
                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;
                        if (float.IsNaN(vxBuffer[offset]) || vxBuffer[offset] < -999 ||
                            float.IsNaN(vyBuffer[offset]) || vyBuffer[offset] < -999) continue;
                        float vx = vxBuffer[offset], vy = vyBuffer[offset];
                        //int val = Convert.ToInt32(vxBuffer[offset] * 100);
                        //int r = val / 65536;
                        //int g = (val - r * 65536) / 256;
                        //int b = val % 256;
                        int r = 0;
                        int g = 0;
                        int b = 0;
                        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);
            }
        }
        #endregion
        #region 元数据
        /// <summary>
        /// 处理元数据
        /// </summary>
@@ -329,29 +465,6 @@
            {
                sw.Write(json);
            }
        }
        #region 暂时不用
        /// <summary>
        /// 重采样 *
        /// </summary>
        private static void Resample(Dataset ds, string dest, int width, int height)
        {
            // 设置Warp的选项:https://blog.csdn.net/qq_43210879/article/details/121350561
            string[] options = new string[] {
                "format=GTiff",
                "width=" + width,
                "height=" + height,
                "dstSRS=EPSG:4326"
            };
            OSGeo.GDAL.Driver driver = Gdal.GetDriverByName("GTiff");
            Dataset destDs = driver.Create(dest, width, height, ds.RasterCount, ds.GetRasterBand(1).DataType, null);
            GDALWarpAppOptions warpAppOptions = new GDALWarpAppOptions(options);
            Gdal.Warp(destDs, new Dataset[] { ds }, warpAppOptions, null, null);
            destDs.Dispose();
        }
        #endregion
    }