Python Forum

Full Version: Working code to build GPX from geotagged JPGs?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

Before I spend time learning how to do it myself, would someone have a working script to read the GPS coordinates from a bunch of JPGs and build a GPX file?

input directory path
for loop read each JPG
<TAB>read GPX coords from Exif
<TAB>add item to table
Save table to GPX file
Thank you.
Not simple, but totally doable, see: https://developer.ibm.com/technologies/p...t-scanner/
You may also want to search pypi.org for existing packages that you can download.
Thanks.

For others' benefit:
import glob
#pip3 install exifread
import exifread
#pip3 install gpxpy
import gpxpy.gpx

OUTPUT = "myfile.gpx"

#https://programmer.ink/think/python-analysis-of-pictures-to-get-the-shooting-time-and-location.html
def format_lati_long(data):#list2float
	list_tmp=str(data).replace('[', '').replace(']', '').split(',')
	list=[ele.strip() for ele in list_tmp]
	data_sec = int(list[-1].split('/')[0]) /(int(list[-1].split('/')[1])*3600)# Second value
	data_minute = int(list[1])/60
	data_degree = int(list[0])
	result=data_degree + data_minute + data_sec
	return result

gpx = gpxpy.gpx.GPX()
for file in glob.glob('*.jpg'):
	img=exifread.process_file(open(file,'rb'))
	latitude=format_lati_long(str(img['GPS GPSLatitude']))
	longitude=format_lati_long(str(img['GPS GPSLongitude']))
	#print(latitude,longitude)
	
	w1 = gpxpy.gpx.GPXWaypoint(latitude, longitude)
	#w1.name = "blah"
	gpx.waypoints.append(w1)

with open(OUTPUT, "w",encoding='UTF-8') as f:
	f.write(gpx.to_xml())
	print(f"Wrote {OUTPUT}")
… and in case you'd rather save data in a geojson file instead of GPX:

#pip3 install geojson
from geojson import Point, Feature, FeatureCollection, dump
…
features = []
for file in glob.glob('*.jpg'):
	…
	point = Point((latitude, longitude))
	features.append(Feature(geometry=point))
	
feature_collection = FeatureCollection(features)
with open(OUTPUT, 'w') as f:
   dump(feature_collection, f)