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

Deprecate sp / spatial tidiers? #392

Closed
alexpghayes opened this issue Jun 18, 2018 · 6 comments
Closed

Deprecate sp / spatial tidiers? #392

alexpghayes opened this issue Jun 18, 2018 · 6 comments

Comments

@alexpghayes
Copy link
Collaborator

We should probably just point people to sf and coercion methods there instead.

@alexpghayes
Copy link
Collaborator Author

alexpghayes commented Jun 21, 2018

Dave isn't sold, so we'll revisit this after the broom 0.5.0 release. Need someone from the spatial community to comment on whether tidy capabilities have been absorbed into sf since neither of us does much spatial stuff.

@dpseidel
Copy link

So it's worth noting perhaps that broom doesn't currently tidy any spatial objects beyond SpatialPolygons, SpatialLinesDataFrame and SpatialPolygonsDataFrame. Try to pass a SpatialPoints or SpatialPointsDataFrame object to the dev version and it will fail with the warning Error: No tidy method for S3 objects of classSpatialPoints (especially odd since sp provides S4 classes). With broom 0.4.4 it will issue the same warning and tidy as a data.frame. In any case, sp objects coerce easily to sf objects using sf::st_as_sf and sf objects behave like tibbles with pretty printing by default . A wholly different behavior but much nicer than printing each corner of a polygon as broom::tidy does, especially when dealing with any Spatial*DataFrame object.

Example with SpatialPolygons object:

library(sp)

# Build basic 2x2 polygon grid as SpatialPolygons
grd <- GridTopology(c(1,1), c(1,1), c(2,2))
polys <- as(grd, "SpatialPolygons")

class(polys)
#> [1] "SpatialPolygons"
#> attr(,"package")
#> [1] "sp"
pryr::otype(polys) # S4 not S3 
#> [1] "S4"

broom::tidy(polys) # tidy
#>    long lat order  hole piece group id
#> 1   0.5 1.5     1 FALSE     1  g1.1 g1
#> 2   0.5 2.5     2 FALSE     1  g1.1 g1
#> 3   1.5 2.5     3 FALSE     1  g1.1 g1
#> 4   1.5 1.5     4 FALSE     1  g1.1 g1
#> 5   0.5 1.5     5 FALSE     1  g1.1 g1
#> 6   1.5 1.5     1 FALSE     1  g2.1 g2
#> 7   1.5 2.5     2 FALSE     1  g2.1 g2
#> 8   2.5 2.5     3 FALSE     1  g2.1 g2
#> 9   2.5 1.5     4 FALSE     1  g2.1 g2
#> 10  1.5 1.5     5 FALSE     1  g2.1 g2
#> 11  0.5 0.5     1 FALSE     1  g3.1 g3
#> 12  0.5 1.5     2 FALSE     1  g3.1 g3
#> 13  1.5 1.5     3 FALSE     1  g3.1 g3
#> 14  1.5 0.5     4 FALSE     1  g3.1 g3
#> 15  0.5 0.5     5 FALSE     1  g3.1 g3
#> 16  1.5 0.5     1 FALSE     1  g4.1 g4
#> 17  1.5 1.5     2 FALSE     1  g4.1 g4
#> 18  2.5 1.5     3 FALSE     1  g4.1 g4
#> 19  2.5 0.5     4 FALSE     1  g4.1 g4
#> 20  1.5 0.5     5 FALSE     1  g4.1 g4

sf::st_as_sf(polys) # sf conversion... tidy printing inherent
#> Simple feature collection with 4 features and 0 fields
#> geometry type:  POLYGON
#> dimension:      XY
#> bbox:           xmin: 0.5 ymin: 0.5 xmax: 2.5 ymax: 2.5
#> epsg (SRID):    NA
#> proj4string:    NA
#>                         geometry
#> 1 POLYGON ((0.5 1.5, 0.5 2.5,...
#> 2 POLYGON ((1.5 1.5, 1.5 2.5,...
#> 3 POLYGON ((0.5 0.5, 0.5 1.5,...
#> 4 POLYGON ((1.5 0.5, 1.5 1.5,...

Created on 2018-06-21 by the reprex package (v0.2.0).

@mdsumner
Copy link

I use fortify the usual way, I don't think broom added anything except a well intended rationalization? When I care about speed or generality (ie sf as well) I use spbabel::sptable, but I'm probably alone in that
Sf can't provide this feature for sp, it requires more system deps than fortify uses and the sf plotting is not as flexible as the fortify approach allows (it just can't do holes properly, but that's rarely needed and ggpolypath does provide what the fortify approach was missing). I wouldn't miss the tidy methods for broom but I'll continue to use fortify for simple and straightforward tasks.

sf::st_coordinates is the sf fortify, but returns a matrix and is named only by abstract 'level'. If you support sf with sf use that function, otherwise I'm happy to port a fortify/tidy.sf.

Apologies for long winded comment!

@mdsumner
Copy link

fwiw, where we are at for practicalities if anyone is wondering:

  • use sfheaders to turn sf into a df, it includes the reverse (create sf from a data frame) and it's fast and well supported
  • use ggplot2::fortify to turn sp into a df, or use spbabel::sptable, or raster::geom

spbabel includes creating sp from a data frame, but if you can live with sf it's probably better to use sfheaders -> sf -> sp using sf's as_Spatial

@github-actions
Copy link

This issue has been automatically closed due to inactivity.

@github-actions
Copy link

github-actions bot commented Apr 7, 2022

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants