[R-sig-Geo] Shape to AscGrid

Roger Bivand Roger.Bivand at nhh.no
Sun Mar 18 13:50:58 CET 2007

On Sat, 17 Mar 2007, Milton Cezar Ribeiro wrote:

> Dear helpers
> I need convert a shape file to AscGrid format. How can I select a field,
> convert to AscGrid and save as ascii file?

This should work:

xx <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1], 
  IDvar="FIPSNO", proj4string=CRS("+proj=longlat +ellps=clrk66"))
grd <- GE_SpatialGrid(xx, asp=1, maxPixels=100)$SG
# to make a SpatialGrid object, could use GridTopology() directly, here 
# asp=1 to be sure that cells are square
poly_which <- overlay(xx, grd)
# copies out the polygon field values to the appropriate grid cells
SGDF <- SpatialGridDataFrame(slot(grd, "grid"), data=poly_which, 
image(SGDF, "BIR74")
plot(xx, add=TRUE)
writeAsciiGrid(SGDF, "BIR74.aai", "BIR74")

Of course, you could compute rates, etc, on the SpatialPolygonDataFrame 
before copying the values out to the grid cells, and you could drop data 
columns in the SpatialPolygonDataFrame first to avoid unecessary copying. 
But this runs pretty quickly anyway.

Hope this helps,


> Kind regards,
> miltinho
> __________________________________________________
> 	[[alternative HTML version deleted]]
> _______________________________________________
> 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