Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.symbology / src / main / java / org / gvsig / symbology / fmap / mapcontext / rendering / legend / impl / AbstractIntervalLegend.java @ 30121

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 21144 2008-06-03 15:47:31Z vcaballero $
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 org.gvsig.symbology.fmap.mapcontext.rendering.legend.impl;
88

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

    
95
import org.gvsig.fmap.dal.exception.DataException;
96
import org.gvsig.fmap.dal.feature.DisposableIterator;
97
import org.gvsig.fmap.dal.feature.Feature;
98
import org.gvsig.fmap.dal.feature.FeatureQuery;
99
import org.gvsig.fmap.dal.feature.FeatureReference;
100
import org.gvsig.fmap.dal.feature.FeatureSet;
101
import org.gvsig.fmap.dal.feature.FeatureStore;
102
import org.gvsig.fmap.mapcontext.MapContextException;
103
import org.gvsig.fmap.mapcontext.MapContextLocator;
104
import org.gvsig.fmap.mapcontext.exceptions.LegendLayerException;
105
import org.gvsig.fmap.mapcontext.rendering.legend.events.SymbolLegendEvent;
106
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
107
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.IInterval;
108
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.IVectorialIntervalLegend;
109
import org.slf4j.Logger;
110
import org.slf4j.LoggerFactory;
111

    
112

    
113
public abstract class AbstractIntervalLegend extends AbstractClassifiedVectorLegend implements IVectorialIntervalLegend{
114
        
115
        protected int shapeType;
116

    
117
        public static final int EQUAL_INTERVALS = 0;
118
        public static final int NATURAL_INTERVALS = 1;
119
        public static final int QUANTILE_INTERVALS = 2;
120
        protected TreeMap symbols = new TreeMap //<Object, ISymbol>
121
                        (new Comparator() { //<Object>
122
                public int compare(Object o1, Object o2) {
123
                        if ((o1 != null) && (o2 != null)) {
124
//                                if (o1 instanceof NullIntervalValue &&
125
//                                                o2 instanceof NullIntervalValue) {
126
//                                        return 0;
127
//                                }
128
//
129
//                                if (o2 instanceof NullIntervalValue) {
130
//                                        return 1;
131
//                                }
132
//
133
//                                if (o1 instanceof NullIntervalValue) {
134
//                                        return -1;
135
//                                }
136

    
137
                                FInterval i2 = (FInterval) o2;
138
                                FInterval i1 = (FInterval) o1;
139

    
140
                                if (i1.getMin() > i2.getMin()) {
141
                                        return 1;
142
                                }
143

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

    
155
                        return 0;
156
                }
157
        });
158
        protected ArrayList keys = new ArrayList(); //<Object>
159
        protected int index = 0;
160
        protected String[] fieldNames;
161
//        protected int fieldId;
162
        protected ISymbol defaultSymbol;
163
//        protected FeatureStore featureStore;
164
        protected int intervalType = NATURAL_INTERVALS;
165
        protected boolean useDefaultSymbol = false;
166
        
167
        private ISymbol nullIntervalSymbol = null;
168

    
169
        final static private Logger logger = LoggerFactory.getLogger(AbstractIntervalLegend.class);
170
        
171
        public void addSymbol(Object key, ISymbol symbol) {
172
                if (key == null) {
173
                        nullIntervalSymbol = symbol;
174
                }
175
                else {
176
                        symbols.put(key, symbol);
177
                        keys.add(key);
178
                }
179
                fireClassifiedSymbolChangeEvent(new SymbolLegendEvent(null, symbol));
180
        }
181

    
182
        /*
183
         * @see com.iver.cit.gvsig.fmap.rendering.IVectorialLegend#getSymbol(int)
184
         */
185
        public ISymbol getSymbol(FeatureReference featureID)
186
                        throws MapContextException, DataException {
187
                return getSymbolByFeature(featureID.getFeature());
188
                
189
//                Object val = featureStore.getFeatureByReference(featureID).get(getClassifyingFieldNames()[0]);
190
//                IInterval interval = getInterval(val);
191
//                ISymbol theSymbol = getSymbolByInterval(interval);
192
//
193
//                if (theSymbol != null) {
194
//                        return theSymbol;
195
//                } else if (useDefaultSymbol) {
196
//                        return getDefaultSymbol();
197
//                }
198
//                return null;
199

    
200
        }
201

    
202
        public ISymbol getSymbolByFeature(Feature feat) throws MapContextException {
203
                Object val = feat.get(getClassifyingFieldNames()[0]);
204
                IInterval interval = getInterval(val);
205
                ISymbol theSymbol = getSymbolByInterval(interval);
206

    
207
                if (theSymbol != null) {
208
                        return theSymbol;
209
                } else if (useDefaultSymbol) {
210
                        return getDefaultSymbol();
211
                }
212
                
213
                return null;
214
//                return theSymbol;
215
        }
216

    
217

    
218
        public FInterval[] calculateIntervals(FeatureStore featureStore, String fieldName, int numIntervalos, int shapeType)
219
        throws DataException {
220
                logger.debug("elRs.start()");
221
//                recordSet.start();
222

    
223
//                int idField = -1;
224
                FeatureSet set = null;
225
                DisposableIterator iterator = null;
226
                try {
227

    
228
                setClassifyingFieldNames(new String[] {fieldName});
229

    
230
                String[] fieldNames = getClassifyingFieldNames();
231
                FeatureQuery featureQuery=featureStore.createFeatureQuery();
232
                featureQuery.setAttributeNames(fieldNames);
233

    
234
                set = featureStore.getFeatureSet(featureQuery);
235
                iterator = set.fastIterator();
236

    
237

    
238
//                for (int i = 0; i < recordSet.getFieldCount(); i++) {
239
//                        String nomAux = recordSet.getFieldName(i).trim();
240
//
241
//                        if (fieldNames[0].compareToIgnoreCase(nomAux) == 0) {
242
//                                idField = i;
243
//
244
//                                break;
245
//                        }
246
//                }
247

    
248
//                if (idField == -1) {
249
//                        logger.error("Campo no reconocido " + fieldNames);
250
//
251
//                        return null;
252
//                }
253

    
254
                double minValue = Double.MAX_VALUE;
255
                double maxValue = Double.NEGATIVE_INFINITY;
256

    
257
                IVectorialIntervalLegend auxLegend = (IVectorialIntervalLegend) MapContextLocator
258
                                        .getMapContextManager().createLegend("VectorialInterval");
259
                auxLegend.setShapeType(shapeType);
260

    
261
                Object clave;
262

    
263
                while (iterator.hasNext()) {
264
                        Feature feature = (Feature) iterator.next();
265

    
266
//                for (int j = 0; j < recordSet.getRowCount(); j++) {
267
                        clave = feature.get(fieldName);
268

    
269
                        IInterval interval = auxLegend.getInterval(clave);
270

    
271
                        ////Comprobar que no esta repetido y no hace falta introducir en el hashtable el campo junto con el simbolo.
272
                        if (auxLegend.getSymbolByInterval(interval) == null) {
273
                                //si no esta creado el simbolo se crea
274
                                double valor = 0;
275

    
276

    
277
                                if (clave instanceof Number) {
278
                                        valor=((Number)clave).doubleValue();
279
                                }else if (clave instanceof Date) {
280
                                        //TODO POR IMPLEMENTAR
281
                                        ///valorDate = elRs.getFieldValueAsDate(idField);
282
                                        ///if (valorDate.before(minValueDate)) minValueDate = valorDate;
283
                                        ///if (valorDate.after(maxValueDate)) maxValueDate = valorDate;
284
//                                } else if (clave instanceof NullValue) {
285
//                                        continue;
286
                                }
287

    
288
                                if (valor < minValue) {
289
                                        minValue = valor;
290
                                }
291

    
292
                                if (valor > maxValue) {
293
                                        maxValue = valor;
294
                                }
295
                        }
296
                }
297

    
298
                FInterval[] intervalArray = null;
299
                switch (getIntervalType()) {
300
                case VectorialIntervalLegend.EQUAL_INTERVALS:
301
                        intervalArray = calculateEqualIntervals(numIntervalos,
302
                                        minValue, maxValue, fieldName);
303

    
304
                        break;
305

    
306
                case VectorialIntervalLegend.NATURAL_INTERVALS:
307
                        intervalArray = calculateNaturalIntervals(featureStore, numIntervalos,
308
                                        minValue, maxValue, fieldName);
309

    
310
                        break;
311

    
312
                case VectorialIntervalLegend.QUANTILE_INTERVALS:
313
                        intervalArray = calculateQuantileIntervals(featureStore, numIntervalos,
314
                                        minValue, maxValue, fieldName);
315

    
316
                        break;
317
                }
318
//                recordSet.stop();
319
                return intervalArray;
320
                } finally {
321
                        if (iterator != null) {
322
                                iterator.dispose();
323
                        }
324
                        if (set != null) {
325
                                set.dispose();
326
                        }
327
                }
328
        }
329

    
330

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

    
346
        if (numIntervals > 1) {
347
            theIntervalArray[0] = new FInterval(minValue, minValue + step);
348

    
349
            for (int i = 1; i < (numIntervals - 1); i++) {
350
                theIntervalArray[i] = new FInterval(minValue + (i * step) +
351
                        0.01, minValue + ((i + 1) * step));
352
            }
353

    
354
            theIntervalArray[numIntervals - 1] = new FInterval(minValue +
355
                    ((numIntervals - 1) * step) + 0.01, maxValue);
356
        } else {
357
            theIntervalArray[0] = new FInterval(minValue, maxValue);
358
        }
359

    
360
        return theIntervalArray;
361
    }
362

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

    
381
        intervalGenerator.generarIntervalos();
382

    
383
        int numIntervalsGen = intervalGenerator.getNumIntervals() - 1;
384

    
385
        if (numIntervalsGen == -1) {
386
            //TODO cuando no puede calcular los intervalos.
387
            numIntervalsGen = 1;
388
        }
389

    
390
        FInterval[] theIntervalArray = new FInterval[numIntervalsGen];
391

    
392
        if (numIntervalsGen > 1) {
393
            theIntervalArray[0] = new FInterval(minValue,
394
                    intervalGenerator.getValorRuptura(0));
395

    
396
            for (int i = 1; i < (numIntervalsGen - 1); i++) {
397
                theIntervalArray[i] = new FInterval(intervalGenerator.getValInit(i -
398
                            1), intervalGenerator.getValorRuptura(i));
399
            }
400

    
401
            theIntervalArray[numIntervalsGen - 1] = new FInterval(intervalGenerator.getValInit(numIntervalsGen -
402
                        2), maxValue);
403
        } else {
404
            theIntervalArray[numIntervalsGen - 1] = new FInterval(minValue,
405
                    maxValue);
406
        }
407

    
408
        return theIntervalArray;
409
    }
