Has anyone successfully created a script tool with an R script using a raster dataset?

2056
7
Jump to solution
09-29-2016 10:36 AM
StacyStrickland
New Contributor II

I have an R script that uses the spatialEco package to create percent volume rasters. It works well as a stand-alone script but I would like to create a script tool in ArcGIS to use in ModelBuilder. The error I am getting seems related to the fact that the 'raster.vol' function needs to operate on a raster layer object but when I use the 'arc.open' function in my script, my raster opens as a :RasterDataset and arcgisbinding doesn't seem to have an inherited method to allow me to use the 'raster' function to convert the object to a raster layer. Any suggestions?

0 Kudos
1 Solution

Accepted Solutions
SteveKopp
Esri Contributor

Stacy - raster data is not currently supported in the the R-ArcGIS Bridge. This is functionality we are adding so if you have particular requirements for raster support please let us know.

Steve

View solution in original post

7 Replies
SteveKopp
Esri Contributor

Stacy - raster data is not currently supported in the the R-ArcGIS Bridge. This is functionality we are adding so if you have particular requirements for raster support please let us know.

Steve

StacyStrickland
New Contributor II

Thanks for the reply, Steve! Looking forward to when raster data is supported to be able to automate some processing.

0 Kudos
DanPatterson_Retired
MVP Emeritus

what are you looking to do since the numpy and scipy covers most of R... depending what you are working on

0 Kudos
StacyStrickland
New Contributor II

I would like to create a tool to use in Model Builder with other tools to automate a series of processes that involves creating a raster in ArcGIS then running a script in R and adding the results back into ArcGIS. See my original post for the exact function I am using in R.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Yes I saw that it is product by some spatialEco and does % raster volume which is why I asked since it sounded pretty generic and something that could be accomplished with the aid of 3D or pure numpy.

0 Kudos
StacyStrickland
New Contributor II

Dan, I'm not familiar with 3D or numpy, so I don't know if they are capable of the same raster calculation. Something to look into, thanks!

0 Kudos
DanPatterson_Retired
MVP Emeritus

There is some pretty simple stuff you can do with numpy.  Consider a slope with elevations ranging from 0 to 29 m and a cell size of 1m^2.  What volume of earth would be removed if we removed all the material >=15 leaving behind a flat plateau behind.

In [15]: a = np.arange(5*6).reshape(5,6)
Out[16]: a
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29]])

In [17]: a[a>15]
Out[17]: array([16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29])

In [18]: np.sum(np.abs(15 - a[a>15]))
Out[18]: 105

So if you have a specific example, I am sure it can be done.  You aren't limited to 1d or 2d arrays either.  For instance, consider a tiny place (2x2) over 5 time periods.  How do you calculate the per area averages over the time period, the averages per time and the overall average.

In [19]: t = np.arange(5*2*2).reshape(5,2,2)

In [20]: t
Out[20]: 
array([[[ 0,  1],
        [ 2,  3]],

       [[ 4,  5],
        [ 6,  7]],

       [[ 8,  9],
        [10, 11]],

       [[12, 13],
        [14, 15]],

       [[16, 17],
        [18, 19]]])

In [21]: np.average(t, axis=0)
Out[21]: 
array([[ 8.000,  9.000],
       [ 10.000,  11.000]])

In [22]: np.average(t, axis=1)
Out[22]: 
array([[ 1.000,  2.000],
       [ 5.000,  6.000],
       [ 9.000,  10.000],
       [ 13.000,  14.000],
       [ 17.000,  18.000]])

In [23]: np.average(t)
Out[23]: 
9.5

R and Numpy do share a lot... just a bit of difference in syntax.. there are a few translation tables out there converting between R, Numpy and Julia for instance.  I have a large repository of 'stuff' on my blog if you are interested.