R-ArcGIS Bridge / Reticulate

745
1
04-26-2022 09:40 AM
AndyMarine
New Contributor

Hi all

I am wondering if anyone has any advice, I am running R-ArcGIS Bridge with the reticulate and arcgisbinding package in RStudio / R-GUI, and I am experiencing some strange behavior when executing certain arcpy tools. Most tools work fine, and I do not think I have a problem with the bridge/reticulate install.

library(reticulate)
py_discover_config()
arcpy <- import("arcpy")
arcpy$CheckOutExtension("Spatial")
arcpy$env$overwriteOutput = TRUE
library(arcgisbinding)
arc.check_product()

For example:

The below works fine in RStudio, the buffer distance inherits the units from the input, in case of WGS84, its in degrees.

arcpy$analysis$Buffer('INPUT.shp', 'BUFFEROUT.shp', 1, "", "", "", "", "") 

When I try to set the standard distance unit of Kilometers in the standard way that we do this in ArcGIS, RStudio crashes with "Fatal Error", R-GUI just exits, I can run the buffer tool absolutely fine in ArcGIS:

arcpy$analysis$Buffer('INPUT.shp', 'BUFFEROUT.shp', "1 Kilometers", "", "", "", "", "")

I also observed similar issues when using CalculateGeometryAttributes, I get the hard exit when I set "AREA AREA_GEODESIC" as the column name and area calculation. Again the tool runs fine with the correct paramters in ArcGIS.

arcpy$management$CalculateGeometryAttributes('INPUT.shp', "AREA AREA_GEODESIC")

I wondered if I was not coding the input string correctly, many tools work fine and do not have an issue, if I put the wrong arguments into the arcpy tool in RStudio, it gives me the standard error that I see in Python. 

Thanks

Andy

 

 

 

0 Kudos
1 Reply
by Anonymous User
Not applicable

Hi Andy,

Thanks for your question above. I think the issue is related to the function signature, and translating some of the Pythonic inputs of arcpy methods to R.

Below is the snippet that runs successfully:

library(reticulate)
#py_discover_config() ## Did not work
use_python("C:/ArcGIS/Pro/bin/Python/envs/arcgispro-py3/python.exe")
arcpy <- import("arcpy")
arcpy$CheckOutExtension("Spatial")
arcpy$env$overwriteOutput = TRUE
library(arcgisbinding)

arc.check_product()

data.path <- "<path to my gdb>"

arcpy$analysis$Buffer(file.path(data.path,"Cases_state"), file.path(data.path,"Cases_state_Buffer_1"), "1 Kilometers", "FULL", "ROUND", "NONE", NULL, "PLANAR")

arcpy$management$CalculateGeometryAttributes(file.path(data.path,"farm_training"), "RED AREA_GEODESIC", '', '', NULL, "SAME_AS_INPUT")

 

The two discrepancies I see in your code are, function signatures and lacking Nones. As per function signatures, I always run the tool in Pro once, go to History and copy the Python snippet. The tool runs I did in Pro 2.9 have different signatures than the ones you posted, that can be one source of error.

Secondly, some of the empty parameters will be brought in as None, when transforming this snippet in R, you should change these to NULL.

Below are some suggestions to make reticulate easier to use (these are not necessarily sources of error but some suggestions to make your life easier):

- Instead of using py_discover_config, I recommend using use_python. It is a precise call you can make to the python interpreter installed with Pro. There is nothing wrong with using py_discover_config, however you will be at the mercy of your environment variables and if you have more than one Python version installed on your machine, there can be issues. In my case, even though I had one Python version, this function did not detect python.exe inside arcgispro-py3.

-  I also recommend using file.path to define full paths to data (reticulate needs full paths) since Windows-style backslashes in directory names is a common source of error when using arcpy through reticulate. If they exist in the full path, arcpy can produce unintuitive error messages. file.path  moves this stop-gap forward while producing correct file paths that comply with UNIX-style paths that R expects. 

I hope the above helps.

 

Orhun