Re: [R-sig-Geo] how to create several polygons from a list of vertices
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 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 > 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 > > 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 > >> 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
Re: [R-sig-Geo] how to create several polygons from a list of vertices
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 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 > 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 >> 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@r-project.org >>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo >>> >> > > [[alternative HTML version deleted]] > > ___ > R-sig-Geo mailing list > R-sig-Geo@r-project.org > https://stat.ethz.ch/mailman/listinfo/r-sig-geo ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Re: [R-sig-Geo] how to create several polygons from a list of vertices
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 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 > 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@r-project.org >> https://stat.ethz.ch/mailman/listinfo/r-sig-geo >> > [[alternative HTML version deleted]] ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Re: [R-sig-Geo] how to create several polygons from a list of vertices
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 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@r-project.org > https://stat.ethz.ch/mailman/listinfo/r-sig-geo > [[alternative HTML version deleted]] ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
[R-sig-Geo] how to create several polygons from a list of vertices
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@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Re: [R-sig-Geo] Extract coordinates from rasterbrick
Dear Ákos and Vijay, Thank you very much for the solutions. They work perfectly! Sincerely, Milu On Tue, Aug 14, 2018 at 6:12 PM, Vijay Lulla wrote: > And if you need coordinates as part of your data frame just do > cbind(coordinates(x), as.data.frame(x, xy = TRUE)) > > On Tue, Aug 14, 2018 at 11:50 AM Bede-Fazekas Ákos > wrote: > > > Dear Milu, > > > > I think that you are looking for as.data.frame(x, xy = TRUE). > > > > HTH, > > Ákos Bede-Fazekas > > Hungarian Academy of Sciences > > > > > > 2018.08.14. 17:03 keltezéssel, Miluji Sb írta: > > > Dear all, > > > > > > I have the following rasterbrick (x); > > > > > > class : RasterBrick > > > dimensions : 112, 272, 30464, 7305 (nrow, ncol, ncell, nlayers) > > > resolution : 0.25, 0.25 (x, y) > > > extent : -132, -64, 24, 52 (xmin, xmax, ymin, ymax) > > > coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 > > > data source : /projectnb/climpct/TEX_1986_2005.nc > > > names : X1986.01.01, X1986.01.02, X1986.01.03, X1986.01.04, > > > X1986.01.05, X1986.01.06, X1986.01.07, X1986.01.08, X1986.01.09, > > > X1986.01.10, X1986.01.11, X1986.01.12, X1986.01.13, X1986.01.14, > > > X1986.01.15, ... > > > Date: 1986-01-01, 2005-12-31 (min, max) > > > varname : tex > > > > > > > > > I can obtain the values by; > > > > > > as.data.frame(getValues(x)) > > > > > > However, I would like to extract the values by all coordinates in the > > file > > > in the form of a dataframe. Is it possible to do so? Any help will be > > > greatly appreciated. Thank you. > > > > > > Sincerely, > > > > > > Milu > > > > > > [[alternative HTML version deleted]] > > > > > > ___ > > > R-sig-Geo mailing list > > > R-sig-Geo@r-project.org > > > https://stat.ethz.ch/mailman/listinfo/r-sig-geo > > > > > > > ___ > > R-sig-Geo mailing list > > R-sig-Geo@r-project.org > > https://stat.ethz.ch/mailman/listinfo/r-sig-geo > > > > [[alternative HTML version deleted]] > > ___ > R-sig-Geo mailing list > R-sig-Geo@r-project.org > https://stat.ethz.ch/mailman/listinfo/r-sig-geo > [[alternative HTML version deleted]] ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Re: [R-sig-Geo] Extract coordinates from rasterbrick
And if you need coordinates as part of your data frame just do cbind(coordinates(x), as.data.frame(x, xy = TRUE)) On Tue, Aug 14, 2018 at 11:50 AM Bede-Fazekas Ákos wrote: > Dear Milu, > > I think that you are looking for as.data.frame(x, xy = TRUE). > > HTH, > Ákos Bede-Fazekas > Hungarian Academy of Sciences > > > 2018.08.14. 17:03 keltezéssel, Miluji Sb írta: > > Dear all, > > > > I have the following rasterbrick (x); > > > > class : RasterBrick > > dimensions : 112, 272, 30464, 7305 (nrow, ncol, ncell, nlayers) > > resolution : 0.25, 0.25 (x, y) > > extent : -132, -64, 24, 52 (xmin, xmax, ymin, ymax) > > coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 > > data source : /projectnb/climpct/TEX_1986_2005.nc > > names : X1986.01.01, X1986.01.02, X1986.01.03, X1986.01.04, > > X1986.01.05, X1986.01.06, X1986.01.07, X1986.01.08, X1986.01.09, > > X1986.01.10, X1986.01.11, X1986.01.12, X1986.01.13, X1986.01.14, > > X1986.01.15, ... > > Date: 1986-01-01, 2005-12-31 (min, max) > > varname : tex > > > > > > I can obtain the values by; > > > > as.data.frame(getValues(x)) > > > > However, I would like to extract the values by all coordinates in the > file > > in the form of a dataframe. Is it possible to do so? Any help will be > > greatly appreciated. Thank you. > > > > Sincerely, > > > > Milu > > > > [[alternative HTML version deleted]] > > > > ___ > > R-sig-Geo mailing list > > R-sig-Geo@r-project.org > > https://stat.ethz.ch/mailman/listinfo/r-sig-geo > > > > ___ > R-sig-Geo mailing list > R-sig-Geo@r-project.org > https://stat.ethz.ch/mailman/listinfo/r-sig-geo > [[alternative HTML version deleted]] ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Re: [R-sig-Geo] Extract coordinates from rasterbrick
Dear Milu, I think that you are looking for as.data.frame(x, xy = TRUE). HTH, Ákos Bede-Fazekas Hungarian Academy of Sciences 2018.08.14. 17:03 keltezéssel, Miluji Sb írta: Dear all, I have the following rasterbrick (x); class : RasterBrick dimensions : 112, 272, 30464, 7305 (nrow, ncol, ncell, nlayers) resolution : 0.25, 0.25 (x, y) extent : -132, -64, 24, 52 (xmin, xmax, ymin, ymax) coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 data source : /projectnb/climpct/TEX_1986_2005.nc names : X1986.01.01, X1986.01.02, X1986.01.03, X1986.01.04, X1986.01.05, X1986.01.06, X1986.01.07, X1986.01.08, X1986.01.09, X1986.01.10, X1986.01.11, X1986.01.12, X1986.01.13, X1986.01.14, X1986.01.15, ... Date: 1986-01-01, 2005-12-31 (min, max) varname : tex I can obtain the values by; as.data.frame(getValues(x)) However, I would like to extract the values by all coordinates in the file in the form of a dataframe. Is it possible to do so? Any help will be greatly appreciated. Thank you. Sincerely, Milu [[alternative HTML version deleted]] ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
[R-sig-Geo] Extract coordinates from rasterbrick
Dear all, I have the following rasterbrick (x); class : RasterBrick dimensions : 112, 272, 30464, 7305 (nrow, ncol, ncell, nlayers) resolution : 0.25, 0.25 (x, y) extent : -132, -64, 24, 52 (xmin, xmax, ymin, ymax) coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 data source : /projectnb/climpct/TEX_1986_2005.nc names : X1986.01.01, X1986.01.02, X1986.01.03, X1986.01.04, X1986.01.05, X1986.01.06, X1986.01.07, X1986.01.08, X1986.01.09, X1986.01.10, X1986.01.11, X1986.01.12, X1986.01.13, X1986.01.14, X1986.01.15, ... Date: 1986-01-01, 2005-12-31 (min, max) varname : tex I can obtain the values by; as.data.frame(getValues(x)) However, I would like to extract the values by all coordinates in the file in the form of a dataframe. Is it possible to do so? Any help will be greatly appreciated. Thank you. Sincerely, Milu [[alternative HTML version deleted]] ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo