Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / rendering / legend / AbstractIntervalLegend.java @ 21911

History | View | Annotate | Download (16.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.fmap.mapcontext.rendering.legend;
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.apache.log4j.Logger;
96
import org.gvsig.fmap.data.ReadException;
97
import org.gvsig.fmap.data.vectorial.Feature;
98
import org.gvsig.fmap.data.vectorial.FeatureID;
99
import org.gvsig.fmap.data.vectorial.FeatureStore;
100
import org.gvsig.fmap.data.vectorial.FeatureType;
101
import org.gvsig.fmap.mapcontext.Messages;
102
import org.gvsig.fmap.mapcontext.exceptions.LegendLayerException;
103
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
104
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
105
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbologyFactory;
106

    
107

    
108
public abstract class AbstractIntervalLegend extends AbstractClassifiedVectorLegend implements IVectorialIntervalLegend{
109
        protected int shapeType;
110

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

    
123
                                if (o2 instanceof NullIntervalValue) {
124
                                        return 1;
125
                                }
126

    
127
                                if (o1 instanceof NullIntervalValue) {
128
                                        return -1;
129
                                }
130

    
131
                                FInterval i2 = (FInterval) o2;
132
                                FInterval i1 = (FInterval) o1;
133

    
134
                                if (i1.getMin() > i2.getMin()) {
135
                                        return 1;
136
                                }
137

    
138
                                if (i1.getMin() < i2.getMin()) {
139
                                        return -1;
140
                                }
141
                                if (i1.getMax() < i2.getMax()) {
142
                                        return -1;
143
                                }
144
                                if (i1.getMax() > i2.getMax()) {
145
                                        return 1;
146
                                }
147
                        }
148

    
149
                        return 0;
150
                }
151
        });
152
        protected ArrayList<Object> keys = new ArrayList<Object>();
153
        protected int index = 0;
154
        protected String[] fieldNames;
155
        protected int fieldId;
156
        protected ISymbol defaultSymbol;
157
        protected FeatureStore featureStore;
158
        protected int intervalType = NATURAL_INTERVALS;
159
        protected boolean useDefaultSymbol = false;
160

    
161
        private Logger logger = Logger.getLogger(AbstractIntervalLegend.class);
162

    
163
        public void addSymbol(Object key, ISymbol symbol) {
164
                symbols.put(key, symbol);
165
                keys.add(key);
166
                fireClassifiedSymbolChangeEvent(new SymbolLegendEvent(null, symbol));
167
        }
168

    
169
        /*
170
         * @see com.iver.cit.gvsig.fmap.rendering.IVectorialLegend#getSymbol(int)
171
         */
172
        public ISymbol getSymbol(FeatureID featureID) throws ReadException {
173
                Object val = featureStore.getFeatureByID(featureID).get(fieldId);
174
                IInterval interval = getInterval(val);
175
                ISymbol theSymbol = getSymbolByInterval(interval);
176

    
177

    
178
                if (theSymbol != null) {
179
                        return theSymbol;
180
                } else if (useDefaultSymbol) {
181
                        return getDefaultSymbol();
182
                }
183

    
184
                return null;
185
        }
186

    
187

    
188
        public ISymbol getSymbolByFeature(Feature feat) {
189
                Object val = feat.get(fieldId);
190
                IInterval interval = getInterval(val);
191
                ISymbol theSymbol = getSymbolByInterval(interval);
192

    
193
                if (theSymbol == null)
194
                        return getDefaultSymbol();
195
//
196
//                if (shapeType == FShape.POLYGON && theSymbol instanceof IMarkerSymbol) {
197
//                        // transform it to a fill symbol
198
//                        MarkerFillSymbol aux = new MarkerFillSymbol();
199
//                        // tell the fill style to draw the IMarkerSymbol
200
//                        // as a IFillSymbol centering it in the shape polygon
201
//                        // centroid and applying offset (if any).
202
//                        aux.setMarker((IMarkerSymbol) theSymbol);
203
//                        SimpleMarkerFillPropertiesStyle p = new SimpleMarkerFillPropertiesStyle();
204
//                        p.setFillStyle(SimpleMarkerFillPropertiesStyle.SINGLE_CENTERED_SYMBOL);
205
//                        aux.setMarkerFillProperties(p);
206
//                        theSymbol = aux;
207
//                }
208
//
209

    
210
                return theSymbol;
211
        }
212

    
213

    
214
        public FInterval[] calculateIntervals(FeatureStore featureStore, String fieldName, int numIntervalos, int shapeType)
