Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence:...

87
Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial ̶̶̶̶ Share Alike http://creativecommons.org/licenses/by-nc-sa/2.5/

Transcript of Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence:...

Page 1: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Spatial Dynamical Modeling with TerraME

Tiago CarneiroGilberto CâmaraPedro Andrade

Licence: Creative Commons ���� By Attribution ���� Non Commercial ���� Share Alikehttp://creativecommons.org/licenses/by-nc-sa/2.5/

Page 2: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

What models are needed to describe human actions?

Modelling human-environment interactions

Page 3: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Clocks, clouds or ants?

Clocks: deterministic equations

Clouds: statistical distributions

Ants: emerging behaviour

Page 4: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

f ( It+n )

. . FF

f (It) f (It+1) f (It+2)

Dynamic Spatial Models

“A dynamical spatial model is a computational representation of a real-world process where a location on the earth’s surface changes in response to variations on external and internal dynamics on the landscape” (Peter Burrough)

Page 5: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Nature-society modelling with TerraME

Nature: Physical equations Describe processes

Society: Decisions on how to Use Earth´s resources

Page 6: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Nature-society modelling with TerraME

Nature: Physical equations Describe processes

Society: Decisions on how to Use Earth´s resources

Nature: Cellular space Society: Agents

Page 7: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Agen

t

Spa

ce

Space Agent

Benenson and Torrens, “Geographic Automata Systems”, IJGIS, 2005(but many questions remain...)

Modelling collective spatial actions

Page 8: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Computational Modelling with Cell SpacesCell Spaces

Generalized Proximity Matriz – GPM

Hybrid Automata model

Nested scales

Agents in space

Page 9: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

TerraME - overview

Model data in cell spaces

Read/write data from a database

Page 10: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

2500 m 2.500 m e 500 m

Cellular Data Base Resolution

Page 11: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Large farmer (25 cells)

500 m (all)

Small farmer (2 cells)

Spatial structure

Page 12: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Behavior is non-homogeneous in space and time

Page 13: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Phase transitions

Newly implanted

Deforesting

Slowing down

latency > 6 years

Deforestation > 80%

Iddle

Year of creation

Deforestation = 100%

Multi-scale modelling

Express anisotropy

y=a0 + a1x1 + a2x2 + ... +aixi +E

Statistics and agents

Page 14: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Tools for observing simulations

Page 15: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

TerraME functionality

Eclipse & LUA plugin• model description• model highlight syntax

TerraView• data acquisition• data visualization• data management• data analysis

TerraLibdatabase

da

ta

Model source code

MODEL DATA

mod

el

• model syntax semantic checking• model execution

TerraME INTERPRETER

LUA interpreter

TerraME framework

TerraME/LUA interface

model d

ata

Page 16: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

TerraLib: spatio-temporal database as a basis for innovation

Visualization (TerraView)

Spatio-temporalDatabase (TerraLib)

Modelling (TerraME)

Data Mining(GeoDMA)Statistics (aRT)

G. Câmara et al.“TerraLib: An open-source GIS library for large-scale environmental and socio-economic applications”. In: B. Hall, M. Leahy (eds.), “Open Source Approaches to Spatial Data Handling”. Berlin, Springer, 2008.

Page 17: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

TerraLib

TerraME C++ Framework

C++ Signal Processing

librarys

C++ Mathematical

librarys

C++ Statistical

librarys

TerraML Virtual Machine

TerraME: Software Architecture

TerraMLCompiler

TerraML Language

Model 1 Model 2 Model 3 Model 4

Page 18: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Lua and the Web

Where is Lua?

Inside Brazil Petrobras, the Brazilian Oil Company Embratel (the main telecommunication company in

Brazil) many other companies

Outside Brazil Lua is used in hundreds of projects, both

commercial and academic CGILua still in restricted use

until recently all documentation was in Portuguese

TerraME Programming Language: Extension of Lua Lua is the language of choice for computer games

[Ierusalimschy et al, 1996]source: the Lua team

Page 19: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Lua

Roberto Ierusalimschy

PUC-Rio, Brazil

Page 20: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

What is Lua?

4 Yet Another Scripting Language

4 an “extension” language

4 implemented as a library in ANSI C

HostProgram

LuaInterpreter

