-
Notifications
You must be signed in to change notification settings - Fork 0
/
fototool.py
289 lines (208 loc) · 7.46 KB
/
fototool.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
#-------------------------------------------------------------------------------
# Name: module1
# Purpose:
#
# Author: EWO
#
# Created: 31-10-2019
# Copyright: (c) EWO 2019
# Licence: MIT
#------------------------------------------------------------------------------
import os
import piexif
from fractions import Fraction
import pandas as pd
import simpledbf
import numpy as np
from numpy import nan
from datetime import datetime
def to_deg(value, loc):
"""convert decimal coordinates into degrees, munutes and seconds tuple
Keyword arguments: value is float gps-value, loc is direction list ["S", "N"] or ["W", "E"]
return: tuple like (25, 13, 48.343 ,'N')
"""
if value < 0:
loc_value = loc[0]
elif value > 0:
loc_value = loc[1]
else:
loc_value = ""
abs_value = abs(value)
deg = int(abs_value)
t1 = (abs_value-deg)*60
min = int(t1)
sec = round((t1 - min)* 60, 5)
return (deg, min, sec, loc_value)
def change_to_rational(number):
"""convert a number to rantional
Keyword arguments: number
return: tuple like (1, 2), (numerator, denominator)
"""
f = Fraction(str(number))
return (f.numerator, f.denominator)
def set_gps_location(file_name, lat, lng, altitude):
"""Adds GPS position as EXIF metadata and maintains date data.
Keyword arguments:
file_name -- image file
lat -- latitude (as float)
lng -- longitude (as float)
altitude -- altitude (as float)
"""
# read exif from picture
exif_dict_old = piexif.load(file_name)
# try to read image creation date from exif
try:
old_date = exif_dict_old['Exif'][36867]
# print old_date
# use file creation date if exif date is not available
except:
mtime = os.path.getmtime(file_name)
# convert binary date format to exif date format
old_date = datetime.fromtimestamp(mtime).strftime('%Y:%m:%d %H:%M:%S')
lat_deg = to_deg(lat, ["S", "N"])
lng_deg = to_deg(lng, ["W", "E"])
exiv_lat = (change_to_rational(lat_deg[0]), change_to_rational(lat_deg[1]), change_to_rational(lat_deg[2]))
exiv_lng = (change_to_rational(lng_deg[0]), change_to_rational(lng_deg[1]), change_to_rational(lng_deg[2]))
gps_ifd = {
piexif.GPSIFD.GPSVersionID: (2, 0, 0, 0),
piexif.GPSIFD.GPSAltitudeRef: 1,
piexif.GPSIFD.GPSAltitude: change_to_rational(round(altitude)),
piexif.GPSIFD.GPSLatitudeRef: lat_deg[3],
piexif.GPSIFD.GPSLatitude: exiv_lat,
piexif.GPSIFD.GPSLongitudeRef: lng_deg[3],
piexif.GPSIFD.GPSLongitude: exiv_lng,
}
exif_ifd = {piexif.ExifIFD.DateTimeOriginal: old_date
}
# print file_name
# print exif_ifd
exif_dict = {"Exif":exif_ifd, "GPS": gps_ifd}
exif_bytes = piexif.dump(exif_dict)
piexif.insert(exif_bytes, file_name)
class RDWGSConverter:
"""Converts RD_new to WGS84.
Keyword arguments:
"""
X0 = 155000
Y0 = 463000
phi0 = 52.15517440
lam0 = 5.38720621
def fromRdToWgs( self, coords ):
Kp = [0,2,0,2,0,2,1,4,2,4,1]
Kq = [1,0,2,1,3,2,0,0,3,1,1]
Kpq = [3235.65389,-32.58297,-0.24750,-0.84978,-0.06550,-0.01709,-0.00738,0.00530,-0.00039,0.00033,-0.00012]
Lp = [1,1,1,3,1,3,0,3,1,0,2,5]
Lq = [0,1,2,0,3,1,1,2,4,2,0,0]
Lpq = [5260.52916,105.94684,2.45656,-0.81885,0.05594,-0.05607,0.01199,-0.00256,0.00128,0.00022,-0.00022,0.00026]
dX = 1E-5 * ( coords[0] - self.X0 )
dY = 1E-5 * ( coords[1] - self.Y0 )
phi = 0
lam = 0
for k in range(len(Kpq)):
phi = phi + ( Kpq[k] * dX**Kp[k] * dY**Kq[k] )
phi = self.phi0 + phi / 3600
for l in range(len(Lpq)):
lam = lam + ( Lpq[l] * dX**Lp[l] * dY**Lq[l] )
lam = self.lam0 + lam / 3600
return [phi,lam]
def importExcel(name):
""" Retrieves RD coordinates from pandas dataframe, returns WSG84.
Keyword arguments:
"""
# name = name.replace('-','_')
#df_select = df.loc[df.iloc[:,0].apply(str) == name.lower()]
df_select = df[pd.Series(df.loc[:,fotocol]).str.contains(name, case=False)]
# print df_select
# df_select_row = df_select[['XIN', 'YIN']]
df_select_row = df_select[[xstring, ystring]]
coords = [0.0, 0.0]
try:
coords = [df_select_row[xstring].item(), df_select_row[ystring].item()]
coords = [float(i) for i in coords]
except:
# global fail
# fail = fail + 1
# print name + 'fail2'
pass
nan = 0.0
# print coords
if coords == [u'null', u'null'] or coords == [0.0, 0.0] or coords == [np.nan, np.nan] or coords == [nan, nan] or df_select_row.isnull().values.any():
global nocoords
nocoords = nocoords + 1
return False
else:
conv = RDWGSConverter()
wgsCoords = conv.fromRdToWgs( coords )
return wgsCoords
# .CSV, .XLS or GBD containing RD coordinates and image name
file_name = r'C:\...'# path to file + file name
# folder with itmages
folderPath = r"C:\..."
#file_name_path = r'C:\\'
#folderList_path = os.listdir(file_name_path)
#file_name = file_name_path+'\\'+folderList_path[2]
fail = 0
succes = 0
nocoords = 0
# df = "test"
fotocol = 'FOTONR' # column name with foto id or object id
xstring = 'X' # column with X coordinates.
ystring = 'Y' # column with Y coordinates.
if 'eilschalen' in file_name or 'eilschaal' in file_name:
xstring = 'X_PEIL'
ystring = 'Y_PEIL'
elif 'uiker' in file_name or 'yphon' in file_name or 'ifon' in file_name or 'luizen' in file_name:
xstring = 'XIN'
ystring = 'YIN'
elif 'rug' in file_name:
xstring = 'X'
ystring = 'Y'
elif 'ut' in file_name:
xstring = 'XPUT'
ystring = 'YPUT'
elif 'dam' in file_name:
xstring = 'XLINKS'
ystring = 'YLINKS'
else:
xstring = 'X' # column with X coordinates.
ystring = 'Y' # column with Y coordinates.
sheet = 1 # sheet name or sheet number or list of sheet numbers and names
# try to read table
# df = pd.read_csv(file_name, sep = ';', thousands='.', decimal=",")
try:
df = pd.read_excel(io=file_name, sheet_name=sheet)
# print df
df = df.stack().str.replace(',','.').unstack()
print df
except:
try:
df = pd.read_csv(file_name, sep = ';', thousands='.', decimal=",")
# df = df.stack().str.replace(',','.').unstack()
df[xstring] = pd.to_numeric(df[xstring],errors='coerce')
df[ystring] = pd.to_numeric(df[ystring],errors='coerce')
except:
print 'ok'
dbf = simpledbf.Dbf5(file_name, codec='utf-8')
print dbf
df = dbf.to_dataframe()
df = df.stack().str.replace(',','.').unstack()
print df
print df
#list all files in directory
folderList = os.listdir(folderPath)
print folderList
for i in folderList:
print i
coord = importExcel(i.lstrip("0")[:-4])
# print coord
if (coord != False):
fotoUrl = folderPath+'\\'+i
set_gps_location(fotoUrl,coord[0],coord[1],0)
succes = succes + 1
else:
print i +' fail'
print coord
fail = fail + 1
print 'fail ' + str(fail-nocoords)
print 'nocoords ' + str(nocoords)
print 'succes ' + str(succes)