Skip to content

Commit

Permalink
fix issue #15
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariosmsk committed Sep 21, 2018
1 parent c0d5b9b commit 3960d95
Showing 1 changed file with 108 additions and 105 deletions.
213 changes: 108 additions & 105 deletions ImportPhotos.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,119 +374,122 @@ def ok(self):
self.layernamePhotos.append(lphoto)

for count, imgpath in enumerate(photos):
self.dlg.progressBar.setValue(int(count * self.total))
name = os.path.basename(imgpath)
imgpath = imgpath.replace('\\', '/')
if Qgis.QGIS_VERSION >= '3.0':
with open(imgpath, 'rb') as imgpathF:
tags = exifread.process_file(imgpathF, details=False)
if not tags.keys() & {"GPS GPSLongitude", "GPS GPSLatitude"}:
continue

lat, lon = self.get_exif_location(tags, "lonlat")
try:
altitude = float(tags["GPS GPSAltitude"].values[0].num) / float(tags["GPS GPSAltitude"].values[0].den)
except:
altitude = ''
uuid_ = str(uuid.uuid4())
try:
dt1, dt2 = tags["Image DateTime"].values.split()
date = dt1.replace(':', '/')
time_ = dt2
except:
try:
date = tags["GPS GPSDate"].values.replace(':', '/')
tt = [str(i) for i in tags["GPS GPSTimeStamp"].values]
time_ = "{:0>2}:{:0>2}:{:0>2}".format(tt[0], tt[1], tt[2])
except:
date = ''
time_ = ''
try:
azimuth = float(tags["GPS GPSImgDirection"].values[0].num) / float(tags["GPS GPSImgDirection"].values[0].den)
except:
azimuth = ''
try:
north = str(tags["GPS GPSImgDirectionRef"].values)
except:
north = ''
try:
maker = tags['Image Make']
except:
maker = ''
try:
model = tags['Image Model']
except:
model = ''

else:
# something for QGIS 2
a = {}
info = Image.open(imgpath)
info = info._getexif()

if info == None:
continue

for tag, value in info.items():
if TAGS.get(tag, tag) == 'GPSInfo' or TAGS.get(tag, tag) == 'DateTime' or TAGS.get(tag,
tag) == 'DateTimeOriginal':
a[TAGS.get(tag, tag)] = value

if a == {}:
continue

if a['GPSInfo'] != {}:
if 1 and 2 and 3 and 4 in a['GPSInfo']:
lat = [float(x) / float(y) for x, y in a['GPSInfo'][2]]
latref = a['GPSInfo'][1]
lon = [float(x) / float(y) for x, y in a['GPSInfo'][4]]
lonref = a['GPSInfo'][3]

lat = lat[0] + lat[1] / 60 + lat[2] / 3600
lon = lon[0] + lon[1] / 60 + lon[2] / 3600

if latref == 'S':
lat = -lat
if lonref == 'W':
lon = -lon
else:
try:
self.dlg.progressBar.setValue(int(count * self.total))
name = os.path.basename(imgpath)
imgpath = imgpath.replace('\\', '/')
if Qgis.QGIS_VERSION >= '3.0':
with open(imgpath, 'rb') as imgpathF:
tags = exifread.process_file(imgpathF, details=False)
if not tags.keys() & {"GPS GPSLongitude", "GPS GPSLatitude"}:
continue

lat, lon = self.get_exif_location(tags, "lonlat")
try:
altitude = float(tags["GPS GPSAltitude"].values[0].num) / float(tags["GPS GPSAltitude"].values[0].den)
except:
altitude = ''
uuid_ = str(uuid.uuid4())
if 'DateTime' or 'DateTimeOriginal' in a:
if 'DateTime' in a:
dt1, dt2 = a['DateTime'].split()
elif 'DateTimeOriginal' in a:
dt1, dt2 = a['DateTimeOriginal'].split()
try:
dt1, dt2 = tags["Image DateTime"].values.split()
date = dt1.replace(':', '/')
time_ = dt2
except:
try:
date = tags["GPS GPSDate"].values.replace(':', '/')
tt = [str(i) for i in tags["GPS GPSTimeStamp"].values]
time_ = "{:0>2}:{:0>2}:{:0>2}".format(tt[0], tt[1], tt[2])
except:
date = ''
time_ = ''
try:
azimuth = float(tags["GPS GPSImgDirection"].values[0].num) / float(tags["GPS GPSImgDirection"].values[0].den)
except:
azimuth = ''
try:
north = str(tags["GPS GPSImgDirectionRef"].values)
except:
north = ''
try:
maker = tags['Image Make']
except:
maker = ''
try:
model = tags['Image Model']
except:
model = ''

if 6 in a['GPSInfo']:
if len(a['GPSInfo'][6]) > 1:
mAltitude = float(a['GPSInfo'][6][0])
mAltitudeDec = float(a['GPSInfo'][6][1])
altitude = str(mAltitude / mAltitudeDec)
else:
altitude = ''
else:
# something for QGIS 2
a = {}
info = Image.open(imgpath)
info = info._getexif()

if 16 and 17 in a['GPSInfo']:
north = str(a['GPSInfo'][16])
azimuth = str(a['GPSInfo'][17][0])
else:
north = ''
azimuth = ''
if info == None:
continue

for tag, value in info.items():
if TAGS.get(tag, tag) == 'GPSInfo' or TAGS.get(tag, tag) == 'DateTime' or TAGS.get(tag,
tag) == 'DateTimeOriginal':
a[TAGS.get(tag, tag)] = value

if a == {}:
continue

maker = ''
model = ''

self.lon.append(lon)
self.lat.append(lat)
self.truePhotosCount = self.truePhotosCount + 1
geo_info = {"properties": {'ID': uuid_, 'Name': name, 'Date': date, 'Time': time_, 'Lon': lon,
'Lat': lat, 'Altitude': altitude, 'North': north, 'Azimuth': azimuth,
'Camera Maker': str(maker), 'Camera Model': str(model), 'Path': imgpath},
"geometry": {"coordinates": [lon, lat], "type": "Point"}}
geoPhotos.append(geo_info)
if a['GPSInfo'] != {}:
if 1 and 2 and 3 and 4 in a['GPSInfo']:
lat = [float(x) / float(y) for x, y in a['GPSInfo'][2]]
latref = a['GPSInfo'][1]
lon = [float(x) / float(y) for x, y in a['GPSInfo'][4]]
lonref = a['GPSInfo'][3]

lat = lat[0] + lat[1] / 60 + lat[2] / 3600
lon = lon[0] + lon[1] / 60 + lon[2] / 3600

if latref == 'S':
lat = -lat
if lonref == 'W':
lon = -lon
else:
continue

uuid_ = str(uuid.uuid4())
if 'DateTime' or 'DateTimeOriginal' in a:
if 'DateTime' in a:
dt1, dt2 = a['DateTime'].split()
elif 'DateTimeOriginal' in a:
dt1, dt2 = a['DateTimeOriginal'].split()
date = dt1.replace(':', '/')
time_ = dt2

if 6 in a['GPSInfo']:
if len(a['GPSInfo'][6]) > 1:
mAltitude = float(a['GPSInfo'][6][0])
mAltitudeDec = float(a['GPSInfo'][6][1])
altitude = str(mAltitude / mAltitudeDec)
else:
altitude = ''

if 16 and 17 in a['GPSInfo']:
north = str(a['GPSInfo'][16])
azimuth = str(a['GPSInfo'][17][0])
else:
north = ''
azimuth = ''

maker = ''
model = ''

self.lon.append(lon)
self.lat.append(lat)
self.truePhotosCount = self.truePhotosCount + 1
geo_info = {"properties": {'ID': uuid_, 'Name': name, 'Date': date, 'Time': time_, 'Lon': lon,
'Lat': lat, 'Altitude': altitude, 'North': north, 'Azimuth': azimuth,
'Camera Maker': str(maker), 'Camera Model': str(model), 'Path': imgpath},
"geometry": {"coordinates": [lon, lat], "type": "Point"}}
geoPhotos.append(geo_info)
except:
pass

geojson = {"type": "FeatureCollection", "crs": {"type": "name", "properties": {"name": "crs:OGC:1.3:CRS84"}},
"features": geoPhotos}
Expand Down

0 comments on commit 3960d95

Please sign in to comment.