0

I would like to create a simple polygon using the sf package.

From my understanding, I have to create a two column matrix where the last row is identical to the first one to close the ring of the polygon.

p1 <- structure(c(0.424084334649542, 0.316800956453136, 0.394125036134903, 
                 0.424084334649542, 0.646053530054886, 0.608338100572382, 0.851623048730609, 
                 0.646053530054886), .Dim = c(4L, 2L))

The matrix p1 has the form that I want

          [,1]      [,2]
[1,] 0.4240843 0.6460535
[2,] 0.3168010 0.6083381
[3,] 0.3941250 0.8516230
[4,] 0.4240843 0.6460535

If I try to create a polygon with this data I got this error

st_polygon(p1)

Error in MtrxSet(x, dim, type = "POLYGON", needClosed = TRUE) :
is.list(x) is not TRUE

However for linestrings it works

st_linestring(p1)

LINESTRING (0.4240843 0.6460535, 0.316801 0.6083381, 0.394125 0.851623, 0.4240843 0.6460535)

Anyone knows why?

Thanks for the help.

1 Answer 1

1

My mistake!

I forgot to include p1 into a list.

This code works

st_polygon(list(p1))

POLYGON ((0.4240843 0.6460535, 0.316801 0.6083381, 0.394125 0.851623, 0.4240843 0.6460535))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.