Search code examples
pythonnumpypython-requestsrasterio

Storing output of URL directly into numpy raster


Is there a way to store the output of a url that returns a geotiff (or tiff file) directly into a numpy array or rasterio variable using the python requests library (or any other python library)? I can use python requests for a json like this:

requests.get(URL).json()

Solution

  • requests.get(URL).content
    

    gives you the binary data from your file which you may be able to convert using the numpy.frombuffer function. But if I remember correctly, the geotiff format has some header information which you would have to offset for.

    Alternatively, you could save the file to disc

    open('myfile.tiff','wb').write(requests.get(URL).content)
    

    and then read it using something like the scipy.ndimage.imread function.