|
1 | 1 | # Standard library imports |
2 | 2 | import os |
| 3 | +import sqlite3 |
3 | 4 |
|
4 | 5 | # Third party imports |
5 | 6 | import vtk |
6 | 7 | from vtk.web import protocols as vtk_protocols |
7 | 8 |
|
8 | 9 | # Local application imports |
9 | | -from opengeodeweb_microservice.database.data import Data |
10 | | -from opengeodeweb_microservice.database.connection import get_session |
11 | 10 |
|
12 | 11 |
|
13 | 12 | class VtkView(vtk_protocols.vtkWebProtocol): |
14 | 13 | def __init__(self): |
15 | 14 | super().__init__() |
16 | 15 | self.DATA_FOLDER_PATH = os.getenv("DATA_FOLDER_PATH") |
| 16 | + # self.DATABASE_PATH = os.getenv("DATABASE_PATH") |
17 | 17 | self.DataReader = vtk.vtkXMLPolyDataReader() |
18 | 18 | self.ImageReader = vtk.vtkXMLImageDataReader() |
19 | 19 |
|
20 | | - def get_data_info(self, id: str) -> Data: |
21 | | - data_entry = Data.get(id) |
22 | | - if not data_entry: |
23 | | - raise ValueError(f"Data with id {id} not found") |
24 | | - return data_entry |
25 | | - |
26 | | - def get_data_file_path(self, id: str, filename: str = "") -> str: |
27 | | - data_entry = self.get_data_info(id) |
28 | | - if filename: |
29 | | - return os.path.join(self.DATA_FOLDER_PATH, id, filename) |
30 | | - return os.path.join(self.DATA_FOLDER_PATH, id, data_entry.native_file_name) |
31 | | - |
32 | | - def load_data(self, id: str): |
33 | | - data_entry = self.get_data_info(id) |
34 | | - file_path = self.get_data_file_path(id, data_entry.native_file_name) |
35 | | - |
36 | | - # if not os.path.exists(file_path): |
37 | | - # raise FileNotFoundError(f"File not found at {file_path}") |
38 | | - |
39 | | - if file_path.endswith(".vtp"): |
40 | | - reader = vtk.vtkXMLPolyDataReader() |
41 | | - elif file_path.endswith(".vti"): |
42 | | - reader = vtk.vtkXMLImageDataReader() |
43 | | - elif file_path.endswith(".vtu"): |
44 | | - reader = vtk.vtkXMLUnstructuredGridReader() |
45 | | - else: |
46 | | - raise ValueError(f"Unsupported file extension for {file_path}") |
47 | | - |
48 | | - reader.SetFileName(file_path) |
49 | | - return reader, data_entry.geode_object |
50 | | - |
51 | 20 | def get_data_base(self): |
52 | 21 | return self.getSharedObject("db") |
53 | 22 |
|
| 23 | + def get_db_connection(self): |
| 24 | + conn = sqlite3.connect(self.DATA_FOLDER_PATH) |
| 25 | + conn.row_factory = sqlite3.Row |
| 26 | + return conn |
| 27 | + |
| 28 | + def get_data_by_id(self, data_id): |
| 29 | + conn = self.get_db_connection() |
| 30 | + try: |
| 31 | + cursor = conn.cursor() |
| 32 | + cursor.execute("SELECT * FROM data WHERE id = ?", (data_id,)) |
| 33 | + data = cursor.fetchone() |
| 34 | + return dict(data) if data else None |
| 35 | + finally: |
| 36 | + conn.close() |
| 37 | + |
| 38 | + def get_all_data(self): |
| 39 | + conn = self.get_db_connection() |
| 40 | + try: |
| 41 | + cursor = conn.cursor() |
| 42 | + cursor.execute("SELECT * FROM data") |
| 43 | + data = cursor.fetchall() |
| 44 | + return [dict(row) for row in data] |
| 45 | + finally: |
| 46 | + conn.close() |
| 47 | + |
54 | 48 | def get_renderer(self): |
55 | 49 | return self.getSharedObject("renderer") |
56 | 50 |
|
|
0 commit comments