Solved gnuplot display bug
[alpinway.git] / processes / shortestpath.py
blobe57b28290d05bb698c6db6b22ddc29d3e5cf253a
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.x1 = self.addLiteralInput(identifier ="x1",
20 title = "Start x coordinate",
21 type = type(0.0))
23 self.y1 = self.addLiteralInput(identifier ="y1",
24 title = "Start y coordinate",
25 type = type(0.0))
27 self.x2 = self.addLiteralInput(identifier ="x2",
28 title = "End x coordinate",
29 type = type(0.0))
31 self.y2 = self.addLiteralInput(identifier ="y2",
32 title = "End y coordinate",
33 type = type(0.0))
35 self.cost = self.addLiteralInput(identifier ="cost",
36 title = "Start x coordinate",
37 type = type(0.0))
39 self.los = self.addComplexOutput(identifier = "los",
40 title = "Resulting output map",
41 formats = [{"mimeType":"text/xml"}])
43 def execute(self):
44 self.cmd("g.mapset mapset=nico")
45 self.cmd("g.region -d")
46 self.status.set(msg="Region setted", percentDone=20)
47 # self.cmd("v.buffer input=roads output=roads_buff buffer=100 scale=1.0 tolerance=0.01")
48 # if int(self.cost.value) == 1:
49 # self.cmd("echo '0 %s %s %s %s ' | v.net.path in=roads_buff out=path afcolumn=COST2 dmax=5000 abcolumn=COST2 1>&2" %\
50 # (self.x1.value,self.y1.value,self.x2.value,self.y2.value))
51 # else:
52 # self.cmd("v.net.path in=roads out=path dmax=5000 --o >&2" %\
53 # self.cmd("echo 0 %s %s %s %s | v.net.path input=roads output=path --o" %\
54 # self.cmd("v.net.path input=allgpx output=path ","1 %s %s %s %s" %
55 #(self.x1.value,self.y1.value,self.x2.value,self.y2.value))
56 self.cmd("echo ""0 %s %s %s %s"" | v.net.path input=allgpx output=path --o" % (self.x1.value,self.y1.value,self.x2.value,self.y2.value))
57 self.status.set(msg="Path created",percentDone=40)
58 # self.cmd("r.profile input=elevation.10m output=app.p profile=591525.38,4914696.00,600678.75,4925038.12")
59 # self.cmd("echo $(tail -n +13 /home/nico/app.point | head -n -2 | tr " " "," | tr \"\n\" \" \" | tr -d \" \" | tail -c +2) > /home/nico/point")
60 self.cmd("v.out.ascii input=path output=/tmp/grass/app.point format=standard")
61 i = 0
62 coords=''
63 f=open('/tmp/grass/app.point', 'r')
64 h=f.readlines()
65 l=len(h)
66 for line in h:
67 i = i+1
68 #jump to the 14th row
69 #check if the coordinates are correct
70 #print line.strip()
71 if ((i>12)&(i<l-2)):
72 coords=coords+re.sub(' +',',',line.strip())+','
73 coords=coords.rstrip(',')
74 # print coords
75 f.close();
76 #Elaboro path per avere le coordinate necessarie
77 self.cmd("r.profile input=elevation profile=%s output=/tmp/grass/path.profile" % coords)
78 self.status.set(msg="Altimetric Profile created",percentDone=60)
79 g = Gnuplot.Gnuplot()
80 g.title('Profilo altimetrico del sentiero')
81 g.xlabel('Distanza percorsa')
82 g.ylabel('Altitudine')
83 g('set terminal unknown')
84 g('set data style lines')
85 g.plot(Gnuplot.File('/tmp/grass/path.profile', title=None))
86 g.hardcopy('/tmp/grass/tmp.png',terminal = 'png')
87 self.status.set(msg="Gnuplot image generated",percentDone=70)
88 self.cmd("v.out.ogr format=GML input=path dsn=out.xml olayer=path.xml")
89 self.status.set(msg="Results saved", percentDone=80)
92 if "out.xml" in os.listdir(os.curdir):
93 self.los.setValue("out.xml")
94 return
95 else:
96 return "Output file not created"