from pywps.Process.Process import WPSProcess class Process(WPSProcess): """Main process class""" def __init__(self): """Process initialization""" from types import * import os os.environ['PYTHON_EGG_CACHE'] = '/tmp' # init process WPSProcess.__init__(self, identifier = "testRProcess", title="R", version = "0.1", storeSupported = "true", statusSupported = "true", abstract="Essai de discretisation de Jenks avec R", grassLocation = False) # process inputs # complex input self.dataIn = self.addComplexInput(identifier="donnees", title = "Variable a classer", # some optional parameters abstract="Variable au format XML (liste des individus)", # default is empty #metadata=[{"foo":"bar"}], # default is empty formats=[{"mimeType":"text/xml"}], # default value maxOccurs=1, # default value maxmegabites="2") # default maximum size # string input self.nbClassesIn = self.addLiteralInput(identifier = "nbClasses", title = "Nombre de classes",abstract="Nombre de classes a produire.", maxOccurs=1) self.methodeIn = self.addLiteralInput(identifier = "methode", title = "Methode de classification",abstract="Methode de discretisation a utiliser.", maxOccurs=1, type = StringType, allowedValues = ["fixed", "sd", "equal", "pretty", "quantile", "kmeans", "hclust", "bclust", "fisher", "jenks"], default = "jenks") # process outputs # complex output #self.classesOut = self.addComplexOutput(identifier="classes", # title="Liste des classes calculees") # literal output self.textOut = self.addLiteralOutput(identifier="text", title="Resultats") def execute(self): """Execute process. Each command will be executed and output values will be set """ # run some command from the command line self.status.set("Lancement de la commande",20) #out = self.cmd(["rj"]) import rpy2.robjects as robjects robjects.r('library(e1071)') robjects.r('library(classInt)') robjects.r('library(XML)') robjects.r('data(jenks71)') robjects.r('doc = xmlRoot(xmlTreeParse("%s"))' % self.getInputValue('donnees')) print 'Debug' xsa = robjects.r.xmlSApply jenksData = robjects.r('xmlSApply(doc, function(x) as.numeric(xmlSApply(x, xmlValue)))') ci = robjects.r.classIntervals out = str(ci(jenksData, n = self.getInputValue('nbClasses'), style = self.getInputValue('methode'))) # setting output values self.textOut.setValue(out)