Get ROIs in ROI dataset
Description: Get ROIs in ROI dataset
URI: /datasets/ROI/<name>
args: rid = <ROI id>,
p = <point>,
r = <range>
d = <density>
http method: GET
@app.route('/datasets/ROI/<name>', methods=['GET'])
def retrieve_ROI(name):
cur = conn.cursor()
'''
r = requests.get("http://127.0.0.1:5566/datasets/ROI")
if name not in r.json()['datasets']:
return error_request()
'''
parameter = []
# rid = rid
if 'rid' in request.args:
if not re.match('[0-9]+',request.args['rid']):
return error_request()
parameter.append('rid = '+request.args['rid'])
# point(lon,lat) @ range
# 測試point是否為有效格式? (須包含兩個值代表經緯度)
if 'p' in request.args:
if not re.match('[-+]?[0-9]*\.?[0-9]+\,[-+]?[0-9]*\.?[0-9]+',request.args['p']):
return error_request()
parameter.append('point('+request.args['p']+') @ range')
# range in range
# 測試range是否為有效格式? (須包含四個值代表經緯度範圍
if 'r' in request.args:
if not re.match('[-+]?[0-9]*\.?[0-9]+\,[-+]?[0-9]*\.?[0-9]+,[-+]?[0-9]*\.?[0-9]+\,[-+]?[0-9]*\.?[0-9]+',request.args['r']):
return error_request()
parameter.append("range @box '"+request.args['r']+"'")
# density
if 'd' in request.args:
if not re.match('[0-9]+',request.args['d']):
return error_request()
parameter.append("density >= "+request.args['d'])
# score
if 's' in request.args:
if not re.match('[0-9]*\.?[0-9]+',request.args['s']):
return error_request()
parameter.append("score >= "+request.args['s'])
if len(parameter) > 0:
query = "select * from roi."+name+" "+"WHERE "+" AND ".join(parameter)+" ORDER BY rid ASC;"
else:
query = "select * from roi."+name+" ORDER BY rid ASC;"
try:
cur.execute(query)
except psycopg2.Error as e:
return error_request()
rows = [r for r in cur]
dataset = []
for row in rows:
rid = row[0]
density = row[1]
score = row[2]
buffer = row[3].split(',')
range = {'east':buffer[0].strip('(') .strip(')'), 'north': buffer[1].strip('(') .strip(')'), 'west': buffer[2].strip('(') .strip(')'), 'south': buffer[3].strip('(') .strip(')')}
dataset.append({'rid':rid, 'density': density, 'score': score, 'range':range})
return jsonify({'name':name, 'ROI': dataset})