Skip to content

polygon object constructed from data frame breaks plot #514

@barryrowlingson

Description

@barryrowlingson
> packageVersion("sf")
[1] ‘0.5.1’

Create a square:

> d = data.frame(x=c(0,1,1,0,0), y=c(0,0,1,1,0))
> d
  x y
1 0 0
2 1 0
3 1 1
4 0 1
5 0 0

Now try and make a polygon from the coordinates. First, as documented, converting d to a matrix:

> st_polygon(list(as.matrix(d)))
POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))

Secondly, without matrix conversion:

> st_polygon(list(d))
POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))

Both objects print the same, so my first assumption would have been that this has worked...

The first way plots correctly, but the second way errors:

> plot(st_polygon(list(as.matrix(d))))
> plot(st_polygon(list(d)))
Error in CPL_get_bbox(obj, 2) : 
  Not compatible with requested type: [type=list; target=double].

The structures are a bit different:

> str(st_polygon(list(d)))
List of 1
 $ :'data.frame':	5 obs. of  2 variables:
  ..$ x: num [1:5] 0 1 1 0 0
  ..$ y: num [1:5] 0 0 1 1 0
 - attr(*, "class")= chr [1:3] "XY" "POLYGON" "sfg"
> str(st_polygon(list(as.matrix(d))))
List of 1
 $ : num [1:5, 1:2] 0 1 1 0 0 0 0 1 1 0
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:2] "x" "y"
 - attr(*, "class")= chr [1:3] "XY" "POLYGON" "sfg"

Not sure if the solution is to coerce data frames to matrix before manipulation, or erroring, or figuring out why plot fails...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions