Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read web #118

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 50 additions & 9 deletions ImportPhotos.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
os.path.dirname(__file__), 'ui/impphotos.ui'))

FIELDS = ['fid', 'ID', 'Name', 'Date', 'Time', 'Lon', 'Lat', 'Altitude', 'North', 'Azimuth', 'Cam. Maker',
'Cam. Model', 'Title', 'Comment', 'Path', 'RelPath', 'Timestamp', 'Images']
'Cam. Model', 'Title', 'Comment', 'Path', 'RelPath', 'Timestamp', 'Images', 'Link','Description']

SUPPORTED_PHOTOS_EXTENSIONS = ['jpg', 'jpeg', 'JPG', 'JPEG']

Expand Down Expand Up @@ -248,14 +248,21 @@ def initGui(self):
self.dlg.closebutton.clicked.connect(self.dlg.close)
self.dlg.toolButtonImport.clicked.connect(self.toolButtonImport)
self.dlg.toolButtonOut.clicked.connect(self.toolButtonOut)
self.dlg.toolButtonRelative.clicked.connect(self.toolButtonRelative)


# Add QgsRuleBasedRendererWidget
# temp_layer is a class variable because we need to keep its reference
# so the RendererWidget does not crash QGIS
# If it's not a class variable, then it goes out of scope after this method
# and as mentioned, QGIS crashes because it tries to access it.
self.temp_layer = QgsVectorLayer(
'Point?crs=epsg:4326&field=ID:string&field=Name:string&field=Date:date&field=Time:text&field=Lon:double&field=Lat:double&field=Altitude:double&field=Cam.Mak:string&field=Cam.Mod:string&field=Title:string&field=Comment:string&field=Path:string&field=RelPath:string&field=Timestamp:string&field=Images:string',
'Point?crs=epsg:4326&field=ID:string&field=Name:string&'\
'field=Date:date&field=Time:text&field=Lon:double&field=Lat:double'\
'&field=Altitude:double&field=Cam.Mak:string&field=Cam.Mod:string'\
'&field=Title:string&field=Comment:string&field=Path:string'\'
'&field=RelPath:string&field=Timestamp:string&field=Images:string'\
'&field=Link:string&field=Description:string',
'temp_layer',
'memory')
self.temp_layer.setRenderer(QgsFeatureRenderer.defaultRenderer(QgsWkbTypes.PointGeometry))
Expand Down Expand Up @@ -329,6 +336,16 @@ def get_path_relative_to_project_root(self, abs_path):
rel_path = os.path.normpath(abs_path)
return rel_path

def toolButtonRelative(self):
directory_path = QFileDialog.getExistingDirectory(
self.dlg, self.tr('Select a folder:'),
os.path.expanduser('~'), QFileDialog.ShowDirsOnly)

if directory_path:
self.selected_folder = directory_path[:]
self.dlg.relativeroot.setText(directory_path)


