| | |
| | | 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); |
| | | |
| | |
| | | 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, 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; |
| | |
| | | |
| | | /// <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) |
| | | { |
| | |
| | | /// <summary> |
| | | /// Tif值转存至Png |
| | | /// </summary> |
| | | private static void Tif2Png(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, int heightOffset = 0) |
| | | { |
| | | Dataset ds = null; |
| | | try |
| | |
| | | Graphics graphic = Graphics.FromImage(image); |
| | | graphic.Clear(Color.Transparent); // 填充透明色 |
| | | |
| | | //double perHeight = (layer.extension.maxHeight - layer.extension.minHeight) * 100 / 65536; |
| | | //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); |
| | | |
| | | 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) |
| | | { |
| | | //image.SetPixel(x, y, Color.Transparent); |
| | | continue; |
| | | } |
| | | if (float.IsNaN(buffer[offset]) || buffer[offset] < -999) continue; |
| | | |
| | | int val = Convert.ToInt32(buffer[offset] * 100); |
| | | //int val = Convert.ToInt32(buffer[offset] * 100); |
| | | int val = Convert.ToInt32((buffer[offset] - layer.extension.minHeight + heightOffset) * 100 / perHeight); |
| | | int r = val / 65536; |
| | | int g = (val - r * 65536) / 256; |
| | | int b = val % 256; |
| | |
| | | image.SetPixel(x, y, color); |
| | | } |
| | | } |
| | | |
| | | image.Save(png, System.Drawing.Imaging.ImageFormat.Png); |
| | | } |
| | | finally |
| | |
| | | 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; |
| | |
| | | } |
| | | |
| | | string png = Path.Combine(waterPath, sizes[0] + "_" + sizes[1] + ".png"); |
| | | Tif2Png(layer, tif, png, sizes[0], sizes[1]); |
| | | Tif2Png(layer, tif, png, sizes[0], sizes[1], 1); |
| | | } |
| | | } |
| | | |
| | |
| | | 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 => |
| | | //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; |
| | | |
| | |
| | | if (null != vxDs) vxDs.Dispose(); |
| | | if (null != vyDs) vyDs.Dispose(); |
| | | } |
| | | }); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | 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 fx = GetFloatValue(vxBuffer[offset]); |
| | | float fy = GetFloatValue(vyBuffer[offset]); |
| | | |
| | | 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; |
| | | double dv = GetXyValue(fx, fy); |
| | | double dx = float.IsNaN(fx) ? 0 : fx * 0.5 + 0.5; |
| | | double dy = float.IsNaN(fy) ? 0 : fy * 0.5 + 0.5; |
| | | |
| | | 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); |
| | |
| | | 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) ? float.NaN : val; |
| | | if (float.IsNaN(val) || val < -999) return float.NaN; |
| | | if (val > 1) return 1; |
| | | if (val < -1) return -1; |
| | | |
| | | return val; |
| | | } |
| | | |
| | | private static double GetXyValue(float fx, float fy) |
| | | { |
| | | if (float.IsNaN(fx) && float.IsNaN(fy)) |
| | | { |
| | | return 0; |
| | | } |
| | | 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)); |
| | | } |
| | | #endregion |
| | | |
| | |
| | | { |
| | | sw.Write(json); |
| | | } |
| | | } |
| | | #endregion |
| | | |
| | | #region 暂时不用 |
| | | /// <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(); |
| | | } |
| | | #endregion |
| | | } |