Spaces:
Sleeping
Sleeping
| # Example taken from here - https://shinylive.io/py/examples/#map | |
| import os | |
| from shiny import reactive | |
| from shiny.express import input, render, ui | |
| from shinywidgets import render_widget | |
| import ipyleaflet as ipyl | |
| import pandas as pd | |
| import geopandas as gpd | |
| from ipyleaflet import Map, Rectangle, basemaps, basemap_to_tiles, LayersControl, ColorMap, FeatureGroup | |
| from branca.colormap import linear | |
| from shapely.geometry import box | |
| def point_to_square(row, size=0.0009): | |
| x, y = row.geometry.x, row.geometry.y | |
| half = size / 2 | |
| return box(x - half, y - half, x + half, y + half) | |
| df = pd.read_csv("example.csv") | |
| # Step 2: Create GeoDataFrame with UTM Zone 15N | |
| gdf = gpd.GeoDataFrame( | |
| df, | |
| geometry=gpd.points_from_xy(df['x'], df['y']), | |
| crs="EPSG:32615" # <- Your current system | |
| ) | |
| # Step 3: Reproject to WGS84 (lat/lon) | |
| gdf = gdf.to_crs("EPSG:4326") | |
| # Convert points to small square polygons | |
| gdf["geometry_box"] = gdf.apply(point_to_square, axis=1) | |
| print(gdf.head()) | |
| # Step 4: Extract lat/lon columns for ipyleaflet | |
| gdf["lat"] = gdf.geometry.y | |
| gdf["lon"] = gdf.geometry.x | |
| temp_colormap = linear.YlOrRd_09.scale(df['LST'].min(), df['LST'].max()) | |
| # def generate_temp_layer(): | |
| # rectangles = [] | |
| # for _, row in gdf.iterrows(): | |
| # lat = row['lat'] | |
| # lon = row['lon'] | |
| # size_deg = 0.0009 # Approx ~100m (latitude degrees) | |
| # bounds = [ | |
| # (lat - size_deg / 2, lon - size_deg / 2), | |
| # (lat + size_deg / 2, lon + size_deg / 2) | |
| # ] | |
| # color = temp_colormap(row['LST']) | |
| # rect = Rectangle( | |
| # bounds=bounds, | |
| # color=color, | |
| # fill_color=color, | |
| # fill_opacity=0.5, | |
| # weight=0 | |
| # ) | |
| # rectangles.append(rect) | |
| # return rectangles | |
| city_centers = { | |
| "London": (51.5074, 0.1278), | |
| "Paris": (48.8566, 2.3522), | |
| "New York": (40.7128, -74.0060), | |
| "Guatemala city": (14.6349149, -90.5068824), | |
| "Zone 15": (14.61925, -90.49386) | |
| } | |
| ui.input_text("Text", "Some vector", "Zone 15") | |
| def map(): | |
| city = use_in_model(input.Text()) | |
| center = city_centers.get(city, (0, 0)) | |
| m = Map(center=center, zoom=13, basemap=basemaps.OpenStreetMap.Mapnik) | |
| # Add rectangle overlay | |
| geo_data = ipyl.GeoData( | |
| geo_dataframe=gdf, | |
| style={ | |
| "color": "black", | |
| "fillColor": "#3366cc", | |
| "opacity": 0.05, | |
| "weight": 1.9, | |
| "dashArray": "2", | |
| "fillOpacity": 0.6, | |
| }, | |
| hover_style={"fillColor": "red", "fillOpacity": 0.2}, | |
| name="Temperature", | |
| ) | |
| m.add(geo_data) | |
| m.add(ipyl.LayersControl()) | |
| return m | |
| # @render_widget | |
| # def map(): | |
| # city = use_in_model(input.Text()) | |
| # center = city_centers.get(city, (0, 0)) | |
| # # m = Map(center=center, zoom=13, basemap=basemaps.OpenStreetMap.Mapnik) | |
| # m = Map(center=city_centers["Guatemala city"], zoom=13, basemap=basemaps.OpenStreetMap.Mapnik) | |
| # print(f"Centering on city: {city}") | |
| # # Add rectangle overlay | |
| # for rect in generate_temp_layer(): | |
| # m.add_layer(rect) | |
| # # Optional: Add color legend | |
| # # m.add_control(temp_colormap.legend(title= "Temp")) | |
| # # Add controls | |
| # m.add_control(LayersControl(position="topright")) | |
| # return m | |
| def use_in_model(x): | |
| # TODO: Call some model loaded in huggingface_hub. | |
| # Define the client once then just call the | |
| # client from here. | |
| # from huggingface_hub import InferenceClient | |
| # client = InferenceClient( | |
| # api_key=os.environ["HF_TOKEN"], | |
| # provider="auto", | |
| # ) | |
| # | |
| # completion = client.chat.completions.create( | |
| # model="deepseek-ai/DeepSeek-V3-0324", | |
| # messages=[{"role": "user", "content": "A story about hiking in the mountains"}] | |
| # ) | |
| return x | |
| def text(): | |
| return ("You entered: " + use_in_model(input.Text())) | |
| def _(): | |
| try: | |
| map.widget.center = city_centers[use_in_model(input.Text())] | |
| except: | |
| pass |