|
|
@@ -5,6 +5,7 @@ import queue
|
|
|
import os
|
|
|
import zipfile
|
|
|
import shutil
|
|
|
+import sys
|
|
|
|
|
|
from flask import Flask
|
|
|
from flask import render_template
|
|
|
@@ -12,8 +13,7 @@ from flask import request
|
|
|
from flask import json, jsonify
|
|
|
|
|
|
from MAVProxy.modules.mavproxy_map import srtm
|
|
|
-
|
|
|
-from terrain_gen import makeTerrain
|
|
|
+from terrain_gen import add_offset
|
|
|
|
|
|
# The output folder for all zipped terrain requests
|
|
|
app = Flask(__name__,static_url_path='/terrain', static_folder='outputTer',)
|
|
|
@@ -21,71 +21,35 @@ app = Flask(__name__,static_url_path='/terrain', static_folder='outputTer',)
|
|
|
def clamp(n, smallest, largest):
|
|
|
return max(smallest, min(n, largest))
|
|
|
|
|
|
-class TerGenThread(threading.Thread):
|
|
|
- # This is the terrain generator. It will check the queue
|
|
|
- # for new requests.
|
|
|
- def __init__(self):
|
|
|
- threading.Thread.__init__(self)
|
|
|
- self.event = threading.Event()
|
|
|
- # SRTM downloader. Single instance passed to terrain generator
|
|
|
- self.downloader = srtm.SRTMDownloader(debug=False, cachedir='./srtmcache')
|
|
|
- self.downloader.loadFileList()
|
|
|
- self.terStats = {}
|
|
|
- self.terQ = queue.Queue()
|
|
|
-
|
|
|
- def addStatus(self, uuidkey, value):
|
|
|
- self.terStats[uuidkey] = value
|
|
|
-
|
|
|
- def run(self):
|
|
|
- print("Starting Terrain Generator")
|
|
|
- while not self.event.is_set():
|
|
|
- time.sleep(0.01)
|
|
|
- if not self.terQ.empty():
|
|
|
- (radius, lat, lon, spacing, uuidkey, outfolder) = self.terQ.get()
|
|
|
- print("Starting request: " + str(uuidkey))
|
|
|
- self.terStats[uuidkey] = "Processing"
|
|
|
- # generate terrain
|
|
|
- makeTerrain(self.downloader, radius, lat, lon, spacing, uuidkey, outfolder)
|
|
|
-
|
|
|
- self.terStats[uuidkey] = "Compressing"
|
|
|
- #compress into single file for user
|
|
|
- folderthis = os.path.join(os.getcwd(), outfolder + "-tmp", uuidkey)
|
|
|
- zipthis = os.path.join(os.getcwd(), outfolder, uuidkey + '.zip')
|
|
|
- terrain_zip = zipfile.ZipFile(zipthis, 'w')
|
|
|
-
|
|
|
- print("At Compress ")
|
|
|
- #numpercent = numpercent * 1.1
|
|
|
- for folder, subfolders, files in os.walk(folderthis):
|
|
|
- for file in files:
|
|
|
- terrain_zip.write(os.path.join(folder, file), file, compress_type = zipfile.ZIP_DEFLATED)
|
|
|
-
|
|
|
- terrain_zip.close()
|
|
|
-
|
|
|
- #remove old folder
|
|
|
- try:
|
|
|
- shutil.rmtree(folderthis)
|
|
|
- except OSError as e:
|
|
|
- print("Error: %s : %s" % (folderthis, e.strerror))
|
|
|
-
|
|
|
- print("At Done")
|
|
|
-
|
|
|
- del self.terStats[uuidkey]
|
|
|
-
|
|
|
- print("Exiting Terrain Generator")
|
|
|
-
|
|
|
-# Terrain generator checker
|
|
|
-x = TerGenThread()
|
|
|
-x.start()
|
|
|
-
|
|
|
-def queueStatus():
|
|
|
- return len(x.terStats)
|
|
|
-
|
|
|
-def wholeStat():
|
|
|
- return str(x.terStats)
|
|
|
-
|
|
|
-def shutdown():
|
|
|
- x.event.set()
|
|
|
- x.join()
|
|
|
+def getDatFile(lat, lon):
|
|
|
+ if lat < 0:
|
|
|
+ NS = 'S'
|
|
|
+ else:
|
|
|
+ NS = 'N'
|
|
|
+ if lon < 0:
|
|
|
+ EW = 'W'
|
|
|
+ else:
|
|
|
+ EW = 'E'
|
|
|
+ return "%c%02u%c%03u.DAT" % (NS, min(abs(int(lat)), 99),
|
|
|
+ EW, min(abs(int(lon)), 999))
|
|
|
+
|
|
|
+def compressFiles(fileList, uuidkey, outfolder):
|
|
|
+ # create a zip file comprised of dat tiles
|
|
|
+ zipthis = os.path.join(os.getcwd(), outfolder, uuidkey + '.zip')
|
|
|
+ terrain_zip = zipfile.ZipFile(zipthis, 'w')
|
|
|
+
|
|
|
+ try:
|
|
|
+ for fileSingle in fileList:
|
|
|
+ # terrain_zip.write(os.path.join(folder, file), file, compress_type = zipfile.ZIP_DEFLATED)
|
|
|
+ terrain_zip.write(fileSingle, os.path.basename(fileSingle), compress_type = zipfile.ZIP_DEFLATED)
|
|
|
+ except:
|
|
|
+ print("Unexpected error:", sys.exc_info()[0])
|
|
|
+ terrain_zip.close()
|
|
|
+ return False
|
|
|
+
|
|
|
+
|
|
|
+ terrain_zip.close()
|
|
|
+ return True
|
|
|
|
|
|
@app.route('/')
|
|
|
def index():
|
|
|
@@ -112,23 +76,36 @@ def generate():
|
|
|
# UUID for this terrain generation
|
|
|
uuidkey = str(uuid.uuid1())
|
|
|
|
|
|
- # Add this job to the processing queue
|
|
|
- x.terQ.put((radius, lat, lon, 100, uuidkey, 'outputTer'))
|
|
|
- #x.terStats[uuidkey] = "In Queue, pos={0}".format(terQ.qsize())
|
|
|
- x.addStatus(uuidkey, "In Queue, pos={0}".format(x.terQ.qsize()))
|
|
|
-
|
|
|
- return render_template('generate.html', urlkey="/terrain/" + uuidkey + ".zip", waittime=x.terQ.qsize()+1, uuidkey=uuidkey)
|
|
|
+ # get a list of files required to cover area
|
|
|
+ filelist = []
|
|
|
+ done = set()
|
|
|
+ for dx in range(-radius, radius):
|
|
|
+ for dy in range(-radius, radius):
|
|
|
+ (lat2,lon2) = add_offset(lat*1e7, lon*1e7, dx*1000.0, dy*1000.0)
|
|
|
+ lat_int = int(round(lat2 * 1.0e-7))
|
|
|
+ lon_int = int(round(lon2 * 1.0e-7))
|
|
|
+ tag = (lat_int, lon_int)
|
|
|
+ if tag in done:
|
|
|
+ #numpercent += 1
|
|
|
+ continue
|
|
|
+ done.add(tag)
|
|
|
+ #create_degree(downloader, lat_int, lon_int, folderthis, spacing)
|
|
|
+ filelist.append(os.path.join(os.getcwd(), "processedTerrain", getDatFile(lat_int, lon_int)))
|
|
|
+
|
|
|
+ #create_degree(downloader, lat, lon, folderthis, spacing)
|
|
|
+ #filelist.append(getDatFile(lat, lon))
|
|
|
+ print(filelist)
|
|
|
+
|
|
|
+ #compress
|
|
|
+ success = compressFiles(filelist, uuidkey, 'outputTer')
|
|
|
+
|
|
|
+ if success:
|
|
|
+ print("Generated " + "/terrain/" + uuidkey + ".zip")
|
|
|
+ return render_template('generate.html', urlkey="/terrain/" + uuidkey + ".zip", uuidkey=uuidkey)
|
|
|
+ else:
|
|
|
+ print("Failed " + "/terrain/" + uuidkey + ".zip")
|
|
|
+ return render_template('generate.html', error = "Processing terrain", uuidkey=uuidkey)
|
|
|
else:
|
|
|
print("Bad get")
|
|
|
return render_template('generate.html', error = "Need to use POST, not GET")
|
|
|
|
|
|
-@app.route('/status/<uuid:uuidkey>')
|
|
|
-def status(uuidkey):
|
|
|
- if not uuidkey:
|
|
|
- return jsonify(status = 'Error: incorrect UUID')
|
|
|
- elif str(uuidkey) in x.terStats:
|
|
|
- return jsonify(status = 'success', data = str(x.terStats[str(uuidkey)]))
|
|
|
- elif os.path.exists(os.path.join('.', 'outputTer', str(uuidkey) + ".zip")):
|
|
|
- return jsonify(status = 'success', data = "ready")
|
|
|
- else:
|
|
|
- return jsonify(status = 'Error: bad UUID ' + str(os.path.join('.', 'outputTer', str(uuidkey) + ".zip")))
|