-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
173 lines (151 loc) · 5.45 KB
/
run.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import json
import random
from flask import Flask, render_template, request, jsonify, send_file
import os
import sys
import pathlib
sys.path.append(pathlib.Path('./src/').absolute().__str__())
from src import utils
from src import algorithm
from src.CustomData import YSZJData, LGZJData, KMLData
from src.StandardData import StandardData
app = Flask(__name__, template_folder='./templates/', static_folder='./static/')
@app.route('/')
def main():
return render_template('index.html')
@app.route('/getDataSource', methods=["POST"])
def getDataSource():
data = request.get_json()
field = data['sourceField']
data_source = utils.getAllDataSource(field)
response = {
'dataSource': data_source
}
return jsonify(response)
@app.route('/processPreview', methods=["POST"])
def processPreview():
try:
data = request.get_json()
data_source = data['dataSource']
merged_df = StandardData.mergeAllDataSource(data_source)
clusters, ready_shortcut = StandardData.processToWebUI(merged_df, config['kMeansCluster'])
#map_path = algorithm.reDrawPoints(merged_df, save_dir='static/tmp/')
response = {
'mapData': clusters,
'readyShortcut': ready_shortcut,
'pointCount': len(merged_df),
#'mapPath': map_path
}
return jsonify(response)
except Exception as e:
print(e)
@app.route('/getConfigInfo', methods=["GET"])
def getConfigInfo():
response = {
'kMeansCluster': config['kMeansCluster'],
'mapType': config['mapType']
}
return jsonify(response)
@app.route('/processShortcut', methods=["POST"])
def processShortcut():
data = request.get_json()
data_source = data['dataSource']
merged_df = StandardData.mergeAllDataSource(data_source)
filename = utils.genFileName()
StandardData.save(merged_df, os.path.join('./data/shortcut', filename))
response = {
'status': True,
'shortcutPath': filename,
}
return jsonify(response)
@app.route('/processExport', methods=["POST"])
def processExport():
data = request.get_json()
shortcut_source = data['shortcutSource'].split('】')[-1]
export_type = data['exportType']
as_name = data['asName']
if as_name == '':
as_name = utils.genFileName(_format='')
df = StandardData.load(os.path.join('data/shortcut', shortcut_source))
export_path = os.path.join('data/export', f'{as_name}-{export_type}.csv')
if export_type == "yszj-ios":
export_path = os.path.join('data/export', f'{as_name}-{export_type}.csv')
df = StandardData.cvtToYSZJFormat(df)
YSZJData.export(df, export_path)
status = True
elif export_type == "lgzj-android":
export_path = os.path.join('data/export', f'{as_name}-{export_type}.csv')
df = StandardData.cvtToLGZJFormat(df, export_os="android")
LGZJData.export(df, export_path)
status = True
elif export_type == "lgzj-ios":
export_path = os.path.join('data/export', f'{as_name}-{export_type}.csv')
df = StandardData.cvtToLGZJFormat(df, export_os="ios")
LGZJData.export(df, export_path)
status = True
elif export_type == "google-earth":
export_path = os.path.join('data/export', f'{as_name}-{export_type}.kml')
tree = StandardData.cvtToKMLFormat(df)
KMLData.export(tree, export_path)
status = True
else:
status = False
response = {
'status': status,
'message': f'Export file as 【{export_path}】' if status else 'Failed.'
}
return jsonify(response)
@app.route('/removeShortcutFiles', methods=["GET"])
def removeShortcutFiles():
file_count = utils.clearFolder('./data/shortcut/')
response = {
'status': f'Done, {file_count} files were removed.'
}
return jsonify(response)
@app.route('/removeExportFiles', methods=["GET"])
def removeExportFiles():
file_count = utils.clearFolder('./data/export/')
response = {
'status': f'Done, {file_count} files were removed.'
}
return jsonify(response)
@app.route('/removeTmpFiles', methods=["GET"])
def removeTmpFiles():
file_count = 0
file_count += utils.clearFolder('./data/tmp/')
file_count += utils.clearFolder('./static/tmp/')
response = {
'status': f'Done, {file_count} files were removed.'
}
return jsonify(response)
@app.route('/quickstart', methods=["POST"])
def quickstart():
file = request.files['file']
to_type = request.form['type']
file.save('./data/tmp/upload_data.csv')
if to_type == 'YSZJ2LGZJ':
df = YSZJData.read('upload_data.csv', default_dir='./data/tmp')
df = YSZJData.cvtToStandardFormat(df)
df = StandardData.cvtToLGZJFormat(df)
LGZJData.export(df, './data/tmp/res_data.csv')
elif to_type == 'LGZJ2YSZJ':
df = LGZJData.read('upload_data.csv', default_dir='./data/tmp')
df = LGZJData.cvtToStandardFormat(df)
df = StandardData.cvtToYSZJFormat(df)
YSZJData.export(df, './data/tmp/res_data.csv')
csv_data = './data/tmp/res_data.csv'
return send_file(
csv_data,
mimetype='text/csv',
as_attachment=True,
download_name='downloaded_file.csv'
)
if __name__ == "__main__":
import sys
sys.path.append('.')
config = dict()
with open('./config.json', 'r') as f:
data = json.load(f)
config['kMeansCluster'] = int(data['kMeansCluster'])
config['mapType'] = data['mapType']
app.run()