27
Working with the R-ArcGIS Bridge Esri Canada Toronto User Conference Haydn Lawrence - [email protected] Michael G. Leahy PhD - [email protected] October 11, 2018

Working with the R-ArcGIS Bridge - Esri

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Working with the R-ArcGIS Bridge - Esri

Working with the R-ArcGIS Bridge

Esri Canada Toronto User Conference

Haydn Lawrence - [email protected]

Michael G. Leahy PhD - [email protected]

October 11, 2018

Page 2: Working with the R-ArcGIS Bridge - Esri

Agenda

• Introduction / Overview

• Basic use of the R-ArcGIS package in R scripts

• Building geoprocessing script tools using R-ArcGIS

• Integration of R script tools into the ArcGIS Platform

Page 3: Working with the R-ArcGIS Bridge - Esri

An overview of R and the R-ArcGIS bridge

Introduction

Page 4: Working with the R-ArcGIS Bridge - Esri

About

• Open Source system for statistical computation

and graphics – has become de facto standard

language for statisticians

• Over 13,000 packages available from the

Comprehensive R Archive Network (about 11,000

a year ago, more than doubled since 2016)

• Powerful language and packages

for generating plots and graphics

Source: StackOverflow

r4stats.com

ggvis.rstudio.com

www.r-bloggers.com

Page 5: Working with the R-ArcGIS Bridge - Esri

About the R-ArcGIS bridge

ArcGIS Seamlessly Read/Write ArcGIS

Feature Class, Raster, & Tabular datasets

https://r-arcgis.github.io/

Enable execution of R functions in ArcGIS

applications as Geoprocessing script tools

Page 6: Working with the R-ArcGIS Bridge - Esri

Setup Steps

1. Install ArcGIS Pro 1.1+ (or ArcGIS 10.3.1+)

2. Install R 3.3.2+ or later (https://www.r-project.org/ or https://mran.microsoft.com/open)

3. Install the arcgisbinding package:

ArcGIS Pro

Project -> Options -> Geoprocessing

ArcGIS Desktop (e.g., ArcMap)

Run the Install tool in the R Integration toolbox

Detailed Instructions: https://github.com/R-ArcGIS/r-bridge-install

Page 7: Working with the R-ArcGIS Bridge - Esri

Getting Started

• Choose/install an IDE for editing R code:

- Default R-GUI

- RStudio

- Visual Studio / Visual Studio Code

• Learn R basics:

- https://highered-esricanada.github.io/r-arcgis-tutorials/

• R-ArcGIS vignette, manual, samples:

- https://r-arcgis.github.io/assets/arcgisbinding-vignette.html

- https://r-arcgis.github.io/assets/arcgisbinding.pdf

- https://github.com/R-ArcGIS/r-sample-tools

Page 8: Working with the R-ArcGIS Bridge - Esri

Using the R-ArcGIS Bridge in R

Page 9: Working with the R-ArcGIS Bridge - Esri

Using the arcgisbinding package in R

• To start working with arcgisbinding in R:

> library(arcgisbinding)

> arc.check_product()

Note: only needed for standalone R scripts

Loading the library in R

Page 10: Working with the R-ArcGIS Bridge - Esri

Using the arcgisbinding package in R

• Connect to data:

gis_data <- arc.open(<path>)

• Load data:

features_df <- arc.select(gis_data, ...)

raster_obj <- arc.raster(gis_data, ...)

Working with ArcGIS datasets

• Convert data (to sp, sf, or raster):

data_sp <- arc.data2sp(features_df)

data_sf <- arc.data2sf(features_df)

data_raster <- as.raster(raster_obj)

...

• Write data:

arc.write(path=<path>, data=data_sp)

Page 11: Working with the R-ArcGIS Bridge - Esri

Example use of the arcgisbinding

package in R code

Page 12: Working with the R-ArcGIS Bridge - Esri

Building R Script Tools

Page 13: Working with the R-ArcGIS Bridge - Esri

R-ArcGIS Script Tools

• Make R statistical functionality accessible to users/applications

across the ArcGIS Platform:

ArcGIS Pro R Workspace

Page 14: Working with the R-ArcGIS Bridge - Esri

R-ArcGIS Script Tools

• Main entry point for an R-script to be used as a script tool:

tool_exec <- function(in_params, out_params) { ... <code> ... }

• Get/set input and output parameters as named properties:

feature_data <- arc.select(arc.open(in_params$in_data), ...)

out_params$result <- sum(feature_data$values)

arc.write(path=out_params$out_data, data=feature_data)

• Return output parameters when function is finished:

return(out_params)

Basic structure

Page 15: Working with the R-ArcGIS Bridge - Esri

R-ArcGIS Script Tools

• Set progress label/position for UI feedback:

arc.progress_label("Analysis in progress...")

arc.progress_pos(50)

• Print messages and warnings in the Geoprocessing window:

message(paste(c("Input value: ", in_params$in_val), collapse="")))

