Skip to content

Commit aa749fb

Browse files
authored
Support multiple GeoTIFF files (#109)
* Support GeoTIFF as well as NetCDF multifile raster * Cache grid file for MultiFileRasters * Use rioxarray merge_arrays rather than xarray.merge
1 parent f862e18 commit aa749fb

File tree

5 files changed

+157
-108
lines changed

5 files changed

+157
-108
lines changed

example_multiband/create_multiband_netcdf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
x = np.linspace(lon*scale + offset + dx/2, (lon + 1)*scale + offset - dx/2, nx)
3737
y = np.linspace(lat*scale + offset + dy/2, (lat + 1)*scale + offset - dy/2, ny)
38+
y = y[::-1]
3839
filename = os.path.join("example_multiband", f"dummy_{lon}_{lat}.nc")
3940

4041
red = rng.random(size=(ny, nx), dtype=np.float32)

mapshader/core.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
from mapshader.mercator import MercatorTileDefinition
2121
from mapshader.sources import MapSource
22-
from .multifile import MultiFileNetCDF
22+
from .multifile import MultiFileRaster
2323

2424
import spatialpandas
2525

@@ -76,8 +76,9 @@ def create_agg(source: MapSource,
7676
agg_func = source.agg_func
7777
geometry_type = source.geometry_type
7878

79-
if isinstance(source.data, MultiFileNetCDF):
79+
if isinstance(source.data, MultiFileRaster):
8080
dataset = source.data.load_overview(z, source.band)
81+
# Note this is really an xr.DataArray.
8182
if dataset is None:
8283
dataset = source.data.load_bounds(xmin, ymin, xmax, ymax, source.band,
8384
source.transforms)

mapshader/io.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from os.path import expanduser
1+
from os.path import expanduser, splitext
22

33
import geopandas as gpd
44
import numpy as np
@@ -34,31 +34,37 @@ def load_raster(file_path, transforms, force_recreate_overviews,
3434
The loaded data.
3535
"""
3636

37-
if file_path.endswith('.tif'):
37+
file_extension = splitext(file_path)[1]
38+
if not file_extension:
39+
raise RuntimeError(f"file_path does not have a file extension: {file_path}")
3840

39-
arr = xr.open_rasterio(expanduser(file_path),
40-
chunks={'y': 512, 'x': 512})
41+
arr = None
4142

42-
if hasattr(arr, 'nodatavals'):
43+
if '*' in file_path:
44+
# Multiple files.
45+
if file_extension in ['.nc', '.tif']:
46+
arr = SharedMultiFile.get(file_path, transforms, force_recreate_overviews)
4347

44-
if np.issubdtype(arr.data.dtype, np.integer):
45-
arr.data = arr.data.astype('f8')
48+
else:
49+
# Single file.
50+
if file_extension == '.tif':
51+
arr = xr.open_rasterio(expanduser(file_path), chunks={'y': 512, 'x': 512})
4652

47-
for val in arr.nodatavals:
48-
arr.data[arr.data == val] = np.nan
53+
if hasattr(arr, 'nodatavals'):
54+
if np.issubdtype(arr.data.dtype, np.integer):
55+
arr.data = arr.data.astype('f8')
4956

50-
arr.name = file_path
57+
for val in arr.nodatavals:
58+
arr.data[arr.data == val] = np.nan
5159

52-
elif file_path.endswith('.nc'):
60+
arr.name = file_path
5361

54-
if '*' in file_path:
55-
arr = SharedMultiFile.get(file_path, transforms, force_recreate_overviews)
56-
else:
62+
elif file_path.endswith('.nc'):
5763
# TODO: add chunk parameter to config
5864
arr = xr.open_dataset(file_path, chunks={'x': 512, 'y': 512})[layername]
5965
arr['name'] = file_path
6066

61-
else:
67+
if arr is None:
6268
raise TypeError(f"Unable to load raster {file_path}")
6369

6470
return arr

0 commit comments

Comments
 (0)