Use NoiseModelling by scripting¶
Introduction¶
You master the use of the “WPS Builder” user interface. Now you want to stop using this interface and run the processes with your favorite programming language.
Well know that it is possible!
It’s actually quite simple.
All you have to do is prepare the HTTP calls you will make to Geoserver.
The Web Processing Service API¶
The WPS is not something we did on our own. It’s a standardized interface very well established in the geospatial community. So you will find a lot of documentation and libraries.
You can first learn more about it:
How it works¶
Processing scripts are hosted by a GeoServer instance. This software contain a web server that listen for localhost requests.
What your scripts need to do is simply access this web server like a web browser would. The difficulty being the creation of the entries expected by the service.
Method to easily find inputs query¶
The POST request must contain the expected inputs.
You can use your navigator debug mode with the WPS Builder GUI In order to generate the inputs for you.
Once in WPS Gui press F12 key to enter debug mode, then click on network tab:
From here click on “Run process”, now you have the complete url and query to copy-paste into your script.
Get started using Python¶
The following script run the exact WPS blocks used in the Get Started tutorial.
We can use the String.Template class in order to substitute our inputs.
urllib.request module is also helpful for post queries.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | import urllib.request from string import Template import_file = Template('<p0:Execute xmlns:p0="http://www.opengis.net/wps/1.0.0" service="WPS" version="1.0.0"><p1' ':Identifier xmlns:p1="http://www.opengis.net/ows/1.1">Import_and_Export:Import_File</p1' ':Identifier><p0:DataInputs><p0:Input><p1:Identifier ' 'xmlns:p1="http://www.opengis.net/ows/1.1">pathFile</p1:Identifier><p0:Data><p0:LiteralData' '>$path</p0:LiteralData></p0:Data></p0:Input></p0:DataInputs' '><p0:ResponseForm><p0:RawDataOutput><p1:Identifier ' 'xmlns:p1="http://www.opengis.net/ows/1.1">result</p1:Identifier></p0:RawDataOutput></p0' ':ResponseForm></p0:Execute>') get_lday = Template('<p0:Execute xmlns:p0="http://www.opengis.net/wps/1.0.0" service="WPS" ' 'version="1.0.0"><p1:Identifier xmlns:p1="http://www.opengis.net/ows/1.1">NoiseModelling' ':Noise_level_from_traffic</p1:Identifier><p0:DataInputs><p0:Input><p1:Identifier ' 'xmlns:p1="http://www.opengis.net/ows/1.1">tableReceivers</p1:Identifier><p0:Data><p0:LiteralData' '>$table_receivers</p0:LiteralData></p0:Data></p0:Input><p0:Input><p1:Identifier ' 'xmlns:p1="http://www.opengis.net/ows/1.1">tableBuilding</p1:Identifier><p0:Data><p0:LiteralData' '>$table_buildings</p0:LiteralData></p0:Data></p0:Input><p0:Input><p1:Identifier ' 'xmlns:p1="http://www.opengis.net/ows/1.1">tableDEM</p1:Identifier><p0:Data><p0:LiteralData' '>$table_dem</p0:LiteralData></p0:Data></p0:Input><p0:Input><p1:Identifier ' 'xmlns:p1="http://www.opengis.net/ows/1.1">tableRoads</p1:Identifier><p0:Data><p0:LiteralData' '>$table_roads</p0:LiteralData></p0:Data></p0:Input></p0:DataInputs><p0:ResponseForm><p0' ':RawDataOutput ><p1:Identifier xmlns:p1="http://www.opengis.net/ows/1.1">result</p1:Identifier' '></p0:RawDataOutput></p0:ResponseForm></p0:Execute>') export_table = Template('<p0:Execute xmlns:p0="http://www.opengis.net/wps/1.0.0" service="WPS" ' 'version="1.0.0"><p1:Identifier ' 'xmlns:p1="http://www.opengis.net/ows/1.1">Import_and_Export:Export_Table</p1:Identifier><p0' ':DataInputs><p0:Input><p1:Identifier ' 'xmlns:p1="http://www.opengis.net/ows/1.1">tableToExport</p1:Identifier><p0:Data><p0' ':LiteralData>$table_to_export</p0:LiteralData></p0:Data></p0:Input><p0:Input><p1:Identifier ' 'xmlns:p1="http://www.opengis.net/ows/1.1">exportPath</p1:Identifier><p0:Data><p0:LiteralData' '>$export_path</p0:LiteralData></p0:Data></p0:Input></p0:DataInputs><p0:ResponseForm><p0' ':RawDataOutput><p1:Identifier ' 'xmlns:p1="http://www.opengis.net/ows/1.1">result</p1:Identifier></p0:RawDataOutput></p0' ':ResponseForm></p0:Execute>') def call_geoserver(data): req = urllib.request.Request(url='http://localhost:9580/geoserver/ows', data=bytes(data, encoding="utf8"), method='POST') req.add_header('Content-Type', 'application/xml; charset=utf-8') with urllib.request.urlopen(req) as f: print(f.status) print(f.reason) if f.status == 200: print(str(f.read(), encoding="utf8")) call_geoserver(import_file.substitute({"path": "data_dir/data/wpsdata/buildings.shp"})) call_geoserver(import_file.substitute({"path": "data_dir/data/wpsdata/ground_type.shp"})) call_geoserver(import_file.substitute({"path": "data_dir/data/wpsdata/receivers.shp"})) call_geoserver(import_file.substitute({"path": "data_dir/data/wpsdata/roads.shp"})) call_geoserver(import_file.substitute({"path": "data_dir/data/wpsdata/dem.geojson"})) call_geoserver(get_lday.substitute({"table_receivers": "RECEIVERS", "table_buildings": "BUILDINGS" , "table_roads": "ROADS", "table_dem": "DEM"})) call_geoserver(export_table.substitute({"table_to_export": "LDAY_GEOM", "export_path" : "lday_geom.shp"})) |