Skip to content

Add Topology.to_polars(), with the geometries as WKB as polars-st stores them - #276

Merged
mattijn merged 2 commits into
mainfrom
feat/to-polars
Oct 14, 2026
Merged

mattijn merged 2 commits into
mainfrom
feat/to-polars

Conversation

@mattijn

@mattijn mattijn commented Oct 14, 2026 •

Copy link
Copy Markdown
Owner

Summary

Follows #275, the other way: Topology.to_polars() gives a Polars DataFrame, the id and the properties as columns and the geometry as WKB in the column geometry, as polars-st stores it.

import polars_st as st

gdf = st.GeoDataFrame(topo.to_polars())
  • With a CRS that has an EPSG code, of the input (such as the SRID of polars-st, or the CRS of a GeoDataFrame) or given with crs=, the WKB is EWKB with this code as SRID, so polars-st knows the CRS. Without one it is plain WKB.
  • The options are those of to_gdf(): crs, validate, winding_order and object_name; the geometries are the same as those of to_gdf().
  • The ids that go in come out: the column id holds the ids of the features, from the index of a GeoDataFrame, the ids of GeoJSON features, or a column id of a Polars DataFrame. That last one is new on the input side: a column id of a Polars DataFrame now gives the ids of the features, as the index of a GeoDataFrame, instead of being a property (so to_json and to_gdf keep them too). Duplicate ids raise as in a FeatureCollection; with ignore_index=True the features are numbered.
  • A feature whose geometry collapsed on the grid (Drop rings that collapse on the grid and keep the feature #257) has null as geometry; a property missing on some features is null there.
  • Neither Polars nor polars-st is imported by topojson until to_polars() is called; the tests build and read the WKB with shapely, so they run without polars-st.
  • A section .to_polars() on Retrieval data types, whose example runs while the site is built.

🤖 Generated with Claude Code

…res them

to_polars() gives a Polars DataFrame with the id and the properties as columns and
the geometry as WKB in the column geometry; with a CRS that has an EPSG code (of
the input, or given) it is EWKB with this code as SRID, so polars-st knows the
CRS. It takes the options of to_gdf(); a feature without geometry is null.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

Benchmark result

benchmark
These PR changes are benchmarked against five files (see tests/benchmark_compute.py for what each one tests), compared with PyPi's latest release and the repository's main branch.

A column id of a Polars DataFrame gives the ids of the features, as the index of a
GeoDataFrame does, instead of being a property, so that the ids that go in come out
of to_json, to_gdf and to_polars. Duplicate ids raise as in a FeatureCollection;
with ignore_index the features are numbered.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

Benchmark result

benchmark
These PR changes are benchmarked against five files (see tests/benchmark_compute.py for what each one tests), compared with PyPi's latest release and the repository's main branch.

@mattijn
mattijn merged commit d93bfa4 into main Oct 14, 2026
8 checks passed
@mattijn mattijn mentioned this pull request Oct 14, 2026
@mattijn
mattijn deleted the feat/to-polars branch October 14, 2026 21:24
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 this pull request may close these issues.

1 participant