# Difference between revisions of "FUTURES tutorial"

Wenzeslaus (talk | contribs) (add See also) |
Wenzeslaus (talk | contribs) (→Workflow: add info about installing R pkgs) |
||

Line 44: | Line 44: | ||

<source lang="bash"> | <source lang="bash"> | ||

g.gui | g.gui | ||

+ | </source> | ||

+ | |||

+ | To install the required R packages, go to the command line terminal which was started with GRASS GIS and R: | ||

+ | |||

+ | <source lang="bash"> | ||

+ | R | ||

+ | </source> | ||

+ | |||

+ | This starts the R command line where you need to execute the following code (and go through any dialogs about installation if R shows them): | ||

+ | |||

+ | <source lang="R"> | ||

+ | install.packages(c("MuMIn", "lme4", "optparse", "rgrass7")) | ||

</source> | </source> | ||

## Revision as of 09:30, 24 April 2018

r.futures.* is an implementation of FUTure Urban-Regional Environment Simulation (FUTURES) which is a model for multilevel simulations of emerging urban-rural landscape structure. FUTURES produces regional projections of landscape patterns using coupled submodels that integrate nonstationary drivers of land change: per capita demand (DEMAND submodel), site suitability (POTENTIAL submodel), and the spatial structure of conversion events (PGA submodel).

This tutorial shows how to prepare data and run the model. You can download sample dataset and simulate urban growth in The Triangle, region in the Piedmont of North Carolina in the United States with rapidly growing cities Raleigh, Durham and Chapel Hill.

## Contents

# Software

Required software includes:

# Input data used in this tutorial

Download sample dataset containing:

- digital elevation model (NED)
- NLCD 2001, 2011
- NLCD 1992/2001 Retrofit Land Cover Change Product
- transportation network (TIGER)
- county boundaries (TIGER)
- protected areas (Secured Lands)
- cities above 20000 (USGS)
- county population past estimates and future projections (NC OSBM) per county (nonspatial)

# Workflow

Download sample data and unzip it. Create `grassdata` directory on your disk (or use the one you already have) and move there Location `futures_ncspm`.
Launch GRASS GIS and select your `grassdata` directory, Location `futures_ncspm` and Mapset `practice1`.

Install addons:

```
g.extension r.futures
g.extension r.sample.category
```

Close GUI and restart it:

```
g.gui
```

To install the required R packages, go to the command line terminal which was started with GRASS GIS and R:

```
R
```

This starts the R command line where you need to execute the following code (and go through any dialogs about installation if R shows them):

```
install.packages(c("MuMIn", "lme4", "optparse", "rgrass7"))
```

## Initial steps and data preparation

First we will set computational region of our analyses to an extent covering our study area and so that the cells are aligned with one of our landuse rasters:

```
g.region raster=landuse_2011 -p
```

We will derive urbanized areas from NLCD dataset for year 1992, 2001 and 2011 by extracting categories category 21 - 24 into a new binary map where developed is 1, undeveloped 0 and NULL (no data) is area unsuitable for development (water, wetlands, protected areas). First we will convert protected areas from vector to raster and set NULLs to zeros (for simpler raster algebra expression in the next step):

```
v.to.rast input=protected_areas output=protected_areas use=val
r.null map=protected_areas null=0
```

And then create rasters of developed/undeveloped areas using raster algebra:

```
r.mapcalc "urban_1992 = if(landuse_1992 >= 21 && landuse_1992 <= 24, 1, if(landuse_1992 == 11 || landuse_1992 >= 90 || protected_areas, null(), 0))"
r.mapcalc "urban_2001 = if(landuse_2001 >= 21 && landuse_2001 <= 24, 1, if(landuse_2001 == 11 || landuse_2001 >= 90 || protected_areas, null(), 0))"
r.mapcalc "urban_2011 = if(landuse_2011 >= 21 && landuse_2011 <= 24, 1, if(landuse_2011 == 11 || landuse_2011 >= 90 || protected_areas, null(), 0))"
```

We will convert vector counties to raster with the values of the FIPS attribute which links to population file:

```
v.to.rast input=counties type=area use=attr attribute_column=FIPS output=counties
```

If we need to run FUTURES only for smaller number of counties, we can extract them based on the identifier:

```
v.to.rast input=counties type=area where="FIPS in (37183, 37101)" use=attr attribute_column=FIPS output=counties_subset
```

Before further steps, we will set our working directory so that the input population files and text files
we are going to create are saved in one directory and easily accessible.
You can do that from menu *Settings* → *GRASS working environment* → *Change working directory*.
Select (or create) a directory and move there the downloaded files `population_projection.csv` and `population_trend.csv`.

