管道基础大数据平台系统开发-【CS】-ExportMap
1
13693261870
2024-07-24 663662d22683211608cee07e1c1e32761e992a2a
1
已修改2个文件
129 ■■■■■ 文件已修改
SimuTools/Tools/GdalHelper.cs 18 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
SimuTools/Tools/Handle.cs 111 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
SimuTools/Tools/GdalHelper.cs
@@ -9,6 +9,7 @@
{
    public class GdalHelper
    {
        #region 成员变量+构造函数
        private static bool isInited;
        private static GdalHelper instance;
@@ -42,6 +43,7 @@
                }
            }
        }
        #endregion
        /// <summary>
        /// 实例
@@ -135,14 +137,14 @@
            }
            String srsName = ds.GetSpatialRef().GetName();
            if (srsName.Contains(CGCS2000))
            {
                point.TransformTo(sr4490);
            }
            else
            {
            //if (srsName.Contains(CGCS2000))
            //{
            //    point.TransformTo(sr4490);
            //}
            //else
            //{
                point.TransformTo(sr4326);
            }
            //}
            point.SwapXY();
            return point;
@@ -161,7 +163,7 @@
                ds = Gdal.Open(file, Access.GA_ReadOnly);
                if (null == ds) return;
                OSGeo.GDAL.Band band = ds.GetRasterBand(1);
                Band band = ds.GetRasterBand(1);
                if (0 == band.GetOverviewCount())
                {
                    ds.BuildOverviews("nearest", new int[] { 2, 4, 6, 8, 16 });
SimuTools/Tools/Handle.cs
@@ -40,10 +40,10 @@
            CopeTerrain(terrainFile, outPath, layer);
            CopeWater(waterPath, outPath, layer);
            CopeFlow(flowPath, outPath, layer);
            //CopeFlow(flowPath, outPath, layer);
            CopeLayerJson(outPath, layer);
            if (Directory.Exists(temp)) Directory.Delete(temp, true);
            //if (Directory.Exists(temp)) Directory.Delete(temp, true);
        }
        #region 地形
@@ -58,8 +58,8 @@
                ds = Gdal.Open(terrainFile, Access.GA_ReadOnly);
                if (null == ds || 0 == ds.RasterCount || null == ds.GetSpatialRef()) return;
                SetTerrainInfo(ds, layer);
                //CreateTerrainPng(ds, layer, outPath);
                SetTerrainInfo(ds, outPath, layer);
                CreateTerrainPng(ds, layer, outPath);
            }
            finally
            {
@@ -70,10 +70,10 @@
        /// <summary>
        /// 设置地形信息
        /// </summary>
        private static void SetTerrainInfo(Dataset ds, Domain.Layer layer)
        private static void SetTerrainInfo(Dataset ds, string outPath, 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);
@@ -137,23 +137,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);
@@ -340,7 +329,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);
@@ -484,18 +473,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);
                }
            }
@@ -509,10 +491,71 @@
        {
            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) val = 255;
            return val;
        }
        #endregion
        #region 元数据
        /// <summary>
        /// 设置地形信息
        /// </summary>
        private static void SetTerrainInfo2(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 CopeLayerJson(string outPath, Domain.Layer layer)