Skip to content

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Oct 8, 2025

Problem

Leaflet fails to render simple features (sf objects) with Z (elevation) or M (measure) dimensions, throwing obscure errors instead. This affects LINESTRING, POINT, POLYGON, and other geometry types when they contain 3D or 4D coordinate data.

library(leaflet)
library(sf)

# This fails with an obscure error
s1 <- st_linestring(matrix(c(0, 1, 0, 1, 5, 6), ncol = 3))  # Z dimension
leaflet() %>% addPolylines(data = s1)  # Error!

Root Cause

The internal sf_coords() function expects exactly 2D coordinates (lng, lat) but sf objects with:

  • Z dimension have 3 coordinates (X, Y, Z)
  • ZM dimensions have 4 coordinates (X, Y, Z, M)

When sf_coords() tries to assign 2 names to 3+ columns, it fails with an error.

Solution

Following @jcheng5's suggestion, this PR adds sf::st_zm() calls to remove Z and M dimensions at all entry points where sf objects are processed. The st_zm() function:

  • Safely removes Z and M dimensions from geometries
  • Returns 2D geometries (XY only) suitable for mapping
  • Is safe to call on objects that already don't have ZM dimensions

Changes

Added sf::st_zm() calls to 5 methods in R/normalize-sf.R:

  1. sanitize_sf() - handles sf data frames
  2. pointData.sfc_POINT() - handles sfc_POINT collections
  3. pointData.POINT() - handles individual POINT objects
  4. polygonData.sfc() - handles sfc collections (LINESTRING, POLYGON, etc.)
  5. polygonData.sfg() - handles individual geometry objects

This ensures ZM dimensions are removed regardless of how sf objects enter the leaflet system.

Testing

Added comprehensive test coverage for:

  • LINESTRING with Z dimension
  • LINESTRING with ZM dimensions
  • POINT with Z dimension
  • sf data frames with Z dimension
  • sfc collections with Z dimension

All tests verify that geometries with Z/M dimensions are correctly converted to 2D coordinates and render without errors.

Example

After this fix, the following code works correctly:

library(leaflet)
library(sf)

# Create a linestring with Z dimension
s1 <- st_linestring(matrix(c(0, 1, 0, 1, 5, 6), ncol = 3))

# Now renders successfully!
leaflet() %>% addPolylines(data = s1)

Fixes issue where sf objects with Z or ZM dimensions fail to map.

Original prompt

This section details on the original issue you should resolve

<issue_title>Simple features with ZM dimensions fail to map</issue_title>
<issue_description>Leaflet will not render linestrings with Z or ZM dimensions. Instead it gives an obscure error with an obscure traceback. It would be helpful if leaflet removed or ignored the ZM dimensions or at least gave a helpful error message.

library(leaflet)
library(magrittr)
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.1.3, proj.4 4.9.3

s1 <- rbind(c(0,3,1),c(0,4,1),c(1,5,1),c(2,5,1))
(ls <- st_linestring(s1))
#> LINESTRING Z (0 3 1, 0 4 1, 1 5 1, 2 5 1)

leaflet(ls) %>% addPolylines()
#> Error in if (length(nms) != n || any(nms == "")) stop("'options' must be a fully named list, or have no names (NULL)"): missing value where TRUE/FALSE needed

# Works
leaflet(st_zm(ls)) %>% addPolylines()
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
<comment_new><author>@jcheng5</author><body>
Looks like there's no harm in calling `st_zm()` on a linestring that already doesn't have ZM, should we just do that always? @bhaskarvk @timelyportfolio </body></comment_new>
</comments>
Fixes #482

💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: jcheng5 <129551+jcheng5@users.noreply.github.com>
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copilot AI and others added 2 commits October 8, 2025 20:49
Co-authored-by: jcheng5 <129551+jcheng5@users.noreply.github.com>
Co-authored-by: jcheng5 <129551+jcheng5@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Fix rendering issue for linestrings with ZM dimensions Fix: Handle sf objects with Z/M dimensions in leaflet rendering Oct 8, 2025
Copilot finished work on behalf of jcheng5 October 8, 2025 20:54
@Copilot Copilot AI requested a review from jcheng5 October 8, 2025 20:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Simple features with ZM dimensions fail to map

3 participants