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

2018-08-15 Thread Antonio Silva
Hi Vijay, Michael and Obrl

Thanks for the answers, finally I could do the job.

I learned a lot too!

Best regards

Antonio Olinto

Em Ter, 14 de ago de 2018 11:19 PM, Vijay Lulla 
escreveu:

> Maybe you can try this then?
>
> polys <- SpatialPolygons(lapply(unique(vertices$cod),
>  function(x) {
>Polygons(list(Polygon(vertices[vertices$cod ==
> x, 1:2])), ID=x)
>  } ))
> HTH,
> Vijay.
>
> On Tue, Aug 14, 2018 at 6:03 PM 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

>>>
>
> --
> Vijay Lulla
>
> Assistant Professor,
> Dept. of Geography, IUPUI
> 425 University Blvd, CA-207C.
> Indianapolis, IN-46202
> vlu...@iupui.edu
> ORCID: https://orcid.org/-0002-0823-2522
> Webpage: http://vijaylulla.com
>

[[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

2018-08-14 Thread Michael Sumner
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

2018-08-14 Thread obrl soil
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

2018-08-14 Thread Antonio Silva
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

2018-08-14 Thread Vijay Lulla
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

2018-08-14 Thread Antonio Silva
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