GeoFrame
GeoPandas-style geospatial dataframes for Elixir, powered by GeoArrow and Rust.
Status: latest release
0.3.3, published on Hex; see the changelog. Behaviour is checked against GeoPandas 1.2 / Shapely 2.2 on 19 118 reference cases, but the API may still change before 1.0.
Elixir has Explorer for dataframes, but no way to carry geometry with spatial semantics. GeoFrame adds it without forking Explorer:
- Attributes live in an
Explorer.DataFrame. - Geometry lives in Rust as a GeoArrow column behind a Rustler resource, so chained spatial operations never round-trip through Elixir terms.
- Algorithms come from the GeoRust
geocrate,proj4rsand pure-Rust ports of the GEOS algorithms GeoPandas relies on. No GDAL, GEOS or PROJ install. - Livebook integration through Kino.
%GeoFrame.GeoDataFrame{
data: Explorer.DataFrame, # attribute columns (Polars)
geometry: GeoFrame.GeoSeries, # active GeoArrow column held in Rust
secondary_geometries: [...], # more geometry columns, each with its own CRS
geometry_column: "geometry"
}
Row-changing operations (filter, sort, slice, join) compute row indices once and apply take
to both the attributes and the geometry, so they never drift apart.
Install
def deps do
[
{:geoframe, "~> 0.3.3"},
# optional
{:kino, "~> 0.19"}, # Livebook rendering, deck.gl widget, Smart Cells
{:kino_maplibre, "~> 0.1"}, # the Map tab (the deck.gl widget needs nothing extra)
{:postgrex, "~> 0.22"}, # PostGIS
{:req, "~> 0.5"} # only for the notebooks (they download sample data)
]
end
The Hex package downloads a precompiled NIF (no Rust toolchain needed) for the nine targets listed under Precompiled NIFs below, verified against the checksum file in the package.
To build from source instead, depend on the v0.3.3 git tag (or use a path: dependency on a
checkout):
{:geoframe, github: "CountlinkX-Solutions/geoframe", tag: "v0.3.3"}
A git or path dependency compiles the NIF on your machine with Rustler (a dependency of GeoFrame; you do not add it), so you need a stable Rust toolchain.
Supported versions
- Elixir 1.17 or later. CI tests Elixir 1.17 / OTP 27 and Elixir 1.19 / OTP 28;
development uses Elixir 1.19 / OTP 28 (
.tool-versions). - Rust (stable) only when the NIF is built from source: a git or path dependency, or
GEOFRAME_BUILD=true(for example on a platform without a precompiled build). The Hex package's precompiled NIF needs no Rust toolchain.
Precompiled NIFs (from 0.2.0)
From 0.2.0, each release attaches precompiled NIF libraries, and from 0.3.3 GeoFrame is on
Hex, so installing it from there needs no Rust toolchain. Each release attaches builds for
aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-musl,
x86_64-apple-darwin, x86_64-pc-windows-gnu, x86_64-pc-windows-msvc,
x86_64-unknown-freebsd, x86_64-unknown-linux-gnu and x86_64-unknown-linux-musl (NIF
version 2.15), through rustler_precompiled. The
download is verified against the checksum file shipped in the Hex package.
- Hex packages (from 0.3.3) carry the checksum file and download the precompiled NIF.
- Git and path dependencies build the NIF from source automatically (needs a Rust
toolchain; Rustler is fetched as a dependency of GeoFrame). The precompiled download is
verified against a checksum file that only the Hex package ships (it is generated from the
release assets, after the tag exists), so a package without it, such as
{:geoframe, github: "CountlinkX-Solutions/geoframe", tag: "v0.3.3"}, is compiled instead. No environment variable is needed. GEOFRAME_BUILD=trueforces a source build even when a checksum file is present, for example if your platform has no precompiled build. Set it for bothmix deps.getandmix deps.compile, or add{:rustler, "~> 0.38"}to your own deps. GeoFrame always builds from source in:devand:testinside its own checkout.
Quickstart
alias Explorer.DataFrame
alias GeoFrame.{GeoDataFrame, GeoSeries}
zones =
GeoDataFrame.new(
DataFrame.new(zone: ["downtown", "beach"]),
GeoSeries.from_wkt!(
[
"POLYGON ((-80.21 25.75, -80.18 25.75, -80.18 25.78, -80.21 25.78, -80.21 25.75))",
"POLYGON ((-80.14 25.76, -80.12 25.76, -80.12 25.80, -80.14 25.80, -80.14 25.76))"
],
crs: "EPSG:4326"
)
)
places =
GeoDataFrame.new(
DataFrame.new(name: ["cafe", "pier", "park"]),
GeoSeries.from_xy([-80.19, -80.13, -80.0], [25.76, 25.79, 25.9], crs: "EPSG:4326")
)
# Which zone is each place in? A left join keeps "park", which is in none.
joined = GeoFrame.sjoin(places, zones, how: :left, predicate: :within)
# Reproject to UTM 17N and measure in square metres.
zones |> GeoDataFrame.to_crs("EPSG:32617") |> GeoDataFrame.area() |> Explorer.Series.to_list()
# Write and read any supported format.
:ok = GeoFrame.write_file(zones, "zones.gpkg")
zones = GeoFrame.read_file!("zones.gpkg")
What it does
Every operation GeoFrame shares with GeoPandas is tested against it; the member-by-member matrix and the known deviations are in docs/parity.md.
Reading and writing
| Format | Read | Write | Extensions |
|---|---|---|---|
| GeoParquet 1.0/1.1 | read_parquet/2 (bbox: filter) |
write_parquet/3 (compression, row groups, native GeoArrow, covering bbox) |
.parquet, .geoparquet |
| GeoJSON (RFC 7946) | read_geojson/2 |
write_geojson/3 |
.geojson, .json |
| FlatGeobuf | read_flatgeobuf/2 (bbox: via the index) |
write_flatgeobuf/3 |
.fgb |
| Feather v2 (Arrow IPC file) | read_feather/2 |
write_feather/3 (record_batch_size:) |
.feather, .arrow |
Shapefile (.cpg, .prj, zipped) |
read_shapefile/2 |
write_shapefile/3 |
.shp, .zip |
| GeoPackage (several layers, rtree) | read_gpkg/2 (layer:) |
write_gpkg/3 (layer:, mode:) |
.gpkg |
PostGIS (optional :postgrex) |
read_postgis/3 |
to_postgis/4 |
database |
| Arrow IPC stream (binary) | from_arrow/2 |
to_arrow/2 (on GeoSeries and GeoDataFrame) |
in memory |
read_file/2andwrite_file/3pick the format from the extension or adriver:option;list_layers/2andread_file_info/2describe a file. Files written by GeoPandas 1.2 / pyogrio read directly, and GeoPandas reads what GeoFrame writes.- Pure Rust, except GeoPackage, which is SQLite through
rusqlitewith the bundled SQLite amalgamation (the only C code GeoFrame compiles). - Several geometry columns: a frame has one active geometry and any number of secondary
GeoSeriescolumns (put_geometry/3,set_geometry/3,geometry_columns/1). GeoParquet, Feather and Arrow keep all of them; the single-geometry formats raise, as GeoPandasto_filedoes. - Z and M: geometries can be XY, XYZ, XYM or XYZM (
has_z/1,z/1,force_2d/1,from_xy(z:),get_coordinates(include_z:)). Every format keeps them (GeoJSON keeps Z only, as RFC 7946 has no M). See Z and M.
Coordinate reference systems
set_crs/2,to_crs/2andestimate_utm_crs/2work with any EPSG geographic, geocentric, projected or compound code, and with PROJJSON or WKT (WKT1, WKT2) definitions that have no code, aspyproj.CRS.from_user_inputdoes.to_crsagrees with pyproj to 1e-6 projected units on the reference cases.- Datum shifts follow PROJ without a network: a grid if one is installed (
GeoFrame.Grids, NTv2.gsband GeoTIFF.tif; nothing is downloaded), else the Helmert operation PROJ picks by area of use. See Datum shifts and grids and CRS definitions and codes outside the table.
Geometry operations
On GeoSeries; the common ones are also on GeoDataFrame and GeoFrame.
- Measures:
area,length,distance(planar, geodesic, haversine),hausdorff_distance,frechet_distance,bounds,total_bounds,minimum_clearance,project. - Predicates:
intersects,contains,within,touches,dwithin,relate,is_valid,is_simple, ... and the coverage validatorsis_valid_coverage,invalid_coverage_edges. - Constructive:
buffer,offset_curve,simplify,simplify_coverage,convex_hull,concave_hull, bounding shapes,maximum_inscribed_circle,delaunay_triangles,voronoi_polygons,polygonize,snap,segmentize,set_precision,make_valid. - Set operations:
union,intersection,difference,symmetric_difference,union_all,clip,overlay(all fivehows),explode,dissolve. - Grouping:
group_by/summarise(attribute aggregations and geometry aggregations per group:union_all,intersection_all,collect,envelope,convex_hull,centroid,first,last),group_map(a function per group, like pandasgroupby.apply), and groupedhead,tail,sliceandsample. - Joins:
sjoinandsjoin_nearest(R-tree, every predicate,dwithin),sindex_query,sindex_nearest. - Transforms:
translate,rotate,scale,skew,affine_transform,wrap_longitude. - Grids:
GeoSeries.make_grid/3(square and hexagon cells). - Explorer verbs on a
GeoDataFrame:select,discard,rename,put,pull,filter_with,sort_with,mutate_with,head,tail,slice,sample,distinct,drop_nil,join,concat_rows; the geometry follows the rows.
Livebook
With Kino installed (see GeoFrame.Kino):
- A
GeoDataFramerenders with Map, Table and Inspect tabs (aGeoSerieswith Map and Inspect). The map needskino_maplibreand draws up to 50,000 features and 500,000 vertices; the Table tab shows at most 100,000 rows and 250,000 vertices, with a notice when rows are left out. Every cap is an option or aconfig :geoframekey. GeoFrame.Kino.deck/2sends binary buffers to deck.gl (no GeoJSON), so a million features stay interactive, with hover tooltips and colour or size by column.- Two Smart Cells are registered when Kino is present: Read spatial file and Spatial join, both under Smart in a Livebook section.
PostGIS
{:ok, conn} =
Postgrex.start_link(hostname: "localhost", database: "gis", types: GeoFrame.Postgrex.Types)
{:ok, gdf} = GeoFrame.read_postgis(conn, "SELECT id, geom FROM places WHERE id > $1", params: [10])
:ok = GeoFrame.to_postgis(gdf, conn, "places_copy", if_exists: :replace, spatial_index: true)
geometry columns decode to EWKB (parsed in Rust; the SRID becomes the CRS) and numeric
columns keep every digit. Without the types module, select ST_AsEWKB(geom) AS geom. See
GeoFrame.PostGIS.
Notebooks
Open them in Livebook. They download Natural Earth data on first run, and the maps need internet access in the browser for the basemap.
| Notebook | What it shows |
|---|---|
notebooks/tour.livemd |
A tour on one story (Europe): every file format, reprojection, buffers, spatial joins, overlay, dissolve, simplification, distances and the coverage tools. |
notebooks/deck.livemd |
The deck.gl widget on real data: countries, cities, rivers, colour and size by column, the Smart Cells, one million points. |
notebooks/compare.livemd |
GeoFrame and GeoPandas side by side: ten analyses on real data (buffer and dissolve, overlay, joins, nearest, clipping, geodesic distances, Voronoi), each in GeoFrame and in an executable Python cell (Pythonx), with a PASS/FAIL table per metric. scripts/compare_geopandas.sh repeats the comparison from the command line. |
notebooks/spike.livemd |
The short first walkthrough: read GeoParquet, measures, reprojection, filtering, maps, Z coordinates, write and read back. |
Livebook has no headless runner, so elixir scripts/run_livemd.exs notebooks/tour.livemd
extracts a notebook's code cells and runs them in order. It also runs the Python cells through Pythonx. CI does this for all four.
Limits and known deviations
- Where GeoPandas returns
False,0,NaNor an empty geometry for a null or empty input, GeoFrame returnsnil; a few operations on degenerate or invalid input differ from GEOS. Every difference is listed, and tested, in docs/parity.md. - Inputs that would abort the VM (a stack overflow, a huge allocation) are rejected with an error; the caps (WKT depth, grid cells, buffer vertices, ...) are in Resource limits.
read_file/2reads a file into memory whole.GeoFrame.stream_file/2reads GeoParquet, FlatGeobuf, Arrow IPC / Feather, Shapefile (.shp,.zip), GeoPackage and GeoJSON lazily as a stream ofGeoDataFramebatches (batch_size:, and where the format supports itcolumns:,bbox:,layer:,encoding:), so a file larger than memory can be processed (GeoJSON is read twice, once to infer the column types, so every batch has the dtypes ofread_file/2);read_file(path, bbox: {minx, miny, maxx, maxy})filters every format but Feather as GeoPandas does (the GDAL formats keep the features whose geometry intersects the box);read_file(path, rows: n | first..last)reads only those rows through the same readers (GeoParquet skips row groups), so the first rows of a huge file are cheap.- Curved geometries (
CircularString,CompoundCurve,CurvePolygon,MultiCurve,MultiSurface) are read linearized exactly as GDAL does (4 degree steps,OGR_ARC_STEPSIZEhonoured) from GeoPackage, PostGIS, WKB and GeoParquet / Feather WKB; WKT curves are not parsed, and GeoFrame never writes curves (Curved geometries). - Not planned:
plot(use the Kino map), geocoding, pandas index alignment, a persistent spatial index object, and OGR drivers that need GDAL (GML, KML, GPX, FileGDB, ...; convert withogr2ogrfirst).
Development
Requires Elixir 1.19 / OTP 28 (see .tool-versions) and a Rust toolchain. A checkout always
builds the NIF from source in :dev and :test.
mix deps.get
mix test # Elixir tests, doctests and the GeoPandas reference suite
cargo test --workspace # Rust tests
mix format --check-formatted && mix credo --strict && mix compile --warnings-as-errors
mix docs # ExDoc; CI fails on warnings
-
Reference suite:
test/reference/generate.py(dev-only, needs GeoPandas 1.2) writes the fixtures intest/reference/fixtures/, andtest/geo_frame/reference_test.exscompares GeoFrame with them in CI without Python.REFERENCE_REPORT=1 mix test test/geo_frame/reference_test.exsprints the cases per operation; known differences live intest/reference/known_deviations.exs. See test/reference/README.md. -
I/O cross-check:
scripts/crosscheck_io.shwrites files with GeoFrame and reads them back in GeoPandas;scripts/crosscheck_postgis.shdoes the same for PostGIS. -
PostGIS tests are excluded unless a database is given:
docker run -d --name geoframe-postgis -e POSTGRES_PASSWORD=geoframe -p 55432:5432 postgis/postgis:17-3.5 GEOFRAME_POSTGIS_URL=postgres://postgres:geoframe@127.0.0.1:55432/postgres mix test --only postgis -
Benchmarks:
mix run scripts/bench_sjoin.exstimes large spatial joins. -
Docs links:
mix docs --warnings-as-errorsrejects a relative README link to a file that is not an ExDoc extra (theextrasinmix.exs), and a relative link whose file name matches an extra (such astest/reference/README.md) silently points to that extra, so link such files with an absolute GitHub URL, as the Contributing section does. -
Releasing precompiled NIFs: pushing a
v*tag runs.github/workflows/release.yml, which buildslibgeoframe_nativefor every target listed inlib/geo_frame/native.exand attaches the archives to the GitHub release of that tag (creating it if needed; the tag must equalvplus themix.exsversion).workflow_dispatchruns the same builds without publishing. Once the release has all assets, generate the checksum file that goes into the package, then publish the package and its docs:mix rustler_precompiled.download GeoFrame.Native --all --print MIX_ENV=prod mix hex.publish package mix hex.publish docsThe first command writes
checksum-Elixir.GeoFrame.Native.exs(not committed to git;mix hex.buildpicks it up throughchecksum-*.exs). The package is published from:prodbecausebuild_from_source?/0inmix.exsis true in:devand:test, where the package would listrustleras a required dependency (in:prod, with the checksum file present, it is optional), and the docs from:devbecause ExDoc is a:dev/:testdependency. Keep the target list inrelease.ymlandlib/geo_frame/native.exin sync.
Contributing
Contributions are welcome; see CONTRIBUTING.md. Everyone taking part is expected to follow the Code of Conduct. Report security vulnerabilities privately as described in SECURITY.md, not in public issues.
License
Apache-2.0. See LICENSE.
The EPSG definitions and datum operations GeoFrame embeds come from the EPSG Geodetic Parameter
Dataset (IOGP), through the crs-definitions crate and tables generated from the PROJ database;
see NOTICE for the attribution and the EPSG terms of use.