Yaswanth56 commited on
Commit
2c07583
·
verified ·
1 Parent(s): b7f96db

Update services/map_service.py

Browse files
Files changed (1) hide show
  1. services/map_service.py +8 -12
services/map_service.py CHANGED
@@ -1,15 +1,18 @@
1
  import folium
2
- from PIL import Image
3
- import io
4
 
5
  def generate_map(gps_coordinates, cracks):
6
  if not gps_coordinates or not cracks:
7
  return None
8
 
 
9
  avg_lat = sum(coord[0] for coord in gps_coordinates) / len(gps_coordinates)
10
  avg_lon = sum(coord[1] for coord in gps_coordinates) / len(gps_coordinates)
 
 
11
  m = folium.Map(location=[avg_lat, avg_lon], zoom_start=15)
12
 
 
13
  for coord, crack in zip(gps_coordinates[-len(cracks):], cracks):
14
  folium.Marker(
15
  location=coord,
@@ -17,16 +20,9 @@ def generate_map(gps_coordinates, cracks):
17
  icon=folium.Icon(color='red' if crack['severity'] == 'Severe' else 'orange' if crack['severity'] == 'Moderate' else 'green')
18
  ).add_to(m)
19
 
20
- # Saving the map to an HTML file (no conversion to image needed here)
21
  map_path_html = "map_temp.html"
22
  m.save(map_path_html)
23
 
24
- # Convert the HTML map to an image using PIL (without wkhtmltoimage)
25
- try:
26
- img = Image.open(map_path_html)
27
- img_path = "map_temp.png"
28
- img.save(img_path)
29
- return img_path
30
- except Exception as e:
31
- print(f"Error converting map to PNG: {str(e)}")
32
- return None
 
1
  import folium
2
+ import os
 
3
 
4
  def generate_map(gps_coordinates, cracks):
5
  if not gps_coordinates or not cracks:
6
  return None
7
 
8
+ # Calculate the average latitude and longitude
9
  avg_lat = sum(coord[0] for coord in gps_coordinates) / len(gps_coordinates)
10
  avg_lon = sum(coord[1] for coord in gps_coordinates) / len(gps_coordinates)
11
+
12
+ # Create a Folium map centered at the average GPS coordinates
13
  m = folium.Map(location=[avg_lat, avg_lon], zoom_start=15)
14
 
15
+ # Add markers for each crack
16
  for coord, crack in zip(gps_coordinates[-len(cracks):], cracks):
17
  folium.Marker(
18
  location=coord,
 
20
  icon=folium.Icon(color='red' if crack['severity'] == 'Severe' else 'orange' if crack['severity'] == 'Moderate' else 'green')
21
  ).add_to(m)
22
 
23
+ # Save the map as an HTML file
24
  map_path_html = "map_temp.html"
25
  m.save(map_path_html)
26
 
27
+ # Return the HTML map file path
28
+ return map_path_html