An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20091014/c5ea56ca/attachment.pl>
Multiple polygons
4 messages · Roger Bivand, Rui Catarino
On Wed, 14 Oct 2009, Rui Catarino wrote:
Hello to all, This is my first attempt to use GIS tools in R so I'm sorry if my question is to na?ve, but the truth is that I've spent several hours looking in forum's and packages for a solution with no joy. What I'm trying to achieve is to create multiple polygons (around 150) based in a table with (x,y) coordinates. This table also as two extra fields one which will be the code (numeric value) for each polygon. The second field is the numeric order in which the points in each polygon should be joined.
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
Thanks in advance Rui [[alternative HTML version deleted]]
Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: Roger.Bivand at nhh.no
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:
Hello to all, This is my first attempt to use GIS tools in R so I'm sorry if my question is to na?ve, but the truth is that I've spent several hours looking in forum's and packages for a solution with no joy. What I'm trying to achieve is to create multiple polygons (around 150)
based in a table with (x,y) coordinates. This table also as two extra fields one which will be the code (numeric value) for each polygon.
The
second field is the numeric order in which the points in each polygon should be joined.
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
Thanks in advance Rui [[alternative HTML version deleted]]
Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: Roger.Bivand at nhh.no ______________________________________________________________________ This email has been scanned by the MessageLabs Email Security System. For more information please visit http://www.messagelabs.com/email ______________________________________________________________________ -------------- next part -------------- A non-text attachment was scrubbed... Name: Example.xls Type: application/vnd.ms-excel Size: 16384 bytes Desc: Example.xls URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20091014/2149d2de/attachment.xls>
On Wed, 14 Oct 2009, Rui Catarino wrote:
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.
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
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:
Hello to all, This is my first attempt to use GIS tools in R so I'm sorry if my question is to na?ve, but the truth is that I've spent several hours looking in forum's and packages for a solution with no joy. What I'm trying to achieve is to create multiple polygons (around 150)
based in a table with (x,y) coordinates. This table also as two extra fields one which will be the code (numeric value) for each polygon.
The
second field is the numeric order in which the points in each polygon should be joined.
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
Thanks in advance Rui [[alternative HTML version deleted]]
Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: Roger.Bivand at nhh.no