sibi-dst 0.3.28__py3-none-any.whl → 0.3.29__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ from __future__ import annotations
2
+
3
+ from .geo_location_service import *
4
+
5
+ __all__ = [
6
+ 'GeolocationService'
7
+ ]
@@ -0,0 +1,63 @@
1
+ import os
2
+ from urllib.parse import urlparse
3
+ from geopy.geocoders import Nominatim
4
+ from geopy.exc import GeocoderTimedOut, GeocoderServiceError
5
+
6
+ app_nominatim_url = os.environ.get('NOMINATIM_URL', None)
7
+ app_geo_locator_test_place = os.environ.get('GEO_LOCATOR_TEST_PLACE', "San Jose, Costa Rica")
8
+
9
+
10
+ class GeolocationService:
11
+ debug: bool = False
12
+
13
+ def __init__(self, debug=False):
14
+ self.geolocator = None
15
+ self._initialize_geolocator()
16
+ self.debug = debug
17
+
18
+ def _initialize_geolocator(self):
19
+ nominatim_url = app_nominatim_url
20
+ if not nominatim_url:
21
+ if self.debug:
22
+ print("Nominatim URL not provided in environment variables.")
23
+ return
24
+
25
+ try:
26
+ parsed_url = urlparse(nominatim_url)
27
+ nominatim_url = parsed_url.netloc
28
+ self.geolocator = Nominatim(user_agent="ibis", scheme="http", domain=nominatim_url)
29
+
30
+ # Test the geolocator to ensure it is available
31
+ location = self.geolocator.geocode(app_geo_locator_test_place)
32
+ if location:
33
+ if self.debug:
34
+ print("Geolocator is available.")
35
+ else:
36
+ if self.debug:
37
+ print("Geolocator service is not responding correctly.")
38
+ self.geolocator = None
39
+ except (GeocoderTimedOut, GeocoderServiceError) as e:
40
+ print(f"Error initializing geolocator: {e}")
41
+ self.geolocator = None
42
+
43
+ def geocode(self, address):
44
+ if not self.geolocator:
45
+ if self.debug:
46
+ print("Geolocator is not available.")
47
+ return None
48
+ try:
49
+ return self.geolocator.geocode(address)
50
+ except (GeocoderTimedOut, GeocoderServiceError) as e:
51
+ print(f"Error during geocoding: {e}")
52
+ return None
53
+
54
+ def reverse(self, coordinates, exactly_one=True):
55
+ if not self.geolocator:
56
+ if self.debug:
57
+ print("Geolocator is not available.")
58
+ return None
59
+ try:
60
+ return self.geolocator.reverse(coordinates, exactly_one=exactly_one)
61
+ except (GeocoderTimedOut, GeocoderServiceError) as e:
62
+ print(f"Error during reverse geocoding: {e}")
63
+ return None
@@ -0,0 +1,55 @@
1
+ from .geo_location_service import GeolocationService, GeocoderTimedOut, GeocoderServiceError
2
+
3
+ # Initialize the geolocator once
4
+ geolocator = None
5
+
6
+
7
+ def get_geolocator():
8
+ global geolocator
9
+ if geolocator is None:
10
+ geolocator = GeolocationService(debug=True)
11
+ return geolocator
12
+
13
+
14
+ #geolocator = GeolocationService(debug=True)
15
+
16
+
17
+ def get_address_by_coordinates(latitude, longitude, exactly_one=True):
18
+ geolocator = get_geolocator()
19
+ try:
20
+ location = geolocator.reverse((latitude, longitude), exactly_one=exactly_one)
21
+ if not location:
22
+ return "No address found for this location."
23
+ address = location.address
24
+ return address
25
+ except GeocoderTimedOut:
26
+ return "GeocoderTimedOut: Failed to reach the server."
27
+
28
+
29
+ def get_coordinates_for_address(address):
30
+ """
31
+ Geocode an address using a custom Nominatim server.
32
+
33
+ :param address: The address to geocode.
34
+ :return: A dictionary with the location's latitude, longitude, and full address, or a message if an error occurs.
35
+ """
36
+ geolocator = get_geolocator()
37
+ try:
38
+ location = geolocator.geocode(address)
39
+
40
+ # Check if location was found
41
+ if location:
42
+ return {
43
+ "Address": location.address,
44
+ "Latitude": location.latitude,
45
+ "Longitude": location.longitude
46
+ }
47
+ else:
48
+ return "Location not found."
49
+
50
+ except GeocoderTimedOut:
51
+ return "GeocoderTimedOut: Request timed out."
52
+ except GeocoderServiceError as e:
53
+ return f"GeocoderServiceError: {str(e)}"
54
+ except Exception as e:
55
+ return f"Error: {str(e)}"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sibi-dst
3
- Version: 0.3.28
3
+ Version: 0.3.29
4
4
  Summary: Data Science Toolkit
