CineAI commited on
Commit
7b3c9be
·
verified ·
1 Parent(s): a6b09ed

Create map_gen.py

Browse files
Files changed (1) hide show
  1. src/map_gen.py +49 -0
src/map_gen.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Optional
2
+
3
+ import folium
4
+ import geopy
5
+
6
+
7
+ class MapGenerator:
8
+ def __init__(self, timeout: int = 10):
9
+ self.geocoder = geopy.Nominatim(user_agent="TheAmateur", timeout=timeout)
10
+ self.line_coordinates = []
11
+
12
+ def get_map(self, location1: str, location2: Optional[str] = None, error: Optional[float] = None, direction: Optional[str] = None):
13
+ try:
14
+ l1 = self.geocoder.geocode(location1)
15
+ if not l1:
16
+ print(f"Warning: Location not found: {location1}")
17
+ return None
18
+
19
+ if location2:
20
+ l2 = self.geocoder.geocode(location2)
21
+ if not l2:
22
+ print(f"Warning: Location not found: {location2}")
23
+ return None
24
+
25
+ map_obj = folium.Map(location=[l2.latitude, l2.longitude], zoom_start=7)
26
+ folium.Marker([l1.latitude, l1.longitude], popup='Loc 1', icon=folium.Icon(icon="cloud")).add_to(map_obj)
27
+ folium.Marker([l2.latitude, l2.longitude], popup='Loc 2', icon=folium.Icon(color="green")).add_to(map_obj)
28
+
29
+ if error and direction:
30
+ self.line_coordinates.extend([[l1.latitude, l1.longitude], [l2.latitude, l2.longitude]])
31
+ text = f"{error}km in {direction}"
32
+ folium.PolyLine(
33
+ locations=self.line_coordinates,
34
+ color='blue',
35
+ weight=5,
36
+ opacity=0.8,
37
+ tooltip=text
38
+ ).add_to(map_obj)
39
+ else:
40
+ map_obj = folium.Map(location=[l1.latitude, l1.longitude], zoom_start=10)
41
+ folium.Marker([l1.latitude, l1.longitude], popup='Loc', icon=folium.Icon(icon="cloud")).add_to(map_obj)
42
+
43
+ return map_obj
44
+ except geopy.exc.GeocoderUnavailable as e:
45
+ print(f"Error: Geocoding service is unavailable. {e}")
46
+ return None
47
+ except Exception as e:
48
+ print(f"An unexpected error occurred in get_map(): {e}")
49
+ return None