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

Add bulk image export button #117

Open
wants to merge 4 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
44 changes: 40 additions & 4 deletions ImportPhotos.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import os
import uuid
import json
from pathlib import Path

# Import python module
CHECK_MODULE = ''
Expand Down Expand Up @@ -242,6 +243,12 @@ def initGui(self):
text=self.tr('Update Photos'),
callback=self.update_photos,
parent=self.iface.mainWindow())
icon_path = ':/plugins/ImportPhotos/icons/export.svg'
self.add_action(
icon_path,
text=self.tr('Bulk Export'),
callback=self.bulk_export,
parent=self.iface.mainWindow())

self.dlg = ImportPhotosDialog()
self.dlg.ok.clicked.connect(self.import_photos)
Expand Down Expand Up @@ -331,7 +338,7 @@ def get_path_relative_to_project_root(self, abs_path):

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

if directory_path:
Expand Down Expand Up @@ -513,7 +520,7 @@ def completed(self, result):
"""
self.layerPhotos_final.setMapTipTemplate(expression)

def update_photos(self):
def layer_selector(self, title: str = 'Select layer to update'):
layers = {}

for layer in self.project_instance.mapLayers().values():
Expand All @@ -524,15 +531,21 @@ def update_photos(self):
if layers.keys():
selected_layer_name, ok = QInputDialog.getItem(
self.iface.mainWindow(),
self.tr("Select layer to update"),
self.tr(title),
"Layer List:", layers.keys(), 0, False)
else:
self.showMessage('Error', self.tr('No photos layer(s) found'), 'Warning')
return

if not ok:
return
self.selected_layer = layers[selected_layer_name]

return layers[selected_layer_name]

def update_photos(self):
self.selected_layer = self.layer_selector()
if not self.selected_layer:
return

# All picture paths that are currently saved in the shapefile layer
picture_paths = []
Expand Down Expand Up @@ -622,6 +635,29 @@ def update_photos(self):

self.showMessage(title, msg, 'Information')

def bulk_export(self):
self.selected_layer = self.layer_selector('Select layer to export')
if not self.selected_layer:
return

path_column_index = self.selected_layer.fields().indexFromName('Path')

directory_path = QFileDialog.getExistingDirectory(
self.dlg, self.tr('Select an output folder'),
os.path.expanduser('~'), QFileDialog.ShowDirsOnly)
if not directory_path:
return

images = self.selected_layer.getFeatures()
for image in images:
src_path = Path(image.attributes()[path_column_index])
dest_dir = Path(directory_path)
dest_filename = dest_dir / Path(f"{src_path.parent.name}_{src_path.name}")
dest_filename.write_bytes(src_path.read_bytes())

self.showMessage('Success', self.tr('Export complete.'), self.tr('Information'))
return

def get_geo_infos_from_photo(self, photo_path):
try:
rel_path = self.get_path_relative_to_project_root(photo_path)
Expand Down
1 change: 1 addition & 0 deletions icons/export.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading