-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape.py
87 lines (74 loc) · 2.43 KB
/
scrape.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
import numpy as np
import urllib
import json
from PVcalc import PVcalc
from utils import *
# import utm
from gml_parser import *
def loadSchoolData():
f = urllib.urlopen("http://www.education.gov.uk/schools/performance/geo/la202_all.html")
text = f.readlines()
schDataStr = text[58][8:]
schDataStrPruned = schDataStr[:-2]
return json.loads(schDataStrPruned)
def findSchool(schData,schName):
school = None
for i in schData:
if i['name'] == schName:
school = i
if school:
return school
else:
print "School '%s' not found" % schName
return
def findSchoolBuilding(buildings,buildingsPos,schPos,method):
if method == 'lat_long':
# utmPos = utm.from_latlon(schPos[0],schPos[1])
# easting,northing = (utmPos[0],utmPos[1])
easting,northing = WGS84toOSGB36(schPos[0],schPos[1])
else:
easting,northing = (schPos[0],schPos[1])
return locateBuilding(buildings,buildingsPos,(easting,northing))
def locateBuilding(buildings,buildingsPos,pos):
distances = calcDists(pos,buildingsPos)
index = np.argmin(distances)
print index
closestBuilding = buildings[np.argmin(distances)]
return closestBuilding
def loadBuildingsPos(buildings):
positions = []
for building in buildings:
positions.append(locationOfBuilding(building))
return np.array(positions)
def annotateBuilding(gmldoc,building,schPos,schName):
posStr = "%s %s" % (schPos[0],schPos[1])
appendObject(gmldoc,building,'gml:schName',schName)
appendObject(gmldoc,building,'gml:schPos',posStr)
def calculatePV(building):
PV = PVcalc()
PV.loadBuilding(building)
PV.setUpPVcalc()
PV.calcMoneySaved()
PV.calcCost()
PV.calcTimeForBreakEven()
return PV.M,PV.cost,PV.t
if __name__ == "__main__":
print "loading school data..."
schools = loadSchoolData()['schools']
# school = findSchool(schData,'Broadhurst School')
print "loading building data..."
gmldoc,buildings = loadBuildings('data/TQ28.gml') # TODO
print "calculating positions of buildings..."
buildingsPos = loadBuildingsPos(buildings)
schoolBuildings = []
for school in schools:
print school['name']
building = findSchoolBuilding(buildings,buildingsPos,school['markpos'],'lat_long')
schoolBuildings.append(building)
annotateBuilding(gmldoc,building,school['markpos'],school['name'])
savings,cost,years = calculatePV(building)
print savings,cost,years
write2csv((school['name'],savings,cost,years),'data/camden.csv')
print ""
print "writing new gml file..."
createNewGml('data/camden_schools.gml',schoolBuildings)