-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkue_find.py
554 lines (469 loc) · 20.3 KB
/
kue_find.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
# Copyright Bunting Labs, Inc. 2024
import os
from enum import IntEnum
from time import time
import hashlib
from functools import lru_cache
import csv
from osgeo import ogr, osr, gdal
import numpy as np
from qgis.core import (
QgsCoordinateReferenceSystem,
QgsTask,
QgsApplication,
Qgis,
QgsMessageLog,
QgsProject,
QgsCoordinateTransform,
)
from PyQt5.QtCore import pyqtSignal
from .kue_messages import KUE_FIND_FILTER_EXPLANATION
# I'm not sure sqlite3 is always available.
KUE_SQLITE_PATH = os.path.join(os.path.dirname(__file__), "find_file_index.sqlite")
try:
import sqlite3
USE_SQLITE = True
except ImportError:
USE_SQLITE = False
class FindType(IntEnum):
FIND_RASTER = 1
FIND_VECTOR_POINT = 2
FIND_VECTOR_LINE = 3
FIND_VECTOR_POLYGON = 4
VECTOR_EXTENSIONS = (".shp", ".gpkg", ".fgb")
RASTER_EXTENSIONS = (".tif",)
@lru_cache(maxsize=100)
def transformation_from_srs_to_4326(source_srs: osr.SpatialReference):
target_srs = osr.SpatialReference()
target_srs.ImportFromEPSG(4326)
target_srs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)
return osr.CoordinateTransformation(source_srs, target_srs)
# Returns 8 bytes of entropy (2^64 possibilities)
def hash_file_path(file_path: str) -> bytes:
return hashlib.sha1(file_path.encode()).digest()[:8]
def levenshtein_distance(s1: str, s2: str) -> int:
if len(s1) < len(s2):
return levenshtein_distance(s2, s1)
if not s2:
return len(s1)
previous_row = range(len(s2) + 1)
for i, c1 in enumerate(s1):
current_row = [i + 1]
for j, c2 in enumerate(s2):
insertions = previous_row[j + 1] + 1
deletions = current_row[j] + 1
substitutions = previous_row[j] + (c1 != c2)
current_row.append(min(insertions, deletions, substitutions))
previous_row = current_row
return previous_row[-1]
def humanize_atime(atime: int) -> str:
delta = int(time()) - atime
minutes = delta // 60
hours = minutes // 60
days = hours // 24
months = days // 30
years = days // 365
if years > 0:
return f"{years} years ago"
if months > 0:
return f"{months} months ago"
if days > 0:
return f"{days} days ago"
if hours > 0:
return f"{hours} hours ago"
return f"{minutes} minutes ago"
class IndexingTask(QgsTask):
def __init__(self, dir_path, description="Indexing files for Kue /find"):
super().__init__(description, QgsTask.CanCancel)
self.dir_path = dir_path
self.files = []
self.exception = None
self.processed_files = 0
def run(self):
if USE_SQLITE:
conn = sqlite3.connect(KUE_SQLITE_PATH)
c = conn.cursor()
c.execute("""CREATE TABLE IF NOT EXISTS files
(file_path_hash BLOB PRIMARY KEY,
bbox_minx REAL,
bbox_miny REAL,
bbox_maxx REAL,
bbox_maxy REAL,
cache_time INTEGER,
geometry_type INTEGER
)""")
conn.commit()
try:
files_to_index = []
# Build index once
target_extensions = VECTOR_EXTENSIONS + RASTER_EXTENSIONS
for root, dirs, files in os.walk(self.dir_path):
if self.isCanceled():
if USE_SQLITE:
conn.close()
return False
# Skip hidden directories by modifying dirs in-place
dirs[:] = [d for d in dirs if not d.startswith(".")]
for file in files:
if file.endswith(target_extensions) and not file.startswith("."):
files_to_index.append(os.path.join(root, file))
target_srs = osr.SpatialReference()
target_srs.ImportFromEPSG(4326)
for full_path in files_to_index:
if self.isCanceled():
if USE_SQLITE:
conn.close()
return False
self.processed_files += 1
self.setProgress(int(100 * self.processed_files / len(files_to_index)))
filename = os.path.basename(full_path)
stats = os.stat(full_path)
if USE_SQLITE:
needle_file_hash = hash_file_path(full_path)
c.execute(
"SELECT bbox_minx, bbox_miny, bbox_maxx, bbox_maxy, geometry_type FROM files WHERE file_path_hash = ?",
(needle_file_hash,),
)
row = c.fetchone()
if row:
bbox = (row[0], row[1], row[2], row[3])
find_type = FindType(row[4]) if row[4] is not None else None
file_type = (
"raster"
if find_type == FindType.FIND_RASTER
else "vector"
if find_type
else None
)
geom_type = {
FindType.FIND_VECTOR_POINT: "point",
FindType.FIND_VECTOR_LINE: "line",
FindType.FIND_VECTOR_POLYGON: "polygon",
}.get(find_type)
self.files.append(
{
"path": full_path,
"last_accessed": int(stats.st_atime),
"last_modified": int(stats.st_mtime),
"type": file_type,
"geometry_type": geom_type,
"bbox": bbox,
}
)
continue
if filename.endswith(VECTOR_EXTENSIONS):
# Get extent and transform if needed
try:
ds = ogr.Open(full_path)
except Exception:
QgsMessageLog.logMessage(
f"Failed to open {os.path.basename(full_path)}",
"KueFind",
level=Qgis.Warning,
)
continue
if ds is None:
continue
layer = ds.GetLayer(0)
geom_type = ogr.GeometryTypeToName(layer.GetGeomType())
file_type = "vector"
# Get extent and transform if needed
try:
# Get source SRS - Fix: Use GetSpatialRef() instead of GetProjection()
source_crs = QgsCoordinateReferenceSystem(
layer.GetSpatialRef().ExportToWkt()
)
bbox = layer.GetExtent() # Returns (minx,maxx,miny,maxy)
if source_crs.isValid():
source_srs = osr.SpatialReference()
source_srs.ImportFromWkt(source_crs.toWkt())
source_srs.SetAxisMappingStrategy(
osr.OAMS_TRADITIONAL_GIS_ORDER
)
transform = transformation_from_srs_to_4326(source_srs)
if not isinstance(transform, osr.CoordinateTransformation):
QgsMessageLog.logMessage(
f"No coordinate transform for {os.path.basename(full_path)}",
"KueFind",
level=Qgis.Warning,
)
bbox = None
continue
# Convert bbox corners preserving lon/lat order
point_sw = ogr.CreateGeometryFromWkt(
f"POINT ({bbox[0]} {bbox[2]})"
) # minx,miny
point_ne = ogr.CreateGeometryFromWkt(
f"POINT ({bbox[1]} {bbox[3]})"
) # maxx,maxy
try:
point_sw.Transform(transform)
point_ne.Transform(transform)
# Order as minx,miny,maxx,maxy (min_lon,min_lat,max_lon,max_lat)
bbox = (
point_sw.GetX(),
point_sw.GetY(),
point_ne.GetX(),
point_ne.GetY(),
)
except Exception:
QgsMessageLog.logMessage(
f"Coordinate transform failed for {os.path.basename(full_path)}",
"KueFind",
level=Qgis.Warning,
)
bbox = None
else:
QgsMessageLog.logMessage(
f"No CRS for {os.path.basename(full_path)}",
"KueFind",
level=Qgis.Warning,
)
except Exception:
QgsMessageLog.logMessage(
f"Failed to get extent for {os.path.basename(full_path)}",
"KueFind",
level=Qgis.Warning,
)
bbox = None
ds = None
elif filename.endswith(RASTER_EXTENSIONS):
file_type = "raster"
geom_type = None
bbox = None
try:
ds = gdal.Open(full_path)
except Exception:
QgsMessageLog.logMessage(
f"Failed to open {os.path.basename(full_path)}",
"KueFind",
level=Qgis.Warning,
)
continue
if ds is None:
continue
try:
geotransform = ds.GetGeoTransform()
if geotransform:
width = ds.RasterXSize
height = ds.RasterYSize
# Calculate bounding box coordinates
minx = geotransform[0]
maxy = geotransform[3]
maxx = minx + width * geotransform[1]
miny = maxy + height * geotransform[5]
bbox = (
minx,
miny,
maxx,
maxy,
) # Already in min_lon,min_lat,max_lon,max_lat order
# Transform to EPSG:4326 if needed
source_crs = QgsCoordinateReferenceSystem(
ds.GetProjection()
)
if source_crs.isValid():
source_srs = osr.SpatialReference()
source_srs.ImportFromWkt(source_crs.toWkt())
source_srs.SetAxisMappingStrategy(
osr.OAMS_TRADITIONAL_GIS_ORDER
)
transform = transformation_from_srs_to_4326(source_srs)
point_sw = ogr.CreateGeometryFromWkt(
f"POINT ({bbox[0]} {bbox[1]})"
) # min_lon,min_lat
point_ne = ogr.CreateGeometryFromWkt(
f"POINT ({bbox[2]} {bbox[3]})"
) # max_lon,max_lat
point_sw.Transform(transform)
point_ne.Transform(transform)
bbox = (
point_sw.GetX(),
point_sw.GetY(),
point_ne.GetX(),
point_ne.GetY(),
)
except Exception:
QgsMessageLog.logMessage(
f"Coordinate transform failed for {os.path.basename(full_path)}",
"KueFind",
level=Qgis.Warning,
)
bbox = None
ds = None
if USE_SQLITE and bbox is not None:
c.execute(
"INSERT OR REPLACE INTO files (file_path_hash, bbox_minx, bbox_miny, bbox_maxx, bbox_maxy, geometry_type) VALUES (?, ?, ?, ?, ?, ?)",
(
needle_file_hash,
bbox[0] if bbox else None,
bbox[1] if bbox else None,
bbox[2] if bbox else None,
bbox[3] if bbox else None,
FindType.FIND_RASTER.value
if file_type == "raster"
else FindType.FIND_VECTOR_POINT.value
if geom_type == "Point"
else FindType.FIND_VECTOR_LINE.value
if geom_type == "Line String"
else FindType.FIND_VECTOR_POLYGON.value
if geom_type == "Polygon"
else None,
),
)
self.files.append(
{
"path": full_path,
"last_accessed": int(stats.st_atime),
"last_modified": int(stats.st_mtime),
"type": file_type,
"geometry_type": geom_type,
"bbox": bbox,
}
)
if USE_SQLITE:
conn.commit()
conn.close()
return True
except Exception as e:
self.exception = e
QgsMessageLog.logMessage(
f"Got caught exception: {e}", "KueFind", level=Qgis.Warning
)
# Uncaught exception, but still close out so it doesn't hang.
if USE_SQLITE:
conn.commit()
conn.close()
return False
def finished(self, result):
if result and not self.isCanceled():
self.setProgress(100)
from PyQt5.QtCore import QObject
class KueFind(QObject):
filesIndexed = pyqtSignal(int)
def __init__(self, iface):
super().__init__()
self.iface = iface
self.files = []
self.bbox_finder = BBoxFinder(
os.path.join(os.path.dirname(__file__), "regions_and_countries.csv")
)
self.index_task = None
self.index_task_trash = [] # Keep reference to prevent garbage collection
def index(self, dir_path: str):
# Create and start the indexing task
self.index_task = IndexingTask(dir_path)
def task_completed(exception=None, result=None):
if exception is None:
self.files = self.index_task.files
self.filesIndexed.emit(len(self.files))
self.index_task = None
self.index_task.taskCompleted.connect(task_completed)
self.index_task.taskTerminated.connect(task_completed)
QgsApplication.taskManager().addTask(self.index_task)
self.index_task_trash.append(self.index_task) # Prevent garbage collection
def search(self, query: str, filter_for_map_canvas: bool = False, n: int = 12):
# Start indexing if not already started
if not self.files and self.index_task is None:
self.index(os.path.expanduser("~"))
return []
query_words = query.lower().split()
# Filter files that contain all query words
matching_files = [
f
for f in self.files
if all(word in f["path"].lower() for word in query_words)
]
# Filter for map canvas if requested
if filter_for_map_canvas and self.iface.mapCanvas():
canvas_extent = self.iface.mapCanvas().extent()
# Transform extent to EPSG:4326 if needed
project_crs = self.iface.mapCanvas().mapSettings().destinationCrs()
if project_crs != QgsCoordinateReferenceSystem("EPSG:4326"):
try:
transform = QgsCoordinateTransform(
project_crs,
QgsCoordinateReferenceSystem("EPSG:4326"),
QgsProject.instance(),
)
canvas_extent = transform.transformBoundingBox(canvas_extent)
except Exception as e:
QgsMessageLog.logMessage(
f"Failed to transform canvas extent to EPSG:4326: {e}",
"KueFind",
level=Qgis.Warning,
)
canvas_bbox = (
canvas_extent.xMinimum(),
canvas_extent.yMinimum(),
canvas_extent.xMaximum(),
canvas_extent.yMaximum(),
)
matching_files = [
f
for f in matching_files
if f["bbox"] is None or self._bboxes_intersect(f["bbox"], canvas_bbox)
]
# Sort by last accessed time, most recent first
results = sorted(
matching_files, key=lambda f: f["last_accessed"], reverse=True
)[:n]
return [
(
f["path"],
humanize_atime(f["last_accessed"]),
f["type"],
f["geometry_type"],
self.bbox_finder.find_containing_bbox(f["bbox"]) if f["bbox"] else "",
)
for f in results
]
def _bboxes_intersect(self, bbox1, bbox2):
"""Check if two bounding boxes intersect"""
return not (
bbox1[2] < bbox2[0] # bbox1 is left of bbox2
or bbox1[0] > bbox2[2] # bbox1 is right of bbox2
or bbox1[3] < bbox2[1] # bbox1 is below bbox2
or bbox1[1] > bbox2[3] # bbox1 is above bbox2
)
class BBoxFinder:
def __init__(self, bbox_file):
self.names = []
with open(bbox_file) as f:
num_lines = sum(1 for line in f) - 1 # Subtract 1 for header
self.bboxes = np.empty((num_lines + 1, 4), dtype=np.float32)
# Special boxes
self.names.append("Null Island")
self.bboxes[0] = [-3.0, -3.0, 3.0, 3.0]
with open(bbox_file) as f:
reader = csv.reader(f)
next(reader) # Skip header
for i, row in enumerate(reader):
name, minx, miny, maxx, maxy = row
self.names.append(name)
self.bboxes[i + 1] = [
float(minx),
float(miny),
float(maxx),
float(maxy),
]
# Pre-compute areas
self.areas = (self.bboxes[:, 2] - self.bboxes[:, 0]) * (
self.bboxes[:, 3] - self.bboxes[:, 1]
)
def find_containing_bbox(self, query_bbox):
# Unpack query bbox
qminx, qminy, qmaxx, qmaxy = query_bbox
# Find all bboxes that contain the query
contains = (
(self.bboxes[:, 0] <= qminx)
& (self.bboxes[:, 1] <= qminy)
& (self.bboxes[:, 2] >= qmaxx)
& (self.bboxes[:, 3] >= qmaxy)
)
if not np.any(contains):
return "World"
# Get the smallest containing bbox
containing_idx = contains.nonzero()[0]
min_area_idx = containing_idx[np.argmin(self.areas[containing_idx])]
return self.names[min_area_idx]