Advertisement
Guest User

Tokyo Bay Height Map Creator

a guest
Jan 21st, 2020
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 KB | None | 0 0
  1. // for LINQPad 5
  2.  
  3. var http = new HttpClient();
  4.  
  5. var tile_start = new int[]{1819-2,806-3};
  6. var tile_range = 10;
  7. var tile_zoom = 11;
  8. var bmp = new Bitmap(tile_range*256,tile_range*256);
  9.  
  10. for(var pixel_x = 0;pixel_x < tile_range*256;pixel_x++) {
  11. for(var pixel_y = 0;pixel_y < tile_range*256;pixel_y++) {
  12. bmp.SetPixel(pixel_x,pixel_y,Color.Black);
  13. }
  14. }
  15.  
  16. for(var count_x = 0; count_x < tile_range;count_x++) {
  17. for(var count_y = 0; count_y < tile_range;count_y++) {
  18. try{
  19. var tile_x = tile_start[0]+count_x;
  20. var tile_y = tile_start[1]+count_y;
  21. var url = $@"https://cyberjapandata.gsi.go.jp/xyz/dem_png/{tile_zoom}/{tile_x}/{tile_y}.png";
  22. var stream = await http.GetStreamAsync(url);
  23. var src_bmp = new Bitmap(stream);
  24.  
  25. for(var pixel_x = 0;pixel_x < 256;pixel_x++) {
  26. for(var pixel_y = 0;pixel_y < 256;pixel_y++) {
  27.  
  28. var h = ToHeight(src_bmp.GetPixel(pixel_x,pixel_y));
  29. bmp.SetPixel(pixel_x + count_x * 256,pixel_y + count_y * 256,ToColor(h));
  30. }
  31. }
  32. }catch{}
  33. }
  34. }
  35.  
  36. for(var count_x = 0; count_x < tile_range;count_x++) {
  37. for(var count_y = 0; count_y < tile_range;count_y++) {
  38. try{
  39. var tile_x = tile_start[0]+count_x;
  40. var tile_y = tile_start[1]+count_y;
  41. var url = $@"https://cyberjapandata.gsi.go.jp/xyz/lcm25k_2012/{tile_zoom}/{tile_x}/{tile_y}.png";
  42. var stream = await http.GetStreamAsync(url);
  43. var src_bmp = new Bitmap(stream);
  44.  
  45. for(var pixel_x = 0;pixel_x < 256;pixel_x++) {
  46. for(var pixel_y = 0;pixel_y < 256;pixel_y++) {
  47.  
  48. var color = src_bmp.GetPixel(pixel_x,pixel_y);
  49. if(color.R == 101 && color.G == 202 && color.B >= 250) {
  50.  
  51. bmp.SetPixel(pixel_x + count_x * 256,pixel_y + count_y * 256,Color.Black);
  52. }
  53.  
  54. }
  55. }
  56. }catch{}
  57. }
  58. }
  59.  
  60.  
  61. bmp.Dump();
  62.  
  63. Color ToColor(double height) {
  64.   var min = -40.0;
  65.   var max = 300;
  66.  
  67.   if(height > max/2) height = max/4 + height/2;
  68.  
  69.   var value =
  70.     (height - min) / (max - min) * 255;
  71.   if(height < 1) value = 0;
  72.  
  73.   value = Math.Max(0,Math.Min(255,value));
  74.  
  75.   return Color.FromArgb((int)value,(int)value,(int)value);
  76. }
  77.  
  78. double ToHeight(Color color) {
  79. var pow2_8 = 256;
  80. var pow2_16 = 65536;
  81. var pow2_23 = 8388608;
  82. var pow2_24 = 16777216;
  83.  
  84. var r = color.R;
  85.           var g = color.G;
  86.           var b = color.B;
  87.           var h = 0.0;
  88.           if (r != 128 || g != 0 || b != 0) {
  89.             var d = r * pow2_16 + g * pow2_8 + b;
  90.             h = (d < pow2_23) ? d : d - pow2_24;
  91.             if (h == -pow2_23) h = 0;
  92.             else h *= 0.01;
  93.             return h;
  94.           }
  95.   return -9999;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement