Statistics
| Revision:

root / trunk / extensions / extRemoteSensing / src / org / gvsig / remotesensing / decisiontrees / DecisionTree.java @ 20872

History | View | Annotate | Download (6.69 KB)

1 17088 gsdiego
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
         *
3
         * Copyright (C) 2006 Instituto de Desarrollo Regional and Generalitat Valenciana.
4
         *
5
         * This program is free software; you can redistribute it and/or
6
         * modify it under the terms of the GNU General Public License
7
         * as published by the Free Software Foundation; either version 2
8
         * of the License, or (at your option) any later version.
9
         *
10
         * This program is distributed in the hope that it will be useful,
11
         * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
         * GNU General Public License for more details.
14
         *
15
         * You should have received a copy of the GNU General Public License
16
         * along with this program; if not, write to the Free Software
17
         * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
         *
19
         * For more information, contact:
20
         *
21
         *  Generalitat Valenciana
22
         *   Conselleria d'Infraestructures i Transport
23
         *   Av. Blasco Iba?ez, 50
24
         *   46010 VALENCIA
25
         *   SPAIN
26
         *
27
         *      +34 963862235
28
         *   gvsig@gva.es
29
         *      www.gvsig.gva.es
30
         *
31
         *    or
32
         *
33
         *   Instituto de Desarrollo Regional (Universidad de Castilla La-Mancha)
34
         *   Campus Universitario s/n
35
         *   02071 Alabacete
36
         *   Spain
37
         *
38
         *   +34 967 599 200
39
         */
40 16928 gsdiego
package org.gvsig.remotesensing.decisiontrees;
41
42 20339 dguerrero
import java.awt.Color;
43
import java.util.HashMap;
44
import java.util.Iterator;
45
46
import com.iver.utiles.IPersistence;
47
import com.iver.utiles.XMLEntity;
48
49 17088 gsdiego
/**
50
 * Clase que representa un ?rbol de decisi?n
51
 *
52
 * @author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
53
 *
54
 */
