TopoLoader.gd 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 orientation: String = "N"
  13. var latMin = 0
  14. var latMax = 30
  15. var lonMin = 0
  16. var lonMax = 45
  17. var cache = {}
  18. var file: File
  19. # Called when the node enters the scene tree for the first time.
  20. func open(latMinIN, lonMinIn):
  21. latMin = (int(latMinIN)/30) * 30
  22. latMax = latMin+30
  23. lonMin = (int(lonMinIn)/45) * 45
  24. lonMax = lonMin+45
  25. if latMinIN >= 0:
  26. orientation = "N"
  27. else:
  28. orientation = "S"
  29. file = File.new()
  30. print("opening topo: "+filePrefix+getFilename())
  31. var err = file.open(filePrefix+getFilename(), File.READ)
  32. if err != 0:
  33. Downloader.download(getFilename(), "topo")
  34. func close():
  35. file.close()
  36. #queue_free()
  37. func clear():
  38. file.close()
  39. cache = {}
  40. func cacheArea(latMinIN, latMaxIN, lonMinIN, lonMaxIN):
  41. if not _validate(latMinIN,lonMinIN) and not _validate(latMaxIN,lonMaxIN) :
  42. print("out of scope!")
  43. return
  44. var lat = latMinIN
  45. while lat <= latMaxIN:
  46. cache[lat]={}
  47. var line = abs(lat)*resolution*samplesPerLine*4
  48. # the origin of each .img file lies in it's upper left corner
  49. # so we need to read the files from the end on the northern hemisphere
  50. if lat >= 0:
  51. var origin = latMax*resolution*samplesPerLine*4
  52. file.seek((origin - line) + lonMinIN*resolution*4)
  53. else:
  54. file.seek(line + lonMinIN*resolution*4)
  55. var lon = lonMinIN
  56. while lon <= lonMaxIN:
  57. cache[lat][lon] = file.get_float()
  58. lon += 1.0/float(resolution)
  59. lat += 1.0/float(resolution)
  60. return
  61. func read(lat,lon):
  62. if _validate(lat,lon):
  63. return cache[lat][lon]
  64. else:
  65. print("out of scope!")
  66. func _validate(lat,lon):
  67. var condition = true
  68. if abs(lat) > latMax:
  69. condition = false
  70. if abs(lat) < latMin:
  71. condition = false
  72. if lon > lonMax:
  73. condition = false
  74. if lon < lonMin:
  75. condition = false
  76. return condition
  77. #if abs(lat) <= latMax and abs(lat) >= latMin and lon <= lonMax and lon >= lonMin:
  78. # return true
  79. #return false
  80. func getFilename():
  81. # example sldem2015_512_00n_30n_000_045_float.img
  82. # "%s/sldem2015_%s_%0*d%s_%0*d%s_%00*d_%00*d_float.img"
  83. var properties
  84. if orientation == "S":
  85. properties = [resolution, latMax, orientation, latMin, orientation, lonMin, lonMax]
  86. else:
  87. properties = [resolution, latMin, orientation, latMax, orientation, lonMin, lonMax]
  88. return formatString % properties
  89. # Called every frame. 'delta' is the elapsed time since the previous frame.
  90. #func _process(delta):
  91. # pass