/
Project.py
380 lines (317 loc) · 7.7 KB
/
Project.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#Team 43, Project 2, CST 205
#Eric Haro, Lesley Amezcua, David Beach
#This program extracts GPS metadata from an iPhone image, and displays it
#on a map
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import argparse
from PIL import Image
from PIL.ExifTags import TAGS
import random
import os
#this asks for the path of the image
#askimg = raw_input("path your image: ")
askFolder = raw_input("name of image folder: ")
#this creates the file name to be created and tells user
randomnumber = random.random()
strrandomnumber = str(randomnumber) + '.txt'
datafile = strrandomnumber
#datafile = askimg.split(".")[0] + '.txt'
print "Exif data location: " + datafile
#'askimg' is the path of the image
#imgpath = askimg
#exif scraper
#creates a text file after the name of the image that ends with ".txt"
def getMetaData(imgname, out): # imagename is the name of image and out is out of text file
try:
metaData = {}
imgFile = Image.open(imgname)
#print "Getting meta data..."
info = imgFile._getexif()
if info:
#print "found meta data!"
for (tag, value) in info.items():
tagname = TAGS.get(tag, tag)
metaData[tagname] = value
if not out:
print tagname, value
if out:
#print "Outputting to file..."
with open(out, 'w') as f:
for (tagname, value) in metaData.items():
f.write(str(tagname)+"\t"+\
str(value)+"\n")
except:
print "Failed"
#look for the 3 numbers used for the North direction and puts them into the correct format for the tranlator
#returns the latitudes found
def North(phrase):
tempLatList = []
i = phrase.index('N')
comma = 0
while True:
if phrase[i] == ",":
comma += 1
if comma == 2:
t = i
end = 0
while True:
onelat = []
if phrase[t] == "(":
while True:
if phrase[t+1] == ",":
end = 1
break
onelat.append(phrase[t+1])
t += 1
if end == 1:
break
t -= 1
onelatstr = ''.join(onelat)
#latlist.append(onelatstr)
tempLatList.append(onelatstr)
comma += 1
if comma == 6:
t = i
end = 0
while True:
onelat = []
if phrase[t] == "(":
while True:
if phrase[t+1] == ",":
end = 1
break
onelat.append(phrase[t+1])
t += 1
if end == 1:
break
t -= 1
onelatstr = ''.join(onelat)
#latlist.append(onelatstr)
tempLatList.append(onelatstr)
comma += 1
if comma == 8:
t = i
end = 0
while True:
onelat = []
if phrase[t] == "(":
while True:
if phrase[t+1] == ",":
end = 1
break
onelat.append(phrase[t+1])
t += 1
if end == 1:
break
t -= 1
onelatstr = ''.join(onelat)
#latlist.append(onelatstr)
tempLatList.append(onelatstr)
comma += 1
break
i += 1
#print latlist
return tempLatList
#look for the 3 numbers used for the west direction and puts them into the correct format for the tranlator
#returns the longitudes found
def West(phrase):
tempLonList = []
if set('W').issubset(phrase):
i = phrase.index('W')
comma = 0
while True:
if phrase[i] == ",":
comma += 1
if comma == 2:
t = i
end = 0
while True:
onelat = []
if phrase[t] == "(":
while True:
if phrase[t+1] == ",":
end = 1
break
onelat.append(phrase[t+1])
t += 1
if end == 1:
break
t -= 1
onelatstr = ''.join(onelat)
#lonlist.append(onelatstr)
tempLonList.append(onelatstr)
comma += 1
if comma == 6:
t = i
end = 0
while True:
onelat = []
if phrase[t] == "(":
while True:
if phrase[t+1] == ",":
end = 1
break
onelat.append(phrase[t+1])
t += 1
if end == 1:
break
t -= 1
onelatstr = ''.join(onelat)
#lonlist.append(onelatstr)
tempLonList.append(onelatstr)
comma += 1
if comma == 8:
t = i
end = 0
while True:
onelat = []
if phrase[t] == "(":
while True:
if phrase[t+1] == ",":
end = 1
break
onelat.append(phrase[t+1])
t += 1
if end == 1:
break
t -= 1
onelatstr = ''.join(onelat)
#lonlist.append(onelatstr)
tempLonList.append(onelatstr)
comma += 1
break
i += 1
return tempLonList
#print lonlist
#look for the 3 numbers used for the west direction and puts them into the correct format for the tranlator
def East():
if set('E').issubset(phrase):
i = phrase.index('E')
comma = 0
while True:
if phrase[i] == ",":
comma += 1
if comma == 2:
t = i
end = 0
while True:
onelat = []
if phrase[t] == "(":
while True:
if phrase[t+1] == ",":
end = 1
break
onelat.append(phrase[t+1])
t += 1
if end == 1:
break
t -= 1
onelatstr = ''.join(onelat)
lonlist.append(onelatstr)
comma += 1
if comma == 6:
t = i
end = 0
while True:
onelat = []
if phrase[t] == "(":
while True:
if phrase[t+1] == ",":
end = 1
break
onelat.append(phrase[t+1])
t += 1
if end == 1:
break
t -= 1
onelatstr = ''.join(onelat)
lonlist.append(onelatstr)
comma += 1
if comma == 8:
t = i
end = 0
while True:
onelat = []
if phrase[t] == "(":
while True:
if phrase[t+1] == ",":
end = 1
break
onelat.append(phrase[t+1])
t += 1
if end == 1:
break
t -= 1
onelatstr = ''.join(onelat)
lonlist.append(onelatstr)
comma += 1
break
i += 1
#maps the latitudes and longitudes list of lats and longs
def mapTut(dLat, dLon):
m = Basemap(width=12000000,height=9000000,projection='lcc',
resolution=None,lat_1=45.,lat_2=55,lat_0=50,lon_0=-107.)
m.bluemarble()
for i in xrange(len(dLat)):
x,y = m(dLon[i],dLat[i])
m.plot(x,y, 'ro')
plt.title("Geo Plotting")
plt.show()
#gets the info from the image and returns the lat and lon after being processed
# Function
def getGPS(imgpath, datafile):
getMetaData(imgpath, datafile)
#img.txt reader
strdatafile = str(datafile)
file = open(strdatafile,'r+')
linenumber = 0
searchlines = file.readlines()
file.close
phrase = []
phrasetemp = []
#this searches for 'GPS' in the exif data file
for i, line in enumerate(searchlines):
if "GPS" in line:
for l in searchlines[i:i+1]:
y = str(l)
#print y
for letter in y:
phrase.append(letter)
#lats = East()
lats = North(phrase)
lons = West(phrase)
#GPS translator
lat = lats
lon = lons
dlat = int(lat[0]) + (float(lat[1])/60.0) + (float(lat[2])/3600.0)
dlon = int(lon[0]) + (float(lon[1])/60.0) + (float(lon[2])/3600.0)
dlat = dlat -0.27
dlon = -dlon + 1.39
return dlat,dlon
#creates a path based on the fodler that the user says they want to use
imageDir = os.getcwd()+"/"+askFolder+"/"
#gets all the images inside of the foolder
folderImages=os.listdir(imageDir)
totalLats = []
totalLons = []
for img in folderImages:
print img
#filters based on the image extension
if(img.split(".")[1] == "JPG" or img.split(".")[1] == "jpg"):
#sends in the path to the image and the name of the textfile that will contain the metadata
imgLat, imgLon = getGPS(imageDir+img, img.split("/")[-1]+".txt")
print "lat: "
print imgLat
print "lon: "
print imgLon
#appends the lats and lons to a list so that they can all be displayed
totalLats.append(imgLat)
totalLons.append(imgLon)
#function call sequence
#getGPS->getMetaData
# ->North
# ->West
#mapPut
#plots the map
mapTut(totalLats,totalLons)