Skip to content

Multiple polygons

4 messages · Roger Bivand, Rui Catarino

#
On Wed, 14 Oct 2009, Rui Catarino wrote:

            
What do the input data look like? Is there a list of +/-150 data.frame 
objects, or a single data.frame object (read with read.table()). If the 
latter, use split() first to make a list of data.frames, where the list 
components will take the name of the ID column values (assumed all 
identical for coordinates belonging to the same polygon).

Once you've got the list, say lst, you'll use lapply() to build a list of 
Polygons objects, each built of a list with a single Polygon object. This 
needs the coordinates in the right order and closed, so something like:

crds <- cbind(obj$x, obj$y)
crds <- crds[obj$order,]
crds <- rbind(crds, crds[1,])

should get them re-ordered, and then ring-closed. Insert the names as ID 
as appropriate. If this seems obscure, provide a very simple example data 
set on a website - it is actually quite straightforward if the data are 
cleanly organised.

Hope this helps,

Roger

  
    
#
Hello again,

Thank you for such a quick reply.
 
You were right, it seems a bit obscure so following your suggestion I
attach a small example of what looks like my table. The list is in excel
format and I usually use the package xlsReadWrite to read the excel
files.

Once again thank you for your trouble
Rui  

-----Original Message-----
From: Roger Bivand [mailto:Roger.Bivand at nhh.no] 
Sent: 14 October 2009 14:30
To: Rui Catarino
Cc: r-sig-geo at stat.math.ethz.ch
Subject: Re: [R-sig-Geo] Multiple polygons
On Wed, 14 Oct 2009, Rui Catarino wrote:

            
The
What do the input data look like? Is there a list of +/-150 data.frame 
objects, or a single data.frame object (read with read.table()). If the 
latter, use split() first to make a list of data.frames, where the list 
components will take the name of the ID column values (assumed all 
identical for coordinates belonging to the same polygon).

Once you've got the list, say lst, you'll use lapply() to build a list
of 
Polygons objects, each built of a list with a single Polygon object.
This 
needs the coordinates in the right order and closed, so something like:

crds <- cbind(obj$x, obj$y)
crds <- crds[obj$order,]
crds <- rbind(crds, crds[1,])

should get them re-ordered, and then ring-closed. Insert the names as ID

as appropriate. If this seems obscure, provide a very simple example
data 
set on a website - it is actually quite straightforward if the data are 
cleanly organised.

Hope this helps,

Roger

  
    
#
On Wed, 14 Oct 2009, Rui Catarino wrote:

            
After converting to CSV (no Excel here):

Example.df <- read.csv("Example.csv")
ldf <- split(Example.df, Example.df$Polygon_n)
# split into list on polygon ID
lpls <- lapply(ldf, function(x) {
   crds <- cbind(x$Long, x$Lat)
   crds <- crds[x$order,]
   crds <- rbind(crds, crds[1,])
   ID <- as.character(unique(x$Polygon_n))
   Polygons(list(Polygon(crds)), ID=ID)
})
# lapply to make a list of Polygons objects
lvdf <- lapply(ldf, function(x) {
   rn <- as.character(unique(x$Polygon_n))
   StartDate <- strptime(unique(as.character(x$StartDate)), format="%m/%d/%Y")
   CloseDate <- strptime(unique(as.character(x$CloseDate)), format="%m/%d/%Y")
   AnalysisDate <- strptime(unique(as.character(x$AnalysisDate)),
     format="%m/%d/%Y")
   data.frame(StartDate=StartDate, CloseDate=CloseDate,
     AnalysisDate=AnalysisDate, row.names=rn)
})
vdf <- do.call("rbind", lvdf)
# lapply to extract the unique values and make them into POSIXlt objects 
# as they are dates, then do.call("rbind", ...) to stick the n single
# line data.frames together
spdf <- SpatialPolygonsDataFrame(SpatialPolygons(lpls,
   proj4string=CRS("+proj=longlat +datum=WGS84")), data=vdf)
# assemble the list of Polygons objects and matching data.frame
# as a SpatialPolygonsDataFrame

All the *apply() functions are really user friendly and save very much 
more time than that needed to learn them. Think of lapply() and sapply() 
like a for loop along a vector, usually along a list, doing something to 
each component - here custom functions. Starting on a small, 
representative example usually helps to get the incantation right.

Note that the orders were all redundant here, so adjustment may be needed 
there.

Hope this helps,

Roger