Read and write GDAL-compatible geospatial data into Polars and GeoPolars.
Supports reading and writing the following geospatial formats into / from a Polars Dataframe:
- GeoJSON
- ShapeFiles
- CSV with lat / lon
- FlatGeobuf
- KML
- GPX
- PostGIS (via network)
- SpatialLite
- ... and many more
use polars_gdal::df_from_resource;
let df = df_from_resource("my_shapefile.shp", None).unwrap();
println!("{}", df);
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);
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);
let mut params = polars_gdal::Params::default();
let csv_parsing_options = ["EMPTY_STRING_AS_NULL=YES", "KEEP_GEOM_COLUMNS=NO", "X_POSSIBLE_NAMES=Lon*", "Y_POSSIBLE_NAMES=Lat*"];
params.open_options = Some(&csv_parsing_options);
let df = df_from_resource("lat_lon_countries.csv", Some(params)).unwrap();
println!("{}", df);
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);
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)?;
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)?;