## Potential submodel

Module r.futures.potential implements POTENTIAL submodel as a part of FUTURES land change model. POTENTIAL is implemented using a set of coefficients that relate a selection of site suitability factors to the probability of a place becoming developed. This is implemented using the parameter table in combination with maps of those site suitability factors (mapped predictors). The coefficients are obtained by conducting multilevel logistic regression in R with package lme4 where the coefficients may vary by county. The best model is selected automatically using dredge function from package MuMIn.

First, we will derive couple of predictors.

### Predictors

#### Slope

We will derive slope in degrees from digital elevation model using r.slope.aspect module:

```
r.slope.aspect elevation=elevation_30m slope=slope
```

#### Distance from lakes/rivers

First we will extract category *Open Water* from 2011 NLCD dataset:

```
r.mapcalc "water = if(landuse_2011 == 11, 1, null())"
```

Then we compute the distance to water with module r.grow.distance and set color table to shades of blue:

```
r.grow.distance input=water distance=dist_to_water
r.colors -n map=dist_to_water color=blues
```

#### Distance from protected areas

We will use raster protected of protected areas we already created, but we will set NULL values to zero. We compute the distance to protected areas with module r.grow.distance and set color table from green to red:

```
r.null map=protected_areas setnull=0
r.grow.distance input=protected_areas distance=dist_to_protected
r.colors map=dist_to_protected color=gyr
```

#### Forests

We will smooth the transition between forest and other land use, see NLCD category *Open Water*:

```
r.mapcalc "forest = if(landuse_2011 >= 41 && landuse_2011 <= 43, 1, 0)"
r.neighbors -c input=forest output=forest_smooth size=15 method=average
r.colors map=forest_smooth color=ndvi
```

#### Travel time to cities

Here we will compute travel time to cities (> population 20000)
as cumulative cost distance where cost is defined as travel time on roads.
First we specify the speed on different types of roads.
We copy the roads raster into our mapset so that we can change it
by adding a new attribute field `speed`. Then we assign speed values (km/h) based on the type of road:

```
g.copy vector=roads,myroads
v.db.addcolumn map=myroads columns="speed double precision"
v.db.update map=myroads column=speed value=50 where="MTFCC = 'S1400'"
v.db.update map=myroads column=speed value=100 where="MTFCC IN ('S1100', 'S1200')"
```

Now we rasterize the selected road types using the speed values from the attribute table as raster values.

```
v.to.rast input=myroads type=line where="MTFCC IN ('S1100', 'S1200', 'S1400')" output=roads_speed use=attr attribute_column=speed
```

We set the rest of the area to low speed and recompute the speed as time to travel through a 30m cell in minutes:

```
r.null map=roads_speed null=5
r.mapcalc "roads_travel_time = 1.8 / roads_speed"
```

Finally we compute the travel time to larger cities using r.cost:

```
r.cost input=roads_travel_time output=travel_time_cities start_points=cities_20000
r.colors map=travel_time_cities color=byr
```

#### Road density

We will rasterize roads and use moving window analysis (r.neighbors) to compute road density:

```
v.to.rast input=roads output=roads use=val
r.null map=roads null=0
r.neighbors -c input=roads output=road_dens size=25 method=average
```

#### Distance to interchanges

We will consider TIGER roads of type `Ramp` as interchanges, rasterize them and compute euclidean distance to them:

```
v.to.rast input=roads type=line where="MTFCC = 'S1630'" output=interchanges use=val
r.grow.distance -m input=interchanges distance=dist_interchanges
```

### Development pressure

We compute development pressure with r.futures.devpressure. Development pressure is a predictor based on number of neighboring developed cells within search distance, weighted by distance. The development pressure variable plays a special role in the model, allowing for a feedback between predicted change and change in subsequent steps.

```
r.futures.devpressure -n input=urban_2011 output=devpressure_1 method=gravity size=10 gamma=1 scaling_factor=1
r.futures.devpressure -n input=urban_2011 output=devpressure_0_5 method=gravity size=30 gamma=0.5 scaling_factor=0.1
```

When gamma increases, development influence decreases more rapidly with distance. Size is half the size of the moving window. When gamma is low, local development influences more distant places. We will derive 2 layers with different gamma and size parameters for the potential statistical model.

### Rescaling variables

First we will look at the ranges of our predictor variables by running a short Python code snippet in Python shell:

```
for name in ['slope', 'dist_to_water', 'dist_to_protected', 'forest_smooth', 'travel_time_cities', 'road_dens', 'dist_interchanges', 'devpressure_0_5', 'devpressure_1']:
minmax = grass.raster_info(name)
print name, minmax['min'], minmax['max']
```

