source: TI02-CSML/trunk/services/3rdParty/pywps-1.0.0/pywps/processes/classify.py @ 2194

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/services/3rdParty/pywps-1.0.0/pywps/processes/classify.py@2194
Revision 2194, 5.0 KB checked in by lawrence, 13 years ago (diff)

Adding various specs and 3rd party code of interest for the CSML
services development.

Line 
1#!/usr/bin/python
2"""
3pywps process example:
4
5classify: Classify satellite image
6"""
7# Author:       Stepan Kafka
8# Lince:
9#
10# Web Processing Service implementation
11# Copyright (C) 2006 Jachym Cepicky
12#
13# This program is free software; you can redistribute it and/or modify
14# it under the terms of the GNU General Public License as published by
15# the Free Software Foundation; either version 2 of the License.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
25
26import os,time,string,sys
27
28class Process:
29#####################################################################
30#
31# Configuration part of the process
32#
33#####################################################################
34    def __init__(self):
35        #
36        # Mandatory parameters
37        #
38        # Identifier - name of this process
39        self.Identifier = "classify"
40        # processVersion - version of this process
41        self.processVersion = "0.1"
42        # Title - title for this process
43        self.Title="Image classification"
44        self.Abstract="GRASS processed imagery classification. Only unsupervised is supported at the moment."
45        #
46        # Inputs
47        # Inputs is an array of input structure
48        # Inputs = [ {input1},{input2},{...} ]
49        #
50        self.Inputs = [
51                # This module has 2 inputs:
52                # 1) Input raster map
53                # 2) Value to be added
54       
55                # First input
56                    {
57                        'Identifier':'input',
58                        'Title': 'Input raster map',
59                        'ComplexValueReference': {
60                            'Formats': [ "image/tiff" ]
61                        },
62                        'value':None,
63                    },
64       
65                # Second input
66                    {
67                        'Identifier': 'classes',
68                        'Title': 'Number of classes',
69                        'Abstract':'Unsupervised classification number of classes',
70                        'LiteralData': {
71                            'values':["*"], # values
72                        },
73                        'dataType' : type(0), # you should set this for input params controll
74                        'value':20,
75                    },
76                ]
77       
78        #
79        # Output
80        # The structure is not much different from the input structure
81        #
82        self.Outputs = [
83            {
84                'Identifier': 'output',
85                'Title': 'Resulting output map',
86                'ComplexValueReference': {
87                    'Formats':["image/tiff"],
88                },
89                'value':None,
90            },
91        ]
92       
93        #
94        # Optional attributes
95        #
96        #
97        # storeSuported = "true" or "false" - should the resulting map be stored on our disk?
98        self.storeSupported = "true"
99       
100        #
101        # statusSupported = "true" or "false" - if statusLocation is set, the server
102        # will not wait for the end of the operation and will return the
103        # ExectuceResponce XML file immediately, without the ProcessOutput section
104        # but with the statusLocation parameter
105        self.statusSupported = "true"
106        # and many others
107       
108    #####################################################################
109    #
110    # Execute part of the process
111    #
112    #####################################################################
113    def execute(self):
114        """
115        This function
116            1) Imports the raster map
117            2) runs r.mapcalc out=in+value
118            3) Exports the raster map
119            4) returns the new file name or 'None' if something went wrong
120        """
121       
122        os.system("r.in.gdal -o -k input=%s output=input 1>&2" % (self.Inputs[0]['value']))               
123        os.system("g.region -p rast=input.1 1>&2")
124        os.system("i.group group=gr subgroup=klas input=`g.mlist type=rast sep=',' pattern='input.*'` 1>&2")
125        os.system("i.cluster group=gr subgroup=klas sigfile=sig classes=%s 1>&2" % (self.Inputs[1]['value']))
126        os.system("i.maxlik -q group=gr subgroup=klas sigfile=sig class=classif reject=rejected 1>&2")
127        os.system("r.out.tiff -t input=classif output=output0 1>&2")
128        os.system("/usr/local/bin/gdal_translate -of GTiff output0.tif output.tif  1>&2")
129       
130        # check the resulting file or any other variable, which interrests you
131        if "output.tif" in os.listdir(os.curdir):
132            self.Outputs[0]['value'] = "output.tif"
133            return
134        else:
135            return "Output file not created"
136       
Note: See TracBrowser for help on using the repository browser.