TopoLoader.gd 3.3 KB

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