Presentation is loading. Please wait.

Presentation is loading. Please wait.

Spatial Dynamical Modeling with TerraME

Similar presentations


Presentation on theme: "Spatial Dynamical Modeling with TerraME"— Presentation transcript:

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

2 Dynamic Spatial Models
f (It) f (It+1) f (It+2) f ( It+n ) F F . . “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)

3 Computational Modelling with Cell Spaces
Generalized Proximity Matriz – GPM Hybrid Automata model Nested scales Agents in space

4 TerraME - overview Model data in cell spaces Read/write data
from a database

5 Cellular Data Base Resolution
2500 m 2.500 m e 500 m

6 TerraME functionality

7 TerraME: Software Architecture
Model 1 Model 2 Model 3 Model 4 TerraML Language TerraMLCompiler TerraML Virtual Machine TerraME C++ Framework C++ Signal Processing librarys C++ Mathematical librarys C++ Statistical librarys TerraLib

8 TerraLib: spatio-temporal database as a basis for innovation
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. Visualization (TerraView) Modelling (TerraME) Spatio-temporal Database (TerraLib) Data Mining(GeoDMA) Statistics (aRT)

9 TerraME integration with GIS (TerraView)
“GPM” Plugin TerraView 3.2.0 TerraLib Database “FillCell” Plugin TerraView 3.2.0 9

10 Conversion from GIS data to cell spaces
Real world Vector geospatial data Cell space

11 The mixed pixel problem
How can you transform from vectors to cell attributes?

12 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

13 Fill the attributes of the cell spaces
For each data type to be transformed, there are appropriate operations

14 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

15 Roberto Ierusalimschy PUC-Rio, Brazil
Lua Roberto Ierusalimschy PUC-Rio, Brazil PUC-Rio is the Catholic University of Rio de Janeiro. Our Computer Science Department ("Departamento de Informatica") is considered the best one in Brazil. I am an associate professor there. Lua is the work of Waldemar Celes, Luiz H. Figueiredo and me, in no particular order. CGILua is the work of Renato Borges, Anna Hester and me; Anna and Renato are graduate students of mine...

16 What is Lua? Yet Another Scripting Language an “extension” language
implemented as a library in ANSI C Host Program Lua Interpreter -- a Lua script color = RED b = button { label = ‘OK’, x = 10, y = 20} Lua lives in a niche close to Tcl, Perl and Python. From its kind of use, Tcl is the closer; both Tcl and Lua were created as an embedded language (in opposition to Perl and Python). Both are small and "slim". Lua seems to be more appropriate than Tcl for real programs, and then gets as an alternative to Python and Perl. (In fact, I don't know whether Python is really a scripting language; it seems a very nice language, but a little "heavy" for real "quick-and-dirty" programming.) Lua is much smaller than Perl, on the other hand it has a much smaller library. I would say Perl is more appropriate for systems administration (standard Lua has no Posix facilities, only ANSI C), but is really for hackers.

17 Why Lua? Simple and flexible Small, Efficient, Portable
“Simple things simple, complex things possible” Small, Efficient, Portable Whole library written in ANSI C, compiles the same source code in all platforms Typical uses: MS-DOS, Windows (3.1, 95, NT), Unix (Linux, Solaris, IRIX, AIX, ULTRIX), Next, OS/2, Mac Lua is smaller, faster and more portable than Tcl, Perl and Python. Simplicity and flexibility are more arguable qualities...

18 Where is Lua? Inside Brazil Outside 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 CGILua is also spread, but mainly in Brazil. CGILua has got a commercial impact much bigger than Lua, and that created some pressure for documentation in Portuguese. Only recently we found the time to translate it to English...

19 How is Lua? Pascal-like Syntax.
function fat (n) if n == 0 then return 1 else return n*fat(n-1) end Pascal-like Syntax. Interpreter executes sequence of statements. function definitions are also statements (see later) Six types: numbers, tables, functions, strings, userdata, nil

20 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);

21 Types

22 Type nil Different from everything else Default variable type
Also acts as false (boolean)

23 Type boolean Comparison value if (rain == true) then ....

24 Type number Unique native type for numbers double (by default) a = 3
c = 4.5e-8

25 Type string Immutable No size limit (read large files as strings)
No termination value (‘\0’) Powerful Pattern-matching in standard library myname = “Werner Kuhn”;

26 Tables Implement associative arrays:
any value (including functions and other tables) can be used both for indices and values t = {} creates an empty table t[1] = "hello" t.x = print t.x is sugar for t[‘x’] t.x(t[1]) prints ‘hello’ t.next = t circular list The first line creates an empty table, and stores a reference to it in “t”. Then the string “hello” is associated with index 1, and the function print is associated with index “x”.

27 Table loc = { cover = "forest", distRoad = 0.3, distUrban = 2 };
loc.cover = “cerrado”; loc[“cover”] = “soja”; if (loc.distUrban > 1.5) then