-- a Lua scriptcolor = REDb = button { label = ‘OK’, x = 10, y = 20}

Page 21: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Why Lua?

4 Simple and flexible 8“Simple things simple, complex things possible”

4 Small, Efficient, Portable 8Whole library written in ANSI C, compiles the same

source code in all platforms8Typical uses: MS-DOS, Windows (3.1, 95, NT), Unix (Linux,

Solaris, IRIX, AIX, ULTRIX), Next, OS/2, Mac

Page 22: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

How is Lua?

4 Pascal-like Syntax.

4 Interpreter executes sequence of statements.8 function definitions are also statements (see later)

4 Six types: numbers, tables, functions, strings, userdata, nil

function fat (n) if n == 0 then return 1 else return n*fat(n-1) endend

Page 23: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Variables and Values Case sensitive

semicolon may optionally follow any statement

a = 1

b = a*2

print(a)

print(b)

Page 24: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Comments double hyphen (--) until the end of the line.

block comments start with --[[ and run until ]]

print("hello") -- my comment

-- print("hello”)

--[[

print(10) -- no action (comment)

--]]

Page 25: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

My first Lua program C = 2 -- rain/t

K = 0.4 -- flow coefficient

q = 0

-- RULES

for time = 0, 20, 1 do

-- soil water

q = q + C - K*q

end

print(“q = "..q)

Page 26: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Types

Page 27: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Type nil

Different from everything else

Default variable type

Also acts as false (boolean)

Page 28: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Type boolean

4Comparison value

8if (rain == true) then ....

Page 29: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

boolean false/true nil and false are false, everything else is true zero and the empty string are true operators and, or, and not

print(true and false)print(true and (false or true))print(false or (true and false) or (true and true))

Page 30: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

number

the only type for numeric values double-precision floating-point number arithmetic operators: +, –, *, / exponent (^) and modulus (%) boolean operators (<, >, <=, >=, ~=, and ==)

A = 6 + 2.2 * 4e+3a = A ^ 2b = A % 7print(a > b)

print(b ~= 2)

Page 31: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

ParenthesesAlways optional (except in the case of function call)

When in doubt, use parentheses

a+-i < b/2+1 <--> (a + (-i)) < ((b/2)+1)

5+x^2*8 <--> 5 + ( (x^2)*8 )

a < y and y <= z <--> (a < y) and (y <= z)

–x^y^z <--> –(x^(y^z))

Page 32: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Type string

4 Immutable

4 No size limit (read large files as strings)

4 No termination value (‘\0’)

4 Powerful Pattern-matching in standard library

8myname = “Werner Kuhn”;

Page 33: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

if statement

An if statement tests condition and executes its then-part or its else-part (optional) accordingly

a = 6; b = 5

if a < b then print("a < b")

elseif a < b + 5 thenprint("b <= a < b+5")

elseprint("a > b+5")

end

Page 34: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

for statementfor var = exp1, exp2, exp3 dosomething

end Execute something for each value of var from exp1 to exp2,

using exp3 as the step to increment var. This third expression is optional (default is 1).

for i = 1, 10 doprint(i)

endfor i = 1, 10, 2 doprint(i)

end

Page 35: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Tables

4 Implement associative arrays:8any value (including functions and other tables) can be

used both for indices and values

t = {} -- creates an empty tablet[1] = "hello"t.x = print -- t.x is sugar for t[‘x’]t.x(t[1]) -- prints ‘hello’t.next = t -- circular list

Page 36: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

table Tables can be indexed not only with numbers, but also with

strings or any other value of the language, except nilloc = {cover = "forest",distRoad = 0.3,distUrban = 2

}

print(loc["cover"])print(loc.cover)loc.distRoad = loc.distRoad^2loc.distTotal = loc.distRoad + loc.distUrbanloc.deforestationPot = 1/loc.distTotal

Page 37: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Tables within tables

loc = { cover = "forest",

dist = {road = 0.3, urban = 2}

}

print(loc.dist.road)

loc.dist.total = loc.dist.road + loc.dist.urban

print(loc.dist.total)

Page 38: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Constructors: Create and init tables

4 Record style8point={x=10,y=20}8print(point.y) --> 20

4 List style8days={"Sun","Mon","Tue","Wed”, Sat"}8print(days[3]) --> Tue

4 Mixed style8points={{x=0,y=0}, point, n=2}8print(points[points.n].y) --> 20

Page 39: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Lua and the Web

Constructors

article{ author="F.P.Brooks", title="The Mythical Man-Month", year=1975,}

news = { {text = "New version 2.0", date = "21/05/1997"}, {text = "New example", date = "21/05/1997"}, {text = "New version: 2.1",date = "17/06/1997"},}

calls function“article”

Page 40: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

function

A function can carry out a specific task (commonly called procedure) or compute and return values.

A function is a first-class value in Lua. Functions can be stored in variables and in tables, can be

passed as arguments, and can be returned by other functions, giving great flexibility to the language.

myprint = printprint = nilmyprint(2)print = myprint

Page 41: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Functions in Luafunction fat (n)

if n == 0 then

return 1

else

return n*fat(n-1)

end

end

Page 42: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Higher-order Functions Functions can also be parameters to other functions. This kind

of function is what we call a higher-order function.

function foreach(tab, func)

for position, value in pairs(tab) dofunc(value, position)

endendx = {7, 3, 2, 6, 4}foreach(x, function(element)print(element)

end)foreach(x, function(value, position)print(position, value)

end)

Page 43: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Lua and the Web

Functions in Lua

4 First class values

function inc (x) return x+1end

inc = function (x) return x+1 end

sugar

clone = {}foreach(t, function (i,e) clone[i]=e end)

4 Example: cloning a table t

Page 44: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Functions and Tables

w = { redraw = function () ... end, pick = function (x,y) ... end,}

if w.pick(x,y) then w.redraw()end

Page 45: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Tables with functionsTables may have their own functions.

loc = {cover = "forest", distRoad = 0.3, distUrban = 2,deforestPot = function(myloc)

return 1/(myloc.distRoad + myloc.distUrban)

end}

print(loc.deforestPot(loc))print(loc:deforestPot())

Page 46: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Tables with functions4 We can declare a “class” in Lua by creating a function that

takes a table constructor as argument.

function MyLocation(locdata)locdata.covertype = "forest"locdata.deforPot = function(self) return 1/(self.distRoad + self.distUrban)endreturn locdata

end

loc = MyLocation({distRoad = 0.3, distUrban = 2})loc = MyLocation{distRoad = 0.3, distUrban = 2}print(loc.covertype)print(loc:deforPot())

Page 47: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Tables x Objects

4 Tables are dynamically created objects.

list

value - vnext -

old list...

list = {value=v, next=list}

Page 48: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Objects4 First-class functions+ tables = almost OO

8Tables can have functions as fields

4 Sugar for method definition and call8 Implicit parameter self

a.foo(a,x)a:foo(x)

a.foo = function (self,x) ...end

function a:foo (x) ...end

sugar

sugar

Page 49: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

My second Lua programC = 2; -- rain/tK = 0.4; -- flow coefficientq = 0; --function rain (t) if (t < 10) then

return 4 – 4*math.cos(math.pi*t/10);else

return 4 – 4*math.cos(math.pi*(t-10)/10); endend--for time = 0, 20, 1 do

-- soil waterq = q + rain(time) - K*q;

end-- report

print(“q = "..q);

Page 50: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Standard libraries

BasicStringTableMathIOOSDebugCoroutine

Page 51: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

TerraME: Vision

Nature: represented by a cellular space

Society: represented by agents

Several interacting entities share the same spatiotemporal structure.

Page 52: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

rainrain rain

N

Itacolomi do ItambéPeak Lobo’s Range

My third Lua program

Define a two-dimensional gridMake it rain on the gridLet water flow downwards

Page 53: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

TerraME Runtime Environment

Eclipse & LUA plugin• model description• model highlight syntax

TerraView• data acquisition• data visualization• data management• data analysis

TerraLibdatabase

da

ta

Model source code

MODEL DATA

mod

el

• model syntax semantic checking• model execution

TerraME INTERPRETER

LUA interpreter

TerraME framework

TerraME/LUA interface

model d

ata

Page 54: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

TerraME allows nested scales

Page 55: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Nested scales are necessary for human-environment models

Diverse space partitions can have different scales

Page 56: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Cellular Space

A geographical area of interest, divided into a grid. Each cell in the grid has one or more attributes. Stored and retrieved from a TerraLib database

Page 57: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Loading Data

-- Loads the TerraLib cellular spacecsCabecaDeBoi = CellularSpace{

dbType = "ADO",host = “localhost",database = "c:\\cabecaDeBoi.mdb",user = "",password = "",layer = "cellsLobo90x90",theme = "cells",select = { “height", “soilWater", “capInf" }

}csCabecaDeBoi:load();

csCabecaDeBoi:loadMooreNeighbourhood;

GIS

Page 58: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Creating temporary cellular spaces

game = CellularSpace { xdim = N, ydim = N }

Page 59: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Referencing cells

A CellularSpace has a special attribute called cells. It is a one-dimensional table of references for each Cell in the CellularSpace

-- c is the seventh cell in the cellular space

c = csCabecaDeBoi.cells[ 7 ];-- Updating the attribute “infcap” from the seventh cell

c.infcap = 10;print (csCabecaDeBoi.cells[7].infCap);

Page 60: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Database management-- loads a cellular spacecsAmazonia:load()csAmazonia:loadNeighbourhood("Moore")-- save (time, themeName, attrTableName) -- for time = 1, 10,1 do csAmazonia:save(time, “sim", {"water"})end

Eclipse & LUA plugin• model description• model highlight syntax

TerraView• data acquisition• data visualization• data management• data analysis

TerraLibdatabase

Model source code

MODEL DATA

• model syntax semantic checking• model execution

TerraME INTERPRETER

LUA interpreter

TerraME framework

TerraME/LUA interface

Page 61: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

The Cell type

A Cell value has two special attributes: latency and past. The latency attribute registers the period of time since the last

change in a cell attribute value.The past attribute is a copy of all cell attribute values in the

instant of the last change.

if(cell.cover == "abandon" and cell.latency >= 10) then cell.cover = "secFor"

endcell.water = cell.past.water + 2

Page 62: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Traversing a Cell Space

forEachCell(cs, function())

Applies the chosen function to each cell of the cellular space. This function enables using different rules in a cellular space.

forEachCell(csQ, function(cell)

cell.Water = cell.past.Water + 2

return trueend

)

Page 63: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Von Neumann Neighborhood

Moore Neighborhood

Isotropic neighbourhoods in cell spaces

Page 64: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Traversing a Neighbourhood

csq:loadNeighbourhood(“Moore”);forEachCell(csQ, function(cell)

count = 0; forEachNeighbour(cell, 0, function(cell, neigh)

if (neigh.past.value == 1 and neigh ~= cell) then count = count + 1; end end; ); -- for each neighbor

Page 65: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

for i, cell ipairs( csValeDoAnary ) do

end

count = 0 ;

print(“Number of deforested cells: ”.. count);

if ( cell.past.sim_cover == 1 ) then

cell.sim_cover = 0;

count = count + 1 ;

end

cell.synchronize( );

Synchronizing a cell space

tntn+1

rule

?

Page 66: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Synchronizing a cell space

tntn+1

rule

TerraME keeps two copies of a cellular space in memory: one stores the past values of the cell attributes, and another stores the current (present) values of the cell attributes.

The model equations must read (the right side of the equation rules) the past copy, and must write (the left side of the equation rules) the values to the present copy of the cellular space.

At the correct moment, it will be necessary to synchronize the two copies of the cellular space, copying the current attribute values to the past copy of the cellular space

Page 67: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Synchronization

Always read from the pastAlways write to the present….csQ:syncronize();

Page 68: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Trajectories: spatial patterns of change

modeller defined functions which map indexes (atributtes) to geo-objects (cells).

it = Trajectory{ myCellSpace, function(cell) return cell.cover == "forest“ end, function( c1, c2 )

return c1.dist_roads < c2.dist_roads end}

Page 69: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Which objects are nearest to each other?

Page 70: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Using Generalized Proximity Matrices (GPM)

Consolidated area Emergent area

Page 71: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

TerraME neighborhoods are graphs

Euclidean space Open network Closed network

D2

D1

[Aguiar et al., 2003]

Page 72: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Create or load neighborhoods-- Create a Moore neighborhoodcreateMooreNeighborhood( myCellSpace, “neighName” )

-- Create a 3x3 neighborhoodcreate3x3Neighborhood(myCellSpace, filterF() , weightF(),

name )

-- Create a MxN neighborhoodcreateMxNNeighborhood( M, N, myCellSpace,filterF(),

weightF(), name )

-- Load neighborhood from TerraLib databasemyCellSpace: loadTerraLibGPM(“myGPM");-- Load neighborhood from TerraLib GAL filesmyCellSpace:loadGALNeighborhood("c:\\myNeigh.gal")

Page 73: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Building neighborhoods between cell spaces

spatialCoupling( M, N, cs1,cs2, filterF, weightF, name )

filterF(cell, neigh) Boolean

wheighF(cell, neigh) Real

Page 74: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Example: neighborhood to simulate rain

-- Creates a 3x3 Neighborhood based on the cell "slope"-- only lower neighbors are consideredcreate3x3Neighborhood(

csQ,function(cell,neigh)

return neigh.altimetry < cell.altimetryend,function(cell, neigh)

return (cell.altimetry - neigh.altimetry)/(cell.altimetry + neigh.altimetry)

end,"slope"

);

Page 75: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

“GPM” PluginTerraView 3.2.0

“FillCell” PluginTerraView 3.2.0

TerraME integration with GIS (TerraView)

TerraLib Database

Page 76: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Conversion from GIS data to cell spaces

Vector geospatial data

Cell space

Real world

Page 77: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

The mixed pixel problem

How can you transform from vectors to cell attributes?

Page 78: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Fill the attributes of the cell spaces

For each data type to be transformed, there are appropriate operations

Page 79: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Using “FillCell” plugin to build Cell Spaces

1. Install the FillCell plugin: Copy the file "celulas.dll" to the directory "C: \ Program Files \ TerraView3.2.0 \ plugins".2. Build the cell space with the desired resolution

Page 80: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Filling Cells from vector data

Numerical areas (polygons, cells)

Categorical areas (polygons, cells)

Lines and points

Min, max, average, sum, standard dev Majority class (by number or by area) Percentage of each class, Percentage of majority class, area of majority class

Average/Sum intersection-weighted Presence, minimum distance, count

Page 81: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

rainrain rain

N

Itacolomi do ItambéPeak Lobo’s Range

Page 82: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Picture direction

Itacolomido Itambé Peak

Lobo’s Range

Page 83: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Demo: Rain Drainage Model

Database: c:\\TerraME\\Database\\CabecadeBoi.mdb Model: c:\\TerraME\\Modelos\\demo4_chuva_geoBD.lua Model: c:\\TerraME\\Modelos\\demo7_chuva_geoBD.lua

Page 84: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

SimulationResult(36 min.)

Page 85: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Demo: Fire propagation

Database: c:\\TerraME\\Database\\db_emas.mdbModel: c:\\TerraME\\Modelos\\demo6_FireSpreadModel.lua

CA 1 CA 2 CA 3 CA 4 CA 5

CA 1CA 1 0.1000.100 0.2500.250 0.2610.261 0.2730.273 0.2850.285

CA 2CA 2 0.1130.113 0.2530.253 0.2640.264 0.2760.276 0.2880.288

CA 3CA 3 0.1160.116 0.2560.256 0.2670.267 0.2790.279 0.2910.291

CA 4CA 4 0.1190.119 0.2590.259 0.2700.270 0.2820.282 0.2940.294

CA 5CA 5 0.1220.122 0.2620.262 0.2730.273 0.2850.285 0.2970.297

QUEIMANDO

INER

TE

Page 86: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

Demo: Amazon deforestation

Database: c:\\TerraME\\Database\\amazonia.mdbModel: c:\\TerraME\\Modelos\\demo3_desflorestamento_save.lua

Page 87: Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.

References

Carneiro, T., 2006. Nested-CA: a foundation for multiscale modeling of land use and land change., in PhD Thesis in Computer Science. National Institute of Space Research: São José dos Campos, Brazil.

Carneiro, T.; Câmara, G., 2007. A Gentle Introduction to TerraME. INPE Report, 2007.

Ierusalimschy, R. 2006. Programming in Lua (2nd edition). Rio de Janeiro, Lua.Org.