Automating site suitability
Part 1: Exporting a tool as a Python script
Open your model from the in-class exercise from the previous class period
From the Model Builder menu, click on “Export -> Send to Python Window”
From the Model Builder menu, click on “Export -> Export to Python file”
Inspect the file in a text editor
Part 2: Adding parameters to a model
Open your model from the in-class exercise from the previous class period
From the Model Builder menu, click on the “Variable” icon
For the variable type, select “Coordinate System”
Then, right-click on the variable and select “Parameter”
Save your model. Then open the model by double clicking on it. You should see the Coordinate System parameter available.
Part 3: Further automation
- Run the following line of code from the console in RStudio to install necessary packages:
install.packages(c("tigris", "raster", "dplyr", "sf", "leaflet"))
- Follow along with the instructor by copying, pasting, and editing the code below:
library(tigris)
library(raster)
library(dplyr)
library(sf)
library(leaflet)
wi_dem <- raster("Q:/StudentCoursework/Haffnerm/DATA/DEM_30m/demgw930/")
plot(wi_dem)
site_suitability <- function(input_raster, county, slope_min, slope_max, asp_min, asp_max) {
## download counties data from US Census
counties <- counties(state = "Wisconsin", class = "sf", cb = TRUE, progress_bar = FALSE)
## filter county, transform crs
county <- counties %>%
filter(NAME == !!county) %>%
st_transform(., crs(input_raster))
## crop raster to area of interest
county_dem <- crop(wi_dem, county)
## create suitability raster
suit_rast <-
terrain(county_dem, opt = "slope", unit = "degrees") > slope_min &
terrain(county_dem, opt = "slope", unit = "degrees") < slope_max &
terrain(county_dem, opt = "aspect", unit = "degrees") > asp_min &
terrain(county_dem, opt = "aspect", unit = "degrees") < asp_max
## set plot parameters
par(mfrow = c(2,2))
## plot each step
plot(county_dem, main = "DEM")
plot(terrain(county_dem, opt = "slope", unit = "degrees") > slope_min &
terrain(county_dem, opt = "slope", unit = "degrees") < slope_max,
main = "Slope criteria",
col = c("white", "blue"))
plot(terrain(county_dem, opt = "aspect", unit = "degrees") > asp_min &
terrain(county_dem, opt = "aspect", unit = "degrees") < asp_max,
main = "Aspect criteria",
col = c("white", "orange"))
plot(suit_rast,
main = "Suitable areas",
col = c("white", "black"))
## reset plot parameters
par(mar = c(1,1,1,1))
return(suit_rast)
}
suitable_locations <- site_suitability(input_raster = _____,
county = _____,
slope_min = _____,
slope_max = _____,
asp_min = _____,
asp_max = _____)
leaflet(width = "100%") %>%
addTiles() %>%
addRasterImage(suitable_locations, opacity = 0.5, col = c("#ffffff", "#ff0000"))
Imagine you are seeking to build a solar farm in Buffalo County. Set the slope and aspect parameters properly to find suitable locations.