[Python] How to reduce size of html component rendered from HoloView/GeoView with OSM tile...

Discussão em 'Python' iniciado por Stack, Novembro 30, 2024.

  1. Stack

    Stack Membro Participativo

    I have a requirement to display an interactive image of a geographic region with time slider. Geographic image will be overlayed with climate data like rainfall. User should be able to change the time range on slider and see where the rainfall is based on color map. User will always be looking at local geographic region like one city/one state (this will be predetermined). My data is in netcd4 format with dimensions lat, long, time and variable Rainfall. I am using OSM tiles for geographic region, and Geovie/Holoview for generating image and rendering it into html file. I will embed this html file as iframe in another website. My problem is I am not able to limit the tiles, when I start to zoom out, I am still able to zoom out to entire world and that my file size is in MBs, for one data set, it was 68 MB and for another it was 600MB.

    My code is below:

    import xarray as xr
    import holoviews as hv
    import geoviews as gv
    from geoviews import tile_sources

    # Set up Holoviews and GeoViews with the Bokeh backend
    hv.extension('bokeh')

    # Load the NetCDF file
    file_path = "RF25_ind2023_rfp25.nc" # Path to the NetCDF file
    # Replace with the path to your NetCDF file
    data = xr.open_dataset(file_path)

    # Extract variables
    time = data['TIME']
    lat = data['LATITUDE']
    lon = data['LONGITUDE']
    rainfall = data['RAINFALL']

    # Ensure latitude and longitude are dimensions, not coordinates
    rainfall = rainfall.transpose('TIME', 'LATITUDE', 'LONGITUDE')

    # Create a HoloMap of GeoViews Images
    images = {}
    tiles = tile_sources.OSM
    #.opts(frame_width=800, frame_height=600,global_extent=False, width=800, height=600,xlim=(60, 110), ylim=(0, 40))
    for t in time.values:
    rainfall_slice = rainfall.sel(TIME=t)
    gv_image = gv.Image(
    (lon, lat, rainfall_slice),
    kdims=['LONGITUDE', 'LATITUDE'],
    vdims=['RAINFALL']
    ).opts(alpha=0.5, cmap='Blues', colorbar=True, title="Rainfall",tools=['hover'])
    images[float(t)] = gv_image # Ensure time is numeric for the slider

    # Create a HoloMap indexed by time
    #holomap = hv.HoloMap(images, kdims=['TIME'])
    dim_values = time.values

    # Convert to a set
    dim_values_set = set(dim_values)

    # Print the set
    #print(dim_values_set)

    holomap= gv.HoloMap({t:(tiles*images[float(t)])
    for t in dim_values_set},kdims=['TIME'])
    holomap.opts(width=800,height=500)

    # Save the visualization as an HTML file


    output_file = 'rainfall_visualization_with_slider.html'
    hv.save(holomap, output_file, backend='bokeh')

    print(f"Visualization saved as {output_file}")



    Shold I be using something else? I tried setting options on tile sources, but that didn't work.

    Continue reading...

Compartilhe esta Página