TopoLoader.gd 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 maxheight = 10.783
  14. var minheight = -8.719
  15. var latMin = 0
  16. var latMax = 30
  17. var lonMin = 0
  18. var lonMax = 45
  19. var cache = {}
  20. var file: File
  21. var fileReady = false
  22. onready var heightmap = Image.new()
  23. signal fileReady
  24. # Called when the node enters the scene tree for the first time.
  25. func open(latMinIN, lonMinIn):
  26. latMin = (int(latMinIN)/30) * 30
  27. if latMinIN >= 0:
  28. orientation = "N"
  29. latMax = latMin+30
  30. else:
  31. orientation = "S"
  32. latMax = latMin-30
  33. lonMin = (int(lonMinIn)/45) * 45
  34. lonMax = lonMin+45
  35. file = File.new()
  36. print("opening topo: "+getFilepath())
  37. var err = file.open(getFilepath(), File.READ)
  38. if err == 7:
  39. Downloader.connect("file_downloaded", self, "_http_request_completed")
  40. Downloader.download(getFilename(), "topo")
  41. var dl = Downloader.Download.new()
  42. while not dl._file_name == getFilepath():
  43. dl = yield(Messenger, "fileDownloaded")
  44. file.open(getFilepath(), File.READ)
  45. fileReady = true
  46. emit_signal("fileReady")
  47. return true
  48. elif err != 0:
  49. push_error("error opening file err:%s" %err)
  50. return false
  51. else:
  52. fileReady = true
  53. return true
  54. func _http_request_completed(download):
  55. fileReady = true
  56. emit_signal("fileReady")
  57. func close():
  58. file.close()
  59. #queue_free()
  60. func clear():
  61. file.close()
  62. cache = {}
  63. func cacheArea(latMinIN, latMaxIN, lonMinIN, lonMaxIN):
  64. if not _validate(latMinIN,lonMinIN) and not _validate(latMaxIN,lonMaxIN) :
  65. print("out of scope!")
  66. return
  67. if not file.is_open():
  68. var err = file.open(getFilepath(), File.READ)
  69. var lat = latMinIN
  70. while lat <= latMaxIN:
  71. cache[lat]={}
  72. var line = abs(lat)*resolution*samplesPerLine*4
  73. # the origin of each .img file lies in it's upper left corner
  74. # so we need to read the files from the end on the northern hemisphere
  75. if lat >= 0:
  76. var origin = latMax*resolution*samplesPerLine*4
  77. file.seek((origin - line) + lonMinIN*resolution*4)
  78. else:
  79. file.seek(line + lonMinIN*resolution*4)
  80. var lon = lonMinIN
  81. while lon <= lonMaxIN:
  82. cache[lat][lon] = file.get_float()
  83. lon += 1.0/float(resolution)
  84. lat += 1.0/float(resolution)
  85. return
  86. func createHeightmap():
  87. var minLat
  88. var minLon
  89. var maxLat
  90. var maxLon
  91. var idxX = 0
  92. var idxY = 0
  93. var value = 0.0
  94. heightmap.create(len(cache[cache.keys()[0]]), len(cache), false, Image.FORMAT_RF)
  95. heightmap.lock()
  96. for y in cache.keys():
  97. if not minLat:
  98. minLat = y
  99. for x in cache[y].keys():
  100. if not minLon:
  101. minLon = x
  102. value = inverse_lerp(minheight, maxheight, cache[y][x])
  103. heightmap.set_pixel(idxX, idxY, Color(value,0,0))#Color(cache[y][x],0,0))
  104. maxLon = x
  105. idxX += 1
  106. maxLat = y
  107. idxY += 1
  108. idxX = 0
  109. heightmap.unlock()
  110. var heightmapfilepath = "user://heightmap/%s:%s-%s:%s.png"%[minLat, minLon, maxLat, maxLon]
  111. heightmap.save_png(heightmapfilepath)
  112. return heightmap
  113. func read(lat,lon):
  114. if _validate(lat,lon):
  115. return cache[lat][lon]
  116. else:
  117. print("out of scope!")
  118. func _validate(lat,lon):
  119. var condition = true
  120. if abs(lat) > latMax:
  121. condition = false
  122. if abs(lat) < latMin:
  123. condition = false
  124. if lon > lonMax:
  125. condition = false
  126. if lon < lonMin:
  127. condition = false
  128. return condition
  129. #if abs(lat) <= latMax and abs(lat) >= latMin and lon <= lonMax and lon >= lonMin:
  130. # return true
  131. #return false
  132. func getFilename():
  133. # example sldem2015_512_00n_30n_000_045_float.img
  134. # "%s/sldem2015_%s_%0*d%s_%0*d%s_%00*d_%00*d_float.img"
  135. var properties
  136. if orientation == "S":
  137. properties = [resolution, abs(latMax), orientation, abs(latMin), orientation, lonMin, lonMax]
  138. else:
  139. properties = [resolution, latMin, orientation, latMax, orientation, lonMin, lonMax]
  140. return formatString % properties
  141. func getFilepath():
  142. return filePrefix+getFilename()
  143. # Called every frame. 'delta' is the elapsed time since the previous frame.
  144. #func _process(delta):
  145. # pass