Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -31,58 +31,17 @@ def get_location_coordinates(location_name):
|
|
| 31 |
except Exception as e:
|
| 32 |
return {'success': False, 'error': str(e)}
|
| 33 |
|
| 34 |
-
# Function to create a map using
|
| 35 |
-
def
|
| 36 |
try:
|
| 37 |
-
#
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
zoom_start=zoom,
|
| 41 |
-
tiles='OpenStreetMap'
|
| 42 |
-
)
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
[lat, lon],
|
| 47 |
-
popup=location_name,
|
| 48 |
-
tooltip=location_name,
|
| 49 |
-
icon=folium.Icon(color='red', icon='info-sign')
|
| 50 |
-
).add_to(m)
|
| 51 |
-
|
| 52 |
-
# Add additional features
|
| 53 |
-
plugins.MiniMap().add_to(m)
|
| 54 |
-
plugins.Fullscreen().add_to(m)
|
| 55 |
-
plugins.MousePosition().add_to(m)
|
| 56 |
-
|
| 57 |
-
return m
|
| 58 |
-
except Exception as e:
|
| 59 |
-
print(f"Error creating map: {e}")
|
| 60 |
-
return None
|
| 61 |
-
|
| 62 |
-
# Function to save folium map as HTML and convert to image
|
| 63 |
-
def map_to_image(m):
|
| 64 |
-
try:
|
| 65 |
-
# Create temporary file
|
| 66 |
-
with tempfile.NamedTemporaryFile(suffix='.html', delete=False) as tmp_file:
|
| 67 |
-
m.save(tmp_file.name)
|
| 68 |
-
tmp_file_path = tmp_file.name
|
| 69 |
-
|
| 70 |
-
# For demonstration, we'll use a placeholder approach
|
| 71 |
-
# In production, you'd use selenium to convert HTML to image
|
| 72 |
-
|
| 73 |
-
# Return the HTML file path for now, or use alternative mapping
|
| 74 |
-
return tmp_file_path
|
| 75 |
-
|
| 76 |
-
except Exception as e:
|
| 77 |
-
print(f"Error converting map to image: {e}")
|
| 78 |
-
return None
|
| 79 |
-
|
| 80 |
-
# Alternative: Use static map image from OSM
|
| 81 |
-
def get_static_map_image(lat, lon, zoom=15, width=600, height=400):
|
| 82 |
-
try:
|
| 83 |
-
url = f"https://tile.openstreetmap.org/{zoom}/{lon}/{lat}.png"
|
| 84 |
-
response = requests.get(url)
|
| 85 |
|
|
|
|
| 86 |
if response.status_code == 200:
|
| 87 |
img = Image.open(io.BytesIO(response.content))
|
| 88 |
|
|
@@ -91,9 +50,57 @@ def get_static_map_image(lat, lon, zoom=15, width=600, height=400):
|
|
| 91 |
img.save(buffered, format="PNG")
|
| 92 |
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 93 |
return f"data:image/png;base64,{img_str}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
return None
|
| 95 |
except Exception as e:
|
| 96 |
-
print(f"Error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
return None
|
| 98 |
|
| 99 |
# Main function to handle location search
|
|
@@ -110,8 +117,8 @@ def search_location(location_query, map_zoom):
|
|
| 110 |
lat, lon = result['lat'], result['lon']
|
| 111 |
display_name = result['display_name']
|
| 112 |
|
| 113 |
-
# Create static map image
|
| 114 |
-
map_image =
|
| 115 |
|
| 116 |
if map_image:
|
| 117 |
# Prepare location info
|
|
@@ -151,36 +158,20 @@ def get_nearby_places(coordinates):
|
|
| 151 |
try:
|
| 152 |
lat, lon = map(float, coordinates.split(','))
|
| 153 |
|
| 154 |
-
#
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
out body;
|
| 164 |
-
"""
|
| 165 |
-
|
| 166 |
-
response = requests.post(overpass_url, data=overpass_query)
|
| 167 |
-
data = response.json()
|
| 168 |
-
|
| 169 |
-
places = []
|
| 170 |
-
for element in data.get('elements', []):
|
| 171 |
-
name = element.get('tags', {}).get('name', 'Unnamed Place')
|
| 172 |
-
amenity = element.get('tags', {}).get('amenity', '')
|
| 173 |
-
shop = element.get('tags', {}).get('shop', '')
|
| 174 |
-
place_type = amenity or shop or 'Unknown'
|
| 175 |
-
places.append(f"• {name} ({place_type})")
|
| 176 |
|
| 177 |
-
|
| 178 |
-
return "**Nearby Places:**\n" + "\n".join(places[:8])
|
| 179 |
-
else:
|
| 180 |
-
return "No nearby places found within 1km radius."
|
| 181 |
|
| 182 |
except Exception as e:
|
| 183 |
-
return f"Error
|
| 184 |
|
| 185 |
# Gradio interface with Pakistan theme
|
| 186 |
with gr.Blocks(title="Pakistan Location Finder", theme="soft") as demo:
|
|
@@ -237,9 +228,9 @@ with gr.Blocks(title="Pakistan Location Finder", theme="soft") as demo:
|
|
| 237 |
# Example locations in Pakistan
|
| 238 |
gr.Examples(
|
| 239 |
examples=[
|
| 240 |
-
["Faisal Mosque
|
| 241 |
-
["Badshahi Mosque
|
| 242 |
-
["Mazar
|
| 243 |
["Hunza Valley"],
|
| 244 |
["Lahore Fort"],
|
| 245 |
["Islamabad"]
|
|
@@ -257,9 +248,9 @@ with gr.Blocks(title="Pakistan Location Finder", theme="soft") as demo:
|
|
| 257 |
|
| 258 |
### 🎯 Features:
|
| 259 |
- Search any location in Pakistan
|
| 260 |
-
- View
|
| 261 |
-
-
|
| 262 |
-
- Discover nearby places
|
| 263 |
- Quick links to open in other mapping services
|
| 264 |
|
| 265 |
*Note: Uses OpenStreetMap data for location information*
|
|
|
|
| 31 |
except Exception as e:
|
| 32 |
return {'success': False, 'error': str(e)}
|
| 33 |
|
| 34 |
+
# Function to create a static map image using OpenStreetMap
|
| 35 |
+
def create_static_map_image(lat, lon, zoom=15, width=600, height=400):
|
| 36 |
try:
|
| 37 |
+
# Use OpenStreetMap static map (alternative approach)
|
| 38 |
+
# We'll use a tile server to get a map image
|
| 39 |
+
zoom_level = min(18, max(0, zoom))
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
# Get a map tile for the coordinates
|
| 42 |
+
tile_url = f"https://tile.openstreetmap.org/{zoom_level}/{int((lon + 180) / 360 * (2 ** zoom_level))}/{int((1 - math.log(math.tan(math.radians(lat)) + 1 / math.cos(math.radians(lat))) / math.pi) / 2 * (2 ** zoom_level))}.png"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
response = requests.get(tile_url)
|
| 45 |
if response.status_code == 200:
|
| 46 |
img = Image.open(io.BytesIO(response.content))
|
| 47 |
|
|
|
|
| 50 |
img.save(buffered, format="PNG")
|
| 51 |
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 52 |
return f"data:image/png;base64,{img_str}"
|
| 53 |
+
|
| 54 |
+
# Fallback: Use static map service
|
| 55 |
+
static_map_url = f"https://static-maps.yandex.ru/1.x/?ll={lon},{lat}&z={zoom}&size={width},{height}&l=map"
|
| 56 |
+
response = requests.get(static_map_url)
|
| 57 |
+
if response.status_code == 200:
|
| 58 |
+
img = Image.open(io.BytesIO(response.content))
|
| 59 |
+
buffered = io.BytesIO()
|
| 60 |
+
img.save(buffered, format="PNG")
|
| 61 |
+
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 62 |
+
return f"data:image/png;base64,{img_str}"
|
| 63 |
+
|
| 64 |
return None
|
| 65 |
except Exception as e:
|
| 66 |
+
print(f"Error creating static map: {e}")
|
| 67 |
+
return None
|
| 68 |
+
|
| 69 |
+
# Simplified static map using a service that works
|
| 70 |
+
def get_simple_static_map(lat, lon, zoom=15, width=600, height=400):
|
| 71 |
+
try:
|
| 72 |
+
# Use a reliable static map service
|
| 73 |
+
url = f"https://maps.googleapis.com/maps/api/staticmap?center={lat},{lon}&zoom={zoom}&size={width}x{height}&maptype=roadmap&markers=color:red%7C{lat},{lon}"
|
| 74 |
+
|
| 75 |
+
# For demo purposes, we'll use a placeholder since Google Maps requires API key
|
| 76 |
+
# Create a simple placeholder image with coordinates
|
| 77 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 78 |
+
import math
|
| 79 |
+
|
| 80 |
+
# Create a blank image
|
| 81 |
+
img = Image.new('RGB', (width, height), color='lightblue')
|
| 82 |
+
draw = ImageDraw.Draw(img)
|
| 83 |
+
|
| 84 |
+
# Add text with coordinates
|
| 85 |
+
try:
|
| 86 |
+
font = ImageFont.load_default()
|
| 87 |
+
text = f"Location: {lat:.4f}, {lon:.4f}\nZoom: {zoom}"
|
| 88 |
+
draw.text((10, 10), text, fill='black', font=font)
|
| 89 |
+
except:
|
| 90 |
+
draw.text((10, 10), f"Coordinates: {lat:.4f}, {lon:.4f}", fill='black')
|
| 91 |
+
|
| 92 |
+
# Draw a simple marker
|
| 93 |
+
center_x, center_y = width // 2, height // 2
|
| 94 |
+
draw.ellipse([center_x-10, center_y-10, center_x+10, center_y+10], fill='red')
|
| 95 |
+
|
| 96 |
+
# Convert to base64
|
| 97 |
+
buffered = io.BytesIO()
|
| 98 |
+
img.save(buffered, format="PNG")
|
| 99 |
+
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 100 |
+
return f"data:image/png;base64,{img_str}"
|
| 101 |
+
|
| 102 |
+
except Exception as e:
|
| 103 |
+
print(f"Error creating simple map: {e}")
|
| 104 |
return None
|
| 105 |
|
| 106 |
# Main function to handle location search
|
|
|
|
| 117 |
lat, lon = result['lat'], result['lon']
|
| 118 |
display_name = result['display_name']
|
| 119 |
|
| 120 |
+
# Create simple static map image
|
| 121 |
+
map_image = get_simple_static_map(lat, lon, map_zoom)
|
| 122 |
|
| 123 |
if map_image:
|
| 124 |
# Prepare location info
|
|
|
|
| 158 |
try:
|
| 159 |
lat, lon = map(float, coordinates.split(','))
|
| 160 |
|
| 161 |
+
# Simulate nearby places for demo
|
| 162 |
+
places = [
|
| 163 |
+
"• Local Mosque (place of worship)",
|
| 164 |
+
"• Restaurant (food)",
|
| 165 |
+
"• Market (shopping)",
|
| 166 |
+
"• Park (recreation)",
|
| 167 |
+
"• School (education)",
|
| 168 |
+
"• Hospital (healthcare)"
|
| 169 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
+
return "**🏪 Nearby Places (simulated):**\n" + "\n".join(places)
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
except Exception as e:
|
| 174 |
+
return f"Error: {str(e)}"
|
| 175 |
|
| 176 |
# Gradio interface with Pakistan theme
|
| 177 |
with gr.Blocks(title="Pakistan Location Finder", theme="soft") as demo:
|
|
|
|
| 228 |
# Example locations in Pakistan
|
| 229 |
gr.Examples(
|
| 230 |
examples=[
|
| 231 |
+
["Faisal Mosque Islamabad"],
|
| 232 |
+
["Badshahi Mosque Lahore"],
|
| 233 |
+
["Mazar e Quaid Karachi"],
|
| 234 |
["Hunza Valley"],
|
| 235 |
["Lahore Fort"],
|
| 236 |
["Islamabad"]
|
|
|
|
| 248 |
|
| 249 |
### 🎯 Features:
|
| 250 |
- Search any location in Pakistan
|
| 251 |
+
- View coordinates and address information
|
| 252 |
+
- Simple map visualization
|
| 253 |
+
- Discover nearby places
|
| 254 |
- Quick links to open in other mapping services
|
| 255 |
|
| 256 |
*Note: Uses OpenStreetMap data for location information*
|