[R-sig-Geo] how to create several polygons from a list of vertices
Vijay Lulla
vij@ylull@ @ending from gm@il@com
Wed Aug 15 04:19:24 CEST 2018
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 <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
>>>
>>
--
Vijay Lulla
Assistant Professor,
Dept. of Geography, IUPUI
425 University Blvd, CA-207C.
Indianapolis, IN-46202
vlulla using iupui.edu
ORCID: https://orcid.org/0000-0002-0823-2522
Webpage: http://vijaylulla.com
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list