Skip to content

Commit 25ccd75

Browse files
committed
Merge branch 'feature/create_file_structure' of https://github.com/cloudcomputingproject/main_repo into feature/create_file_structure
2 parents d79bb41 + 071c763 commit 25ccd75

File tree

3 files changed

+107
-6
lines changed

3 files changed

+107
-6
lines changed

Diff for: application/api/external_api.py

+30-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from servers import worldbank
2424
from servers import geocoding
2525
from servers import foodstandartsagency
26+
from servers import police
2627

2728
# Flask-Cache (configured to use App Engine Memcache API)
2829
cache = Cache(app)
@@ -41,7 +42,8 @@
4142
servers = {
4243
'worldbank' : 1,
4344
'geocoding' : 2,
44-
'foodstandartsagency' : 3
45+
'foodstandartsagency' : 3,
46+
'police' : 4
4547
}
4648

4749
@external_api.route('/api/'+str(servers['worldbank'])+'/<dataType>/<date>/<format>')
@@ -65,13 +67,36 @@ def getFoodStandartsAgencyData(name, location, format):
6567
result = foodstandartsagency.getData(name, location, format)
6668
return jsonify(result)
6769

70+
#Gets the crime categories from the police api
71+
@external_api.route('/api/'+str(servers['police']))
72+
def getCrimeCategories():
73+
result = police.getCategories()
74+
return jsonify(result)
6875

76+
# Gets the neighbourhoods in a county from the police api
77+
@external_api.route('/api/'+str(servers['police'])+'/<county>')
78+
def getNeighbourhoods(county):
79+
result = police.getNeighbourhoodsData(county)
80+
return jsonify(result)
6981

82+
# Gets the boundary coordinates for a neighbourhood in a county
83+
@external_api.route('/api/'+str(servers['police'])+'/<county>/<nhood>')
84+
def getBoundary(county, nhood):
85+
result = police.getBoundaryData(county, nhood)
86+
return jsonify(result)
7087

88+
# Gets the crimes done in this location during that month from the police api
89+
# The date must be a string in the format 'yyyy-mm'
90+
@external_api.route('/api/'+str(servers['police'])+'/<category>/<lat>/<lng>/<date>')
91+
def getCrimes(category, lat, lng, date):
92+
result = police.getCrimesData(category, lat, lng, date)
93+
return jsonify(result)
7194

72-
73-
74-
75-
95+
# Gets the crimes done in this area during that month from the police api
96+
# The date must be a string in the format 'yyyy-mm'
97+
@external_api.route('/api/'+str(servers['police'])+'/<category>/<latArr>/<lngArr>/<date>')
98+
def getCrimesInArea(category, latArr, lngArr, date):
99+
result = police.getCrimesInAreaData(category, latArr, lngArr, date)
100+
return jsonify(result)
76101

77102

Diff for: application/api/servers/police.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
2+
import urllib2
3+
import json
4+
5+
from google.appengine.api import users
6+
from google.appengine.runtime.apiproxy_errors import CapabilityDisabledError
7+
8+
from flask import request, render_template, flash, url_for, redirect, Blueprint, json, jsonify
9+
10+
from flask_cache import Cache
11+
12+
13+
14+
from application.decorators import login_required, admin_required
15+
from application.forms import ExampleForm
16+
from application.models import ExampleModel
17+
18+
from application import app
19+
20+
url = 'http://data.police.uk/api/'
21+
22+
def getData():
23+
global url
24+
#json object
25+
data = urllib2.urlopen(url)
26+
27+
#for testing purposes
28+
#print json.load(data)
29+
30+
url = 'http://data.police.uk/api/'
31+
return data
32+
33+
def getCategories():
34+
global url
35+
url += 'crime-categories?date=2011-08'
36+
return getData()
37+
38+
#Neighbourhoods must be lower case and the white spaces replaced with '-'
39+
def getNeighbourhoodsData(county):
40+
global url
41+
url += county + '/neighbourhoods'
42+
return getData()
43+
44+
def getBoundaryData(county, nhood):
45+
global url
46+
url += county + '/' + getNeighbourhoodID(county, nhood) + '/boundary'
47+
return getData()
48+
49+
def getNeighbourhoodID(county, nhood):
50+
global url
51+
url+= county + '/neighbourhoods'
52+
data = json.load(urllib2.urlopen(url))
53+
54+
for j in xrange(0, len(data)):
55+
if data[j]["name"] == nhood:
56+
return data[j]["id"]
57+
#date must be in the format yyyy-mm
58+
def getCrimesData(category, lat, lng, date):
59+
global url
60+
url+='crimes-street/' + category + '?lat=' + str(lat) + '&lng=' + str(lng) + '&date=' + date
61+
return getData()
62+
63+
def getCrimesInAreaData(category, latArr, lngArr, date):
64+
global url
65+
url+='crimes-street/' + category + '?poly='
66+
for j in xrange(0, len(latArr)):
67+
url+=str(latArr[j]) + ',' + str(lngArr[j]) + ':'
68+
url = url[:-1]
69+
url+='&date=' + date
70+
return getData()
71+
72+
73+
#getCategories()
74+
#getNeighbourhoods('hampshire')
75+
#getBoundary('hampshire', 'Fair Oak')
76+
#getCrimes('all-crimes', 52.629729, -1.131592, '2014-09')
77+
#getCrimesInArea('all-crimes', [52.268, 52.794, 52.130], [0.543, 0.238, 0.478], '2014-09')

Diff for: index.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,3 @@ indexes:
99
# manually, move them above the marker line. The index.yaml file is
1010
# automatically uploaded to the admin console when you next deploy
1111
# your application using appcfg.py.
12-
# test

0 commit comments

Comments
 (0)