ilwis3_chap12

41
Scripts and functions CHAPTER 12 Scripts and functions In the previous chapters you have seen a number of examples of data analysis !hich !ere mainly performed by usin" dialo" boxes of the various I#$I% operations& %patial data analysis can also be performed in I#$I% by typin" commands calculations or expressions on the Command line of the 'ain !indo! or by combinin" these in a script & $hen usin" a script a series of commands calculations and expressions are  predefined in a list& These are consecutively executed !hen the script is run& (urthermore a script may contain variables so that the script can be used for different input maps tables or values& %cripts can save a lot of time !hen dealin" !ith fre)uently occurrin" analyses& The script lan"ua"e is similar to the Command line syntax and !e !ill therefore have a closer loo* at the Command line in the first part of this chapter& $e !ill then start creatin" scripts !ith and !ithout variables& In chapter + ,attribute data handlin"- and in chapters . / and 0 you have been !or*in" !ith the Command line to perform calculations on tables and maps& Instead of typin" lon" and similar calculations on the Command line for different input maps one  by one you can store fre)uen tly used calculations as  functions !hich can then be used for different maps& The use of functions !ill be treated in the last part of this ILWIS 3.0 User’s Guide 4  oubleclic* the I#$I% icon on the des*top&  3se the 4avi"ator  to "o the directory5 C56I#$I% 7&8 ata63sers 9uide6 Chapter12&

Upload: geomongolia

Post on 03-Jun-2018

212 views

Category:

Documents


0 download

TRANSCRIPT

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 1/41

Scripts and functions

CHAPTER 12

Scripts and functions

In the previous chapters you have seen a number of examples of data analysis !hich

!ere mainly performed by usin" dialo" boxes of the various I#$I% operations&

%patial data analysis can also be performed in I#$I% by typin" commands

calculations or expressions on the Command line of the 'ain !indo! or by

combinin" these in a script &

$hen usin" a script a series of commands calculations and expressions are

 predefined in a list& These are consecutively executed !hen the script is run&

(urthermore a script may contain variables so that the script can be used for

different input maps tables or values& %cripts can save a lot of time !hen dealin"

!ith fre)uently occurrin" analyses&

The script lan"ua"e is similar to the Command line syntax and !e !ill therefore have a

closer loo* at the Command line in the first part of this chapter& $e !ill then start

creatin" scripts !ith and !ithout variables&

In chapter + ,attribute data handlin"- and in chapters . / and 0 you have been

!or*in" !ith the Command line to perform calculations on tables and maps& Instead of

typin" lon" and similar calculations on the Command line for different input maps one

 by one you can store fre)uently used calculations as functions !hich can then be

used for different maps& The use of functions !ill be treated in the last part of this

ILWIS 3.0 User’s Guide 4

• oubleclic* the I#$I% icon on the des*top&

• 3se the 4avi"ator  to "o the directory5 C56I#$I% 7&8 ata63sers 9uide6

Chapter12&

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 2/41

Scripts and functions

chapter&

:efore you can start !ith the exercises you should start up I#$I% and chan"e the

subdirectory to C56I#$I% 7&8 ata63sers 9uide6Chapter12 or to the directory !here

the data files for this chapter are stored&

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 3/41

Scripts and functions

12.1 Creating a script

In this part you !ill learn ho! to create a script& $ith the help of a script a complete

9I% or Remote %ensin" analysis can be performed automatically& A script may

contain all the commands and expressions that are listed in the I#$I% Help topic

Appendices I#$I% script lan"ua"e ,syntax-& These include commands and expressions

for the creation and calculation of data ob;ects for ob;ect mana"ement ,e&"& copy or

delete- and for display of data ob;ects ,open and sho!-& <ther scripts and other

$indo!s applications can also be called from !ithin a script&

The script lan"ua"e is similar to the Command line syntax and !e !ill therefore

have a closer loo* at the Command line first&

12.1.1 Working fro t!e Coand "ine

'ost of the activities that are done via menus can also be done via the Command

lineof the 'ain !indo! located at the top of the 'ain !indo! ;ust belo! the

toolbars&

The Command line can be used for the follo!in" activities5 To perform ILWIS

commands5 To display edit or create I#$I% ob;ects and toobtain dialo" boxes to start an I#$I% operation& =ou can also use some special

script lan"ua"e for data management 5 copyin" and deletin" ob;ects and brea*in"

dependency lin*s of ob;ects>

To perform calculations !ith maps and attribute tables& The calculations !ith

tables !ere treated in chapter + and the 'ap Calculation statements !ere discussed

in chapter />

To perform ILWIS expressions allo!in" you to perform complete I#$I% operations

directly from the Command line> To "enerate script syntaxes that can be copied to a script&

?The Command line has a history5 use the #ist button or the Arro! up *ey to

retrieve previously used expressions and commands& The Escape *ey can be used

to clear the Command line&

=ou can also copy and paste text bac* and forth from the Command line to the

ILWIS 3.0 User’s Guide 4

ILWIS 3.0 User’ s Guide4#$

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 4/41

Scripts and functions

Clipboard !ith the follo!in" *eystro*es5

Ctrl@C Copy the selected part to the Clipboard&

Ctrl@ Paste the contents from the Clipboard&

To perform an I#$I% command you ;ust type it on the Command line& %ome

examples of commands are listed in Table 12&1& %ince the Command line is case

insensitive it doesnBt matter if you type them in upper or lo!ercase& A completeovervie! of I#$I% commands can be found in the I#$I% Help topic Appendices

I#$I% script lan"ua"e ,syntax-&

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 5/41

Scripts and functions