410

    
411
    /**
412
     * QUANTILE INTERVAL Devuelve un Array con el n?mero de intervalos que se
413
     * quieren crear. Los intervalos se distribuyen de forma quantile.
414
     * @param recordSet
415
     *
416
     * @param numIntervals n?mero de intervalos
417
     * @param minValue Valor m?nimo.
418
     * @param maxValue Valor m?ximo.
419
     * @param fieldName Nombre del campo
420
     *
421
     * @return Array con los intervalos.
422
     * @throws LegendLayerException
423
     */
424
    private FInterval[] calculateQuantileIntervals(FeatureStore featureStore, int numIntervals, double minValue,
425
        double maxValue, String fieldName) throws DataException {
426
        QuantileIntervalGenerator intervalGenerator = new QuantileIntervalGenerator(
427
                        featureStore, fieldName, numIntervals);
428

    
429
        intervalGenerator.generarIntervalos();
430

    
431
        int numIntervalsGen = intervalGenerator.getNumIntervalGen();
432
        FInterval[] theIntervalArray = new FInterval[numIntervalsGen];
433

    
434
        if (intervalGenerator.getNumIntervalGen() > 1) {
435
            theIntervalArray[0] = new FInterval(minValue,
436
                    intervalGenerator.getValRuptura(0));
437

    
438
            for (int i = 1; i < (numIntervalsGen - 1); i++) {
439
                theIntervalArray[i] = new FInterval(intervalGenerator.getValInit(i -
440
                            1), intervalGenerator.getValRuptura(i));
441
            }
442

    
443
            theIntervalArray[numIntervalsGen - 1] = new FInterval(intervalGenerator.getValInit(numIntervalsGen -
444
                        2), maxValue);
445
        } else {
446
            theIntervalArray[numIntervalsGen - 1] = new FInterval(minValue,
447
                    maxValue);
448
        }
449

    
450
        return theIntervalArray;
451
    }
