forked from AmphibiaWeb/SpeciesLookup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webapi.py
93 lines (78 loc) · 2.97 KB
/
webapi.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
# everything to do with api creation
from flask import Flask, current_app, jsonify
import os
import kmlparserclass as k
import ray_casting as r
import lookup_table as table
import pickle
import datetime
os.chdir("/home/chenyu_shi/SpeciesLookup")
app = Flask(__name__, static_url_path='/static')
file_Name = "gridtable"
fileObject = open(file_Name, 'rb')
grid_cells = pickle.load(fileObject)
fileObject.close()
@app.route('/')
def home():
return current_app.send_static_file("index.html")
@app.route('/about/')
def api_root():
"""
:return: instruction on how to use this
"""
instruction = """Welcome to Species Lookup web api!
Try formulate your query in this format( specieslookup.berkeley.edu/search/[longitude,latitude]) to get proper result
here's an example: specieslookup.berkeley.edu/search/-122.264776,37.870871
Note: do not add space between long and lat """
return instruction
@app.route('/search/<points>')
def get(points):
"""
:param points: (long,lat)
:return: a count of species and their scientific names
"""
try:
# parse input coordinates
longa, lat = float(points.split(",")[0]), float(points.split(",")[1])
i, j = table.grid_cell.array_index(longa, lat, (1, 1))
point = r.points(longa, lat)
cell = grid_cells[i][j]
result = []
for species in cell.species:
par = k.parser(species)
if par.inside(point):
# check cached results to see if each specie's range map contains this point
result.append(species)
result.append("count: " + str(len(result)))
return str(result), 200
except:
message = """unknown error, try specieslookup.berkeley.edu/about/ for instructions on query formatting"""
return message, 400
@app.route('/search_json/<points>')
def get_json(points):
"""
:param points: (long,lat)
:return: a count of species and their scientific names
"""
try:
# parse input coordinates
longa, lat = float(points.split(",")[0]), float(points.split(",")[1])
i, j = table.grid_cell.array_index(longa, lat, (1, 1))
point = r.points(longa, lat)
cell = grid_cells[i][j]
result = []
for species in cell.species:
par = k.parser(species)
if par.inside(point):
# check cached results to see if each specie's range map contains this point
now = datetime.datetime.now()
result.append(
{"class": par.class_name, "order": par.order, "family": par.family, "scientific_name": species,
"url": par.url, "index_date": str(now)})
result.sort(key=lambda x: x["scientific_name"])
return jsonify(count=len(result), species=result), 200
except:
message = """unknown error, try specieslookup.berkeley.edu/about/ for instructions on query formatting"""
return message, 400
if __name__ == '__main__':
app.run()