We will rescale some of our input variables:

```
r.mapcalc "dist_to_water_km = dist_to_water / 1000"
r.mapcalc "dist_to_protected_km = dist_to_protected / 1000"
r.mapcalc "dist_interchanges_km = dist_interchanges / 1000"
r.mapcalc "road_dens_perc = road_dens * 100"
r.mapcalc "forest_smooth_perc = forest_smooth * 100"
```

### Sampling

To sample only in the analyzed counties, we will clip development layer:

```
r.mapcalc "urban_2011_clip = if(counties, urban_2011)"
```

To estimate the number of sampling points, we can use r.report to report number of developed/undeveloped cells and their ratio.

```
r.report map=urban_2011 units=h,c,p
```

We will sample the predictors and the response variable with 10000 random points in undeveloped areas and 2000 points in developed area:

```
r.sample.category input=urban_2011_clip output=sampling sampled=counties,devpressure_0_5,devpressure_1,slope,road_dens_perc,forest_smooth_perc,dist_to_water_km,dist_to_protected_km,dist_interchanges_km,travel_time_cities npoints=10000,2000
```

The attribute table can be exported as CSV file (not necessary step):

```
v.db.select map=sampling columns=urban_2011_clip,counties,devpressure_0_5,devpressure_1,slope,road_dens_perc,forest_smooth_perc,dist_to_water_km,dist_to_protected_km,dist_interchanges_km,travel_time_cities separator=comma file=samples.csv
```

### Development potential

Now we find best model for predicting urbanization using r.futures.potential which wraps an R script.

We can play with different combinations of predictors, for example:

```
r.futures.potential input=sampling output=potential.csv columns=devpressure_0_5,slope,road_dens_perc,forest_smooth_perc,dist_to_water_km,travel_time_cities developed_column=urban_2011_clip subregions_column=counties
r.futures.potential input=sampling output=potential.csv columns=devpressure_1,road_dens_perc,dist_to_water_km,dist_to_protected_km,travel_time_cities developed_column=urban_2011_clip subregions_column=counties
```

Or we can run R dredge function to find "best" model. We can specify minimum and maximum number of predictors the final model should use.

```
r.futures.potential -d input=sampling output=potential.csv columns=devpressure_1,slope,road_dens_perc,forest_smooth_perc,dist_to_water_km,dist_to_protected_km,dist_interchanges_km,travel_time_cities developed_column=urban_2011_clip subregions_column=counties min_variables=4 max_variables=7
```

You can then open the output file `potential.csv`, which is a CSV file with tabs as separators.

For this tutorial, the final potential is created with:

```
r.futures.potential input=sampling output=potential.csv columns=devpressure_1,slope,forest_smooth_perc,dist_interchanges_km,travel_time_cities developed_column=urban_2011_clip subregions_column=counties --o
```

## Demand submodel

First we will mask out roads so that they don't influence into per capita land demand relation.

```
v.to.rast input=roads type=line output=roads_mask use=val
r.mask roads_mask -i
```

We will use r.futures.demand which derives the population vs. development relation. The relation can be linear/logarithmic/logarithmic2/exponential/exponential approach. Look for examples of the different relations in the manual.

- linear: y = A + Bx
- logarithmic: y = A + Bln(x)
- logarithmic2: y = A + B * ln(x - C) (requires SciPy)
- exponential: y = Ae^(BX)
- exp_approach: y = (1 - e^(-A(x - B))) + C (requires SciPy)

The format of the input population CSV files is described in the manual. It is important to have synchronized categories of subregions and the column headers of the CSV files (in our case FIPS number). How to simply generate the list of years (for which demand is computed) is described in r.futures.demand manual, for example run this in Python console:

```
','.join([str(i) for i in range(2011, 2036)])
```

Then we can create the DEMAND file:

```
r.futures.demand development=urban_1992,urban_2001,urban_2011 subregions=counties observed_population=population_trend.csv projected_population=population_projection.csv simulation_times=2011,2012,2013,2014,2015,2016,2017,2018,2019,2020,2021,2022,2023,2024,2025,2026,2027,2028,2029,2030,2031,2032,2033,2034,2035 plot=plot_demand.pdf demand=demand.csv separator=comma
```

In your current working directory, you should find files `plot_demand.png` and `demand.csv`.

If you have SciPy installed, you can experiment with other methods for fitting the functions:

```
r.futures.demand ... method=logarithmic2
r.futures.demand ... method=exp_approach
```

If necessary, you can create a set of demand files produced by fitting each method separately and then pick for each county the method which seems best and manually create a new demand file.

When you are finished, remove the mask as it is not needed for the next steps.

```
r.mask -r
```

