Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML representation from query raises IndexError for empty result #79

Closed
DahnJ opened this issue Aug 27, 2024 · 2 comments · Fixed by #80
Closed

HTML representation from query raises IndexError for empty result #79

DahnJ opened this issue Aug 27, 2024 · 2 comments · Fixed by #80

Comments

@DahnJ
Copy link

DahnJ commented Aug 27, 2024

Thanks for this package, looks like a great API for vector-raster operations!

Description

HTML representation from query raises IndexError for empty results.

MRE

import geopandas as gpd
import xarray as xr
import xvec

from geodatasets import get_path

from shapely.geometry import Point

counties = gpd.read_file(get_path("geoda.natregimes"))

# From https://xvec.readthedocs.io/en/stable/intro.html
cube = xr.Dataset(
    data_vars=dict(
        population=(["county", "year"], counties[["PO60", "PO70", "PO80", "PO90"]]),
        unemployment=(["county", "year"], counties[["UE60", "UE70", "UE80", "UE90"]]),
        divorce=(["county", "year"], counties[["DV60", "DV70", "DV80", "DV90"]]),
        age=(["county", "year"], counties[["MA60", "MA70", "MA80", "MA90"]]),
    ),
    coords=dict(county=counties.geometry, year=[1960, 1970, 1980, 1990]),
).xvec.set_geom_indexes("county", crs=counties.crs)

# Has 1 result, HTML representation works fine
result = cube.xvec.query(
    "county", 
    [Point(-94.9, 48.7).buffer(0.1)],
    predicate="within"
)
result._repr_html_()

# This has 0 results, errors during HTML repr
result = cube.xvec.query(
    "county", 
    [Point(-94.9, 48.7).buffer(1)],
    predicate="within"
)
result._repr_html_()
Traceback
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[16], line 1
----> 1 result._repr_html_()

File ~/miniconda3/envs/geo/lib/python3.12/site-packages/xarray/core/dataset.py:2578, in Dataset._repr_html_(self)
   2576 if OPTIONS["display_style"] == "text":
   2577     return f"<pre>{escape(repr(self))}</pre>"
-> 2578 return formatting_html.dataset_repr(self)

File ~/miniconda3/envs/geo/lib/python3.12/site-packages/xarray/core/formatting_html.py:344, in dataset_repr(ds)
    336 obj_type = f"xarray.{type(ds).__name__}"
    338 header_components = [f"<div class='xr-obj-type'>{escape(obj_type)}</div>"]
    340 sections = [
    341     dim_section(ds),
    342     coord_section(ds.coords),
    343     datavar_section(ds.data_vars),
--> 344     index_section(_get_indexes_dict(ds.xindexes)),
    345     attr_section(ds.attrs),
    346 ]
    348 return _obj_repr(ds, header_components, sections)

File ~/miniconda3/envs/geo/lib/python3.12/site-packages/xarray/core/formatting_html.py:205, in _mapping_section(mapping, name, details_func, max_items_collapse, expand_option_name, enabled)
    198 expanded = _get_boolean_with_default(
    199     expand_option_name, n_items < max_items_collapse
    200 )
    201 collapsed = not expanded
    203 return collapsible_section(
    204     name,
--> 205     details=details_func(mapping),
    206     n_items=n_items,
    207     enabled=enabled,
    208     collapsed=collapsed,
    209 )

File ~/miniconda3/envs/geo/lib/python3.12/site-packages/xarray/core/formatting_html.py:165, in summarize_indexes(indexes)
    164 def summarize_indexes(indexes) -> str:
--> 165     indexes_li = "".join(
    166         f"<li class='xr-var-item'>{summarize_index(v, i)}</li>"
    167         for v, i in indexes.items()
    168     )
    169     return f"<ul class='xr-var-list'>{indexes_li}</ul>"

File ~/miniconda3/envs/geo/lib/python3.12/site-packages/xarray/core/formatting_html.py:166, in <genexpr>(.0)
    164 def summarize_indexes(indexes) -> str:
    165     indexes_li = "".join(
--> 166         f"<li class='xr-var-item'>{summarize_index(v, i)}</li>"
    167         for v, i in indexes.items()
    168     )
    169     return f"<ul class='xr-var-list'>{indexes_li}</ul>"

File ~/miniconda3/envs/geo/lib/python3.12/site-packages/xarray/core/formatting_html.py:150, in summarize_index(coord_names, index)
    148 index_id = f"index-{uuid.uuid4()}"
    149 preview = escape(inline_index_repr(index))
--> 150 details = short_index_repr_html(index)
    152 data_icon = _icon("icon-database")
    154 return (
    155     f"<div class='xr-index-name'><div>{name}</div></div>"
    156     f"<div class='xr-index-preview'>{preview}</div>"
   (...)
    160     f"<div class='xr-index-data'>{details}</div>"
    161 )

File ~/miniconda3/envs/geo/lib/python3.12/site-packages/xarray/core/formatting_html.py:142, in short_index_repr_html(index)
    139 if hasattr(index, "_repr_html_"):
    140     return index._repr_html_()
--> 142 return f"<pre>{escape(repr(index))}</pre>"

File ~/miniconda3/envs/geo/lib/python3.12/site-packages/xvec/index.py:290, in GeometryIndex.__repr__(self)
    288     return f"GeometryIndex([{wkts[0]}], crs={srs})"
    289 joined = "\n ".join(wkts[1:])
--> 290 return f"GeometryIndex(\n[{wkts[0]}\n {joined}],\ncrs={srs})".replace(
    291     "\n", "\n" + " " * 4
    292 )

IndexError: list index out of range

Environment

  • Python 3.12
  • xvec: 0.3.0
  • xarray: 2024.7.0
@keewis

This comment was marked as resolved.

@martinfleis
Copy link
Member

@DahnJ Thanks! Fixed!

@github-staff github-staff deleted a comment from DahnJ Aug 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants