管道基础大数据平台系统开发-【CS】-ExportMap
13693261870
2024-07-19 5d77b35448990a62846b29ce96ceb0cf2faa20f7
添加Tif值转存至Png
已修改2个文件
69 ■■■■ 文件已修改
SimuTools/FrmMain.cs 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
SimuTools/Tools/Handle.cs 60 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
SimuTools/FrmMain.cs
@@ -7,8 +7,6 @@
{
    public partial class FrmMain : Form
    {
        private bool flag;
        public FrmMain()
        {
            InitializeComponent();
@@ -109,10 +107,9 @@
                return;
            }
            if (flag) return;
            try
            {
                flag = true;
                this.btnRun.Enabled = false;
                LogOut.Info("开始运行 >>");
                outPath = Path.Combine(outPath, serviceName);
@@ -123,13 +120,13 @@
                Tools.Handle.Run(terrainFile, waterPath, flowPath, outPath);
                flag = false;
                this.btnRun.Enabled = true;
                LogOut.Info("运行结束 <<");
                MessageBox.Show("运行结束!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                flag = false;
                this.btnRun.Enabled = true;
                LogOut.Error(ex.StackTrace);
                ShowErr("运行出错:" + ex.Message);
            }
SimuTools/Tools/Handle.cs
@@ -5,16 +5,19 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SimuTools.Tools
{
    public class Handle
    {
        public static readonly int MAX = 256 * 256;
        public static readonly string GdalPath = ConfigurationManager.AppSettings["gdalPath"];
        public static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory;
@@ -88,7 +91,6 @@
            foreach (int[] sizes in layer.terrain.size)
            {
                //string filePath = Path.Combine(outPath, sizes[0] + "_" + sizes[1] + ".png");
                //// 填充内存图像
                //byte[] buffer = new byte[sizes[0] * sizes[1] * 3];
                //for (int i = 0; i < buffer.Length; i++)
@@ -103,7 +105,8 @@
                    continue;
                }
                //
                string png = Path.Combine(terrainPath, sizes[0] + "_" + sizes[1] + ".png");
                Terrain2Png(layer, tif, png, sizes[0], sizes[1]);
            }
        }
@@ -112,6 +115,7 @@
        /// </summary>
        private static void Resample(string source, string dest, int width, int height)
        {
            // https://blog.51cto.com/u_16099346/6691820
            string cmd = string.Format("{0}gdalwarp.exe -t_srs {1} -ts {2} {3} -r {4} -of GTiff \"{5}\" \"{6}\"", GdalPath, "EPSG:4326", width, height, "bilinear", source, dest);
            string err = ExecExe(cmd);
        }
@@ -158,6 +162,56 @@
        }
        /// <summary>
        /// Tif值转存至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.extension.maxHeight - layer.extension.minHeight) * 100 / 65536;
                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        if (buffer[x * y] == double.NaN || buffer[x * y] == -9999)
                        {
                            image.SetPixel(x, y, Color.Transparent);
                            continue;
                        }
                        //int val = Convert.ToInt32(buffer[x * y]* 100); //Convert.ToInt32((buffer[x * y] - layer.extension.minHeight) * 100);
                        int val = Convert.ToInt32((buffer[x * y] - layer.extension.minHeight) * 100 / perHeight);
                        int r = val / 65536;
                        int g = (val - r * 65536) / 256;
                        int b = val % 256;
                        Color color = Color.FromArgb(0, r, g, b);
                        image.SetPixel(x, y, color);
                    }
                }
                image.Save(png, System.Drawing.Imaging.ImageFormat.Png);
            }
            finally
            {
                if (null != ds) ds.Dispose();
            }
        }
        /// <summary>
        /// 处理水面
        /// </summary>
        private static void CopeWater(string waterPath, string outPath, Domain.Layer layer)