Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / operations / strategies / QueryByGeometryVisitor.java @ 28332

History | View | Annotate | Download (6.07 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 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
package com.iver.cit.gvsig.fmap.operations.strategies;
42

    
43
import java.awt.geom.Rectangle2D;
44

    
45
import org.geotools.resources.geometry.XRectangle2D;
46

    
47
import com.iver.cit.gvsig.exceptions.visitors.ProcessVisitorException;
48
import com.iver.cit.gvsig.exceptions.visitors.StartVisitorException;
49
import com.iver.cit.gvsig.exceptions.visitors.VisitorException;
50
import com.iver.cit.gvsig.fmap.core.IGeometry;
51
import com.iver.cit.gvsig.fmap.layers.FBitSet;
52
import com.iver.cit.gvsig.fmap.layers.FLayer;
53
import com.vividsolutions.jts.geom.Geometry;
54
import com.vividsolutions.jts.geom.IntersectionMatrix;
55

    
56

    
57
public class QueryByGeometryVisitor implements FeatureVisitor {
58

    
59
        private Geometry geom = null;
60
    private IGeometry geom_gvSIG = null;
61
    private Rectangle2D geomBounds;
62
        private int relation;
63
    private FBitSet bitset = null;
64
        public static final int EQUALS = 0;
65
        public static final int DISJOINT = 1;
66
        public static final int INTERSECTS = 2;
67
        public static final int TOUCHES = 3;
68
        public static final int CROSSES = 4;
69
        public static final int WITHIN = 5;
70
        public static final int CONTAINS = 6;
71
        public static final int OVERLAPS = 7;
72

    
73
    private Geometry getJTSgeom()
74
    {
75
        if (geom == null)
76
            geom = geom_gvSIG.toJTSGeometry();
77
        return geom;
78
    }
79

    
80
        /**
81
         *
82
         */
83
        public QueryByGeometryVisitor(IGeometry geom, int relation) {
84
        this.geomBounds = geom.getBounds2D();
85
                // this.geom = geom.toJTSGeometry();
86
        this.geom_gvSIG = geom;
87
                this.relation = relation;
88
        }
89

    
90
        /**
91
         * @see com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor#visit(com.iver.cit.gvsig.fmap.core.IGeometry, int)
92
         */
93
        public void visit(IGeometry g, int index) throws VisitorException, ProcessVisitorException {
94
        Geometry g1;
95
        IntersectionMatrix m;
96
        switch (relation) {
97
        case CONTAINS:
98
                if (XRectangle2D.intersectInclusive(geomBounds, g.getBounds2D())) {
99
                        g1 = g.toJTSGeometry();
100
                        m = getJTSgeom().relate(g1);
101

    
102
                        if (m.isContains()) {
103
                                bitset.set(index, true);
104
                        }
105
                }
106
                break;
107

    
108
        case CROSSES:
109
                if (XRectangle2D.intersectInclusive(geomBounds, g.getBounds2D()))
110
                {
111
                        g1 = g.toJTSGeometry();
112
                        m = getJTSgeom().relate(g1);
113

    
114
                        if (m.isCrosses(g1.getDimension(), geom.getDimension())) {
115
                                bitset.set(index, true);
116
                        }
117
                }
118
                break;
119

    
120
        case DISJOINT:
121
                // TODO: POR OPTIMIZAR
122
                g1 = g.toJTSGeometry();
123
                m = getJTSgeom().relate(g1);
124
                if (m.isDisjoint()) {
125
                        bitset.set(index, true);
126
                }
127
                break;
128

    
129
        case EQUALS:
130
                // TODO: REVISAR ESTO PARA QUE COMPRUEBE SI SON IGUALES LOS RECTANGULOS
131
                // Y DE PASO, COMPROBAR SI HACE FALTA USAR ESTA FUNCI?N DONDE
132
                // SE USE EL Rectangle2D.contains
133
                if (XRectangle2D.containsInclusive(geomBounds, g.getBounds2D()))
134
                {
135
                        g1 = g.toJTSGeometry();
136
                        m = getJTSgeom().relate(g1);
137
                        if (m.isEquals(g1.getDimension(), getJTSgeom().getDimension())) {
138
                                bitset.set(index, true);
139
                        }
140
                }
141
                break;
142

    
143
        case INTERSECTS:
144
                if (XRectangle2D.intersectInclusive(geomBounds, g.getBounds2D()))
145
                {
146
                        g1 = g.toJTSGeometry();
147
                        m = getJTSgeom().relate(g1);
148

    
149
                        if (m.isIntersects()) {
150
                                bitset.set(index, true);
151
                        }
152
                }
153
                break;
154

    
155
        case OVERLAPS:
156
                if (XRectangle2D.intersectInclusive(geomBounds, g.getBounds2D()))
157
                {
158
                        g1 = g.toJTSGeometry();
159
                        m = getJTSgeom().relate(g1);
160
                        if (m.isOverlaps(g1.getDimension(),
161
                                        geom.getDimension())) {
162
                                bitset.set(index, true);
163
                        }
164
                }
165

    
166
                break;
167

    
168
        case TOUCHES:
169
                if (XRectangle2D.intersectInclusive(geomBounds, g.getBounds2D()))
170
                {
171
                        g1 = g.toJTSGeometry();
172
                        m = getJTSgeom().relate(g1);
173
                        if (m.isTouches(g1.getDimension(), geom.getDimension())) {
174
                                bitset.set(index, true);
175
                        }
176
                }
177

    
178
                break;
179

    
180
        case WITHIN:
181
                if (XRectangle2D.intersectInclusive(geomBounds, g.getBounds2D())) {
182
                        g1 = g.toJTSGeometry();
183
                        m = getJTSgeom().relate(g1);
184

    
185
                        if (m.isWithin()) {
186
                                bitset.set(index, true);
187
                        }
188
                }
189

    
190
                break;
191
        }
192
        }
193

    
194
        /**
195
         * @see com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor#stop(com.iver.cit.gvsig.fmap.layers.FLayer)
196
         */
197
        public void stop(FLayer layer) throws VisitorException {
198
        }
199

    
200
        /**
201
         * @see com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor#start(com.iver.cit.gvsig.fmap.layers.FLayer)
202
         */
203
        public boolean start(FLayer layer) throws StartVisitorException {
204
        bitset = new FBitSet();
205
                return true;
206
        }
207

    
208
        public FBitSet getBitSet() {
209
                return bitset;
210
        }
211

    
212
        public String getProcessDescription() {
213
                return "Looking for features by geometric criteria";
214
        }
215
}