svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / operations / strategies / DefaultStrategy.java @ 3302
History | View | Annotate | Download (9.97 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.Graphics2D; |
44 |
import java.awt.geom.AffineTransform; |
45 |
import java.awt.geom.Point2D; |
46 |
import java.awt.geom.Rectangle2D; |
47 |
import java.awt.image.BufferedImage; |
48 |
import java.util.BitSet; |
49 |
|
50 |
import org.apache.log4j.Logger; |
51 |
import org.cresques.cts.ICoordTrans; |
52 |
|
53 |
import com.iver.cit.gvsig.fmap.DriverException; |
54 |
import com.iver.cit.gvsig.fmap.ViewPort; |
55 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
56 |
import com.iver.cit.gvsig.fmap.core.v02.FSymbol; |
57 |
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes; |
58 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
59 |
import com.iver.cit.gvsig.fmap.layers.FBitSet; |
60 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
61 |
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
62 |
import com.iver.cit.gvsig.fmap.layers.VectorialAdapter; |
63 |
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData; |
64 |
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial; |
65 |
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable; |
66 |
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer; |
67 |
import com.iver.cit.gvsig.fmap.operations.Cancellable; |
68 |
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend; |
69 |
import com.iver.cit.gvsig.fmap.rendering.styling.FStyle2D; |
70 |
|
71 |
|
72 |
/**
|
73 |
* Implementa la Strategy por defecto. Los m?todos que tendr?n en com?n la
|
74 |
* mayor parte de las estrategias
|
75 |
*/
|
76 |
public class DefaultStrategy implements Strategy { |
77 |
public static final int EQUALS = 0; |
78 |
public static final int DISJOINT = 1; |
79 |
public static final int INTERSECTS = 2; |
80 |
public static final int TOUCHES = 3; |
81 |
public static final int CROSSES = 4; |
82 |
public static final int WITHIN = 5; |
83 |
public static final int CONTAINS = 6; |
84 |
public static final int OVERLAPS = 7; |
85 |
|
86 |
private static Logger logger = Logger.getLogger(DefaultStrategy.class.getName()); |
87 |
FLayer capa = null;
|
88 |
|
89 |
/**
|
90 |
* Crea un nuevo DefaultStrategy.
|
91 |
*
|
92 |
* @param capa DOCUMENT ME!
|
93 |
*/
|
94 |
public DefaultStrategy(FLayer capa) {
|
95 |
this.capa = capa;
|
96 |
|
97 |
SingleLayer foo = (SingleLayer) capa; |
98 |
ClassifiableVectorial vectorial = (ClassifiableVectorial) capa; |
99 |
} |
100 |
|
101 |
/**
|
102 |
* @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByRect(java.awt.geom.Rectangle2D)
|
103 |
*/
|
104 |
public FBitSet queryByRect(Rectangle2D rect) throws DriverException { |
105 |
QueryByRectVisitor visitor = new QueryByRectVisitor();
|
106 |
|
107 |
visitor.setRect(rect); |
108 |
|
109 |
try {
|
110 |
process(visitor); |
111 |
} catch (VisitException e) {
|
112 |
throw new RuntimeException( |
113 |
"QueryByRectVisitor lanza una VisitException?");
|
114 |
} |
115 |
|
116 |
return visitor.getBitSet();
|
117 |
} |
118 |
|
119 |
/**
|
120 |
* @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByShape(com.iver.cit.gvsig.fmap.fshape.IGeometry,
|
121 |
* int)
|
122 |
*/
|
123 |
public FBitSet queryByShape(IGeometry g, int relationship) |
124 |
throws DriverException, VisitException {
|
125 |
QueryByShapeVisitor visitor = new QueryByShapeVisitor();
|
126 |
visitor.setRelationShip(relationship); |
127 |
visitor.setShape(g); |
128 |
process(visitor); |
129 |
|
130 |
return visitor.getBitSet();
|
131 |
} |
132 |
|
133 |
/**
|
134 |
* @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#getSelectionBounds()
|
135 |
*/
|
136 |
public Rectangle2D getSelectionBounds() { |
137 |
return null; |
138 |
} |
139 |
|
140 |
/**
|
141 |
* @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#createIndex()
|
142 |
*/
|
143 |
public void createIndex() { |
144 |
} |
145 |
|
146 |
/**
|
147 |
* @see com.iver.cit.gvsig.fmap.operations.LayerOperations#draw(java.awt.image.BufferedImage,
|
148 |
* java.awt.Graphics2D, FStyle2D)
|
149 |
*/
|
150 |
public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort, |
151 |
Cancellable cancel) throws DriverException {
|
152 |
try {
|
153 |
VectorialAdapter adapter = ((SingleLayer) capa).getSource(); |
154 |
ICoordTrans ct = getCapa().getCoordTrans(); |
155 |
logger.debug("adapter.start()");
|
156 |
adapter.start(); |
157 |
|
158 |
logger.debug("getCapa().getRecordset().start()");
|
159 |
SelectableDataSource rsSel = ((AlphanumericData) getCapa()).getRecordset(); |
160 |
if (rsSel != null) |
161 |
rsSel.start(); |
162 |
|
163 |
int sc;
|
164 |
Rectangle2D extent = viewPort.getAdjustedExtent();
|
165 |
AffineTransform at = viewPort.getAffineTransform();
|
166 |
|
167 |
sc = adapter.getShapeCount(); |
168 |
// TODO: A revisar si es o no conveniente este sistema
|
169 |
// de comunicaci?n con los drivers.
|
170 |
DriverAttributes attr = adapter.getDriverAttributes(); |
171 |
boolean bMustClone = false; |
172 |
if (attr != null) |
173 |
{ |
174 |
if (attr.isLoadedInMemory())
|
175 |
{ |
176 |
bMustClone = attr.isLoadedInMemory(); |
177 |
} |
178 |
} |
179 |
VectorialLegend l = (VectorialLegend) ((ClassifiableVectorial) capa).getLegend(); |
180 |
FBitSet bitSet = null;
|
181 |
if (getCapa() instanceof Selectable){ |
182 |
Selectable selection = (Selectable) getCapa(); |
183 |
bitSet = selection.getSelection(); |
184 |
} |
185 |
for (int i = 0; i < sc; i++) { |
186 |
if (cancel.isCanceled()) {
|
187 |
break;
|
188 |
} |
189 |
|
190 |
|
191 |
IGeometry geom = adapter.getShape(i); |
192 |
|
193 |
if (geom == null) { |
194 |
continue;
|
195 |
} |
196 |
|
197 |
if (ct != null) { |
198 |
if (bMustClone)
|
199 |
geom = geom.cloneGeometry(); |
200 |
geom.reProject(ct); |
201 |
} |
202 |
|
203 |
// if (geom.intersects(extent)) {
|
204 |
if (geom.fastIntersects(extent.getMinX(), extent.getMinY(),
|
205 |
extent.getWidth(), extent.getHeight())) { |
206 |
FSymbol symbol = l.getSymbol(i); |
207 |
|
208 |
if (symbol ==null) |
209 |
continue;
|
210 |
if (bitSet != null) |
211 |
if (bitSet.get(i)) {
|
212 |
symbol = FSymbol.getSymbolForSelection(symbol); |
213 |
} |
214 |
geom.draw(g, viewPort, symbol); |
215 |
} |
216 |
/* else
|
217 |
{
|
218 |
System.out.println("no pinto id=" + i);
|
219 |
} */
|
220 |
} |
221 |
|
222 |
logger.debug("getCapa().getRecordset().stop()");
|
223 |
if (rsSel != null) |
224 |
rsSel.stop(); |
225 |
|
226 |
|
227 |
logger.debug("adapter.stop()");
|
228 |
adapter.stop(); |
229 |
// TODO: A revisar si es o no conveniente este sistema
|
230 |
// de comunicaci?n con los drivers.
|
231 |
// DriverAttributes attr = adapter.getDriverAttributes();
|
232 |
/* if (attr != null)
|
233 |
{
|
234 |
if (attr.isLoadedInMemory())
|
235 |
{
|
236 |
// Quitamos lo de la reproyecci?n al vuelo para que
|
237 |
// solo se haga una vez.
|
238 |
getCapa().setCoordTrans(null);
|
239 |
}
|
240 |
} */
|
241 |
|
242 |
} catch (DriverIOException e) {
|
243 |
throw new DriverException(e); |
244 |
} catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
|
245 |
throw new DriverException(e); |
246 |
} catch (DriverException e) {
|
247 |
throw new DriverException(e); |
248 |
} |
249 |
} |
250 |
|
251 |
/**
|
252 |
* Devuelve la capa.
|
253 |
*
|
254 |
* @return Returns the capa.
|
255 |
*/
|
256 |
public FLayer getCapa() {
|
257 |
return capa;
|
258 |
} |
259 |
|
260 |
/**
|
261 |
* @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor,
|
262 |
* java.util.BitSet)
|
263 |
*/
|
264 |
public void process(FeatureVisitor visitor, BitSet subset) |
265 |
throws DriverException, VisitException {
|
266 |
try {
|
267 |
logger.debug("visitor.start()");
|
268 |
|
269 |
if (visitor.start(capa)) {
|
270 |
VectorialAdapter va = ((SingleLayer) capa).getSource(); |
271 |
|
272 |
va.start(); |
273 |
for (int i = 0; i < va.getShapeCount(); i++) { |
274 |
if (subset.get(i)) {
|
275 |
visitor.visit(va.getShape(i), i); |
276 |
} |
277 |
} |
278 |
va.stop(); |
279 |
|
280 |
logger.debug("visitor.stop()");
|
281 |
visitor.stop(capa); |
282 |
} |
283 |
} catch (DriverIOException e) {
|
284 |
throw new DriverException(e); |
285 |
} |
286 |
} |
287 |
|
288 |
/**
|
289 |
* DOCUMENT ME!
|
290 |
*
|
291 |
* @param visitor DOCUMENT ME!
|
292 |
*
|
293 |
* @throws DriverException DOCUMENT ME!
|
294 |
* @throws VisitException
|
295 |
*
|
296 |
* @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor)
|
297 |
*/
|
298 |
public void process(FeatureVisitor visitor) |
299 |
throws DriverException, VisitException {
|
300 |
try {
|
301 |
logger.debug("visitor.start()");
|
302 |
|
303 |
if (visitor.start(capa)) {
|
304 |
VectorialAdapter va = ((SingleLayer) capa).getSource(); |
305 |
ICoordTrans ct = getCapa().getCoordTrans(); |
306 |
va.start(); |
307 |
for (int i = 0; i < va.getShapeCount(); i++) { |
308 |
IGeometry geom = va.getShape(i); |
309 |
if (ct != null) { |
310 |
geom.reProject(ct); |
311 |
} |
312 |
|
313 |
visitor.visit(geom, i); |
314 |
} |
315 |
va.stop(); |
316 |
logger.debug("visitor.stop()");
|
317 |
visitor.stop(capa); |
318 |
} |
319 |
} catch (DriverIOException e) {
|
320 |
throw new DriverException(e); |
321 |
} |
322 |
} |
323 |
|
324 |
/**
|
325 |
* @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByPoint(Point2D,
|
326 |
* double)
|
327 |
*/
|
328 |
public FBitSet queryByPoint(Point2D p, double tolerance) |
329 |
throws DriverException {
|
330 |
// TODO: OJO!!!!. Est? implementado como un rectangulo.
|
331 |
// Lo correcto deber?a ser calculando las distancias reales
|
332 |
// es decir, con un c?rculo.
|
333 |
Rectangle2D recPoint = new Rectangle2D.Double(p.getX() - (tolerance / 2), |
334 |
p.getY() - (tolerance / 2), tolerance, tolerance);
|
335 |
return queryByRect(recPoint);
|
336 |
} |
337 |
|
338 |
/* (non-Javadoc)
|
339 |
* @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#print(java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort)
|
340 |
*/
|
341 |
public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel) |
342 |
throws DriverException {
|
343 |
draw(null, g, viewPort, cancel); // Quiero ejecutar el draw del padre, que es el que va sin acelaraci?n!! |
344 |
} |
345 |
} |