[R-sig-Geo] Spatial analysis - matrix algebra

Paulo Cardoso pecardoso at netcabo.pt
Mon Jul 7 12:23:24 CEST 2008


Thanks Mike. I know rgdal package (you introduce it to me!) but I used it
occasionally.

One question remains since I want to consider an array of options to get
cost distance surfaces (e.g. Varying the cost of each habitat).

I've being playing around with calling other programs from R and change
iteratively parameters to be considered at each "run".
Something like this:

for(z in 1:length(iter)) ## R iterations with iterations to be used by the
program
{
  input[17]<-paste("NUMITNS",as.character(iter[z]),sep=" ") ## write the
iter parameter in R internal input file
    for(cc in 1:length(costs)) ## R iterations with costs to be used by the
program
    {
      unit$cost<-as.numeric(costs[cc]) ## write the cost parameter into the
R internal unit file
      #unit$cost[abund$pu[abund$species==4]]<-as.numeric(costs[cc]) ## Cost
for seagrasses - not used
      write.table(unit,"unit.dat",sep="\t",row.names=F,quote=F,dec=".") ##
write the unit.dat file to be used in each R loop
        for(tt in 1:length(perc.t)) ## R iterations with CF target
percentages to be used by the program
        {
          target[,3]<-agg.abx202[,2]*perc.t[tt] ## All targets with perc.t %
of the total available area
          target[which(target$name=="Marine
waters"),3]<-agg.abx202[which(agg.abx202$species=="Marine waters"),2]*0.1 ##
Marine waters with 10% of total available in all scenarios
          target[which(target$name=="Terrestrial habitats"),3]<-0 ##
Terrestrial habitats with 0% of total available in all cenarios
            for(ii in 1:length(spf.t)) ## R iterations with Penalty to be
used by the program Target spf parameter
            {
              target[,4]<-rep(as.numeric(spf.t[ii]),nrow(target)) ## write
the SPF parameter into the R internal target file
              write.table(target,"target.dat",sep=",",row.names=F,quote =
FALSE) ## write the target.dat file to be used in each R loop
                for(i in 1:length(blm)) ## R iterations with BLM factor to
be used by the program
                {
                  input[10]<-paste("BLM",blm[i]) ## write the BLM factor
into the R internal input file
                    for(r in 1:length(nreps))
                    {
                      input[14]<-paste("NUMREPS",nreps[r]) ## write the
Number of Runs into the R internal input file
                      temp<-date()
                      input[35]<-paste("SCENNAME"," ",  ## write the
Scenario Name into the R internal input file with all parameters
 
paste(substring(temp,12,13),substring(temp,15,16),substring(temp,18,19),sep=
""),"-", # internal clock time at the beggining of the process
                                  "Output","-",                 # Initial
Sentence for each by the program Run
                                  "Iter",as.character(iter[z]), # Number of
iteractions
                                  "Cost",costs[cc],"-",         # Cost used
                                  "Targ",perc.t[tt],"-",        # % of
Target used
                                  "Spf",spf.t[ii],"-",          # SPF used
                                  "Blm",blm[i],"-",             # BLM used
                                  "Reps",nreps[r]               # Nreps used
                                  ,sep="")

                      write(input,"input.dat")# Re-write the final input.dat
file to be used at each program run with the corresponding changed
parameters
                      system("program.exe",wait=T,invisible=T) # Calling
program to execute each scenário
                    }
                }
            }
        }
    }
}


In fact I have no idea if R can perform cost analysis as IDRISI, ArgGIS,
GRASS, SAGA, etc...
In a cost analysis we usually have a point, a line or polygon defined as
start positions (sources in this case), so we have the interaction of Two
themes...



-----Original Message-----
From: Michael Sumner [mailto:mdsumner at utas.edu.au] 
Sent: segunda-feira, 7 de Julho de 2008 11:05
To: Paulo Cardoso
Cc: r-sig-geo at stat.math.ethz.ch
Subject: Re: [R-sig-Geo] Spatial analysis - matrix algebra

Hi Paulo,

You can read ESRI ASCII grids (amongst many others) using rgdal package:

library(rgdal)
d <- readGDAL("file1.asc")

See gdalDrivers() for a listing of the available file formats for your 
package build.

The read above would result in a single-colum SpatialGridDataFrame - 
named "band1". To add further files as a new column (assuming the 
subsequent files have exactly the same grid topology):

d$band2 <- readGDAL("file2.asc")$band1

. . . and so on.

To obtain the resulting raster bands as individual matrices you can use 
as.image.SpatialGridDataFrame:

m1 <- as.image.SpatialGridDataFrame(d[1])$z

m2 <- as.image.SpatialGriddataFrame(d[2])$z

. . .

Then use your matrices as needed.  If you only want the matrices and not 
the intermediate SGDF objects, use readGDAL directly:

m1 <- as.image.SpatialGridDataFrame(readGDAL("file1.asc")[1])
. . .

These could be collected in a single 3-way array, or a list of matrices 
as needed.

HTH,

Mike.


Paulo Cardoso wrote:
> Can R perform matrix algebra between ascii converted Esri grids? I'm
> convinced that its trivial.
>
>  
>
> The question is a little more complex (from my point of view and
> inexperience): I'm interested in perform cost surface and distance surface
> analysis avoiding ArcGIS tools. Maybe R can deal with grid formatted as
> matrices...
>
>  
>
> Maybe R could call FWtools to perform all calculations iteratively...
>
>  
>
> Paulo
>
>  
>
>
> 	[[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
>
>
> No virus found in this incoming message.
> Checked by AVG - http://www.avg.com 
> Version: 8.0.138 / Virus Database: 270.4.5/1537 - Release Date: 7/6/2008
5:26 AM
>
>
>
>   

Internal Virus Database is out of date.
Checked by AVG. 

03-07-2008
8:32




More information about the R-sig-Geo mailing list