TopoLoader.gd 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. file.seek((latMax*resolution*samplesPerLine*4 - lat*resolution*samplesPerLine*4) + lonMinIN*resolution*4)
  35. var lon = lonMinIN
  36. while lon <= lonMaxIN:
  37. cache[lat][lon] = file.get_float()
  38. lon += 1.0/float(resolution)
  39. lat += 1.0/float(resolution)
  40. return
  41. func read(lat,lon):
  42. if _validate(lat,lon):
  43. return cache[lat][lon]
  44. else:
  45. print("out of scope!")
  46. func _validate(lat,lon):
  47. if lat <= latMax and lat >= latMin and lon <= lonMax and lon >= lonMin:
  48. return true
  49. return false
  50. # Called every frame. 'delta' is the elapsed time since the previous frame.
  51. #func _process(delta):
  52. # pass