R, ArcGIS, and other Tutorials
Page 1 of 1
R, ArcGIS, and other Tutorials
This tutorial is called "How to connect R and ArcGIS using RPyGeo".
First of all you need to download, install and load package RPyGeo in R:
library(RPyGeo)
When you’re dealing with loads of data it’s better to have both input and output folders. The easiest way is to define them at the beginning and then use the paste () function when you want to import (or export) any data from (or to) these folders.
Create input and output folder:
input.folder<-"C:/data/input/"
output.folder<-"C:/data/output/"
Now, imagine that you have three rasters (temperature, timber, rainfall) in your input folder that you want to use to calculate a cost layer, simply by multiplying them. The first thing you have to do is define your RPyGeo environment, so that R knows where Python is in your computer.
Define RPyGeo environment:
rpygeo.env<-rpygeo.build.env(python.path="C:/Python25",python.command="python.exe",workspace="C:/data/input/", extent = "C:/data/any.shp", mask = "C:/data/any.shp", overwriteoutput = 1, cellsize=0.05)
Then you just need to use the map algebra Python function through RPyGeo (remember to use space between operators!), and tell R to store the output in your output folder, by using the paste () function.
Cost layer calculation using map algebra:
rpygeo.SingleOutputMapAlgebra.sa("temperature * timber * rainfall", paste(output.folder, "costlayer",sep=""), env=rpygeo.env)
Now, imagine that you want to import this output to R so you can use it for statistical analysis. In two steps, you can easily do that. First, you need to transform the raster into and ascii file, and them import it to R.
Tranform rater to ascii and save the output in the input folder:
rpygeo.RasterToASCII.conversion(paste(output.folder,"costlayer",sep=""),paste(input.folder,"costlayer.asc",sep=""))
Import data to R (here you might need to load the maptools package):
costlayer<-as.matrix(readAsciiGrid(paste(input.folder,"costlayer.asc",sep="")))
This way your costlayer variable will be a regular matrix and you can do any statistical analysis on it.
I hope it helps!
First of all you need to download, install and load package RPyGeo in R:
library(RPyGeo)
When you’re dealing with loads of data it’s better to have both input and output folders. The easiest way is to define them at the beginning and then use the paste () function when you want to import (or export) any data from (or to) these folders.
Create input and output folder:
input.folder<-"C:/data/input/"
output.folder<-"C:/data/output/"
Now, imagine that you have three rasters (temperature, timber, rainfall) in your input folder that you want to use to calculate a cost layer, simply by multiplying them. The first thing you have to do is define your RPyGeo environment, so that R knows where Python is in your computer.
Define RPyGeo environment:
rpygeo.env<-rpygeo.build.env(python.path="C:/Python25",python.command="python.exe",workspace="C:/data/input/", extent = "C:/data/any.shp", mask = "C:/data/any.shp", overwriteoutput = 1, cellsize=0.05)
Then you just need to use the map algebra Python function through RPyGeo (remember to use space between operators!), and tell R to store the output in your output folder, by using the paste () function.
Cost layer calculation using map algebra:
rpygeo.SingleOutputMapAlgebra.sa("temperature * timber * rainfall", paste(output.folder, "costlayer",sep=""), env=rpygeo.env)
Now, imagine that you want to import this output to R so you can use it for statistical analysis. In two steps, you can easily do that. First, you need to transform the raster into and ascii file, and them import it to R.
Tranform rater to ascii and save the output in the input folder:
rpygeo.RasterToASCII.conversion(paste(output.folder,"costlayer",sep=""),paste(input.folder,"costlayer.asc",sep=""))
Import data to R (here you might need to load the maptools package):
costlayer<-as.matrix(readAsciiGrid(paste(input.folder,"costlayer.asc",sep="")))
This way your costlayer variable will be a regular matrix and you can do any statistical analysis on it.
I hope it helps!
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|