-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpi_action_export_cluster.py
95 lines (75 loc) · 3.38 KB
/
pi_action_export_cluster.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
'''
Like pi_action_export BUT uses a list of selected images to determine
groupings of pictures based on location.
Export folder names are based one the parent directory of the selected
image appended with "c_{n}" where n = the cluster number. Those images
without a location are copied to "c_0". If all images contained a location
the "c_0" folder is not created.
Listens for event names generated when this action was created.
Like all actions, this class has a 'handle_event(self,event,values)' method.
When a new instance of this class is created:
text - menu item text to display
rowget - method to call to get list of rows to export
selectget - method to call to get list of node locations
To use, simply create a new instance with the name of the event
and the key of the list within event values. For example:
# set up action for Export Event and Gallery Element
PiActionExport(rowget=self.get_row,selectget=self.get_selected_rows).item(),
User may want to generate a map before calling this function. The map will
then show which clusters will be exported to.
TODO: create a "map clusters" function which would look for "c_{n}" folders
in the "_export" subdirectory generated by this action. It would then
generate a map with one point for each cluster and a link pointing to
the exported cluster directory. Use that function in the tree list for
selected folders?
'''
import os
from pathlib import Path
import shutil
import pi_config as c
from pi_action import PiAction
from pi_action_export import PiActionExport
class PiActionExport(PiActionExport):
'''
Export selected files and folders.
Parms:
text - menu item text to display
rowget - method to call to get list of rows to export
clusters - method to call to get rows which define
the cluster centroids
'''
@staticmethod
def get_cluster(clusters,test_row):
''' Given a list of cluster represented by rows in parm clusters,
calculate the index of the cluster row which is closest to test_row.
Return -1 if no GPS co-ordinates in row or none in any clusters '''
c_node = -1
min_d = -1
c_idx = -1
for c in clusters:
c_idx += 1
d = c.get_hyp_sq(test_row)
if d != -1:
if d < min_d or min_d == -1:
c_node = c_idx
min_d = d
return c_node
def __init__(self,text="Cluster",rowget=None,clusters=None):
self._clusters = clusters
super().__init__(text,rowget)
def handle_event(self,event,values):
''' Handle Cluster Event - copy each image into a cluster
subdirectory based on which cluster it is closest to.
Cluster 0 is for images without gps location. '''
# callback row getter
rows = self._rowget(values)
cnt = 0
# callback cluster getter
clusters = self._clusters(values)
for row in rows:
cnt += 1
subdir = row['file_location']
fn = row['file_name']
dst_dir = f'{c.directory}/_export{subdir}'
self.copy_file(subdir,dst_dir,fn,f'export {cnt} {subdir}/{fn}')
c.update_status(f'exported {cnt} images to {c.directory}/_export')