TopoLoader.gd 2.1 KB

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