Commit 86d129d
Add first-class Polars DataFrame support (#1283)
* Add first-class Polars DataFrame support
Fixes #1276
This commit adds native support for Polars-based geospatial workflows in leafmap,
addressing the feature request for first-class Polars-ST and polars-h3 integration.
## Changes:
### New Method: add_polars()
- Accepts Polars DataFrames with geometry columns (WKB or WKT format)
- Supports Polars-ST spatial DataFrames
- Automatic CRS detection with fallback to EPSG:4326
- Full compatibility with existing leafmap styling and options
- Internally converts to GeoPandas for rendering
### Features:
- Works with WKB (Well-Known Binary) geometries from Polars-ST
- Works with WKT (Well-Known Text) geometry strings
- Supports all geometry types (Point, LineString, Polygon, etc.)
- Preserves non-geometry columns as attributes
- Compatible with polars-h3 H3-indexed workflows
- Comprehensive error handling and validation
### Documentation:
- Added example notebook: examples/notebooks/polars_support.ipynb
- Includes examples for:
- Simple WKT point data
- Converting GeoDataFrame to Polars
- Polars-first data processing pipelines
- Filtering and visualization
## Benefits:
1. Users can stay in Polars-native workflows end-to-end
2. No manual conversion code required
3. Better performance for large datasets
4. Seamless integration with Polars-ST and polars-h3
5. Natural visualization frontend for Arrow-native geospatial stacks
## Implementation:
The add_polars() method:
1. Validates input is a Polars DataFrame
2. Checks geometry column exists
3. Converts Polars to pandas
4. Parses WKB/WKT geometries using shapely
5. Creates GeoPandas GeoDataFrame
6. Calls existing add_gdf() method
This approach ensures compatibility with all existing leafmap backends
(folium, ipyleaflet, maplibre, etc.) without requiring changes to the
rendering pipeline.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add polars as optional dependency and update documentation
- Added 'polars' optional dependency in pyproject.toml
- Updated installation.md with Polars support section
- Documented pip install "leafmap[polars]" command
- Listed other optional dependencies for reference
- Tested with geo conda environment - all tests passed
* Add add_polars() to folium and maplibre backends
- Added add_polars() method to leafmap/foliumap.py
- Added add_polars() method to leafmap/maplibregl.py
- Both backends now support Polars DataFrame visualization
- Tested with WKT and WKB geometries
- All backends (ipyleaflet, folium, maplibre) working correctly
The method signatures match each backend's existing add_gdf() API:
- foliumap: Simpler API with layer_name, zoom_to_layer, info_mode, opacity
- maplibre: Full API with layer_type, filter, paint, fit_bounds, visible, etc.
All tests passed with geo conda environment.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix installation.md: restore missing bash code fences
Accidentally removed bash code markers when adding Optional Dependencies section.
All bash code blocks now properly formatted with code fences.
* Update notebook example
* Address GitHub Copilot review comments for add_polars()
Fixes based on 10 Copilot comments:
1. Type consistency: Changed Optional[list[str]] to Optional[List[str]]
2. Added missing 'encoding' parameter to match add_gdf() API
3. Improved exception handling: Use specific TypeError/ValueError instead of bare except
4. Replace print() with warnings.warn() for UserWarning
5. Added datetime column handling (same as add_gdf())
6. Fixed notebook imports: Added wkb to top-level imports, reordered Example 3
7. Simplified CRS detection: Removed speculative Polars-ST metadata code
8. Improved performance: Use vectorized gpd.GeoSeries.from_wkb/from_wkt
9. Added null geometry validation before processing
10. Better error messages with specific parse failures
Changes:
- Use warnings.warn() with UserWarning and stacklevel=2
- Vectorized geometry parsing with gpd.GeoSeries.from_wkb() and from_wkt()
- Early validation for null/empty geometry columns
- Datetime handling to prevent GeoJSON serialization issues
- Clear CRS warning message directing users to 'crs' parameter
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>1 parent 67f0da4 commit 86d129d
7 files changed
Lines changed: 754 additions & 0 deletions
File tree
- docs
- notebooks
- leafmap
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
33 | 60 | | |
34 | 61 | | |
35 | 62 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
0 commit comments