452

    
453
    public ISymbol getSymbolByInterval(IInterval key) {
454

    
455
                if (key == null){
456
                        if (nullIntervalSymbol != null) {
457
                                return nullIntervalSymbol;
458
                        }
459
                        else if (isUseDefaultSymbol()) {
460
                                return defaultSymbol;
461
                        }
462
                        return null;
463
                }
464
                if (symbols.containsKey(key)) {
465
                        return (ISymbol) symbols.get(key);
466
                }
467

    
468
                if (isUseDefaultSymbol()) {
469
                        return defaultSymbol;
470
                }
471

    
472
                return null;
473
        }
474

    
475

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

    
480
                for (int i = 0; i < descriptions.length; i++) {
481
                        descriptions[i] = auxSym[i].getDescription();
482
                }
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
                ISymbol[] symbolList;
500
                if (nullIntervalSymbol == null) {
501
                        symbolList = new ISymbol[symbols.size()];
502
                        return (ISymbol[]) symbols.values().toArray(symbolList);
503
                }
504
                else {
505
                        symbolList = new ISymbol[symbols.size() + 1];
506
                        symbolList[0] = nullIntervalSymbol;
507
                        int i = 1;
508
                        for (Iterator iterator = symbols.values().iterator(); iterator
509
                                        .hasNext();) {
510
                                symbolList[i] = (ISymbol) iterator.next();
511
                                i++;
512
                        }
513
                        return symbolList;
514
                }