5
5
  Author: Luis Valverde
6
6
  Author-email: lvalverdeb@gmail.com
@@ -26,6 +26,9 @@ sibi_dst/df_helper/core/_filter_handler.py,sha256=t3uLLJX5hWO_dWKCCz8Dwpc9RZ5PMH
26
26
  sibi_dst/df_helper/core/_params_config.py,sha256=Og3GYth0GVWpcOYWZWRy7CZ5PDsg63Nmqo-W7TUrA_0,3503
27
27
  sibi_dst/df_helper/core/_query_config.py,sha256=Y8LVSyaKuVkrPluRDkQoOwuXHQxner1pFWG3HPfnDHM,441
28
28
  sibi_dst/df_helper/data_cleaner.py,sha256=lkxQoXLvGzXCicFUimnA5nen5qkrO1oxgl_p2Be2o8w,5183
29
+ sibi_dst/geopy_helper/__init__.py,sha256=Q1RJiUZIOlV0QNNLjxZ_2IZS5LqIe5jRbeQkfD1Vm60,112
30
+ sibi_dst/geopy_helper/geo_location_service.py,sha256=l0dV0XuEk-tcWdaOymgN9WulR6xp3k7yJUgqYvnqCKo,2288
31
+ sibi_dst/geopy_helper/utils.py,sha256=R9X6ew0L_SuCpsA_AQK1wd3BspRGtV83q3mhBkcKr4A,1664
29
32
  sibi_dst/osmnx_helper/__init__.py,sha256=QeAKEeVXZk_qn8o0d3BOoGgv2lzatcI2yBqY3ZqviKI,153
30
33
  sibi_dst/osmnx_helper/base_osm_map.py,sha256=s2OY_XfwjZA3ImJNtCgevGBCbwRVe3dY3QVkTHEulB0,5794
31
34
  sibi_dst/osmnx_helper/basemaps/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -47,6 +50,6 @@ sibi_dst/utils/filepath_generator.py,sha256=hjI7gQwfwRToPeuzoUQDayHKQrr4Ivhi4Chl
47
50
  sibi_dst/utils/log_utils.py,sha256=4eLmoV8VC7wDwPr1mRfDKP24_-laGO6ogE4U0u3DUuA,2315
48
51
  sibi_dst/utils/parquet_saver.py,sha256=hLrWr1G132y94eLopDPPGQGDsAiR1lQ8id4QQtGYPE4,4349
49
52
  sibi_dst/utils/storage_manager.py,sha256=7nkfeBW_2xlF59pGj7V2aY5TLwpJnPQuPVclqjavJOA,3856
50
- sibi_dst-0.3.28.dist-info/METADATA,sha256=9xBeLwWalUf7exDK-0NZfnYmUQnOIdV2xa0PYNTd85I,2436
51
- sibi_dst-0.3.28.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
52
- sibi_dst-0.3.28.dist-info/RECORD,,
53
+ sibi_dst-0.3.29.dist-info/METADATA,sha256=KsqbOJSoa7LOBN5ibbG-MLCgCSGSI_Ps7LfdLHDPErQ,2436
54
+ sibi_dst-0.3.29.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
55
+ sibi_dst-0.3.29.dist-info/RECORD,,