-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
158 lines (141 loc) · 4.84 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
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
import logging
import time
import json
import pymongo
import datetime
from flask import Flask
from flask import jsonify
from flask import request
from subprocess import Popen, PIPE, DEVNULL
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
client = pymongo.MongoClient("mongodb://192.168.2.223:27017")
@app.route('/health_check', methods=['GET'])
def health_check():
'''
health_check
'''
app.logger.info("health_check")
response = {'status': 'OK'}
return jsonify(response), 200
@app.route('/v1/get_regiones', methods=['GET'])
def get_regiones():
'''
get_regiones
'''
app.logger.info("get_regiones")
db = client['regiones']
regiones = db.list_collection_names()
response = {'regiones':regiones}
return jsonify(response), 200
@app.route('/v1/get_comunas', methods=['GET'])
def get_comunas():
'''
get_comunas
'''
app.logger.info("get_comunas")
db = client['comunas']
comunas = db.list_collection_names()
response = {'comunas':comunas}
return jsonify(response), 200
@app.route('/v1/get_comuna_by_name', methods=['GET'])
def get_comuna_by_name():
'''
get_comuna_by_name
'''
app.logger.info("get_comuna_by_name")
try:
comuna_id = request.args.get('comuna')
app.logger.info("Comuna ID: {}".format(comuna_id))
db = client['comunas']
collec = db[comuna_id]
doc = collec.find_one()
if doc == None:
return jsonify({"status":"not found"}), 200
doc.pop("_id",None)
response = doc
return jsonify(response), 200
except pymongo.errors.InvalidName:
return jsonify({"status":"empty field"}), 200
@app.route('/v1/get_comuna_by_region_id', methods=['GET'])
def get_comuna_by_region_id():
'''
- Te paso el código de región y el parámetro de simplificación
- Me retornas el topojson de las comunas a la región correspondiente
'''
try:
region_cut = request.args.get('region')
simplify = request.args.get('simplify')
app.logger.info("Comuna ID: {}".format(region_cut))
app.logger.info("simplify: {}".format(simplify))
db = client['comunas']
collec = db[region_cut]
doc = collec.find_one()
if doc == None:
return jsonify({"status":"not found"}), 200
doc.pop("_id",None)
# https://gist.github.com/arthur-e/8495616
ts = datetime.datetime.now().timestamp()
geo = "{}.{}".format(ts,'json')
topo = "topo_{}.{}".format(ts,'json')
with open(geo, 'w') as outfile:
json.dump(doc, outfile)
cmd = "toposimplify {} -p {} -o {}".format(geo, simplify, topo)
# app.logger.info(cmd)
process = Popen(cmd , stdout=DEVNULL , stderr=DEVNULL , shell=True)
process.wait()
with open(topo) as json_file:
data = json.load(json_file)
response = data
return jsonify(response), 200
except pymongo.errors.InvalidName:
return jsonify({"status":"empty field"}), 200
@app.route('/v1/get_region_by_id', methods=['GET'])
def get_region_by_id():
'''
get_region_by_id:
- Te paso el código de región y el parámetro de simplificación
- Me retornas el topojson de la región correspondiente
'''
try:
region_id = request.args.get('region')
simplify = request.args.get('simplify')
app.logger.info("Region ID: {}".format(region_id))
app.logger.info("simplify: {}".format(simplify))
db = client['regiones']
collec = db[region_id]
doc = collec.find_one()
if doc == None:
return jsonify({"status":"not found"}), 200
doc.pop("_id",None)
# app.logger.info(doc)
#Topo
#using
# https://gist.github.com/arthur-e/8495616
ts = datetime.datetime.now().timestamp()
geo = "{}.{}".format(ts,'json')
topo = "topo_{}.{}".format(ts,'json')
with open(geo, 'w') as outfile:
json.dump(doc, outfile)
cmd = "toposimplify {} -p {} -o {}".format(geo, simplify, topo)
process = Popen(cmd , stdout=DEVNULL , stderr=DEVNULL , shell=True)
process.wait()
with open(topo) as json_file:
data = json.load(json_file)
response = data
return jsonify(response), 200
except pymongo.errors.InvalidName:
return jsonify({"status":"empty field"}), 200
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)
else:
# setup logging using gunicorn logger
formatter = logging.Formatter(
'[%(asctime)s.%(msecs)03d] [%(name)s] [%(levelname)s] - %(message)s',
'%d-%m-%Y %H:%M:%S'
)
gunicorn_logger = logging.getLogger('gunicorn.error')
app.logger.handlers = gunicorn_logger.handlers
app.logger.handlers[0].setFormatter(formatter)
app.logger.setLevel(logging.DEBUG)