Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plot handling of empty sf points objects #436

Closed
davidorme opened this issue Jul 24, 2017 · 3 comments
Closed

Plot handling of empty sf points objects #436

davidorme opened this issue Jul 24, 2017 · 3 comments

Comments

@davidorme
Copy link

Hi,

sf is looking fantastic - really nice to use. I'm running some code that plots different levels of a factor from alternative data sources to compare them. Sometimes, one source or another doesn't have a particular level, so I get an empty subset.

I've found that the points plotting throws an Error for empty dataframes. Both linestring and polygon have transparent handling mechanisms: you can plot an empty sf object provided you give limits and you can plot(xx, add=TRUE) for an empty sf object. But the behaviour for point features is inconsistent and throws an error.

Error in apply(mat, 1, function(x) all(is.finite(x))) : 
  dim(X) must have a positive length

The error is generated by this line in plot.sfc_POINT:

ne = apply(mat, 1, function(x) all(is.finite(x)))

The preceding lines handle the case where length(x) is zero, but this one doesn't. The other plot.sfc_ functions have an if statement that traps empty cases. Some demo code to throw the error.

# construct some simple examples
sfc = st_sfc(st_point(c(3,3)), st_point(c(6,6)))
pts = st_sf(data.frame(a=1:2, geom=sfc))
sfc = st_sfc(st_linestring(matrix(1:10, , 2)),
		    st_linestring(matrix(10:1, , 2)))
lns = st_sf(data.frame(a=1:2, geom=sfc))
sfc = st_sfc(st_polygon(list(matrix(c(1,1,1,2,2,2,2,1,1,1),ncol=2, byrow=TRUE))),
		    st_polygon(list(matrix(c(5,5,5,6,6,6,6,5,5,5),ncol=2, byrow=TRUE))))
ply = st_sf(data.frame(a=1:2, geom=sfc))

# plot those
plot(st_geometry(lns))
plot(st_geometry(pts), add=TRUE)
plot(st_geometry(ply), add=TRUE)

# create some empty sf objects
lns_empty <- subset(lns, a == Inf)
pts_empty <- subset(pts, a == Inf)
ply_empty <- subset(ply, a == Inf)

# plotting empty lines or polygons works provided limits are provided
xl <- yl <- c(0,10)
plot(st_geometry(lns_empty), xlim=xl, ylim=yl)
plot(st_geometry(ply_empty), xlim=xl, ylim=yl)

# and adding empty lines or poylgons to an existing plot does nothing silently
plot(st_geometry(pts), xlim=xl, ylim=yl)
plot(st_geometry(lns_empty), add=TRUE)

plot(st_geometry(pts), xlim=xl, ylim=yl)
plot(st_geometry(ply_empty), add=TRUE)

# But plotting empty pts is impossible, even when limits are provided
plot(st_geometry(pts_empty), xlim=xl, ylim=yl)

# and adding empty points to an existing plot throws an error
plot(st_geometry(lns), xlim=xl, ylim=yl)
plot(st_geometry(pts_empty), add=TRUE)
@edzer
Copy link
Member

edzer commented Jul 24, 2017

Thanks for the clear example, should work now!

@davidorme
Copy link
Author

Thanks very much - that's done it. In retrospect, I should have just dropped in the new plot.R rather than reinstall sf from source but, heh, I needed to upgrade to GDAL2 anyway. I'm not sure brew really needed to build gcc 7.1.0 from scratch...

@charliejhadley
Copy link

@edzer I posted an issue about leaflet not handing empty data frames here rstudio/leaflet#452 but there was a question about whether the issue lives in sf like here.

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

No branches or pull requests

3 participants