def toolButtonImport(self):
directory_path = QFileDialog.getExistingDirectory(
self.dlg, self.tr('Select a folder:'),
Expand All @@ -342,17 +359,24 @@ def import_photos(self):
self.layer_renderer = self.dlg.findChild(QgsRuleBasedRendererWidget, "renderer_widget").renderer()

file_not_found = False
if self.dlg.imp.text() == '' and not os.path.isdir(self.dlg.imp.text()):
if self.dlg.imp.text() == '' and not os.path.isdir(self.dlg.imp.text()): #should have been or?
file_not_found = True
msg = self.tr('Please select a directory photos.')
if self.dlg.out.text() == '' and not os.path.isabs(self.dlg.out.text()):
if self.dlg.out.text() == '' and not os.path.isabs(self.dlg.out.text()): #should have been or?
file_not_found = True
msg = self.tr('Please define output file location.')

if file_not_found:
self.showMessage('Warning', msg, 'Warning')
return

if self.dlg.relativeroot.text() == '':
self.relativeroot = self.dlg.imp.text()
else:
self.relativeroot = self.dlg.relativeroot.text()

self.webroot = self.dlg.webroot.text() # Will be checked later if it is ''

# get paths of photos
self.photos_to_import = []
for root, dirs, files in os.walk(self.dlg.imp.text()):
Expand All @@ -363,7 +387,11 @@ def import_photos(self):
if len(self.photos_to_import) == 0:
self.showMessage('Warning', self.tr('No photos were found!'), 'Warning')
return


# Set up for url:



self.dlg.close()
self.call_import_photos()
# QGuiApplication.setOverrideCursor(Qt.WaitCursor)
Expand Down Expand Up @@ -625,7 +653,17 @@ def update_photos(self):
def get_geo_infos_from_photo(self, photo_path):
try:
rel_path = self.get_path_relative_to_project_root(photo_path)
ImagesSrc = '<img src = "' + rel_path + '" width="300" height="225"/>'
url = '' # use photo_path, remove relative root, add web root
description = ''
if self.webroot != '':
webrelpath = os.path.relpath(photo_path,self.relativeroot)
url = self.webroot+webrelpath
ImagesSrc = '<img src = "' + url + '" width="300" height="225"/>'
else:
url = photo_path
ImagesSrc = '<img src = "' + rel_path + '" width="300" height="225"/>'
# This will make a clickable link in kml
description = f'<a href="{url}">{os.path.basename(photo_path)}</a>'
if CHECK_MODULE == 'exifread':
with open(photo_path, 'rb') as imgpathF:
tags = exifread.process_file(imgpathF, details=False)
Expand Down Expand Up @@ -793,7 +831,8 @@ def get_geo_infos_from_photo(self, photo_path):
'Cam. Maker': str(maker), 'Cam. Model': str(model),
'Title': str(title), 'Comment': user_comm,
'Path': photo_path, 'RelPath': rel_path,
'Timestamp': timestamp, 'Images': ImagesSrc
'Timestamp': timestamp, 'Images': ImagesSrc, 'Link':url,
'Description': description
},
"geometry": {
"coordinates": [lon, lat],
Expand All @@ -813,7 +852,8 @@ def get_geo_infos_from_photo(self, photo_path):
'Cam. Maker': str(maker), 'Cam. Model': str(model),
'Title': str(title), 'Comment': user_comm,
'Path': photo_path, 'RelPath': rel_path,
'Timestamp': timestamp, 'Images': ImagesSrc
'Timestamp': timestamp, 'Images': ImagesSrc, 'Link': url,
'Description': description
},
"geometry": {
"coordinates": [lon, lat],
Expand All @@ -822,10 +862,11 @@ def get_geo_infos_from_photo(self, photo_path):
}
except:
pass

# geo_info should exist from default
return geo_info

except Exception as e:
# print(e) # Can be uncommented for debugging
return ''

def showMessage(self, title, msg, icon):
Expand Down
21 changes: 17 additions & 4 deletions code/PhotosViewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ def __init__(self, drawSelf):
self.allpicturesImpath = {} # feature id / picture path
self.allpicturesAzimuth = {}
self.allpicturesName = {}
self.allpicturesLink = {}
for i, f in enumerate(self.drawSelf.layerActive.getFeatures()):
attributes = f.attributes()
if 'PATH' in self.drawSelf.fields:
Expand Down Expand Up @@ -223,14 +224,22 @@ def __init__(self, drawSelf):
azimuth = attributes[f.fieldNameIndex('Azimuth')]
except:
azimuth = None

try:
link = attributes[f.fieldNameIndex('Link')]
except:
link = None

self.allpictures[f.id()] = name_
self.allpicturesdates[f.id()] = dateTrue
self.allpicturestimes[f.id()] = timeTrue
self.allpicturesImpath[f.id()] = imPath
self.allpicturesAzimuth[f.id()] = azimuth
self.allpicturesName[f.id()] = name_

self.allpicturesLink[f.id()] = link



self.viewer = PhotosViewer(self)

######################################################################################
Expand Down Expand Up @@ -309,7 +318,8 @@ def __init__(self, drawSelf):
self.add_window_place = QLabel(self) # temporary
self.add_window_place.setSizePolicy(QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum))
self.add_window_place.setFrameShape(QFrame.NoFrame)

self.add_window_place.setOpenExternalLinks(True) # To make link clickable

self.infoPhoto1 = QLabel(self)
self.infoPhoto1.setSizePolicy(sizePolicy)
self.infoPhoto1.setFrameShape(QFrame.Box)
Expand Down Expand Up @@ -683,8 +693,11 @@ def updateWindow(self):
self.infoPhoto2.setText(
self.tr('Time: ') + self.allpicturestimes[self.drawSelf.featureIndex][0:8])
self.infoPhoto3.setText(self.tr('Layer: ') + self.drawSelf.layerActiveName)
self.add_window_place.setText(self.allpicturesName[self.drawSelf.featureIndex])

link = self.allpicturesLink[self.drawSelf.featureIndex]
header = self.allpicturesName[self.drawSelf.featureIndex]
if link is not None:
header =f'<a href="{link}">{header}</a>'
self.add_window_place.setText(header)
azimuth = self.allpicturesAzimuth[self.drawSelf.featureIndex]
if type(azimuth) is str:
try:
Expand Down
Loading