TopoLoader.gd 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. extends Node
  2. var Downloader = preload("res://downloader.gd")
  3. var filePrefix = "user://topo/"
  4. var formatString = "SLDEM2015_%s_%02d%s_%02d%s_%03d_%03d_FLOAT.IMG"
  5. # example sldem2015_512_00n_30n_000_045_float.img
  6. var datafile = "user://topo/sldem2015_512_00n_30n_000_045_float.img"
  7. var samplesPerLine = 23040
  8. var lines = 15360
  9. var offset = 1737.4*1000
  10. var scaleFact = 1
  11. var resolution = 512
  12. var latMin = 0
  13. var latMax = 30
  14. var lonMin = 0
  15. var lonMax = 45
  16. var cache = {}
  17. var file: File
  18. # Called when the node enters the scene tree for the first time.
  19. func open(latMinIN, lonMinIn):
  20. latMin = (int(latMinIN)/30) * 30
  21. latMax = latMin+30
  22. lonMin = (int(lonMinIn)/45) * 45
  23. lonMax = lonMin+45
  24. file = File.new()
  25. print("opening topo: "+filePrefix+getFilename())
  26. var err = file.open(filePrefix+getFilename(), File.READ)
  27. if err != 0:
  28. Downloader.download(getFilename(), "topo")
  29. func close():
  30. file.close()
  31. #queue_free()
  32. func clear():
  33. file.close()
  34. cache = {}
  35. func cacheArea(latMinIN, latMaxIN, lonMinIN, lonMaxIN):
  36. if not _validate(latMinIN,lonMinIN) and not _validate(latMaxIN,lonMaxIN) :
  37. print("out of scope!")
  38. return
  39. var lat = latMinIN
  40. while lat <= latMaxIN:
  41. cache[lat]={}
  42. var line = lat*resolution*samplesPerLine*4
  43. # the origin of each .img file lies in it's upper left corner
  44. # so we need to read the files from the end on the northern hemisphere
  45. if lat >= 0:
  46. var origin = latMax*resolution*samplesPerLine*4
  47. file.seek((origin - line) + lonMinIN*resolution*4)
  48. else:
  49. file.seek(line + lonMinIN*resolution*4)
  50. var lon = lonMinIN
  51. while lon <= lonMaxIN:
  52. cache[lat][lon] = file.get_float()
  53. lon += 1.0/float(resolution)
  54. lat += 1.0/float(resolution)
  55. return
  56. func read(lat,lon):
  57. if _validate(lat,lon):
  58. return cache[lat][lon]
  59. else:
  60. print("out of scope!")
  61. func _validate(lat,lon):
  62. if lat <= latMax and lat >= latMin and lon <= lonMax and lon >= lonMin:
  63. return true
  64. return false
  65. func getFilename():
  66. # example sldem2015_512_00n_30n_000_045_float.img
  67. # "%s/sldem2015_%s_%0*d%s_%0*d%s_%00*d_%00*d_float.img"
  68. var orientation: String
  69. if latMin >= 0:
  70. orientation = "N"
  71. else:
  72. orientation = "S"
  73. var properties = [resolution, latMin, orientation, latMax, orientation, lonMin, lonMax]
  74. return formatString % properties
  75. # Called every frame. 'delta' is the elapsed time since the previous frame.
  76. #func _process(delta):
  77. # pass