-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKML_Creator.py
68 lines (58 loc) · 2.21 KB
/
KML_Creator.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
import os
import simplekml
from GPSPhoto import gpsphoto
from PIL import Image
from PIL.ExifTags import TAGS
def get_exif(image_file_path):
exif_table = {}
image = Image.open(image_file_path)
info = image.getexif()
for tag, value in info.items():
decoded = TAGS.get(tag, tag)
exif_table[decoded] = value
return exif_table
def create_KML(dirPath, savePath, count, icon, size, kml):
# Get all the files in the current directory.
files = os.listdir(dirPath)
# Iterate over all the files.
for file in files:
# Check if the file is an image file.
filePath = dirPath + '\\' + file
if os.path.isfile(filePath) and file.endswith('.jpg') or file.endswith('.png'):
try:
data = gpsphoto.getGPSData(filePath)
long = data["Longitude"]
lat = data["Latitude"]
except:
try:
f = open("error_logging.txt", "a")
# writing in the file
f.write(str(filePath + ' No coordinates\n'))
# closing the file
f.close()
except PermissionError:
continue
continue
# Apply the function to the file.
pnt = kml.newpoint(name=file)
pnt.coords = [(long, lat)]
pnt.description = fr'<img style="max-width:{size}px;" src="file:///{filePath}">'
style = simplekml.Style()
if icon:
pnt.style.iconstyle.icon.href = filePath
else:
pnt.style.iconstyle.icon.href = r'http://maps.google.com/mapfiles/kml/shapes/square.png'
pnt.style.iconstyle.scale = 0.66
pnt.style.labelstyle.scale = 0
# Check if the file is a directory.
elif os.path.isdir(filePath):
count += 1
# Recursively call the function on the directory.
create_KML(filePath, savePath, count, icon, size, kml)
def run_KML(dirPath, savePath, count, icon, size):
kml = simplekml.Kml()
f = open("error_logging.txt", "a")
f.write('')
f.close()
create_KML(dirPath, savePath, count, icon, size, kml)
kml.save(savePath)