Vypublikování skriptu jako WPS procesu¶
Skript upravíme následovně:
- Vytvoříme třídu
Processs rodičovskou třídouWPSProcess, která je definována v rámci PyWPS (řádky 17, 19 a 21-28). - Definujeme vstupní (řádky 30-32) a výstupní (řádky 34-37) parametry WPS procesu
- Implementujeme funkce
export()(39), která výstupní vektorovou mapu exportuje do souboru ve formátu ESRI Shapefile, který bude zkomprimován a poslán klientovi. - Implementujeme funkci
execute()(61), která se vykoná v okamžiku, kdy od klienta dorazí na server dotaz typurequest=execute. - Vlastní tělo původního skriptu vnoříme do funkce
run()(66).
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import logging
import types
from zipfile import ZipFile
gisbase = os.environ['GISBASE'] = "/usr/lib/grass72"
sys.path.append(os.path.join(os.environ["GISBASE"], "etc", "python"))
os.environ['LD_LIBRARY_PATH'] = os.path.join(os.environ["GISBASE"], "lib")
from grass.pygrass.modules import Module
from grass.pygrass.vector import VectorTopo
from pywps.Process import WPSProcess
class Process(WPSProcess):
def __init__(self):
WPSProcess.__init__(self,
identifier='obce_psc',
version="1.0",
title="Dotaz na obce podle PSČ",
abstract="Testovací služba PyWPS.",
grassLocation='gismentors_pywps',
storeSupported=True,
statusSupported=True)
self.input_psc = self.addLiteralInput(identifier = "psc",
title = "Zájmové PSČ",
type = types.StringType)
self.output = self.addComplexOutput(identifier = "output",
title = "Output zipped shapefile",
formats = [ {"mimeType":"application/x-zipped-shp"} ],
asReference = True)
def export(self, map_name):
output_dir = os.path.join('/tmp', '{}_{}'.format(map_name, os.getpid()))
os.mkdir(output_dir)
output_file = '{}/{}.zip'.format(output_dir, map_name)
logging.debug("Export started")
Module('v.out.ogr',
input=map_name,
output='{}/{}.shp'.format(output_dir, map_name),
overwrite=True)
os.chdir(output_dir)
with ZipFile(output_file, 'w') as shpzip:
shpzip.write('{}.dbf'.format(map_name))
shpzip.write('{}.shp'.format(map_name))
shpzip.write('{}.shx'.format(map_name))
shpzip.write('{}.prj'.format(map_name))
logging.debug("Export finished")
self.output.setValue(output_file)
def execute(self):
map_name = self.run()
self.export(map_name)
def run(self):
logging.debug("Computation started")
psc = self.input_psc.getValue()
map_name = 'obce_psc_{}'.format(psc)
obce = VectorTopo('obce', mapset='psc')
obce.open('r')
vystup = VectorTopo(map_name)
vystup.open('w', tab_cols=[('cat', 'INTEGER PRIMARY KEY'),
('nazev', 'TEXT'),
('psc', 'INTEGER')])
obec_id = None
obce_psc = set()
for prvek in obce.viter('areas'):
if prvek.attrs is None:
continue
if prvek.attrs['psc'] == psc:
if obec_id is None:
obec_id = prvek.id
for b in prvek.boundaries():
for n in b.read_area_ids():
if n != -1 and n != obec_id:
obce_psc.add(n)
obce_psc.add(obec_id)
hranice = list()
cat = 1
for prvek in obce.viter('areas'):
if prvek.id not in obce_psc:
continue
for b in prvek.boundaries():
if b.id not in hranice:
hranice.append(b.id)
vystup.write(b)
vystup.write(prvek.centroid(), cat=cat, attrs=(prvek.attrs['nazev'], prvek.attrs['psc']))
cat += 1
vystup.table.conn.commit()
vystup.close()
obce.close()
logging.debug("Computation finished")
return map_name
if __name__ == "__main__":
process = Process()
process.execute()
|