55 20339 dguerrero
public class DecisionTree implements IPersistence{
56
57
        private DecisionTreeNode                 root                         = null;
58
        /**
59
         * Tabla de colores: Integer,Color
60
         */
61
        private HashMap                                        colorTable                 = null;
62
        /**
63
         * Tabla de variables: "varname","layerName[BandN]"
64
         */
65
        private HashMap                                        variablesTable         = null;
66 16928 gsdiego
67 20339 dguerrero
68
        public DecisionTree(){
69
70
        }
71
72
        public DecisionTree(DecisionTreeNode root) {
73
                this.root = root;
74
        }
75
76
        public String getClassName() {
77
                return getClass().getName();
78
        }
79
80
        public XMLEntity getXMLEntity() {
81
82
                if (root!=null){
83
                        XMLEntity xml = new XMLEntity();
84
                        XMLEntity xmlDedisionTree = new XMLEntity();
85
                        xmlDedisionTree.setName("decision_tree");
86
87
                        XMLEntity xmlTree = getXmlTree(root);
88
                        xmlDedisionTree.addChild(xmlTree);
89
90
                        if (variablesTable!=null){
91
                                XMLEntity xmlVars = getXmlVariablesTable();
92
                                xmlDedisionTree.addChild(xmlVars);
93
                        }
94
95
                        if (colorTable != null){
96
                                XMLEntity xmlColorTable = getXmlColorTable();
97
                                xmlDedisionTree.addChild(xmlColorTable);
98
                        }
99
100
                        xml.addChild(xmlDedisionTree);
101
                        return xml;
102
                }
103
                else return null;
104
        }
105
106
        public void setXMLEntity(XMLEntity xml) {
107
                XMLEntity xmlDecisionTree = xml.getChild(0);
108
                XMLEntity xmlTree = xmlDecisionTree.firstChild("name", "tree");
109
                if (xmlTree!=null){
110
                        root = setXMLTree(xmlTree);
111
                }
112
113
                XMLEntity xmlVars = xmlDecisionTree.firstChild("name", "variables");
114
                if (xmlVars!=null){
115
                        getVariablesTable().clear();
116
                        XMLEntity xmlVar = null;
117
                        String layerBand = "";
118
                        for (int i=0; i<xmlVars.getChildrenCount();i++){
119
                                xmlVar = xmlVars.getChild(i);
120
                                layerBand = xmlVar.getStringProperty("layer")+"["+"Band"+xmlVar.getStringProperty("band")+"]";
121
                                getVariablesTable().put(xmlVar.getObjectProperty("var"), layerBand);
122
                        }
123
                }
124
125
                XMLEntity xmlColorTable = xmlDecisionTree.firstChild("name", "color_table");
126
                if (xmlColorTable!=null){
127
                        getColorTable().clear();
128
129
                        XMLEntity xmlColor = null;
130
                        Integer classID = null;
131
                        Color color = null;
132
                        int r,g,b;
133
                        for (int i=0; i<xmlColorTable.getChildrenCount();i++){
134
                                xmlColor = xmlColorTable.getChild(i);
135
                                r = xmlColor.getIntProperty("red");
136
                                g = xmlColor.getIntProperty("green");
137
                                b = xmlColor.getIntProperty("blue");
138
                                color = new Color(r,g,b);
139
                                classID = new Integer(xmlColor.getIntProperty("class_id"));
140
                                getColorTable().put(classID, color);
141
                        }
142
                }
143
        }
144
145
        private DecisionTreeNode setXMLTree(XMLEntity xmlTree) {
146
                DecisionTreeNode treeNode = new DecisionTreeNode();
147
                if (xmlTree.contains("expression")){
148
                        treeNode.setExpression(xmlTree.getStringProperty("expression"));
149
                        DecisionTreeNode leftChild = setXMLTree(xmlTree.getChild(0));
150
                        DecisionTreeNode rightChild = setXMLTree(xmlTree.getChild(1));
151
                        treeNode.setChildren(leftChild,rightChild);
152
                }
153
                else
154
                        treeNode.setClassID(xmlTree.getIntProperty("value"));
155
156
                return treeNode;
157
        }
158
159
        public HashMap getColorTable() {
160
                if (colorTable == null)
161
                        colorTable = new HashMap();
162
                return colorTable;
163
        }
164
165
        public void setColorTable(HashMap colorTable) {
166
                this.colorTable = colorTable;
167
        }
168
169
        public DecisionTreeNode getRoot() {
170
                if (root == null)
171
                        root = new DecisionTreeNode();
172
                return root;
173
        }
174
175
        public void setRoot(DecisionTreeNode root) {
176
                this.root = root;
177
        }
178
179
        public HashMap getVariablesTable() {
180
                if (variablesTable==null)
181
                        variablesTable = new HashMap();
182
                return variablesTable;
183
        }
184
185
        public void setVariablesTable(HashMap variablesTable) {
186
                this.variablesTable = variablesTable;
187
        }
188
189
        private XMLEntity getXmlTree(DecisionTreeNode treeNode){
190
                XMLEntity xml = new XMLEntity ();
191
                xml.setName("tree");
192
                if (treeNode.isFinal()){
193
                        xml.putProperty("value", treeNode.getClassID());
194
                }
195
                else{
196
                        xml.putProperty("expression", treeNode.getExpression());
197
                        xml.addChild(getXmlTree(treeNode.getLeftChild()));
198
                        xml.addChild(getXmlTree(treeNode.getRightChild()));
199
                }
200
                return xml;
201
        }
202
203
        private XMLEntity getXmlVariablesTable(){
204
                XMLEntity xml = new XMLEntity ();
205
                xml.setName("variables");
206
                for (Iterator iter = variablesTable.keySet().iterator(); iter.hasNext();) {
207
                        String var  = (String) iter.next();
208
                        String layer = (String)variablesTable.get(var);
209
                        String layerName = layer.substring(0,layer.indexOf("["));
210
                        int band = Integer.valueOf(layer.substring(layer.lastIndexOf("Band")+4,layer.lastIndexOf("]"))).intValue();
211
                        XMLEntity variableXml = new XMLEntity();
212
                        variableXml.setName("variable");
213
                        variableXml.putProperty("var", var);
214
                        variableXml.putProperty("layer", layerName);
215
                        variableXml.putProperty("band", band);
216
                        xml.addChild(variableXml);
217
                }
218
                return xml;
219
        }
220
221
        private XMLEntity getXmlColorTable(){
222
                XMLEntity xml = new XMLEntity ();
223
                xml.setName("color_table");
224
                for (Iterator iterator = colorTable.keySet().iterator(); iterator.hasNext();) {
225
                        Integer classId = (Integer) iterator.next();
226
                        Color classColor = (Color)colorTable.get(classId);
227
                        XMLEntity colorXml = new XMLEntity();
228
                        colorXml.setName("color_entry");
229
                        colorXml.putProperty("class_id", classId.intValue());
230
                        colorXml.putProperty("red", classColor.getRed());
231
                        colorXml.putProperty("green", classColor.getGreen());
232
                        colorXml.putProperty("blue", classColor.getBlue());
233
                        xml.addChild(colorXml);
234
                }
235
                return xml;
236
        }
237
238 16928 gsdiego
}