[R] ascii-grid export

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Wed Nov 6 11:04:06 CET 2013


On Mon, Nov 4, 2013 at 7:27 AM, Enzo Cocca <enzo.ccc at gmail.com> wrote:
> yes barry I really need this.
>
> I tried to use raster or rgdal but with poor results.
>
> I have this function:
>
> VGM_PARAM_A3 <- gstat(id="bos_bison",
> formula=combusto~1,locations=~coord_x+coord_y, data=archezoology_table,
> nmax = 10)
>
> VGM_PARAM_A3 <- gstat(VGM_PARAM_A3, model=vgm(1, "Sph", 5, 0),
> fill.all=TRUE)
>
> ESV_A3 <- variogram(VGM_PARAM_A3, map=True, with=0.1, cutoff=9)
>
> VARMODEL_A3 = fit.lmc(ESV_A3, VGM_PARAM_A3)
>
> plot(ESV_A3, threshold = 5, col.regions = bpy.colors(), xlab=, ylab=,
> main="Map - A3")
> png("C:\Users\User\pyarchinit_R_folder\A3 semivariogram_map.png",
> width=10000, height=10000, res=400)
>
> I make a png file but how can I convert it in ascii-grid?

Why do you want to make an ascii-grid out of this? The variogram map
isn't in geographical coordinates, its in coordinate differences in x
and y

The return value when map=TRUE doesn't seem to be too well documented,
but looks like it is a list with a 'map' element that is a spatial
pixels data frame. Here's an example using the demo data (I can't run
your code because I don't have your data, please try and make your
problems easily reproducible):

require(sp)
require(gstat)
data(meuse)
coordinates(meuse)=~x+y
v=variogram(log(zinc)~1, meuse,map=TRUE,cutoff=900,width=10)
class(v$map)
[1] "SpatialPixelsDataFrame"
attr(,"package")
[1] "sp"

Now that can be written using rgdal's writeGDAL function.

However, you need the AAIGrid driver to work properly:

> writeGDAL(v$map,"vmap.ag",driver="AAIGrid")
Error in .local(.Object, ...) : Dataset copy failed

raster package to the rescue:

> require(raster)
> writeRaster(raster(v$map),"v.asc","ascii")
class       : RasterLayer

[etc]

When I look at the file, I have an ESRI grid file:

$ head -5 v.asc
NCOLS 181
NROWS 181
XLLCORNER -905
YLLCORNER -905
CELLSIZE 10
[+ data]

Now, that's assuming you wanted to write the data, not a pretty image
picture like you get when you plot the variogram map. And there's
still the mystery of why you want to write a non-geographic
coordinate-based dataset to a geographic data format...

 And you should probably have asked this on R-sig-geo where the
geographRs (including the authors of gstat) hang out.

 Hope this helps anyway.

Barry



More information about the R-help mailing list