Different argument passing into pywps process
[alpinway.git] / processes / shortestpath.py
blob30942a99d925fe74341a991c0574b49138ca7c86
1 #!/usr/bin/python
3 # Author: Stepan Kafka
5 import os,time,string,sys,Gnuplot,re
6 from pywps.Process.Process import WPSProcess
8 class Process(WPSProcess):
9 def __init__(self):
10 WPSProcess.__init__(self,
11 identifier = "shortestpath",
12 title="Shortest path",
13 abstract="Find the shortest path on the Trentino SAT network",
14 version = "0.2",
15 storeSupported = True,
16 statusSupported = True,
17 grassLocation="alpi")
19 self.cost = self.addLiteralInput(identifier ="cost",
20 title = "Start x coordinate",
21 type = type(0.0),
22 maxOccurs = 200)
23 self.coord = self.addLiteralInput(identifier ="coord",
24 title = "Points",
25 type = type("*"),
26 minOccurs =4,
27 maxOccurs =200)
29 self.los = self.addComplexOutput(identifier = "los",
30 title = "Resulting output map",
31 formats = [{"mimeType":"text/xml"}])
33 def execute(self):
35 self.cmd("g.mapset mapset=nico")
36 self.cmd("g.region -d")
37 self.status.set(msg="Region setted", percentDone=20)
38 lpoints=self.coord.value[0].split(",")
40 #Points are stored in lpoints
41 #we must find every shortest path between two consecutive point and
42 #put all together
43 npoints=len(lpoints)/2
44 #the number of loop cycle in which shortest path is called is npoints-1
45 x1=lpoints[0]
46 y1=lpoints[1]
47 for i in range(2,2*npoints-1,2):
48 x2=lpoints[i]
49 y2=lpoints[i+1]
50 #Find the shortest path between the two points
51 self.cmd("echo ""0 %s %s %s %s"" | v.net.path input=allgpx output=path --o" % (x1,y1,x2,y2))
52 #print "echo shortest between"+x1+","+y1+":"+x2+","+y2
54 #self.cmd("v.overlay")
55 #add to the final path the segment
57 x1=x2
58 y1=y2
60 self.status.set(msg="Path created",percentDone=40)
61 self.cmd("v.out.ascii input=path output=/tmp/grass/app.point format=standard")
62 i = 0
63 coords=''
64 f=open('/tmp/grass/app.point', 'r')
65 h=f.readlines()
66 l=len(h)
67 for i in range(13,l-2):
68 #jump to the 13th row
69 coords=coords+re.sub(' +',',',h[i].strip())+','
70 coords=coords.rstrip(',')
71 f.close();
72 self.cmd("r.profile input=elevation profile=%s output=/tmp/grass/path.profile" % coords)
73 self.status.set(msg="Altimetric Profile created",percentDone=60)
74 g = Gnuplot.Gnuplot()
75 g.title('Profilo altimetrico del sentiero')
76 g.xlabel('Distanza percorsa')
77 g.ylabel('Altitudine')
78 g('set terminal unknown')
79 g('set data style lines')
80 g.plot(Gnuplot.File('/tmp/grass/path.profile', title=None))
81 g.hardcopy('/tmp/grass/profile.png',terminal = 'png')
82 self.status.set(msg="Gnuplot image generated",percentDone=70)
83 self.cmd("v.out.ogr format=GML input=path dsn=out.xml olayer=path.xml")
84 self.status.set(msg="Results saved", percentDone=80)
87 if "out.xml" in os.listdir(os.curdir):
88 self.los.setValue("out.xml")
89 return
90 else:
91 return "Output file not created"