root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / GraphicLayer.java @ 8208
History | View | Annotate | Download (7.1 KB)
1 |
/*
|
---|---|
2 |
* Created on 19-sep-2005
|
3 |
*
|
4 |
* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
5 |
*
|
6 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
7 |
*
|
8 |
* This program is free software; you can redistribute it and/or
|
9 |
* modify it under the terms of the GNU General Public License
|
10 |
* as published by the Free Software Foundation; either version 2
|
11 |
* of the License, or (at your option) any later version.
|
12 |
*
|
13 |
* This program is distributed in the hope that it will be useful,
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
* GNU General Public License for more details.
|
17 |
*
|
18 |
* You should have received a copy of the GNU General Public License
|
19 |
* along with this program; if not, write to the Free Software
|
20 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
21 |
*
|
22 |
* For more information, contact:
|
23 |
*
|
24 |
* Generalitat Valenciana
|
25 |
* Conselleria d'Infraestructures i Transport
|
26 |
* Av. Blasco Ib??ez, 50
|
27 |
* 46010 VALENCIA
|
28 |
* SPAIN
|
29 |
*
|
30 |
* +34 963862235
|
31 |
* gvsig@gva.es
|
32 |
* www.gvsig.gva.es
|
33 |
*
|
34 |
* or
|
35 |
*
|
36 |
* IVER T.I. S.A
|
37 |
* Salamanca 50
|
38 |
* 46005 Valencia
|
39 |
* Spain
|
40 |
*
|
41 |
* +34 963163400
|
42 |
* dac@iver.es
|
43 |
*/
|
44 |
package com.iver.cit.gvsig.fmap.layers; |
45 |
|
46 |
import java.awt.Graphics2D; |
47 |
import java.awt.geom.Rectangle2D; |
48 |
import java.awt.image.BufferedImage; |
49 |
import java.util.ArrayList; |
50 |
|
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.ISymbol; |
57 |
import com.iver.cit.gvsig.fmap.core.v02.FGraphicUtilities; |
58 |
import com.iver.cit.gvsig.fmap.rendering.FGraphic; |
59 |
import com.iver.utiles.swing.threads.Cancellable; |
60 |
import com.vividsolutions.jts.index.ItemVisitor; |
61 |
import com.vividsolutions.jts.index.SpatialIndex; |
62 |
import com.vividsolutions.jts.index.quadtree.Quadtree; |
63 |
|
64 |
public class GraphicLayer extends FLyrDefault { |
65 |
private ArrayList graphics = new ArrayList(); |
66 |
private ArrayList symbols = new ArrayList(); |
67 |
private Rectangle2D fullExtent; |
68 |
private SpatialIndex spatialIndex = new Quadtree(); |
69 |
private FBitSet selection = new FBitSet(); |
70 |
|
71 |
public GraphicLayer() {
|
72 |
super();
|
73 |
System.err.println("GraphicLayer: Constructor"); |
74 |
} |
75 |
|
76 |
/* (non-Javadoc)
|
77 |
* @see com.iver.cit.gvsig.fmap.layers.FLayer#getFullExtent()
|
78 |
*/
|
79 |
public Rectangle2D getFullExtent() throws DriverException { |
80 |
return fullExtent;
|
81 |
} |
82 |
|
83 |
/* (non-Javadoc)
|
84 |
* @see com.iver.cit.gvsig.fmap.layers.FLayer#draw(java.awt.image.BufferedImage, java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort, com.iver.cit.gvsig.fmap.operations.Cancellable, double)
|
85 |
*/
|
86 |
public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale) throws DriverException { |
87 |
if (isVisible() && isWithinScale(scale)){
|
88 |
drawGraphics(image, g, viewPort, cancel); |
89 |
} |
90 |
} |
91 |
|
92 |
/**
|
93 |
* Generic visitor pattern. It may be used for queryByRect, byPoint,
|
94 |
* by polygon, etc.
|
95 |
* @param visitor
|
96 |
* @param cancel
|
97 |
*/
|
98 |
public void process(ItemVisitor visitor, Cancellable cancel) |
99 |
{ |
100 |
int numReg;
|
101 |
|
102 |
for (numReg = 0; numReg < graphics.size(); numReg++) { |
103 |
if (cancel.isCanceled()) {
|
104 |
break;
|
105 |
} |
106 |
|
107 |
FGraphic theGraphic = (FGraphic) graphics.get(numReg); |
108 |
visitor.visitItem(theGraphic); |
109 |
} |
110 |
|
111 |
} |
112 |
|
113 |
/* (non-Javadoc)
|
114 |
* @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort, com.iver.cit.gvsig.fmap.operations.Cancellable, double)
|
115 |
*/
|
116 |
public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale) throws DriverException { |
117 |
if (isVisible() && isWithinScale(scale)){
|
118 |
drawGraphics(null, g, viewPort, cancel);
|
119 |
} |
120 |
} |
121 |
|
122 |
private void drawGraphics(BufferedImage image, Graphics2D g, |
123 |
ViewPort viewPort, Cancellable cancel) |
124 |
{ |
125 |
int numReg;
|
126 |
Rectangle2D elExtent = viewPort.getAdjustedExtent();
|
127 |
if (elExtent == null) return; |
128 |
|
129 |
//int anchoMapa;
|
130 |
//int altoMapa;
|
131 |
//double anchoReal;
|
132 |
//double altoReal;
|
133 |
//double escala;
|
134 |
ISymbol theSymbol = null;
|
135 |
// System.out.println("Dibujando gr?ficos...(" + graphics.size() + ")");
|
136 |
|
137 |
ICoordTrans ct = getCoordTrans(); |
138 |
|
139 |
for (numReg = 0; numReg < graphics.size(); numReg++) { |
140 |
if (cancel.isCanceled()) {
|
141 |
break;
|
142 |
} |
143 |
|
144 |
FGraphic theGraphic = (FGraphic) graphics.get(numReg); |
145 |
IGeometry geom = theGraphic.getGeom(); |
146 |
|
147 |
// Modificaci?n para Jorge, para que le reproyecte los gr?ficos.
|
148 |
if (ct != null) { |
149 |
geom = geom.cloneGeometry(); |
150 |
geom.reProject(ct); |
151 |
} |
152 |
|
153 |
if (geom.fastIntersects(elExtent.getMinX(),
|
154 |
elExtent.getMinY(), elExtent.getWidth(), elExtent.getHeight())) |
155 |
{ |
156 |
theSymbol = (ISymbol) symbols.get(theGraphic.getIdSymbol()); |
157 |
if (selection.get(numReg)) // Si est? seleccinado |
158 |
{ |
159 |
FGraphicUtilities.DrawHandlers(g, viewPort.getAffineTransform(), geom.getHandlers(IGeometry.SELECTHANDLER)); |
160 |
} |
161 |
else
|
162 |
{ |
163 |
theGraphic.draw(g, viewPort, theSymbol); |
164 |
} |
165 |
} |
166 |
} |
167 |
} |
168 |
|
169 |
public int addSymbol(ISymbol newSymbol) |
170 |
{ |
171 |
if (symbols.add(newSymbol))
|
172 |
return symbols.size()-1; |
173 |
return -1; |
174 |
|
175 |
} |
176 |
/**
|
177 |
* Add a graphic to the graphic layer.
|
178 |
* @param g
|
179 |
*/
|
180 |
public void addGraphic(FGraphic g) |
181 |
{ |
182 |
if (graphics.add(g))
|
183 |
{ |
184 |
|
185 |
// spatialIndex.insert(g.getGeom().getBounds2D())
|
186 |
if (fullExtent == null) { |
187 |
fullExtent = g.getGeom().getBounds2D(); |
188 |
} else {
|
189 |
fullExtent.add(g.getGeom().getBounds2D()); |
190 |
} |
191 |
|
192 |
// return graphics.size()-1;
|
193 |
} |
194 |
// return -1;
|
195 |
|
196 |
} |
197 |
|
198 |
public void insertGraphic(int position, FGraphic g) { |
199 |
graphics.add(position, g); |
200 |
if (fullExtent == null) { |
201 |
fullExtent = g.getGeom().getBounds2D(); |
202 |
} else {
|
203 |
fullExtent.add(g.getGeom().getBounds2D()); |
204 |
} |
205 |
} |
206 |
|
207 |
/**
|
208 |
*
|
209 |
*/
|
210 |
public void clearAllGraphics() |
211 |
{ |
212 |
System.err.println("Clear all graphics. Antes hab?a " + graphics.size()); |
213 |
graphics.clear(); |
214 |
} |
215 |
/**
|
216 |
*
|
217 |
*/
|
218 |
public void clearSymbolsGraphics() |
219 |
{ |
220 |
symbols.clear(); |
221 |
} |
222 |
|
223 |
public FBitSet getSelection() {
|
224 |
return selection;
|
225 |
} |
226 |
|
227 |
public void setSelection(FBitSet selection) { |
228 |
this.selection = selection;
|
229 |
} |
230 |
|
231 |
public FBitSet queryByRect(Rectangle2D r) { |
232 |
FBitSet newSel = new FBitSet();
|
233 |
for (int numReg = 0; numReg < graphics.size(); numReg++) { |
234 |
|
235 |
FGraphic theGraphic = (FGraphic) graphics.get(numReg); |
236 |
IGeometry geom = theGraphic.getGeom(); |
237 |
if (geom.intersects(r))
|
238 |
{ |
239 |
newSel.set(numReg); |
240 |
} |
241 |
} |
242 |
return newSel;
|
243 |
} |
244 |
|
245 |
} |