-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
103 lines (80 loc) · 2.93 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from flask import Flask, jsonify, render_template, request, make_response, send_from_directory
import json
import urllib3
import requests
import numpy as np
import cv2
import navpy
import os
import string
import sys
import zipfile
from fileHandling.fileWriters import *
from terrainGen.generateTerrain import *
app = Flask(__name__)
def read_frontend_api_key():
with open('config/api.json', 'r') as f:
config_data = json.load(f)
return config_data['frontend_bing_maps_key']
@app.route('/')
def home():
return render_template("index.html", frontend_key=read_frontend_api_key())
@app.route('/generate', methods=['GET', 'POST'])
def generate():
json_data = request.get_json()
print(json_data)
try:
# Get cwd
setdir = os.getcwd()
print("current dir: "+setdir)
# Command line input
model_name = json_data['model_name']
file_path = "static/" + model_name
print(file_path)
height_img_name = model_name+"_heightmap"
aerial_img_name = model_name+"_aerial"
print(json_data['sideLength'])
size_m = float(json_data['sideLength'])
if not os.path.exists(file_path):
os.makedirs(file_path)
print("made model folder")
if not os.path.exists(file_path+"/textures"):
os.mkdir(file_path+"/textures")
print("made textures folder")
max_alt = gen_terrain(file_path, height_img_name, aerial_img_name,
json_data['latitude'], json_data['longitude'], size_m)
# Change to templates directory
os.chdir("templates")
# Read template files
config_template = read_template("config_temp.txt")
sdf_template = read_template("sdf_temp.txt")
# Change to model directory
os.chdir(setdir+"/"+file_path)
# Write to model.config
write_config_file(config_template, model_name, "auto-gne",
"na", "auto-gened by intelligent quads")
# Write to model.sdf
write_sdf_file(model_name, height_img_name, aerial_img_name,
sdf_template, size_m, size_m, max_alt)
os.chdir(setdir+'/static')
# zip directory
zipf = zipfile.ZipFile(model_name + '.zip', 'w', zipfile.ZIP_DEFLATED)
zipdir(model_name, zipf)
zipf.close()
os.chdir(setdir)
finally:
# Change back to cwd
os.chdir(setdir)
print(setdir)
send_from_directory(setdir+'/static', model_name+'.zip')
# os.remove(setdir+'/static/'+model_name+'.zip')
return json.dumps({'success': True, 'filename': model_name+'.zip'}), 200, {'ContentType': 'json'}
def zipdir(path, ziph):
print("ziping")
# ziph is zipfile handle
for root, dirs, files in os.walk(path):
for file in files:
ziph.write(os.path.join(root, file))
if __name__ == '__main__':
# run app in debug mode on port 5000
app.run(debug=True, port=5000)