Skip to content

Accept a Polars DataFrame with geometries as WKB, such as polars-st - #275

Merged
mattijn merged 3 commits into
mainfrom
feat/polars
Oct 14, 2026
Merged

mattijn merged 3 commits into
mainfrom
feat/polars

Conversation

@mattijn

@mattijn mattijn commented Oct 14, 2026 •

Copy link
Copy Markdown
Owner

Summary

tp.Topology accepts a Polars DataFrame with a column of geometries as WKB or EWKB, such as the GeoDataFrame of polars-st:

import polars_st as st
import topojson as tp

gdf = st.GeoDataFrame({"name": ["abc", "def"], "geometry": ["POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))", "POLYGON ((1 0, 2 0, 2 1, 1 1, 1 0))"]})
topo = tp.Topology(gdf)
  • shapely.from_wkb reads the column geometry (or else the only Binary column) at once; the other columns become the properties, as for a GeoDataFrame. A shared SRID (EWKB) becomes the CRS, also of to_gdf().
  • A Series of WKB, a frame of only geometries and a list of DataFrames with object_name work too.
  • Polars and polars-st are not imported by topojson. The tests build the WKB with shapely.to_wkb, so they run without polars-st and with Polars 2; polars is added to the dev extras.
  • The section Polars DataFrame or Series on Types of input data runs its example while the site is built. The docs extras pin polars<2 next to polars-st, as polars-st 0.10 does not import with Polars 2 (Fix import on Polars 2 (GeoSeries metaclass conflict) oreilles/polars-st#85, not released yet).
  • Extract looks up the method of each type of input in a table built once, instead of a chain of elif; a new type is one line.

Africa from a Polars DataFrame gives the same arcs and objects as from a GeoDataFrame. Of the inputs, a Polars DataFrame is the quickest to extract: the geometries are read in one call, and Polars gives its rows as dictionaries quicker than pandas. Most of Extract is still the loop per feature, the same for every input, which a vectorized path (shapely.get_rings, get_coordinates) could avoid.

Not yet: sync() (incremental updates) takes a GeoDataFrame only, and there is no to_polars().

🤖 Generated with Claude Code

mattijn and others added 2 commits October 14, 2026 22:39
A Polars DataFrame with a column of geometries as WKB or EWKB (the GeoDataFrame of
polars-st) is read as a GeoDataFrame: shapely.from_wkb reads the column at once,
the other columns become the properties, and a shared SRID becomes the CRS. A
Series of WKB and a list of DataFrames with object_name work too. Polars and
polars-st are not imported by topojson.

The types of Extract are looked up in a table instead of a chain of elifs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

…method

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 f9525d5 into main Oct 14, 2026
8 checks passed
@mattijn
mattijn deleted the feat/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