Statistics
| Revision:

gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster_dataaccess_refactoring / org.gvsig.raster.lib / org.gvsig.raster.lib.api / src / main / java / org / gvsig / raster / roi / AbstractROI.java @ 2436

History | View | Annotate | Download (11.9 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.raster.roi;
23

    
24
import java.awt.Color;
25
import java.util.List;
26

    
27
import org.gvsig.fmap.dal.coverage.RasterLocator;
28
import org.gvsig.fmap.dal.coverage.RasterManager;
29
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
30
import org.gvsig.fmap.dal.coverage.datastruct.GridExtent;
31
import org.gvsig.fmap.dal.coverage.exception.FileNotOpenException;
32
import org.gvsig.fmap.dal.coverage.exception.GridException;
33
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
34
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
35
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
36
import org.gvsig.fmap.dal.coverage.store.props.Statistics;
37
import org.gvsig.tools.ToolsLocator;
38
import org.gvsig.tools.dynobject.DynStruct;
39
import org.gvsig.tools.persistence.PersistenceManager;
40
import org.gvsig.tools.persistence.PersistentState;
41
import org.gvsig.tools.persistence.exception.PersistenceException;
42

    
43
/**
44
 * Clase que representa las Regiones de Interes. 
45
 * 
46
 * @author Diego Guerrero Sevilla (diego.guerrero@uclm.es).
47
 *
48
 */
49
public abstract class AbstractROI implements ROI {
50
        public static final String      PERSISTENT_NAME        = "AbstractROI_Persistent";
51
    public static final String      PERSISTENT_DESCRIPTION = "AbstractROI Persistent";
52
    
53
        private long                    id                     = 0;
54
        private String                                         name                           = "";
55
        private Color                                        color                           = null;
56
        private RasterDataStore         store                            = null;
57
        protected Statistics                        statistic                    = null;
58
        /**
59
         * M?nimo extent que contiene al ROI.
60
         */
61
        protected Extent                             roiExtent              = null;
62
        private int                     pxWidth                = -1;
63
        private int                     pxHeight               = -1;
64
        
65
        /**
66
         * Coord. x del grid donde est? laz esquina sup. iz. del roi (coords. pixel).
67
         */
68
        protected int                                         xOffset                           = 0;
69
        /**
70
         * Coord. y del grid donde est? la esquina sup. iz. del roi (coords. pixel).
71
         */
72
        protected int                                        yOffset                           = 0;
73
        /**
74
         * Subconjunto de bandas que abarca el ROI.
75
         */
76
        private int                                         bandsSubset[]          = null;
77
        private RasterManager           manager                = null;
78
        private int                     operationBand          = 0;
79
        
80
        public AbstractROI(RasterDataStore store) {
81
                this.manager = RasterLocator.getManager();
82
                this.store = store;
83
                roiExtent = this.manager.getDataStructFactory().createExtent();
84
                statistic = this.manager.createROIStatistics(this);
85
        }
86
        
87
        public RasterDataStore getStore() {
88
                return store;
89
        }
90
        
91
        /**
92
         * Obtiene el extent m?ximo de todas las ROIs pasadas por par?metro.
93
         * @param rois Lista de ROIs para calcular la extensi?n m?xima que ocupan
94
         * @return Entent m?ximo
95
         */
96
        public static Extent getROIsMaximunExtent(List<ROI> rois) {
97
                double minx = 0, miny = 0, maxx = 0, maxy = 0;
98
                for (int i = 0; i < rois.size(); i++) {
99
                        Extent ext = ((AbstractROI) rois.get(i)).getROIExtent();
100
                        if(i == 0) {
101
                                minx = ext.minX();
102
                                miny = ext.minY();
103
                                maxx = ext.maxX();
104
                                maxy = ext.maxY();
105
                        } else {
106
                                if(ext.minX() < minx)
107
                                        minx = ext.minX();
108
                                if(ext.minY() < miny)
109
                                        miny = ext.minY();
110
                                if(ext.maxX() > maxx)
111
                                        maxx = ext.maxX();
112
                                if(ext.maxY() > maxy)
113
                                        maxy = ext.maxY();
114
                        }
115
                }
116
                return RasterLocator.getManager().getDataStructFactory().createExtent(minx, miny, maxx, maxy);
117
        }
118

    
119
        public ROI add(ROI roi){
120
                return null;
121
        }
122
        
123
        public ROI intersect(ROI roi){
124
                return null;
125
        }
126
        
127
        public ROI subtract(ROI roi){
128
                return null;
129
        }
130
        
131
        public ROI exclusiveOr(ROI roi){
132
                return null;
133
        }
134

    
135
        public double getCellSize() {
136
                return store.getCellSize();
137
        }
138
        
139
        public Extent getROIExtent() {
140
                return roiExtent;
141
        }
142
        
143
        public Extent getStoreExtent() {
144
                return store.getExtent();
145
        }
146

    
147
        public int getLayerWidth() {
148
                return (int)store.getWidth();
149
        }
150

    
151
        public int getLayerHeight() {
152
                return (int)store.getHeight();
153
        }
154
        
155
        /**
156
         * 
157
         * @return n?mero de celdas del ROI.
158
         */
159
        public long getValues() throws GridException {
160
                if (!statistic.isCalculated())
161
                        try {
162
                                statistic.calculate(1);
163
                        } catch (FileNotOpenException e) {
164
                                throw new GridException("", e);
165
                        } catch (RasterDriverException e) {
166
                                throw new GridException("", e);
167
                        } catch (ProcessInterruptedException e) {
168
                                throw new GridException("", e);
169
                        }
170
                return statistic.getNumberOfCells()[operationBand];
171
        }
172

    
173
        public double getMaxValue() throws GridException {
174
                if (!statistic.isCalculated())
175
                        try {
176
                                statistic.calculate(1);
177
                        } catch (FileNotOpenException e) {
178
                                throw new GridException("", e);
179
                        } catch (RasterDriverException e) {
180
                                throw new GridException("", e);
181
                        } catch (ProcessInterruptedException e) {
182
                                throw new GridException("", e);
183
                        }
184
                return statistic.getMax()[operationBand];
185
        }
186

    
187
        public double getMeanValue() throws GridException {
188
                if (!statistic.isCalculated())
189
                        try {
190
                                statistic.calculate(1);
191
                        } catch (FileNotOpenException e) {
192
                                throw new GridException("", e);
193
                        } catch (RasterDriverException e) {
194
                                throw new GridException("", e);
195
                        } catch (ProcessInterruptedException e) {
196
                                throw new GridException("", e);
197
                        }
198
                return statistic.getMean()[operationBand];
199
        }
200

    
201
        public double getMinValue() throws GridException {
202
                if (!statistic.isCalculated())
203
                        try {
204
                                statistic.calculate(1);
205
                        } catch (FileNotOpenException e) {
206
                                throw new GridException("", e);
207
                        } catch (RasterDriverException e) {
208
                                throw new GridException("", e);
209
                        } catch (ProcessInterruptedException e) {
210
                                throw new GridException("", e);
211
                        }
212
                return statistic.getMin()[operationBand];
213
        }
214

    
215
        /**
216
        * Devuelve la matriz de varianza-covarianza, si no se encuentra calculada se calcula
217
        * @retrun Matriz de varianza-covarianza
218
        */
219
        public double[][] getVarCovMatrix() throws GridException {
220
                if(!statistic.isAdvancedStatisticsCalculated())
221
                        statistic.calculateAdvanced();
222
                return statistic.getVarianceCovarianceMatrix();
223
        }
224
        
225
        public int getROIPxWidth() {
226
                if(pxWidth < 0)
227
                        pxWidth = (int) Math.abs(Math.floor((roiExtent.maxX() - roiExtent.minX()) / store.getCellSize()));
228
                return pxWidth;
229
        }
230

    
231
        public int getROIPxHeight() {
232
                if(pxHeight < 0)
233
                        pxHeight = (int) Math.abs(Math.floor((roiExtent.minY() - roiExtent.maxY()) / store.getCellSize()));
234
                return pxHeight;
235
        }
236

    
237
        public double getNoDataValue() {
238
                //                 TODO Ver si usar un nodatavalue especifico del roi.
239
                return store.getNoDataValue().getValue().doubleValue();
240
        }
241

    
242
        public double getVariance() throws GridException {
243
                if (!statistic.isCalculated())
244
                        try {
245
                                statistic.calculate(1);
246
                        } catch (FileNotOpenException e) {
247
                                throw new GridException("", e);
248
                        } catch (RasterDriverException e) {
249
                                throw new GridException("", e);
250
                        } catch (ProcessInterruptedException e) {
251
                                throw new GridException("", e);
252
                        };
253
                return statistic.getVariance()[operationBand];
254
        }
255

    
256
        /**
257
         * Checks if a point in pixel coordinates which is inside the grid, belongs to
258
         * a geometry
259
         * @param x X coordinate
260
         * @param y Y coordinate 
261
         * @return true is inside of a geometry 
262
         */
263
        public abstract boolean isInsideOfPolygon(int x, int y);
264
        
265
        /**
266
         * Checks if a rectangle in pixel coordinates intersects with some geometry of this ROI 
267
         * @param x X upper left coordinate
268
         * @param y Y upper left coordinate
269
         * @param w Width of the area in pixels
270
         * @param h Height of the area in pixels
271
         * @return true is inside of a geometry 
272
         */
273
        public abstract boolean isInside(double x, double y, double w, double h);        
274

    
275
        public Color getColor() {
276
                return color;
277
        }
278

    
279
        public void setColor(Color color) {
280
                this.color = color;
281
        }
282

    
283
        public String getName() {
284
                return name;
285
        }
286

    
287
        public void setName(String name) {
288
                this.name = name;
289
        }
290
        
291
        public long getID() {
292
                return id;
293
        }
294
        
295
        public void setID(long id) {
296
                this.id = id;
297
        }
298
        
299
        public String toString() {
300
                return getName();
301
        }
302
        
303
        /**
304
         *Establece el Subconjunto de bandas que abarca el ROI. 
305
         *
306
         */
307
        public void setBandsSubset(int bandsSubset[]){
308
                this.bandsSubset=bandsSubset;
309
                statistic.setCalculated(false);
310
        }
311
        
312
        /**
313
         * Restaura el subconjunto accesible por el ROI a todas las bandas del Grid.
314
         */
315
        public void clearBandsSubset(){
316
                if (bandsSubset!=null){
317
                        bandsSubset = null;
318
                        statistic.setCalculated(false);
319
                }
320
        }
321
        
322
        /**
323
         * @return N?mero de bandas que abarca el ROI.
324
         */
325
        public int getBandCount() {
326
                return store.getBandCount();
327
        }
328

    
329
        /**
330
         * 
331
         * @return Subconjunto de bandas que abarca el ROI.
332
         */
333
        public int[] getBandsSubset() {
334
                return bandsSubset;
335
        }
336

    
337
        protected Statistics getStatistic() {
338
                return statistic;
339
        }
340
        
341

    
342
        /**
343
         * @deprecated The regions of interest should not be persisted
344
         */
345
        @SuppressWarnings("unchecked")
346
        public void loadFromState(PersistentState state)
347
                        throws PersistenceException {
348
                this.id = state.getLong("id");
349
                this.name = state.getString("name");
350
                this.color = (Color)state.get("color");
351
                //this.grid = (Grid)state.get("grid");
352
                this.statistic = (Statistics)state.get("statistic");
353
                this.roiExtent = (GridExtent)state.get("roiExtent");
354
                this.xOffset = state.getInt("xOffset");
355
                this.yOffset = state.getInt("yOffset");
356
                
357
                List<Integer> aux = state.getList("bandsSubset");
358
                this.bandsSubset = new int[aux.size()];
359
                for (int i = 0; i < aux.size(); i++) {
360
                        this.bandsSubset[i] = aux.get(i).intValue();
361
                }
362
                this.manager = (RasterManager)state.get("manager");
363
                this.operationBand = state.getInt("operationBand");
364
        }
365

    
366
        /**
367
         * @deprecated The regions of interest should not be persisted
368
         */
369
        public void saveToState(PersistentState state) throws PersistenceException {
370
                state.set("id", id);                
371
                state.set("name", name);
372
                state.set("color", color);        
373
                //state.set("grid", grid);        
374
                state.set("statistic", statistic);        
375
                state.set("roiExtent", roiExtent);        
376
                state.set("xOffset", xOffset);        
377
                state.set("yOffset", yOffset);
378
                state.set("bandsSubset", bandsSubset);
379
                state.set("manager", manager);
380
                state.set("operationBand", operationBand);
381
        }        
382
        
383
        /**
384
         * @deprecated The regions of interest should not be persisted
385
         */
386
        public static void registerPersistence() {
387
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
388
                DynStruct definition = manager.getDefinition(PERSISTENT_NAME);
389
                if( definition == null ) {
390
                        definition = manager.addDefinition(
391
                                        AbstractROI.class,
392
                                        PERSISTENT_NAME,
393
                                        PERSISTENT_DESCRIPTION,
394
                                        null, 
395
                                        null
396
                        );
397
                        definition.addDynFieldLong("id").setMandatory(true);
398
                        definition.addDynFieldString("name").setMandatory(true);
399
                        definition.addDynFieldObject("color").setClassOfValue(Color.class).setMandatory(false);
400
                        //definition.addDynFieldObject("grid").setClassOfValue(Grid.class).setMandatory(false);
401
                        definition.addDynFieldObject("statistic").setClassOfValue(Statistics.class).setMandatory(false);
402
                        definition.addDynFieldObject("roiExtent").setClassOfValue(GridExtent.class).setMandatory(false);
403
                        definition.addDynFieldInt("xOffset").setMandatory(false);
404
                        definition.addDynFieldInt("yOffset").setMandatory(false);
405
                        definition.addDynFieldList("bandsSubset").setClassOfItems(Integer.class).setMandatory(false);
406
                        definition.addDynFieldObject("manager").setClassOfValue(GridExtent.class).setMandatory(true);
407
                        definition.addDynFieldInt("operationBand").setMandatory(true);
408
                }
409
        }
410
}