Skip to content

Commit

Permalink
Allow url inputs to raster constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
fbunt committed Aug 21, 2024
1 parent a65e88f commit dc3d5bc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
15 changes: 10 additions & 5 deletions raster_tools/io.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import urllib
from pathlib import Path

import dask
Expand Down Expand Up @@ -142,16 +143,20 @@ def normalize_null_value(nv, dtype):
return nv


def open_raster_from_path(path):
def open_raster_from_path_or_url(path):
if type(path) in IO_UNDERSTOOD_TYPES:
path = str(path)
path = os.path.abspath(path)
else:
raise RasterIOError(
f"Could not resolve input to a raster path: '{path}'"
f"Could not resolve input to a raster path or URL: '{path}'"
)
validate_path(path)
ext = _get_extension(path)
if urllib.parse.urlparse(path) == "":
# Assume file path
validate_path(path)
ext = _get_extension(path)
else:
# URL
ext = ""

xrs = None
# Try to let gdal open anything but NC, HDF, GRIB files
Expand Down
4 changes: 2 additions & 2 deletions raster_tools/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
chunk,
is_batch_file,
normalize_xarray_data,
open_raster_from_path,
open_raster_from_path_or_url,
write_raster,
)

Expand Down Expand Up @@ -568,7 +568,7 @@ def get_raster_ds(raster):

ds = parse_batch_script(raster).final_raster._ds
else:
rs, mask, nv = open_raster_from_path(raster)
rs, mask, nv = open_raster_from_path_or_url(raster)
xmask = xr.DataArray(mask, dims=rs.dims, coords=rs.coords)
ds = make_raster_ds(rs.rio.write_nodata(nv), xmask)
ds = _xarray_to_raster_ds(ds)
Expand Down

0 comments on commit dc3d5bc

Please sign in to comment.