Examples of Lua scripts

Print Previous page Top page

 

Simple examples (one script can prepare multiple values of ​​semantics):

 

1. Multiply the area of ​​the object at the height from the semantics field with the code 1 and save the value of one to the semantics field with code 10 (code depends on the map classifier).

 

value = getSquare() * getCodeSem(1)

setCodeSem(10, value)

 

2. If the value of semantics field with code 11 is set, the multiply the object length by the width from semantics field with code 11 and save the value in the semantics field with the code 12 (code depends on the classifier maps). For example, to determine the square of road ​​coverage.

 

if getCodeSem(11, -1) ~= -1 then

 value = getLength() * getCodeSem(11)

 setCodeSem(12, value)

end

 

3. Make a string from semantics field 9 and the layer name and save to the semantics field with code 345. If semantics field 9 not, write the value "No name".

 

value = getCodeSemStr(9, "No name").." - "..getLayerName()

setCodeSem(345, value);

 

Example of script to collect statistics for selected objects and save the report to a text file includes three functions with the required names:

- Start();

- DoObject();

- Stop().

 

• Function Start () and Stop () are useful for creating variables and store the result of calculations.

• Function DoObject () is called on each object.

• If the function Start () or DoObject () is not in the script, the entire text of the script is main function as the previous example.

• Function Stop () may be present in any case, and called after the processing of all selected objects, if it is in the script.

 

Function Start () is called when opening the script to initialize global variables

 

function Start()

TypeA = 0

TypeB = 0

TypeC = 0

Total = 0;

File  = io.open("c:\\Report.txt", "w")

end

 

Function Stop () is called before the closing script to summarize the findings and report generation

 

function Stop()

s = "Processed objects count is "..Total.."\n\r"

File:write(s)

p = 100 * TypeA / Total

s = "Objects count for type A is "..TypeA.."\t percentage is "..p.."\n\r"

File:write(s)

p = 100 * TypeB / Total

s = " Objects count for type B is "..TypeB.."\t percentage is "..p.."\n\r"

File:write(s)

p = 100 * TypeC / Total

s = " Objects count for type C is "..TypeC.."\t percentage is "..p.."\n\r"

File:write(s)

 

io.close(File)

end

 

Statistics collection is performed in the function DoObject (), if the script contains a function Start ()

 

function DoObject()

if getCodeSem(4) < 100 then TypeA = TypeA + 1;

 elseif getCodeSem(4) < 200 then TypeB = TypeB + 1;

  else TypeC = TypeC + 1; end

 

Total = Total + 1

end

 

Example script to update the text and the semantics of the caption text

 

1. Request value of semantics field with code 9 "own name"

sem = getCodeSemStr(9, -1)

if sem ~= -1

then

 

2. Replace the string "abc." to "abcdef" in semantics

if string.find(sem, "abc.") and string.len(sem) == 4 then

setCodeSem(9, "abcdef")

end

end

 

3. Is type of object label?

if getLocal() ~= 3 then

return;

end

 

text = getText(0);

 

4. Replace the string "abc." to "abcdef" in label

if string.find(text, " abc.") and string.len(text) == 4 then

setText(0, "abcdef")

end

 

Example of a script for formation of text semantics in the form of a part of a line of other semantics

 

sem = getCodeSemStr(9, -1)

if sem ~= -1

then

Replace the string "38:13:000106:1708" onto ":1708"

 

size = string.len(sem)

if size > 13 then

 val = string.sub(sem, 13)

 setCodeSem(1111, val)

end

end