Steamlit - Upload and use User Shapefile and Tif File

Hi all, how would I get a user to upload and then use a shapefile?

I want to upload and then use a .shp and .tif file to create an elevation using the pyosp package, but when trying to use it for pyosp I get the following error:

RuntimeError: not a string

I have tried converting to a shapefile as a string, but with additional errors. Any help would be much appreciated.

Hello @Geobro1!

If you have a package like pyosp that requires an actual file path, one option is to use NamedTemporyFile, like this (this example uses PIL, but it should work with any library that is reading from a file)

from tempfile import NamedTemporaryFile

import streamlit as st
from PIL import Image

uploaded = st.file_uploader("Upload a file", type=["tif"])

if uploaded:
    with NamedTemporaryFile("wb", suffix=".tif") as f:
        f.write(uploaded.getvalue())
        # f.name is the path of the temporary file
        im = Image.open(f.name)
1 Like

Thank you for your reply @blackary . Messed up and was calling it the wrong thing but that has worked perfectly thank you

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.