static void Scan2(string srcDir) { for (int x = 1; x < 72; x += 2) { for (int y = 1; y < 36; y += 2) { string file = Path.Combine(srcDir, "tile_" + x.ToString("D2") + "_" + y.ToString("D2") + "_3" + ".tdmp"); if (File.Exists(file)) { TDMPIO d1 = new TDMPIO(); d1.Load(new DevFileLocation(file)); Point coord; coord.X = (int)Math.Truncate(d1.Xllcorner); coord.Y = (int)Math.Truncate(d1.Yllcorner); int col = x * 5 - 185; int lat = 95 - y * 5; if (col != coord.X || lat != coord.Y) { Console.WriteLine("Err: " + Path.GetFileNameWithoutExtension(file)); d1.Xllcorner = col; d1.Yllcorner = lat; Stream stm = File.Open(file, FileMode.Open); stm.SetLength(0); d1.Save(stm); } } } } }
static void ElevationBias() { const string SrcDir = @"E:\Documents\ic10gd\Source\Code2015\bin\x86\Debug\terrain.lpk"; const string OutDir = @"E:\Desktop\out"; for (int x = 1; x < 72; x += 2) { for (int y = 1; y < 36; y += 2) { string file = Path.Combine(SrcDir, "tile_" + x.ToString("D2") + "_" + y.ToString("D2") + "_0" + ".tdmp"); string file2 = Path.Combine(OutDir, "tile_" + x.ToString("D2") + "_" + y.ToString("D2") + ".tdmp"); if (File.Exists(file)) { TDMPIO d1 = new TDMPIO(); d1.Load(new DevFileLocation(file)); for (int i = 0; i < d1.Height; i++) { for (int j = 0; j < d1.Width; j++) { int idx = i * d1.Height + j; d1.Data[idx] += 45;; } } Stream sout = File.Open(file2, FileMode.OpenOrCreate); d1.Save(sout); } } } }
static void DoubleSize() { const string SrcDir = @"E:\Documents\ic10gd\Source\Code2015\bin\x86\Debug\terrain.lpk"; const string OutDir = @"E:\Desktop\out"; for (int x = 1; x < 72; x += 2) { for (int y = 1; y < 36; y += 2) { string file = Path.Combine(SrcDir, "tile_" + x.ToString("D2") + "_" + y.ToString("D2") + "_3.tdmp"); string file2 = Path.Combine(OutDir, "tile_" + x.ToString("D2") + "_" + y.ToString("D2") + "_3.tdmp"); if (File.Exists(file)) { TDMPIO d1 = new TDMPIO(); d1.Load(new FileLocation(file)); d1.XSpan *= 2; d1.YSpan *= 2; Stream sout = File.Open(file2, FileMode.OpenOrCreate); d1.Save(sout); } } } }
public override void Convert(ResourceLocation source, ResourceLocation dest) { TDMPIO srcData = new TDMPIO(); srcData.Load(source); srcData.Bits = 12; srcData.Save(dest.GetStream); }
public override void Convert(ResourceLocation source, ResourceLocation dest) { FileLocation fl = source as FileLocation; if (fl != null) { string path = fl.Path; string dir = Path.GetDirectoryName(path); string fn = Path.GetFileNameWithoutExtension(path); TDMPIO srcData = new TDMPIO(); srcData.Load(source); float[] demData = srcData.Data; int tw = srcData.Width; int th = srcData.Height; int level = 0; FileStream fs = new FileStream(Path.Combine(dir, fn + "_" + level++.ToString() + ".tdmp"), FileMode.OpenOrCreate, FileAccess.Write); srcData.Save(fs); do { tw = tw / 4 + 1; th = th / 4 + 1; float[] levelData = Resize(demData, srcData.Width, srcData.Height, tw, th); TDMPIO result = new TDMPIO(); result.Width = tw; result.Height = th; result.XSpan = srcData.XSpan; result.YSpan = srcData.YSpan; result.Xllcorner = srcData.Xllcorner; result.Yllcorner = srcData.Yllcorner; result.Bits = srcData.Bits; result.Data = levelData; fs = new FileStream(Path.Combine(dir, fn + "_" + level++.ToString() + ".tdmp"), FileMode.OpenOrCreate, FileAccess.Write); result.Save(fs); }while (tw > 32 && th > 32); } }
public override void Convert(ResourceLocation source, ResourceLocation dest) { TDMPIO src = new TDMPIO(); src.Load(source); float[] rsd = TDmpLodGen.Resize(src.Data, src.Width, src.Height, ResizeSize, ResizeSize); TDMPIO dst = new TDMPIO(); dst.Width = ResizeSize; dst.Height = ResizeSize; dst.Bits = src.Bits; dst.Xllcorner = src.Xllcorner; dst.Yllcorner = src.Yllcorner; dst.XSpan = src.XSpan; dst.YSpan = src.YSpan; dst.Data = rsd; dst.Save(dest.GetStream); }
public override void Convert(ResourceLocation source, ResourceLocation dest) { FileLocation fl = source as FileLocation; if (fl != null) { string path = fl.Path; string dir = Path.GetDirectoryName(path); string fn = Path.GetFileNameWithoutExtension(path); TDMPIO srcData = new TDMPIO(); srcData.Load(source); float[] demData = srcData.Data; int tw = srcData.Width; int th = srcData.Height; int level = 0; FileStream fs = new FileStream(Path.Combine(dir, fn + "_" + level++.ToString() + ".tdmp"), FileMode.OpenOrCreate, FileAccess.Write); srcData.Save(fs); do { tw = tw / 4 + 1; th = th / 4 + 1; float[] levelData = Resize(demData, srcData.Width, srcData.Height, tw, th); TDMPIO result = new TDMPIO(); result.Width = tw; result.Height = th; result.XSpan = srcData.XSpan; result.YSpan = srcData.YSpan; result.Xllcorner = srcData.Xllcorner; result.Yllcorner = srcData.Yllcorner; result.Bits = srcData.Bits; result.Data = levelData; fs = new FileStream(Path.Combine(dir, fn + "_" + level++.ToString() + ".tdmp"), FileMode.OpenOrCreate, FileAccess.Write); result.Save(fs); } while (tw > 32 && th > 32); } }
void Convert() { for (int x = 1; x < 72; x += 2) { for (int y = 1; y < 36; y += 2) { int minX = x - 2; int maxX = x + 2; int minY = y - 2; int maxY = y + 2; if (minX < 0) { minX = 71; } if (maxX > 72) { maxX = 1; } //if (minY < 0) minY = 23; //if (maxY > 72) maxY = 1; string[] files = new string[9]; files[0] = Path.Combine(srcDir, "tile_" + minX.ToString("D2") + "_" + minY.ToString("D2") + ".tdmp"); files[1] = Path.Combine(srcDir, "tile_" + x.ToString("D2") + "_" + minY.ToString("D2") + ".tdmp"); files[2] = Path.Combine(srcDir, "tile_" + maxX.ToString("D2") + "_" + minY.ToString("D2") + ".tdmp"); files[3] = Path.Combine(srcDir, "tile_" + minX.ToString("D2") + "_" + y.ToString("D2") + ".tdmp"); files[4] = Path.Combine(srcDir, "tile_" + x.ToString("D2") + "_" + y.ToString("D2") + ".tdmp"); files[5] = Path.Combine(srcDir, "tile_" + maxX.ToString("D2") + "_" + y.ToString("D2") + ".tdmp"); files[6] = Path.Combine(srcDir, "tile_" + minX.ToString("D2") + "_" + maxY.ToString("D2") + ".tdmp"); files[7] = Path.Combine(srcDir, "tile_" + x.ToString("D2") + "_" + maxY.ToString("D2") + ".tdmp"); files[8] = Path.Combine(srcDir, "tile_" + maxX.ToString("D2") + "_" + maxY.ToString("D2") + ".tdmp"); bool[] exists = new bool[9]; bool passed = false; for (int i = 0; i < files.Length; i++) { exists[i] = File.Exists(files[i]); passed |= exists[i]; } passed &= exists[4]; if (passed) { int width = 0; int height = 0; int bits = 32; bool parsed = false; TDMPIO[] dataBlocks = new TDMPIO[9]; for (int i = 0; i < 9; i++) { if (exists[i]) { TDMPIO d1 = new TDMPIO(); d1.Load(new DevFileLocation(files[i])); if (!parsed) { width = d1.Width; height = d1.Height; bits = d1.Bits; parsed = true; } dataBlocks[i] = d1; } } float[] original = dataBlocks[4].Data; float[] sampleSrc1 = dataBlocks[0] == null ? null : BathymetrySlopSampleData(dataBlocks[0].Data); float[] sampleSrc2 = dataBlocks[1] == null ? null : BathymetrySlopSampleData(dataBlocks[1].Data); float[] sampleSrc3 = dataBlocks[2] == null ? null : BathymetrySlopSampleData(dataBlocks[2].Data); float[] sampleSrc4 = dataBlocks[3] == null ? null : BathymetrySlopSampleData(dataBlocks[3].Data); float[] sampleSrc5 = dataBlocks[4] == null ? null : BathymetrySlopSampleData(dataBlocks[4].Data); float[] sampleSrc6 = dataBlocks[5] == null ? null : BathymetrySlopSampleData(dataBlocks[5].Data); float[] sampleSrc7 = dataBlocks[6] == null ? null : BathymetrySlopSampleData(dataBlocks[6].Data); float[] sampleSrc8 = dataBlocks[7] == null ? null : BathymetrySlopSampleData(dataBlocks[7].Data); float[] sampleSrc9 = dataBlocks[8] == null ? null : BathymetrySlopSampleData(dataBlocks[8].Data); DataGetter dg = new DataGetter( dataBlocks[0] == null ? null : sampleSrc1, dataBlocks[1] == null ? null : sampleSrc2, dataBlocks[2] == null ? null : sampleSrc3, dataBlocks[3] == null ? null : sampleSrc4, dataBlocks[4] == null ? null : sampleSrc5, dataBlocks[5] == null ? null : sampleSrc6, dataBlocks[6] == null ? null : sampleSrc7, dataBlocks[7] == null ? null : sampleSrc8, dataBlocks[8] == null ? null : sampleSrc9, width, height); #region 高斯模糊 int brushSize = 65; float[] databy = new float[width * height]; float[] weights = MathEx.ComputeGuassFilter1D((float)Math.Sqrt(1 * brushSize), brushSize); #region Normalize float maxValue = 0; for (int i = 0; i < brushSize; i++) { maxValue += weights[i]; } if (maxValue != 0) { float scale = 1.0f / maxValue; for (int i = 0; i < brushSize; i++) { weights[i] *= scale; } } #endregion int mid = brushSize / 2; #region Y方向模糊 for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { int idx = i * width + j; if (original[idx] < 1600) { float val = 0; for (int ii = 0; ii < brushSize; ii++) { val += weights[ii] * dg[i + ii - mid + width, j + height]; } databy[idx] = MathEx.Clamp(0, 1600, val); } else { databy[idx] = original[idx]; } } } #endregion float[] databx = new float[width * height]; dg.MainData = databy; #region X方向模糊 for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { int idx = i * width + j; if (original[idx] < 1600) { float val = 0; for (int ii = 0; ii < brushSize; ii++) { val += weights[ii] * dg[i + width, j + ii - mid + height]; } databx[idx] = MathEx.Clamp(0, 1600, val); } else { databx[idx] = original[idx]; } } } #endregion #endregion string fileName = Path.Combine(dstDir, Path.GetFileName(files[4])); string fileNameImg = Path.Combine(dstDir, Path.GetFileName(files[4]) + "s.png"); string fileNameImg2 = Path.Combine(dstDir, Path.GetFileName(files[4]) + "d.png"); TDMPIO result = new TDMPIO(); result.Data = databx; result.Bits = 16; result.Height = height; result.Width = width; result.XSpan = dataBlocks[4].XSpan; result.YSpan = dataBlocks[4].YSpan; result.Xllcorner = dataBlocks[4].Xllcorner; result.Yllcorner = dataBlocks[4].Yllcorner; result.Save(File.Open(fileName, FileMode.OpenOrCreate, FileAccess.Write)); #region 预览图 for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { int idx = i * width + j; original[idx] /= 7000; databx[idx] /= 7000; } } OutPng(databx, width, height, fileNameImg); OutPng(original, width, height, fileNameImg2); #endregion } } } }
static void SrtmBath(string srcDir, string bathy) { const string tmpDir = @"E:\Desktop\tmp\"; const int bathWidth = 18433; const int bathHeight = 9217; byte[] bathyData; BinaryReader br = new BinaryReader(File.Open(bathy, FileMode.Open)); bathyData = br.ReadBytes(bathWidth * bathHeight); br.Close(); for (int x = 1; x < 72; x += 2) { for (int y = 1; y < 36; y += 2) { string file = Path.Combine(srcDir, "tile_" + x.ToString("D2") + "_" + y.ToString("D2") + "_0" + ".tdmp"); if (File.Exists(file)) { string file2 = Path.Combine(tmpDir, "tile_" + x.ToString("D2") + "_" + (y + 6).ToString("D2") + "_0" + ".tdmp"); File.Copy(file, file2); } } } for (int x = 1; x < 72; x += 2) { for (int y = 1; y < 36; y += 2) { string file = Path.Combine(tmpDir, "tile_" + x.ToString("D2") + "_" + y.ToString("D2") + "_0" + ".tdmp"); int startX = (x - 1) * 256; int startY = (y - 1) * 256;// +1536; if (y > 3 && y < 33) { if (File.Exists(file)) { TDMPIO d1 = new TDMPIO(); d1.Load(new DevFileLocation(file)); d1.XSpan *= 2; d1.YSpan *= 2; PlanetEarth.TileCoord2CoordNew(x, y, out d1.Xllcorner, out d1.Yllcorner); float[] data = d1.Data; for (int i = 0; i < d1.Height; i++) { for (int j = 0; j < d1.Width; j++) { int idx = i * d1.Height + j; data[idx] *= 5000; data[idx] += 1500; data[idx] -= (0xff - bathyData[(startY + i) * bathWidth + startX + j]) * (1500f / 256f); //data[idx] /= 7000; } } Stream sout = File.Open(Path.Combine(@"E:\Desktop\out\", Path.GetFileNameWithoutExtension(file) + ".tdmp"), FileMode.OpenOrCreate); d1.Save(sout); for (int i = 0; i < d1.Height; i++) { for (int j = 0; j < d1.Width; j++) { int idx = i * d1.Height + j; data[idx] /= 7000; } } TDmpBlur.OutPng(data, d1.Width, d1.Height, Path.Combine(@"E:\Desktop\out\", Path.GetFileNameWithoutExtension(file) + ".png")); } else { TDMPIO d2 = new TDMPIO(); d2.Width = 513; d2.Height = 513; d2.XSpan = 10; d2.YSpan = 10; d2.Bits = 16; PlanetEarth.TileCoord2CoordNew(x, y, out d2.Xllcorner, out d2.Yllcorner); d2.Data = new float[513 * 513]; for (int i = 0; i < d2.Height; i++) { for (int j = 0; j < d2.Width; j++) { int idx = i * d2.Height + j; d2.Data[idx] = 1600; d2.Data[idx] -= (0xff - bathyData[(startY + i) * bathWidth + startX + j]) * (1500f / 256f); } } Stream sout = File.Open(Path.Combine(@"E:\Desktop\out\", Path.GetFileNameWithoutExtension(file) + ".tdmp"), FileMode.OpenOrCreate); d2.Save(sout); for (int i = 0; i < d2.Height; i++) { for (int j = 0; j < d2.Width; j++) { int idx = i * d2.Height + j; d2.Data[idx] /= 7000; } } TDmpBlur.OutPng(d2.Data, d2.Width, d2.Height, Path.Combine(@"E:\Desktop\out\", Path.GetFileNameWithoutExtension(file) + ".png")); } } } } }
static void Convert(string srcDir, string dstDir, string suffix) { for (int x = 1; x < 72; x += 2) { for (int y = 1; y < 36; y += 2) { int minX = x - 2; int maxX = x + 2; int minY = y - 2; int maxY = y + 2; if (minX < 0) { minX = 71; } if (maxX > 72) { maxX = 1; } //if (minY < 0) minY = 23; //if (maxY > 72) maxY = 1; string[] files = new string[9]; files[0] = Path.Combine(srcDir, "tile_" + minX.ToString("D2") + "_" + minY.ToString("D2") + suffix + ".tdmp"); files[1] = Path.Combine(srcDir, "tile_" + x.ToString("D2") + "_" + minY.ToString("D2") + suffix + ".tdmp"); files[2] = Path.Combine(srcDir, "tile_" + maxX.ToString("D2") + "_" + minY.ToString("D2") + suffix + ".tdmp"); files[3] = Path.Combine(srcDir, "tile_" + minX.ToString("D2") + "_" + y.ToString("D2") + suffix + ".tdmp"); files[4] = Path.Combine(srcDir, "tile_" + x.ToString("D2") + "_" + y.ToString("D2") + suffix + ".tdmp"); files[5] = Path.Combine(srcDir, "tile_" + maxX.ToString("D2") + "_" + y.ToString("D2") + suffix + ".tdmp"); files[6] = Path.Combine(srcDir, "tile_" + minX.ToString("D2") + "_" + maxY.ToString("D2") + suffix + ".tdmp"); files[7] = Path.Combine(srcDir, "tile_" + x.ToString("D2") + "_" + maxY.ToString("D2") + suffix + ".tdmp"); files[8] = Path.Combine(srcDir, "tile_" + maxX.ToString("D2") + "_" + maxY.ToString("D2") + suffix + ".tdmp"); bool[] exists = new bool[9]; bool passed = false; for (int i = 0; i < files.Length; i++) { exists[i] = File.Exists(files[i]); passed |= exists[i]; } passed &= exists[4]; if (passed) { int width = 0; int height = 0; int bits = 32; bool parsed = false; TDMPIO[] dataBlocks = new TDMPIO[9]; for (int i = 0; i < 9; i++) { if (exists[i]) { TDMPIO d1 = new TDMPIO(); d1.Load(new DevFileLocation(files[i])); if (!parsed) { width = d1.Width; height = d1.Height; bits = d1.Bits; parsed = true; } dataBlocks[i] = d1; } } float[] databy = new float[width * height]; DataGetter dg = new DataGetter( dataBlocks[0] == null ? null : dataBlocks[0].Data, dataBlocks[1] == null ? null : dataBlocks[1].Data, dataBlocks[2] == null ? null : dataBlocks[2].Data, dataBlocks[3] == null ? null : dataBlocks[3].Data, dataBlocks[4] == null ? null : dataBlocks[4].Data, dataBlocks[5] == null ? null : dataBlocks[5].Data, dataBlocks[6] == null ? null : dataBlocks[6].Data, dataBlocks[7] == null ? null : dataBlocks[7].Data, dataBlocks[8] == null ? null : dataBlocks[8].Data, width, height); string fileName = Path.Combine(dstDir, Path.GetFileName(files[4])); TDMPIO result = new TDMPIO(); result.Bits = 16; result.Height = height; result.Width = width; result.XSpan = dataBlocks[4].XSpan; result.YSpan = dataBlocks[4].YSpan; result.Xllcorner = dataBlocks[4].Xllcorner; result.Yllcorner = dataBlocks[4].Yllcorner; result.Data = new float[width * height]; int[] weightMap = new int[width * height]; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { result.Data[i * width + j] = dg[i + height, j + width]; weightMap[i * width + j] = 1; } } for (int i = 0; i < height; i++) { if (dg.HasValue(i + height, width - 1)) { result.Data[i * width] += dg[i + height, width - 1]; weightMap[i * width]++; } if (dg.HasValue(i + height, 2 * width)) { result.Data[i * width + height - 1] += dg[i + height, 2 * width]; weightMap[i * width + height - 1]++; } } for (int j = 0; j < width; j++) { if (dg.HasValue(height - 1, j + width)) { result.Data[j] += dg[height - 1, j + width]; weightMap[j]++; } if (dg.HasValue(2 * height, j + width)) { result.Data[width * (height - 1) + j] += dg[2 * height, j + width]; weightMap[width * (height - 1) + j]++; } } //result.Data[0] += dg[width - 1, height - 1]; //weightMap[0]++; //result.Data[width - 1] += dg[2 * width, height - 1]; //weightMap[width - 1]++; //result.Data[width * (height - 1)] += dg[width - 1, 2 * height]; //weightMap[width * (height - 1) - 1]++; //result.Data[width * height - 1] += dg[2 * width, 2 * height]; //weightMap[width * height - 1]++; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { int idx = i * width + j; result.Data[idx] /= (float)weightMap[idx]; } } result.Save(File.Open(fileName, FileMode.OpenOrCreate, FileAccess.Write)); Console.Write('#'); } } } }
void Convert() { for (int x = 1; x < 80; x += 2) { for (int y = 1; y < 36; y += 2) { string file1 = Path.Combine(srcDir, "tile_" + x.ToString("D2") + "_" + y.ToString("D2") + ".tdmp"); string file2 = Path.Combine(srcDir, "tile_" + (x + 1).ToString("D2") + "_" + y.ToString("D2") + ".tdmp"); string file3 = Path.Combine(srcDir, "tile_" + x.ToString("D2") + "_" + (y + 1).ToString("D2") + ".tdmp"); string file4 = Path.Combine(srcDir, "tile_" + (x + 1).ToString("D2") + "_" + (y + 1).ToString("D2") + ".tdmp"); bool ex1 = File.Exists(file1); bool ex2 = File.Exists(file2); bool ex3 = File.Exists(file3); bool ex4 = File.Exists(file4); if (ex1 || ex2 || ex3 || ex4) { int width = 0; int height = 0; float xSpan = 0; float ySpan = 0; float xllCorner = 0; float yllCorner = 0; int bits = 32; bool parsed = false; TDMPIO d1 = new TDMPIO(); if (ex1) { d1.Load(new DevFileLocation(file1)); if (!parsed) { width = d1.Width; height = d1.Height; xSpan = d1.XSpan; ySpan = d1.YSpan; xllCorner = d1.Xllcorner; yllCorner = d1.Yllcorner; bits = d1.Bits; } } TDMPIO d2 = new TDMPIO(); if (ex2) { d2.Load(new DevFileLocation(file2)); if (!parsed) { width = d2.Width; height = d2.Height; xSpan = d2.XSpan; ySpan = d2.YSpan; xllCorner = d2.Xllcorner - xSpan; yllCorner = d2.Yllcorner; bits = d2.Bits; } } TDMPIO d3 = new TDMPIO(); if (ex3) { d3.Load(new DevFileLocation(file3)); if (!parsed) { width = d3.Width; height = d3.Height; xSpan = d3.XSpan; ySpan = d3.YSpan; xllCorner = d3.Xllcorner; yllCorner = d3.Yllcorner - ySpan; bits = d3.Bits; } } TDMPIO d4 = new TDMPIO(); if (ex4) { d4.Load(new DevFileLocation(file4)); if (!parsed) { width = d4.Width; height = d4.Height; xSpan = d4.XSpan; ySpan = d4.YSpan; xllCorner = d4.Xllcorner - xSpan; yllCorner = d4.Yllcorner - ySpan; bits = d4.Bits; } } TDMPIO data = new TDMPIO(); data.Data = new float[width * height * 4]; data.Width = width * 2; data.Height = height * 2; data.Xllcorner = xllCorner; data.Yllcorner = yllCorner; data.XSpan = xSpan * 2; data.YSpan = ySpan * 2; data.Bits = bits; DataGetter dg = new DataGetter(d1.Data, d2.Data, d3.Data, d4.Data, width, height); for (int i = 0; i < data.Height; i++) { for (int j = 0; j < data.Width; j++) { data.Data[i * data.Width + j] = dg[i, j]; } } string outPath = Path.Combine(dstDir, "tile_" + x.ToString("D2") + "_" + y.ToString("D2") + ".tdmp"); FileStream fs = new FileStream(outPath, FileMode.OpenOrCreate, FileAccess.Write); data.Save(fs); } } } }
public override void Convert(ResourceLocation source, ResourceLocation dest) { int resSize = Parameters.RescaleSize; StreamReader sr = new StreamReader(source.GetStream, Encoding.Default); int width = -1; int height = -1; int nodVal = NoValue; float cellSize = 0.00083333333333333f; float xllcorner = 0; float yllcorner = 0; int[][] heightMap = null; #region 读取SRTM int row = 0; char[] sep = new char[] { ' ' }; string[] emptyV = new string[1] { string.Empty }; List <char> charLine = new List <char>(24000); bool beginData = false; while (!sr.EndOfStream) { string line = sr.ReadLine(); string[] v; if (beginData) { v = emptyV; } else { v = line.Split(sep, StringSplitOptions.RemoveEmptyEntries); } switch (v[0]) { case "ncols": width = int.Parse(v[1]); break; case "nrows": height = int.Parse(v[1]); heightMap = new int[height][]; for (int i = 0; i < height; i++) { heightMap[i] = new int[width]; } break; case "xllcorner": xllcorner = float.Parse(v[1]); break; case "yllcorner": yllcorner = float.Parse(v[1]); break; case "cellsize": cellSize = float.Parse(v[1]); break; case "NODATA_value": nodVal = int.Parse(v[1]); if (ProgressCBK != null) { ProgressCBK.Invoke(0, width + 100); } beginData = true; break; default: Split(line, heightMap[row], nodVal); //for (int i = 0; i < width; i++) //{ // heightMap[row][i] = int.Parse(v[i]); // if (heightMap[row][i] == nodVal) // { // heightMap[row][i] = 0; // } // else // { // heightMap[row][i] += (int)Parameters.ZeroLevel; // } //} if (ProgressCBK != null) { ProgressCBK.Invoke(row, width + 100); } row++; break; } } sr.Close(); if (ProgressCBK != null) { ProgressCBK.Invoke(width, width + 100); } #endregion float invHeightScale = 1.0f / Parameters.HeightScale; //Bitmap bmp = new Bitmap(Parameters.RescaleSize, Parameters.RescaleSize); //BitmapData bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, Parameters.RescaleSize, Parameters.RescaleSize), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); //ColorValue* bdst = (ColorValue*)bmpData.Scan0; float[] demData = new float[Parameters.RescaleSize * Parameters.RescaleSize]; #region 次卷积 float wzoom = width / (float)resSize; float hzoom = height / (float)resSize; int[] buffer = new int[16]; float[] afu = new float[4]; float[] afv = new float[4]; for (int i = 0; i < resSize; i++) { float srcy = (i + 0.5f) * hzoom - 0.5f; int y0 = (int)srcy; if (y0 > srcy) { --y0; } for (int j = 0; j < resSize; j++) { float srcx = (j + 0.5f) * wzoom - 0.5f; int x0 = (int)srcx; if (x0 > srcx) { --x0; } float fv = srcx - x0; float fu = srcy - y0; for (int ii = 0; ii < 4; ii++) { for (int jj = 0; jj < 4; jj++) { int x = x0 + jj - 1; int y = y0 + ii - 1; if (x < 0) { x = 0; } if (y < 0) { y = 0; } if (x >= width) { x = width - 1; } if (y >= height) { y = height - 1; } buffer[ii * 4 + jj] = heightMap[y][x]; } } afu[0] = MathEx.Sinc(1 + fu); afu[1] = MathEx.Sinc(fu); afu[2] = MathEx.Sinc(1 - fu); afu[3] = MathEx.Sinc(2 - fu); afv[0] = MathEx.Sinc(1 + fv); afv[1] = MathEx.Sinc(fv); afv[2] = MathEx.Sinc(1 - fv); afv[3] = MathEx.Sinc(2 - fv); float s = 0; for (int ii = 0; ii < 4; ii++) { float a = 0; for (int jj = 0; jj < 4; jj++) { a += afu[jj] * buffer[ii * 4 + jj]; } s += a * afv[ii]; } demData[i * Parameters.RescaleSize + j] = s * invHeightScale; //byte c = (byte)(255 * s * invHeightScale); //bdst[i * Parameters.RescaleSize + j] = new ColorValue(c, c, c); } } if (ProgressCBK != null) { ProgressCBK.Invoke(width + 100, width + 100); } //bmp.UnlockBits(bmpData); //bmp.Save(@"E:\Desktop\sss.png", ImageFormat.Png); //bmp.Dispose(); #endregion TDMPIO result = new TDMPIO(); result.Xllcorner = xllcorner; result.Yllcorner = yllcorner; result.Width = resSize; result.Height = resSize; result.XSpan = 5; result.YSpan = 5; result.Bits = 32; result.Data = demData; result.Save(dest.GetStream); }
public override void Convert(ResourceLocation source, ResourceLocation dest) { int resSize = Parameters.RescaleSize; StreamReader sr = new StreamReader(source.GetStream, Encoding.Default); int width = -1; int height = -1; int nodVal = NoValue; float cellSize = 0.00083333333333333f; float xllcorner = 0; float yllcorner = 0; int[][] heightMap = null; #region 读取SRTM int row = 0; char[] sep = new char[] { ' ' }; string[] emptyV = new string[1] { string.Empty }; List<char> charLine = new List<char>(24000); bool beginData = false; while (!sr.EndOfStream) { string line = sr.ReadLine(); string[] v; if (beginData) { v = emptyV; } else { v = line.Split(sep, StringSplitOptions.RemoveEmptyEntries); } switch (v[0]) { case "ncols": width = int.Parse(v[1]); break; case "nrows": height = int.Parse(v[1]); heightMap = new int[height][]; for (int i = 0; i < height; i++) { heightMap[i] = new int[width]; } break; case "xllcorner": xllcorner = float.Parse(v[1]); break; case "yllcorner": yllcorner = float.Parse(v[1]); break; case "cellsize": cellSize = float.Parse(v[1]); break; case "NODATA_value": nodVal = int.Parse(v[1]); if (ProgressCBK != null) { ProgressCBK.Invoke(0, width + 100); } beginData = true; break; default: Split(line, heightMap[row], nodVal); //for (int i = 0; i < width; i++) //{ // heightMap[row][i] = int.Parse(v[i]); // if (heightMap[row][i] == nodVal) // { // heightMap[row][i] = 0; // } // else // { // heightMap[row][i] += (int)Parameters.ZeroLevel; // } //} if (ProgressCBK != null) { ProgressCBK.Invoke(row, width + 100); } row++; break; } } sr.Close(); if (ProgressCBK != null) { ProgressCBK.Invoke(width, width + 100); } #endregion float invHeightScale = 1.0f / Parameters.HeightScale; //Bitmap bmp = new Bitmap(Parameters.RescaleSize, Parameters.RescaleSize); //BitmapData bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, Parameters.RescaleSize, Parameters.RescaleSize), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); //ColorValue* bdst = (ColorValue*)bmpData.Scan0; float[] demData = new float[Parameters.RescaleSize * Parameters.RescaleSize]; #region 三次卷积 float wzoom = width / (float)resSize; float hzoom = height / (float)resSize; int[] buffer = new int[16]; float[] afu = new float[4]; float[] afv = new float[4]; for (int i = 0; i < resSize; i++) { float srcy = (i + 0.5f) * hzoom - 0.5f; int y0 = (int)srcy; if (y0 > srcy) --y0; for (int j = 0; j < resSize; j++) { float srcx = (j + 0.5f) * wzoom - 0.5f; int x0 = (int)srcx; if (x0 > srcx) --x0; float fv = srcx - x0; float fu = srcy - y0; for (int ii = 0; ii < 4; ii++) { for (int jj = 0; jj < 4; jj++) { int x = x0 + jj - 1; int y = y0 + ii - 1; if (x < 0) x = 0; if (y < 0) y = 0; if (x >= width) x = width - 1; if (y >= height) y = height - 1; buffer[ii * 4 + jj] = heightMap[y][x]; } } afu[0] = MathEx.Sinc(1 + fu); afu[1] = MathEx.Sinc(fu); afu[2] = MathEx.Sinc(1 - fu); afu[3] = MathEx.Sinc(2 - fu); afv[0] = MathEx.Sinc(1 + fv); afv[1] = MathEx.Sinc(fv); afv[2] = MathEx.Sinc(1 - fv); afv[3] = MathEx.Sinc(2 - fv); float s = 0; for (int ii = 0; ii < 4; ii++) { float a = 0; for (int jj = 0; jj < 4; jj++) { a += afu[jj] * buffer[ii * 4 + jj]; } s += a * afv[ii]; } demData[i * Parameters.RescaleSize + j] = s * invHeightScale; //byte c = (byte)(255 * s * invHeightScale); //bdst[i * Parameters.RescaleSize + j] = new ColorValue(c, c, c); } } if (ProgressCBK != null) { ProgressCBK.Invoke(width + 100, width + 100); } //bmp.UnlockBits(bmpData); //bmp.Save(@"E:\Desktop\sss.png", ImageFormat.Png); //bmp.Dispose(); #endregion TDMPIO result = new TDMPIO(); result.Xllcorner = xllcorner; result.Yllcorner = yllcorner; result.Width = resSize; result.Height = resSize; result.XSpan = 5; result.YSpan = 5; result.Bits = 32; result.Data = demData; result.Save(dest.GetStream); }
static void ElevationBias() { const string SrcDir = @"E:\Documents\ic10gd\Source\Code2015\bin\x86\Debug\terrain.lpk"; const string OutDir = @"E:\Desktop\out"; for (int x = 1; x < 72; x += 2) { for (int y = 1; y < 36; y += 2) { string file = Path.Combine(SrcDir, "tile_" + x.ToString("D2") + "_" + y.ToString("D2") + "_0" + ".tdmp"); string file2 = Path.Combine(OutDir, "tile_" + x.ToString("D2") + "_" + y.ToString("D2") + ".tdmp"); if (File.Exists(file)) { TDMPIO d1 = new TDMPIO(); d1.Load(new DevFileLocation(file)); for (int i = 0; i < d1.Height; i++) { for (int j = 0; j < d1.Width; j++) { int idx = i * d1.Height + j; d1.Data[idx] += 45; ; } } Stream sout = File.Open(file2, FileMode.OpenOrCreate); d1.Save(sout); } } } }
static void Convert(string srcDir, string dstDir, string suffix) { for (int x = 1; x < 72; x += 2) { for (int y = 1; y < 36; y += 2) { int minX = x - 2; int maxX = x + 2; int minY = y - 2; int maxY = y + 2; if (minX < 0) minX = 71; if (maxX > 72) maxX = 1; //if (minY < 0) minY = 23; //if (maxY > 72) maxY = 1; string[] files = new string[9]; files[0] = Path.Combine(srcDir, "tile_" + minX.ToString("D2") + "_" + minY.ToString("D2") + suffix + ".tdmp"); files[1] = Path.Combine(srcDir, "tile_" + x.ToString("D2") + "_" + minY.ToString("D2") + suffix + ".tdmp"); files[2] = Path.Combine(srcDir, "tile_" + maxX.ToString("D2") + "_" + minY.ToString("D2") + suffix + ".tdmp"); files[3] = Path.Combine(srcDir, "tile_" + minX.ToString("D2") + "_" + y.ToString("D2") + suffix + ".tdmp"); files[4] = Path.Combine(srcDir, "tile_" + x.ToString("D2") + "_" + y.ToString("D2") + suffix + ".tdmp"); files[5] = Path.Combine(srcDir, "tile_" + maxX.ToString("D2") + "_" + y.ToString("D2") + suffix + ".tdmp"); files[6] = Path.Combine(srcDir, "tile_" + minX.ToString("D2") + "_" + maxY.ToString("D2") + suffix + ".tdmp"); files[7] = Path.Combine(srcDir, "tile_" + x.ToString("D2") + "_" + maxY.ToString("D2") + suffix + ".tdmp"); files[8] = Path.Combine(srcDir, "tile_" + maxX.ToString("D2") + "_" + maxY.ToString("D2") + suffix + ".tdmp"); bool[] exists = new bool[9]; bool passed = false; for (int i = 0; i < files.Length; i++) { exists[i] = File.Exists(files[i]); passed |= exists[i]; } passed &= exists[4]; if (passed) { int width = 0; int height = 0; int bits = 32; bool parsed = false; TDMPIO[] dataBlocks = new TDMPIO[9]; for (int i = 0; i < 9; i++) { if (exists[i]) { TDMPIO d1 = new TDMPIO(); d1.Load(new DevFileLocation(files[i])); if (!parsed) { width = d1.Width; height = d1.Height; bits = d1.Bits; parsed = true; } dataBlocks[i] = d1; } } float[] databy = new float[width * height]; DataGetter dg = new DataGetter( dataBlocks[0] == null ? null : dataBlocks[0].Data, dataBlocks[1] == null ? null : dataBlocks[1].Data, dataBlocks[2] == null ? null : dataBlocks[2].Data, dataBlocks[3] == null ? null : dataBlocks[3].Data, dataBlocks[4] == null ? null : dataBlocks[4].Data, dataBlocks[5] == null ? null : dataBlocks[5].Data, dataBlocks[6] == null ? null : dataBlocks[6].Data, dataBlocks[7] == null ? null : dataBlocks[7].Data, dataBlocks[8] == null ? null : dataBlocks[8].Data, width, height); string fileName = Path.Combine(dstDir, Path.GetFileName(files[4])); TDMPIO result = new TDMPIO(); result.Bits = 16; result.Height = height; result.Width = width; result.XSpan = dataBlocks[4].XSpan; result.YSpan = dataBlocks[4].YSpan; result.Xllcorner = dataBlocks[4].Xllcorner; result.Yllcorner = dataBlocks[4].Yllcorner; result.Data = new float[width * height]; int[] weightMap = new int[width * height]; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { result.Data[i * width + j] = dg[i + height, j + width]; weightMap[i * width + j] = 1; } } for (int i = 0; i < height; i++) { if (dg.HasValue(i + height, width - 1)) { result.Data[i * width] += dg[i + height, width - 1]; weightMap[i * width]++; } if (dg.HasValue(i + height, 2 * width)) { result.Data[i * width + height - 1] += dg[i + height, 2 * width]; weightMap[i * width + height - 1]++; } } for (int j = 0; j < width; j++) { if (dg.HasValue(height - 1, j + width)) { result.Data[j] += dg[height - 1, j + width]; weightMap[j]++; } if (dg.HasValue(2 * height, j + width)) { result.Data[width * (height - 1) + j] += dg[2 * height, j + width]; weightMap[width * (height - 1) + j]++; } } //result.Data[0] += dg[width - 1, height - 1]; //weightMap[0]++; //result.Data[width - 1] += dg[2 * width, height - 1]; //weightMap[width - 1]++; //result.Data[width * (height - 1)] += dg[width - 1, 2 * height]; //weightMap[width * (height - 1) - 1]++; //result.Data[width * height - 1] += dg[2 * width, 2 * height]; //weightMap[width * height - 1]++; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { int idx = i * width + j; result.Data[idx] /= (float)weightMap[idx]; } } result.Save(File.Open(fileName, FileMode.OpenOrCreate, FileAccess.Write)); Console.Write('#'); } } } }
void Convert() { for (int x = 1; x < 72; x += 2) { for (int y = 1; y < 36; y += 2) { int minX = x - 2; int maxX = x + 2; int minY = y - 2; int maxY = y + 2; if (minX < 0) minX = 71; if (maxX > 72) maxX = 1; //if (minY < 0) minY = 23; //if (maxY > 72) maxY = 1; string[] files = new string[9]; files[0] = Path.Combine(srcDir, "tile_" + minX.ToString("D2") + "_" + minY.ToString("D2") + ".tdmp"); files[1] = Path.Combine(srcDir, "tile_" + x.ToString("D2") + "_" + minY.ToString("D2") + ".tdmp"); files[2] = Path.Combine(srcDir, "tile_" + maxX.ToString("D2") + "_" + minY.ToString("D2") + ".tdmp"); files[3] = Path.Combine(srcDir, "tile_" + minX.ToString("D2") + "_" + y.ToString("D2") + ".tdmp"); files[4] = Path.Combine(srcDir, "tile_" + x.ToString("D2") + "_" + y.ToString("D2") + ".tdmp"); files[5] = Path.Combine(srcDir, "tile_" + maxX.ToString("D2") + "_" + y.ToString("D2") + ".tdmp"); files[6] = Path.Combine(srcDir, "tile_" + minX.ToString("D2") + "_" + maxY.ToString("D2") + ".tdmp"); files[7] = Path.Combine(srcDir, "tile_" + x.ToString("D2") + "_" + maxY.ToString("D2") + ".tdmp"); files[8] = Path.Combine(srcDir, "tile_" + maxX.ToString("D2") + "_" + maxY.ToString("D2") + ".tdmp"); bool[] exists = new bool[9]; bool passed = false; for (int i = 0; i < files.Length; i++) { exists[i] = File.Exists(files[i]); passed |= exists[i]; } passed &= exists[4]; if (passed) { int width = 0; int height = 0; int bits = 32; bool parsed = false; TDMPIO[] dataBlocks = new TDMPIO[9]; for (int i = 0; i < 9; i++) { if (exists[i]) { TDMPIO d1 = new TDMPIO(); d1.Load(new DevFileLocation(files[i])); if (!parsed) { width = d1.Width; height = d1.Height; bits = d1.Bits; parsed = true; } dataBlocks[i] = d1; } } float[] original = dataBlocks[4].Data; float[] sampleSrc1 = dataBlocks[0] == null ? null : BathymetrySlopSampleData(dataBlocks[0].Data); float[] sampleSrc2 = dataBlocks[1] == null ? null : BathymetrySlopSampleData(dataBlocks[1].Data); float[] sampleSrc3 = dataBlocks[2] == null ? null : BathymetrySlopSampleData(dataBlocks[2].Data); float[] sampleSrc4 = dataBlocks[3] == null ? null : BathymetrySlopSampleData(dataBlocks[3].Data); float[] sampleSrc5 = dataBlocks[4] == null ? null : BathymetrySlopSampleData(dataBlocks[4].Data); float[] sampleSrc6 = dataBlocks[5] == null ? null : BathymetrySlopSampleData(dataBlocks[5].Data); float[] sampleSrc7 = dataBlocks[6] == null ? null : BathymetrySlopSampleData(dataBlocks[6].Data); float[] sampleSrc8 = dataBlocks[7] == null ? null : BathymetrySlopSampleData(dataBlocks[7].Data); float[] sampleSrc9 = dataBlocks[8] == null ? null : BathymetrySlopSampleData(dataBlocks[8].Data); DataGetter dg = new DataGetter( dataBlocks[0] == null ? null : sampleSrc1, dataBlocks[1] == null ? null : sampleSrc2, dataBlocks[2] == null ? null : sampleSrc3, dataBlocks[3] == null ? null : sampleSrc4, dataBlocks[4] == null ? null : sampleSrc5, dataBlocks[5] == null ? null : sampleSrc6, dataBlocks[6] == null ? null : sampleSrc7, dataBlocks[7] == null ? null : sampleSrc8, dataBlocks[8] == null ? null : sampleSrc9, width, height); #region 高斯模糊 int brushSize = 65; float[] databy = new float[width * height]; float[] weights = MathEx.ComputeGuassFilter1D((float)Math.Sqrt(1 * brushSize), brushSize); #region Normalize float maxValue = 0; for (int i = 0; i < brushSize; i++) { maxValue += weights[i]; } if (maxValue != 0) { float scale = 1.0f / maxValue; for (int i = 0; i < brushSize; i++) { weights[i] *= scale; } } #endregion int mid = brushSize / 2; #region Y方向模糊 for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { int idx = i * width + j; if (original[idx] < 1600) { float val = 0; for (int ii = 0; ii < brushSize; ii++) { val += weights[ii] * dg[i + ii - mid + width, j + height]; } databy[idx] = MathEx.Clamp(0, 1600, val); } else databy[idx] = original[idx]; } } #endregion float[] databx = new float[width * height]; dg.MainData = databy; #region X方向模糊 for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { int idx = i * width + j; if (original[idx] < 1600) { float val = 0; for (int ii = 0; ii < brushSize; ii++) { val += weights[ii] * dg[i + width, j + ii - mid + height]; } databx[idx] = MathEx.Clamp(0, 1600, val); } else databx[idx] = original[idx]; } } #endregion #endregion string fileName = Path.Combine(dstDir, Path.GetFileName(files[4]) ); string fileNameImg = Path.Combine(dstDir, Path.GetFileName(files[4]) + "s.png"); string fileNameImg2 = Path.Combine(dstDir, Path.GetFileName(files[4]) + "d.png"); TDMPIO result = new TDMPIO(); result.Data = databx; result.Bits = 16; result.Height = height; result.Width = width; result.XSpan = dataBlocks[4].XSpan; result.YSpan = dataBlocks[4].YSpan; result.Xllcorner = dataBlocks[4].Xllcorner; result.Yllcorner = dataBlocks[4].Yllcorner; result.Save(File.Open(fileName, FileMode.OpenOrCreate, FileAccess.Write)); #region 预览图 for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { int idx = i * width + j; original[idx] /= 7000; databx[idx] /= 7000; } } OutPng(databx, width, height, fileNameImg); OutPng(original, width, height, fileNameImg2); #endregion } } } }