[R-sig-Geo] how to create several polygons from a list of vertices

Michael Sumner md@umner @ending from gm@il@com
Wed Aug 15 02:09:57 CEST 2018


Here's another way with spbabel.


library(dplyr)
library(spbabel)
pts <-
  tibble::tribble(~ID,  ~x,  ~y, ~grp,
                   1 , -33, -23,    1,
                   2 , -32, -23,    1,
                   3 , -32, -22,    1,
                   4 , -33, -22,    1,
                   5 , -32, -23,    2,
                   6 , -31, -23,    2,
                   7 , -31, -22,    2,
                   8 , -32, -22,    2,
                   9 , -31, -23,    3,
                   10, -30, -23,    3,
                   11, -30, -22,    3,
                   12, -31, -22,    3,
                   13, -33, -22,    4,
                   14, -32, -22,    4,
                   15, -32, -21,    4,
                   16, -33, -21,    4,
                   17, -32, -22,    5,
                   18, -31, -22,    5,
                   19, -31, -21,    5,
                   20, -32, -21,    5,
                   21, -31, -22,    6,
                   22, -30, -22,    6,
                   23, -30, -21,    6,
                   24, -31, -21,    6)


## objects and branches (parts) are the same level

## objects and branches (parts) are the same level
## and we maintain "grp" as the object attribute data
x <- pts %>% transmute(grp = grp, x_ = x, y_ = y,
               object_ = grp, branch_ = grp,
               order_ = ID, island_ = TRUE) %>% spbabel::sp()





On Wed, 15 Aug 2018 at 09:25 obrl soil <obrlsoilau using gmail.com> wrote:

> Hi Antonio,
>
> have you tried with sf? Like:
>
> library(sf)
>
> pts <-
>   tibble::tribble(~ID,  ~x,  ~y, ~grp,
>                    1 , -33, -23,    1,
>                    2 , -32, -23,    1,
>                    3 , -32, -22,    1,
>                    4 , -33, -22,    1,
>                    5 , -32, -23,    2,
>                    6 , -31, -23,    2,
>                    7 , -31, -22,    2,
>                    8 , -32, -22,    2,
>                    9 , -31, -23,    3,
>                    10, -30, -23,    3,
>                    11, -30, -22,    3,
>                    12, -31, -22,    3,
>                    13, -33, -22,    4,
>                    14, -32, -22,    4,
>                    15, -32, -21,    4,
>                    16, -33, -21,    4,
>                    17, -32, -22,    5,
>                    18, -31, -22,    5,
>                    19, -31, -21,    5,
>                    20, -32, -21,    5,
>                    21, -31, -22,    6,
>                    22, -30, -22,    6,
>                    23, -30, -21,    6,
>                    24, -31, -21,    6)
>
> squares <- split(pts, pts$grp)
> squares <- lapply(squares, function(g) {
>   # get just coords
>   g <- as.matrix(g[, c(2,3)])
>   # repeat first point last to close poly
>   g <- rbind(g, g[1, ])
>   # convert to an sf polygon object
>   gp <- sf::st_polygon(list(g))
>   # make sure the vertices are in an order
>   # that complies with the simple
>   # features standard
>   gp <- sf::st_buffer(gp, 0L)
> })
> # turn list of polygons into geometry column
> squares_sfc <- sf::st_sfc(squares) # can add crs = ?? to this call
> # add an ID to make an sf data frame
> squares_sf <- sf::st_sf('ID' = seq(6), 'geometry' = squares_sfc)
>
> # if you still need to use sp for whatever reason
> squares_sp <- as(squares_sf, "Spatial")
>
> Regards,
> @obrl_soil
>
> On Wed, Aug 15, 2018 at 8:03 AM, Antonio Silva <aolinto.lst using gmail.com>
> wrote:
> > Thanks Lulla,
> >
> > Nice solution. I could also export it as a shapefile after transforming
> it
> > to a spatial polygon dataframe.
> >
> > The problem is that I could not "individualize" the squares in a
> multipart
> > layer. They all have the same ID. I tried to change this without success:
> > "Single ID required".
> >
> > The attribute table of the shapefile should have 6 lines in my example
> and
> > not only one.
> >
> > Any other option?
> >
> > Thanks again,
> >
> > Antonio Olinto
> >
> >
> > Em ter, 14 de ago de 2018 às 18:10, Vijay Lulla <vijaylulla using gmail.com>
> > escreveu:
> >
> >> Maybe something like this?
> >>
> >> poly <- SpatialPolygons(list(Polygons(tapply(seq_len(nrow(vertices)),
> >>                                              vertices$cod,
> >>                                              function(x)
> >> Polygon(vertices[x,1:2])), ID="1")),
> >>                         proj4string=CRS("+proj=longlat +ellps=WGS84
> >> +datum=WGS84 +no_defs"))
> >>
> >>
> >> On Tue, Aug 14, 2018 at 4:17 PM Antonio Silva <aolinto.lst using gmail.com>
> >> wrote:
> >>
> >>> Hi,
> >>>
> >>> I have a data.frame with the vertices (lon / lat) and codes from
> several
> >>> squares (more than 500 in the real dataset).
> >>> I want to create an object with these polygons (squares) and after this
> >>> export it as a shapefile.
> >>> With the script below I can draw one square.
> >>> library(sp)
> >>> P1 = Polygon(vertices[1:4,1:2])
> >>> Ps1 = SpatialPolygons(list(Polygons(list(P1), ID = "1")),
> >>> proj4string=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"))
> >>> plot(Ps1, axes = TRUE)
> >>>
> >>> Now I'm trying to create one object with all squares at once.
> >>> Is it possible?
> >>>
> >>> Thanks a lot,
> >>>
> >>> Antônio Olinto
> >>>
> >>> sample data:vertices
> >>>    lon lat cod
> >>> 1  -33 -23   1
> >>> 2  -32 -23   1
> >>> 3  -32 -22   1
> >>> 4  -33 -22   1
> >>> 5  -32 -23   2
> >>> 6  -31 -23   2
> >>> 7  -31 -22   2
> >>> 8  -32 -22   2
> >>> 9  -31 -23   3
> >>> 10 -30 -23   3
> >>> 11 -30 -22   3
> >>> 12 -31 -22   3
> >>> 13 -33 -22   4
> >>> 14 -32 -22   4
> >>> 15 -32 -21   4
> >>> 16 -33 -21   4
> >>> 17 -32 -22   5
> >>> 18 -31 -22   5
> >>> 19 -31 -21   5
> >>> 20 -32 -21   5
> >>> 21 -31 -22   6
> >>> 22 -30 -22   6
> >>> 23 -30 -21   6
> >>> 24 -31 -21   6
> >>>
> >>>         [[alternative HTML version deleted]]
> >>>
> >>> _______________________________________________
> >>> R-sig-Geo mailing list
> >>> R-sig-Geo using r-project.org
> >>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> >>>
> >>
> >
> >         [[alternative HTML version deleted]]
> >
> > _______________________________________________
> > R-sig-Geo mailing list
> > R-sig-Geo using r-project.org
> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
-- 
Dr. Michael Sumner
Software and Database Engineer
Australian Antarctic Division
203 Channel Highway
Kingston Tasmania 7050 Australia

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list