Python Forum
Working code to build GPX from geotagged JPGs?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Working code to build GPX from geotagged JPGs?
#1
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.
Reply
#2
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.
Reply
#3
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}")
Reply
#4
… 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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  New to Python - Not sure why this code isn't working - Any help appreciated TheGreatNinx 4 962 Jul-22-2023, 10:21 PM
Last Post: Pedroski55
  code not working when executed from flask app ThomasDC 1 892 Jul-18-2023, 07:16 AM
Last Post: ThomasDC
  I am new to python and Could someone please explain how this below code is working? kartheekdas 2 1,017 Dec-19-2022, 05:24 PM
Last Post: kartheekdas
Exclamation My code is not working as I expected and I don't know why! Marinho 4 1,083 Oct-13-2022, 08:09 PM
Last Post: deanhystad
  My Code isn't working... End3r 4 1,931 Mar-21-2022, 10:12 AM
Last Post: End3r
  |SOLVED] Glob JPGs, read EXIF, update file timestamp? Winfried 5 2,488 Oct-21-2021, 03:29 AM
Last Post: buran
  I don't undestand why my code isn't working. RuyCab 2 1,992 Jun-17-2021, 03:06 PM
Last Post: RuyCab
  code is not working , can anybody help? RandomPerson69 4 2,913 Mar-22-2021, 04:24 PM
Last Post: deanhystad
  Short code for EventGhost not working Patricia 8 3,689 Feb-09-2021, 07:49 PM
Last Post: Patricia
  Code no longer working yk303 14 10,180 Dec-21-2020, 10:58 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020