Coverage for mindsdb / integrations / handlers / openstreetmap_handler / openstreetmap_handler.py: 0%
42 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-21 00:36 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-21 00:36 +0000
1import overpy
3from mindsdb.integrations.handlers.openstreetmap_handler.openstreetmap_tables import (OpenStreetMapNodeTable,
4 OpenStreetMapWayTable, OpenStreetMapRelationTable)
5from mindsdb.integrations.libs.api_handler import APIHandler
6from mindsdb.integrations.libs.response import (
7 HandlerStatusResponse as StatusResponse,
8 HandlerResponse as Response,
9)
10from mindsdb.utilities import log
11from mindsdb_sql_parser import parse_sql
13logger = log.getLogger(__name__)
16class OpenStreetMapHandler(APIHandler):
17 """The OpenStreetMap handler implementation."""
19 def __init__(self, name: str, **kwargs):
20 """Registers all API tables and prepares the handler for an API connection.
22 Args:
23 name: (str): The handler name to use
24 """
25 super().__init__(name)
27 connection_data = kwargs.get("connection_data", {})
28 self.connection_data = connection_data
29 self.kwargs = kwargs
31 self.connection = None
32 self.is_connected = False
34 nodes_data = OpenStreetMapNodeTable(self)
35 self._register_table("nodes", nodes_data)
37 ways_data = OpenStreetMapWayTable(self)
38 self._register_table("ways", ways_data)
40 relations_data = OpenStreetMapRelationTable(self)
41 self._register_table("relations", relations_data)
43 def connect(self) -> StatusResponse:
44 """Set up the connection required by the handler.
46 Returns:
47 StatusResponse: connection object
48 """
49 if self.is_connected is True:
50 return self.connection
52 api_session = overpy.Overpass()
54 self.connection = api_session
56 self.is_connected = True
58 return self.connection
60 def check_connection(self) -> StatusResponse:
61 """Check connection to the handler.
63 Returns:
64 HandlerStatusResponse
65 """
66 response = StatusResponse(False)
68 try:
69 api_session = self.connect()
70 if api_session is not None:
71 response.success = True
72 except Exception as e:
73 logger.error('Error connecting to OpenStreetMap!')
74 response.error_message = str(e)
76 self.is_connected = response.success
78 return response
80 def native_query(self, query: str) -> Response:
81 """Execute a native query on the handler.
83 Args:
84 query (str): The query to execute.
86 Returns:
87 Response: The response from the query.
88 """
89 ast = parse_sql(query)
90 return self.query(ast)