| | |
| | | |
| | | 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 地形 |
| | |
| | | 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 |
| | | { |
| | |
| | | /// <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); |
| | |
| | | //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); |
| | | |
| | |
| | | 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); |
| | | |
| | |
| | | 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); |
| | | } |
| | | } |
| | |
| | | { |
| | | 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) |