管道基础大数据平台系统开发-【CS】-ExportMap
13693261870
2024-07-25 9d25253a85cc4fb3aa0f4adc764626ad0e64d66a
SimuTools/Tools/Handle.cs
@@ -18,6 +18,10 @@
        #region 成员变量
        public static readonly int MAX = 256 * 256;
        public static readonly double MAX_X_OFFSET = 0.002;
        public static readonly double MIN_X_OFFSET = -0.00005;
        public static readonly double WaterHeightOffset = 1.0;
        public static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory;
@@ -38,7 +42,7 @@
            string temp = Path.Combine(outPath, "temp");
            if (!Directory.Exists(temp)) Directory.CreateDirectory(temp);
            CopeTerrain(terrainFile, outPath, layer);
            CopeTerrain(terrainFile, waterPath, outPath, layer);
            CopeWater(waterPath, outPath, layer);
            CopeFlow(flowPath, outPath, layer);
            CopeLayerJson(outPath, layer);
@@ -50,7 +54,7 @@
        /// <summary>
        /// 处理地形
        /// </summary>
        private static void CopeTerrain(string terrainFile, string outPath, Domain.Layer layer)
        private static void CopeTerrain(string terrainFile, string waterPath, string outPath, Domain.Layer layer)
        {
            Dataset ds = null;
            try
@@ -59,7 +63,8 @@
                if (null == ds || 0 == ds.RasterCount || null == ds.GetSpatialRef()) return;
                SetTerrainInfo(ds, layer);
                //CreateTerrainPng(ds, layer, outPath);
                SetWaterInfo(waterPath, layer);
                CreateTerrainPng(ds, layer, outPath);
            }
            finally
            {
@@ -72,14 +77,14 @@
        /// </summary>
        private static void SetTerrainInfo(Dataset ds, Domain.Layer layer)
        {
            String srsName = ds.GetSpatialRef().GetName();
            layer.terrain.epsg = !string.IsNullOrEmpty(srsName) && srsName.Contains(GdalHelper.CGCS2000) ? "EPSG:4490" : "EPSG:4326";
            //String srsName = ds.GetSpatialRef().GetName();
            //layer.terrain.epsg = !string.IsNullOrEmpty(srsName) && srsName.Contains(GdalHelper.CGCS2000) ? "EPSG:4490" : "EPSG:4326";
            Geometry minPoint = GdalHelper.GetMinPoint(ds);
            Geometry maxPoint = GdalHelper.GetMaxPoint(ds);
            double minx = GetMinVal(minPoint.GetX(0), 10000000);
            double miny = GetMinVal(minPoint.GetY(0), 10000000);
            double maxx = GetMaxVal(maxPoint.GetX(0), 10000000);
            double maxx = GetMaxVal(maxPoint.GetX(0) + MAX_X_OFFSET, 10000000);
            double maxy = GetMaxVal(maxPoint.GetY(0), 10000000);
            layer.extension = new Extension(minx, miny, maxx, maxy, double.MaxValue, double.MinValue);
@@ -137,23 +142,12 @@
            //string[] options = Gdal.ParseCommandLine(args);
            string[] options = new string[]
            {
                "-t_srs",
                layer.terrain.epsg, // "EPSG:4326",
                "-ts",
                width.ToString(),
                height.ToString(),
                "-te",
                layer.extension.minx.ToString(),
                layer.extension.miny.ToString(),
                layer.extension.maxx.ToString(),
                layer.extension.maxy.ToString(),
                "-te_srs",
                layer.terrain.epsg, // "EPSG:4326",
                "-r",
                "bilinear",
                "-of",
                "GTiff",
                "-t_srs", "EPSG:4326", // layer.terrain.epsg,
                "-ts", width.ToString(), height.ToString(),
                "-te", layer.extension.minx.ToString(), layer.extension.miny.ToString(), layer.extension.maxx.ToString(), layer.extension.maxy.ToString(),
                "-te_srs", "EPSG:4326", // layer.terrain.epsg,
                "-r", "bilinear",
                "-of", "GTiff"
            };
            GDALWarpAppOptions warpAppOptions = new GDALWarpAppOptions(options);
@@ -180,19 +174,30 @@
                Graphics graphic = Graphics.FromImage(image);
                graphic.Clear(Color.Transparent); // 填充透明色
                double perHeight = (layer.terrain.maxHeight - layer.terrain.minHeight) * 100 / 65536;
                //double perHeight = (layer.terrain.maxHeight - layer.terrain.minHeight) * 100 / 65536;
                //double differ = layer.terrain.maxHeight - layer.terrain.minHeight;
                double differ = layer.extension.maxHeight - layer.extension.minHeight, minHeight = layer.extension.minHeight;
                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;
                        if (float.IsNaN(buffer[offset]) || buffer[offset] < -999 || buffer[offset] < minHeight) 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;
                        //int val = Convert.ToInt32((buffer[offset] - layer.terrain.minHeight) * 100 / perHeight);
                        //int val = Convert.ToInt32((buffer[offset] - layer.terrain.minHeight) / differ * 255); // 65535
                        int r = 0, g, b;
                        if (buffer[offset] - layer.extension.maxHeight > 0)
                        {
                            g = b = 255;
                        }
                        else
                        {
                            int val = Convert.ToInt32((buffer[offset] - minHeight) / differ * 65535);
                            g = val / 256;
                            b = val % 256;
                        }
                        Color color = Color.FromArgb(127, r, g, b);
                        image.SetPixel(x, y, color);
@@ -210,18 +215,26 @@
        #region 水面
        /// <summary>
        /// 处理水面
        /// 设置水面信息
        /// </summary>
        private static void CopeWater(string waterPath, string outPath, Domain.Layer layer)
        private static void SetWaterInfo(string waterPath, Domain.Layer layer)
        {
            List<string> files = GetFiles(waterPath);
            List<string> files = layer.waters.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 void CopeWater(string waterPath, string outPath, Domain.Layer layer)
        {
            List<String> files = layer.waters.files;
            if (files.Count == 0 || files.Count != layer.waters.data.Count) return;
            ProcessWaters(files, outPath, layer);
        }
        /// <summary>
@@ -230,7 +243,7 @@
        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;
            if (null == files || files.Length == 0) return new List<string>();
            List<string> list = files.ToList();
            list.Sort();
@@ -256,6 +269,8 @@
                DateTime d = new DateTime(now.Year, now.Month, now.Day, hour, minute, second);
                layer.waters.data.Add((d.Ticks - startTicks) / 10000);
            }
            layer.duration.start = layer.waters.data[0];
            layer.duration.end = layer.waters.data[layer.waters.data.Count - 1];
        }
        /// <summary>
@@ -280,8 +295,8 @@
                    if (null != ds) ds.Dispose();
                }
            });
            layer.extension.minHeight = GetMinVal(layer.extension.minHeight);
            layer.extension.maxHeight = GetMaxVal(layer.extension.maxHeight + WaterHeightOffset);
            layer.extension.minHeight = GetMinVal(layer.extension.minHeight - 1);
            layer.extension.maxHeight = GetMaxVal(layer.extension.maxHeight + 1);
        }
        /// <summary>