215
        throws ReadException, LegendLayerException {
216
                logger.debug("elRs.start()");
217
//                recordSet.start();
218

    
219
                int idField = -1;
220

    
221
                setClassifyingFieldNames(new String[] {fieldName});
222

    
223
                String[] fieldNames = getClassifyingFieldNames();
224
                Iterator iterator=featureStore.getDataCollection(new String[]{fieldName},null,null).iterator();
225

    
226

    
227
//                for (int i = 0; i < recordSet.getFieldCount(); i++) {
228
//                        String nomAux = recordSet.getFieldName(i).trim();
229
//
230
//                        if (fieldNames[0].compareToIgnoreCase(nomAux) == 0) {
231
//                                idField = i;
232
//
233
//                                break;
234
//                        }
235
//                }
236

    
237
                if (idField == -1) {
238
                        logger.error("Campo no reconocido " + fieldNames);
239

    
240
                        return null;
241
                }
242

    
243
                double minValue = Double.MAX_VALUE;
244
                double maxValue = Double.NEGATIVE_INFINITY;
245

    
246
                VectorialIntervalLegend auxLegend = LegendFactory.
247
                    createVectorialIntervalLegend(shapeType);
248

    
249
                Object clave;
250

    
251
                while (iterator.hasNext()) {
252
                        Feature feature = (Feature) iterator.next();
253

    
254
//                for (int j = 0; j < recordSet.getRowCount(); j++) {
255
                        clave = feature.get(idField);
256

    
257
                        IInterval interval = auxLegend.getInterval(clave);
258

    
259
                        ////Comprobar que no esta repetido y no hace falta introducir en el hashtable el campo junto con el simbolo.
260
                        if (auxLegend.getSymbolByInterval(interval) == null) {
261
                                //si no esta creado el simbolo se crea
262
                                double valor = 0;
263

    
264

    
265
                                if (clave instanceof Number) {
266
                                        valor=((Number)clave).doubleValue();
267
                                }else if (clave instanceof Date) {
268
                                        //TODO POR IMPLEMENTAR
269
                                        ///valorDate = elRs.getFieldValueAsDate(idField);
270
                                        ///if (valorDate.before(minValueDate)) minValueDate = valorDate;
271
                                        ///if (valorDate.after(maxValueDate)) maxValueDate = valorDate;
272
                                } else if (clave instanceof NullValue) {
273
                                        continue;
274
                                }
275

    
276
                                if (valor < minValue) {
277
                                        minValue = valor;
278
                                }
279

    
280
                                if (valor > maxValue) {
281
                                        maxValue = valor;
282
                                }
283
                        }
284
                }
285

    
286
                FInterval[] intervalArray = null;
287
                switch (getIntervalType()) {
288
                case VectorialIntervalLegend.EQUAL_INTERVALS:
289
                        intervalArray = calculateEqualIntervals(numIntervalos,
290
                                        minValue, maxValue, fieldName);
291

    
292
                        break;
293

    
294
                case VectorialIntervalLegend.NATURAL_INTERVALS:
295
                        intervalArray = calculateNaturalIntervals(featureStore, numIntervalos,
296
                                        minValue, maxValue, fieldName);
297

    
298
                        break;
299

    
300
                case VectorialIntervalLegend.QUANTILE_INTERVALS:
301
                        intervalArray = calculateQuantileIntervals(featureStore, numIntervalos,
302
                                        minValue, maxValue, fieldName);
303

    
304
                        break;
305
                }
306
//                recordSet.stop();
307
                return intervalArray;
308
        }
309

    
310

    
311
    /**
312
     * EQUAL INTERVAL Devuelve un Array con el n?mero de intervalos que se
313
     * quieren crear. Los intervalos se crean con un tama?o igual entre ellos.
314
     * @param numIntervals n?mero de intervalos
315
     * @param minValue Valor m?nimo.
316
     * @param maxValue Valor m?ximo.
317
     * @param fieldName Nombre del campo
318
     *
319
     * @return Array con los intervalos.
320
     */
