TopoLoader.gd 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. extends Node
  2. var Downloader = preload("res://downloader.gd")
  3. var datafile = "height/SLDEM2015_512_00N_30N_000_045_FLOAT.IMG"
  4. var samplesPerLine = 23040
  5. var lines = 15360
  6. var offset = 1737.4#*1000
  7. var scaleFact = 1
  8. var resolution = 512
  9. var latMin = 0
  10. var latMax = 30
  11. var lonMin = 0
  12. var lonMax = 45
  13. var cache = {}
  14. var file: File
  15. # Called when the node enters the scene tree for the first time.
  16. func open():
  17. file = File.new()
  18. if not file.file_exists("user://"+datafile):
  19. var downloader = Downloader.new()
  20. add_child(downloader)
  21. downloader.load(datafile, "height")
  22. yield(downloader, "request_completed")
  23. file.open("user://"+datafile, File.READ)
  24. func close():
  25. file.close()
  26. queue_free()
  27. func cacheArea(latMinIN, latMaxIN, lonMinIN, lonMaxIN):
  28. if not _validate(latMinIN,lonMinIN) and not _validate(latMaxIN,lonMaxIN) :
  29. print("out of scope!")
  30. return
  31. var lat = latMinIN
  32. while lat <= latMaxIN:
  33. cache[lat]={}
  34. var line = lat*resolution*samplesPerLine*4
  35. # the origin of each .img file lies in it's upper left corner
  36. # so we need to read the files from the end on the northern hemisphere
  37. if lat >= 0:
  38. var origin = latMax*resolution*samplesPerLine*4
  39. file.seek((origin - line) + lonMinIN*resolution*4)
  40. else:
  41. file.seek(line + lonMinIN*resolution*4)
  42. var lon = lonMinIN
  43. while lon <= lonMaxIN:
  44. cache[lat][lon] = file.get_float()
  45. lon += 1.0/float(resolution)
  46. lat += 1.0/float(resolution)
  47. return
  48. func read(lat,lon):
  49. if _validate(lat,lon):
  50. return cache[lat][lon]
  51. else:
  52. print("out of scope!")
  53. func _validate(lat,lon):
  54. if lat <= latMax and lat >= latMin and lon <= lonMax and lon >= lonMin:
  55. return true
  56. return false
  57. # Called every frame. 'delta' is the elapsed time since the previous frame.
  58. #func _process(delta):
  59. # pass