GeoFrame

CI Hex version HexDocs

GeoPandas-style geospatial dataframes for Elixir, powered by GeoArrow and Rust.

Status: latest release 0.3.4, 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:

%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.4"},
    # 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.4 git tag (or use a path: dependency on a checkout):

{:geoframe, github: "CountlinkX-Solutions/geoframe", tag: "v0.3.4"}

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

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.

Third-party licenses. The NIF library statically links third-party Rust crates, each under its own license (MIT, Apache-2.0, BSD and other permissive licenses). Releases after 0.3.3 attach THIRD_PARTY_LICENSES.html next to the precompiled archives on the GitHub releases page, listing those crates with their license texts. It is generated with cargo-about from about.toml, so a source build can produce the same list.

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

Coordinate reference systems

Geometry operations

On GeoSeries; the common ones are also on GeoDataFrame and GeoFrame.

Livebook

With Kino installed (see GeoFrame.Kino):

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

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

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.