From e298c1c981028bf67f923e8fe5a6afd1a2a9016e Mon Sep 17 00:00:00 2001 From: Andrew Huang Date: Thu, 29 Aug 2024 16:42:00 -0700 Subject: [PATCH 1/3] attempt 1 --- hvplot/converter.py | 22 +++++++++++++++++++++- hvplot/util.py | 28 +++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/hvplot/converter.py b/hvplot/converter.py index 00e31fe12..74b4402eb 100644 --- a/hvplot/converter.py +++ b/hvplot/converter.py @@ -59,6 +59,7 @@ is_cudf, is_streamz, is_ibis, + is_xvec, is_xarray, is_xarray_dataarray, process_crs, @@ -1048,6 +1049,7 @@ def _process_data( if col is not None: grid.append(col) streaming = False + print('HEY') if is_geodataframe(data): datatype = 'geopandas' if hasattr(data, 'geom_type') else 'spatialpandas' self.data = data @@ -1123,7 +1125,20 @@ def _process_data( coords = [c for c in data.coords if data[c].shape != () and c not in ignore] dims = [c for c in data.dims if data[c].shape != () and c not in ignore] - if kind is None and (not (x or y) or all(c in data.coords for c in (x, y))): + use_xvec = is_xvec(data) + if kind is None and use_xvec: + first_index_name = list(data.xvec.geom_coords)[0] + first_index_values = data[first_index_name][0].item() + geom_type = type(first_index_values).__name__.replace('Multi', '') + if kind is None: + if geom_type == 'Point': + kind = 'points' + elif geom_type == 'Polygon': + kind = 'polygons' + elif geom_type in ('LineString', 'LineRing', 'Line'): + kind = 'paths' + + elif kind is None and (not (x or y) or all(c in data.coords for c in (x, y))): if list(data.coords) == ['band', 'y', 'x']: kind = 'rgb' gridded = True @@ -1154,6 +1169,7 @@ def _process_data( by, groupby, use_dask, + use_xvec, persist, gridded, label, @@ -1190,6 +1206,8 @@ def _process_data( elif isinstance(da, xr.Dataset): self._title = partial(xr.DataArray._title_for_slice, da)() + print(x, y, by_new, groupby_new) + self.data = data else: raise ValueError(f'Supplied data type {type(data).__name__} not understood') @@ -2870,6 +2888,8 @@ def _geom_plot(self, x=None, y=None, data=None, kind='polygons'): else: obj = obj.overlay(sort=False) else: + kdims = ['x', 'y'] + print(data, kdims, vdims, params) obj = element(data, kdims, vdims, **params) return ( diff --git a/hvplot/util.py b/hvplot/util.py index 75f1df016..77bd6075d 100644 --- a/hvplot/util.py +++ b/hvplot/util.py @@ -430,6 +430,14 @@ def is_streamz(data): return sdf and isinstance(data, (sdf.DataFrame, sdf.Series, sdf.DataFrames, sdf.Seriess)) +def is_xvec(data): + if not hasattr(data, 'xvec'): + return False + import xvec # noqa + + return len(data.xvec.geom_coords) > 0 + + def is_xarray(data): if not check_library(data, 'xarray'): return False @@ -482,10 +490,24 @@ def is_geodataframe(data): def process_xarray( - data, x, y, by, groupby, use_dask, persist, gridded, label, value_label, other_dims, kind=None + data, + x, + y, + by, + groupby, + use_dask, + use_xvec, + persist, + gridded, + label, + value_label, + other_dims, + kind=None, ): import xarray as xr + print(type(data)) + if isinstance(data, xr.Dataset): dataset = data else: @@ -509,6 +531,9 @@ def process_xarray( data_vars = list(dataset.data_vars) ignore = (by or []) + (groupby or []) + if use_xvec: + ignore += list(data.xvec.geom_coords) + dims = [c for c in dataset.coords if dataset[c].shape != () and c not in ignore][::-1] index_dims = [d for d in dims if d in dataset.indexes] @@ -565,6 +590,7 @@ def process_xarray( if groupby is None: groupby = [c for c in leftover_dims if c not in (by or [])] + return data, x, y, by, groupby From 401e685611dfb523c235521f5ba608e02c90ab41 Mon Sep 17 00:00:00 2001 From: Andrew Huang Date: Thu, 29 Aug 2024 17:00:35 -0700 Subject: [PATCH 2/3] attempt 2 --- hvplot/converter.py | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/hvplot/converter.py b/hvplot/converter.py index 74b4402eb..b1b03f80d 100644 --- a/hvplot/converter.py +++ b/hvplot/converter.py @@ -1049,7 +1049,22 @@ def _process_data( if col is not None: grid.append(col) streaming = False - print('HEY') + + if is_xvec(data): + import xvec # noqa: F401 + + geom_coords = list(data.xvec.geom_coords) + if len(geom_coords) > 1: + param.main.param.warning( + f'Only the first geometry coord will be rendered: {geom_coords[0]!r}. The ' + f"others are 'flattened' in groupby: {geom_coords[1:]}" + ) + data = data.drop_vars(geom_coords[1:]) + if groupby is None: + groupby = [dim for dim in data.dims if dim != geom_coords[0]] + data = data.xvec.to_geodataframe() + self.source_data = data + if is_geodataframe(data): datatype = 'geopandas' if hasattr(data, 'geom_type') else 'spatialpandas' self.data = data @@ -1125,20 +1140,7 @@ def _process_data( coords = [c for c in data.coords if data[c].shape != () and c not in ignore] dims = [c for c in data.dims if data[c].shape != () and c not in ignore] - use_xvec = is_xvec(data) - if kind is None and use_xvec: - first_index_name = list(data.xvec.geom_coords)[0] - first_index_values = data[first_index_name][0].item() - geom_type = type(first_index_values).__name__.replace('Multi', '') - if kind is None: - if geom_type == 'Point': - kind = 'points' - elif geom_type == 'Polygon': - kind = 'polygons' - elif geom_type in ('LineString', 'LineRing', 'Line'): - kind = 'paths' - - elif kind is None and (not (x or y) or all(c in data.coords for c in (x, y))): + if kind is None and (not (x or y) or all(c in data.coords for c in (x, y))): if list(data.coords) == ['band', 'y', 'x']: kind = 'rgb' gridded = True @@ -1169,7 +1171,6 @@ def _process_data( by, groupby, use_dask, - use_xvec, persist, gridded, label, @@ -1206,8 +1207,6 @@ def _process_data( elif isinstance(da, xr.Dataset): self._title = partial(xr.DataArray._title_for_slice, da)() - print(x, y, by_new, groupby_new) - self.data = data else: raise ValueError(f'Supplied data type {type(data).__name__} not understood') @@ -2888,8 +2887,6 @@ def _geom_plot(self, x=None, y=None, data=None, kind='polygons'): else: obj = obj.overlay(sort=False) else: - kdims = ['x', 'y'] - print(data, kdims, vdims, params) obj = element(data, kdims, vdims, **params) return ( From 3243cbba744fe198f6762ec9443107fcb998a2d6 Mon Sep 17 00:00:00 2001 From: Andrew Huang Date: Thu, 29 Aug 2024 17:04:45 -0700 Subject: [PATCH 3/3] cleanup --- hvplot/util.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/hvplot/util.py b/hvplot/util.py index 77bd6075d..9eeb4187c 100644 --- a/hvplot/util.py +++ b/hvplot/util.py @@ -496,7 +496,6 @@ def process_xarray( by, groupby, use_dask, - use_xvec, persist, gridded, label, @@ -506,8 +505,6 @@ def process_xarray( ): import xarray as xr - print(type(data)) - if isinstance(data, xr.Dataset): dataset = data else: @@ -531,8 +528,6 @@ def process_xarray( data_vars = list(dataset.data_vars) ignore = (by or []) + (groupby or []) - if use_xvec: - ignore += list(data.xvec.geom_coords) dims = [c for c in dataset.coords if dataset[c].shape != () and c not in ignore][::-1] index_dims = [d for d in dims if d in dataset.indexes]