321
    private FInterval[] calculateEqualIntervals(int numIntervals, double minValue,
322
        double maxValue, String fieldName) {
323
        FInterval[] theIntervalArray = new FInterval[numIntervals];
324
        double step = (maxValue - minValue) / numIntervals;
325

    
326
        if (numIntervals > 1) {
327
            theIntervalArray[0] = new FInterval(minValue, minValue + step);
328

    
329
            for (int i = 1; i < (numIntervals - 1); i++) {
330
                theIntervalArray[i] = new FInterval(minValue + (i * step) +
331
                        0.01, minValue + ((i + 1) * step));
332
            }
333

    
334
            theIntervalArray[numIntervals - 1] = new FInterval(minValue +
335
                    ((numIntervals - 1) * step) + 0.01, maxValue);
336
        } else {
337
            theIntervalArray[0] = new FInterval(minValue, maxValue);
338
        }
339

    
340
        return theIntervalArray;
341
    }
342

    
343
    /**
344
     * NATURAL INTERVAL Devuelve un Array con el n?mero de intervalos que se
345
     * quieren crear. Los intervalos se distribuyen de forma natural.
346
     *
347
     * @param numIntervals n?mero de intervalos
348
     * @param minValue Valor m?nimo.
349
     * @param maxValue Valor m?ximo.
350
     * @param fieldName Nombre del campo
351
     *
352
     * @return Array con los intervalos.
353
     * @throws LegendLayerException
354
     */
355
    private FInterval[] calculateNaturalIntervals(FeatureStore featureStore, int numIntervals, double minValue,
356
        double maxValue, String fieldName) throws LegendLayerException {
357
        NaturalIntervalGenerator intervalGenerator = new NaturalIntervalGenerator(
358
                        featureStore, fieldName, numIntervals);
359

    
360
        try {
361
            intervalGenerator.generarIntervalos();
362
        } catch (ReadException e) {
363
                throw new LegendLayerException(Messages.getString("failed_calculating_intervals"), e);
364
        }
365

    
366
        int numIntervalsGen = intervalGenerator.getNumIntervals() - 1;
367

    
368
        if (numIntervalsGen == -1) {
369
            //TODO cuando no puede calcular los intervalos.
370
            numIntervalsGen = 1;
371
        }
372

    
373
        FInterval[] theIntervalArray = new FInterval[numIntervalsGen];
374

    
375
        if (numIntervalsGen > 1) {
376
            theIntervalArray[0] = new FInterval(minValue,
377
                    intervalGenerator.getValorRuptura(0));
378

    
379
            for (int i = 1; i < (numIntervalsGen - 1); i++) {
380
                theIntervalArray[i] = new FInterval(intervalGenerator.getValInit(i -
381
                            1), intervalGenerator.getValorRuptura(i));
382
            }
383

    
384
            theIntervalArray[numIntervalsGen - 1] = new FInterval(intervalGenerator.getValInit(numIntervalsGen -
385
                        2), maxValue);
386
        } else {
387
            theIntervalArray[numIntervalsGen - 1] = new FInterval(minValue,
388
                    maxValue);
389
        }
390

    
391
        return theIntervalArray;
392
    }
393

    
394
    /**
395
     * QUANTILE INTERVAL Devuelve un Array con el n?mero de intervalos que se
396
     * quieren crear. Los intervalos se distribuyen de forma quantile.
397
     * @param recordSet
398
     *
399
     * @param numIntervals n?mero de intervalos
400
     * @param minValue Valor m?nimo.
401
     * @param maxValue Valor m?ximo.
402
     * @param fieldName Nombre del campo
403
     *
404
     * @return Array con los intervalos.
405
     * @throws LegendLayerException
406
     */
407
    private FInterval[] calculateQuantileIntervals(FeatureStore featureStore, int numIntervals, double minValue,
408
        double maxValue, String fieldName) throws LegendLayerException {
409
        QuantileIntervalGenerator intervalGenerator = new QuantileIntervalGenerator(
410
                        featureStore, fieldName, numIntervals);
411

    
412
        try {
413
            intervalGenerator.generarIntervalos();
414
        } catch (ReadException e) {
415
                throw new LegendLayerException(Messages.getString("failed_calculating_intervals"), e);
416
        }
417

    
418
        int numIntervalsGen = intervalGenerator.getNumIntervalGen();
419
        FInterval[] theIntervalArray = new FInterval[numIntervalsGen];
420

    
421
        if (intervalGenerator.getNumIntervalGen() > 1) {
422
            theIntervalArray[0] = new FInterval(minValue,
423
                    intervalGenerator.getValRuptura(0));
424

    
425
            for (int i = 1; i < (numIntervalsGen - 1); i++) {
426
                theIntervalArray[i] = new FInterval(intervalGenerator.getValInit(i -
427
                            1), intervalGenerator.getValRuptura(i));
428
            }
429

    
430
            theIntervalArray[numIntervalsGen - 1] = new FInterval(intervalGenerator.getValInit(numIntervalsGen -
431
                        2), maxValue);
432
        } else {
433
            theIntervalArray[numIntervalsGen - 1] = new FInterval(minValue,
434
                    maxValue);
435
        }
436

    
437
        return theIntervalArray;
438
    }
