-
Notifications
You must be signed in to change notification settings - Fork 2
/
data.py
77 lines (51 loc) · 2.18 KB
/
data.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
import urllib2
import StringIO
import gzip
import os
# set of fuctions for loading data from hackathon S3 site
baseURL = "https://s3-ap-southeast-1.amazonaws.com/ph-opentraffic-data/"
# stores data locally in this directory
dataDirectory = "data/"
# call with city name ("cebu" or "manila" YYYY-MM-DD for first day (monday) of week to downalod
# e.g. getData("cebu" "2015-12-28") -> "data/week_
# returns path to decompressed file
def getData(city, week):
cityUrl = baseURL + city + "/"
filename = "week_" + week + ".csv.gz"
cityDataDirectory = dataDirectory + city + "/"
if not os.path.exists(cityDataDirectory):
os.makedirs(cityDataDirectory)
outFilePath = cityDataDirectory + filename[:-3]
if os.path.exists(outFilePath):
print "File " + outFilePath + " already downloaded."
return outFilePath
print "Downloading " + outFilePath + "..."
response = urllib2.urlopen(cityUrl + filename)
compressedFile = StringIO.StringIO(response.read())
decompressedFile = gzip.GzipFile(fileobj=compressedFile)
with open(outFilePath, 'w') as outfile:
outfile.write(decompressedFile.read())
num_lines = sum(1 for line in open(outFilePath))
print "Downloaded " + outFilePath + " (" + str(num_lines) + " lines)"
return outFilePath
def getGeoData(city):
cityUrl = baseURL + city + "/"
zipFilename = city + "_geometry.zip"
shpFilename = city + "_geometry.shp"
cityDataDirectory = dataDirectory + city + "/"
if not os.path.exists(cityDataDirectory):
os.makedirs(cityDataDirectory)
if os.path.exists(cityDataDirectory + shpFilename):
print "File " + shpFilename + " already downloaded."
return cityDataDirectory + shpFilename
import urllib
print "Downloanding " + shpFilename + "..."
getFile = urllib.URLopener()
getFile.retrieve(cityUrl + zipFilename, cityDataDirectory + zipFilename)
import zipfile
zip_ref = zipfile.ZipFile(cityDataDirectory + zipFilename, 'r')
zip_ref.extractall(cityDataDirectory)
zip_ref.close()
os.remove(cityDataDirectory + zipFilename)
print "Downloaded " + shpFilename
return cityDataDirectory + shpFilename