Statistics
| Revision:

svn-gvsig-desktop / tags / J2ME_compat_v1_2_Build_1209 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / rendering / AbstractIntervalLegend.java @ 19509

History | View | Annotate | Download (17.6 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 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

    
42
/* CVS MESSAGES:
43
*
44
* $Id: AbstractIntervalLegend.java 14418 2007-10-04 11:22:02Z jvidal $
45
* $Log$
46
* Revision 1.12  2007-09-19 16:25:39  jaume
47
* ReadExpansionFileException removed from this context and removed unnecessary imports
48
*
49
* Revision 1.11  2007/09/10 15:47:11  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.10  2007/08/03 09:22:08  jaume
53
* refactored class names
54
*
55
* Revision 1.9  2007/08/02 10:19:03  jvidal
56
* implements IPersistance
57
*
58
* Revision 1.8  2007/08/01 11:45:59  jaume
59
* passing general tests (drawing test yet missing)
60
*
61
* Revision 1.7  2007/05/28 15:36:42  jaume
62
* *** empty log message ***
63
*
64
* Revision 1.6  2007/05/17 09:32:06  jaume
65
* *** empty log message ***
66
*
67
* Revision 1.5  2007/05/10 14:13:36  jaume
68
* *** empty log message ***
69
*
70
* Revision 1.4  2007/05/10 09:44:08  jaume
71
* Refactored legend interface names
72
*
73
* Revision 1.3  2007/03/27 09:28:40  jaume
74
* *** empty log message ***
75
*
76
* Revision 1.2  2007/03/09 11:20:56  jaume
77
* Advanced symbology (start committing)
78
*
79
* Revision 1.1.2.2  2007/02/15 16:23:44  jaume
80
* *** empty log message ***
81
*
82
* Revision 1.1.2.1  2007/02/13 16:19:19  jaume
83
* graduated symbol legends (start commiting)
84
*
85
*
86
*/
87
package com.iver.cit.gvsig.fmap.rendering;
88

    
89
import java.util.ArrayList;
90
import java.util.Comparator;
91
import java.util.Iterator;
92
import java.util.TreeMap;
93

    
94
import org.apache.log4j.Logger;
95

    
96
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
97
import com.hardcode.gdbms.engine.data.DataSource;
98
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
99
import com.hardcode.gdbms.engine.values.DateValue;
100
import com.hardcode.gdbms.engine.values.DoubleValue;
101
import com.hardcode.gdbms.engine.values.FloatValue;
102
import com.hardcode.gdbms.engine.values.IntValue;
103
import com.hardcode.gdbms.engine.values.LongValue;
104
import com.hardcode.gdbms.engine.values.NullValue;
105
import com.hardcode.gdbms.engine.values.Value;
106
import com.iver.cit.gvsig.exceptions.layers.LegendLayerException;
107
import com.iver.cit.gvsig.fmap.Messages;
108
import com.iver.cit.gvsig.fmap.core.FShape;
109
import com.iver.cit.gvsig.fmap.core.IFeature;
110
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
111
import com.iver.cit.gvsig.fmap.core.styles.SimpleMarkerFillPropertiesStyle;
112
import com.iver.cit.gvsig.fmap.core.symbols.IMarkerSymbol;
113
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
114
import com.iver.cit.gvsig.fmap.core.symbols.MarkerFillSymbol;
115
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
116

    
117
public abstract class AbstractIntervalLegend extends AbstractClassifiedVectorLegend implements IVectorialIntervalLegend{
118
        protected int shapeType;
119

    
120
        public static final int EQUAL_INTERVALS = 0;
121
        public static final int NATURAL_INTERVALS = 1;
122
        public static final int QUANTILE_INTERVALS = 2;
123
        protected TreeMap symbols = new TreeMap
124
                        (new Comparator() {
125
                public int compare(Object o1, Object o2) {
126
                        if ((o1 != null) && (o2 != null)) {
127
                                if (o1 instanceof NullIntervalValue &&
128
                                                o2 instanceof NullIntervalValue) {
129
                                        return 0;
130
                                }
131

    
132
                                if (o2 instanceof NullIntervalValue) {
133
                                        return 1;
134
                                }
135

    
136
                                if (o1 instanceof NullIntervalValue) {
137
                                        return -1;
138
                                }
139

    
140
                                FInterval i2 = (FInterval) o2;
141
                                FInterval i1 = (FInterval) o1;
142

    
143
                                if (i1.getMin() > i2.getMin()) {
144
                                        return 1;
145
                                }
146

    
147
                                if (i1.getMin() < i2.getMin()) {
148
                                        return -1;
149
                                }
150
                                if (i1.getMax() < i2.getMax()) {
151
                                        return -1;
152
                                }
153
                                if (i1.getMax() > i2.getMax()) {
154
                                        return 1;
155
                                }
156
                        }
157

    
158
                        return 0;
159
                }
160
        });
161
        protected ArrayList keys = new ArrayList();
162
        protected int index = 0;
163
        protected String[] fieldNames;
164
        protected int fieldId;
165
        protected ISymbol defaultSymbol;
166
        protected DataSource dataSource;
167
        protected int intervalType = NATURAL_INTERVALS;
168
        protected boolean useDefaultSymbol = false;
169

    
170
        private Logger logger = Logger.getLogger(AbstractIntervalLegend.class);
171

    
172
        public void addSymbol(Object key, ISymbol symbol) {
173
                symbols.put(key, symbol);
174
                keys.add(key);
175
        }
176

    
177
        /*
178
         * @see com.iver.cit.gvsig.fmap.rendering.IVectorialLegend#getSymbol(int)
179
         */
180
        public ISymbol getSymbol(int recordIndex) throws ReadDriverException {
181
                Value val = dataSource.getFieldValue(recordIndex, fieldId);
182
                IInterval interval = getInterval(val);
183
                ISymbol theSymbol = getSymbolByInterval(interval);
184

    
185

    
186

    
187
                if (shapeType == FShape.POLYGON && theSymbol instanceof IMarkerSymbol) {
188
                        // transform it to a fill symbol
189
                        MarkerFillSymbol aux = new MarkerFillSymbol();
190
                        // tell the fill style to draw the IMarkerSymbol
191
                        // as a IFillSymbol centering it in the shape polygon
192
                        // centroid and applying offset (if any).
193
                        aux.setMarker((IMarkerSymbol) theSymbol);
194
                        SimpleMarkerFillPropertiesStyle p = new SimpleMarkerFillPropertiesStyle();
195
                        p.setFillStyle(SimpleMarkerFillPropertiesStyle.SINGLE_CENTERED_SYMBOL);
196
                        aux.setMarkerFillProperties(p);
197
                        theSymbol = aux;
198
                }
199

    
200
                if (theSymbol != null) {
201
                        return theSymbol;
202
                } else if (useDefaultSymbol) {
203
                        return getDefaultSymbol();
204
                }
205

    
206
                return null;
207
        }
208

    
209

    
210
        public ISymbol getSymbolByFeature(IFeature feat) {
211
                Value val = feat.getAttribute(FLyrVect.forTestOnlyVariableUseIterators_REMOVE_THIS_FIELD
212
                        ? 0 :fieldId);
213
                IInterval interval = getInterval(val);
214
                ISymbol theSymbol = getSymbolByInterval(interval);
215

    
216
                if (theSymbol == null) 
217
                        return getDefaultSymbol();
218
                
219
                if (shapeType == FShape.POLYGON && theSymbol instanceof IMarkerSymbol) {
220
                        // transform it to a fill symbol
221
                        MarkerFillSymbol aux = new MarkerFillSymbol();
222
                        // tell the fill style to draw the IMarkerSymbol
223
                        // as a IFillSymbol centering it in the shape polygon
224
                        // centroid and applying offset (if any).
225
                        aux.setMarker((IMarkerSymbol) theSymbol);
226
                        SimpleMarkerFillPropertiesStyle p = new SimpleMarkerFillPropertiesStyle();
227
                        p.setFillStyle(SimpleMarkerFillPropertiesStyle.SINGLE_CENTERED_SYMBOL);
228
                        aux.setMarkerFillProperties(p);
229
                        theSymbol = aux;
230
                }
231
                
232
                
233
                return theSymbol;
234
        }
235
        
236
        
237
        public FInterval[] calculateIntervals(DataSource recordSet, String fieldName, int numIntervalos, int shapeType)
238
        throws ReadDriverException, LegendLayerException {
239
                logger.debug("elRs.start()");
240
                recordSet.start();
241

    
242
                int idField = -1;
243
                
244
                setClassifyingFieldNames(new String[] {fieldName});
245

    
246
                String[] fieldNames = getClassifyingFieldNames();
247

    
248

    
249
                for (int i = 0; i < recordSet.getFieldCount(); i++) {
250
                        String nomAux = recordSet.getFieldName(i).trim();
251

    
252
                        if (fieldNames[0].compareToIgnoreCase(nomAux) == 0) {
253
                                idField = i;
254

    
255
                                break;
256
                        }
257
                }
258

    
259
                if (idField == -1) {
260
                        logger.error("Campo no reconocido " + fieldNames);
261

    
262
                        return null;
263
                }
264

    
265
                double minValue = Double.MAX_VALUE;
266
                double maxValue = Double.NEGATIVE_INFINITY;
267

    
268
                VectorialIntervalLegend auxLegend = LegendFactory.
269
                    createVectorialIntervalLegend(shapeType);
270

    
271
                Value clave;
272

    
273
                for (int j = 0; j < recordSet.getRowCount(); j++) {
274
                        clave = recordSet.getFieldValue(j, idField);
275

    
276
                        IInterval interval = auxLegend.getInterval(clave);
277

    
278
                        ////Comprobar que no esta repetido y no hace falta introducir en el hashtable el campo junto con el simbolo.
279
                        if (auxLegend.getSymbolByInterval(interval) == null) {
280
                                //si no esta creado el simbolo se crea
281
                                double valor = 0;
282

    
283

    
284
                                if (clave instanceof IntValue) {
285
                                        valor = ((IntValue) clave).getValue();
286
                                } else if (clave instanceof DoubleValue) {
287
                                        valor = ((DoubleValue) clave).getValue();
288
                                } else if (clave instanceof FloatValue) {
289
                                        valor = ((FloatValue) clave).getValue();
290
                                } else if (clave instanceof LongValue) {
291
                                        valor = ((LongValue) clave).getValue();
292
                                } else if (clave instanceof DateValue) {
293
                                        //TODO POR IMPLEMENTAR
294
                                        ///valorDate = elRs.getFieldValueAsDate(idField);
295
                                        ///if (valorDate.before(minValueDate)) minValueDate = valorDate;
296
                                        ///if (valorDate.after(maxValueDate)) maxValueDate = valorDate;
297
                                } else if (clave instanceof NullValue) {
298
                                        continue;
299
                                }
300

    
301
                                if (valor < minValue) {
302
                                        minValue = valor;
303
                                }
304

    
305
                                if (valor > maxValue) {
306
                                        maxValue = valor;
307
                                }
308
                        }
309
                }
310

    
311
                FInterval[] intervalArray = null;
312
                switch (getIntervalType()) {
313
                case VectorialIntervalLegend.EQUAL_INTERVALS:
314
                        intervalArray = calculateEqualIntervals(numIntervalos,
315
                                        minValue, maxValue, fieldName);
316

    
317
                        break;
318

    
319
                case VectorialIntervalLegend.NATURAL_INTERVALS:
320
                        intervalArray = calculateNaturalIntervals(recordSet, numIntervalos,
321
                                        minValue, maxValue, fieldName);
322

    
323
                        break;
324

    
325
                case VectorialIntervalLegend.QUANTILE_INTERVALS:
326
                        intervalArray = calculateQuantileIntervals(recordSet, numIntervalos,
327
                                        minValue, maxValue, fieldName);
328

    
329
                        break;
330
                }
331
                recordSet.stop();
332
                return intervalArray;
333
        }
334

    
335
        
336
    /**
337
     * EQUAL INTERVAL Devuelve un Array con el n?mero de intervalos que se
338
     * quieren crear. Los intervalos se crean con un tama?o igual entre ellos.
339
     * @param numIntervals n?mero de intervalos
340
     * @param minValue Valor m?nimo.
341
     * @param maxValue Valor m?ximo.
342
     * @param fieldName Nombre del campo
343
     *
344
     * @return Array con los intervalos.
345
     */
346
    private FInterval[] calculateEqualIntervals(int numIntervals, double minValue,
347
        double maxValue, String fieldName) {
348
        FInterval[] theIntervalArray = new FInterval[numIntervals];
349
        double step = (maxValue - minValue) / numIntervals;
350

    
351
        if (numIntervals > 1) {
352
            theIntervalArray[0] = new FInterval(minValue, minValue + step);
353

    
354
            for (int i = 1; i < (numIntervals - 1); i++) {
355
                theIntervalArray[i] = new FInterval(minValue + (i * step) +
356
                        0.01, minValue + ((i + 1) * step));
357
            }
358

    
359
            theIntervalArray[numIntervals - 1] = new FInterval(minValue +
360
                    ((numIntervals - 1) * step) + 0.01, maxValue);
361
        } else {
362
            theIntervalArray[0] = new FInterval(minValue, maxValue);
363
        }
364

    
365
        return theIntervalArray;
366
    }
367

    
368
    /**
369
     * NATURAL INTERVAL Devuelve un Array con el n?mero de intervalos que se
370
     * quieren crear. Los intervalos se distribuyen de forma natural.
371
     *
372
     * @param numIntervals n?mero de intervalos
373
     * @param minValue Valor m?nimo.
374
     * @param maxValue Valor m?ximo.
375
     * @param fieldName Nombre del campo
376
     *
377
     * @return Array con los intervalos.
378
     * @throws LegendLayerException 
379
     */
380
    private FInterval[] calculateNaturalIntervals(DataSource recordSet, int numIntervals, double minValue,
381
        double maxValue, String fieldName) throws LegendLayerException {
382
        NaturalIntervalGenerator intervalGenerator = new NaturalIntervalGenerator(
383
                        recordSet, fieldName, numIntervals);
384

    
385
        try {
386
            intervalGenerator.generarIntervalos();
387
        } catch (ReadDriverException e) {
388
                throw new LegendLayerException(Messages.getString("failed_calculating_intervals"), e);
389
        }
390

    
391
        int numIntervalsGen = intervalGenerator.getNumIntervals() - 1;
392

    
393
        if (numIntervalsGen == -1) {
394
            //TODO cuando no puede calcular los intervalos.
395
            numIntervalsGen = 1;
396
        }
397

    
398
        FInterval[] theIntervalArray = new FInterval[numIntervalsGen];
399

    
400
        if (numIntervalsGen > 1) {
401
            theIntervalArray[0] = new FInterval(minValue,
402
                    intervalGenerator.getValorRuptura(0));
403

    
404
            for (int i = 1; i < (numIntervalsGen - 1); i++) {
405
                theIntervalArray[i] = new FInterval(intervalGenerator.getValInit(i -
406
                            1), intervalGenerator.getValorRuptura(i));
407
            }
408

    
409
            theIntervalArray[numIntervalsGen - 1] = new FInterval(intervalGenerator.getValInit(numIntervalsGen -
410
                        2), maxValue);
411
        } else {
412
            theIntervalArray[numIntervalsGen - 1] = new FInterval(minValue,
413
                    maxValue);
414
        }
415

    
416
        return theIntervalArray;
417
    }
418

    
419
    /**
420
     * QUANTILE INTERVAL Devuelve un Array con el n?mero de intervalos que se
421
     * quieren crear. Los intervalos se distribuyen de forma quantile.
422
     * @param recordSet 
423
     *
424
     * @param numIntervals n?mero de intervalos
425
     * @param minValue Valor m?nimo.
426
     * @param maxValue Valor m?ximo.
427
     * @param fieldName Nombre del campo
428
     *
429
     * @return Array con los intervalos.
430
     * @throws LegendLayerException 
431
     */
432
    private FInterval[] calculateQuantileIntervals(DataSource recordSet, int numIntervals, double minValue,
433
        double maxValue, String fieldName) throws LegendLayerException {
434
        QuantileIntervalGenerator intervalGenerator = new QuantileIntervalGenerator(
435
                        recordSet, fieldName, numIntervals);
436

    
437
        try {
438
            intervalGenerator.generarIntervalos();
439
        } catch (ReadDriverException e) {
440
                throw new LegendLayerException(Messages.getString("failed_calculating_intervals"), e);
441
        }
442

    
443
        int numIntervalsGen = intervalGenerator.getNumIntervalGen();
444
        FInterval[] theIntervalArray = new FInterval[numIntervalsGen];
445

    
446
        if (intervalGenerator.getNumIntervalGen() > 1) {
447
            theIntervalArray[0] = new FInterval(minValue,
448
                    intervalGenerator.getValRuptura(0));
449

    
450
            for (int i = 1; i < (numIntervalsGen - 1); i++) {
451
                theIntervalArray[i] = new FInterval(intervalGenerator.getValInit(i -
452
                            1), intervalGenerator.getValRuptura(i));
453
            }
454

    
455
            theIntervalArray[numIntervalsGen - 1] = new FInterval(intervalGenerator.getValInit(numIntervalsGen -
456
                        2), maxValue);
457
        } else {
458
            theIntervalArray[numIntervalsGen - 1] = new FInterval(minValue,
459
                    maxValue);
460
        }
461

    
462
        return theIntervalArray;
463
    }
464

    
465
        public ISymbol getSymbolByInterval(IInterval key) {
466
                if (key == null) {
467
                        return null;
468
                }
469

    
470
                if (symbols.containsKey(key)) {
471
                        return (ISymbol) symbols.get(key);
472
                }
473

    
474
                return null;
475
        }
476

    
477
        public String[] getDescriptions() {
478
                String[] descriptions = new String[symbols.size()];
479
                ISymbol[] auxSym = getSymbols();
480

    
481
                for (int i = 0; i < descriptions.length; i++)
482
                        descriptions[i] = auxSym[i].getDescription();
483

    
484
                return descriptions;
485
        }
486

    
487

    
488
        public Object[] getValues() {
489
                return symbols.keySet().toArray();
490
        }
491

    
492
        public void clear() {
493
                index = 0;
494
                keys.clear();
495
                symbols.clear();
496
        }
497

    
498
        public ISymbol[] getSymbols() {
499
                return (ISymbol[]) symbols.values().toArray(new ISymbol[0]);
500
        }
501

    
502
        public String[] getClassifyingFieldNames() {
503
                return fieldNames;
504
        }
505

    
506
        public void setDefaultSymbol(ISymbol s) {
507
                if (s == null) throw new NullPointerException("Default symbol cannot be null");
508
                defaultSymbol = s;
509
        }
510

    
511
        public void setClassifyingFieldNames(String[] fieldNames) {
512
                this.fieldNames = fieldNames;
513
        }
514

    
515
        public ISymbol getDefaultSymbol() {
516
                NullIntervalValue niv=new NullIntervalValue();
517
                if (symbols.containsKey(niv))
518
                        return (ISymbol)symbols.get(niv);
519

    
520
                if(defaultSymbol==null)
521
                    defaultSymbol=SymbologyFactory.createDefaultFillSymbol();//mio
522
                return defaultSymbol;
523
        }
524

    
525
   
526
    public void setDataSource(DataSource ds)
527
        throws FieldNotFoundException, ReadDriverException {
528
            if (!FLyrVect.forTestOnlyVariableUseIterators_REMOVE_THIS_FIELD) {
529
                    /*
530
                     * when we move definitely to feature iterators this
531
                     * method  
532
                     */
533
                    dataSource = ds;
534
                    ds.start();
535
                    fieldId = ds.getFieldIndexByName(fieldNames[0]);
536
                    ds.stop();
537
            } 
538
    }
539

    
540
        
541
        public IInterval getInterval(Value v) {
542
                for (int i = 0; i < keys.size(); i++) {
543
                        if (((IInterval) keys.get(i)).isInInterval(v)) {
544
                                return (IInterval) keys.get(i);
545
                        }
546
                }
547

    
548
                return null;
549
        }
550

    
551
        public void setIntervalType(int intervalType) {
552
                this.intervalType = intervalType;
553
        }
554

    
555
        
556
        public int getIntervalType() {
557
                return intervalType;
558
        }
559

    
560
        public void useDefaultSymbol(boolean b) {
561
                useDefaultSymbol = b;
562
        }
563

    
564

    
565
        public boolean isUseDefaultSymbol() {
566
                return useDefaultSymbol;
567
        }
568

    
569
        
570
        public void delSymbol(Object obj) {
571
                keys.remove(obj);
572
                symbols.remove(obj);
573
        }
574

    
575
        
576
        public void replace(ISymbol oldSymbol, ISymbol newSymbol) {
577
                if (symbols.containsValue(oldSymbol)) {
578
                        Iterator it = symbols.keySet().iterator();
579
                        while (it.hasNext()) {
580
                                Object key = it.next();
581
                                if (symbols.get(key).equals(oldSymbol)) {
582
                                        symbols.remove(key);
583
                                        symbols.put(key, newSymbol);
584
                                }
585
                        }
586
                }
587
        }
588
}