439

    
440
        public ISymbol getSymbolByInterval(IInterval key) {
441
                if (key == null) {
442
                        return null;
443
                }
444

    
445
                if (symbols.containsKey(key)) {
446
                        return (ISymbol) symbols.get(key);
447
                }
448

    
449
                return null;
450
        }
451

    
452
        public String[] getDescriptions() {
453
                String[] descriptions = new String[symbols.size()];
454
                ISymbol[] auxSym = getSymbols();
455

    
456
                for (int i = 0; i < descriptions.length; i++)
457
                        descriptions[i] = auxSym[i].getDescription();
458

    
459
                return descriptions;
460
        }
461

    
462

    
463
        public Object[] getValues() {
464
                return symbols.keySet().toArray();
465
        }
466

    
467
        public void clear() {
468
                index = 0;
469
                keys.clear();
470
                symbols.clear();
471
        }
472

    
473
        public ISymbol[] getSymbols() {
474
                return (ISymbol[]) symbols.values().toArray(new ISymbol[0]);
475
        }
476

    
477
        public String[] getClassifyingFieldNames() {
478
                return fieldNames;
479
        }
480

    
481
        public void setDefaultSymbol(ISymbol s) {
482
            ISymbol old = defaultSymbol;
483
                if (s == null) throw new NullPointerException("Default symbol cannot be null");
484
                defaultSymbol = s;
485
                fireDefaultSymbolChangedEvent(new SymbolLegendEvent(old, defaultSymbol));
486
        }
487

    
488
        public void setClassifyingFieldNames(String[] fieldNames) {
489
                this.fieldNames = fieldNames;
490
        }
491

    
492
        public ISymbol getDefaultSymbol() {
493
                NullIntervalValue niv=new NullIntervalValue();
494
                if (symbols.containsKey(niv))
495
                        return (ISymbol)symbols.get(niv);
496

    
497
                if(defaultSymbol==null)
498
                    defaultSymbol=SymbologyFactory.createDefaultFillSymbol();//mio
499
                return defaultSymbol;
500
        }
501

    
502

    
503
    public void setFeatureStore(FeatureStore featureStore)
504
        throws ReadException {
505
                    /*
506
                     * when we move definitely to feature iterators this
507
                     * method
508
                     */
509
                    this.featureStore = featureStore;
510
//                    ds.start();
511
                    fieldId = ((FeatureType)featureStore.getFeatureTypes().get(0)).getFieldIndex(fieldNames[0]);
512
//                    ds.stop();
513
    }
514

    
515

    
516
        public IInterval getInterval(Object v) {
517
                for (int i = 0; i < keys.size(); i++) {
518
                        if (((IInterval) keys.get(i)).isInInterval(v)) {
519
                                return (IInterval) keys.get(i);
520
                        }
521
                }
522

    
523
                return null;
524
        }
525

    
526
        public void setIntervalType(int intervalType) {
527
                this.intervalType = intervalType;
528
        }
529

    
530

    
531
        public int getIntervalType() {
532
                return intervalType;
533
        }
534

    
535
        public void useDefaultSymbol(boolean b) {
536
                useDefaultSymbol = b;
537
        }
538

    
539

    
540
        public boolean isUseDefaultSymbol() {
541
                return useDefaultSymbol;
542
        }
543

    
544

    
545
        public void delSymbol(Object obj) {
546
                keys.remove(obj);
547
                symbols.remove(obj);
548
                fireClassifiedSymbolChangeEvent(
549
                                new SymbolLegendEvent(
550
                                                symbols.remove(obj),
551
                                                null));
552
        }
553

    
554

    
555
        public void replace(ISymbol oldSymbol, ISymbol newSymbol) {
556
                if (symbols.containsValue(oldSymbol)) {
557
                        Iterator it = symbols.keySet().iterator();
558
                        while (it.hasNext()) {
559
                                Object key = it.next();
560
                                if (symbols.get(key).equals(oldSymbol)) {
561
                                        symbols.remove(key);
562
                                        symbols.put(key, newSymbol);
563
                                }
564
                        }
565
                }
566
        }
567
}