Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / rendering / AbstractIntervalLegend.java @ 31228

History | View | Annotate | Download (15.3 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 31228 2009-10-08 10:13:04Z fpenarrubia $
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.IFeature;
109
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
110
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
111
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
112

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

    
116
        public static final int EQUAL_INTERVALS = 0;
117
        public static final int NATURAL_INTERVALS = 1;
118
        public static final int QUANTILE_INTERVALS = 2;
119
        protected TreeMap<Object, ISymbol> symbols = new TreeMap<Object, ISymbol>
120
        (new Comparator<Object>() {
121
                public int compare(Object o1, Object o2) {
122
                        if ((o1 != null) && (o2 != null)) {
123
                                if (o1 instanceof NullIntervalValue &&
124
                                                o2 instanceof NullIntervalValue) {
125
                                        return 0;
126
                                }
127

    
128
                                if (o2 instanceof NullIntervalValue) {
129
                                        return 1;
130
                                }
131

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

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

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

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

    
154
                        return 0;
155
                }
156
        });
157
        protected ArrayList<Object> keys = new ArrayList<Object>();
158
        protected int index = 0;
159
        protected String[] fieldNames;
160
        protected int fieldId;
161
        protected ISymbol defaultSymbol;
162
        protected DataSource dataSource;
163
        protected int intervalType = NATURAL_INTERVALS;
164
        protected boolean useDefaultSymbol = false;
165

    
166
        private Logger logger = Logger.getLogger(AbstractIntervalLegend.class);
167

    
168
        public void addSymbol(Object key, ISymbol symbol) {
169
                symbols.put(key, symbol);
170
                keys.add(key);
171
                fireClassifiedSymbolChangeEvent(new SymbolLegendEvent(null, symbol));
172
        }
173

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

    
182

    
183
                if (theSymbol != null) {
184
                        return theSymbol;
185
                } else if (useDefaultSymbol) {
186
                        return getDefaultSymbol();
187
                }
188

    
189
                return null;
190
        }
191

    
192

    
193
        public ISymbol getSymbolByFeature(IFeature feat) {
194
//                Value val = feat.getAttribute(FLyrVect.forTestOnlyVariableUseIterators_REMOVE_THIS_FIELD
195
//                                ? 0 :fieldId);
196
                Value val = feat.getAttribute(0);
197
                IInterval interval = getInterval(val);
198
                ISymbol theSymbol = getSymbolByInterval(interval);
199

    
200
                return theSymbol;
201
        }
202

    
203

    
204
        public FInterval[] calculateIntervals(DataSource recordSet, String fieldName, int numIntervalos, int shapeType)
205
        throws ReadDriverException, LegendLayerException {
206
                logger.debug("elRs.start()");
207
                recordSet.start();
208

    
209
                int idField = -1;
210

    
211
                setClassifyingFieldNames(new String[] {fieldName});
212

    
213
                String[] fieldNames = getClassifyingFieldNames();
214

    
215

    
216
                idField = recordSet.getFieldIndexByName(fieldNames[0]);
217

    
218
                
219
                if (idField == -1) {
220
                        logger.error("Campo no reconocido " + fieldNames);
221

    
222
                        return null;
223
                }
224

    
225
                double minValue = Double.MAX_VALUE;
226
                double maxValue = Double.NEGATIVE_INFINITY;
227

    
228
                VectorialIntervalLegend auxLegend = LegendFactory.
229
                createVectorialIntervalLegend(shapeType);
230

    
231
                Value clave;
232

    
233
                for (int j = 0; j < recordSet.getRowCount(); j++) {
234
                        clave = recordSet.getFieldValue(j, idField);
235

    
236
                        IInterval interval = auxLegend.getInterval(clave);
237

    
238
                        ////Comprobar que no esta repetido y no hace falta introducir en el hashtable el campo junto con el simbolo.
239
                        if (auxLegend.getSymbolByInterval(interval) == null) {
240
                                //si no esta creado el simbolo se crea
241
                                double valor = 0;
242

    
243

    
244
                                if (clave instanceof IntValue) {
245
                                        valor = ((IntValue) clave).getValue();
246
                                } else if (clave instanceof DoubleValue) {
247
                                        valor = ((DoubleValue) clave).getValue();
248
                                } else if (clave instanceof FloatValue) {
249
                                        valor = ((FloatValue) clave).getValue();
250
                                } else if (clave instanceof LongValue) {
251
                                        valor = ((LongValue) clave).getValue();
252
                                } else if (clave instanceof DateValue) {
253
                                        //TODO POR IMPLEMENTAR
254
                                        ///valorDate = elRs.getFieldValueAsDate(idField);
255
                                        ///if (valorDate.before(minValueDate)) minValueDate = valorDate;
256
                                        ///if (valorDate.after(maxValueDate)) maxValueDate = valorDate;
257
                                } else if (clave instanceof NullValue) {
258
                                        continue;
259
                                }
260

    
261
                                if (valor < minValue) {
262
                                        minValue = valor;
263
                                }
264

    
265
                                if (valor > maxValue) {
266
                                        maxValue = valor;
267
                                }
268
                        }
269
                }
270

    
271
                FInterval[] intervalArray = null;
272
                switch (getIntervalType()) {
273
                case VectorialIntervalLegend.EQUAL_INTERVALS:
274
                        intervalArray = calculateEqualIntervals(numIntervalos,
275
                                        minValue, maxValue, fieldName);
276

    
277
                        break;
278

    
279
                case VectorialIntervalLegend.NATURAL_INTERVALS:
280
                        intervalArray = calculateNaturalIntervals(recordSet, numIntervalos,
281
                                        minValue, maxValue, fieldName);
282

    
283
                        break;
284

    
285
                case VectorialIntervalLegend.QUANTILE_INTERVALS:
286
                        intervalArray = calculateQuantileIntervals(recordSet, numIntervalos,
287
                                        minValue, maxValue, fieldName);
288

    
289
                        break;
290
                }
291
                recordSet.stop();
292
                return intervalArray;
293
        }
294

    
295

    
296
        /**
297
         * EQUAL INTERVAL Devuelve un Array con el n?mero de intervalos que se
298
         * quieren crear. Los intervalos se crean con un tama?o igual entre ellos.
299
         * @param numIntervals n?mero de intervalos
300
         * @param minValue Valor m?nimo.
301
         * @param maxValue Valor m?ximo.
302
         * @param fieldName Nombre del campo
303
         *
304
         * @return Array con los intervalos.
305
         */
306
        private FInterval[] calculateEqualIntervals(int numIntervals, double minValue,
307
                        double maxValue, String fieldName) {
308
                FInterval[] theIntervalArray = new FInterval[numIntervals];
309
                double step = (maxValue - minValue) / numIntervals;
310

    
311
                if (numIntervals > 1) {
312
                        theIntervalArray[0] = new FInterval(minValue, minValue + step);
313

    
314
                        for (int i = 1; i < (numIntervals - 1); i++) {
315
                                theIntervalArray[i] = new FInterval(minValue + (i * step) +
316
                                                0.01, minValue + ((i + 1) * step));
317
                        }
318

    
319
                        theIntervalArray[numIntervals - 1] = new FInterval(minValue +
320
                                        ((numIntervals - 1) * step) + 0.01, maxValue);
321
                } else {
322
                        theIntervalArray[0] = new FInterval(minValue, maxValue);
323
                }
324

    
325
                return theIntervalArray;
326
        }
327

    
328
        /**
329
         * NATURAL INTERVAL Devuelve un Array con el n?mero de intervalos que se
330
         * quieren crear. Los intervalos se distribuyen de forma natural.
331
         *
332
         * @param numIntervals n?mero de intervalos
333
         * @param minValue Valor m?nimo.
334
         * @param maxValue Valor m?ximo.
335
         * @param fieldName Nombre del campo
336
         *
337
         * @return Array con los intervalos.
338
         * @throws LegendLayerException
339
         */
340
        private FInterval[] calculateNaturalIntervals(DataSource recordSet, int numIntervals, double minValue,
341
                        double maxValue, String fieldName) throws LegendLayerException {
342
                NaturalIntervalGenerator intervalGenerator = new NaturalIntervalGenerator(
343
                                recordSet, fieldName, numIntervals);
344

    
345
                try {
346
                        intervalGenerator.generarIntervalos();
347
                } catch (ReadDriverException e) {
348
                        throw new LegendLayerException(Messages.getString("failed_calculating_intervals"), e);
349
                }
350

    
351
                int numIntervalsGen = intervalGenerator.getNumIntervals() - 1;
352

    
353
                if (numIntervalsGen == -1) {
354
                        //TODO cuando no puede calcular los intervalos.
355
                        numIntervalsGen = 1;
356
                }
357

    
358
                FInterval[] theIntervalArray = new FInterval[numIntervalsGen];
359

    
360
                if (numIntervalsGen > 1) {
361
                        theIntervalArray[0] = new FInterval(minValue,
362
                                        intervalGenerator.getValorRuptura(0));
363

    
364
                        for (int i = 1; i < (numIntervalsGen - 1); i++) {
365
                                theIntervalArray[i] = new FInterval(intervalGenerator.getValInit(i -
366
                                                1), intervalGenerator.getValorRuptura(i));
367
                        }
368

    
369
                        theIntervalArray[numIntervalsGen - 1] = new FInterval(intervalGenerator.getValInit(numIntervalsGen -
370
                                        2), maxValue);
371
                } else {
372
                        theIntervalArray[numIntervalsGen - 1] = new FInterval(minValue,
373
                                        maxValue);
374
                }
375

    
376
                return theIntervalArray;
377
        }
378

    
379
        /**
380
         * QUANTILE INTERVAL Devuelve un Array con el n?mero de intervalos que se
381
         * quieren crear. Los intervalos se distribuyen de forma quantile.
382
         * @param recordSet
383
         *
384
         * @param numIntervals n?mero de intervalos
385
         * @param minValue Valor m?nimo.
386
         * @param maxValue Valor m?ximo.
387
         * @param fieldName Nombre del campo
388
         *
389
         * @return Array con los intervalos.
390
         * @throws LegendLayerException
391
         */
392
        private FInterval[] calculateQuantileIntervals(DataSource recordSet, int numIntervals, double minValue,
393
                        double maxValue, String fieldName) throws LegendLayerException {
394
                QuantileIntervalGenerator intervalGenerator = new QuantileIntervalGenerator(
395
                                recordSet, fieldName, numIntervals);
396

    
397
                try {
398
                        intervalGenerator.generarIntervalos();
399
                } catch (ReadDriverException e) {
400
                        throw new LegendLayerException(Messages.getString("failed_calculating_intervals"), e);
401
                }
402

    
403
                int numIntervalsGen = intervalGenerator.getNumIntervalGen();
404
                FInterval[] theIntervalArray = new FInterval[numIntervalsGen];
405

    
406
                if (intervalGenerator.getNumIntervalGen() > 1) {
407
                        theIntervalArray[0] = new FInterval(minValue,
408
                                        intervalGenerator.getValRuptura(0));
409

    
410
                        for (int i = 1; i < (numIntervalsGen - 1); i++) {
411
                                theIntervalArray[i] = new FInterval(intervalGenerator.getValInit(i -
412
                                                1), intervalGenerator.getValRuptura(i));
413
                        }
414

    
415
                        theIntervalArray[numIntervalsGen - 1] = new FInterval(intervalGenerator.getValInit(numIntervalsGen -
416
                                        2), maxValue);
417
                } else {
418
                        theIntervalArray[numIntervalsGen - 1] = new FInterval(minValue,
419
                                        maxValue);
420
                }
421

    
422
                return theIntervalArray;
423
        }
424

    
425
        public ISymbol getSymbolByInterval(IInterval key) {
426

    
427
                if (key == null){
428
                        if (isUseDefaultSymbol())
429
                                return defaultSymbol;
430
                        return null;
431
                }
432
                if (symbols.containsKey(key)) {
433
                        return (ISymbol) symbols.get(key);
434
                }
435

    
436
                if (isUseDefaultSymbol())
437
                        return defaultSymbol;
438

    
439
                return null;
440
        }
441

    
442
        public String[] getDescriptions() {
443
                String[] descriptions = new String[symbols.size()];
444
                ISymbol[] auxSym = getSymbols();
445

    
446
                for (int i = 0; i < descriptions.length; i++)
447
                        descriptions[i] = auxSym[i].getDescription();
448

    
449
                return descriptions;
450
        }
451

    
452

    
453
        public Object[] getValues() {
454
                return symbols.keySet().toArray();
455
        }
456

    
457
        public void clear() {
458
                index = 0;
459
                keys.clear();
460
                symbols.clear();
461
        }
462

    
463
        public ISymbol[] getSymbols() {
464
                return (ISymbol[]) symbols.values().toArray(new ISymbol[0]);
465
        }
466

    
467
        public String[] getClassifyingFieldNames() {
468
                return fieldNames;
469
        }
470

    
471
        public void setDefaultSymbol(ISymbol s) {
472
                ISymbol old = defaultSymbol;
473
                if (s == null) throw new NullPointerException("Default symbol cannot be null");
474
                defaultSymbol = s;
475
                fireDefaultSymbolChangedEvent(new SymbolLegendEvent(old, defaultSymbol));
476
        }
477

    
478
        public void setClassifyingFieldNames(String[] fieldNames) {
479
                this.fieldNames = fieldNames;
480
        }
481

    
482
        public ISymbol getDefaultSymbol() {
483
                NullIntervalValue niv=new NullIntervalValue();
484
                if (symbols.containsKey(niv))
485
                        return (ISymbol)symbols.get(niv);
486

    
487
                if(defaultSymbol==null)
488
                        defaultSymbol=SymbologyFactory.createDefaultSymbolByShapeType(shapeType);
489
                return defaultSymbol;
490
        }
491

    
492

    
493
        public void setDataSource(DataSource ds)
494
        throws FieldNotFoundException, ReadDriverException {
495
//                if (!FLyrVect.forTestOnlyVariableUseIterators_REMOVE_THIS_FIELD) {
496
//                        /*
497
//                         * when we move definitely to feature iterators this
498
//                         * method
499
//                         */
500
//                        dataSource = ds;
501
//                        ds.start();
502
//                        fieldId = ds.getFieldIndexByName(fieldNames[0]);
503
//                        ds.stop();
504
//                }
505
        }
506

    
507

    
508
        public IInterval getInterval(Value v) {
509
                for (int i = 0; i < keys.size(); i++) {
510
                        if (((IInterval) keys.get(i)).isInInterval(v)) {
511
                                return (IInterval) keys.get(i);
512
                        }
513
                }
514

    
515
                return null;
516
        }
517

    
518
        public void setIntervalType(int intervalType) {
519
                this.intervalType = intervalType;
520
        }
521

    
522

    
523
        public int getIntervalType() {
524
                return intervalType;
525
        }
526

    
527
        public void useDefaultSymbol(boolean b) {
528
                useDefaultSymbol = b;
529
        }
530

    
531

    
532
        public boolean isUseDefaultSymbol() {
533
                return useDefaultSymbol;
534
        }
535

    
536

    
537
        public void delSymbol(Object obj) {
538
                keys.remove(obj);
539
                symbols.remove(obj);
540
                fireClassifiedSymbolChangeEvent(
541
                                new SymbolLegendEvent(
542
                                                symbols.remove(obj),
543
                                                null));
544
        }
545

    
546

    
547
        public void replace(ISymbol oldSymbol, ISymbol newSymbol) {
548
                if (symbols.containsValue(oldSymbol)) {
549
                        Iterator<Object> it = symbols.keySet().iterator();
550
                        while (it.hasNext()) {
551
                                Object key = it.next();
552
                                if (symbols.get(key).equals(oldSymbol)) {
553
                                        symbols.remove(key);
554
                                        symbols.put(key, newSymbol);
555
                                        fireClassifiedSymbolChangeEvent(
556
                                                        new SymbolLegendEvent(oldSymbol, newSymbol));
557
                                }
558
                        }
559
                }
560
        }
561
}