Title: | ArcGIS Geoprocessing via Python |
---|---|
Description: | Provides access to ArcGIS geoprocessing tools by building an interface between R and the ArcPy Python side-package via the reticulate package. |
Authors: | Alexander Brenning [aut, cre], Fabian Polakowski [aut], Marc Becker [aut], Jannes Muenchow [ctb] |
Maintainer: | Alexander Brenning <[email protected]> |
License: | GPL-3 |
Version: | 1.0.1 |
Built: | 2024-11-20 03:19:58 UTC |
Source: | https://github.com/r-spatial/rpygeo |
Provide access to (virtually any) ArcGIS geoprocessing tool from within R by running Python geoprocessing without writing Python code or touching ArcGIS.
The package utilizes the ArcPy Python site-package or the ArcGIS API
in order to access ArcGIS functionality. The function
rpygeo_build_env
can be applied to generate an ArcPy or arcgis
object.
Maintainer: Alexander Brenning [email protected]
Authors:
Fabian Polakowski [email protected]
Marc Becker [email protected]
Other contributors:
Jannes Muenchow (0000-0001-7834-4717) [contributor]
Useful links:
# load the ArcPy module related to ArcGIS Pro (and save it as a R # object called "arcpy_m") in R and also set the overwrite parameter # to FALSE and add some extensions. Note that we do not have to set the path # because the Python version is located in the default location # (C:/Program Files/ArcGIS/Pro/bin/Python/envs/arcgispro-py3/)in this example. ## Not run: arcpy <- rpygeo_build_env(overwrite = TRUE, extensions = c("3d", "Spatial", "na"), pro = TRUE) ## End(Not run) # Suppose we want to calculate the slope of a Digtial Elevation Model. # It is possible to get the description of any ArcPy function as a R list: ## Not run: py_function_docs("arcpy$Slope_3d") # Now we can run our computation: ## Not run: arcpy$Slope_3d(arcpy$Slope_3d(in_raster = "dem.tif", out_raster = "slope.tif")
# load the ArcPy module related to ArcGIS Pro (and save it as a R # object called "arcpy_m") in R and also set the overwrite parameter # to FALSE and add some extensions. Note that we do not have to set the path # because the Python version is located in the default location # (C:/Program Files/ArcGIS/Pro/bin/Python/envs/arcgispro-py3/)in this example. ## Not run: arcpy <- rpygeo_build_env(overwrite = TRUE, extensions = c("3d", "Spatial", "na"), pro = TRUE) ## End(Not run) # Suppose we want to calculate the slope of a Digtial Elevation Model. # It is possible to get the description of any ArcPy function as a R list: ## Not run: py_function_docs("arcpy$Slope_3d") # Now we can run our computation: ## Not run: arcpy$Slope_3d(arcpy$Slope_3d(in_raster = "dem.tif", out_raster = "slope.tif")
Subtraction operator for map algebra. Spatial Analylist extension is requiered for map algebra.
raster_1 %rpygeo_-% raster_2
raster_1 %rpygeo_-% raster_2
raster_1 |
raster dataset or numeric |
raster_2 |
raster dataset or numeric |
reticulate object
Marc Becker
## Not run: # Load the ArcPy module and build environment arcpy <- arcpy_build_env(overwrite = TRUE, workspace = "C:/workspace") # Write raster to workspace directory writeRater(elev, "C:/workspace/elev.tif", extensions = "Spatial") # Create raster object ras <- arcpy$sa$Raster("elev.tif") # Substract raster from itself ras %rpygeo_+% ras %>% rpygeo_load() ## End(Not run)
## Not run: # Load the ArcPy module and build environment arcpy <- arcpy_build_env(overwrite = TRUE, workspace = "C:/workspace") # Write raster to workspace directory writeRater(elev, "C:/workspace/elev.tif", extensions = "Spatial") # Create raster object ras <- arcpy$sa$Raster("elev.tif") # Substract raster from itself ras %rpygeo_+% ras %>% rpygeo_load() ## End(Not run)
Multiplication operator for map algebra. Spatial Analylist extension is requiered for map algebra.
raster_1 %rpygeo_*% raster_2
raster_1 %rpygeo_*% raster_2
raster_1 |
raster dataset or numeric |
raster_2 |
raster dataset or numeric |
reticulate object
Marc Becker
## Not run: # Load the ArcPy module and build environment arcpy <- arcpy_build_env(overwrite = TRUE, workspace = "C:/workspace") # Write raster to workspace directory writeRater(elev, "C:/workspace/elev.tif", extensions = "Spatial") # Create raster object ras <- arcpy$sa$Raster("elev.tif") # Multiply raster to itself ras %rpygeo_+% ras %>% rpygeo_load() ## End(Not run)
## Not run: # Load the ArcPy module and build environment arcpy <- arcpy_build_env(overwrite = TRUE, workspace = "C:/workspace") # Write raster to workspace directory writeRater(elev, "C:/workspace/elev.tif", extensions = "Spatial") # Create raster object ras <- arcpy$sa$Raster("elev.tif") # Multiply raster to itself ras %rpygeo_+% ras %>% rpygeo_load() ## End(Not run)
Division operator for map algebra. Spatial Analylist extension is requiered for map algebra.
raster_1 %rpygeo_/% raster_2
raster_1 %rpygeo_/% raster_2
raster_1 |
raster dataset or numeric |
raster_2 |
raster dataset or numeric |
reticulate object
Marc Becker
## Not run: # Load the ArcPy module and build environment arcpy <- arcpy_build_env(overwrite = TRUE, workspace = "C:/workspace") # Write raster to workspace directory writeRater(elev, "C:/workspace/elev.tif", extensions = "Spatial") # Create raster object ras <- arcpy$sa$Raster("elev.tif") # Divide raster by itself ras %rpygeo_+% ras %>% rpygeo_load() ## End(Not run)
## Not run: # Load the ArcPy module and build environment arcpy <- arcpy_build_env(overwrite = TRUE, workspace = "C:/workspace") # Write raster to workspace directory writeRater(elev, "C:/workspace/elev.tif", extensions = "Spatial") # Create raster object ras <- arcpy$sa$Raster("elev.tif") # Divide raster by itself ras %rpygeo_+% ras %>% rpygeo_load() ## End(Not run)
Addition operator for map algebra. Spatial Analylist extension is requiered for map algebra.
raster_1 %rpygeo_+% raster_2
raster_1 %rpygeo_+% raster_2
raster_1 |
raster dataset or numeric |
raster_2 |
raster dataset or numeric |
reticulate object
Marc Becker
## Not run: # Load the ArcPy module and build environment arcpy <- arcpy_build_env(overwrite = TRUE, workspace = "C:/workspace") # Write raster to workspace directory writeRater(elev, "C:/workspace/elev.tif", extensions = "Spatial") # Create raster object ras <- arcpy$sa$Raster("elev.tif") # Add raster to itself ras %rpygeo_+% ras %>% rpygeo_load() ## End(Not run)
## Not run: # Load the ArcPy module and build environment arcpy <- arcpy_build_env(overwrite = TRUE, workspace = "C:/workspace") # Write raster to workspace directory writeRater(elev, "C:/workspace/elev.tif", extensions = "Spatial") # Create raster object ras <- arcpy$sa$Raster("elev.tif") # Add raster to itself ras %rpygeo_+% ras %>% rpygeo_load() ## End(Not run)
Initialises the Python ArcPy site-package in R via the
reticulate
package. Addtionally environment settings and extensions
are configured.
rpygeo_build_env(path = NULL, overwrite = TRUE, extensions = NULL, x64 = FALSE, pro = FALSE, arcgisAPI = FALSE, workspace = NULL, scratch_workspace = NULL)
rpygeo_build_env(path = NULL, overwrite = TRUE, extensions = NULL, x64 = FALSE, pro = FALSE, arcgisAPI = FALSE, workspace = NULL, scratch_workspace = NULL)
path |
Full path to folder containing Python version which is linked to
the ArcPy site-package. If left empty, the function looks
for |
overwrite |
If |
extensions |
Optional character vector listing ArcGIS extension that should be enabled (does not work while using ArcGIS API for Python) |
x64 |
Logical (default: |
pro |
Logical (default: |
arcgisAPI |
Logical (default: |
workspace |
Path of ArcGIS workspace in which to perform the geoprocessing (does not work while using ArcGIS API for Python). |
scratch_workspace |
Path to ArcGIS scratch workspace in which to store
temporary files (does not work while using ArcGIS API for Python). If
|
Returns ArcPy or ArcGIS modules in R
Fabian Polakowski, Marc Becker
## Not run: # Load ArcPy side-package of ArcGIS Pro with 3D and Spatial Analysis extension. # Set environment setting 'overwrite' to TRUE. # Note that no path parameter is necessary because Python is located in the # default location. arcpy <- rpygeo_build_env(overwrite = TRUE, extensions = c("3d", "Spatial"), pro = TRUE) ## End(Not run) # Load the ArcPy module when your Python version is located in a different # folder
## Not run: # Load ArcPy side-package of ArcGIS Pro with 3D and Spatial Analysis extension. # Set environment setting 'overwrite' to TRUE. # Note that no path parameter is necessary because Python is located in the # default location. arcpy <- rpygeo_build_env(overwrite = TRUE, extensions = c("3d", "Spatial"), pro = TRUE) ## End(Not run) # Load the ArcPy module when your Python version is located in a different # folder
This function opens the help file for ArcPy function in viewer panel or if not available in the browser.
rpygeo_help(arcpy_function)
rpygeo_help(arcpy_function)
arcpy_function |
ArcPy module with function or class |
Marc Becker
## Not run: # Load the ArcPy module and build environment arcpy <- arcpy_build_env(overwrite = TRUE, workspace = tempdir()) # Open help file rpygeo_help(arcpy$Slope_3d) ## End(Not run)
## Not run: # Load the ArcPy module and build environment arcpy <- arcpy_build_env(overwrite = TRUE, workspace = tempdir()) # Open help file rpygeo_help(arcpy$Slope_3d) ## End(Not run)
This function loads the output of an ArcPy function into the R session. Raster files are loaded as raster
objects and vector files as sf
objects.
rpygeo_load(data)
rpygeo_load(data)
data |
|
Currently files and datasets stored in file geodatabases are supported.
Supported file formats:
Tagged Image File Format (.tif)
Erdas Imagine Images (.img)
Esri Arc/Info Binary Grid (.adf)
Esri ASCII Raster (.asc)
Esri Shapefiles (.shp)
Supported datasets:
Feature Class
Raster Dataset
Esri has not released an API for raster datasets in file geodatabases. rpygeo_load
converts a raster dataset to a temporary ASCII raster first and then loads it into the R session. Be aware that this can take a long time for large raster datasets.
This function can be used with the %>%
operator from the dplyr
package. The %>%
operator forwards the reticulate
object from the ArcPy function to rpygeo_load
(s. Example 1). If used without the %>%
operator an reticulate
object can be specified for the data
parameter (s. Example 2). It is also possible to use the filename of the ArcPy function output (s. Example 3). For Arc/Info Binary Grids the data
parameter is just the name of the directory, which contains the adf
files.
raster
or sf
object
Marc Becker
## Not run: # Load packages library(RPyGeo) library(magrittr) library(RQGIS) library(spData) # Get data data(dem, package = "RQGIS") data(nz, package = "spData") # Write data to disk writeRaster(dem, file.path(tempdir(), "dem.tif"), format = "GTiff") st_write(nz, file.path(tempdir(), "nz.shp")) # Load the ArcPy module and build environment arcpy <- arcpy_build_env(overwrite = TRUE, workspace = tempdir()) # Create a slope raster and load it into the R session (Example 1) slope <- arcpy$Slope_3d(in_raster = "dem.tif", out_raster = "slope.tif") %>% rpygeo_load() # Create a aspect raster and load it into the R session (Example 2) ras_aspect <- arcpy$sa$Aspect(in_raster = "dem.tif") rpygeo_load(ras_aspect) # Convert elevation raster to polygon shapefile and load it into R session (Example 3) arcpy$RasterToPolygon_conversion("dem.tif", "elev.shp") rpygeo_load("elev.shp") ## End(Not run)
## Not run: # Load packages library(RPyGeo) library(magrittr) library(RQGIS) library(spData) # Get data data(dem, package = "RQGIS") data(nz, package = "spData") # Write data to disk writeRaster(dem, file.path(tempdir(), "dem.tif"), format = "GTiff") st_write(nz, file.path(tempdir(), "nz.shp")) # Load the ArcPy module and build environment arcpy <- arcpy_build_env(overwrite = TRUE, workspace = tempdir()) # Create a slope raster and load it into the R session (Example 1) slope <- arcpy$Slope_3d(in_raster = "dem.tif", out_raster = "slope.tif") %>% rpygeo_load() # Create a aspect raster and load it into the R session (Example 2) ras_aspect <- arcpy$sa$Aspect(in_raster = "dem.tif") rpygeo_load(ras_aspect) # Convert elevation raster to polygon shapefile and load it into R session (Example 3) arcpy$RasterToPolygon_conversion("dem.tif", "elev.shp") rpygeo_load("elev.shp") ## End(Not run)
This function saves temporary a raster as permanent raster to the workspace.
rpygeo_save(data, filename)
rpygeo_save(data, filename)
data |
|
filename |
Filename with extension or without extension if the workspace is file geodatabase |
Some ArcPy functions have no parameter to specify an output raster. Instead they return a raster object and a temporary raster is saved to the scratch workspace. This functions writes the temporary raster as a permanent raster to the workspace.
How the file is written depends on the workspace and scratch workspace environment settings.
Workspace and scratch workspace are directories: Raster is loaded with the raster
package and is written to workspace directory. The file format is inferred from the file extension in the filename
parameter.
Workspace and scratch workspace are file geodatabases: Raster is copied to workspace file geodatabase. No file extension necessary for the filename
parameter.
Workspace is file geodatabase and scratch workspace is directory: Raster is copied to workspace file geodatabase. No file extension necessary for the filename
parameter.
Workspace is directory and scratch workspace is file geodatabase: Raster is exported to workspace directory. The filename
parameter is ignored due to restrictions in arcpy.RasterToOtherFormat_conversion
function. If the automatically generated filename already exists, a number is appended to the end of the filename.
Marc Becker
## Not run: # Load packages library(RPyGeo) library(RQGIS) library(magrittr) # Get data data(dem, package = "RQGIS") # Load the ArcPy module and build environment arcpy <- arcpy_build_env(overwrite = TRUE, workspace = tempdir()) # Write raster to workspace directory writeRaster(dem, file.path(tempdir(), "dem.tif"), format = "GTiff") # Calculate temporary aspect file and save to workspace arcpy$sa$Aspect(in_raster = "dem.tif") %>% rpygeo_save("aspect.tif") ## End(Not run)
## Not run: # Load packages library(RPyGeo) library(RQGIS) library(magrittr) # Get data data(dem, package = "RQGIS") # Load the ArcPy module and build environment arcpy <- arcpy_build_env(overwrite = TRUE, workspace = tempdir()) # Write raster to workspace directory writeRaster(dem, file.path(tempdir(), "dem.tif"), format = "GTiff") # Calculate temporary aspect file and save to workspace arcpy$sa$Aspect(in_raster = "dem.tif") %>% rpygeo_save("aspect.tif") ## End(Not run)
Search for ArcPy functions and classes with a character string or regular expression.
rpygeo_search(search_term = NULL)
rpygeo_search(search_term = NULL)
search_term |
Search term. Regular expressions are possible. |
The list members are referenced by the ArcPy module names. Each member contains a character vector of matching ArcPy functions and classes. Except for the main module, functions and classes have to be accessed by their module names (s. examples).
Named list of character vectors of matching ArcPy functions and classes
Marc Becker
## Not run: # Load packages library(RPyGeo) library(magrittr) library(RQGIS) # Get data data(dem, package = "RQGIS") # Write data to disk writeRaster(dem, file.path(tempdir(), "dem.tif"), format = "GTiff") # Load the ArcPy module and build environment arcpy <- rpygeo_build_env(overwrite = TRUE, workspace = tempdir(), extensions = "Spatial") # Search for ArcPy functions, which contain the term slope rpygeo_search("slope") #> $toolbox #> [1] "Slope_3d" "SurfaceSlope_3d" #> #> $main #> [1] "Slope_3d" "SurfaceSlope_3d" #> #> $sa #> [1] "Slope" #> #> $ddd #> [1] "Slope" "SurfaceSlope" # Run function from sa module arcpy$sa$Slope(in_raster="dem.tif") # Run function from main module arcpy$Slope_3d(in_raster="dem.tif") ## End(Not run)
## Not run: # Load packages library(RPyGeo) library(magrittr) library(RQGIS) # Get data data(dem, package = "RQGIS") # Write data to disk writeRaster(dem, file.path(tempdir(), "dem.tif"), format = "GTiff") # Load the ArcPy module and build environment arcpy <- rpygeo_build_env(overwrite = TRUE, workspace = tempdir(), extensions = "Spatial") # Search for ArcPy functions, which contain the term slope rpygeo_search("slope") #> $toolbox #> [1] "Slope_3d" "SurfaceSlope_3d" #> #> $main #> [1] "Slope_3d" "SurfaceSlope_3d" #> #> $sa #> [1] "Slope" #> #> $ddd #> [1] "Slope" "SurfaceSlope" # Run function from sa module arcpy$sa$Slope(in_raster="dem.tif") # Run function from main module arcpy$Slope_3d(in_raster="dem.tif") ## End(Not run)