TopoLoader.gd 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. extends Node
  2. var Downloader = preload("res://downloader.gd")
  3. var filePrefix = "user://topo"
  4. var formatString = "%s/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: "+getFilename())
  26. file.open(getFilename(), File.READ)
  27. func close():
  28. file.close()
  29. #queue_free()
  30. func clear():
  31. file.close()
  32. cache = {}
  33. func cacheArea(latMinIN, latMaxIN, lonMinIN, lonMaxIN):
  34. if not _validate(latMinIN,lonMinIN) and not _validate(latMaxIN,lonMaxIN) :
  35. print("out of scope!")
  36. return
  37. var lat = latMinIN
  38. while lat <= latMaxIN:
  39. cache[lat]={}
  40. var line = lat*resolution*samplesPerLine*4
  41. # the origin of each .img file lies in it's upper left corner
  42. # so we need to read the files from the end on the northern hemisphere
  43. if lat >= 0:
  44. var origin = latMax*resolution*samplesPerLine*4
  45. file.seek((origin - line) + lonMinIN*resolution*4)
  46. else:
  47. file.seek(line + lonMinIN*resolution*4)
  48. var lon = lonMinIN
  49. while lon <= lonMaxIN:
  50. cache[lat][lon] = file.get_float()
  51. lon += 1.0/float(resolution)
  52. lat += 1.0/float(resolution)
  53. return
  54. func read(lat,lon):
  55. if _validate(lat,lon):
  56. return cache[lat][lon]
  57. else:
  58. print("out of scope!")
  59. func _validate(lat,lon):
  60. if lat <= latMax and lat >= latMin and lon <= lonMax and lon >= lonMin:
  61. return true
  62. return false
  63. func getFilename():
  64. # example sldem2015_512_00n_30n_000_045_float.img
  65. # "%s/sldem2015_%s_%0*d%s_%0*d%s_%00*d_%00*d_float.img"
  66. var orientation: String
  67. if latMin >= 0:
  68. orientation = "n"
  69. else:
  70. orientation = "s"
  71. var properties = [filePrefix, resolution, latMin, orientation, latMax, orientation, lonMin, lonMax]
  72. return formatString % properties
  73. # Called every frame. 'delta' is the elapsed time since the previous frame.
  74. #func _process(delta):
  75. # pass