Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / rendering / VectorialIntervalLegend.java @ 11558

History | View | Annotate | Download (11.3 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. 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 Ib??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
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.fmap.rendering;
42

    
43
import java.awt.Color;
44

    
45
import com.iver.cit.gvsig.fmap.core.ISLDCompatible;
46
import com.iver.cit.gvsig.fmap.core.SLDTags;
47
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
48
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
49
import com.iver.cit.gvsig.fmap.layers.XMLException;
50
import com.iver.utiles.StringUtilities;
51
import com.iver.utiles.XMLEntity;
52

    
53

    
54
/**
55
 * <p>VectorialIntervalLegend (which should be better called GraduatedColorLegend) is
56
 * a legend that allows to classify ranges of values using a color gradient based
57
 * on a value.<b>
58
 * </p>
59
 * <p>
60
 * The color gradient will be calculated according the starting color, the
61
 * ending color and the amount of intervals.
62
 * </p>
63
 * @author  Vicente Caballero Navarro
64
 */
65
public class VectorialIntervalLegend extends AbstractIntervalLegend {
66
    private Color startColor = Color.red;
67
    private Color endColor = Color.blue;
68
    private int shapeType;
69

    
70
    /**
71
     * Crea un nuevo VectorialIntervalLegend.
72
     */
73
    public VectorialIntervalLegend() {
74
        //defaultSymbol = LegendFactory.DEFAULT_POLYGON_SYMBOL;
75
    }
76

    
77
    /**
78
     * Crea un nuevo VectorialIntervalLegend.
79
     *
80
     * @param type tipo de shape.
81
     */
82
    public VectorialIntervalLegend(int type) {
83
        setShapeType(type);
84
    }
85

    
86
    /**
87
         * creates the SLD String that defines this legend type.
88
         */
89
    public String getSLDString(String layerName) {
90

    
91
            try{
92

    
93
                        XmlBuilder xmlBuilder = new XmlBuilder();
94
                        xmlBuilder.writeHeader();
95
                        xmlBuilder.openTag(SLDTags.SLD_ROOT, SLDTags.VERSION_ATTR, SLDTags.VERSION_1_0_0);
96
                        xmlBuilder.openTag(SLDTags.NAMEDLAYER);
97
                        xmlBuilder.writeTag(SLDTags.NAME,layerName);
98
                        xmlBuilder.openTag(SLDTags.USERSTYLE);
99
                        xmlBuilder.openTag(SLDTags.FEATURETYPESTYLE);
100
                        xmlBuilder.writeTag(SLDTags.FEATURETYPENAME,fieldName);
101

    
102
                        ISymbol[] symbols = this.getSymbols();
103
                        Object[] values = this.getValues();
104

    
105
                        FInterval interval;
106
                        for(int i = 0; i < symbols.length; i++ )
107
                        {
108
                                interval = (FInterval)values[i];
109
                                //interval = (FInterval)getInterval(ValueFactory.createValue(valueDbl.doubleValue()));
110
                                xmlBuilder.openTag(SLDTags.RULE);
111
                                xmlBuilder.writeTag(SLDTags.NAME, ""+interval.getMin() +" - " +interval.getMax());
112
                                xmlBuilder.openTag(SLDTags.FILTER);
113
                                xmlBuilder.openTag(SLDTags.AND);
114
                                xmlBuilder.openTag(SLDTags.PROPERTYISGREATEROREQUALTHAN);
115
                                xmlBuilder.writeTag(SLDTags.PROPERTYNAME,fieldName);
116
                                xmlBuilder.writeTag(SLDTags.LITERAL, ""+interval.getMin());
117
                                xmlBuilder.closeTag();
118
                                xmlBuilder.openTag(SLDTags.PROPERTYISLESSOREQUALTHAN);
119
                                xmlBuilder.writeTag(SLDTags.PROPERTYNAME,fieldName);
120
                                xmlBuilder.writeTag(SLDTags.LITERAL, ""+interval.getMax());
121
                                xmlBuilder.closeTag();
122
                                xmlBuilder.closeTag();
123
                                if (symbols[i] instanceof ISLDCompatible)
124
                                {
125
                                        ISLDCompatible symSLD = (ISLDCompatible) symbols[i];
126
                                        xmlBuilder.writeRaw(symSLD.toSLD());
127
                                }
128
                                else
129
                                        throw new RuntimeException("Cannot convert Symbol " + i + " " + symbols[i].getDescription() + " to SLD");
130
                                xmlBuilder.closeTag();
131
                                xmlBuilder.closeTag();
132
                        }
133

    
134
                        xmlBuilder.closeTag();
135
                        xmlBuilder.closeTag();
136
                        xmlBuilder.closeTag();
137
                        xmlBuilder.closeTag();
138
                        return xmlBuilder.getXML();
139

    
140
            }catch(Exception e)
141
            {
142
                    e.printStackTrace();
143
                    return null;
144
            }
145
    }
146

    
147
    /**
148
     * DOCUMENT ME!
149
     *
150
     * @return DOCUMENT ME!
151
     *
152
     * @see com.iver.cit.gvsig.fmap.rendering.ILegend#getXMLEntity()
153
     */
154
    public XMLEntity getXMLEntity() {
155
        XMLEntity xml = new XMLEntity();
156
        xml.putProperty("className", this.getClass().getName());
157
        xml.putProperty("useDefaultSymbolB", useDefaultSymbol);
158
        if (getDefaultSymbol() == null) {
159
            xml.putProperty("useDefaultSymbol", 0);
160
        } else {
161
            xml.putProperty("useDefaultSymbol", 1);
162
            xml.addChild(getDefaultSymbol().getXMLEntity());
163
        }
164

    
165
        xml.putProperty("fieldName", fieldName);
166
        xml.putProperty("index", index);
167
        xml.putProperty("labelfield", labelFieldName);
168
        xml.putProperty("labelfield", labelFieldName);
169
        xml.putProperty("labelFieldHeight", labelFieldHeight);
170
        xml.putProperty("labelFieldRotation", labelFieldRotation);
171

    
172
        xml.putProperty("intervalType", intervalType);
173
        xml.putProperty("numKeys", keys.size());
174

    
175
        if (keys.size() > 0) {
176
            xml.putProperty("tipoValueKeys", keys.get(0).getClass().getName());
177

    
178
            String[] sk = new String[keys.size()];
179

    
180
            for (int i = 0; i < keys.size(); i++) {
181
                sk[i] = ((IInterval) keys.get(i)).toString();
182
            }
183

    
184
            xml.putProperty("keys", getValues());
185

    
186
            for (int i = 0; i < keys.size(); i++) {
187
                xml.addChild(getSymbols()[i].getXMLEntity());
188
            }
189
        }
190

    
191
        xml.putProperty("startColor", StringUtilities.color2String(startColor));
192
        xml.putProperty("endColor", StringUtilities.color2String(endColor));
193

    
194
        ///xml.putProperty("numSymbols", symbols.size());
195
        ///xml.putProperty("indexs", getIndexs());
196
        ///xml.putProperty("values", getValues());
197
        return xml;
198
    }
199

    
200
    /**
201
     * Inserta los atributos del XMLEntity.
202
     *
203
     * @param xml XMLEntity.
204
     */
205
    public void setXMLEntity03(XMLEntity xml) {
206
        fieldName = xml.getStringProperty("fieldName");
207
        index = xml.getIntProperty("index");
208
        labelFieldName = xml.getStringProperty("labelfield");
209

    
210
        if (xml.contains("intervalType")) { //TODO Esta condici?n es para poder cargar la versi?n 0.3, se puede eliminar cuando ya no queramos soportar esta versi?n.
211
            intervalType = xml.getIntProperty("intervalType");
212
        }
213

    
214
        int useDefaultSymbol = xml.getIntProperty("useDefaultSymbol");
215

    
216
        if (useDefaultSymbol == 1) {
217
            setDefaultSymbol( SymbologyFactory.createSymbolFromXML(xml.getChild(0), null));
218
        } else {
219
            setDefaultSymbol(null);
220
        }
221

    
222
        int numKeys = xml.getIntProperty("numKeys");
223

    
224
        if (numKeys > 0) {
225
            String className = xml.getStringProperty("tipoValueKeys");
226
            String[] sk = xml.getStringArrayProperty("keys");
227
            IInterval auxInterval;
228

    
229
            for (int i = 0; i < numKeys; i++) {
230
                auxInterval = FInterval.create(sk[i]);
231
                symbols.put(auxInterval,
232
                    SymbologyFactory.createSymbolFromXML(xml.getChild(i + useDefaultSymbol), null));
233
                keys.add(auxInterval);
234
                System.out.println("auxInterval =" + auxInterval + "Symbol =" +
235
                                 SymbologyFactory.createSymbolFromXML(xml.getChild(i + useDefaultSymbol), null)
236
                           .getDescription());
237
            }
238
        }
239

    
240
        startColor = StringUtilities.string2Color(xml.getStringProperty(
241
                    "startColor"));
242
        endColor = StringUtilities.string2Color(xml.getStringProperty(
243
                    "endColor"));
244
    }
245

    
246
    /**
247
     * Inserta los atributos del XMLEntity.
248
     *
249
     * @param xml XMLEntity.
250
     */
251
    public void setXMLEntity(XMLEntity xml) {
252
        fieldName = xml.getStringProperty("fieldName");
253
        index = xml.getIntProperty("index");
254
        labelFieldName = xml.getStringProperty("labelfield");
255
        if (xml.contains("labelFieldHeight")) {
256
            setLabelHeightField(xml.getStringProperty("labelFieldHeight"));
257
        }
258

    
259
        if (xml.contains("labelFieldRotation")) {
260
            setLabelRotationField(xml.getStringProperty("labelFieldRotation"));
261
        }
262

    
263
        if (xml.contains("intervalType")) { //TODO Esta condici?n es para poder cargar la versi?n 0.3, se puede eliminar cuando ya no queramos soportar esta versi?n.
264
            intervalType = xml.getIntProperty("intervalType");
265
        }
266
        useDefaultSymbol = xml.getBooleanProperty("useDefaultSymbolB");
267
        int hasDefaultSymbol = xml.getIntProperty("useDefaultSymbol");
268

    
269
        if (hasDefaultSymbol == 1) {
270
            setDefaultSymbol(SymbologyFactory.createSymbolFromXML(xml.getChild(0), null));
271
        } else {
272
            setDefaultSymbol(null);
273
        }
274

    
275
        int numKeys = xml.getIntProperty("numKeys");
276

    
277
        if (numKeys > 0) {
278
            String className = xml.getStringProperty("tipoValueKeys");
279
            String[] sk = xml.getStringArrayProperty("keys");
280
            IInterval auxInterval;
281

    
282
            for (int i = 0; i < numKeys; i++) {
283
                auxInterval = FInterval.create(sk[i]);
284
                symbols.put(auxInterval,
285
                                SymbologyFactory.createSymbolFromXML(xml.getChild(i + hasDefaultSymbol), null));
286
                keys.add(auxInterval);
287
                System.out.println("auxInterval =" + auxInterval + "Symbol =" +
288
                                SymbologyFactory.createSymbolFromXML(xml.getChild(i + hasDefaultSymbol), null)
289
                           .getDescription());
290
            }
291
        }
292

    
293
        startColor = StringUtilities.string2Color(xml.getStringProperty(
294
                    "startColor"));
295
        endColor = StringUtilities.string2Color(xml.getStringProperty(
296
                    "endColor"));
297
    }
298

    
299
    /**
300
     * @see com.iver.cit.gvsig.fmap.rendering.ILegend#cloneLegend()
301
     */
302
    public ILegend cloneLegend() throws XMLException {
303
        return LegendFactory.createFromXML(getXMLEntity());
304
    }
305

    
306
    /**
307
         * Devuelve el color final.
308
         * @return  color final.
309
         * @uml.property  name="endColor"
310
         */
311
    public Color getEndColor() {
312
        return endColor;
313
    }
314

    
315
    /**
316
         * Inserta el color final.
317
         * @param endColor  Color final.
318
         * @uml.property  name="endColor"
319
         */
320
    public void setEndColor(Color endColor) {
321
        this.endColor = endColor;
322
    }
323

    
324
    /**
325
         * Devuelve el color inicial.
326
         * @return  Color inicial.
327
         * @uml.property  name="startColor"
328
         */
329
    public Color getStartColor() {
330
        return startColor;
331
    }
332

    
333
    /**
334
         * Inserta el color inicial.
335
         * @param startColor  Color inicial.
336
         * @uml.property  name="startColor"
337
         */
338
    public void setStartColor(Color startColor) {
339
        this.startColor = startColor;
340
    }
341

    
342
        public int getShapeType() {
343
                return shapeType;
344
        }
345

    
346
        public void setShapeType(int shapeType) {
347
                if (this.shapeType != shapeType) {
348
                        setDefaultSymbol(SymbologyFactory.
349
                                        createDefaultSymbolByShapeType(shapeType));
350
                        this.shapeType = shapeType;
351
                }
352
        }
353
}