forked from geoporttishare/Helsinki_GreenView
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2_metadataCollector.py
117 lines (89 loc) · 4.56 KB
/
2_metadataCollector.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
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 31 14:45:24 2018
"""
#This function is used to collect the metadata of the GSV panoramas based on the sample point shapefile
# Copyright(C) Xiaojiang Li, Ian Seiferling, Marwa Abdulhai, Senseable City Lab, MIT
# Modified by Akseli Toikka and Ville Mäkinen Finnish Geospatial Research Institute FGI. National Land Survey of Finland
def GSVpanoMetadataCollector(samplesFeatureClass,num,ouputTextFolder):
'''
This function is used to call the Google API url to collect the metadata of
Google Street View Panoramas. The input of the function is the shapefile of the created sample points. If a GSV panorama exists within
50 meters of the sample point location, the Google API returns the metadata of that panorama.
The output is the generated panoinfo matrics stored in the text file
Parameters:
samplesFeatureClass: the shapefile of the created sample points
num: the number of sites proced every time
ouputTextFolder: the output folder for the panoinfo
'''
import urllib,urllib2
import xmltodict
import cStringIO
import ogr, osr
import time
import os,os.path
if not os.path.exists(ouputTextFolder):
os.makedirs(ouputTextFolder)
driver = ogr.GetDriverByName('ESRI Shapefile')
# change the projection of shapefile to the WGS84
dataset = driver.Open(samplesFeatureClass)
layer = dataset.GetLayer()
sourceProj = layer.GetSpatialRef()
targetProj = osr.SpatialReference()
targetProj.ImportFromEPSG(4326)
transform = osr.CoordinateTransformation(sourceProj, targetProj)
# loop all the features in the featureclass
feature = layer.GetNextFeature()
featureNum = layer.GetFeatureCount()
batch = featureNum/num
for b in range(batch):
# for each batch process num GSV site
start = b*num
end = (b+1)*num
if end > featureNum:
end = featureNum
ouputTextFile = 'Pnt_start%s_end%s.txt'%(start,end)
ouputGSVinfoFile = os.path.join(ouputTextFolder,ouputTextFile)
# skip over those existing txt files
if os.path.exists(ouputGSVinfoFile):
continue
time.sleep(1)
with open(ouputGSVinfoFile, 'w') as panoInfoText:
# process num feature each time
for i in range(start, end):
feature = layer.GetFeature(i)
geom = feature.GetGeometryRef()
# trasform the current projection of input shapefile to WGS84
#WGS84 is Earth centered, earth fixed terrestrial ref system
geom.Transform(transform)
lon = geom.GetX()
lat = geom.GetY()
key = #Input Your Key here
# get the meta data of panoramas
urlAddress = r'http://maps.google.com/cbk?output=xml&ll=%s,%s'%(lat,lon)
time.sleep(0.05)
# the output result of the meta data is a xml object
metaDataxml = urllib2.urlopen(urlAddress)
metaData = metaDataxml.read()
data = xmltodict.parse(metaData)
# in case there is not panorama in the site, therefore, continue
if data['panorama']==None:
continue
else:
panoInfo = data['panorama']['data_properties']
# get the meta data of the panorama
panoDate = panoInfo.items()[4][1]
panoId = panoInfo.items()[5][1]
panoLat = panoInfo.items()[8][1]
panoLon = panoInfo.items()[9][1]
print 'The coordinate (%s,%s), panoId is: %s, panoDate is: %s'%(panoLon,panoLat,panoId, panoDate)
lineTxt = 'panoID: %s panoDate: %s longitude: %s latitude: %s\n'%(panoId, panoDate, panoLon, panoLat)
panoInfoText.write(lineTxt)
panoInfoText.close()
# ------------Main Function -------------------
if __name__ == "__main__":
import os, os.path
root = #root of the working directory
inputShp = os.path.join(root,#input shapefile of sample points)
outputTxt = root
GSVpanoMetadataCollector(inputShp,1000,outputTxt)