Mix.install(
  [
    {:geoframe, path: Path.join(__DIR__, "..")},
    {:kino, "~> 0.19"},
    {:maplibre, "~> 0.1.9"},
    {:kino_maplibre, "~> 0.1.13"}
  ]
)

Introduction

GeoFrame is a GeoPandas-style geospatial dataframe for Elixir. Geometry lives in Rust as a GeoArrow column, attributes live in an Explorer.DataFrame, and the two stay aligned through every row operation.

This notebook walks through the Phase 0 spike: read a GeoParquet file, inspect it, compute planar measures, filter rows, draw it, and write it back.

The map tab uses MapLibre's demo basemap, so your browser needs internet access.

Read a GeoParquet file

The fixture holds four polygons and one point near Miami, in EPSG:4326, with a missing population value to show how nulls travel.

path = Path.join([__DIR__, "..", "test", "fixtures", "neighborhoods.parquet"]) |> Path.expand()
gdf = GeoFrame.read_parquet!(path)

The cell above ends in a GeoFrame.GeoDataFrame, which Livebook renders with Map, Table and Inspect tabs.

Pieces are available separately:

{GeoFrame.GeoDataFrame.n_rows(gdf), GeoFrame.GeoDataFrame.names(gdf),
 GeoFrame.GeoDataFrame.crs(gdf)}
GeoFrame.GeoDataFrame.data(gdf)

Measures

Measures are planar, in the units of the CRS (here square degrees, which is fine for a demo but not a real area). Each returns an Explorer.Series; the point has zero area and length.

area = GeoFrame.GeoDataFrame.area(gdf)
perimeter = GeoFrame.GeoDataFrame.length(gdf)

{Explorer.Series.to_list(area), Explorer.Series.to_list(perimeter)}
GeoFrame.GeoDataFrame.bounds(gdf)
GeoFrame.GeoSeries.total_bounds(GeoFrame.GeoDataFrame.geometry(gdf))

Centroids replace the geometry and keep the attributes aligned:

GeoFrame.GeoDataFrame.centroid(gdf)

Reproject and measure in metres

Square degrees are not an area. to_crs/2 reprojects (pure Rust, no PROJ install), and estimate_utm_crs/1 picks the UTM zone that fits the data. Axis order is always x = longitude/easting, y = latitude/northing.

utm = GeoFrame.GeoDataFrame.estimate_utm_crs(gdf)
metric = GeoFrame.GeoDataFrame.to_crs(gdf, utm)

{utm, GeoFrame.GeoDataFrame.crs(metric)}

Areas are now square metres, and lengths metres:

{Explorer.Series.to_list(GeoFrame.GeoDataFrame.area(metric)),
 Explorer.Series.to_list(GeoFrame.GeoDataFrame.length(metric))}

Without leaving degrees, area_geodesic/1 measures on the WGS 84 ellipsoid directly, and agrees with the UTM area to within a fraction of a percent (length_geodesic/1 and distance(a, b, method: :geodesic) do the same for lengths and distances):

geodesic = GeoFrame.GeoDataFrame.geometry(gdf) |> GeoFrame.GeoSeries.area_geodesic()
utm_area = GeoFrame.GeoDataFrame.area(metric)

{Explorer.Series.to_list(geodesic), Explorer.Series.to_list(utm_area)}

An integer EPSG code works too (here Web Mercator), and a series without a CRS raises, as in GeoPandas, so call set_crs/2 first:

gdf |> GeoFrame.GeoDataFrame.to_crs(3857) |> GeoFrame.GeoDataFrame.crs()

Filtering

filter_with/2 takes the attribute columns, picks the rows, and applies the same row positions to the geometry.

residential =
  GeoFrame.GeoDataFrame.filter_with(gdf, fn df ->
    Explorer.Series.equal(df["kind"], "residential")
  end)

Add the area as an attribute, then sort by it:

gdf
|> GeoFrame.GeoDataFrame.mutate_with(fn _df ->
  [area: GeoFrame.GeoDataFrame.area(gdf)]
end)
|> GeoFrame.GeoDataFrame.sort_with(&[desc: &1["area"]])
|> GeoFrame.GeoDataFrame.data()

Maps

Rendering uses GeoFrame.Kino.map/2 under the hood. Call it directly to get the map alone or to pass options such as max_features: (inputs above the cap, 50,000 by default, show a notice instead of a map):

GeoFrame.Kino.map(gdf)

A series draws on its own too:

GeoFrame.Kino.map(GeoFrame.GeoDataFrame.geometry(residential))

Export to GeoJSON for any other tool:

GeoFrame.GeoDataFrame.to_geojson(residential) |> Map.fetch!("features") |> hd()

Z coordinates

Geometries can carry a Z ordinate (and M). from_xy/3 takes z:, and get_coordinates/2 returns it with include_z: true. The map draws the 2D projection; Inspect shows the Z.

peaks = GeoFrame.GeoSeries.from_xy([-80.19, -80.2], [25.77, 25.78], z: [12.5, 30.0], crs: "EPSG:4326")

{GeoFrame.GeoSeries.to_wkt(peaks), GeoFrame.GeoSeries.get_coordinates(peaks, include_z: true)}
peaks

Write and read back

out = Path.join(System.tmp_dir!(), "geoframe_spike_#{System.unique_integer([:positive])}.parquet")
:ok = GeoFrame.write_parquet(residential, out)
back = GeoFrame.read_parquet!(out)

File.rm!(out)

{GeoFrame.GeoDataFrame.n_rows(back), GeoFrame.GeoDataFrame.crs(back)}