backend/new-overpass #52
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							| @@ -25,8 +25,9 @@ class Overpass : | ||||
|         self.caching_strategy = CachingStrategy.use(caching_strategy, cache_dir=cache_dir) | ||||
|  | ||||
|  | ||||
|     @classmethod | ||||
|     def build_query(self, area: tuple, osm_types: osm_types, | ||||
|                     selector: str, conditions=[], out='center'): | ||||
|                     selector: str, conditions=[], out='center') -> str: | ||||
|         """ | ||||
|         Constructs a query string for the Overpass API to retrieve OpenStreetMap (OSM) data. | ||||
|  | ||||
| @@ -83,7 +84,7 @@ class Overpass : | ||||
|         return query | ||||
|  | ||||
|  | ||||
|     def send_query(self, query: str) -> dict: | ||||
|     def send_query(self, query: str) -> ET: | ||||
|         """ | ||||
|         Sends the Overpass QL query to the Overpass API and returns the parsed JSON response. | ||||
|  | ||||
| @@ -124,3 +125,47 @@ class Overpass : | ||||
|  | ||||
|         except urllib.error.URLError as e: | ||||
|             raise ConnectionError(f"Error connecting to Overpass API: {e}") from e | ||||
|  | ||||
|  | ||||
| def get_base_info(elem: ET.Element, osm_type: osm_types, with_name=False) : | ||||
|     """ | ||||
|     Extracts base information (coordinates, OSM ID, and optionally a name) from an OSM element. | ||||
|  | ||||
|     This function retrieves the latitude and longitude coordinates, OSM ID, and optionally the name | ||||
|     of a given OpenStreetMap (OSM) element. It handles different OSM types (e.g., 'node', 'way') by | ||||
|     extracting coordinates either directly or from a center tag, depending on the element type. | ||||
|  | ||||
|     Args: | ||||
|         elem (ET.Element): The XML element representing the OSM entity. | ||||
|         osm_type (str): The type of the OSM entity (e.g., 'node', 'way'). If 'node', the coordinates | ||||
|                         are extracted directly from the element; otherwise, from the 'center' tag. | ||||
|         with_name (bool): Whether to extract and return the name of the element. If True, it attempts | ||||
|                           to find the 'name' tag within the element and return its value. Defaults to False. | ||||
|  | ||||
|     Returns: | ||||
|         tuple: A tuple containing: | ||||
|             - osm_id (str): The OSM ID of the element. | ||||
|             - coords (tuple): A tuple of (latitude, longitude) coordinates. | ||||
|             - name (str, optional): The name of the element if `with_name` is True; otherwise, not included. | ||||
|     """ | ||||
|     # 1. extract coordinates | ||||
|     if osm_type != 'node' : | ||||
|         center = elem.find('center') | ||||
|         lat = float(center.get('lat')) | ||||
|         lon = float(center.get('lon')) | ||||
|  | ||||
|     else : | ||||
|         lat = float(elem.get('lat')) | ||||
|         lon = float(elem.get('lon')) | ||||
|  | ||||
|     coords = tuple((lat, lon)) | ||||
|  | ||||
|     # 2. Extract OSM id | ||||
|     osm_id = elem.get('id') | ||||
|  | ||||
|     # 3. Extract name if specified and return | ||||
|     if with_name : | ||||
|         name = elem.find("tag[@k='name']").get('v') if elem.find("tag[@k='name']") is not None else None | ||||
|         return osm_id, coords, name | ||||
|     else : | ||||
|         return osm_id, coords | ||||
|   | ||||
| @@ -6,7 +6,7 @@ import numpy as np | ||||
| from sklearn.cluster import DBSCAN | ||||
| from pydantic import BaseModel | ||||
|  | ||||
| from ..overpass.overpass import Overpass | ||||
| from ..overpass.overpass import Overpass, get_base_info | ||||
| from ..structs.landmark import Landmark | ||||
| from .get_time_distance import get_distance | ||||
| from ..constants import OSM_CACHE_DIR | ||||
| @@ -117,16 +117,10 @@ class ClusterManager: | ||||
|             for osm_type in osm_types : | ||||
|                 for elem in result.findall(osm_type): | ||||
|                      | ||||
|                     if osm_type != 'node' : | ||||
|                         center = elem.find('center') | ||||
|                         lat = float(center.get('lat')) | ||||
|                         lon = float(center.get('lon')) | ||||
|                         points.append(tuple((lat, lon))) | ||||
|  | ||||
|                     else : | ||||
|                         lat = float(elem.get('lat')) | ||||
|                         lon = float(elem.get('lon')) | ||||
|                         points.append(tuple((lat, lon))) | ||||
|                     # Get coordinates and append them to the points list | ||||
|                     _, coords = get_base_info(elem, osm_type) | ||||
|                     if coords is not None : | ||||
|                         points.append(coords) | ||||
|  | ||||
|             if points : | ||||
|                 self.all_points = np.array(points) | ||||
| @@ -263,24 +257,10 @@ class ClusterManager: | ||||
|  | ||||
|             for osm_type in osm_types : | ||||
|                 for elem in result.findall(osm_type): | ||||
|                     name = elem.find("tag[@k='name']").get('v') if elem.find("tag[@k='name']") is not None else None | ||||
|                     center = elem.find('center') | ||||
|  | ||||
|                     # Extract the center latitude and longitude if available. | ||||
|                     if name is None : | ||||
|                         continue | ||||
|                     id, coords, name = get_base_info(elem, osm_type, with_name=True) | ||||
|  | ||||
|                     if osm_type != 'node' : | ||||
|                         lat = float(center.get('lat')) | ||||
|                         lon = float(center.get('lon')) | ||||
|  | ||||
|                     else : | ||||
|                         lat = float(elem.get('lat')) | ||||
|                         lon = float(elem.get('lon')) | ||||
|  | ||||
|                     coords = tuple((lat, lon)) | ||||
|  | ||||
|                     if coords is None : | ||||
|                     if name is None or coords is None : | ||||
|                         continue | ||||
|  | ||||
|                     d = get_distance(cluster.centroid, coords) | ||||
| @@ -288,7 +268,7 @@ class ClusterManager: | ||||
|                         min_dist = d | ||||
|                         new_name = name | ||||
|                         osm_type = osm_type     # Add type: 'way' or 'relation' | ||||
|                         osm_id = elem.get('id')     # Add OSM id | ||||
|                         osm_id = id             # Add OSM id | ||||
|  | ||||
|         return Landmark( | ||||
|             name=new_name, | ||||
|   | ||||
| @@ -8,7 +8,7 @@ from ..structs.preferences import Preferences | ||||
| from ..structs.landmark import Landmark | ||||
| from .take_most_important import take_most_important | ||||
| from .cluster_manager import ClusterManager | ||||
| from ..overpass.overpass import Overpass | ||||
| from ..overpass.overpass import Overpass, get_base_info | ||||
|  | ||||
| from ..constants import AMENITY_SELECTORS_PATH, LANDMARK_PARAMETERS_PATH, OPTIMIZER_PARAMETERS_PATH, OSM_CACHE_DIR | ||||
|  | ||||
| @@ -239,28 +239,19 @@ class LandmarkManager: | ||||
|         landmarks = [] | ||||
|         for osm_type in ['node', 'way', 'relation'] : | ||||
|             for elem in root.findall(osm_type): | ||||
|                 name = elem.find("tag[@k='name']").get('v') if elem.find("tag[@k='name']") is not None else None | ||||
|                 tags = elem.findall('tag') | ||||
|  | ||||
|                 if osm_type != 'node' : | ||||
|                     center = elem.find('center') | ||||
|                     lat = float(center.get('lat')) | ||||
|                     lon = float(center.get('lon')) | ||||
|                     coords = tuple((lat, lon)) | ||||
|  | ||||
|                 else : | ||||
|                     lat = float(elem.get('lat')) | ||||
|                     lon = float(elem.get('lon')) | ||||
|                     coords = tuple((lat, lon)) | ||||
|                 id, coords, name = get_base_info(elem, osm_type, with_name=True) | ||||
|  | ||||
|                 if name is None or coords is None : | ||||
|                     continue | ||||
|  | ||||
|                 tags = elem.findall('tag') | ||||
|  | ||||
|                 # Convert this to Landmark object | ||||
|                 landmark = Landmark(name=name, | ||||
|                                     type=landmarktype, | ||||
|                                     location=coords, | ||||
|                                     osm_id=elem.get('id'), | ||||
|                                     osm_id=id, | ||||
|                                     osm_type=osm_type, | ||||
|                                     attractiveness=0, | ||||
|                                     n_tags=len(tags)) | ||||
|   | ||||
| @@ -2,7 +2,7 @@ | ||||
| import logging | ||||
| import xml.etree.ElementTree as ET | ||||
|  | ||||
| from ..overpass.overpass import Overpass | ||||
| from ..overpass.overpass import Overpass, get_base_info | ||||
| from ..structs.landmark import Toilets | ||||
| from ..constants import OSM_CACHE_DIR | ||||
|  | ||||
| @@ -98,23 +98,12 @@ class ToiletsManager: | ||||
|         toilets_list = [] | ||||
|         for osm_type in ['node', 'way', 'relation'] : | ||||
|             for elem in root.findall(osm_type): | ||||
|                 center = elem.find('center') | ||||
|  | ||||
|                 # Extract the center latitude and longitude if available. | ||||
|                 if osm_type != 'node' : | ||||
|                     lat = float(center.get('lat')) | ||||
|                     lon = float(center.get('lon')) | ||||
|                     location = tuple((lat, lon)) | ||||
|  | ||||
|                 else : | ||||
|                     lat = float(elem.get('lat')) | ||||
|                     lon = float(elem.get('lon')) | ||||
|                     location = tuple((lat, lon)) | ||||
|  | ||||
|                 if location is None : | ||||
|                 # Get coordinates and append them to the points list | ||||
|                 _, coords = get_base_info(elem, osm_type) | ||||
|                 if coords is None : | ||||
|                     continue | ||||
|  | ||||
|                 toilets = Toilets(location=location) | ||||
|                 toilets = Toilets(location=coords) | ||||
|  | ||||
|                 # Extract tags as a dictionary | ||||
|                 tags = {tag.get('k'): tag.get('v') for tag in elem.findall('tag')} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user