@@ -340,7 +355,7 @@
                ds = Gdal.Open(tif, Access.GA_ReadOnly);
                if (null == ds || 0 == ds.RasterCount) return;
                OSGeo.GDAL.Band band = ds.GetRasterBand(1);
                Band band = ds.GetRasterBand(1);
                float[] buffer = new float[width * height];
                band.ReadRaster(0, 0, width, height, buffer, width, height, 0, 0);
@@ -348,19 +363,32 @@
                Graphics graphic = Graphics.FromImage(image);
                graphic.Clear(Color.Transparent); // 填充透明色
                double perHeight = (layer.extension.maxHeight - layer.extension.minHeight) * 100 / 65536;
                //double perHeight = (layer.extension.maxHeight - layer.extension.minHeight) * 100 / 65536;
                double differ = layer.extension.maxHeight - layer.extension.minHeight, minHeight = layer.extension.minHeight;
                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;
                        if (float.IsNaN(buffer[offset]) || buffer[offset] < -999 || buffer[offset] < minHeight) 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;
                        //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;
                        int r = 0, g, b;
                        if (buffer[offset] - layer.extension.maxHeight > 0)
                        {
                            g = b = 255;
                        }
                        else
                        {
                            int val = Convert.ToInt32((buffer[offset] - minHeight) / differ * 65535);
                            g = val / 256;
                            b = val % 256;
                        }
                        Color color = Color.FromArgb(127, r, g, b);
                        image.SetPixel(x, y, color);
