Repository navigation
Conversation
(Grid.from_file currently only adds special support for geopandas files; otherwise it dispatches to xarray open_dataset just like open_grid does.)
erogluorhan
left a comment
There was a problem hiding this comment.
This does what #1754 asks: ux.open_grid() now opens .shp/.geojson files, and the lon/lat swap fix in _read_multipolygon makes the US shapefile read correctly. The one-line root-cause fix and its targeted regression test are nicely done.
- Ran the touched tests on the PR (
test/io/test_geopandas.py,test/core/test_api.py,test/grid/grid/test_initialization.py): 130 passed. ruff check/format clean. - Mutation: with
_geopandas.pyreverted tomain,test_read_shpfileandtest_multipolygon_preserves_lon_latboth fail, so the regression tests catch the bug. - Swept
open_gridon base vs PR: str andPathinputs,.geojson,chunks,merge_coincident_nodes, a missing file, an upper-case extension, andopen_dataset(shp, data, ...). The problems found are in the inline comments.
Before merge: please look at the three inline comments. The upper-case extension is a nit.
| and os.path.splitext(grid_filename_or_obj)[1] in Grid._GEOPANDAS_FILE_EXTENSIONS | ||
| ): | ||
| # Special case for geopandas files (reject other kwargs, then use geopandas backend) | ||
| _got_kw = list(kwargs.keys()) + ([] if chunks is None else ["chunks"]) |
There was a problem hiding this comment.
Two chunks cases don't reach this check correctly:
ux.open_grid(shp, chunks={"n_face": 10})raisesOSError: NetCDF: Unknown file format, not this TypeError.match_chunks_to_ugrid(line ~100) opens the file with xarray before this branch.ux.open_dataset(shp, data, chunks=-1, chunk_grid=False)raises this TypeError even though the user asked not to chunk the grid. Checkinggrid_chunksinstead ofchunkswould fix that.
Moving the geopandas branch above the chunk handling should fix both. The test only covers chunks="auto", which skips match_chunks_to_ugrid.
| f"open_grid() for geopandas files (extension one of: {Grid._GEOPANDAS_FILE_EXTENSIONS}) " | ||
| f"got unexpected keyword argument(s) {_got_kw!r}, when filepath={grid_filename_or_obj!r}." | ||
| ) | ||
| grid = Grid.from_file(grid_filename_or_obj, backend="geopandas") |
There was a problem hiding this comment.
A missing .shp used to raise FileNotFoundError. It now raises pyogrio.errors.DataSourceError, a RuntimeError subclass. The removed test_read_shpfile in test_initialization.py was catching exactly this: restored on this branch, it fails with DataSourceError. That test was still working, since a missing file is its intended input. Could you raise FileNotFoundError here (e.g. an os.path.exists check) and keep the test?
There was a problem hiding this comment.
Added a new test, test_open_grid_raises_when_file_not_found, with a clearer docstring and purpose, instead of restoring the old test directly.
| and os.path.splitext(grid_filename_or_obj)[1] in Grid._GEOPANDAS_FILE_EXTENSIONS | ||
| ): | ||
| # Special case for geopandas files (reject other kwargs, then use geopandas backend) | ||
| _got_kw = list(kwargs.keys()) + ([] if chunks is None else ["chunks"]) |
There was a problem hiding this comment.
(non-blocking) merge_coincident_nodes is popped above, so the kwarg check never sees it. It is also never applied on this path: open_grid(shp, merge_coincident_nodes=False) succeeds silently, and the default True doesn't merge either. sample_chicago_buildings.geojson gives 181 nodes but only 178 unique (lon, lat) pairs. At least the docstring for merge_coincident_nodes should say it doesn't apply to geopandas files.
|
|
||
| elif ( | ||
| isinstance(grid_filename_or_obj, (str, os.PathLike)) | ||
| and os.path.splitext(grid_filename_or_obj)[1] in Grid._GEOPANDAS_FILE_EXTENSIONS |
There was a problem hiding this comment.
(nit) The extension match is case-sensitive: CB_2018.SHP still goes to xarray and fails with the original #1754 error. os.path.splitext(...)[1].lower() would handle that.
and FileNotFoundError for missing files.
|
@erogluorhan Thank you for the review. I believe I was able to address all your comments via the latest changes. Please let me know if you notice anything I missed! |
Closes #1754
Overview
Adds support for geopandas files in
open_gridby dispatching toGrid.from_fileif appropriate based on file extension. (There are no other types of files currently supported byGrid.from_filebut notopen_grid.)Large expansion of scope:
_read_multipolygonwhich kept swapping node_lon and node_lat lists while iteratively reading through the polygons in the underlying file. Added corresponding regression tests intest_geopandas.pywhich now pass but failed before this fix.Here's a quick plot of

ux.UxDataArray(xr.DataArray(np.arange(grid.n_face), dims="n_face"), uxgrid=grid)usinggrid = ux.Grid.from_file(os.path.join(..., "cb_2018_us_nation_20m.shp"))before and after the fix:Minor expansion of scope:
test_read_shpfileintest/grid/grid/test_initialization, which simply pointed at a nonexistent file (grid_fire.shp).PR Checklist
General
Testing & Benchmarking
Documentation and Examples
docs/api.rst; internal (private) function names start with an underscore (_)AI Disclosure
AI Usage: Claude for planning, some suggestions about where to look, and generating the
test_multipolygon_preserves_lon_lattest. Some GitHub Copilot's inline code suggestions while implementing the fixes and tests.