Guest User

position_objetcs.py

a guest
Mar 22nd, 2015
292
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2.  
  3. import sys
  4. import subprocess
  5. import math
  6.  
  7. object_type = sys.argv[1]
  8.  
  9. scenerydir='~/.fgfs/TerraSync'
  10.  
  11. elev = subprocess.Popen(['fgelev', '--fg-scenery', scenerydir], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
  12. #elev = subprocess.Popen(['tee'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
  13.  
  14. class Position():
  15. lat = ''
  16. lng = ''
  17. id = ''
  18. type = ''
  19. ele = ''
  20.  
  21. def tileWidth(lat):
  22. tiletable=[[0,0.125],[22,0.25],[62,0.5],[76,1],[83,2],[86,4],[88,8],[89,360],[90,360]]
  23. for i in range(len(tiletable)):
  24. if abs(lat)>=tiletable[i][0] and abs(lat)<tiletable[i+1][0]:
  25. return float (tiletable[i][1])
  26.  
  27.  
  28. def tileIndexFromCoordinate(lat, lon):
  29. base_y = math.floor(lat)
  30. y = int((lat-base_y)*8)
  31. tilewidth = tileWidth(lat)
  32. base_x = math.floor(math.floor(lon/tilewidth)*tilewidth)
  33. if base_x<-180:
  34. base_x=-180
  35. x = int(math.floor((lon-base_x)/tilewidth))
  36. tileindex = int(((int(math.floor(lon))+180)<<14) + ((int(math.floor(lat))+ 90) << 6) + (y << 3) + x)
  37. return tileindex
  38.  
  39. def pathFromCoordinate(lat, lon):
  40. lat = math.floor(lat)
  41. lon = math.floor(lon)
  42. lat2 = math.floor(lat/10) * 10
  43. lon2 = math.floor(lon/10) * 10
  44.  
  45. dir1 = 'n'
  46. if lat < 0:
  47. dir1 = 's'
  48. dir2 = 'e'
  49. if lon < 0:
  50. dir2 = 'w'
  51.  
  52. return '%s%03d%s%02d/%s%03d%s%02d/'%(dir2, abs(lon2), dir1, abs(lat2), dir2, abs(lon), dir1, abs(lat))
  53.  
  54. print 'Calculating elevation (this can take several minutes!)'
  55. objects = dict()
  56. for l in sys.stdin.readlines():
  57. o = Position()
  58. o.type = object_type
  59. try:
  60. o.id, o.lng, o.lat = l.strip().split()
  61. o.ele = 0
  62. objects['%s:'%o.id] = o
  63. except:
  64. print 'Bad line: %s' % l.strip()
  65.  
  66.  
  67. # Toggle to False for fast tests (0 elev)
  68. if True:
  69. outs, errs = elev.communicate('\n'.join(map(lambda o: '%s %s %s'%(o.id, o.lng, o.lat), objects.values())))
  70. print 'Done'
  71. for l in outs.split('\n'):
  72. try:
  73. id, ele = l.strip().split()
  74. if id in objects:
  75. objects[id].ele = ele
  76. else:
  77. print 'Object not found: %s'%id
  78. except:
  79. print 'Line not processed: ' + l
  80.  
  81.  
  82. tiles = dict()
  83. for o in objects.values():
  84. tileid = pathFromCoordinate(float(o.lat), float(o.lng)) + str(tileIndexFromCoordinate(float(o.lat), float(o.lng))) + '.stg'
  85. if not tiles.has_key(tileid):
  86. tiles[tileid] = list()
  87. tiles[tileid].append(o)
  88.  
  89. for t in tiles.keys():
  90. print '\n=== %s ===\n'%t
  91. for o in tiles[t]:
  92. print 'OBJECT_SHARED %s %s %s %s 0.0 0.0 0.0'%(o.type, o.lng, o.lat, o.ele)
RAW Paste Data