## Patch calibration

Patch calibration can be a very time consuming computation. First we derive patches of new development by comparing historical and latest development. We can run this on the entire area and keep only patches with minimum size 2 cells (1800 = 2 x 30 x 30 m).

```
r.futures.calib development_start=urban_1992 development_end=urban_2011 subregions=counties patch_sizes=patches.txt patch_threshold=1800 -l
```

We obtained a file `patches.txt` (used later in the PGA) - a patch size distribution file - containing sizes of all found patches.

We can look at the distribution of the patch sizes:

```
from matplotlib import pyplot as plt
with open('patches.txt') as f:
patches = [int(patch) for patch in f.readlines()]
plt.hist(patches, 2000)
plt.xlim(0,50)
plt.show()
```

At this point, we start the calibration to get best parameters of patch shape (for this tutorial, this step can be skipped and the suggested parameters are used). First we select only one county to

```
v.to.rast input=counties type=area where="FIPS == 37183" use=attr attribute_column=FIPS output=calib_county
g.region raster=calib_county zoom=calib_county
```

```
r.futures.calib development_start=urban_1992 development_end=urban_2011 subregions=calib_county patch_sizes=patches.txt calibration_results=calib.csv patch_threshold=1800 repeat=5 compactness_mean=0.1,0.3,0.5,0.7,0.9 compactness_range=0.1,0.05 discount_factor=0.1,0.3,0.5,0.7,0.9 predictors=slope,forest_smooth_perc,dist_interchanges_km,travel_time_cities demand=demand.csv devpot_params=potential.csv num_neighbors=4 seed_search=2 development_pressure=devpressure_1 development_pressure_approach=gravity n_dev_neighbourhood=10 gamma=1 scaling_factor=1 incentive_table=incentive_table.txt --o
```

## FUTURES simulation

We will switch back to our previous region:

```
g.region raster=landuse_2011 -p
```

Now we have all the inputs necessary for running r.futures.pga:

The entire command is here:

```
r.futures.pga subregions=counties developed=urban_2011 predictors=slope,forest_smooth_perc,dist_interchanges_km,travel_time_cities devpot_params=potential.csv development_pressure=devpressure_1 n_dev_neighbourhood=10 development_pressure_approach=gravity gamma=1 scaling_factor=1 demand=demand.csv discount_factor=0.3 compactness_mean=0.2 compactness_range=0.1 patch_sizes=patches.txt num_neighbors=4 seed_search=2 random_seed=1 output=final output_series=final
```

### Quick description

The command parameters and their values:

- raster map of counties with their categories

```
... subregions=counties ...
```

- raster of developed (1), undeveloped (0) and NULLs for undevelopable areas

```
... developed=urban_2011 ...
```

- predictors selected with r.futures.potential and their coefficients in a
`potential.csv`file. The order of predictors must match the order in the file and the categories of the counties must match raster`counties`.

```
... predictors=slope,forest_smooth_perc,dist_interchanges_km,travel_time_cities devpot_params=potential.csv ...
```

- initial development pressure computed by r.futures.devpressure, it's important to set here the same parameters

```
... development_pressure=devpressure_1 n_dev_neighbourhood=10 development_pressure_approach=gravity gamma=1 scaling_factor=1 ...
```

- per capita land demand computed by r.futures.demand, the categories of the counties must match raster
`counties`.

```
... demand=demand.csv ...
```

- patch parameters from the calibration step

```
... discount_factor=0.3 compactness_mean=0.4 compactness_range=0.08 patch_sizes=patches.txt ...
```

- recommended parameters for patch growing algorithm

```
... num_neighbors=4 seed_search=2 ...
```

- set random seed for repeatable results or set flag
`-s`to generate seed automatically

```
... random_seed=1 ...
```

- specify final output map and optionally basename for intermediate raster maps

```
output=final output_series=final
```

## Scenarios

Scenarios involving policies that encourage infill versus sprawl can be explored using the `incentive_table` parameter,
which uses a power function to transform the evenness of the probability gradient in POTENTIAL.

It's easy to generate one using python commands as shown on r.futures.pga module manual page:

```
import numpy as np
power = 1
length = 1001
x = np.linspace(0, 1, length)
np.savetxt(fname="incentive_table.txt", fmt='%1.3f', delimiter=',', X=np.column_stack((x, np.power(x, power))), header=",{}".format(length), comments='')
```

Paste those commands for example to the Python Shell in GRASS GIS. You can change the power to a number between 0.25 and 4 to test urban sprawl/infill scenarios. Higher power leads to infill behavior, lower power to urban sprawl.

## Postprocessing

Follow a tutorial how to make an animation from the results.