Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to...

49
Connecting ArcGIS with R and Conda Shaun Walbridge

Transcript of Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to...

Page 1: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

ConnectingArcGISwithRandCondaShaunWalbridge

Page 3: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

ArcGIS

Page 4: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

Today:RandCondaConda

IntroductionOptionaldemo

RandtheR-ArcGISBridgeIntroductionDemoRoadAhead

Page 5: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

SoftwareEcosystem

Page 6: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

ConnectingwithArcGIS

Don’thaveanequivalentAPIforaccessingourservicesviaRliketheArcGISAPIforPython(yet)ArcGISisasystemofrecord.Combinedataandanalysisfrommanyfieldsandintoacommonenvironemnt.Whyconnect?Can’tdoitall,wesupportover1000GPtools—enablingintegrationacrossenvironmentsforrealisticworkflows

Page 7: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes
Page 8: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

TheSciPyStack

Page 9: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

DatasciencePython(SciPystack,Jupyter,scikit-learn,…)C++(Tensorflow,Shark,MLC++)Java(SparkMLlib,Weka)R( )Manyworkflowsrequirecombiningcomponentsfrommultipleenvironments.

MLtaskview

Page 10: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

PackageswithConda

Page 11: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

Whypackagemanagement?

Softwareiscomposedofmanysmallercomponents,oftencalledpackagesorlibraries.It’softenbettertoreusecodethatsolvesaproblemwellratherthanrecreatingitBut,sharingcodeisahardproblem.Doyouhavethesamepackagesofthesameversionsasthedeveloperdid?

Page 12: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

WhyConda?

ScientificPythoncommunityidentifiedthattherewasagapnotbeingaddressedbythecorePythoninfrastructure,limitingtheirabilitytogetpackagesintothehandsofusers

Industrystandardbuiltbypeoplewhocareaboutthisspace—ContinuumAnalytics(Esripartner)

Page 13: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

WhyConda?

Itsolvesthehardproblem:

Handlesdependenciesformanylanguages(C,C++,RandofcoursePython)BuiltforPythonfirst,butitreallysolvesamuchbroaderinfrastructuralissue.

Page 14: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

Conda

Page 15: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

Cross-platform:SimplydeveloprecipesforbuildingandinstallingsoftwareonLinux,OSXandWindows.Opensource:Esriisusingit,youcanuseitinyourownprojectsforothercontexts

Whatcanitinstall?Notjustscientificpackages,caninstalleverythingfrominteractiveenvironmentslike to .Spyder JupyterNotebooks

Page 16: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

Environments—Canisolateanenvironment,flexiblymakechangeswithotaffectinginstalledsoftware.Requirements—includeexplicitstateinformation,notjustthepackagename.Packages—anenvironmentisbuiltupfromoneormorepackages,canbefrommanylanguages–fromRtoC++toFortran.AlsohandlesplatformsandJupyternotebooks

Page 17: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

Wheredopackagescomefrom?

Condapackagescancomefromavarietyoflocations:

manythousandsofpackagesRepositories(e.g.AnacondaCloud,self-hosted)Ondisk

anaconda.org

Page 18: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

CondaBasics

Condainfoisthestartingpoint—ittellsyouthestateoftheenvironment.

conda --help

conda info

Page 19: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

CondaBasicsconda list

# packages in environment at C:\ArcGIS\bin\Python\envs\arcgispro-py3:#colorama 0.3.7 py35_0 defaultscycler 0.10.0 py35_0 defaultsfuture 0.15.2 py35_0 defaultsmatplotlib 1.5.3 np111py35_0e [arcgispro] esrimpmath 0.19 py35_1 defaultsnetcdf4 1.2.4 py35_0e [arcgispro] esrinose 1.3.7 py35_1 defaultsnumexpr 2.6.1 np111py35_0e [arcgispro] esrinumpy 1.11.2 py35_0e [arcgispro] esripandas 0.19.0 np111py35_0 defaultspip 8.1.2 py35_0 defaultspy 1.4.31 py35_0 defaultspyparsing 2.1.4 py35_0 defaultspypdf2 1.26.0 py_0 esripytest 2.9.2 py35_0 defaultspython 3.5.2 0 defaultspython-dateutil 2.5.3 py35_0 defaultspytz 2016.6.1 py35_0 defaultsrequests 2.11.1 py35_0 defaultsscipy 0.18.1 np111py35_0e [arcgispro] esrisetuptools 27.2.0 py35_1 defaultssympy 1.0 py35_0 defaultswheel 0.29.0 py35_0 defaults

Page 20: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

CondaBasicsActivatingenvironments,acoupleways:

UsetheshortcutsManuallyactivatetheenvironment:

cd C:\ArcGIS\bin\Python\Scripts activate arcgispro-py3

Page 21: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

CondaBasicsAcollectionofpackagesandPythoninstalliscalledanenvironmentorenv,thebuildingblockformanagingPythonwithCondaCanhavemultipleenvironmentsandseamlesslyswitchbetweenthem

Page 22: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

Condavs…Name Means Included?

Conda Thecommanditself ✓Miniconda AminimumsetofPythonpackagesto

buildandrunConda.✓

Anaconda Adistribution200+packagesbuiltwithConda

AnacondaServer

Hostthefullinfrastructureinternally