28 Tables in Lua loc = { cover = "forest", distRoad = 0.3, distUrban = 2 }; loc.desfPot = loc.distRoad + loc.distUrban;

29 Constructors: Create and init tables
Record style point={x=10,y=20} print(point.y) --> 20 List style days={"Sun","Mon","Tue","Wed”, Sat"} print(days[3]) --> Tue Mixed style points={{x=0,y=0}, point, n=2} print(points[points.n].y) --> 20 Constructors were inspired by some “declarative” languages, such as BibTex and IDL (a language for describing interfaces in Motif). Note in the last example a use of nested constructors: because a constructor is an expression, it can be used wherever a value is expected.

30 Constructors article{ author="F.P.Brooks",
calls function “article” article{ author="F.P.Brooks", title="The Mythical Man-Month", year=1975, } The syntax “foo{...}” is only syntactic sugar for “foo({...})”, that is, a table is created by the constructor and then passed as argument to the function “foo”. Typically, such a function can check if the fields are correct, supply default values to some fields, and register the table in same global place. Many people write those kind of declarations in an application (a typical example is for describing a GU interface), and don’t even know they are “programming” in Lua... 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"}, } Lua and the Web

31 Functions in Lua function fat (n) if n == 0 then return 1 else
return n*fat(n-1) end

32 Functions in Lua First class values Example: cloning a table t
function inc (x) return x+1 end inc = function (x) sugar Example: cloning a table t In the last example, “foreach” is a predefined function that applies a given function over all elements of a given table. For each element, the function is called with two arguments, the index and the value. Functions as first class values is a key mechanism in Lua; albeit few people do functional programming in Lua, this facility is used for Object-oriented programming in Lua, for redefining functions, and many other goods things. We will see some of them later. clone = {} foreach(t, function (i,e) clone[i]=e end) Lua and the Web

33 Functions and Tables w = { redraw = function () ... end,
pick = function (x,y) ... end, } if w.pick(x,y) then w.redraw() end In the last example, “foreach” is a predefined function that applies a given function over all elements of a given table. For each element, the function is called with two arguments, the index and the value. Functions as first class values is a key mechanism in Lua; albeit few people do functional programming in Lua, this facility is used for Object-oriented programming in Lua, for redefining functions, and many other goods things. We will see some of them later.

34 Tables x Objects Tables are dynamically created objects.
list = {value=v, next=list} list old list ... This is a big difference between tables in Lua and associative arrays in other languages, such as AWK and Tcl. Tables in Lua are “objects”, in the sense of Hoare, that is, dynamically allocated “things” manipulated through pointers to them. Tables in Lua can directly represent dynamic structures such as trees and graphs, even cyclic structures. Moreover, tables allow an Object-Oriented style of programming in Lua; see later. value - v next -

35 Objects First-class functions+ tables = almost OO
Tables can have functions as fields Sugar for method definition and call Implicit parameter self This is only to illustrate how OO can be done in Lua. There is much more to say about that, but CGILua does not use those facilities, so this is not our focus here. Specifically, in the above scheme we miss inheritance. Lua does not provide inheritance, but it is very easy to implement delegation. With delegation, we can do a “prototype-base” OO programming. a.foo(a,x) a:foo(x) a.foo = function (self,x) ... end function a:foo (x) sugar

36 My second Lua program C = 2; -- rain/t K = 0.4; -- flow coefficient
q = 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); end -- for time = 0, 20, 1 do -- soil water q = q + rain(time) - K*q; -- report print(“q = "..q);

37 Standard libraries Basic String Table Math IO OS Debug Coroutine

38 Basic Basic functions print type setmetatable pairs

39 String String manipulation pattern matching string.find string.gsub

40 Table Function for table manipulation table.insert table.remove
table.sort

41 My third Lua program rain rain rain Define a two-dimensional grid
Itacolomi do Itambé Peak Lobo’s Range My third Lua program Define a two-dimensional grid Make it rain on the grid Let water flow downwards N

42 TerraME: Vision An Earth´s environment can be represented as a synthetic environment where analytical entities (rules) change the space properties in time. Several interacting entities share the same spatiotemporal structure. 42

43 TerraME architecture & applications
RondôniaModel DinamicaModel TROLLModel CLUEModel TerraME Language TerraME Compiler TerraME Virtual Machine TerraLib Enviromental Modeling Framework C++ Signal Processing librarys C++ Mathematical librarys C++ Statistical librarys TerraLib 43

44 TerraME Runtime Environment
44

45 The Scale Concept in TerraME
Scale is a generic concept that includes the spatial, temporal, or analytical dimensions used to measure any phenomenon. Extent refers to the magnitude of measurement. Resolution refers to the granularity used in the measures. (Gibson et al. 2000)

46 TerraME allows nested scales

47 Nested scales are necessary for human-environment models
Diverse space partitions can have different scales 47

48 TerraME extensions to Lua
To build spatial dynamic models, TerraME includes new value types in LUA using the constructor mechanism. These values are: CellularSpace, Cell, Neighbourhood

49 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

50 Loading Data -- Loads the TerraLib cellular space
GIS -- Loads the TerraLib cellular space csCabecaDeBoi = CellularSpace { dbType = "ADO", host = “localhost", database = "c:\\cabecaDeBoi.mdb", user = "", password = "", layer = "cellsLobo90x90", theme = "cells", select = { “height", “soilWater", “capInf" } } csCabecaDeBoi:load(); csCabecaDeBoi:loadMooreNeighbourhood;

