-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.py
More file actions
executable file
·89 lines (77 loc) · 3.16 KB
/
Copy pathmap.py
File metadata and controls
executable file
·89 lines (77 loc) · 3.16 KB
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
#!/bin/python
# update jigl generated info pages with a google maps iframe which
# corresponds to the GPS coordinates in the image EXIF data
# expects the name of an jigl generated *_info.html file to update as a parameter
from __future__ import division
import os
import sys
import re
import fileinput
if len(sys.argv) != 2:
print('\nMissing filename argument')
sys.exit(1)
filename = str(sys.argv[1])
print(f'\nProcessing {filename}')
NS_regex = r'^(<nobr> : ){1}[SN]{1}\s+\d{1,3}d\s+\d{1,2}m\s+\d{1,2}\.\d{1,4}(s<br>)$'
EW_regex = r'^(<nobr> : ){1}[EW]{1}\s+\d{1,3}d\s+\d{1,2}m\s+\d{1,2}\.\d{1,4}(s<br>)$'
comment_html = '<nobr>Comment<br>\n'
map_html = '<nobr>Map : <br>\n'
nobr_data_br_regex = r'^(<nobr>){1}[A-Za-z\/\.\s]+(<br>){1}$'
nobr_data_br_counter = 1
nobr_2space_regex = '^(<nobr> : ){1}.+$'
nobr_2space = '<nobr> : '
nobr_2space_counter = 0
rewriteFileNS = False
rewriteFileEW = False
def fix_encoding(filename):
"""Fix encoding issues."""
tmp_file = '/tmp/0.html'
iconv_cmd = f'iconv -t utf-8//IGNORE {filename} > {tmp_file} ; mv {tmp_file} {filename}'
os.system(iconv_cmd)
def getCoords(coords):
negative = ''
direction = coords.split(';')[-1].split()[0]
if direction == 'S' or direction == 'W':
negative = '-'
deg = float(coords.split(';')[-1].split()[1].replace('d',''))
minute = float(coords.split(';')[-1].split()[2].replace('m','')) / 60
second = float(coords.split(';')[-1].split()[3].replace('s<br>','')) / 3600
newCoords = str(deg + minute + second)
return f'{negative}{newCoords}'
try:
for l in fileinput.input([filename], openhook=fileinput.hook_encoded("utf-8")):
continue
except UnicodeDecodeError:
print(f'fixing {filename} encoding')
fix_encoding(filename)
for line in fileinput.FileInput([filename]):
match_nobr_data = re.match(nobr_data_br_regex, line)
if match_nobr_data:
nobr_data_br_counter += 1
matchObj = re.match(NS_regex, line)
if matchObj:
rewriteFileNS = True
NS = matchObj.group()
NS_coords = getCoords(NS)
matchObj = re.match(EW_regex, line)
if matchObj:
rewriteFileEW = True
EW = matchObj.group()
EW_coords = getCoords(EW)
# add vertical spacers for field name column
spacer = '<nobr><br>\n' * ( nobr_data_br_counter + ( 22 - nobr_data_br_counter ))
if rewriteFileNS and rewriteFileEW:
mapHTML = '''
<nobr><iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"
src="https://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q={},{}&aq=&sll=37.6,-95.665&sspn=48.408958,76.201172&ie=UTF8&t=m&z=11&ll={},{}&output=embed">
</iframe><br>
'''.format(NS_coords, EW_coords, NS_coords, EW_coords)
for line in fileinput.FileInput([filename], inplace=True):
if line == comment_html:
sys.stdout.write (line + map_html + spacer)
else:
sys.stdout.write (line)
if re.match(nobr_2space_regex, line):
nobr_2space_counter += 1
if (nobr_2space_counter + 1) == nobr_data_br_counter:
sys.stdout.write (mapHTML)