Skip to content

Commit 25cf977

Browse files
committed
WIP: Reworking place matching algorithm
1 parent e8dbc5e commit 25cf977

File tree

3 files changed

+60
-23
lines changed

3 files changed

+60
-23
lines changed

tempren/cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -681,9 +681,9 @@ def main() -> int:
681681
except FileNotSupportedError as exc:
682682
log.error(f"Error: {exc}")
683683
return ErrorCode.INVALID_DESTINATION_ERROR
684-
except Exception as exc: # NOCOVER: not really testable - final fallback
685-
log.error(f"Unknown error: {exc.__class__.__name__} {exc}")
686-
return ErrorCode.UNKNOWN_ERROR
684+
# except Exception as exc: # NOCOVER: not really testable - final fallback
685+
# log.error(f"Unknown error: {exc.__class__.__name__} {exc}")
686+
# return ErrorCode.UNKNOWN_ERROR
687687
finally:
688688
os.chdir(original_cwd)
689689

tempren/tags/geo.py

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def configure(
4646
use_look_at: bool = True,
4747
use_folders: bool = True,
4848
default: str = "",
49+
to_csv: Optional[str] = None,
4950
) -> None:
5051
"""
5152
:param kml: Path to the KML file containing the places
@@ -63,6 +64,15 @@ def configure(
6364
self.places = self._load_file(kml, use_look_at, use_folders)
6465
self.default = default
6566

67+
if to_csv is not None:
68+
with open(to_csv, "w") as csv_file:
69+
csv_file.writelines(
70+
[
71+
f"{p.latitude}, {p.longitude}, {p.radius / 1000}, {p.name}\n"
72+
for p in self.places
73+
]
74+
)
75+
6676
def _load_file(
6777
self, kml_path: str, use_look_at: bool, use_folders: bool
6878
) -> List[Place]:
@@ -76,7 +86,7 @@ def _collect_places(kml, prefix: List[str]) -> List[Place]:
7686
"/".join(prefix + [kml.name]),
7787
kml.view.latitude,
7888
kml.view.longitude,
79-
radius=kml.view.range,
89+
radius=kml.view.range / 2,
8090
)
8191
prefix = prefix + [kml.name]
8292
return [folder_place] + list(
@@ -92,13 +102,13 @@ def _collect_places(kml, prefix: List[str]) -> List[Place]:
92102
"/".join(prefix + [kml.name]),
93103
kml.view.latitude,
94104
kml.view.longitude,
95-
radius=kml.view.range,
105+
radius=kml.view.range / 2,
96106
)
97107
]
98108
else:
99109
latitude = kml.kml_geometry.kml_coordinates.coords[0][0]
100110
longitude = kml.kml_geometry.kml_coordinates.coords[0][1]
101-
radius = kml.view.range if kml.view is not None else None
111+
radius = kml.view.range if kml.view / 2 is not None else None
102112
return [
103113
Place(
104114
"/".join(prefix + [kml.name]),
@@ -135,10 +145,25 @@ def process(self, file: File, context: Optional[str]) -> str:
135145
@staticmethod
136146
def _find_best_match(places: List[Place], point: Point) -> Optional[Place]:
137147
gc = great_circle()
138-
min_distance_place: Optional[Place] = None
139-
for place in places:
140-
if min_distance_place is None or gc.measure(
141-
place.position, point
142-
) < gc.measure(min_distance_place.position, point):
143-
min_distance_place = place
144-
return min_distance_place
148+
149+
places_in_range = filter(
150+
lambda p: gc.measure(p.position, point) < p.radius, places
151+
)
152+
153+
# FIXME: Fix the types
154+
smallest_place_in_range = next(
155+
sorted(places_in_range, key=lambda p: p.radius), None
156+
)
157+
return smallest_place_in_range
158+
159+
# min_distance: float = -1
160+
# min_distance_place: Optional[Place] = None
161+
#
162+
# for place in places_in_range:
163+
# distance = gc.measure(place.position, point)
164+
# if distance > place.radius:
165+
# continue
166+
# if min_distance_place is None or distance < min_distance:
167+
# min_distance = distance
168+
# min_distance_place = place
169+
# return min_distance_place

tempren/tags/image.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ def convert_tag_value(tag_type, tag_value):
157157
if tag_value[1] == 1:
158158
return tag_value[0]
159159
else:
160+
if tag_value[1] == 0:
161+
return 0
160162
return tag_value[0] / tag_value[1]
161163
if tag_type == TAG_TYPES.Ascii:
162164
return tag_value.decode("ascii")
@@ -227,16 +229,26 @@ def process(self, file: File, context: Optional[str]) -> Any:
227229
assert context is None
228230

229231
exif_dict = piexif.load(str(file.absolute_path))
230-
latitude = extract_exif_value(exif_dict, "GPSLatitude")
231-
latitude_ref = extract_exif_value(exif_dict, "GPSLatitudeRef")
232-
longitude = extract_exif_value(exif_dict, "GPSLongitude")
233-
longitude_ref = extract_exif_value(exif_dict, "GPSLongitudeRef")
232+
try:
233+
latitude = extract_exif_value(exif_dict, "GPSLatitude")
234+
latitude_ref = extract_exif_value(exif_dict, "GPSLatitudeRef")
235+
longitude = extract_exif_value(exif_dict, "GPSLongitude")
236+
longitude_ref = extract_exif_value(exif_dict, "GPSLongitudeRef")
234237

235-
degrees_notation = f"{latitude}{latitude_ref}, {longitude}{longitude_ref}"
236-
if self.use_decimal:
237-
return Point.from_string(degrees_notation).format_decimal()
238-
return degrees_notation
238+
degrees_notation = f"{latitude}{latitude_ref}, {longitude}{longitude_ref}"
239+
if self.use_decimal:
240+
return Point.from_string(degrees_notation).format_decimal()
241+
return degrees_notation
242+
except ValueError:
243+
return ""
239244

240245

241-
class HasGpsPositionTagAlias(TagAlias):
242-
"""%Core.Not(){%Text.IsEmpty(){%Image.GpsPosition()}}"""
246+
class HasGpsPositionTag(GpsPositionTag):
247+
"""Check if file contains valid GPS coordinates that can be extracted by GpsPosition tag"""
248+
249+
def process(self, file: File, context: Optional[str]) -> bool:
250+
# noinspection PyBroadException
251+
try:
252+
return super().process(file, context) != ""
253+
except:
254+
return False

0 commit comments

Comments
 (0)