-
Notifications
You must be signed in to change notification settings - Fork 112
/
Greenview2Shp.py
216 lines (156 loc) · 7.26 KB
/
Greenview2Shp.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# This script is used to convert the green view index results saved in txt to Shapefile
# considering the facts many people are more comfortable with shapefile and GIS
# Copyright(C) Xiaojiang Li, Ian Seiferling, Marwa Abdulhai, Senseable City Lab, MIT
def Read_GSVinfo_Text(GVI_Res_txt):
'''
This function is used to read the information in text files or folders
the fundtion will remove the duplicate sites and only select those sites
have GSV info in green month.
Return:
panoIDLst,panoDateLst,panoLonLst,panoLatLst,greenViewLst
Pamameters:
GVI_Res_txt: the file name of the GSV information txt file
'''
import os,os.path
# empty list to save the GVI result and GSV metadata
panoIDLst = []
panoDateLst = []
panoLonLst = []
panoLatLst = []
greenViewLst = []
# read the green view index result txt files
lines = open(GVI_Res_txt,"r")
for line in lines:
# check the completeness of each line, each line include attribute of, panoDate, lon, lat,greenView
if "panoDate" not in line or "greenview" not in line:
continue
panoID = line.split(" panoDate")[0][-22:]
panoDate = line.split(" longitude")[0][-7:]
coordinate = line.split("longitude: ")[1]
lon = coordinate.split(" latitude: ")[0]
latView = coordinate.split(" latitude: ")[1]
lat = latView.split(', greenview:')[0]
greenView = line.split("greenview:")[1]
# check if the greeView data is valid
if len(greenView)<2:
continue
elif float(greenView) < 0:
print greenView
continue
# remove the duplicated panorama id
if panoID not in panoIDLst:
panoIDLst.append(panoID)
panoDateLst.append(panoDate)
panoLonLst.append(lon)
panoLatLst.append(lat)
greenViewLst.append(greenView)
return panoIDLst,panoDateLst,panoLonLst,panoLatLst,greenViewLst
# read the green view index files into list, the input can be file or folder
def Read_GVI_res(GVI_Res):
'''
This function is used to read the information in text files or folders
the fundtion will remove the duplicate sites and only select those sites
have GSV info in green month.
Return:
panoIDLst,panoDateLst,panoLonLst,panoLatLst,greenViewLst
Pamameters:
GVI_Res: the file name of the GSV information text, could be folder or txt file
last modified by Xiaojiang Li, March 27, 2018
'''
import os,os.path
# empty list to save the GVI result and GSV metadata
panoIDLst = []
panoDateLst = []
panoLonLst = []
panoLatLst = []
greenViewLst = []
# if the input gvi result is a folder
if os.path.isdir(GVI_Res):
allTxtFiles = os.listdir(GVI_Res)
for txtfile in allTxtFiles:
# only read the text file
if not txtfile.endswith('.txt'):
continue
txtfilename = os.path.join(GVI_Res,txtfile)
# call the function to read txt file to a list
[panoIDLst_tem,panoDateLst_tem,panoLonLst_tem,panoLatLst_tem,greenViewLst_tem] = Read_GSVinfo_Text(txtfilename)
panoIDLst = panoIDLst + panoIDLst_tem
panoDateLst = panoDateLst + panoDateLst_tem
panoLonLst = panoLonLst + panoLonLst_tem
panoLatLst = panoLatLst + panoLatLst_tem
greenViewLst = greenViewLst + greenViewLst_tem
else: #for single txt file
[panoIDLst_tem,panoDateLst_tem,panoLonLst_tem,panoLatLst_tem,greenViewLst_tem] = Read_GSVinfo_Text(txtfilename)
return panoIDLst,panoDateLst,panoLonLst,panoLatLst,greenViewLst
def CreatePointFeature_ogr(outputShapefile,LonLst,LatLst,panoIDlist,panoDateList,greenViewList,lyrname):
"""
Create a shapefile based on the template of inputShapefile
This function will delete existing outpuShapefile and create a new shapefile containing points with
panoID, panoDate, and green view as respective fields.
Parameters:
outputShapefile: the file path of the output shapefile name, example 'd:\greenview.shp'
LonLst: the longitude list
LatLst: the latitude list
panoIDlist: the panorama id list
panoDateList: the panodate list
greenViewList: the green view index result list, all these lists can be generated from the function of 'Read_GVI_res'
Copyright(c) Xiaojiang Li, Senseable city lab
last modified by Xiaojiang li, MIT Senseable City Lab on March 27, 2018
"""
import ogr
import osr
# create shapefile and add the above chosen random points to the shapfile
driver = ogr.GetDriverByName("ESRI Shapefile")
# create new shapefile
if os.path.exists(outputShapefile):
driver.DeleteDataSource(outputShapefile)
data_source = driver.CreateDataSource(outputShapefile)
targetSpatialRef = osr.SpatialReference()
targetSpatialRef.ImportFromEPSG(4326)
outLayer = data_source.CreateLayer(lyrname, targetSpatialRef, ogr.wkbPoint)
numPnt = len(LonLst)
print 'the number of points is:',numPnt
if numPnt > 0:
# create a field
idField = ogr.FieldDefn('PntNum', ogr.OFTInteger)
panoID_Field = ogr.FieldDefn('panoID', ogr.OFTString)
panoDate_Field = ogr.FieldDefn('panoDate', ogr.OFTString)
greenView_Field = ogr.FieldDefn('greenView',ogr.OFTReal)
outLayer.CreateField(idField)
outLayer.CreateField(panoID_Field)
outLayer.CreateField(panoDate_Field)
outLayer.CreateField(greenView_Field)
for idx in range(numPnt):
#create point geometry
point = ogr.Geometry(ogr.wkbPoint)
# in case of the returned panoLon and PanoLat are invalid
if len(LonLst[idx]) < 3:
continue
point.AddPoint(float(LonLst[idx]),float(LatLst[idx]))
# Create the feature and set values
featureDefn = outLayer.GetLayerDefn()
outFeature = ogr.Feature(featureDefn)
outFeature.SetGeometry(point)
outFeature.SetField('PntNum', idx)
outFeature.SetField('panoID', panoIDlist[idx])
outFeature.SetField('panoDate',panoDateList[idx])
if len(greenViewList) == 0:
outFeature.SetField('greenView',-999)
else:
outFeature.SetField('greenView',float(greenViewList[idx]))
outLayer.CreateFeature(outFeature)
outFeature.Destroy()
data_source.Destroy()
else:
print 'You created a empty shapefile'
## ----------------- Main function ------------------------
if __name__ == "__main__":
import os
import sys
inputGVIres = r'MYPATHH/spatial-data/greenViewRes'
outputShapefile = 'MYPATHH/spatial-data/GreenViewRes.shp'
lyrname = 'greenView'
[panoIDlist,panoDateList,LonLst,LatLst,greenViewList] = Read_GVI_res(inputGVIres)
print ('The length of the panoIDList is:', len(panoIDlist))
CreatePointFeature_ogr(outputShapefile,LonLst,LatLst,panoIDlist,panoDateList,greenViewList,lyrname)
print('Done!!!')