[R-sig-Geo] Technique required: create a new shapefile from polygon vertices list

Roger Bivand Roger.Bivand at nhh.no
Tue Jun 21 09:40:02 CEST 2005


On Mon, 20 Jun 2005, Rick Reeves wrote:

> 
> 
> 
> I have a collection of polygons, expressed as ordered lists 
> or line segments defined as x/y coordinate pairs. In the line
> set for each polygon, the last coordinate of the last line
> connects to the first coordinate of the first line. 
> 
> I can plot these using the graphics package method 'polygon'. 
> 
> Now, I would like to export them as a shapefile. Seemed like
> this would be simple. So far, I have examined the maptools,
> shapefiles,sp, geoR packages and found no way to write a 
> polygon shapefile that does not require the input data to 
> have been in a shapefile (e.g., maptools.write.polylistShape).

Assuming you would like each list element to be a separate shape:

> library(sp)
> pl <- list(s1=cbind(c(2,4,4,1,2), c(2,3,5,4,2)), s2=cbind(c(5,4,2,5), 
+   c(2,3,2,2)), s3=cbind(c(4,4,5,10,4), c(5,3,2,5,5)))
> str(pl)
List of 3
 $ s1: num [1:5, 1:2] 2 4 4 1 2 2 3 5 4 2
 $ s2: num [1:4, 1:2] 5 4 2 5 2 3 2 2
 $ s3: num [1:5, 1:2] 4 4 5 10 4 5 3 2 5 5
> plot(c(0,11), c(1,6), asp=1, type="n")
> for (i in pl) polygon(i)
> names(pl)
[1] "s1" "s2" "s3"

replicates your list (but here I've named the polygons in the list with 
unique names, you can use names(pl) <- c(<unique names>) if need be.

> for (i in 1:length(pl)) Srsl[[i]] <- Srings(list(Sring(pl[[i]])), 
+   names(pl)[i])
> shps <- SpatialRings(Srsl, pO=1:length(pl))
> plot(shps)

That gives us a SpatialRings object, the plot order pO= argument is
required, but will not be needed later for this case. Now for the 
attribute data - it is required, because shapefiles have to have a DBF 
file:

> df <- data.frame(x1=rbinom(3,6,0.1), x2=sample(letters,3), x3=runif(3), 
+    row.names=getSRSringsIDSlots(shps))
> df
   x1 x2         x3
s1  1  d 0.78274192
s2  0  k 0.07043669
s3  1  o 0.09974697
> SRDF <- SpatialRingsDataFrame(shps, df)

Now that we have a SpatialRingsDataFrame object, we can write it as a 
shapefile (in fact using the facilities in the maptools package inside a 
wrapper in the spmaptools package in the repository at sourceforge):

# rSpatial <- "http://r-spatial.sourceforge.net/R"
# install.packages("spmaptools", repos=rSpatial)
> library(spmaptools)
> writeSRDFShape(SRDF, "mySRDF")
> getinfo.shape("mySRDF")
Shapefile Type: Polygon   # of Shapes: 3 

The sp package mechanisms also let you subset your SRDF, use the polygons 
from the maps package, and use ArcInfo e00 files too if you feel like it. 
You can also use gpclib through the spgpc wrapper to dissolve internal 
borders when aggregating polygons if you need to. More ideas will be 
welcome too ... (we still don't have a way to write a *.prj file from the 
PROJ.4-format coordinate reference system information that we can embed in 
the sp objects, that does need doing).

Hope this helps

Roger 

> 
> Someone suggests using read.table in geoR. But how do you 
> specify that you want to create polygons? 
> 
> Can anyone suggest a method using R methods or otherwise (e.g. gdal)
> for generating a polygon shapefile as I describe? Even
> the Arc GIS documentation is sketchy on thiis topic. 
> 
> Thanks, Rick Reeves
> 
> 
> Scientific Programmer / Quantitative Analyst
> National Center for Ecological Analysis and Synthesis
> University of California, Santa Barbara
> 805 892 2534
> reeves at nceas.ucsb.edu
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> 

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




More information about the R-sig-Geo mailing list