| | |
| | | #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; |
| | |
| | | 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); |
| | | CopeFlow(flowPath, outPath, layer); |
| | | CopeLayerJson(outPath, layer); |
| | | |
| | | //if (Directory.Exists(temp)) Directory.Delete(temp, true); |
| | | if (Directory.Exists(temp)) Directory.Delete(temp, true); |
| | | } |
| | | |
| | | #region 地形 |
| | | /// <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 |
| | |
| | | ds = Gdal.Open(terrainFile, Access.GA_ReadOnly); |
| | | if (null == ds || 0 == ds.RasterCount || null == ds.GetSpatialRef()) return; |
| | | |
| | | SetTerrainInfo(ds, outPath, layer); |
| | | SetTerrainInfo(ds, layer); |
| | | SetWaterInfo(waterPath, layer); |
| | | CreateTerrainPng(ds, layer, outPath); |
| | | } |
| | | finally |
| | |
| | | /// <summary> |
| | | /// 设置地形信息 |
| | | /// </summary> |
| | | private static void SetTerrainInfo(Dataset ds, string outPath, Domain.Layer layer) |
| | | 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"; |
| | |
| | | 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); |
| | | |
| | |
| | | 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); |
| | |
| | | |
| | | #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> |
| | |
| | | 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(); |
| | |
| | | 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> |
| | |
| | | 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> |
| | |
| | | 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); |
| | |
| | | private static int GetSafeValue(int val) |
| | | { |
| | | if (val < 0) return 0; |
| | | if (val > 255) val = 255; |
| | | if (val > 255) return 255; |
| | | |
| | | return val; |
| | | } |
| | |
| | | |
| | | #region 元数据 |
| | | /// <summary> |
| | | /// 处理元数据 |
| | | /// </summary> |
| | | private static void CopeLayerJson(string outPath, Domain.Layer layer) |
| | | { |
| | | if (null == layer) return; |
| | | |
| | | String json = JsonConvert.SerializeObject(layer); |
| | | |
| | | string filePath = Path.Combine(outPath, "layer.json"); |
| | | using (StreamWriter sw = new StreamWriter(filePath)) |
| | | { |
| | | sw.Write(json); |
| | | } |
| | | } |
| | | #endregion |
| | | |
| | | #region 暂时不用 |
| | | /// <summary> |
| | | /// 设置地形信息 |
| | | /// </summary> |
| | | private static void SetTerrainInfo2(Dataset ds, string outPath, Domain.Layer layer) |
| | | private static void SetTerrainInfo(Dataset ds, string outPath, Domain.Layer layer) |
| | | { |
| | | string tempPath = Path.Combine(outPath, "temp"); |
| | | if (!Directory.Exists(tempPath)) Directory.CreateDirectory(tempPath); |
| | |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 处理元数据 |
| | | /// </summary> |
| | | private static void CopeLayerJson(string outPath, Domain.Layer layer) |
| | | { |
| | | if (null == layer) return; |
| | | |
| | | String json = JsonConvert.SerializeObject(layer); |
| | | |
| | | string filePath = Path.Combine(outPath, "layer.json"); |
| | | using (StreamWriter sw = new StreamWriter(filePath)) |
| | | { |
| | | sw.Write(json); |
| | | } |
| | | } |
| | | #endregion |
| | | |
| | | #region 暂时不用 |
| | | /// <summary> |
| | | /// 重采样 * |
| | | /// 重采样 |
| | | /// </summary> |
| | | private static void Resample(string source, string dest, int width, int height) |
| | | { |