%a&"e 12.1' %ome commands that can be used on the Command line&

Coand ()ap"e *escription

<pen ob;ect&ext <pen 9eomorpholo"y&mpr

<pens the isplay <ptions dialo"

 box for the ob;ect or sho!s the

ob;ect immediately ,e&"& tables-

Edit ob;ect&ext Edit 9eomorpholo"y&mpa

<pens poly"on map

9eomorpholo"y in the Poly"on

editor

Copy ob;ect&ext ob;name Copy Citybloc*s&mpa CochaCopies poly"on map citybloc*s

to a ne! name

el ob;ect&ext el temp&mpr eletes raster map Temp

'd directory 'd temp Creates a ne! directory Temp

Help Help <pens the I#$I% Help

=ou !ill notice that you "et an error messa"e !hen you type5 <pen

9eomorpholo"y↵&

This is due to the fact that I#$I% doesnBt *no! !hich ob;ect !ith the name

9eomorpholo"y should be displayed5 a raster map a vector map a table a domain

etc& =ou have to use the name of the ob;ect and its extension&

ILWIS 3.0 User’s Guide 4

• <pen Help "o to the Appendices and bro!se throu"h the follo!in" topics5

I#$I% commands and expressions5 I#$I% commands I#$I% expres

sions I#$I% expressions ,alphabetic- Construction of expressions

I#$I% script lan"ua"e ,syntax-&

• Type the follo!in" command on the Command line5 <pen ↵

• The <pen <b;ect dialo" box appears from !hich you can select any ob;ect&

• %elect raster map 9eomorpholo"y and clic* < & The isplay <ptions

Raster 'apdialo" box is opened& Clic* < & The raster map

9eomorpholo"y is displayed&

• Close the map !indo!&

• Type the follo!in" command on the Command line5

• Type the follo!in" command on the Command line5

<pen9eomorpholo"y&mpr  ↵

• Clic* <  in the isplay <ptionsD Raster 'ap dialo" box&

• Close the ma !indo!&

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 6/41

Scripts and functions

The extension &mpr is the extension of raster maps& In Table 12&2 the extensions of

some I#$I% ob;ects are listed&

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 7/41

Scripts and functions

%a&"e 12.2' (ile extensions of different I#$I% ob;ects&

*escription

&mpr 9eomorpholo"y&mpr (ile extension of raster maps&