Page 23: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

DeeperDive

Page 24: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

HowcanIusethis?ArcGIS(Desktop&Server)shipstheSciPystack—powerfulandoutoftheboxinallproductsCondacommandandaCondarootPythoninstallNewmodules(e.g.requests),environmentwithPro

Page 25: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

HowcanIusethis?ArcGIS(Desktop&Server)shipstheSciPystack—powerfulandoutoftheboxinallproductsCondacommandandaCondarootPythoninstallNewmodules(e.g.requests),environmentwithProGetpackages,expandyourpossibilityspacePackageyourwork:thisisanopportunitytodistributeit,possiblyincludingcommercialsideaswell.

Page 26: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

HowcanIusethis?

Page 27: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

R

Page 28: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

Esriand ?IntegrationviaArcGIS–RbridgeJoined andMoretocome—GIShashistoricallybeenmorecoupledwithPython

RConsortium RFoundation

Page 29: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

Why ?Powerfulcoredatastructuresandoperations

Dataframes,functionalprogrammingUnparalleledbreadthofstatisticalroutines

ThedefactolanguageofStatisticiansCRAN:6400packagesforsolvingproblemsVersatileandpowerfulplotting

Page 30: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

RDataTypesyou’reusedtoseeing…

Numeric-Integer-Character-Logical-timestampDatatypes

Page 31: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

RDataTypesyou’reusedtoseeing…

Numeric-Integer-Character-Logical-timestamp…butothersyouprobablyaren’t:

vector-matrix-data.frame-factor

Datatypes

Page 32: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

DataFramesTreatstabular(andmulti-dimensional)dataasalabeled,indexedseriesofobservations.Soundssimple,butisagamechangerovertypicalsoftwarewhichisjustdoing2Dlayout(e.g.Excel)

Page 33: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

DataTypes# Create a data frame out of an existing sourcedf.from.csv <- read.csv( "data/growth.csv", header=TRUE)

Page 34: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

DataTypes# Create a data frame from scratchquarter <- c(2, 3, 1)person <- c("Goodchild", "Tobler", "Krige")

met.quota <- c(TRUE, FALSE, TRUE)df <- data.frame(person, met.quota, quarter)

Page 35: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

DataTypesR> df person met.quota quarter1 Goodchild TRUE 22 Tobler FALSE 33 Krige TRUE 1

Page 36: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

0D:SpatialPoints1D:SpatialLines2D:SpatialPolygons3D:Solid4D:Space-time

spTypesEntity+Attributemodel

Page 37: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

StatisticalFormulas

DomainspecificlanguageforstatisticsSimilarpropertiesinotherpartsofthelanguage

formodelspecificationconsistency

fit.results <- lm(pollution ~ elevation + rainfall +

caret

Page 38: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

R—ArcGISBridge

Page 39: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

R—ArcGISBridge

Page 40: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

R—ArcGISBridge

ArcGISdeveloperscancreatetoolsandtoolboxesthatintegrateArcGISandRArcGISuserscanaccessRcodethroughgeoprocessingscriptsRuserscanaccessorganizationsGIS’data,managedintraditionalGISways

https://r-arcgis.github.io

Page 41: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

R—ArcGISBridgeStoreyourdatainArcGIS,accessitquicklyinR,returnRobjectsback

toArcGISnativedatatypes(e.g.geodatabasefeatureclasses).

Knowshowtoconvertspatialdatatospobjects.PackageDocumentation

Page 42: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

AccessArcGISfromRStartbyloadingthelibrary,andinitializingconnectiontoArcGIS:

# load the ArcGIS-R bridge librarylibrary(arcgisbinding)# initialize the connection to ArcGIS. Only needed when running directly from R.arc.check_product()

Page 43: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

AccessArcGISfromRFirst,selectadatasource(canbeafeatureclass,alayer,oratable):

Then,filterthedatatothesetyouwanttoworkwith(createsin-memorydataframe):

ThiscreatesanArcGISdataframe–lookslikeadataframe,butretainsreferencesbacktothegeometrydata.

input.fc <- arc.open('data.gdb/features')

filtered.df <- arc.select(input.fc, fields=c('fid', 'mean'), where_clause="mean < 100")

Page 44: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

AccessArcGISfromRNow,ifwewanttodoanalysisinRwiththisspatialdata,weneedittoberepresentedasspobjects.arc.data2spdoestheconversion

forus:

arc.sp2datainvertsthisprocess,takingspobjectsandgeneratingArcGIScompatibledataframes.

df.as.sp <- arc.data2sp(filtered.df)

Page 45: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

AccessArcGISfromRFinishedwithourworkinR,wanttogetthedatabacktoArcGIS.Writeourresultsbacktoanewfeatureclass,witharc.write:

arc.write('data.gdb/new_features', results.df)

Page 46: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

BuildingRScriptTools

Page 47: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

RArcGISBridgeDemo

DetailsofmodelbasedclusteringanalysisintheRSampleTools

Page 48: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

HowToInstallInstallwiththeRbridgeinstallDetailedinstallationinstructions

Page 49: Connecting ArcGIS with R and Conda - 4326.us · from interactive environments like Spyder to Jupyter Notebooks. Environments — Can isolate an environment, flexibly make changes

WhereCanIRunThis?