warning("Input parameter not set, defaulting to x ...")

• Raise errors (and stop execution):

stop("Invalid input data.")

Messaging & Feedback

Page 16: Working with the R-ArcGIS Bridge - Esri

R-ArcGIS Script ToolsToolbox configuration

Page 17: Working with the R-ArcGIS Bridge - Esri

Sample R Script Tool

Page 18: Working with the R-ArcGIS Bridge - Esri

Using R tools in a complete workflow for analysis

Integration of R Script Tools

Page 19: Working with the R-ArcGIS Bridge - Esri

Integration with ArcPy and

Model Builder

Page 20: Working with the R-ArcGIS Bridge - Esri

Project – Emerald ash borer

• Farah Tasneem, Masters student, University of Toronto

• First detected in North America in 2002 in Windsor, Ontario

• Projected $2 billion cost over a 30-year period + unknown ecological cost

• 99% of the ash trees within a woodlot killed within 6 years

• Difficult to detect emerald ash borer at low population levels

Looking for areas in Ontario that are potentially susceptible to large

infestations

1 Emerald ash borer, Natural Resources Canada (2018). Retrieved from: https://www.nrcan.gc.ca/forests/fire-insects-

disturbances/top-insects/13377

Page 21: Working with the R-ArcGIS Bridge - Esri

Project – Data

• Model parameters sample_points_table.xlsx

- Distance to the Nearest Emerald Ash Borer Positive Location

- Forest Processing Facilities

- Population Centres

- Elevation

- Ports

- NDVI

- Slope

- Precipitation

- Maximum Temperature

- Aspect

- Vapour Pressure

Page 22: Working with the R-ArcGIS Bridge - Esri

Project – Data

• Clip of southern part of Ontario

• rGLM training data

- Received from researchers

• Annual Crop Inventory for Ontario

- Reclassified for Emerald ash borer suitability

Page 23: Working with the R-ArcGIS Bridge - Esri

Project – Methods

• Random Generalized Linear Model (rGLM)

- Can be used for several outcomes (e.g. binary, multi-class, count, quantitative) 1

- Advantages of RandomForest and forward selected GLM (less overfitting) 1

• Inverse Distance Weighted (IDW)

- The variable decreases in influence as distance grows

• Reclassify

- Reclassifying land-use types to suitability/probability of EAB presence

1 Song, L., Langfelder, P. and Horvath, S., 2013. Random generalized linear model: A highly accurate and interpretable ensemble

predictor. BMC bioinformatics, 14(1), p.5.

Page 24: Working with the R-ArcGIS Bridge - Esri

Sample Script Tool: Calculating

probabilities using rGLM

Page 25: Working with the R-ArcGIS Bridge - Esri

Project – Outputs

Page 26: Working with the R-ArcGIS Bridge - Esri

R / R-ArcGIS Resources

- R-ArcGIS Project page:

https://r-arcgis.github.io

- R Cheatsheets:

https://www.rstudio.com/resources/cheatsheets/

- Package Vignettes – E.g.:

- R-ArcGIS Bridge (arcgisbinding):

https://r-arcgis.github.io/assets/arcgisbinding-vignette.html

- Geographically Weighted Regression (spgwr):

https://cran.r-project.org/web/packages/spgwr/vignettes/GWR.pdf

- Spatial Inequalities (lctools):

https://cran.r-

project.org/web/packages/lctools/vignettes/SpatialInequalities.pdf

- Samples, Blogs, GeoNet, Videos/Tutorials, etc.:

- https://github.com/R-ArcGIS/r-sample-tools

- https://github.com/R-ArcGIS/CHANS-tools

- https://www.r-bloggers.com/

- https://www.r-project.org/help.html

- https://geonet.esri.com/groups/rstats

- http://hed.esri.ca/resourcefinder/#/search=r/lang=en

- https://learn.arcgis.com/en/projects/analyze-crime-using-

statistics-and-the-r-arcgis-bridge/

- https://youtu.be/i6Pc8SwWpyM?list=PLaPDDLTCmy4Z27y

CYMJkyxj3WHtFBW08l

Page 27: Working with the R-ArcGIS Bridge - Esri

© 2018 Esri Canada Limited. All rights reserved. Trademarks

provided under license from Environmental Systems

Research Institute, Inc. Other product and company names

mentioned herein may be trademarks or registered

trademarks of their respective owners. Errors and omissions

excepted.