51 Creating temporary cellular spaces
game = CellularSpace { xdim = N, ydim = N }

52 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);

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

54 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" end cell.water = cell.past.water + 2

55 Traversing a Cell Space
"for...end" statement: "for i, cell in pairs (csQ.cells) do ...end”. The i and cell variable in the statement are the index and the value of a cell inside the cells attribute from the cellular space csQ. for i, cell in pairs( csQ.cells ) do cell.water = cell.past.water + 2 end

56 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 true end )

57 Isotropic neighbourhoods in cell spaces
Von Neumann Neighborhood Moore Neighborhood

58 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

59 Synchronizing a cell space
tn tn+1 rule count = 0 ; for i, cell ipairs( csValeDoAnary ) do end if ( cell.past.sim_cover == 1 ) then cell.sim_cover = 0; count = count + 1 ; end cell.synchronize( ); ? print(“Number of deforested cells: ”.. count);

60 Synchronizing a cell space
tn tn+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

61 Synchronization Always read from the past Always write to the present
…. csQ:syncronize();

62 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 }

63 Which objects are nearest to each other?

64 Using Generalized Proximity Matrices (GPM)
Consolidated area Emergent area

65 TerraME neighborhoods are graphs
Euclidean space Open network Closed network D2 D1 Ana Paula Aguiar, Gilberto Câmara, Ricardo Cartaxo Souza, “Modeling Spatial Relations by Generalized Proximity Matrices”. V Brazilian Symposium on Geoinformatics, Campos do Jordão, 2003. [Aguiar et al., 2003] 65

66 Create or load neighborhoods
-- Create a Moore neighborhood createMooreNeighborhood( myCellSpace, “neighName” ) -- Create a 3x3 neighborhood create3x3Neighborhood(myCellSpace, filterF() , weightF(), name ) -- Create a MxN neighborhood createMxNNeighborhood( M, N, myCellSpace,filterF(), weightF(), name ) -- Load neighborhood from TerraLib database myCellSpace: loadTerraLibGPM(“myGPM"); -- Load neighborhood from TerraLib GAL files myCellSpace:loadGALNeighborhood("c:\\myNeigh.gal")

67 Building neighborhoods between cell spaces
spatialCoupling( M, N, cs1,cs2, filterF, weightF, name ) filterF(cell, neigh)  Boolean wheighF(cell, neigh)  Real

68 Example: neighborhood to simulate rain
-- Creates a 3x3 Neighborhood based on the cell "slope" -- only lower neighbors are considered create3x3Neighborhood( csQ, function(cell,neigh) return neigh.altimetry < cell.altimetry end, function(cell, neigh) return (cell.altimetry - neigh.altimetry)/ (cell.altimetry + neigh.altimetry) "slope" );

69 Saving cell spaces as images
-- attribute used to generate the image attr_name = "estado" -- values that the attribute can have attr_value = {0,1,2} -- color pallete attr_color = {{0,255,0},{255,0,0},{0,0,0}} -- directory where images will be saved path = "c:\\TerraME\\Results“ -- size of the cell in pixels cellSize = 2 -- load the o espaco celular do banco de dados TerraLib for t = 1, 100 do CStoPNG(myCellSpace, attr_name,t,path,cellSize,attr_value,attr_color) end

70 rain rain rain Itacolomi do Itambé Peak Lobo’s Range N

71 Picture direction Itacolomi do Itambé Peak Lobo’s Range

72 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

73 Simulation Result (36 min.)

74 Demo: Fire propagation
Database: c:\\TerraME\\Database\\db_emas.mdb Model: c:\\TerraME\\Modelos\\demo6_FireSpreadModel.lua QUEIMANDO INERTE CA 1 CA 2 CA 3 CA 4 CA 5 0.100 0.250 0.261 0.273 0.285 0.113 0.253 0.264 0.276 0.288 0.116 0.256 0.267 0.279 0.291 0.119 0.259 0.270 0.282 0.294 0.122 0.262 0.297

75 Demo: Amazon deforestation
Database: c:\\TerraME\\Database\\amazonia.mdb Model: c:\\TerraME\\Modelos\\demo3_desflorestamento_save.lua

76 References Carneiro, T., 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., A Gentle Introduction to TerraME. INPE Report, 2007. Ierusalimschy, R Programming in Lua (2nd edition). Rio de Janeiro, Lua.Org.


Download ppt "Spatial Dynamical Modeling with TerraME"

Similar presentations


Ads by Google