Streamlit for visuallisation of large GeoTiff files

Hi all,

I would need to visualise large geospatial datasets in GeoTiff format. Does streamlit work well with that? Does anyone have an example or tutorial that I could look at?

Many thanks

These can be displayed as a jpg, or do you need to keep the high resolution/file format?

Unfortunately jpg wonโ€™t do for me. I need to retain the high resolution file and format.

Sounds quite specialised. I would suggest also searching for examples where people have worked with medical imagery, which has similar specialised requirements

1 Like

Per StackOverflow, it sounds like TIFF files are not well supported in browsers:

I tried this in Chrome (5MB tif, guess it depends on what is โ€œlargeโ€) and it didnโ€™t render:

import streamlit as st

st.markdown("![Alt Text](https://download.osgeo.org/geotiff/samples/usgs/o41078a5.tif)")

I donโ€™t have a Mac to test on for Safari, but if itโ€™s going to work, I think this is how you would do it.

Best,
Randy

Hi Randy, thanks for the link. Yes, it seems there are some issues rendering the .tiff files.

1 Like

You can use Pillow to load the tiff, then display it directly or convert to png. If the issue is that the file is very large, you could convert it to a cloud optimised geotiff (COG) as these support range requests. You could then display one of the downsampled tiles

1 Like

@randyzwitch Can I upload GeoTiff files using streamlit.file_uploader() function in streamlit?

Why not? It just depends how large they are and what you want to do with them afterwards.

Can you please share some code sample. The size is 100MB. I have to later use that GeoTiff in my code. So how should I use it ? How to access the data of GeoTiff image ?

Hi @saksham_kumar_sharma,

How would you have worked with the GeoTiff image in Python if you werenโ€™t using Streamlit?

How to access the data of GeoTiff image ?

What Python package do you use to access the data of the GeoTiff image?

If can give me a code example (with an existing package) of how you intend to use and manipulate the GeoTiff image in Python, I could help you access the GeoTiff from the file uploader widget.

The file uploader docs show a few common examples of reading files uploaded via st.file_uploader:

1 Like

I want to load the numpy array of that GeoTiff image in python which I have uploaded. How should I do that? Geotiff image have a numpy array of shape (64,1200,1200). Please help me out with some code.

I want to load the numpy array of that GeoTiff image in python which I have uploaded. How should I do that? Geotiff image have a numpy array of shape (64,1200,1200). Please help me out with some code structure.

Have you even tried?
I would suggest: Read the documentation, look at the examples, try it yourself and if you get stuck, share your github link with us and we will help.

Yes I have tried but I am unable to do so.
I am not getting the original array which is of shape (64,1200,1200) but only a 1 dimension array of shape (47238183,).

I donโ€™t know why!
image

Please share some code snippet. i am not able to do with documentation.

This is not a streamlit related question, it is numpy or geopython related.
Anyway, to give you some hints, what about:

y = np.frombuffer(bytes_data, dtype="float32").reshape(64, 1200, 1200)

If you cannot sort this out with the numpy package, i assume you have to use some more sophisticated geopython related python packages, i would look into geotiff or gdal or rasterio packages for a possible solution.

Every time the user give a new image to streamlit file_uploader, so every time the shape is different. So can you please provide a more general solution
for example: user can give any shape like this -
(12,1200,1345)
(15,1500,3422)
(16,34,12)
(100,452,313)
so infinite amount of shapes are possible.

Can you please help me out in this.

I gave you some hints for geo related libraries.
If you want to handle geo data you have to deal with this stuff anyway.
Just use google and stackoverflow and you will find a lot of approaches:

python read geotiff

Thanks it worked.