&mpa Citybloc*&mpa (ile extension of poly"on maps&

&mps Contour&mps (ile extension of se"ment maps&

&mpp Rainfall&mpp (ile extension of point maps&

&tbt Citybloc*&tbt (ile extension of tables&

&mpv Cochabamba&mpv (ile extension of map vie!&

&mpl Tms&mpl (ile extension of a map list&

&ioc :olivia&ioc (ile extension of an ob;ect collection&

&ilo Cochabamba&ilo (ile extension of a layout&

&his %lope&his (ile extension of a raster histo"ram&

&hsa #anduse&hsa (ile extension of a poly"on histo"ram&

&hss raina"e&hss (ile extension of a se"ment histo"ram&

&hsp $ells&hsp (ile extension of a point histo"ram&

&rpr #anduse&rpr (ile extension of a representation&

To practice !ith some commands expressions and script lan"ua"e !e !ill no!

calculate a landslide ris* map& $hen a unit in the 9eomorpholo"y map is classified as

active landslide ,A#- or old landslide ,<#- !e !ill classify it as landslide in the ne!

map& The ne! map %lide !ill contain 2 classes #andslide and 4o #andslide& $e !ill

therefore start !ith the creation of a ne! domain containin" these 2 classes&

In the next part of this chapter !e !ill create the same landslide map but then usin"

a script&

ILWIS 3.0 User’s Guide 4

• Type the follo!in" command on the Command line5 Crdom %lide ↵

• The class domain %lide is created but it still doesnBt contain any items&

• Type the follo!in" command on the Command line5

Additemtodomain %lide #andslideF ↵

Additemtodomain %lide 4o #andslideF ↵

• The domain %lide no! has t!o items& Chec* this by openin" the domain&

• Enter the follo!in" 'apCalc statement5

%lideGdom%lideI((,,9eomorpholo"yFA#F-<R

,9eomorpholo"y F<#F-F#andslideFF4o #andslideF-↵

• Clic* %ho! in the Raster 'ap efinition dialo" box and <  in the

ILWIS 3.0 User’ s Guide4+0

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 8/41

Scripts and functions

12.1.2 Creating and running a script

The exact syntax for the script statements is somethin" you *no! by heart only after

!or*in" a considerable time !ith I#$I%& There are ho!ever several methods that youcan use to create correct expressions for scripts5 3se the menu and the dialo" boxes

for a certain operation& (ill in all re)uired

 parameters in the dialo" box and clic* efine& At that moment the expression for

that specific operation is displayed on the Command line& =ou can then copy the

expression from the Command line into the script& $ithin the script editor and the

Command line you can use the follo!in" *ey stro*es5

CTR#@C Copy the selected part to the Clipboard&

CTR#@ Paste the contents from the Clipboard&

The ILWIS log file& I#$I% *eeps trac* of everythin" you are doin" in a socalled

log file& The I#$I% lo" file is called Ilwis.log  and can be found in the #o" filedirectory that is specified in the irectories part of the Preferences dialo" box ,(ile 

menu in the 'ain !indo!-& The log file is an A%CII file that you can open !ith a

text editor& =ou can copy ,part of- the expressions that are stated in the lo" file to

a script&

In the next exercise !e !ill create a script that !hen executed creates the same

landslide map as created from the Command line in the previous exercise&

• %elect Create %cript from the (ile menu of the 'ain !indo!& The %cript

editor ,(i"ure 12&1- is opened&

1

2

7

J

+

K

,igure 12.1' I#$I% %cript editor !ith an example of an I#$I% script&

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 9/41

Scripts and functions

The script that is needed to calculate the landslide map is sho!n belo! and as you can

see that lan"ua"e is fairly similar to the Command line syntax& Instead of typin" you

can also copy part of the statements from the Command line or from the log file into the

script& 4ote that the filenames have chan"ed so that !e do not over!rite the ob;ects of

the previous exercise&

ILWIS 3.0 User’s Guide 4

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 10/41

Scripts and functions

The line numbers in (i"ure 12&1 are not formin" part of the script& They are only used

here to comment on the various expressions& The script contains the follo!in" expres

sions5

In line 1 a class domain is created !ith the name #andslide& In lines 2 and 7 t!o items are added to this domain&

In line J the raster map #andslide is defined !hich !ill have one of the t!o

items defined in line 2 and 7& The 'ap Calculation formula uses as input the "eo

morpholo"ic map 9eomorpholo"y& The codes A#F and <#F stand for Active

#andslideF and <ld #andslideF&

In line + the map #andslide is calculated& The Calc statement is a typical script

lan"ua"e statement meanin" that an ob;ect has to be calculated before proceedin" to

the next line of the script& This statement is used !hen the outcome of a calculation

or a statement is needed as input for the next&

In line K the map #andslide is displayed&

• Type the follo!in" lines in the text box of the %cript tab5

Rem I#$I% script for calculatin" a landslide map

Crdom #andslide

Additemtodomain #andslide #andslideF

Additemtodomain #andslide 4o #andslideF

#andslideGdom#andslide I(( ,,9eomorpholo"yA#F-

<R,9eomorpholo"yF<#F-F#andslideFF4o #andslideF-

Calc#andslide&mpr

<pen#andslide&mpr

• Clic* the %ave button in the Toolbar  of the %cript editor and save the script

• Chec* the contents of the raster map #andslide and the domain

#andslide and close the map !indo! the domain and the %cript editorafter!ards&

• Clic* the Run %cript button in the Toolbar  of the

%cript editor& $hen you already closed the %cript editor you can select the scriptin the Catalo" clic* it !ith the ri"ht mouse button and select RunL from thecontextsensitive menu& =ou can also type Run #andslide on the Command

lineof the 'ain !indo!&

ILWIS 3.0 User’ s Guide4+2

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 11/41

Scripts and functions

The script #andslide is created& 4ote that the first line starts !ith Rem& This

indicates that this line contains a remark  and !ill not be executed by I#$I%& To run

the script5

The results of runnin" this script are the map #andslide and the domain

#andslide&

ILWIS 3.0 User’s Guide 4

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 12/41

Scripts and functions

12.2 Creating a script -it! ca"cu"ations and e)pressions

$hen usin" 'apCalc expressions in a script no special syntax is re)uired5 you can

simply type the 'apCalc expression as you !ould type it on the Command line of the

'ain !indo!&

The "eneral syntax for 'apCalc expressions in scripts is5

<utmap  Expression

$here

<utmap is the name of the output ob;ect&

is a definition symbol to indicate that a dependent output ob;ect is to be created !hen the assi"nment symbol 5 is used an independent

output ob;ect is created&

 Expression is an expression consistin" of an operation name follo!ed by the

 parameters re)uired by this operation ,bet!een brac*ets and separated

 by commas- or a 'apCalc expression&

(or example to sum maps 'ap1 and 'ap2 to create 'ap7 type in the script5

'ap7 'ap1 @ 'ap2

$hen usin" TabCalc expressions in a script it is re)uired that the !ord TabCalc

and the table name used for the expression are added to the syntax&

The "eneral syntax for TabCalc expressions in scripts is5

TabCalc Tablename Expression

$here

Tabcalc is to indicate that the follo!in" is a table calculation syntax&

Tablename is the name of the table used for the expression&

 Expression is the Command line syntax that you !ould use on the Command

lineof a table !indo! to perform table calculations&

(or example to sum columns Col1 and Col2 in table 'yTable and to store the

results in column Col7 you can type in a script5

TabCalc 'yTable Col7 Col1 @ Col2

=ou can also perform table calculations on other ob;ects that can be opened as a

table e&"& histo"rams point maps class representations& Then specify the extension

,see Table 12&2- of the ob;ect after the ob;ect name5 TabCalcObjectname.ext Col7

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 13/41

Scripts and functions

Col1 @ Col2

?$hen you use lon" ob;ect names for maps or tables and !hen the names of

ob;ects start !ith a di"it or start !ith or contain a space or a special character

then these names must be enclosed in sin"le )uotes& The extension should be left

outside the )uotes& (or more information see the I#$I% Help topic Ho! to use lon"

ob;ect names&

ILWIS 3.0 User’s Guide 4

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 14/41

Scripts and functions

12.2.1 ()ap"e of a script for ap Ca"cu"ation

In chapter / you have been !or*in" !ith 'ap Calculation formulas in the analysis of

a simple hypothetical problem dealin" !ith the calculation of the price of the land inthe Cochabamba re"ion&

 4o! you !ill do the same analysis usin" a script&

The avera"e land prices per hectare are "iven in an attribute table lin*ed to the land

use map& Ho!ever these avera"e values !ill either be hi"her or lo!er dependin" on

a set of criteria5 1& The price of the land !ill be 188M of the avera"e value !hen

located on slopes

of less than 28N and .8M !hen located on slopes of more than 28N& %lope informa

tion is stored in the map %lope&

2& The price of the land !ill be J8M of the avera"e value !hen it is located on an

active landslide or on an area !ith hi"h erosion and K8M !hen located on an old

landslide& (or this criterion !e need the "eomorpholo"ic map 9eomorpholo"y&

$hen evaluatin" the combination of criteria !e only loo* at !hich of the criteria !ill

lead to the lo!est land price&

Please *eep in mind that the ob;ective of this exercise is not that you learn about an

application for that the problem is too hypothetical but that you learn ho! to use'ap Calculation formulas in a script& The script loo*s as follo!s5

/e ILWIS Script

1

2

3

4

#

+

9eomorpholo"y 'apRasteriOePoly"on,9eomorpholo"yCochabamba&"rf-

#anduse 'apRasteriOePoly"on,#anduseCochabamba&"rf-

#andvalue ,#anduse&#andvalue- 2+

#andvalue1 I((,%lope Q 28 #andvalue 8&. #andvalue-

#andvalue2 I((,9eomorpholo"y <#F #andvalue 8&K I((

,,9eomorpholo"y A#F- <R ,9eomorpholo"y HEF- #andvalue 8&J

#andvalue--#andvalScombined 'I4,#andvalue1 #andvalue2-

#andvalSfinal I(34E(,9eomorpholo"y #andvalue2 #andvalScombined-

The line numbers in the table do not form part of the script& They are only used here

to comment on the various expressions&

(or a better understandin" of the script statements it is recommended to repeat the

exercise in section /&1& :elo! only a brief explanation on the script lines is "iven&

ILWIS 3.0 User’ s Guide4+4

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 15/41

Scripts and functions

In lines 1 and 2 the maps 9eomorpholo"y and #anduse are rasteriOed usin" the

"eoreference Cochabamba&

In line 7 the map #anduse is renumbered !ith the values from the column

#andvalue in the attribute table lin*ed to the map #anduse& The land use map is

lin*ed to an attribute table in !hich the avera"e land value ,per hectare- is storedfor each land use type& %ince the avera"e land values are "iven per hectare and you

ILWIS 3.0 User’s Guide 4

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 16/41

Scripts and functions

are !or*in" on maps !ith a pixel siOe of 28 meters you need to divide the land

value by 2+ in order to obtain the avera"e value per pixel&

In line J the first criterion is applied5 If the slope is more than 28N then the priceof the land !ill only be .8M of the avera"e value&

In line + the second criterion is applied5 If the pixel is an old landslide then the

value is only K8M of the avera"e value& If the pixel is on an active landslide or on

an active erosion area the value is only J8M of the avera"e& The information on

landslides and erosion is stored in the map 9eomorpholo"y& Codes are used instead

of the names of the "eomorpholo"ic units& The unit <ld #andslideF in the domain

9eomorpholo"y has the code <#F& If you use codes the formulas can be much

shorter&

4o! you have "enerated t!o maps that contain land values based on one criterion

,#andvalue1 and #andvalue2-& $hat should you do for pixels !here more than one

of these criteria occurs (or example for pixels !ith a slope less than 28N !hich are

located on an active landslide& The best approach is to ta*e for each pixel the

minimum of the same pixel in one of the t!o maps& This is done in line K&

%ince the map #andvalScombined occupies a smaller area than the map

#andvalue the formulas !ill result in undefined values for those places !here

one of the input maps is undefined& This is corrected in line .&

,- only the definitions of the maps are stored& The maps are not calculated until you

open them& $hen you open the last map ,#andvalSfinal- all previous maps are also

calculated&

• <pen the map #andvalSfinal& The calculation starts !ith the first map

that !as defined in the script& Have a loo* at the result and close the map

!indo! and the %cript editor&

• oubleclic* the script #andvalue in the Catalo"& The %cript editor is opened

in !hich you !ill see the script statements&

• Run the script by clic*in" the Run %cript button

in the Toolbar  of the%cript editor&

%ince all the expressions in script #andvalue are !ritten !ith the definition symbol

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 17/41

Scripts and functions

12.2.2 ()ap"e of a script for %a&"e Ca"cu"ation

In section +&. you have been practicin" !ith table ;oinin" for an urban problem usin"

t!o tables5 Citybloc* ,table lin*ed to the map Citybloc* !ith information on the .1.

city bloc*s in the central part of Cochabamba- and istrict ,a table !ith

ILWIS 3.0 User’s Guide 4

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 18/41

Scripts and functions

information on the cadastral districts of the city-& In the last part of the exercise you

solved the follo!in" problem5

Calculate the total area and the total population for each district& Apart from that

calculate the percenta"e cover of residential commercial and institutional buildin"s

in each district& (ind the relation bet!een the number of schools and the number of

schoolchildren ,under 1/ years old- for the districts of Cochabamba city& In order to

solve this problem !e needed to *no! the land use types the area the population

the number of school children and the number of schools in each district&

The information on areas land use types and population is available for each city

 bloc* in table Citybloc*& The information on the number of schools and the

 percenta"e of schoolchildren of the population is *no!n per district and is stored in

table istrict& %ince you *no! for each city bloc* in !hich district it is located you

can use the information from the table Citybloc* and brin" it into the table istrict&

Ho!ever the table Citybloc* contains .1. records and the table istrict only 17& %o

you !ill have to do an a""re"ation&

The script for calculatin" this problem is sho!n belo!5

/e ILWIS Script

1

2

3

4

#

+

$

10

11

12

13

14

1#

1+

<pentbl Citybloc*&tbt

Tabcalc Citybloc* Areadistrict ColumnA""re"ate%um,Area istrict 1-

Tabcalc Citybloc* istrlanduse istrict @ #anduse

Tabcalc Citybloc* Areadistrlu ColumnA""re"ate%um,Areaistrlanduse 1-

Tabcalc Citybloc* Residential Gdom perc>551 I((,#anduse ResidentialF 188 Areadistrlu

Areadistrict 8-

Tabcalc Citybloc* Commercial Gdom perc>551 I((,#anduse CommercialF 188 Areadistrlu

Areadistrict 8-

Tabcalc Citybloc* Institutional Gdom perc>551 I((,#anduse InstitutionalF 188 Areadistrlu

Areadistrict 8-

Closetbl Citybloc*&tbt

Rem <pen the table istrict

<pentbl istrict&tbt

Tabcalc istrict Residential ColumnUoin'ax,Citybloc*s&tbtResidential istrict 1-

Tabcalc istrict Rommercial ColumnUoin'ax,Citybloc*s&tbtCommercial istrict 1-

Tabcalc istrict Institutional ColumnUoin'ax,Citybloc*s&tbtInstitutional istrict 1-

ILWIS 3.0 User’ s Guide4++

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 19/41

Scripts and functions

1

1$

Tabcalc istrict Population ColumnUoin%um,Citybloc*s&tbtPopulation istrict 1-

Tabcalc istrict Children Population Pchildren 188

Tabcalc istrict Childpschool Children %chools

Closetbl istrict&tbt

<pen istrict&tbt

The line numbers in the table are not part of the script& They are only used here to

comment on the various expressions&

ILWIS 3.0 User’s Guide 4

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 20/41

Scripts and functions

In line 1 table Citybloc* !ill be *ept open in memory&

In line 2 the a""re"ate function %um is used to sum up the area per district& The

result is stored in the column Areadistrict of the table Citybloc*&

In line 7 ,istrlanduse istrict @ #anduse- the t!o columns #anduse

and istrict are combined ,concatenated- into a ne! column&

In line J the a""re"ate function %um is used to sum the areas of the land use types

 per district&

In lines + K and . the percenta"e covera"e of residential commercial and institutional areas !ithin each district is calculated& 4ote that the domain of the output

column is specified5 Perc !hich is the percenta"e domain and that the precision

for the values in the output column is set to 1&

In line / the Citybloc* table in memory is closed and in line 18 the istrict

table !ill be *ept open in memory&

In lines 11 12 and 17 the percenta"e cover values for residential commercial and

institutional areas !ithin each district as stored in table Citybloc* are ;oined intotable istrict& The a""re"ate function is needed since 1 record of a district from

table istrict is lin*ed to many records of the same istrict in table Citybloc*&

In line 1J another ;oin operation is performed to ;oin the population data from

table Citybloc* summed up for each district into table istrict&

In line 1+ the total population per district is used in combination !ith the percent

a"e of schoolchildren per district to find the number of schoolchildren per district&

In line 1K the number of children per school is calculated for each district&

• Clic* the I#$I% button in the Toolbar  of the

'ain !indo! to ma*e sure that all ob;ect types are selected&

• oubleclic* the script 3rban in the Catalo"& The %cript editor is opened in

!hich you !ill see the script lan"ua"e&

• Run the script by clic*in" the Run %cript button

in the Toolbar  of the%cript editor&

• Have a loo* at the result and close the table istrict after!ards&

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 21/41

Scripts and functions

In line 1. table istrict is closed&

In line 1/ the output table istrict is displayed&

ILWIS 3.0 User’s Guide 4

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 22/41

Scripts and functions

12.3 Using paraeters in a script

In the previous examples you have only loo*ed at scripts that are made for specific

maps or tables& =ou can also ma*e scripts that are more !idely applicable by

introducin" parameters& Any I#$I% ob;ect can be represented by parameters in a

script as !ell as values and strin"s& Parameters in a script must be !ritten as M1 M2

M7 L M0 ,see I#$I% Help Ho! to use parameters in scripts-&

In this exercise you !ill calculate a slope map in percenta"es and de"rees& The script

loo*s li*e5

/e ILWIS Script' for creating a s"ope ap

1

2

3

4

#

+

em 'apInterpolContour,M1 M2-

emSdx 'ap(ilter,em (V-

emSdy 'ap(ilter,em (=-

M7&mpr 188 H=P,emSdx emSdy- PixsiOe,em-

MJ&mpr RAE9,ATA4,M7&mpr 188--

Calc MJ&mpr

The line numbers in the table are not part of the script& They are only used here to

comment on the various expressions& The script contains the follo!in" expressions5

In line 1 a contour vector map indicated !ith parameter M1 is interpolated usin"

"eoreference M2 to create a i"ital Elevation 'odel em&

In line 2 the di"ital elevation model is filtered usin" a (V filter creatin" a ne!

map emSdx containin" the first derivative in xdirection ,dfdx- per pixel&

In line 7 the di"ital elevation model is filtered usin" a (= filter creatin" a ne!

map emSdy containin" the first derivative in ydirection ,dfdy- per pixel&

In line J a slope map in percenta"es M7 is calculated&

In line + a slope map in de"rees MJ is calculated&

In line K output map MJ is calculated and therefore all maps created by the script

are calculated&

ILWIS 3.0 User’ s Guide4+$

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 23/41

Scripts and functions

 4ote that this script contains four parameters5 M1 till MJ&

$hen you run a script !ith parameters these parameters should be defined either

 before or !hen the script is run& The parameters can be defined in the follo!in"

!ays5 In the %cript editor you can fill out the Parameters tab& Parameter 4ame and

Type can be entered here and !ill later on appear in a Run %cript dialo" box !hen auser runs the script& The user can then select ob;ect names for input parameters

andor type ob;ect names for output parameters&

ILWIS 3.0 User’s Guide 4

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 24/41

Scripts and functions

$hen the Parameters  tab is used also default values for the input and output

 parameters can be filled out in the efault aluestab& This tab is very useful

!hen you !ant to test your script or !hen you directly !ant to run your script&

$hen no parameters are defined on the Parameters tab you can run the script

from the Command line and specify the input and output ob;ects that should be

used for the parameters after the script name5 Run Scriptname Parameter

 Parameter! L Parameter"

I#$I% !ill replace every parameter definition ,M1L&M0- by the specified parame

ter&

(or this exercise the parameters of Table 12&7 are used&

%a&"e 12.3' %cript parameters&

araeters for t!e S"ope script

araeter ae for /un Script dia"og &o) %pe 5ptiona" defau"t o&6ect

M1

M2

M7

MJ

Input Contour map

9eoreference to be used

<utput %lope map in Percenta"es

<utput %lope map in e"rees

%e"ment 'ap

9eoreference

(ilename

(ilename

Contour&mps

Cochabamba&"rf

%lopepct&mpr

%loped"r&mpr

$hen run the script !ill be interpreted as follo!s5

/e ILWIS Script' for creating a s"ope ap

1

2

3

4

#

+

em 'apInterpolContour,Contour&mps Cochabamba&"rf-

emSdx 'ap(ilter,em (V-

emSdy 'ap(ilter,em (=-

%lopepct&mpr 188 H=P,emSdx emSdy- PixsiOe,em-

%loped"r&mpr RAE9,ATA4,%lopepct&mpr 188--

Calc %loped"r&mpr

• <pen the script %lope and loo* at its contents&

• Close the %cript editor&

• =ou can run the script by typin" the follo!in" expression on the Command

lineof the 'ain !indo!5

Run %lope Contour Cochabamba %lopepct %loped"r  ↵

• Have a loo* at the resultin" maps %lopepct and %loped"r& 4ote that the

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 25/41

Scripts and functions

$e !ill first run the script from the Command line of the 'ain !indo!&

$e !ill no! enter the parameters in the Parameters tab and run the script from the

%cript editor&

ILWIS 3.0 User’s Guide 4

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 26/41

Scripts and functions

• <pen script %lope and clic* the Parameters tab& %et the number of parameters to

J&

• Enter the parameter 4ames and Types accordin" to Table 12&7&• Enter Run %lope %cript on the escription line&

• %ave the script&

• Run the script by clic*in" the Run %cript button in

the Toolbar of the%cript editor&

• The Run %lope %criptdialo" box no! opens& %elect the correct input map and

"eoreference and type for the <utput %lope map in Percenta"e %lopepct2and forthe <utput %lope map in e"rees%loped"r2&

• Clic* <&

• Have a loo* at the resultin" maps %lopepct2 and %loped"r2&

• Clic* the efault aluestab& %elect the Input Contour mapand the

9eoreference to be used accordin" to Table 12&7& Type for the <utput

%lope map in Percenta"e%lopepct7and for the <utput %lope map in

e"rees%loped"r7&

• %ave the script&

• Run the script by clic*in" the Run %cript button in

the Toolbar of the%cript editor&

• Have a loo* at the resultin" maps %lopepct7 and %loped"r7& Close the map

!indo!s and the %cript editor&

ILWIS 3.0 User’ s Guide40

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 27/41

Scripts and functions

12.4 /unning a script fro anot!er script

In this example !e !ill ma*e a ne! script ensIn !hich !ill run the existin" script

ensity three times each time usin" a different map as input&

%cript ensity calculates the density of landslides !ithin certain units& It reads5

/e Script *ensit to ca"cu"ate "ands"ide densit

12

3

4

#

+

sM1 TableCross,M1%lide-

calc sM1&tbt

tabcalc M1 AreaClass ColumnUoin%um,sM1&tbtAreaM1-

tabcalc sM1 Area%l iff,%lideWlandslideWArea8-

tabcalc M1 Area%lide ColumnUoin%um ,sM1&tbtArea%lM1-

tabcalc M1 ensity Gdomperc 188 Area%lideAreaClass

calc M1&tbt

As before the line numbers are only used to explain the script& In short5

A cross table is calculated from a variable input map ,M1- and raster map %lide&

In the cross table the total area of each class is calculated and !ritten into theattribute table of the variable input map ,AreaClass-&

Then if landslides occur the total area !ith landslides is calculated per class

and !ritten into the attribute table of the variable input map ,Area%lide-&

In the attribute table the density of landslides is calculated by dividin" Area%lide by

AreaClass&

$ith script ensIn !e can no! call script ensity and use various input maps5

ILWIS 3.0 User’s Guide 4

• Create a script ensIn and enter the lines as "iven in the example&

• %ave the script and exit the editor&

• In the Catalo" clic* script ensIn !ith the ri"ht mouse button and choose

Run&

• Have a loo* at the resultin" attribute tables 9eolo"y %lopeSclasses and

Catchment&

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 28/41

Scripts and functions

/e Script *ensIn t!at ser7es as input for t!e *ensit script

1

2

3

Run ensity 9eolo"y

Run ensity %lopeSclasses

Run ensity Catchment

In line 1 script ensity is executed usin" map 9eolo"y as parameter M1& In line 2 script ensity is executed usin" map %lopeSclasses&

In line 7 script ensity is executed usin" map Catchment&

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 29/41

Scripts and functions

12.# Specia" script "anguage

Even thou"h the script lan"ua"e is not intended to be a pro"rammin" lan"ua"e but

merely a tool to help you process data it does have special features that ma*es it

easier to !or* !ith and to "ive it some extra capabilities& %ome examples of the

special script lan"ua"e is "iven in Table 12&J a complete overvie! is "iven in the

I#$I% Help topic Appendices I#$I% script lan"ua"e ,syntax-&

%a&"e 12.4' %ome commands that can be used on the Command line&

Coand ()ap"e *escription

:e"incomment

Endcomment

 be"incomment

<pen9eomorpholo"y&mprendcomment

All lines of text bet!een the com

mands be"incomment and endcom mentare i"nored by the script&

Pause seconds Pause 28%top the script for a certain amount

of time ,seconds-&

'essa"e text 'essa"e Clic* to continue

<btain a messa"e box on the screen

!ith any text> the text can be as lon"

as you li*e& After pressin" the

< button in the messa"e box the

script !ill continue&

<pen noas* object.ext <pen noas* Citybloc*&mpa<pens the ob;ect object.ext  !ith its

default display options

Closeall CloseallClose all I#$I% !indo!s except the

I#$I% 'ain !indo!&

Suar' Scripts

%cripts are used to automate the operations in I#$I%&

A script is a list of commands calculations and expressions&

$ith the help of a script a complete 9I% or Remote %ensin" analysis can be per

formed automatically&

A script may contain all the commands and expressions as listed in the

Appendices I#$I% commands and expressionssection of the I#$I% Help5 openin"

dialo" boxes 'apCalc TabCalc performin" operations and some other actions&

A script consists of an ob;ect definition file !ith extension &isl ,I#$I% %cript

#an"ua"e- and a data file !ith extension &isf ,I#$I% %cript (ile-&

ILWIS 3.0 User’s Guide 4

ILWIS 3.0 User’ s Guide42

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 30/41

Scripts and functions

A script can be made by copyin" an expression from the Command line after you

have filled in all re)uired parameters in the dialo" box of a certain operation and

clic*ed efine& At that moment the expression for that operation is sho!n on the

Command line& =ou can copy this expression from the Command line into your script&

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 31/41

Scripts and functions

A script can also be made by copyin" parts from the ILWIS log  file after you have

executed some operations via dialo" boxes& I#$I% *eeps trac* of everythin" you are

doin" in a socalled lo" file& The I#$I% lo" file is called Ilwis.log  and you can findits directory from the Preferences& The .log  file can be opened !ith any text editor&

A script can be started from the %cript editor by choosin" the Run L command

on the scriptBs contextsensitive menu in the Catalo" or from the Command line of

the 'ain !indo! by typin"5 Run %criptname ↵

Any I#$I% ob;ect can be represented by parameters in a script as !ell as values

and strin"s& Parameters in a script must be !ritten as M1 M2 M7 etc& Parameters

have a 4ame  and Type  that are entered on the Parameters  tab so that a user canselect ob;ects for the parameters in a dialo" box !hen runnin" the script&

=ou can also specify default ob;ects for parameters on the efault aluestab&

<ther scripts and other $indo!s applications can be called from !ithin a script&

A ran"e of special script lan"ua"e is available to "ive a script more possibilities&

ILWIS 3.0 User’s Guide 4

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 32/41

Scripts and functions

12.+ ,unctions

In Chapter + you have seen the use of calculation formulas to !or* !ith tables andin chapters . and / those to !or* !ith maps& As you have seen there are many

different operators and functions that can be applied on value maps and on class or

I maps& A complete overvie! of the operators and functions available in Table

Calculation and in 'ap Calculation can be found in the I#$I% Help to"ether !ith a

series of examples&

In this exercise you !ill first have a loo* at some examples of functions that are

already present in the system , pre#programmed functions- before you !ill practice

!ith the creation of your o!n functions ,user#defined functions-&

Sste8defined functions

A number of these functions !here already treated in chapters + . and /& Here only

some examples of systemdefined functions are "iven& <ne of the most important

functions is the $onditional I% function&

I((,a&b&c- If condition a is true then return the outcome of expression b or

else ,!hen condition a is not true- return the outcome of expression

c& 'ind the double ff  in I(( ,standin" for I( (unction-&

• Type the follo!in" expression on the Command line of the 'ain !indo!5

Result1 I((,em Q J888 18 8- ↵

• Clic* %ho! evaluate the result and close the map !indo!&

R4,lon"- Returns random lon" inte"er values in the ran"e X1> 2 billion

,2180-Y> To simulate a die use this function in the form of5

R4,K-&

R4,8- Returns a 8 or 1 at random&

R4, - Returns random real values in the ran"e X8>1Q i&e& bet!een 8 and 1

includin" 8 but excludin" 1&

ILWIS 3.0 User’ s Guide44

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 33/41

Scripts and functions

It means5 If a pixel in map em ,i"ital Elevation 'odel- has a value "reater thanJ888 then assi"n the value 18 to this pixel in output map Result1 or else assi"n a 8&

/ando functions

(or statistical purposes you mi"ht need a map !ith random values&

(or example if you !ant to subdivide your map randomly into t!o "roups of pixels

ILWIS 3.0 User’s Guide 4

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 34/41

Scripts and functions

use the follo!in" formula5

in !hich 'ap1 is a "eoreferenced value map !ith value 1 for every pixel& 'ap1 can

 be calculated from any map usin" the appropriate "eoreference& Random uses the

same "eoreference as 'ap1& The pixels in the output map !ill randomly "et the value

8 or 1& Random functions are very useful for all *inds of statistical testin"&

• Type the follo!in" expression on the Command line5

'ap1 I(( ,em 1 1 1- ↵

• The Raster 'ap efinitiondialo" box is opened& %elect system omain

alue and set the alue Ran"e from 8 to 1 and the Precision to 1&8&

Clic* %ho!&

• Type the follo!in" expression on the Command line of the 'ain !indo!5

• The Raster 'ap efinition dialo" box is opened& %elect system omain

alue and set the alue Ran"e from 8 to 1 and the Precision to 1&8&

Clic* %ho!&

• Examine the results and then close the map !indo!&

'I4,a&b- Calculates the minimum of t!o expressions a and b&

'I4,a&b&c- Calculates the minimum of three expressions a b and c&

'AV,a&b- Calculates the maximum of t!o expressions a and b&

'AV,a&b&c- Calculates the maximum of three expressions a b and c&

• Type the follo!in" expression on the Command line5

'in7 'I4,Tmb2 Tmb7-↵

• The Raster 'ap efinitiondialo" box is opened& %elect system omain

Ima"eand clic* %ho!&

• Examine the results and then close the ma !indo!&

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 35/41

Scripts and functions

ina) functions

3sin" these functions you can for instance calculate for each pixel the minimum or

maximum value of 2 or 7 input maps> substitute a& b& c !ith the map names&

User8defined functions

:esides many internal prepro"rammed functions I#$I% "ives the user an

opportunity to create ne! functions& They may be used in all four calculators in

ILWIS 3.0 User’s Guide 4

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 36/41

Scripts and functions

I#$I%5 'apCalc TabCalc %cripts and the poc*et line calculator& Especially !hen

you need to execute calculations that re)uire a lot of typin" !or* several times user

defined functions may be time savin"& A userdefined function is an expression that

may contain any combination of operators functions maps and columns in tables&

(irstly you !ill create a simple function and after that a more complex one&

The (unction editor appears sho!in" your ne!ly created function& If necessary you

can edit your function& The different parameters in your function may be names of

maps or table columns or you use characters ,a& b- !hich you may specify later !hen

you apply the function&

The function in the (unction editor is defined as follo!s5

The line numbers do not form part of the function& They are only used here to explain

the contents&

In line 1 the function name is "iven and the parameters are listed bet!een brac*ets&

In this case there are t!o parameters5 alue a and alue b& Also the output domain is

"iven5 alue&

• oubleclic* 4e! (unction in the <perationlist& The Create (unction

dialo" box is opened&

• Type for the (unction 4ame5 Avera"eS2&

• Type for the Expression5 ,a @ b-2&

1 (unction Avera"eS2,alue a alue b- 5 alue

2 :e"in

3

Return ,a@b-2>4 End>

ILWIS 3.0 User’ s Guide4+

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 37/41

Scripts and functions

In line 2 the !ord :e"in indicates the be"innin" of the actual function expression&

In line 7 the actual function is "iven& 4ote that the expression ends !ith a

semicolon ,>-&

In line J the end of the function is indicated !ith the !ord End&

 4o! you can use your function on the Command line of the 'ain !indo! or table

!indo!& Type an expression that starts !ith an output map name ,or column name-

follo!ed by the definition symbol ,- then the name of your function and fill out the

 parameters& The parameters replacin" the characters a and b in your function have to

 be entered in brac*ets separated by commas& The parameter that is filled in first is

ta*en as the first parameter encountered in your userdefined function&

ILWIS 3.0 User’s Guide 4

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 38/41

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 39/41

Scripts and functions

another variable ,a map or value-& :ut in fact PI represents here the systemdefined

variable& %o you should edit the function to remove the variable declarationalue PI&

The correct definition of the function should be5

(unction Aspect,alue x alue y- 5 alue

:e"in

Return RAE9,ATA42,x y- @ PI->

End>

ILWIS 3.0 User’s Guide 4

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 40/41

Scripts and functions

To apply the function Aspect5

 4o! you can easily calculate several aspect maps of other areas& =ou only have to fill

out your ne! input variables for the function> you have to specify the ne! x and y

map names of the other area&

Suar' ,unctions

I#$I% contains over +8 different functions that are prepro"rammed and that can

 be used in Table Calculation in 'ap Calculation %cript and in the poc*et line cal

culator&

:esides many internal prepro"rammed functions I#$I% "ives the user an oppor

tunity to create ne! functions&

A userdefined function is an expression !hich may contain any combination of

operators functions maps and columns&

3serdefined functions are especially useful !hen you need to execute certain cal

culations !hich re)uire a lot of typin" effort on the Command line&

To apply your function type an expression on the Command line of the 'ain

!indo! or table !indo!& %tart !ith an output map name ,or column name- fol

• Type the follo!in" expression on the Command line of the 'ain $indo!5

Aspect Aspect,emSdx emSdy- ↵

• The Raster 'ap efinitiondialo" box is opened& %elect system omain

alue and set the alue Ran"e from 8 to 7K8 and the Precision to 1&

Clic* %ho!&

• 

ILWIS 3.0 User’ s Guide4$

8/12/2019 ilwis3_chap12

http://slidepdf.com/reader/full/ilwis3chap12 41/41

Scripts and functions

lo!ed by the definition symbol ,- the name of your function and fill out the func

tion parameters& The parameters replacin" the characters a& b& c etc& in your func

tion have to be entered in brac*ets separated by commas& The parameter filled out

first is ta*en as the first parameter encountered in your userdefined function&