-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMetadataToUTMv2.py
349 lines (294 loc) · 13.2 KB
/
MetadataToUTMv2.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
#from tkinter import *
import tkinter
from tkinter import ttk
from tkinter import filedialog
from pyproj import Transformer
from natsort import os_sorted
import tablib
from difflib import SequenceMatcher
from GPSPhoto import gpsphoto
import os
import simplekml
# def graphicalMenu():
# root = Tk()
# root.title("Metadata to UTM v2")
# mainFrame = ttk.Frame(root, padding = "3 3 12 12")
# mainFrame.grid(column = 0, row = 0, sticky = (N, W, E, S))
# root.columnconfigure(0, weight = 1)
# root.rowconfigure(0, weight = 1)
# operationSelection = BooleanVar()
# metadataExtractionRadio = Radiobutton(root, text="Metadata Extraction", variable = operationSelection, value = True)
# metadataExtractionRadio.grid(column=2, row=1, sticky=(W, E))
# coordinateConversionRadio = Radiobutton(root, text="Coordinate Conversion", variable = operationSelection, value = False)
# coordinateConversionRadio.grid(column=3, row=1, sticky=(W, E))
# for child in mainFrame.winfo_children():
# child.grid_configure(padx=5, pady=5)
# root.mainloop()
def mainMenu():
print("METADATA TO UTM CONVERTER".center(80))
print("===============\n".center(80))
print ("[C] - Coordinate Conversion")
print ("[M] - Metadata Extraction")
print ("\n[X] - Exit Program")
userInput = input("\nSelection: ")
if userInput == "C" or userInput == "c":
coordinateConversionMenu()
elif userInput == "M" or userInput == "m":
metadataMainMenu()
elif userInput == "X" or userInput == "x":
exitProgram()
else:
print ("\nPlease select a valid option!")
mainMenu()
exitProgram()
def metadataMainMenu():
print ("[L] - Output Metadata to Latitude/Longitude format.")
print ("[U] - Output Metdata to UTM zone 16N format.")
print ("[S] - Output Metadata to State Plane - NAD83 West Georgia (US Survey ft.)")
print ("[C] - Output Metadata to custom EPSG format.")
print ("[P] - Output Metadata to Google Earth Path. (.kml)")
print ("[G] - Output Metadata to Google Earth Points (.kml)")
print ("\n[X] - Exit Program")
userInput = input("\nSelection: ")
if userInput == "X" or userInput == "x":
exitProgram()
elif userInput == "":
print("This is not a valid input. Please try again")
mainMenu()
folder = getFolder()
importedData = importMetadata(folder)
importedData = appendMetadata(importedData)
kml = False
utm = False
if userInput == "L" or userInput == "l":
convertedCoordinates = importedData
elif userInput == "U" or userInput == "u":
convertedCoordinates = convertCoordinates(importedData, 4326, 26916)
utm = True
elif userInput == "S" or userInput == "s":
convertedCoordinates = convertCoordinates(importedData, 4326, 2240)
utm = True
elif userInput == "C" or userInput == "c":
epsg = getEPSG
convertedCoordinates = convertCoordinates(importedData, 4326, epsg)
utm = True
elif userInput == "P" or userInput == "p":
writtenData = writeLineString(importedData)
kml = True
elif userInput == "G" or userInput == "g":
writtenData = writePoints(importedData)
kml = True
if kml : writtenData = writtenData
else : writtenData = writeData(convertedCoordinates, utm)
exportData(folder, writtenData, kml)
def coordinateConversionMenu():
print("[L] - State Plane to Latitude/Longitude Conversion")
print("[S] - Latitude/Longitude to State Plane Conversion")
print("[B] - State Plane to State Plane Conversion")
print("[X] - Exit")
userInput = input("\nSelection: ")
if userInput == "X" or userInput == "x":
exitProgram()
elif userInput == "":
print("This is not a valid input. Please try again")
mainMenu()
file = getFile()
data = importData(file)
EPSGFrom = 0
EPSGTo = 0
utm = False
print("This conversion format will accept any EPSG codes as a source and/or target.")
print("Some useful codes: \n1. [26916] - NAD83 / UTM zone 16N\n2. [4326] - WGS 84 Latitude/Longitude\n3. [2240] - NAD83 State Plane West Georgia (US Survey ft.)")
continueInput = input("Press `Enter` to continue...")
if userInput == "L" or userInput == "l":
EPSGFrom = getEPSG(True)
EPSGTo = 4326
elif userInput == "S" or userInput == "s":
EPSGFrom = 4326
EPSGTo = getEPSG()
utm = True
elif userInput == "B" or userInput == "b":
EPSGFrom = getEPSG(True)
EPSGTo = getEPSG()
utm = True
convertedCoordinates = convertCoordinates(data, EPSGFrom, EPSGTo)
writtenData = writeData(convertedCoordinates, utm)
exportData(os.path.dirname(file), writtenData)
def getEPSG(gettingFrom = False): #Prompts the user for desired EPSG formats to convert to/from
if gettingFrom:
print("Please input the EPSG Zone that the coordinates are in.")
else:
print("Please input the EPSG Zone that you are converting to.")
try:
EPSGZone = int(input("EPSG Zone: "))
except:
print ("Please enter only digits for the EPSG Zone.")
getEPSG(gettingFrom)
return EPSGZone
def getFolder(): #responsible for displaying the UI and collecting desired selections
while True:
try:
window = tkinter.Tk()
window.wm_attributes("-topmost", 1)
window.withdraw()
workingDir = filedialog.askdirectory(initialdir = "/", title = "Select the directory which contains your images")
except:
workingDir = input("Enter the directory which contains your images.")
if os.path.exists(workingDir): #pathlib is incompatible with pyinstaller, so I'm using os.path
return workingDir
elif workingDir == "":
exitProgram()
else:
print("This is not a valid .csv file! Please try again.")
getFolder()
def getFile(): #responsible for displaying the UI and collecting desired selections
while True:
try:
window = tkinter.Tk()
window.wm_attributes("-topmost", 1)
window.withdraw()
workingFile = filedialog.askopenfilename(parent = window, filetypes = [("CSV Files", "*.csv")], initialdir = "/", title = "Select the directory which contains your coordinates.") #Tkinter allows for the creation of pop-up file directories
except:
workingFile = input("Enter the directory which contains your coordinates.")
if os.path.exists(workingFile): #pathlib is incompatible with pyinstaller, so I'm using os.path
return workingFile
elif workingFile == "":
exitProgram()
else:
print("This is not a valid .csv file! Please try again.")
getFile()
def importMetadata(workingDir): #responsible for extracting metadata from the images themselves
filenames = []
latitudes = []
longitudes = []
altitudes = []
files = os_sorted(os.listdir(workingDir)) #os_sorted from natsort allows the files to sort as '1, 2, 3, 4...20, 21..., 30, 31' instead of '1, 10, 11... 2, 20, 21'
for file in files:
if file.lower().endswith((".png", ".jpg", ".jpeg")): #a previous iteration of this only allowed for jpgs, but this also serves as a quick check that we are actually working with images to avoid IO errors
try:
data = gpsphoto.getGPSData(os.path.join(workingDir, file))#the GPSPhoto library is by far the easiest way to extract the relevant metadata for this
#altitude = gpsinfo.getAlt(os.path.join(workingDir, item))#The altitude is pulled seperately
filenames.append(file)
latitudes.append(data["Latitude"])
longitudes.append(data["Longitude"])
altitudeInFeet = data["Altitude"]*3.28084 #GPSPhoto automatically outputs the elevation in meters, so we are converting it to feet
altitudes.append(altitudeInFeet)
except:
print(f"There was an error reading metadata from: {file}")#If an image does not have the relevant metadata, it will except and stop the whole script, so this handles that situation
importedMetadata = [filenames, latitudes, longitudes, altitudes]
return importedMetadata
def getHeader(data, toFind): #responsible for finding existing headers which roughly match a desired heading such as LATITUDE or NORTHING
headers = data.headers
for header in headers:
if SequenceMatcher(None, toFind, header.lower()).ratio() > 0.75:
return str(header)
raise ValueError(f'{toFind} not found')
exitProgram()
def normalizeData(importedData): #responsible for putting the image metadata into a format similar to what is expected to when the program is importing from a csv file.
names = []
x = []
y = []
altitudes = []
try:
x = importedData[getHeader(importedData, "easting")]
y = importedData[getHeader(importedData, "northing")]
except:
x = importedData[getHeader(importedData, "latitude")]
y = importedData[getHeader(importedData, "longitude")]
try:
altitudes = importedData[getHeader(importedData, "altitude")]
except:
for point in x:
altitudes.append(0)
finally:
i = 0
for point in x:
names.append(i)
i += 1
normalizedData = [names, x, y, altitudes]
return normalizedData
def importData(workingFile):
try:
with open(workingFile, 'r') as file:
importedData = tablib.Dataset().load(file)
normalizedData = normalizeData(importedData)
return normalizedData
except:
print("There was an error importing the data.\n Please ensure the file exists where specified and is in the correct format.")
exitProgram()
def appendMetadata(data):
print("Would you like to append additional metadata to the table?")
print("[Y]es, [N]o?, or E[x]it:")
userInput = input()
if userInput == "Y" or userInput == "y":
originalData = data
newFolder = getFolder()
newData = importMetadata(newFolder)
for name, x, y, altitude in zip(*newData):
originalData[0].append(name)
originalData[1].append(x)
originalData[2].append(y)
originalData[3].append(altitude)
return appendMetadata(originalData)
elif userInput == "N" or userInput == "n":
return data
elif userInput == "X" or userInput == "x":
exitProgram()
else:
print("Please choose a valid option: [Y] or [N]")
return appendMetadata(data)
def convertCoordinates(importedData, EPSGFrom, EPSGTo):
names = []
convertedX = []
convertedY = []
altitudes = []
transformer = Transformer.from_crs(f'EPSG:{EPSGFrom}', f'EPSG:{EPSGTo}', always_xy=True)
for name, y, x, altitude in zip(*importedData):
names.append(name)
convertedCoordinate = transformer.transform(x, y)
convertedX.append(convertedCoordinate[0])
convertedY.append(convertedCoordinate[1])
altitudes.append(altitude)
convertedCoordinates = [names, convertedX, convertedY, altitudes]
return convertedCoordinates
def writeData(convertedCoordinates, UTM = False):
data = tablib.Dataset()
data.append_col(convertedCoordinates[0], header = "NAME") #apparently with Tablib if you just append a column to an empty dataset, it will not generate the headers field properly
data.headers = ["NAME", ] #therefore, we add the headers column after appending for some reason. It works.
if UTM:
data.append_col(convertedCoordinates[2], header="NORTHING")
data.append_col(convertedCoordinates[1], header="EASTING")
else:
data.append_col(convertedCoordinates[1], header="LATITUDE")
data.append_col(convertedCoordinates[2], header = "LONGITUDE")
data.append_col(convertedCoordinates[3], header="ALTITUDE")
return data
def writeLineString(convertedCoordinates):
kml = simplekml.Kml()
lineString = kml.newlinestring(name = "Output")
coords = []
for pointName, latitude, longitude, altitude in zip(*convertedCoordinates): # Don't need the pointName variable, but it appears to be necessary to get zip() to work properly in this configuration
coords.append((longitude, latitude, altitude))
lineString.coords = coords
return kml
def writePoints(convertedCoordinates):
kml = simplekml.Kml()
for pointName, latitude, longitude, altitude in zip(*convertedCoordinates):
point = kml.newpoint(name=pointName)
point.coords = [(longitude, latitude, altitude)]
return kml
def exportData(folder, data, kml = False):
extension = ".kml" if kml else ".csv"
outputPath = os.path.join(os.path.realpath(folder), f'output{extension}')
if kml:
data.save(outputPath)
else:
with open(outputPath, 'w', newline='') as outputFile:
outputFile.write(data.csv)
def exitProgram():
print("Would you like to convert more images?")
exitInput = input("[Y]es or [N]o: ")
if exitInput == "Y" or exitInput == "y" : mainMenu()
else : quit()
mainMenu()
#mainMenu()