NoiseModelling client line interface (CLI)

In this tutorial, we describe the different method to pilot NoiseModelling thanks to scripts. To do so, we will use a separate command-line interface, called ScriptRunner, in which the GUI has been removed (no more WPS Builder).

From that point, NoiseModelling can be executed in 3 different manners:

  1. with simple command lines

  2. with Bash script

  3. with Groovy script

To illustrate, users are invited to reproduce the tutorial “Get Started - GUI” in command lines.

Note

This tutorial is mainly dedicated to advanced users.

Warning

The URL is here adapted to Linux or Mac users. Windows user may adapt the address by replacing / by \ in the folders paths.

Requirements

Warning

For all users (Linux , Mac and Windows), please make sure your Java environment is well configured. For more information, please read the page Requirements.

1. Simple command line

Using the terminal of your operating system

Below is an example of a bash instruction, executing the Import File (located in the directory scripts/).

1cd /home/user/NoiseModelling/
2
3./bin/ScriptRunner -w ./ -s scripts/Import_and_Export/Import_File.groovy --pathFile resources/ground_type.shp

Warning

Adapt /home/user/NoiseModelling address with the real installation folder of NoiseModelling. Use the appropriate ./bin/ScriptRunner or ./bin/ScriptRunner.bat (depending on if you are on Linux / Mac or Windows) file, which is located in the bin/ directory.

2. Bash script

Below is an example of a sequence of simple .groovy scripts, using bash instructions and launching the steps described in the “Get Started - GUI”.

 1#! /bin/bash
 2
 3# Run the get started turorial
 4# https://noisemodelling.readthedocs.io/en/latest/Get_Started_Tutorial.html
 5
 6# Step 4: Upload files to database
 7# create (or load existing) database and load a shape file into the database
 8./bin/ScriptRunner -w ./ -s scripts/Import_and_Export/Import_File.groovy --pathFile resources/ground_type.shp
 9./bin/ScriptRunner -w ./ -s scripts/Import_and_Export/Import_File.groovy --pathFile resources/buildings.shp
10./bin/ScriptRunner -w ./ -s scripts/Import_and_Export/Import_File.groovy --pathFile resources/receivers.shp
11./bin/ScriptRunner -w ./ -s scripts/Import_and_Export/Import_File.groovy --pathFile resources/ROADS2.shp
12./bin/ScriptRunner -w ./ -s scripts/Import_and_Export/Import_File.groovy --pathFile resources/dem.geojson
13
14
15# Step 5: Run Calculation
16./bin/ScriptRunner -w ./ -s scripts/NoiseModelling/Noise_level_from_sources.groovy --tableBuilding BUILDINGS --tableSources ROADS2 --tableReceivers RECEIVERS --tableDEM DEM --tableGroundAbs GROUND_TYPE
17
18# Step 6: Export (& see) the results
19./bin/ScriptRunner -w ./ -s scripts/Import_and_Export/Export_Table.groovy --exportPath RECEIVERS_LEVEL.shp --tableToExport RECEIVERS_LEVEL

3. Groovy script

Below is an example of a complex .groovy script, launching the different steps described in the “Get Started - GUI”.

 1package org.noise_planet.noisemodelling.scripts
 2
 3import org.h2gis.api.ProgressVisitor
 4import org.noise_planet.noisemodelling.scripts.Import_and_Export.Import_File
 5import org.noise_planet.noisemodelling.scripts.NoiseModelling.Noise_level_from_source
 6import org.noise_planet.noisemodelling.scripts.NoiseModelling.Road_Emission_from_Traffic
 7import org.noise_planet.noisemodelling.webserver.utilities.Logging
 8import org.slf4j.Logger
 9import org.slf4j.LoggerFactory
10import groovy.sql.Sql
11import java.io.File
12
13import java.sql.Connection
14
15title = 'Tutorial script'
16description = 'Long description of tutorial script'
17
18inputs = [ resourcesFolder : [
19        description: "Path of the resource folder for input data",
20        title: "Resource folder",
21        default: 'resources',
22        type: String.class
23]]
24
25outputs = [result: [name: 'Result output string', title: 'Result output string', description: 'Result table name. Can be used as input for another WPS process', type: String.class]]
26
27def exec(Connection connection, Map input, ProgressVisitor progress) {
28    Logger logger = LoggerFactory.getLogger("tutorial")
29    ProgressVisitor tutorialProgress = progress.subProcess(7)
30    // 7 steps in this task
31
32    def resourceFolder = input.resourcesFolder as String
33    // Upload files to database
34    def groundTable = new Import_File().exec(connection, ["pathFile": new File(resourceFolder, "ground_type.shp")], tutorialProgress)["outputTable"]
35
36    def buildingTable = new Import_File().exec(connection, ["pathFile": new File(resourceFolder, "buildings.shp")], tutorialProgress)["outputTable"]
37
38    def receiversTable = new Import_File().exec(connection, ["pathFile": new File(resourceFolder, "receivers.shp")], tutorialProgress)["outputTable"]
39
40    def roadsTable = new Import_File().exec(connection, ["pathFile": new File(resourceFolder, "ROADS2.shp")], tutorialProgress)["outputTable"]
41
42    def demTable = new Import_File().exec(connection, ["pathFile": new File(resourceFolder, "dem.geojson")], tutorialProgress)["outputTable"]
43
44    def roadEmissionTable = new Road_Emission_from_Traffic().exec(connection, [tableRoads : roadsTable]).result
45
46    // print some lines of road emission
47    Logging.formatSqlQueryResult(new Sql(connection), "SELECT * FROM $roadEmissionTable LIMIT 10" as String)
48
49    // Run Calculation
50    def resultTable = new Noise_level_from_source().exec(connection, ["tableBuilding": buildingTable, "tableSources": roadEmissionTable, "tableReceivers": receiversTable,
51                                                                      "tableDEM"     : demTable, "tableGroundAbs": groundTable], tutorialProgress)
52
53    // Return results
54    return Logging.formatSqlQueryResult(new Sql(connection), "SELECT * FROM $resultTable.result ORDER BY IDRECEIVER, PERIOD LIMIT 10" as String, 120)
55}

You can find this script get_started_tutorial_complex.groovy on the installation folder of NoiseModelling

To run it use this bash command.

1./bin/ScriptRunner -w ./ -s get_started_tutorial_complex.groovy