Skip to content

Commit

Permalink
add more details
Browse files Browse the repository at this point in the history
  • Loading branch information
Ines Elmufti authored and Ines Elmufti committed Feb 9, 2025
1 parent 31bd869 commit ae27533
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions instageo/apps/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,26 @@ def is_tile_in_viewport(tile_bounds: dict, viewport: dict, zoom: float) -> bool:
"""Check if a tile is within the current viewport."""
lat_min, lat_max = viewport['latitude']['min'], viewport['latitude']['max']
lon_min, lon_max = viewport['longitude']['min'], viewport['longitude']['max']
lat_min -= 1/zoom
lat_max += 1/zoom
lon_min -= 1/zoom
lon_max += 1/zoom
lat_min -= 3/zoom
lat_max += 3/zoom
lon_min -= 3/zoom
lon_max += 3/zoom
tile_lat_min, tile_lat_max = tile_bounds['lat_min'], tile_bounds['lat_max']
tile_lon_min, tile_lon_max = tile_bounds['lon_min'], tile_bounds['lon_max']
return not (tile_lat_max < lat_min or tile_lat_min > lat_max or
tile_lon_max < lon_min or tile_lon_min > lon_max)

@lru_cache(maxsize = 16)
@lru_cache(maxsize = 8)
def read_geotiff_to_xarray(filepath: str) -> tuple[xr.Dataset, CRS]:
"""Read GeoTIFF file into an xarray Dataset."""
xarr_dataset = xr.open_dataset(filepath).sel(band=1)
crs = rasterio.open(filepath).crs
return xarr_dataset, crs
# get scale based on zoom level
# def zoom_to_scale(zoom: float, growth_rate: float = 0.2) -> float:
# scale = 1 - math.exp(-growth_rate * zoom)
# return round(scale, 3)

def zoom_to_scale(zoom: float):
zoom_dict = {1:0.1,2:0.15,3:0.2,4:0.25,5:0.5,6:0.6,7:0.7,8:0.8}
zoom_dict = {1:0.1,2:0.1,3:0.1,4:0.25,5:0.5,6:0.6,7:0.1,8:0.1}
zoom_ceiled = math.ceil(zoom)
print("zoom ciel",zoom_ceiled)
if zoom_ceiled in zoom_dict.keys():
scale = zoom_dict[zoom_ceiled]
else:
Expand All @@ -83,13 +80,15 @@ def create_map_with_geotiff_tiles(tile_metadata: list, viewport: dict, zoom: flo
)
mapbox_layers = []
for tile in tile_metadata:
if len(mapbox_layers) > 10:
break
if is_tile_in_viewport(tile['bounds'], viewport, zoom=zoom):
tile_path = os.path.join(base_dir, tile['name'])
xarr_dataset, crs = read_geotiff_to_xarray(tile_path)
scale = zoom_to_scale(zoom)
print("--zoom--", zoom)
print("----sclale",scale)
img, coordinates = add_raster_to_plotly_figure(xarr_dataset, crs, scale=scale if zoom > 8 else 0.5)
img, coordinates = add_raster_to_plotly_figure(xarr_dataset, crs, scale=scale)
mapbox_layers.append({"sourcetype": "image", "source": img, "coordinates": coordinates})
fig.update_layout(mapbox_layers=mapbox_layers)
return fig
Expand Down

0 comments on commit ae27533

Please sign in to comment.