Geographic Information Systems Asked on November 6, 2021
I’ve geotagged photos stored locally on my computer, which were converted into points.
But I am not happy about where those points are placed by (not very accurate GPS).
On the map, I know for sure, where points should be. I know, that I can manually move points, but GPS place in photos won’t change if I’ll remove and add them again.
Is there any tool or command to move the geotagged photo and change GPS place in a photo with it.
I usually use pyexiv2 to set exif information in JPG files, but when I import the library in a script QGIS script crash.
I found a solution using the library exif:
https://pypi.org/project/exif/
It's so easy to use, and with Qgis I don,'t have any problem.
In this code I insert GPS coordinates to a snapshot of screen:
from exif import Image
with open(file_name, 'rb') as image_file:
my_image = Image(image_file)
my_image.make = "Python"
my_image.gps_latitude_ref=exif_lat_ref
my_image.gps_latitude=exif_lat
my_image.gps_longitude_ref= exif_lon_ref
my_image.gps_longitude= exif_lon
with open(file_name, 'wb') as new_image_file:
new_image_file.write(my_image.get_file())
Answered by RBenet on November 6, 2021
This script will take as an input a folder of images, and a shapefile. The shapefile needs to be attributed with the image name, and the lat and long of the point. The script will write to the EXIF tag of the image the lat and long from the attribute table. After execution the image location will be the same as the point location. The script requires the pyexiv2 library and therefore, only works with Python 2.7 and ArcGIS 10.x. I will port it to Python 3 if anyone asks. Make a backup of your data before you run this code!
import pyexiv2, traceback, sys, arcpy
#the folder of images...
inFolder = "C:/gTemp/WriteEXIFtest/sfnooksack_lo"
#The points that show the correct image locations...
inFC = 'C:/gTemp/WriteEXIFtest/sfnooksack_lo.shp'
#the shapefile needs to have attributes with the image name, lat and long (as DD)
fields = ['IMAGE', 'lat', 'long']
try:
def ddlongtodms(dd):
#converts decimal degrees to degrees minutes and seconds.
if dd < 0:
dd = abs(dd)
GPSLongitudeRef = 'W'
else:
GPSLongitudeRef = 'E'
minutes,seconds = divmod(dd*3600,60)
degrees,minutes = divmod(minutes,60)
return (GPSLongitudeRef,int(degrees),int(minutes),seconds)
def ddlattodms(dd):
#converts decimal degrees to degrees minutes and seconds.
if dd < 0:
dd = abs(dd)
GPSLatitudeRef = 'S'
else:
GPSLatitudeRef = 'N'
minutes,seconds = divmod(dd*3600,60)
degrees,minutes = divmod(minutes,60)
return (GPSLatitudeRef,int(degrees),int(minutes),seconds)
with arcpy.da.SearchCursor(inFC, fields) as cursor:
for row in cursor:
#in this case the image name does not have a .jpg suffix so I add it here...
imagePath = inFolder +'/'+row[0]+'.jpg'
print imagePath
pointLong = ddlongtodms(row[2])
pointLat = ddlattodms(row[1])
print pointLong
print pointLat
try:
metadata = pyexiv2.ImageMetadata(imagePath)
metadata.read()
latitude = [pyexiv2.Rational(pointLat[1],1),pyexiv2.Rational(pointLat[2], 1),pyexiv2.Rational(int(pointLat[3]*100000), 100000)]
longitude = [pyexiv2.Rational(pointLong[1],1),pyexiv2.Rational(pointLong[2], 1),pyexiv2.Rational(int(pointLong[3]*100000), 100000)]
metadata['Exif.GPSInfo.GPSLatitude'] = latitude
metadata.write()
metadata['Exif.GPSInfo.GPSLongitude'] = longitude
metadata.write()
metadata['Exif.GPSInfo.GPSLatitudeRef'] = pointLat[0]
metadata.write()
metadata['Exif.GPSInfo.GPSLongitudeRef'] = pointLong[0]
metadata.write()
except:
print "something bad happened with this file..."
print "Done"
except:
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
pymsg = "PYTHON ERRORS:nTraceback info:n" + tbinfo + "nError Info:n" + str(sys.exc_info()[1])
print pymsg + "n"
And this should work for Python 3 but the Python interpreter is locked in a walled garden in ArcPro. ESRI may have blocked the ability to add the pyexiv2 library. In that case you may need to make a clone.
import pyexiv2, traceback, sys, arcpy
#the folder of images...
inFolder = "C:/gTemp/WriteEXIFtest/sfnooksack_lo"
#The points that show the correct image locations...
inFC = 'C:/gTemp/WriteEXIFtest/sfnooksack_lo.shp'
#the shapefile needs to have attributes with the image name, lat and long (as DD)
fields = ['IMAGE', 'lat', 'long']
try:
def ddlongtodms(dd):
#converts decimal degrees to degrees minutes and seconds.
if dd < 0:
dd = abs(dd)
GPSLongitudeRef = 'W'
else:
GPSLongitudeRef = 'E'
minutes,seconds = divmod(dd*3600,60)
degrees,minutes = divmod(minutes,60)
return (GPSLongitudeRef,int(degrees),int(minutes),seconds)
def ddlattodms(dd):
#converts decimal degrees to degrees minutes and seconds.
if dd < 0:
dd = abs(dd)
GPSLatitudeRef = 'S'
else:
GPSLatitudeRef = 'N'
minutes,seconds = divmod(dd*3600,60)
degrees,minutes = divmod(minutes,60)
return (GPSLatitudeRef,int(degrees),int(minutes),seconds)
with arcpy.da.SearchCursor(inFC, fields) as cursor:
for row in cursor:
#in this case the image name does not have a .jpg suffix so I add it here...
imagePath = inFolder +'/'+row[0]+'.jpg'
pointLong = ddlongtodms(row[2])
pointLat = ddlattodms(row[1])
try:
metadata = pyexiv2.ImageMetadata(imagePath)
metadata.read()
latitude = [pyexiv2.Rational(pointLat[1],1),pyexiv2.Rational(pointLat[2], 1),pyexiv2.Rational(int(pointLat[3]*100000), 100000)]
longitude = [pyexiv2.Rational(pointLong[1],1),pyexiv2.Rational(pointLong[2], 1),pyexiv2.Rational(int(pointLong[3]*100000), 100000)]
metadata['Exif.GPSInfo.GPSLatitude'] = latitude
metadata.write()
metadata['Exif.GPSInfo.GPSLongitude'] = longitude
metadata.write()
metadata['Exif.GPSInfo.GPSLatitudeRef'] = pointLat[0]
metadata.write()
metadata['Exif.GPSInfo.GPSLongitudeRef'] = pointLong[0]
metadata.write()
except:
print ("something bad happened with this file...")
print ("Done")
except:
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
pymsg = "PYTHON ERRORS:nTraceback info:n" + tbinfo + "nError Info:n" + str(sys.exc_info()[1])
print (pymsg + "n")
Answered by GBG on November 6, 2021
I recomend JOSM with plugins 'photo_geotagging' and 'photoadjust'.
Answered by trolleway on November 6, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP