ADDON Profile

From PyWPS wiki

Jump to: navigation, search
"""
The process create a table and a plot of the elevation profile between 2 points
"""
# Author:	Luca Casagrande
#        	http://www.ominiverdi.org
# Lince: 
# 
# Your Process Description
# Copyright (C) 2006 Your Name
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

# PREREQUISITES
# 1) PyWPS: http://pywps.wald.intevation.org
# 2) Matplotlib http://matplotlib.sourceforge.net/
# 3) GRASS  6.2
#
# INSTALLING INTO PYWPS
# following pywps instructions to put this short program into pywps/processes/
# adding 'profile.py' into pywps/process/__init__.py
#
#
import os,time,string,sys,pylab

X=pylab.zeros(0,pylab.Float)

class Process:
	def __init__(self):
		self.Identifier = "profile"
		self.processVersion = "0.1"
		self.storeSupported = "true"
		self.statusSupported = "true"
		self.Title="Elavation profile between 2 points"
		self.Abstract="Print a table with the profile between 2 points"
		self.grassLocation="/home/geko/progetti/grass/dati/spearfish61/"
		self.Inputs = [
                    # 0
                    {
                        'Identifier': 'x1',
                        'Title': 'Start x coordinate',
                        'LiteralValue': {
                        },
                        'dataType': type(0.0),
                    },
                    # 1
                     {
                        'Identifier': 'y1',
                        'Title': 'Start y coordinate',
                        'LiteralValue': {
                            'AnyValue':None, 
                        },
                        'dataType': type(0.0),
                    },
		    # 2
                     {
                        'Identifier': 'x2',
                        'Title': 'End x coordinate',
                        'LiteralValue': {
                            'AnyValue':None, 
                        },
                        'dataType': type(0.0),
                    },
                    # 3
                     {
                        'Identifier': 'y2',
                        'Title': 'End y coordinate',
                        'LiteralValue': {
                            'AnyValue':None, 
                        },
                        'dataType': type(0.0),
                    },
                  
			]
		self.Outputs = [
			{
			'Identifier': 'output',
			'Title': 'Profile table',
			'ComplexValueReference': {
			'Formats':["text/xml"],
			}
			},
			
			{
			'Identifier': 'profile',
			'Title': 'Profile graph',
			'ComplexValueReference': {
			'Formats':["image/png"],
			}
			},
		]	    
	
	def execute(self):
	     os.system("g.region -d")
	     
	     if os.system("r.profile -g input=elevation.dem output=output.txt profile=%s,%s,%s,%s null=* 1>&2" %\
			     (self.Inputs[0]['value'],self.Inputs[1]['value'],self.Inputs[2]['value'],self.Inputs[3]['value'])):
		return "Could not create the profile"
	
	     self.Outputs[0]['value'] = "output.txt"
	     X=pylab.load('output.txt')
	     t=X[:,2]
	     s=X[:,3]
	     pylab.title('Profile Graph')
	     pylab.xlabel('Distance from source (m)')
             pylab.ylabel('Elevation (m)')
             pylab.plot(t,s)
             pylab.grid(True)
             pylab.savefig('profile.png')
	     
	     if "profile.png" in os.listdir(os.curdir):
			self.Outputs[1]['value'] = "profile.png"
			return
	     else:
			return "Profile plot not created"
Personal tools