515
        }
516

    
517
        public String[] getClassifyingFieldNames() {
518
                return fieldNames;
519
        }
520

    
521
        public void setDefaultSymbol(ISymbol s) {
522
            ISymbol old = defaultSymbol;
523
                if (s == null) {
524
                        throw new NullPointerException("Default symbol cannot be null");
525
                }
526
                defaultSymbol = s;
527
                fireDefaultSymbolChangedEvent(new SymbolLegendEvent(old, defaultSymbol));
528
        }
529

    
530
        public void setClassifyingFieldNames(String[] fieldNames) {
531
                this.fieldNames = fieldNames;
532
        }
533

    
534
        public ISymbol getDefaultSymbol() {
535
//                NullIntervalValue niv=new NullIntervalValue();
536
//                if (symbols.containsKey(niv)) {
537
//                        return (ISymbol)symbols.get(niv);
538
//                }
539

    
540
                if(defaultSymbol==null) {
541
                        defaultSymbol = getManager().createSymbol(shapeType);
542
                }
543
                return defaultSymbol;
544
        }
545

    
546

    
547

    
548
//    public void setFeatureStore(FeatureStore featureStore)
549
//        throws DataException {
550
//                    /*
551
//                     * when we move definitely to feature iterators this
552
//                     * method
553
//                     */
554
//                    this.featureStore = featureStore;
555
////                    fieldId = ((FeatureType)featureStore.getFeatureTypes().get(0)).getIndex(fieldNames[0]);
556
//    }
557

    
558

    
559
        public IInterval getInterval(Object v) {
560
                if (v != null ) {
561
                        for (int i = 0; i < keys.size(); i++) {
562
                                if (((IInterval) keys.get(i)).isInInterval(v)) {
563
                                        return (IInterval) keys.get(i);
564
                                }
565
                        }
566
                }
567

    
568
                return null;
569
        }
570

    
571
        public void setIntervalType(int intervalType) {
572
                this.intervalType = intervalType;
573
        }
574

    
575

    
576
        public int getIntervalType() {
577
                return intervalType;
578
        }
579

    
580
        public void useDefaultSymbol(boolean b) {
581
                useDefaultSymbol = b;
582
        }
583

    
584

    
585
        public boolean isUseDefaultSymbol() {
586
                return useDefaultSymbol;
587
        }
588

    
589

    
590
        public void delSymbol(Object obj) {
591
                keys.remove(obj);
592
                symbols.remove(obj);
593
                fireClassifiedSymbolChangeEvent(
594
                                new SymbolLegendEvent(
595
                                                (ISymbol)symbols.remove(obj),
596
                                                null));
597
        }
598

    
599

    
600
        public void replace(ISymbol oldSymbol, ISymbol newSymbol) {
601
                if (symbols.containsValue(oldSymbol)) {
602
                        Iterator it = symbols.keySet().iterator();
603
                        while (it.hasNext()) {
604
                                Object key = it.next();
605
                                if (symbols.get(key).equals(oldSymbol)) {
606
                                        symbols.remove(key);
607
                                        symbols.put(key, newSymbol);
608
                                        fireClassifiedSymbolChangeEvent(
609
                                                        new SymbolLegendEvent(oldSymbol, newSymbol));
610
                                }
611
                        }
612
                }
613
        }
614
}