Statistics
| Revision:

gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / grid / roi / VectorialROIImpl.java @ 2126

History | View | Annotate | Download (6.55 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

    
23
package org.gvsig.raster.impl.grid.roi;
24

    
25
import java.awt.geom.Point2D;
26
import java.awt.geom.Rectangle2D;
27
import java.util.ArrayList;
28
import java.util.List;
29

    
30
import org.gvsig.fmap.dal.coverage.RasterLocator;
31
import org.gvsig.fmap.dal.coverage.RasterManager;
32
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
33
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
34
import org.gvsig.fmap.geom.Geometry;
35
import org.gvsig.raster.roi.AbstractROI;
36
import org.gvsig.raster.roi.VectorialROI;
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 una regi?n de interes conformada por
45
 * elementos vectoriales.
46
 *
47
 * @author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
48
 *
49
 */
50
public class VectorialROIImpl extends AbstractROI implements VectorialROI {
51

    
52
        /**
53
         * Array de geometr?as (IGeometry) que definen el ROI.
54
         */
55
        private ArrayList<Geometry>                geometries   = null;
56

    
57
        public VectorialROIImpl(RasterDataStore store) {
58
                super(store);
59
                geometries = new ArrayList<Geometry>();
60
        }
61

    
62
        public boolean isInside(double x, double y, double w, double h) {
63
                if((x + w) < getROIExtent().minX() || x > getROIExtent().maxX() || (y - h) > getROIExtent().maxY() || y < getROIExtent().minY())
64
                        return false;
65
                for (int i = 0; i < geometries.size(); i++) {
66
                        Rectangle2D celda = new Rectangle2D.Double(x, y, w, h);
67
                        if(((Geometry)geometries.get(i)).intersects(celda))
68
                                return true;
69
                }
70
                return false;
71
        }
72

    
73
        public boolean isInsideOfPolygon(int x, int y) {
74
                double newX = x - xOffset;
75
                double newY = y - yOffset;
76
                //Comprobaci?n de que el punto cae dentro de los l?mites de la bbox de las geometr?as
77
                if (newX < 0 || newX > getROIPxWidth() || newY < 0 || newY > getROIPxHeight()) 
78
                        return false;
79

    
80
                Point2D p = getStore().rasterToWorld(new Point2D.Double(x, y));
81
                for (int i = 0; i < geometries.size(); i++){
82
                        if(((Geometry)geometries.get(i)).contains(p.getX(), p.getY()))
83
                                return true;
84
                }
85
                return false;
86
        }
87

    
88
        /**
89
         * A?ade una geometr?a a la ROI
90
         *
91
         * @param geometry
92
         */
93
        public void addGeometry(Geometry geometry){
94
                RasterManager rManager = RasterLocator.getManager();
95
                geometries.add(geometry);
96

    
97
                Rectangle2D geometryBounds = geometry.getBounds();
98
                if(getROIExtent() == null)
99
                        roiExtent = rManager.getDataStructFactory().createExtent(
100
                                        geometryBounds.getMinX(), 
101
                                        geometryBounds.getMaxY(), 
102
                                        geometryBounds.getMaxX(), 
103
                                        geometryBounds.getMinY());
104
                else
105
                        roiExtent = rManager.getDataStructFactory().createExtent(
106
                                                                        Math.min(geometryBounds.getMinX(), getROIExtent().minX()),
107
                                                                        Math.max(geometryBounds.getMaxY(), getROIExtent().maxY()),
108
                                                                        Math.max(geometryBounds.getMaxX(), getROIExtent().maxX()),
109
                                                                        Math.min(geometryBounds.getMinY(), getROIExtent().minY()));
110

    
111
                double minX = geometryBounds.getMinX() - Math.abs(geometryBounds.getMinX() - getStoreExtent().getMin().getX()) % getCellSize();
112
                double minY = (geometryBounds.getMinY()) - (Math.abs(geometryBounds.getMinY() - getStoreExtent().getMin().getY())) % getCellSize();
113
                double maxX = (geometryBounds.getMaxX()  + getCellSize()) - Math.abs(geometryBounds.getMaxX() - getStoreExtent().getMin().getX()) % getCellSize();
114
                double maxY = (geometryBounds.getMaxY() + getCellSize()) - (Math.abs(geometryBounds.getMaxY() - getStoreExtent().getMin().getY())) % getCellSize();
115

    
116
                if (geometries.size() > 1) {
117
                        minX = Math.min(minX, getROIExtent().getMin().getX());
118
                        minY = Math.min(minY, getROIExtent().getMin().getY());
119
                        maxX = Math.max(maxX, getROIExtent().getMax().getX());
120
                        maxY = Math.max(maxY, getROIExtent().getMax().getY());
121
                }
122

    
123
                xOffset = (int)((minX - getStoreExtent().getMin().getX()) / getCellSize());
124
                yOffset = (int)((getStoreExtent().getMax().getY()- maxY) / getCellSize());
125
                getStatistic().setCalculated(false);
126
        }
127

    
128
        /**
129
         * Eliminar la geometr?a con ?ndice <code>index</code> de la ROI
130
         *
131
         * @param index indice de la geometr?a a eliminar
132
         */
133
        public void deleteGeometry(int index){
134
                geometries.remove(index);
135
                getStatistic().setCalculated(false);
136
        }
137

    
138
        /**
139
         * Elimina todas las geometr?as que definen la ROI.
140
         *
141
         */
142
        public void clear(){
143
                geometries.clear();
144
                getStatistic().setCalculated(false);
145
        }
146

    
147
        /**
148
         *
149
         * @return ArryList con las geometr?as que definen la ROI
150
         */
151
        public List<Geometry> getGeometries() {
152
                return geometries;
153
        }
154

    
155
        
156
        /**
157
         * @deprecated The regions of interest should not be persisted
158
         */
159
        @SuppressWarnings("unchecked")
160
        public void loadFromState(PersistentState state)
161
        throws PersistenceException {
162
                super.loadFromState(state);
163
                List<Geometry> aux = state.getList("geometries");
164
                this.geometries = new ArrayList<Geometry>();
165
                this.geometries.addAll(aux);
166
                
167
                roiExtent = (Extent)state.get("extent");
168
        }
169

    
170
        /**
171
         * @deprecated The regions of interest should not be persisted
172
         */
173
        public void saveToState(PersistentState state) throws PersistenceException {
174
                super.saveToState(state);
175
                state.set("geometries", geometries);                
176
                state.set("extent", getROIExtent());
177
        }        
178

    
179
        /**
180
         * @deprecated The regions of interest should not be persisted
181
         */
182
        public static void registerPersistence() {
183
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
184
                DynStruct definition = manager.getDefinition(PERSISTENT_NAME);
185
                if( definition == null ) {
186
                        definition = manager.addDefinition(
187
                                        VectorialROIImpl.class,
188
                                        PERSISTENT_NAME,
189
                                        PERSISTENT_DESCRIPTION,
190
                                        null, 
191
                                        null
192
                        );
193
                        definition.addDynFieldList("geometries").setClassOfItems(Geometry.class).setMandatory(false);
194
                        definition.addDynFieldObject("extent").setClassOfValue(Extent.class).setMandatory(true);
195
                }
196
        }
197
}