Skip to content

Commit

Permalink
Merge pull request #112 from holesond/fix-viewer-relpath
Browse files Browse the repository at this point in the history
Fixed relative path handling in PhotoWindow and ImportPhotos.
  • Loading branch information
Mariosmsk authored Apr 19, 2024
2 parents cdca500 + 2e2a3e4 commit 41e5247
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
17 changes: 14 additions & 3 deletions ImportPhotos.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from qgis.PyQt import uic
from qgis.PyQt.QtWidgets import QDialog
from qgis.PyQt.QtCore import QSettings, QTranslator, QCoreApplication, Qt, QTextCodec
from qgis.PyQt.QtCore import QFileInfo
from qgis.core import *
from qgis.gui import QgsRuleBasedRendererWidget

Expand Down Expand Up @@ -316,6 +317,17 @@ def toolButtonOut(self):
else:
# Set extension with the specified filter
self.dlg.out.setText(os.path.splitext(outputPath)[0] + extension)

def get_path_relative_to_project_root(self, abs_path):
project_folder = QFileInfo(
self.project_instance.fileName()).absolutePath()
try:
rel_path = os.path.relpath(
path=os.path.normpath(abs_path), start=project_folder)
except ValueError:
# On Windows, when path and start are on different drives.
rel_path = os.path.normpath(abs_path)
return rel_path

def toolButtonImport(self):
directory_path = QFileDialog.getExistingDirectory(
Expand All @@ -324,7 +336,6 @@ def toolButtonImport(self):

if directory_path:
self.selected_folder = directory_path[:]
self.selected_folder = './' + os.path.basename(os.path.normpath(self.selected_folder)) + '/'
self.dlg.imp.setText(directory_path)

def import_photos(self):
Expand Down Expand Up @@ -537,7 +548,7 @@ def update_photos(self):
picture_paths.append(os.path.basename(feature.attribute("Path")))

# Pictures that should be removed from the layer
self.selected_folder = './' + os.path.basename(os.path.normpath(base_picture_directory)) + '/'
self.selected_folder = base_picture_directory
list_pictures = []
try:
for root, dirs, files in os.walk(base_picture_directory):
Expand Down Expand Up @@ -613,7 +624,7 @@ def update_photos(self):

def get_geo_infos_from_photo(self, photo_path):
try:
rel_path = os.path.relpath(photo_path,start=os.path.dirname(self.dlg.out.text()))
rel_path = self.get_path_relative_to_project_root(photo_path)
ImagesSrc = '<img src = "' + rel_path + '" width="300" height="225"/>'
if CHECK_MODULE == 'exifread':
with open(photo_path, 'rb') as imgpathF:
Expand Down
6 changes: 3 additions & 3 deletions code/MouseClick.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ def canvasReleaseEvent(self, event):
else:
return

self.drawSelf.prj = QgsProject.instance()
try:
if not os.path.exists(imPath):
self.prj = QgsProject.instance()
if self.prj.fileName() and 'RELPATH' in fields:
imPath = os.path.join(QFileInfo(self.prj.fileName()).absolutePath(),
if self.drawSelf.prj.fileName() and 'RELPATH' in fields:
imPath = os.path.join(QFileInfo(self.drawSelf.prj.fileName()).absolutePath(),
feature.attributes()[feature.fieldNameIndex('RelPath')])
else:
c = self.drawSelf.noImageFound()
Expand Down
5 changes: 3 additions & 2 deletions code/PhotosViewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,9 @@ def __init__(self, drawSelf):
if not os.path.exists(imPath):
try:
if self.drawSelf.prj.fileName() and 'RELPATH' in self.drawSelf.fields:
imPath = QFileInfo(self.drawSelf.prj.fileName()).absolutePath() + \
attributes[f.fieldNameIndex('RelPath')]
imPath = os.path.join(
QFileInfo(self.drawSelf.prj.fileName()).absolutePath(),
attributes[f.fieldNameIndex('RelPath')])
except:
imPath = ''
try:
Expand Down

0 comments on commit 41e5247

Please sign in to comment.