-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQuery_practice_geo.py
144 lines (121 loc) · 4.48 KB
/
Query_practice_geo.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
from pymongo import MongoClient
from pymongo import ASCENDING, DESCENDING
import re
import csv
import geojson
import json
#connection = Connection('146.185.159.107',27017)
def __connect__():
connection = MongoClient('146.185.159.107', 27017)
db = connection.prescription
db.authenticate('nhshd','nhshd')
return db
def __connectWrite__():
connection = MongoClient('146.185.159.107', 27017)
db = connection.prescription
db.authenticate('nhshd-rw','nhs-m0ng0')
return db
def getCollection(collName,write=False):
if write:
db = __connectWrite__()
else:
db = __connect__()
if collName=='system.indexes':
posts = db.system.indexes
elif collName=='practices':
posts = db.practices
elif collName=='chem_sub':
posts = db.chem_sub
elif collName=='practice_geo':
posts = db.practice_geo
elif collName=='prescriptons':
posts = db.prescriptons
elif collName=='system.users':
posts = db.system.users
else:
raise Exception('Unknown database collection ' + collName)
return posts
def getAllAD():
p = getCollection('prescriptons')
regx = re.compile("^0403030", re.IGNORECASE)
res = p.find({'BNF CODE':regx})
return res
def sumAmounts():
## Function to aggregate
p = getCollection('prescriptons')
# regx = re.compile("^0403030", re.IGNORECASE)
regx = re.compile("^0403030E0", re.IGNORECASE)
return p.aggregate( [{ '$match': {'BNF CODE':regx} } ,
{'$group': { '_id' : "$PRACTICE" ,'totalCost': {'$sum': "$ACT COST"} }}])
def pushMetrics(id):
## Function to take a list of dictonaries with id:practice and
# metricName and push to practice db
db = getCollection('practices')
print db.find({'_id':id})
def pushToPrac(dic):
db = getCollection('practices',write=True)
for pracID,pracDic in dic.iteritems():
for key,value in pracDic.iteritems():
r = db.update({'_id':pracID},{'$set': {'metrics':{key:value}} })
def testPrac(dic):
db = getCollection('practices',write=False)
for pracID,pracDic in dic.iteritems():
res = db.find({'_id':pracID})
for r in res:
print r
def main():
cl = getCollection('practices',write=False)
print '{ "type": "FeatureCollection",','\n';
print '"features": [','\n'
for p in cl.find({ 'loc' : { '$exists' : True} }) :
print '{ "type": "Feature",'
print '"properties" : { '
name = p['name']
name = name.strip()
post = p['post']
post = post.strip()
town = p['town']
town = town.strip()
print '"Name" : "',name,'",',
print '"Post code" : "',post,'",',
print '"Town" : "',town,'"',
#print '"Quantity of Metformin HCl_Tab 500mg prescribed" : "',p['metrics']['Quanity of Metformin HCl_Tab 500mg'],'"',
print'},\n'
print '"geometry": { "type": "Point", "coordinates": [',p['loc']['coordinates'][0],',',p['loc']['coordinates'][1],'] }\n';
print '},',"\n"
print "]\n"
print "}"
#with open('data.geojson', 'w') as outfile:
#with open("geo.practice.csv", "w") as file:
#csv_file = csv.writer(file)
#print p['loc']['coordinates'][0]
#f.write(['loc']['coordinates'][0],
# p['loc']['coordinates'][1]
# ])
#print pt;
#json.dump(p['loc'],outfile)
#csv_file.writerow([p['name'].lstrip(),
# p['town'].lstrip(),
# p['post'].lstrip(),
# p['loc']['coordinates'][0],
# p['loc']['coordinates'][1]
# ]);
#f.close()
#practiceID = 'D81025'
#cl = getCollection('prescriptons',write=False)
#print "Number of prescriptions for practice ",practiceID," : \n";
#print cl.find({"PRACTICE": practiceID}).count()
#for pr in cl.find({"PRACTICE": practiceID}).sort("ACT_COST",DESCENDING) :
# print pr
if __name__ == "__main__":
main()
# res = getAllAD()
# for r in res:
# print r
# print sumAmounts()['result'][:5]
# tmpListMetric = [{u'totalCost': 6.99, u'_id': u'C82651'}, {u'totalCost': 39.38, u'_id': u'C82642'}, {u'totalCost': 63.82, u'_id': u'C82639'}, {u'totalCost': 126.17, u'_id': u'C82624'}, {u'totalCost': 40.97, u'_id': u'C82610'}]
# pushMetrics(id = tmpListMetric[0] )
# db = __connect__()
# print db.collection_names()
# pres = getCollection('practices')
# print pres.find_one()