-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGreenView_CalcDL.py
175 lines (131 loc) · 7.68 KB
/
GreenView_CalcDL.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# This program is used to calculate the green view index based on the collecte metadata. The
# Object based images classification algorithm is used to classify the greenery from the GSV imgs
# in this code, the meanshift algorithm implemented by pymeanshift was used to segment image
# first, based on the segmented image, we further use the Otsu's method to find threshold from
# ExG image to extract the greenery pixels.
# For more details about the object based image classification algorithm
# check: Li et al., 2016, Who lives in greener neighborhoods? the distribution of street greenery and it association with residents' socioeconomic conditions in Hartford, Connectictu, USA
# This program implementing OTSU algorithm to chose the threshold automatically
# For more details about the OTSU algorithm and python implmentation
# cite: http://docs.opencv.org/trunk/doc/py_tutorials/py_imgproc/py_thresholding/py_thresholding.html
# Copyright(C) Xiaojiang Li, Ian Seiferling, Marwa Abdulhai, Bill Cai, Senseable City Lab, MIT
import model_lib as treepedia_dl
# using 18 directions is too time consuming, therefore, here I only use 6 horizontal directions
# Each time the function will read a text, with 1000 records, and save the result as a single TXT
def GreenViewComputing_ogr_6Horizon(GSVinfoFolder, outTXTRoot, greenmonth, key_file):
"""
This function is used to download the GSV from the information provide
by the gsv info txt, and save the result to a shapefile
Required modules: StringIO, numpy, requests, and PIL
GSVinfoTxt: the input folder name of GSV info txt
outTXTRoot: the output folder to store result green result in txt files
greenmonth: a list of the green season, for example in Boston, greenmonth = ['05','06','07','08','09']
key_file: the API keys in txt file, each key is one row, I prepared five keys, you can replace by your owne keys if you have Google Account
last modified by Xiaojiang Li, MIT Senseable City Lab, March 25, 2018
"""
import time
from PIL import Image
import numpy as np
import requests
from StringIO import StringIO
# read the Google Street View API key files, you can also replace these keys by your own
lines = open(key_file,"r")
keylist = []
for line in lines:
key = line[:-1]
keylist.append(key)
print ('The key list is:=============', keylist)
# set a series of heading angle
headingArr = 360/6*np.array([0,1,2,3,4,5])
# number of GSV images for Green View calculation, in my original Green View View paper, I used 18 images, in this case, 6 images at different horizontal directions should be good.
numGSVImg = len(headingArr)*1.0
pitch = 0
# create a folder for GSV images and grenView Info
if not os.path.exists(outTXTRoot):
os.makedirs(outTXTRoot)
# the input GSV info should be in a folder
if not os.path.isdir(GSVinfoFolder):
print 'You should input a folder for GSV metadata'
return
else:
allTxtFiles = os.listdir(GSVinfoFolder)
for txtfile in allTxtFiles:
if not txtfile.endswith('.txt'):
continue
txtfilename = os.path.join(GSVinfoFolder,txtfile)
lines = open(txtfilename,"r")
# create empty lists, to store the information of panos,and remove duplicates
panoIDLst = []
panoDateLst = []
panoLonLst = []
panoLatLst = []
# loop all lines in the txt files
for line in lines:
metadata = line.split(" ")
panoID = metadata[1]
panoDate = metadata[3]
month = panoDate[-2:]
lon = metadata[5]
lat = metadata[7][:-1]
# print (lon, lat, month, panoID, panoDate)
# in case, the longitude and latitude are invalide
if len(lon)<3:
continue
# only use the months of green seasons
if month not in greenmonth:
continue
else:
panoIDLst.append(panoID)
panoDateLst.append(panoDate)
panoLonLst.append(lon)
panoLatLst.append(lat)
# the output text file to store the green view and pano info
gvTxt = 'GV_'+os.path.basename(txtfile)
GreenViewTxtFile = os.path.join(outTXTRoot,gvTxt)
# check whether the file already generated, if yes, skip. Therefore, you can run several process at same time using this code.
print GreenViewTxtFile
if os.path.exists(GreenViewTxtFile):
continue
# write the green view and pano info to txt
with open(GreenViewTxtFile,"w") as gvResTxt:
for i in range(len(panoIDLst)):
panoDate = panoDateLst[i]
panoID = panoIDLst[i]
lat = panoLatLst[i]
lon = panoLonLst[i]
# get a different key from the key list each time
idx = i % len(keylist)
key = keylist[idx]
# calculate the green view index
greenPercent = 0.0
for heading in headingArr:
print "Heading is: ",heading
# using different keys for different process, each key can only request 25,000 imgs every 24 hours
URL = "http://maps.googleapis.com/maps/api/streetview?size=400x400&pano=%s&fov=60&heading=%d&pitch=%d&sensor=false&key=AIzaSyAwLr6Oz0omObrCJ4n6lI4VbCCvmaL1Z3Y"%(panoID,heading,pitch)
# let the code to pause by 1s, in order to not go over data limitation of Google quota
time.sleep(1)
# classify the GSV images and calcuate the GVI
try:
response = requests.get(URL)
im = np.array(Image.open(StringIO(response.content)))
percent = treepedia_dl.predict_single(im)
greenPercent = greenPercent + percent
# if the GSV images are not download successfully or failed to run, then return a null value
except:
greenPercent = -1000
break
# calculate the green view index by averaging six percents from six images
greenViewVal = greenPercent/numGSVImg
print 'The greenview: %s, pano: %s, (%s, %s)'%(greenViewVal, panoID, lat, lon)
# write the result and the pano info to the result txt file
lineTxt = 'panoID: %s panoDate: %s longitude: %s latitude: %s, greenview: %s\n'%(panoID, panoDate, lon, lat, greenViewVal)
gvResTxt.write(lineTxt)
# ------------------------------Main function-------------------------------
if __name__ == "__main__":
import os,os.path
import itertools
GSVinfoRoot = 'MYPATH//spatial-data/metadata'
outputTextPath = r'MYPATH//spatial-data/greenViewRes'
greenmonth = ['01','02','03','04','05','06','07','08','09','10','11','12']
key_file = 'MYPATH/Treepedia/Treepedia/keys.txt'
GreenViewComputing_ogr_6Horizon(GSVinfoRoot,outputTextPath, greenmonth, key_file)