#polars #gdal #csv #kml #geojson #geospatial #postgis #networking #gpx


Read and write GDAL-compatible geospatial data into Polars and GeoPolars

1 unstable release

0.1.0 Jan 11, 2023

#46 in Geospatial

MIT license

905 lines

Read and write GDAL-compatible geospatial data into Polars and GeoPolars.

Supports reading and writing the following geospatial formats into / from a Polars Dataframe:

  1. GeoJSON
  2. ShapeFiles
  3. CSV with lat / lon
  4. FlatGeobuf
  5. KML
  6. GPX
  7. PostGIS (via network)
  8. SpatialLite
  9. ... and many more

Example 1: Dataframe from a file

use polars_gdal::df_from_resource;
let df = df_from_resource("my_shapefile.shp", None).unwrap();
println!("{}", df);

Example 2: DataFrame from raw bytes

use polars_gdal::df_from_bytes;

let geojson = r#"{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"name":"foo"},"geometry":{"type":"Point","coordinates":[1,2]}},{"type":"Feature","properties":{"name":"bar"},"geometry":{"type":"Point","coordinates":[3,4]}}]}"#.as_bytes();

let df = df_from_bytes(geojson, None, None).unwrap();
println!("{}", df);

Example 3: DataFrame from GDAL Layer with filtering query

use polars_gdal::{df_from_layer, gdal};
use gdal::vector::sql;

let dataset = gdal::Dataset::open("my_shapefile.shp")?;
let query = "SELECT kind, is_bridge, highway FROM my_shapefile WHERE highway = 'pedestrian'";
let mut result_set = dataset.execute_sql(query, None, sql::Dialect::DEFAULT).unwrap().unwrap();

let df = df_from_layer(&mut result_set, None).unwrap();
println!("{}", df);

Example 4: DataFrame from Latitude / Longitude CSV with custom parsing options

let mut params = polars_gdal::Params::default();
params.open_options = Some(&csv_parsing_options);
let df = df_from_resource("lat_lon_countries.csv", Some(params)).unwrap();
println!("{}", df);

Example 5: DataFrame from a PostGIS table

use polars_gdal::{df_from_resource, Params};

let mut params = Params::default();
params.layer_name = Some("some_table_name");
let df = df_from_resource("postgresql://user:pass@host/db_name", Some(params)).unwrap();
println!("{}", df);

Example 6: GeoJSON bytes from a Dataframe

use polars_gdal::{gdal, gdal_bytes_from_df, WriteParams};

let df: DataFrame = ...;
let json_driver = gdal::DriverManager::get_driver_by_name("GeoJSON")?;
let geojson_bytes = gdal_bytes_from_df(&df, &json_driver, None)?;

Example 7: Write a shapefile to disk from a DataFrame

use polars_gdal::{gdal, gdal_resource_from_df, WriteParams};

let df: DataFrame = ...;
let shapefile_driver = gdal::DriverManager::get_driver_by_name("ESRI Shapefile")?;
let _dataset = gdal_resource_from_df(&df, &shapefile_driver, "/some/path/on/disk/my_shapefile.shp", None)?;


~461K SLoC