@@ -484,18 +512,11 @@
                    fy = float.IsNaN(fy) ? 0 : fy;
                    double dr = Math.Sqrt(Math.Pow(fx, 2) + Math.Pow(fy, 2));
                    int r = Convert.ToInt32((dr * 0.5 + 0.5) * 255);
                    int r = Convert.ToInt32(dr / 4 * 255);
                    int g = Convert.ToInt32((fx / dr * 0.5 + 0.5) * 255);
                    int b = Convert.ToInt32((fy / dr * 0.5 + 0.5) * 255);
                    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);
                    Color color = Color.FromArgb(127, GetSafeValue(r), GetSafeValue(g), GetSafeValue(b));
                    image.SetPixel(x, y, color);
                }
            }
@@ -508,6 +529,17 @@
        private static float GetFloatValue(float val)
        {
            return (float.IsNaN(val) || val < -999) ? float.NaN : val;
        }
        /// <summary>
        /// 获取安全值
        /// </summary>
        private static int GetSafeValue(int val)
        {
            if (val < 0) return 0;
            if (val > 255) return 255;
            return val;
        }
        #endregion
@@ -531,7 +563,57 @@
        #region 暂时不用
        /// <summary>
        /// 重采样 *
        /// 设置地形信息
        /// </summary>
        private static void SetTerrainInfo(Dataset ds, string outPath, Domain.Layer layer)
        {
            string tempPath = Path.Combine(outPath, "temp");
            if (!Directory.Exists(tempPath)) Directory.CreateDirectory(tempPath);
            string tif = Path.Combine(tempPath, Path.GetFileNameWithoutExtension(ds.GetDescription()) + ".tif");
            Resample(ds, tif);
            if (!File.Exists(tif)) throw new Exception("高程文件重投影失败!");
            Dataset tifDS = null;
            try
            {
                tifDS = Gdal.Open(tif, Access.GA_ReadOnly);
                if (null == tifDS || 0 == tifDS.RasterCount || null == tifDS.GetSpatialRef()) return;
                double[] transform = new double[6];
                tifDS.GetGeoTransform(transform);
                double minx = transform[0];
                double miny = transform[3] - tifDS.RasterYSize * transform[1];
                double maxx = transform[0] + (tifDS.RasterYSize * transform[1]);
                double maxy = transform[3];
                layer.extension = new Extension(minx, miny, maxx, maxy, double.MaxValue, double.MinValue);
                Band band = tifDS.GetRasterBand(1);
                double[] mm = new double[2];
                band.ComputeRasterMinMax(mm, 0);
                layer.terrain.SetHeight(GetMinVal(mm[0]), GetMaxVal(mm[1]));
            }
            finally
            {
                if (null != tifDS) tifDS.Dispose();
            }
        }
        /// <summary>
        /// 重采样
        /// </summary>
        private static void Resample(Dataset ds, string dest)
        {
            string[] options = new string[] { "-t_srs", "EPSG:4326", "-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)
        {